Browse Source

feat: 优化针对windows系统的支持,优化针对C++11版本的支持

ChunelFeng 2 years ago
parent
commit
9d1b19c0f1
33 changed files with 401 additions and 153 deletions
  1. +1
    -1
      CMakeLists.txt
  2. +3
    -0
      README.md
  3. +2
    -1
      src/CBasic/CBasicInclude.h
  4. +1
    -1
      src/CBasic/CException.h
  5. +1
    -1
      src/CBasic/CFuncType.h
  6. +0
    -22
      src/CBasic/CInfoDefine.h
  7. +1
    -1
      src/CBasic/CObject.h
  8. +32
    -0
      src/CBasic/CStatus.h
  9. +29
    -0
      src/CBasic/CStdEx.h
  10. +24
    -0
      src/CBasic/CStrDefine.h
  11. +14
    -0
      src/UtilsCtrl/ThreadPool/Lock/ULockInclude.h
  12. +51
    -0
      src/UtilsCtrl/ThreadPool/Lock/USpinLock.h
  13. +1
    -1
      src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h
  14. +2
    -1
      src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h
  15. +129
    -0
      src/UtilsCtrl/ThreadPool/Queue/UAtomicRingBufferQueue.h
  16. +1
    -0
      src/UtilsCtrl/ThreadPool/Queue/UQueueInclude.h
  17. +0
    -1
      src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h
  18. +16
    -15
      src/UtilsCtrl/ThreadPool/Queue/UWorkStealingQueue.h
  19. +2
    -3
      src/UtilsCtrl/ThreadPool/Task/UTask.h
  20. +2
    -3
      src/UtilsCtrl/ThreadPool/Thread/UThreadBase.h
  21. +2
    -2
      src/UtilsCtrl/ThreadPool/Thread/UThreadPrimary.h
  22. +0
    -1
      src/UtilsCtrl/ThreadPool/UThreadObject.h
  23. +29
    -4
      src/UtilsCtrl/ThreadPool/UThreadPool.cpp
  24. +11
    -1
      src/UtilsCtrl/ThreadPool/UThreadPool.h
  25. +1
    -0
      src/UtilsCtrl/ThreadPool/UThreadPool.inl
  26. +10
    -7
      src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h
  27. +1
    -0
      src/UtilsCtrl/ThreadPool/UThreadPoolInclude.h
  28. +20
    -29
      src/UtilsCtrl/UAllocator.h
  29. +0
    -54
      src/UtilsCtrl/UMemory.h
  30. +12
    -2
      src/UtilsCtrl/UtilsDefine.h
  31. +2
    -0
      src/UtilsCtrl/UtilsFunction.h
  32. +0
    -1
      src/UtilsCtrl/UtilsObject.h
  33. +1
    -1
      tutorial.cpp

+ 1
- 1
CMakeLists.txt View File

@ -9,7 +9,7 @@ message("* * * * * * * * * * * * * * * * *")
cmake_minimum_required(VERSION 3.2.5)
project(CThreadPool VERSION 1.1.0)
project(CThreadPool VERSION 1.2.0)
set(CMAKE_CXX_STANDARD 11)

+ 3
- 0
README.md View File

@ -80,6 +80,9 @@ int main() {
[2022.10.11 - v1.1.0 - [MirrorYuChen](https://github.com/MirrorYuChen)]
* 提供针对C++11版本的支持
[2023.03.07 - v1.2.0 - Chunel]
* 优化windows版本功能
------------
#### 附录-2. 联系方式
* 微信: ChunelFeng

+ 2
- 1
src/CBasic/CBasicInclude.h View File

@ -15,6 +15,7 @@
#include "CStatus.h"
#include "CException.h"
#include "CBasicDefine.h"
#include "CInfoDefine.h"
#include "CStrDefine.h"
#include "CStdEx.h"
#endif //CGRAPH_CBASICINCLUDE_H

+ 1
- 1
src/CBasic/CException.h View File

@ -12,7 +12,7 @@
#include <string>
#include <exception>
#include "CInfoDefine.h"
#include "CStrDefine.h"
CGRAPH_NAMESPACE_BEGIN

+ 1
- 1
src/CBasic/CFuncType.h View File

@ -11,7 +11,7 @@
#include <functional>
#include "CInfoDefine.h"
#include "CStrDefine.h"
#include "CValType.h"
CGRAPH_NAMESPACE_BEGIN

+ 0
- 22
src/CBasic/CInfoDefine.h View File

@ -1,22 +0,0 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CInfoDefine.h
@Time: 2022/4/16 14:01
@Desc:
***************************/
#ifndef CGRAPH_CINFODEFINE_H
#define CGRAPH_CINFODEFINE_H
#include "CBasicDefine.h"
CGRAPH_NAMESPACE_BEGIN
static const char* CGRAPH_EMPTY = "";
static const char* CGRAPH_BASIC_EXCEPTION = "CGraph Exception";
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "function no support";
CGRAPH_NAMESPACE_END
#endif //CGRAPH_CINFODEFINE_H

+ 1
- 1
src/CBasic/CObject.h View File

@ -35,7 +35,7 @@ public:
virtual CStatus run() = 0;
/**
* deinit函数
*
*/
virtual CStatus destroy() {
CGRAPH_EMPTY_FUNCTION

+ 32
- 0
src/CBasic/CStatus.h View File

@ -12,9 +12,17 @@
#include <string>
#include "CBasicDefine.h"
#include "CStrDefine.h"
CGRAPH_NAMESPACE_BEGIN
/**
*
* 0
* warning逻辑
* error逻辑
*
*/
static const int STATUS_OK = 0; /** 正常流程返回值 */
static const int STATUS_ERR = -1; /** 异常流程返回值 */
static const char* STATUS_ERROR_INFO_CONNECTOR = " && "; /** 多异常信息连接符号 */
@ -79,6 +87,14 @@ public:
}
/**
*
*/
void reset() {
error_code_ = STATUS_OK;
error_info_ = CGRAPH_EMPTY;
}
/**
*
* @return
*/
@ -94,6 +110,22 @@ public:
return error_code_ < STATUS_OK; //
}
/**
*
* @return
*/
[[nodiscard]] bool isNotErr() const {
return error_code_ >= STATUS_OK;
}
/**
* ok的error warning
* @return
*/
[[nodiscard]] bool isNotOK() const {
return error_code_ != STATUS_OK;
}
private:
int error_code_ { STATUS_OK }; //
std::string error_info_; //

+ 29
- 0
src/CBasic/CStdEx.h View File

@ -0,0 +1,29 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CStdEx.h
@Time: 2023/1/31 23:15
@Desc:
***************************/
#ifndef CGRAPH_CSTDEX_H
#define CGRAPH_CSTDEX_H
#include <memory>
#include <type_traits>
CGRAPH_NAMESPACE_BEGIN
// std::enable_if_t
template<bool B, typename T = void>
using c_enable_if_t = typename std::enable_if<B, T>::type;
// std::make_unique
template<typename T, typename... Args>
typename std::unique_ptr<T> c_make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
CGRAPH_NAMESPACE_END
#endif //CGRAPH_CSTDEX_H

+ 24
- 0
src/CBasic/CStrDefine.h View File

@ -0,0 +1,24 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: CInfoDefine.h
@Time: 2022/4/16 14:01
@Desc:
***************************/
#ifndef CGRAPH_CSTRDEFINE_H
#define CGRAPH_CSTRDEFINE_H
#include "CBasicDefine.h"
CGRAPH_NAMESPACE_BEGIN
static const char* CGRAPH_EMPTY = "";
static const char* CGRAPH_DEFAULT = "default";
static const char* CGRAPH_UNKNOWN = "unknown";
static const char* CGRAPH_BASIC_EXCEPTION = "CGraph default exception";
static const char* CGRAPH_FUNCTION_NO_SUPPORT = "CGraph function no support";
CGRAPH_NAMESPACE_END
#endif //CGRAPH_CSTRDEFINE_H

+ 14
- 0
src/UtilsCtrl/ThreadPool/Lock/ULockInclude.h View File

@ -0,0 +1,14 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: ULockInclude.h
@Time: 2023/2/21 22:19
@Desc:
***************************/
#ifndef CGRAPH_ULOCKINCLUDE_H
#define CGRAPH_ULOCKINCLUDE_H
#include "USpinLock.h"
#endif //CGRAPH_ULOCKINCLUDE_H

+ 51
- 0
src/UtilsCtrl/ThreadPool/Lock/USpinLock.h View File

@ -0,0 +1,51 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: USpinLock.h
@Time: 2023/2/21 22:17
@Desc:
***************************/
#ifndef CGRAPH_USPINLOCK_H
#define CGRAPH_USPINLOCK_H
#include <atomic>
#include "../UThreadObject.h"
CGRAPH_NAMESPACE_BEGIN
class USpinLock : public UThreadObject {
public:
/**
*
*/
CVoid lock() {
// memory_order_acquire 访
while (flag_.test_and_set(std::memory_order_acquire)) {
}
}
/**
*
*/
CVoid unlock() {
// memory_order_release 访
flag_.clear(std::memory_order_release);
}
/**
*
* @return
*/
CBool tryLock() {
return !flag_.test_and_set();
}
private:
std::atomic_flag flag_ = ATOMIC_FLAG_INIT; //
};
CGRAPH_NAMESPACE_END
#endif //CGRAPH_USPINLOCK_H

+ 1
- 1
src/UtilsCtrl/ThreadPool/Queue/UAtomicPriorityQueue.h View File

@ -64,7 +64,7 @@ public:
* @return
*/
CVoid push(T&& value, int priority) {
std::unique_ptr<T> task(make_unique<T>(std::move(value), priority));
std::unique_ptr<T> task(c_make_unique<T>(std::move(value), priority));
CGRAPH_LOCK_GUARD lk(mutex_);
priority_queue_.push(std::move(task));
}

+ 2
- 1
src/UtilsCtrl/ThreadPool/Queue/UAtomicQueue.h View File

@ -14,6 +14,7 @@
#include <queue>
#include <condition_variable>
#include "../UThreadPoolDefine.h"
#include "UQueueObject.h"
CGRAPH_NAMESPACE_BEGIN
@ -103,7 +104,7 @@ public:
* @param value
*/
CVoid push(T&& value) {
std::unique_ptr<T> task(make_unique<T>(std::move(value)));
std::unique_ptr<T> task(c_make_unique<T>(std::move(value)));
CGRAPH_LOCK_GUARD lk(mutex_);
queue_.push(std::move(task));
cv_.notify_one();

+ 129
- 0
src/UtilsCtrl/ThreadPool/Queue/UAtomicRingBufferQueue.h View File

@ -0,0 +1,129 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: UAtomicRingBufferQueue.h
@Time: 2022/10/22 22:32
@Desc: queue
***************************/
#ifndef CGRAPH_UATOMICRINGBUFFERQUEUE_H
#define CGRAPH_UATOMICRINGBUFFERQUEUE_H
#include <vector>
#include <atomic>
#include "UQueueObject.h"
CGRAPH_NAMESPACE_BEGIN
template<typename T, CUint capacity = CGRAPH_DEFAULT_RINGBUFFER_SIZE>
class UAtomicRingBufferQueue : public UQueueObject {
public:
explicit UAtomicRingBufferQueue() {
head_ = 0;
tail_ = 0;
capacity_ = capacity;
ring_buffer_queue_.resize(capacity_);
}
~UAtomicRingBufferQueue() override {
clear();
}
/**
*
* @param size
* @return
* @notice 使push信息之后使
*/
UAtomicRingBufferQueue* setCapacity(CUint size) {
capacity_ = size;
ring_buffer_queue_.resize(capacity_);
return this;
}
/**
*
* @return
*/
[[nodiscard]] CUint getCapacity() const {
return capacity_;
}
/**
*
* @param value
* @return
*/
template<class TImpl = T>
CVoid push(const TImpl& value) {
{
CGRAPH_UNIQUE_LOCK lk(mutex_);
if (isFull()) {
push_cv_.wait(lk, [this] { return !isFull(); });
}
ring_buffer_queue_[tail_] = std::move(c_make_unique<TImpl>(value));
tail_ = (tail_ + 1) % capacity_;
}
pop_cv_.notify_one();
}
/**
*
* @param value
* @return
*/
template<class TImpl = T>
CVoid waitPop(TImpl& value) {
{
CGRAPH_UNIQUE_LOCK lk(mutex_);
if (isEmpty()) {
pop_cv_.wait(lk, [this] { return !isEmpty(); });
}
value = (*ring_buffer_queue_[head_]);
*ring_buffer_queue_[head_] = {};
head_ = (head_ + 1) % capacity_;
}
push_cv_.notify_one();
}
/**
*
* @return
*/
CStatus clear() {
CGRAPH_FUNCTION_BEGIN
ring_buffer_queue_.resize(0);
head_ = 0;
tail_ = 0;
CGRAPH_FUNCTION_END
}
protected:
CBool isFull() {
// tail写入
return head_ == (tail_ + 1) % capacity_;
}
CBool isEmpty() {
return head_ == tail_;
}
CGRAPH_NO_ALLOWED_COPY(UAtomicRingBufferQueue)
private:
CUint head_; //
CUint tail_; //
CUint capacity_; //
std::condition_variable push_cv_; // 使 cv_了
std::condition_variable pop_cv_; //
std::vector<std::unique_ptr<T> > ring_buffer_queue_; //
};
CGRAPH_NAMESPACE_END
#endif //CGRAPH_UATOMICRINGBUFFERQUEUE_H

+ 1
- 0
src/UtilsCtrl/ThreadPool/Queue/UQueueInclude.h View File

@ -12,5 +12,6 @@
#include "UAtomicQueue.h"
#include "UWorkStealingQueue.h"
#include "UAtomicPriorityQueue.h"
#include "UAtomicRingBufferQueue.h"
#endif //CGRAPH_UQUEUEINCLUDE_H

+ 0
- 1
src/UtilsCtrl/ThreadPool/Queue/UQueueObject.h View File

@ -11,7 +11,6 @@
#include <mutex>
#include "../UThreadPoolDefine.h"
#include "../UThreadObject.h"
CGRAPH_NAMESPACE_BEGIN

+ 16
- 15
src/UtilsCtrl/ThreadPool/Queue/UWorkStealingQueue.h View File

@ -11,27 +11,25 @@
#define CGRAPH_UWORKSTEALINGQUEUE_H
#include <queue>
#include <mutex>
#include <deque>
#include "UQueueObject.h"
#include "../Task/UTask.h"
#include "../Task/UTaskInclude.h"
#include "../Lock/ULockInclude.h"
CGRAPH_NAMESPACE_BEGIN
class UWorkStealingQueue : public UQueueObject {
public:
UWorkStealingQueue() = default;
/**
*
* @param task
*/
CVoid push(UTask&& task) {
while (true) {
if (mutex_.try_lock()) {
if (lock_.tryLock()) {
deque_.emplace_front(std::move(task));
mutex_.unlock();
lock_.unlock();
break;
} else {
std::this_thread::yield();
@ -48,13 +46,13 @@ public:
CBool tryPop(UTask& task) {
// 使raii锁线
bool result = false;
if (mutex_.try_lock()) {
if (lock_.tryLock()) {
if (!deque_.empty()) {
task = std::move(deque_.front()); //
deque_.pop_front();
result = true;
}
mutex_.unlock();
lock_.unlock();
}
return result;
@ -70,13 +68,13 @@ public:
CBool tryPop(UTaskArrRef taskArr,
int maxLocalBatchSize) {
bool result = false;
if (mutex_.try_lock()) {
if (lock_.tryLock()) {
while (!deque_.empty() && maxLocalBatchSize--) {
taskArr.emplace_back(std::move(deque_.front()));
deque_.pop_front();
result = true;
}
mutex_.unlock();
lock_.unlock();
}
return result;
@ -90,13 +88,13 @@ public:
*/
CBool trySteal(UTask& task) {
bool result = false;
if (mutex_.try_lock()) {
if (lock_.tryLock()) {
if (!deque_.empty()) {
task = std::move(deque_.back()); //
deque_.pop_back();
result = true;
}
mutex_.unlock();
lock_.unlock();
}
return result;
@ -110,22 +108,25 @@ public:
*/
CBool trySteal(UTaskArrRef taskArr, int maxStealBatchSize) {
bool result = false;
if (mutex_.try_lock()) {
if (lock_.tryLock()) {
while (!deque_.empty() && maxStealBatchSize--) {
taskArr.emplace_back(std::move(deque_.back()));
deque_.pop_back();
result = true;
}
mutex_.unlock();
lock_.unlock();
}
return result; //
}
UWorkStealingQueue() = default;
CGRAPH_NO_ALLOWED_COPY(UWorkStealingQueue)
private:
std::deque<UTask> deque_;
std::deque<UTask> deque_; //
USpinLock lock_; //
};
CGRAPH_NAMESPACE_END

+ 2
- 3
src/UtilsCtrl/ThreadPool/Task/UTask.h View File

@ -24,6 +24,7 @@ class UTask : public UThreadObject {
virtual ~taskBased() = default;
};
// 退https://github.com/ChunelFeng/CThreadPool/pull/3
template<typename F, typename T = typename std::decay<F>::type>
struct taskDerided : taskBased {
T func_;
@ -38,9 +39,7 @@ public:
, priority_(priority) {}
CVoid operator()() {
if (likely(impl_)) {
impl_->call();
}
impl_->call();
}
UTask() = default;

+ 2
- 3
src/UtilsCtrl/ThreadPool/Thread/UThreadBase.h View File

@ -19,7 +19,6 @@
CGRAPH_NAMESPACE_BEGIN
class UThreadBase : public UThreadObject {
protected:
explicit UThreadBase() {
done_ = true;
@ -59,7 +58,7 @@ protected:
virtual bool popPoolTask(UTaskRef task) {
bool result = pool_task_queue_->tryPop(task);
if (!result && CGRAPH_THREAD_TYPE_SECONDARY == type_) {
// 线
// 线
result = pool_priority_task_queue_->tryPop(task);
}
return result;
@ -74,7 +73,7 @@ protected:
virtual bool popPoolTask(UTaskArrRef tasks) {
bool result = pool_task_queue_->tryPop(tasks, config_->max_pool_batch_size_);
if (!result && CGRAPH_THREAD_TYPE_SECONDARY == type_) {
result = pool_priority_task_queue_->tryPop(tasks, 1);
result = pool_priority_task_queue_->tryPop(tasks, 1); // pop出来一个
}
return result;
}

+ 2
- 2
src/UtilsCtrl/ThreadPool/Thread/UThreadPrimary.h View File

@ -16,7 +16,7 @@ CGRAPH_NAMESPACE_BEGIN
class UThreadPrimary : public UThreadBase {
protected:
explicit UThreadPrimary() {
index_ = -1;
index_ = CGRAPH_SECONDARY_THREAD_COMMON_ID;
pool_threads_ = nullptr;
type_ = CGRAPH_THREAD_TYPE_PRIMARY;
}
@ -201,7 +201,7 @@ protected:
}
private:
int index_ {-1}; // 线index
int index_ {CGRAPH_SECONDARY_THREAD_COMMON_ID}; // 线index
UWorkStealingQueue work_stealing_queue_; //
std::vector<UThreadPrimary *>* pool_threads_; // 线线

+ 0
- 1
src/UtilsCtrl/ThreadPool/UThreadObject.h View File

@ -14,7 +14,6 @@
CGRAPH_NAMESPACE_BEGIN
class UThreadObject : public UtilsObject {
protected:
/**
* thread中的算子run方法

+ 29
- 4
src/UtilsCtrl/ThreadPool/UThreadPool.cpp View File

@ -53,12 +53,15 @@ CStatus UThreadPool::init() {
CGRAPH_FUNCTION_END
}
thread_record_map_.clear();
primary_threads_.reserve(config_.default_thread_size_);
for (int i = 0; i < config_.default_thread_size_; i++) {
auto ptr = CGRAPH_SAFE_MALLOC_COBJECT(UThreadPrimary) // 创建核心线程数
auto ptr = CGRAPH_SAFE_MALLOC_COBJECT(UThreadPrimary); // 创建核心线程数
ptr->setThreadPoolInfo(i, &task_queue_, &primary_threads_, &config_);
status += ptr->init();
// 记录线程和匹配id信息
thread_record_map_[(CSize)std::hash<std::thread::id>{}(ptr->thread_.get_id())] = i;
primary_threads_.emplace_back(ptr);
}
CGRAPH_FUNCTION_CHECK_STATUS
@ -113,6 +116,17 @@ CStatus UThreadPool::submit(CGRAPH_DEFAULT_CONST_FUNCTION_REF func, CMSec ttl,
}
CIndex UThreadPool::getThreadNum(CSize tid) {
int threadNum = CGRAPH_SECONDARY_THREAD_COMMON_ID;
auto result = thread_record_map_.find(tid);
if (result != thread_record_map_.end()) {
threadNum = result->second;
}
return threadNum;
}
CStatus UThreadPool::destroy() {
CGRAPH_FUNCTION_BEGIN
if (!is_init_) {
@ -121,10 +135,20 @@ CStatus UThreadPool::destroy() {
// primary 线程是普通指针,需要delete
for (auto &pt : primary_threads_) {
status += pt->destroy();
CGRAPH_DELETE_PTR(pt)
status += pt->destroy();
}
CGRAPH_FUNCTION_CHECK_STATUS
/**
* destroy和 delete分开两个循环执行
* 线delete后delete的主线程steal当前线程的任务
* windows环境下
* destroy delete
* Ryan大佬(https://github.com/ryanhuang) 提供的帮助
*/
for (auto &pt : primary_threads_) {
CGRAPH_DELETE_PTR(pt)
}
primary_threads_.clear();
// secondary 线程是智能指针,不需要delete
@ -133,6 +157,7 @@ CStatus UThreadPool::destroy() {
}
CGRAPH_FUNCTION_CHECK_STATUS
secondary_threads_.clear();
thread_record_map_.clear();
is_init_ = false;
CGRAPH_FUNCTION_END

+ 11
- 1
src/UtilsCtrl/ThreadPool/UThreadPool.h View File

@ -11,6 +11,7 @@
#include <vector>
#include <list>
#include <map>
#include <future>
#include <thread>
#include <algorithm>
@ -101,6 +102,14 @@ public:
CGRAPH_CALLBACK_CONST_FUNCTION_REF onFinished = nullptr);
/**
* 线id信息线num信息
* @param tid
* @return
* @notice 线-1
*/
CIndex getThreadNum(CSize tid);
/**
* 线
* @return
*/
@ -130,7 +139,7 @@ protected:
CGRAPH_NO_ALLOWED_COPY(UThreadPool)
protected:
private:
CBool is_init_ { false }; //
CBool is_monitor_ { true }; //
CInt cur_index_ = 0; // 线
@ -141,6 +150,7 @@ protected:
std::list<std::unique_ptr<UThreadSecondary>> secondary_threads_; // 线
UThreadPoolConfig config_; // 线
std::thread monitor_thread_; // 线
std::map<CSize, int> thread_record_map_; // 线
};
using UThreadPoolPtr = UThreadPool *;

+ 1
- 0
src/UtilsCtrl/ThreadPool/UThreadPool.inl View File

@ -40,6 +40,7 @@ auto UThreadPool::commit(const FunctionType& func, CIndex index)
return result;
}
template<typename FunctionType>
auto UThreadPool::commitWithPriority(const FunctionType& func, int priority)
-> std::future<typename std::result_of<FunctionType()>::type> {

+ 10
- 7
src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h View File

@ -10,16 +10,18 @@
#define CGRAPH_UTHREADPOOLDEFINE_H
#include <thread>
#if _LIBCPP_STD_VER >= 17
#if __cplusplus >= 201703L
#include <shared_mutex>
#else
# include <mutex>
#endif
#include <memory>
#include "../UtilsDefine.h"
CGRAPH_NAMESPACE_BEGIN
#if _LIBCPP_STD_VER >= 17
#if __cplusplus >= 201703L
using CGRAPH_READ_LOCK = std::shared_lock<std::shared_mutex>;
using CGRAPH_WRITE_LOCK = std::unique_lock<std::shared_mutex>;
#else
@ -46,8 +48,13 @@ static const int CGRAPH_THREAD_SCHED_FIFO = 0;
static const int CGRAPH_THREAD_MIN_PRIORITY = 0; // 线
static const int CGRAPH_THREAD_MAX_PRIORITY = 99; // 线
static const CMSec CGRAPH_MAX_BLOCK_TTL = 10000000; // ms
static const CUint CGRAPH_DEFAULT_RINGBUFFER_SIZE = 1024; //
const static CIndex CGRAPH_SECONDARY_THREAD_COMMON_ID = -1; // 线id标识
static const int CGRAPH_DEFAULT_TASK_STRATEGY = -1; // 线
static const int CGRAPH_LONG_TIME_TASK_STRATEGY = -101; //
static const int CGRAPH_REGION_TASK_STRATEGY = -102; // region的调度策略
static const int CGRAPH_EVENT_TASK_STRATEGY = -103; // event的调度策略
/**
* 线
@ -55,18 +62,14 @@ static const int CGRAPH_LONG_TIME_TASK_STRATEGY = -101;
static const int CGRAPH_DEFAULT_THREAD_SIZE = 8; // 线
static const int CGRAPH_SECONDARY_THREAD_SIZE = 0; // 线
static const int CGRAPH_MAX_THREAD_SIZE = (CGRAPH_DEFAULT_THREAD_SIZE * 2) + 1; // 线
#ifndef _WIN32
static const int CGRAPH_MAX_TASK_STEAL_RANGE = 2; //
#else
static const int CGRAPH_MAX_TASK_STEAL_RANGE = 0; // windows平台暂不支持任务盗取功能
#endif
static const bool CGRAPH_BATCH_TASK_ENABLE = false; //
static const int CGRAPH_MAX_LOCAL_BATCH_SIZE = 2; //
static const int CGRAPH_MAX_POOL_BATCH_SIZE = 2; //
static const int CGRAPH_MAX_STEAL_BATCH_SIZE = 2; //
static const bool CGRAPH_FAIR_LOCK_ENABLE = false; // CGRAPH_BATCH_TASK_ENABLE无效
static const int CGRAPH_SECONDARY_THREAD_TTL = 10; // 线ttls
static const bool CGRAPH_MONITOR_ENABLE = true; //
static const bool CGRAPH_MONITOR_ENABLE = true; // 线
static const int CGRAPH_MONITOR_SPAN = 5; // 线s
static const bool CGRAPH_BIND_CPU_ENABLE = false; // cpu模式线
static const int CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 线

+ 1
- 0
src/UtilsCtrl/ThreadPool/UThreadPoolInclude.h View File

@ -16,5 +16,6 @@
#include "Queue/UQueueInclude.h"
#include "Task/UTaskInclude.h"
#include "Thread/UThreadInclude.h"
#include "Lock/ULockInclude.h"
#endif //CGRAPH_UTHREADPOOLINCLUDE_H

+ 20
- 29
src/UtilsCtrl/UAllocator.h View File

@ -9,17 +9,13 @@
#ifndef CGRAPH_UALLOCATOR_H
#define CGRAPH_UALLOCATOR_H
#ifdef _GENERATE_SESSION_
#include <uuid/uuid.h>
#endif
#include <mutex>
#include <memory>
#include "UMemory.h"
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
static std::mutex g_session_mtx;
/**
* CObject类型的类
*/
@ -31,7 +27,7 @@ public:
* @return
*/
template<typename T,
CTP::enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocCObject() {
T* ptr = nullptr;
while (!ptr) {
@ -40,35 +36,33 @@ public:
return ptr;
}
/**
* unique智能指针信息
*
* @tparam T
* @tparam Args
* @param args
* @return
*/
template<typename T,
CTP::enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static std::unique_ptr<T> makeUniqueCObject() {
return CTP::make_unique<T>();
template<typename T, typename ...Args,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocTemplateCObject(Args... args) {
T* ptr = nullptr;
while (!ptr) {
ptr = new T(std::forward<Args>(args)...);
}
return ptr;
}
/**
*
* unique智能指针信息
* @tparam T
* @return
*/
static std::string generateSession() {
#ifdef _GENERATE_SESSION_
std::lock_guard<std::mutex> lock{ g_session_mtx };
uuid_t uuid;
char session[36] = {0}; // 36
uuid_generate(uuid);
uuid_unparse(uuid, session);
return session;
#else
return CGRAPH_EMPTY; // mac平台session信息
#endif
template<typename T,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static std::unique_ptr<T> makeUniqueCObject() {
return c_make_unique<T>();
}
};
@ -79,9 +73,6 @@ public:
#define CGRAPH_MAKE_UNIQUE_COBJECT(Type) \
UAllocator::makeUniqueCObject<Type>(); \
#define CGRAPH_GENERATE_SESSION \
UAllocator::generateSession(); \
CGRAPH_NAMESPACE_END
#endif //CGRAPH_UALLOCATOR_H

+ 0
- 54
src/UtilsCtrl/UMemory.h View File

@ -1,54 +0,0 @@
/***************************
@Author: MirrorYuChen
@Contact: 2458006366@qq.com
@File: UMemory.h
@Time: 2022/10/11 01:43
@Desc:
***************************/
#ifndef CGRAPH_UMEMORY_H
#define CGRAPH_UMEMORY_H
#include <memory>
#include <type_traits>
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
template<bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T>
struct MakeUniqueResult {
using scalar = std::unique_ptr<T>;
};
template<typename T>
struct MakeUniqueResult<T[]> {
using array = std::unique_ptr<T[]>;
};
template<typename T, size_t N>
struct MakeUniqueResult<T[N]> {
using invalid = void;
};
template<typename T, typename... Args>
typename MakeUniqueResult<T>::scalar make_unique(
Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T>
typename MakeUniqueResult<T>::array make_unique(size_t n) {
return std::unique_ptr<T>(new typename std::remove_extent<T>[n]());
}
template<typename T, typename... Args>
typename MakeUniqueResult<T>::invalid make_unique(
Args &&... /* args */) = delete;
CGRAPH_NAMESPACE_END
#endif // CGRAPH_UMEMORY_H

+ 12
- 2
src/UtilsCtrl/UtilsDefine.h View File

@ -29,7 +29,7 @@ CGRAPH_NAMESPACE_BEGIN
/* 判断传入的指针信息是否为空 */
#define CGRAPH_ASSERT_NOT_NULL(ptr) \
if (unlikely(nullptr == (ptr))) { \
return CStatus("ptr is nullptr"); \
return CStatus("input is nullptr"); \
} \
#define CGRAPH_ASSERT_NOT_NULL_RETURN_NULL(ptr) \
@ -37,10 +37,16 @@ CGRAPH_NAMESPACE_BEGIN
return nullptr; \
} \
#define CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(ptr) \
if (unlikely(nullptr == (ptr))) { \
CGRAPH_THROW_EXCEPTION("input is null") \
}
/* 判断函数流程是否可以继续 */
static std::mutex g_check_status_mtx;
#define CGRAPH_FUNCTION_CHECK_STATUS \
if (unlikely(!status.isOK())) { \
if (unlikely(status.isErr())) { \
std::lock_guard<std::mutex> lock{ g_check_status_mtx }; \
CGRAPH_ECHO("%s | %s | line = [%d], errorCode = [%d], errorInfo = [%s].", \
__FILE__, __FUNCTION__, __LINE__, status.getCode(), status.getInfo().c_str()); \
@ -64,6 +70,10 @@ static std::mutex g_check_status_mtx;
return nullptr; \
} \
#define CGRAPH_CHECK_STATUS_RETURN_THIS_OR_NULL \
return status.isOK() ? this : nullptr; \
#define CGRAPH_SLEEP_MILLISECOND(ms) \
std::this_thread::sleep_for(std::chrono::milliseconds(ms)); \

+ 2
- 0
src/UtilsCtrl/UtilsFunction.h View File

@ -15,6 +15,8 @@
#include <cstdarg>
#include <algorithm>
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
/**

+ 0
- 1
src/UtilsCtrl/UtilsObject.h View File

@ -14,7 +14,6 @@
CGRAPH_NAMESPACE_BEGIN
class UtilsObject : public CObject {
public:
CStatus run() override {
CGRAPH_NO_SUPPORT

+ 1
- 1
tutorial.cpp View File

@ -123,7 +123,7 @@ void tutorial_threadpool_3(UThreadPoolPtr tp) {
int main() {
auto pool = make_unique<UThreadPool>(); // 构造一个线程池类的智能指针
std::unique_ptr<UThreadPool> pool(new UThreadPool()); // 构造一个线程池类的智能指针
CGRAPH_ECHO("======== tutorial_threadpool_1 begin. ========");
tutorial_threadpool_1(pool.get());

Loading…
Cancel
Save