Skip to content

Commit 6701841

Browse files
committed
bugfix: 修复了线程池析构的时候,会出现卡顿的问题
1 parent 7b2d2e5 commit 6701841

26 files changed

Lines changed: 531 additions & 369 deletions
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
/***************************
22
@Author: Chunel
33
@Contact: chunel@foxmail.com
4-
@File: UAllocator.h
5-
@Time: 2021/10/28 9:15 下午
4+
@File: CAllocator.h
5+
@Time: 2024/11/23 21:54
66
@Desc:
77
***************************/
88

9-
#ifndef CGRAPH_UALLOCATOR_H
10-
#define CGRAPH_UALLOCATOR_H
9+
#ifndef CGRAPH_CALLOCATOR_H
10+
#define CGRAPH_CALLOCATOR_H
1111

12+
13+
#include <new>
1214
#include <mutex>
1315
#include <memory>
1416

15-
#include "../CBasic/CBasicInclude.h"
17+
#include "CObject.h"
18+
#include "CStruct.h"
19+
#include "CStdEx.h"
1620

1721
CGRAPH_NAMESPACE_BEGIN
1822

19-
/**
20-
* 仅用于生成CObject类型的类
21-
*/
22-
class UAllocator : public CObject {
23+
class CAllocator {
2324
public:
2425
/**
2526
* 生成一个 CObject 对象
2627
* @tparam T
27-
* @return
28+
* @return T*
2829
*/
2930
template<typename T,
3031
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
@@ -35,7 +36,7 @@ class UAllocator : public CObject {
3536
/**
3637
* 生成一个 CStruct 的对象
3738
* @tparam T
38-
* @return
39+
* @return T*
3940
*/
4041
template<typename T,
4142
c_enable_if_t<std::is_base_of<CStruct, T>::value, int> = 0>
@@ -48,23 +49,22 @@ class UAllocator : public CObject {
4849
* @tparam T
4950
* @tparam Args
5051
* @param args
51-
* @return
52+
* @return T*
5253
*/
5354
template<typename T, typename ...Args,
5455
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
55-
static T* safeMallocTemplateCObject(Args... args) {
56-
T* ptr = nullptr;
57-
while (!ptr) {
58-
ptr = new T(std::forward<Args>(args)...);
56+
static T* safeMallocTemplateCObject(Args&&... args) {
57+
T* result = nullptr;
58+
while (!result) {
59+
result = new(std::nothrow) T(std::forward<Args&&>(args)...);
5960
}
60-
return ptr;
61+
return result;
6162
}
6263

63-
6464
/**
6565
* 生成unique智能指针信息
6666
* @tparam T
67-
* @return
67+
* @return std::unique_ptr<T>
6868
*/
6969
template<typename T,
7070
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
@@ -76,7 +76,7 @@ class UAllocator : public CObject {
7676
/**
7777
* 生成T类型的对象
7878
* @tparam T
79-
* @return
79+
* @return T*
8080
*/
8181
template<class T>
8282
static T* safeMalloc() {
@@ -90,11 +90,12 @@ class UAllocator : public CObject {
9090

9191

9292
#define CGRAPH_SAFE_MALLOC_COBJECT(Type) \
93-
UAllocator::safeMallocCObject<Type>(); \
93+
CAllocator::safeMallocCObject<Type>(); \
9494

9595
#define CGRAPH_MAKE_UNIQUE_COBJECT(Type) \
96-
UAllocator::makeUniqueCObject<Type>(); \
96+
CAllocator::makeUniqueCObject<Type>(); \
9797

9898
CGRAPH_NAMESPACE_END
9999

100-
#endif //CGRAPH_UALLOCATOR_H
100+
101+
#endif //CGRAPH_CALLOCATOR_H

src/CBasic/CBasicInclude.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
#include "CStdEx.h"
2020
#include "CDescInfo.h"
2121
#include "CStruct.h"
22+
#include "CAllocator.h"
2223

2324
#endif //CGRAPH_CBASICINCLUDE_H

src/CBasic/CDescInfo.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class CDescInfo {
2121
* 获取名称信息
2222
* @return
2323
*/
24-
const std::string& getName() const {
25-
return name_;
24+
virtual const std::string& getName() const {
25+
return name_.empty() ? session_ : name_;
2626
}
2727

2828
/**
@@ -45,9 +45,13 @@ class CDescInfo {
4545
* 设置名称信息
4646
* @param name
4747
* @return
48+
* @notice 部分windows编译器不支持函数继承改变返回值类型,故做此区分
4849
*/
49-
virtual auto setName(const std::string& name)
50-
-> decltype(this) {
50+
#ifdef _WIN32
51+
virtual CVoidPtr setName(const std::string& name) {
52+
#else
53+
virtual auto setName(const std::string& name) -> decltype(this) {
54+
#endif
5155
name_ = name;
5256
return this;
5357
}

src/CBasic/CFuncType.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,17 @@ enum class CFunctionType {
5252
+ " | line = [" + ::std::to_string( __LINE__) + "]")
5353

5454

55-
/** 生成一个包含异常位置的 CStatus
56-
* 这里这样实现,是为了符合 CStatus 类似写法
57-
* */
58-
#define CErrStatus(info) \
59-
CStatus(info, CGRAPH_GET_LOCATE) \
60-
6155
/** 返回异常信息和状态 */
6256
#define CGRAPH_RETURN_ERROR_STATUS(info) \
63-
return CErrStatus(info); \
57+
return CStatus(info); \
6458

6559
/** 根据条件判断是否返回错误状态 */
6660
#define CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(cond, info) \
6761
if (unlikely(cond)) { CGRAPH_RETURN_ERROR_STATUS(info); } \
6862

6963
/** 不支持当前功能 */
7064
#define CGRAPH_NO_SUPPORT \
71-
return CErrStatus(CGRAPH_FUNCTION_NO_SUPPORT); \
65+
return CStatus(CGRAPH_FUNCTION_NO_SUPPORT); \
7266

7367
/** 定义为不能赋值和拷贝的对象类型 */
7468
#define CGRAPH_NO_ALLOWED_COPY(CType) \
@@ -82,14 +76,12 @@ enum class CFunctionType {
8276
/** 在异常状态的情况下,抛出异常 */
8377
#define CGRAPH_THROW_EXCEPTION_BY_STATUS(status) \
8478
if (unlikely((status).isErr())) { \
85-
CGRAPH_THROW_EXCEPTION((status).getInfo()); \
86-
} \
79+
CGRAPH_THROW_EXCEPTION((status).getInfo()); } \
8780

8881
/** 根据条件判断是否抛出异常 */
8982
#define CGRAPH_THROW_EXCEPTION_BY_CONDITION(cond, info) \
9083
if (unlikely(cond)) { CGRAPH_THROW_EXCEPTION(info); } \
9184

92-
9385
CGRAPH_NAMESPACE_END
9486

9587
#endif //CGRAPH_CFUNCTYPE_H

src/CBasic/CStatus.h

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,44 @@ CGRAPH_NAMESPACE_BEGIN
2525
static const int STATUS_OK = 0; /** 正常流程返回值 */
2626
static const int STATUS_ERR = -1; /** 异常流程返回值 */
2727
static const int STATUS_CRASH = -996; /** 异常流程返回值 */
28-
static const char* STATUS_ERROR_INFO_CONNECTOR = " && "; /** 多异常信息连接符号 */
2928

3029
class CSTATUS {
3130
public:
3231
explicit CSTATUS() = default;
3332

34-
explicit CSTATUS(const std::string &errorInfo,
35-
const std::string &locateInfo = CGRAPH_EMPTY) {
33+
explicit CSTATUS(const std::string &errorInfo) {
3634
this->error_code_ = STATUS_ERR; // 默认的error code信息
3735
this->error_info_ = errorInfo;
38-
this->error_locate_ = locateInfo;
3936
}
4037

41-
explicit CSTATUS(int errorCode, const std::string &errorInfo,
42-
const std::string &locateInfo = CGRAPH_EMPTY) {
38+
explicit CSTATUS(int errorCode, const std::string &errorInfo) {
4339
this->error_code_ = errorCode;
4440
this->error_info_ = errorInfo;
45-
this->error_locate_ = locateInfo;
4641
}
4742

4843
CSTATUS(const CSTATUS &status) {
49-
if (status.isOK()) {
44+
if (status.error_code_ == error_code_) {
5045
return;
5146
}
5247

5348
this->error_code_ = status.error_code_;
5449
this->error_info_ = status.error_info_;
55-
this->error_locate_ = status.error_locate_;
5650
}
5751

5852
CSTATUS(const CSTATUS &&status) noexcept {
59-
if (status.isOK()) {
53+
if (status.error_code_ == error_code_) {
6054
return;
6155
}
6256

6357
this->error_code_ = status.error_code_;
6458
this->error_info_ = status.error_info_;
65-
this->error_locate_ = status.error_locate_;
6659
}
6760

6861
CSTATUS& operator=(const CSTATUS& status) {
69-
if (!status.isOK()) {
62+
if (this->error_code_ != status.error_code_) {
7063
// 如果status是正常的话,则所有数据保持不变
7164
this->error_code_ = status.error_code_;
7265
this->error_info_ = status.error_info_;
73-
this->error_locate_ = status.error_locate_;
7466
}
7567
return (*this);
7668
}
@@ -83,12 +75,21 @@ class CSTATUS {
8375
if (!this->isErr() && cur.isErr()) {
8476
this->error_code_ = cur.error_code_;
8577
this->error_info_ = cur.error_info_;
86-
this->error_locate_ = cur.error_locate_;
8778
}
8879

8980
return (*this);
9081
}
9182

83+
/**
84+
* 恢复状态信息
85+
*/
86+
void reset() {
87+
if (this->error_code_ != STATUS_OK) {
88+
this->error_code_ = STATUS_OK;
89+
this->error_info_.clear();
90+
}
91+
}
92+
9293
/**
9394
* 获取异常值信息
9495
* @return
@@ -105,14 +106,6 @@ class CSTATUS {
105106
return this->error_info_;
106107
}
107108

108-
/**
109-
* 获取报错位置
110-
* @return
111-
*/
112-
const std::string& getLocate() const {
113-
return this->error_locate_;
114-
}
115-
116109
/**
117110
* 判断当前状态是否可行
118111
* @return
@@ -129,6 +122,14 @@ class CSTATUS {
129122
return error_code_ < STATUS_OK; // 约定异常信息,均为负值
130123
}
131124

125+
/**
126+
* 没有异常。包含 > 0 的警告类型
127+
* @return
128+
*/
129+
bool isNotErr() const {
130+
return error_code_ >= STATUS_OK;
131+
}
132+
132133
/**
133134
* 判断当前状态是否是崩溃了
134135
* @return
@@ -137,6 +138,29 @@ class CSTATUS {
137138
return STATUS_CRASH == error_code_;
138139
}
139140

141+
/**
142+
* 设置异常信息
143+
* @param code
144+
* @param info
145+
* @return
146+
*/
147+
CSTATUS* setInfo(int code, const std::string& info) {
148+
error_code_ = code;
149+
error_info_ = (STATUS_OK == error_code_) ? CGRAPH_EMPTY : info;
150+
return this;
151+
}
152+
153+
/**
154+
* 设置异常信息
155+
* @param info
156+
* @return
157+
*/
158+
CSTATUS* setErrorInfo(const std::string& info) {
159+
error_code_ = STATUS_ERR;
160+
error_info_ = info;
161+
return this;
162+
}
163+
140164
private:
141165
int error_code_ = STATUS_OK; // 错误码信息
142166
std::string error_info_; // 错误信息描述

src/CBasic/CStrDefine.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@
99
#ifndef CGRAPH_CSTRDEFINE_H
1010
#define CGRAPH_CSTRDEFINE_H
1111

12-
#include <string>
13-
1412
#include "CBasicDefine.h"
1513

1614
CGRAPH_NAMESPACE_BEGIN
1715

18-
static const std::string& CGRAPH_EMPTY = "";
19-
static const std::string& CGRAPH_DEFAULT = "default";
20-
static const std::string& CGRAPH_UNKNOWN = "unknown";
21-
static const std::string& CGRAPH_BASIC_EXCEPTION = "CGraph default exception";
22-
static const std::string& CGRAPH_FUNCTION_NO_SUPPORT = "CGraph function no support";
23-
static const std::string& CGRAPH_INPUT_IS_NULL = "input is nullptr";
16+
static const char* CGRAPH_EMPTY = "";
17+
static const char* CGRAPH_DEFAULT = "default";
18+
static const char* CGRAPH_UNKNOWN = "unknown";
19+
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "function no support";
20+
static const char* CGRAPH_INPUT_IS_NULL = "input is nullptr";
2421

2522
CGRAPH_NAMESPACE_END
2623

src/CBasic/CStruct.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ CGRAPH_NAMESPACE_BEGIN
1515

1616
/**
1717
* 所有框架内部结构体定义的基类
18-
* 仅针对类似 bean 数据类型的定义
18+
* 仅针对类似 pod 数据类型的定义
1919
*/
20-
class CStruct {
20+
struct CStruct {
21+
virtual ~CStruct() = default;
2122
};
2223

2324
CGRAPH_NAMESPACE_END

src/CBasic/CValType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "CException.h"
1515

1616
using CChar = CTP::CCHAR;
17-
using CUint = CTP::CUINT;
17+
using CUInt = CTP::CUINT;
1818
using CSize = CTP::CSIZE;
1919
using CVoid = CTP::CVOID;
2020
using CVoidPtr = CTP::CVOID *;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***************************
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: UCvMutex.h
5+
@Time: 2024/12/13 20:27
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_UCVMUTEX_H
10+
#define CGRAPH_UCVMUTEX_H
11+
12+
#include <mutex>
13+
#include <condition_variable>
14+
15+
#include "../UThreadObject.h"
16+
17+
CGRAPH_NAMESPACE_BEGIN
18+
19+
struct UCvMutex : public UThreadObject {
20+
std::mutex mtx_ {};
21+
std::condition_variable cv_ {};
22+
};
23+
24+
CGRAPH_NAMESPACE_END
25+
26+
#endif //CGRAPH_UCVMUTEX_H

0 commit comments

Comments
 (0)