|
|
@@ -32,8 +32,11 @@ |
|
|
#include <stdio.h> |
|
|
#include <stdio.h> |
|
|
#define LOG_INFO(content, args...) \ |
|
|
#define LOG_INFO(content, args...) \ |
|
|
{ printf("[INFO] %s|%d|%s: " #content "\r\n", __FILE__, __LINE__, __func__, ##args); } |
|
|
{ printf("[INFO] %s|%d|%s: " #content "\r\n", __FILE__, __LINE__, __func__, ##args); } |
|
|
|
|
|
#define LOG_ERROR(content, args...) \ |
|
|
|
|
|
{ printf("[ERROR] %s|%d|%s: " #content "\r\n", __FILE__, __LINE__, __func__, ##args); } |
|
|
#else |
|
|
#else |
|
|
#define LOG_INFO(content, args...) |
|
|
#define LOG_INFO(content, args...) |
|
|
|
|
|
#define LOG_ERROR(content, args...) |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
#define RET_TP_OK (0) |
|
|
#define RET_TP_OK (0) |
|
|
@@ -84,7 +87,7 @@ static atomic_bool thread_pool_is_created[MAX_THREAD_POOL_NUM] = {ATOMIC_VAR_INI |
|
|
|
|
|
|
|
|
ThreadPool *GetInstance(int thread_pool_id) { |
|
|
ThreadPool *GetInstance(int thread_pool_id) { |
|
|
if (thread_pool_id < 0 || thread_pool_id >= MAX_THREAD_POOL_NUM) { |
|
|
if (thread_pool_id < 0 || thread_pool_id >= MAX_THREAD_POOL_NUM) { |
|
|
LOG_INFO("invaid context id: %d", thread_pool_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("invaid context id: %d", thread_pool_id); |
|
|
// DestroyThreadPool(thread_pool_id); |
|
|
// DestroyThreadPool(thread_pool_id); |
|
|
return NULL; |
|
|
return NULL; |
|
|
} |
|
|
} |
|
|
@@ -94,16 +97,16 @@ ThreadPool *GetInstance(int thread_pool_id) { |
|
|
Thread *GetThread(int thread_pool_id, int thread_id) { |
|
|
Thread *GetThread(int thread_pool_id, int thread_id) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, thread_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, thread_id); |
|
|
return NULL; |
|
|
return NULL; |
|
|
} |
|
|
} |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
if (thread_list == NULL) { |
|
|
if (thread_list == NULL) { |
|
|
LOG_INFO("thead list is null"); |
|
|
|
|
|
|
|
|
LOG_ERROR("thead list is null"); |
|
|
return NULL; |
|
|
return NULL; |
|
|
} |
|
|
} |
|
|
if (thread_id >= thread_list->size) { |
|
|
if (thread_id >= thread_list->size) { |
|
|
LOG_INFO("invalid thread id: %d, thread_pool_id: %d, thread size: %d", thread_id, thread_pool_id, |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid thread id: %d, thread_pool_id: %d, thread size: %d", thread_id, thread_pool_id, |
|
|
thread_list->size); |
|
|
thread_list->size); |
|
|
return NULL; |
|
|
return NULL; |
|
|
} |
|
|
} |
|
|
@@ -122,11 +125,11 @@ Thread *GetThread(int thread_pool_id, int thread_id) { |
|
|
|
|
|
|
|
|
void FreeThread(ThreadList *thread_list, Thread *thread) { |
|
|
void FreeThread(ThreadList *thread_list, Thread *thread) { |
|
|
if (thread_list == NULL) { |
|
|
if (thread_list == NULL) { |
|
|
LOG_INFO("thead list is null"); |
|
|
|
|
|
|
|
|
LOG_ERROR("thead list is null"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("thread is nullptr"); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread is nullptr"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
// only support sequential release |
|
|
// only support sequential release |
|
|
@@ -175,26 +178,26 @@ int GetMaxFrequence(int core_id) { |
|
|
char path[MAX_PATH_SIZE] = ""; |
|
|
char path[MAX_PATH_SIZE] = ""; |
|
|
int ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpufreq/stats/cpu", "/time_in_state", path); |
|
|
int ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpufreq/stats/cpu", "/time_in_state", path); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("parse cpuid from /sys/devices/system/cpu/cpufreq/stats/cpu/time_in_state failed!"); |
|
|
|
|
|
|
|
|
LOG_ERROR("parse cpuid from /sys/devices/system/cpu/cpufreq/stats/cpu/time_in_state failed!"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
FILE *fp = fopen(path, "rb"); |
|
|
FILE *fp = fopen(path, "rb"); |
|
|
if (fp == NULL) { |
|
|
if (fp == NULL) { |
|
|
ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpufreq/stats/cpu", "/cpufreq/stats/time_in_state", path); |
|
|
ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpufreq/stats/cpu", "/cpufreq/stats/time_in_state", path); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("parse cpuid from /sys/devices/system/cpu/cpufreq/stats/cpu/cpufreq/stats/time_instate failed!"); |
|
|
|
|
|
|
|
|
LOG_ERROR("parse cpuid from /sys/devices/system/cpu/cpufreq/stats/cpu/cpufreq/stats/time_instate failed!"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
fp = fopen(path, "rb"); |
|
|
fp = fopen(path, "rb"); |
|
|
if (fp == NULL) { |
|
|
if (fp == NULL) { |
|
|
ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpu", "/cpufreq/cpuinfo_max_freq", path); |
|
|
ret = ConcatCPUPath(core_id, "/sys/devices/system/cpu/cpu", "/cpufreq/cpuinfo_max_freq", path); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("parse cpuid from /sys/devices/system/cpu/cpufreq/cpuinfo_max_freq failed!"); |
|
|
|
|
|
|
|
|
LOG_ERROR("parse cpuid from /sys/devices/system/cpu/cpufreq/cpuinfo_max_freq failed!"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
fp = fopen(path, "rb"); |
|
|
fp = fopen(path, "rb"); |
|
|
if (fp == NULL) { |
|
|
if (fp == NULL) { |
|
|
LOG_INFO("GetCPUMaxFreq failed, cannot find cpuinfo_max_freq."); |
|
|
|
|
|
|
|
|
LOG_ERROR("GetCPUMaxFreq failed, cannot find cpuinfo_max_freq."); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
int maxFreq = -1; |
|
|
int maxFreq = -1; |
|
|
@@ -222,7 +225,7 @@ int GetMaxFrequence(int core_id) { |
|
|
int SortCpuProcessor() { |
|
|
int SortCpuProcessor() { |
|
|
gCoreNum = GetCpuCoreNum(); |
|
|
gCoreNum = GetCpuCoreNum(); |
|
|
if (gCoreNum <= 0) { |
|
|
if (gCoreNum <= 0) { |
|
|
LOG_INFO("invalid cpu count"); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid cpu count"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
CpuInfo freq_set[gCoreNum]; |
|
|
CpuInfo freq_set[gCoreNum]; |
|
|
@@ -285,18 +288,18 @@ int SetAffinity(pthread_t thread_id, cpu_set_t *cpuSet) { |
|
|
LOG_INFO("thread: %d, mask: %lu", pthread_gettid_np(thread_id), cpuSet->__bits[0]); |
|
|
LOG_INFO("thread: %d, mask: %lu", pthread_gettid_np(thread_id), cpuSet->__bits[0]); |
|
|
int ret = sched_setaffinity(pthread_gettid_np(thread_id), sizeof(cpu_set_t), cpuSet); |
|
|
int ret = sched_setaffinity(pthread_gettid_np(thread_id), sizeof(cpu_set_t), cpuSet); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("bind thread %d to cpu failed. ERROR %d", pthread_gettid_np(thread_id), ret); |
|
|
|
|
|
|
|
|
LOG_ERROR("bind thread %d to cpu failed. ERROR %d", pthread_gettid_np(thread_id), ret); |
|
|
return RET_TP_OK; |
|
|
return RET_TP_OK; |
|
|
} |
|
|
} |
|
|
#endif |
|
|
#endif |
|
|
#else |
|
|
#else |
|
|
#ifdef __APPLE__ |
|
|
#ifdef __APPLE__ |
|
|
LOG_INFO("not bind thread to apple's cpu."); |
|
|
|
|
|
|
|
|
LOG_ERROR("not bind thread to apple's cpu."); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
#else |
|
|
#else |
|
|
int ret = pthread_setaffinity_np(thread_id, sizeof(cpu_set_t), cpuSet); |
|
|
int ret = pthread_setaffinity_np(thread_id, sizeof(cpu_set_t), cpuSet); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("set thread: %lu to cpu failed", thread_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("set thread: %lu to cpu failed", thread_id); |
|
|
return RET_TP_SYSTEM_ERROR; |
|
|
return RET_TP_SYSTEM_ERROR; |
|
|
} |
|
|
} |
|
|
#endif // __APPLE__ |
|
|
#endif // __APPLE__ |
|
|
@@ -307,7 +310,7 @@ int SetAffinity(pthread_t thread_id, cpu_set_t *cpuSet) { |
|
|
int BindMasterThread(int thread_pool_id, bool is_bind) { |
|
|
int BindMasterThread(int thread_pool_id, bool is_bind) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
cpu_set_t mask; |
|
|
cpu_set_t mask; |
|
|
@@ -328,7 +331,7 @@ int BindMasterThread(int thread_pool_id, bool is_bind) { |
|
|
} |
|
|
} |
|
|
int ret = SetAffinity(pthread_self(), &mask); |
|
|
int ret = SetAffinity(pthread_self(), &mask); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("set master thread affinity failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("set master thread affinity failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
LOG_INFO("BindMasterThread success."); |
|
|
LOG_INFO("BindMasterThread success."); |
|
|
@@ -338,7 +341,7 @@ int BindMasterThread(int thread_pool_id, bool is_bind) { |
|
|
int BindSalverThreads(int thread_pool_id, bool is_bind) { |
|
|
int BindSalverThreads(int thread_pool_id, bool is_bind) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
cpu_set_t mask; |
|
|
cpu_set_t mask; |
|
|
@@ -360,12 +363,12 @@ int BindSalverThreads(int thread_pool_id, bool is_bind) { |
|
|
CPU_SET(attach_id, &mask); |
|
|
CPU_SET(attach_id, &mask); |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
int ret = SetAffinity(thread->pthread, &mask); |
|
|
int ret = SetAffinity(thread->pthread, &mask); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("set thread affinity failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("set thread affinity failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@@ -377,12 +380,12 @@ int BindSalverThreads(int thread_pool_id, bool is_bind) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
int ret = SetAffinity(thread->pthread, &mask); |
|
|
int ret = SetAffinity(thread->pthread, &mask); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("set thread affinity failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("set thread affinity failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@@ -399,17 +402,17 @@ int BindThreads(int thread_pool_id, bool is_bind, int mode) { |
|
|
} |
|
|
} |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
thread_pool->mode = mode; |
|
|
thread_pool->mode = mode; |
|
|
int ret = BindMasterThread(thread_pool_id, is_bind); |
|
|
int ret = BindMasterThread(thread_pool_id, is_bind); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("bind master thread failed."); |
|
|
|
|
|
|
|
|
LOG_ERROR("bind master thread failed."); |
|
|
} |
|
|
} |
|
|
ret = BindSalverThreads(thread_pool_id, is_bind); |
|
|
ret = BindSalverThreads(thread_pool_id, is_bind); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("bind salver thread failed."); |
|
|
|
|
|
|
|
|
LOG_ERROR("bind salver thread failed."); |
|
|
} |
|
|
} |
|
|
return ret; |
|
|
return ret; |
|
|
#else |
|
|
#else |
|
|
@@ -420,7 +423,7 @@ int BindThreads(int thread_pool_id, bool is_bind, int mode) { |
|
|
bool PushTaskToQueue(int thread_pool_id, int thread_id, Task *task) { |
|
|
bool PushTaskToQueue(int thread_pool_id, int thread_id, Task *task) { |
|
|
Thread *thread = GetThread(thread_pool_id, thread_id); |
|
|
Thread *thread = GetThread(thread_pool_id, thread_id); |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, thread_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, thread_id); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
const int tail_index = atomic_load_explicit(&thread->tail, memory_order_relaxed); |
|
|
const int tail_index = atomic_load_explicit(&thread->tail, memory_order_relaxed); |
|
|
@@ -438,7 +441,7 @@ bool PushTaskToQueue(int thread_pool_id, int thread_id, Task *task) { |
|
|
|
|
|
|
|
|
bool PopTaskFromQueue(Thread *thread, Task **task) { |
|
|
bool PopTaskFromQueue(Thread *thread, Task **task) { |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("thread is nullptr"); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread is nullptr"); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
if (thread->task_size == 0) { |
|
|
if (thread->task_size == 0) { |
|
|
@@ -456,7 +459,7 @@ bool PopTaskFromQueue(Thread *thread, Task **task) { |
|
|
void WaitAllThread(int thread_pool_id) { |
|
|
void WaitAllThread(int thread_pool_id) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
bool k_success_flag = false; |
|
|
bool k_success_flag = false; |
|
|
@@ -465,7 +468,7 @@ void WaitAllThread(int thread_pool_id) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
Thread *thread = GetThread(thread_pool_id, i); |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread failed, thread_pool_id: %d, thread_id: %d", thread_pool_id, i); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (thread->task_size != 0) { |
|
|
if (thread->task_size != 0) { |
|
|
@@ -479,11 +482,11 @@ void WaitAllThread(int thread_pool_id) { |
|
|
int DistributeTask(int thread_pool_id, Task *task, int task_num) { |
|
|
int DistributeTask(int thread_pool_id, Task *task, int task_num) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
if (task_num > thread_pool->thread_num || task_num <= 1) { |
|
|
if (task_num > thread_pool->thread_num || task_num <= 1) { |
|
|
LOG_INFO("invalid task num: %d, thread num: %d", task_num, thread_pool->thread_num); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid task num: %d, thread num: %d", task_num, thread_pool->thread_num); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
bool k_success_flag = false; |
|
|
bool k_success_flag = false; |
|
|
@@ -498,6 +501,11 @@ int DistributeTask(int thread_pool_id, Task *task, int task_num) { |
|
|
} |
|
|
} |
|
|
// master thread |
|
|
// master thread |
|
|
task->func(task->content, size - 1); |
|
|
task->func(task->content, size - 1); |
|
|
|
|
|
if (task->func == NULL) { |
|
|
|
|
|
LOG_ERROR("task->func is nullptr"); |
|
|
|
|
|
return RET_TP_ERROR; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// wait |
|
|
// wait |
|
|
WaitAllThread(thread_pool_id); |
|
|
WaitAllThread(thread_pool_id); |
|
|
return RET_TP_OK; |
|
|
return RET_TP_OK; |
|
|
@@ -506,7 +514,7 @@ int DistributeTask(int thread_pool_id, Task *task, int task_num) { |
|
|
int AddTask(int thread_pool_id, int func(void *, int), void *content, int task_num) { |
|
|
int AddTask(int thread_pool_id, int func(void *, int), void *content, int task_num) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
// if single thread, run master thread |
|
|
// if single thread, run master thread |
|
|
@@ -529,7 +537,7 @@ int ParallelLaunch(int thread_pool_id, int (*func)(void *, int), void *content, |
|
|
void ThreadRun(Thread *thread) { |
|
|
void ThreadRun(Thread *thread) { |
|
|
ThreadPool *thread_pool = GetInstance(thread->thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread->thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
Task *task = NULL; |
|
|
Task *task = NULL; |
|
|
@@ -540,6 +548,10 @@ void ThreadRun(Thread *thread) { |
|
|
while (thread->activate) { |
|
|
while (thread->activate) { |
|
|
if (PopTaskFromQueue(thread, &task)) { |
|
|
if (PopTaskFromQueue(thread, &task)) { |
|
|
task->func(task->content, thread_id); |
|
|
task->func(task->content, thread_id); |
|
|
|
|
|
if (task->func == NULL) { |
|
|
|
|
|
LOG_ERROR("task->func is nullptr"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
atomic_fetch_sub_explicit(&thread->task_size, 1, memory_order_relaxed); |
|
|
atomic_fetch_sub_explicit(&thread->task_size, 1, memory_order_relaxed); |
|
|
// atomic_store_explicit(&thread->task_size, thread->task_size - 1, memory_order_relaxed); |
|
|
// atomic_store_explicit(&thread->task_size, thread->task_size - 1, memory_order_relaxed); |
|
|
spin_count = 0; |
|
|
spin_count = 0; |
|
|
@@ -560,12 +572,12 @@ void ThreadRun(Thread *thread) { |
|
|
void PushThreadToList(int thread_pool_id, Thread *thread) { |
|
|
void PushThreadToList(int thread_pool_id, Thread *thread) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
if (thread_list == NULL) { |
|
|
if (thread_list == NULL) { |
|
|
LOG_INFO("thread list is null"); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread list is null"); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
@@ -585,7 +597,7 @@ int CreateNewThread(int thread_pool_id, int thread_id) { |
|
|
LOG_INFO("thread_pool_id: %d, create thread: %d", thread_pool_id, thread_id); |
|
|
LOG_INFO("thread_pool_id: %d, create thread: %d", thread_pool_id, thread_id); |
|
|
Thread *thread = (Thread *)malloc(sizeof(Thread)); |
|
|
Thread *thread = (Thread *)malloc(sizeof(Thread)); |
|
|
if (thread == NULL) { |
|
|
if (thread == NULL) { |
|
|
LOG_INFO("create thread failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("create thread failed"); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
@@ -607,12 +619,12 @@ int CreateNewThread(int thread_pool_id, int thread_id) { |
|
|
int ReConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
int ReConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
LOG_INFO("reconfig thread pool, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, mode); |
|
|
LOG_INFO("reconfig thread pool, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, mode); |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
LOG_INFO("invalid thread num: %d", thread_num); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid thread num: %d", thread_num); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
if (thread_num <= thread_pool->thread_num) { |
|
|
if (thread_num <= thread_pool->thread_num) { |
|
|
@@ -625,7 +637,7 @@ int ReConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
thread_pool->thread_list = (ThreadList *)malloc(sizeof(ThreadList)); |
|
|
thread_pool->thread_list = (ThreadList *)malloc(sizeof(ThreadList)); |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
LOG_INFO("create thread list failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("create thread list failed"); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
@@ -638,7 +650,7 @@ int ReConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
for (int i = curr_thread_num - 1, j = 0; j < add_thread_num; ++i, ++j) { |
|
|
for (int i = curr_thread_num - 1, j = 0; j < add_thread_num; ++i, ++j) { |
|
|
int ret = CreateNewThread(thread_pool_id, i); |
|
|
int ret = CreateNewThread(thread_pool_id, i); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("create new thread failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("create new thread failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@@ -648,7 +660,7 @@ int ReConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
int CreateThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
int CreateThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
LOG_INFO("create thread pool, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, mode); |
|
|
LOG_INFO("create thread pool, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, mode); |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
LOG_INFO("invalid thread num: %d", thread_num); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid thread num: %d", thread_num); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
#ifdef BIND_CORE |
|
|
#ifdef BIND_CORE |
|
|
@@ -659,7 +671,7 @@ int CreateThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
#endif |
|
|
#endif |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
thread_pool->thread_num = thread_num > MAX_THREAD_NUM ? MAX_THREAD_NUM : thread_num; |
|
|
thread_pool->thread_num = thread_num > MAX_THREAD_NUM ? MAX_THREAD_NUM : thread_num; |
|
|
@@ -669,7 +681,7 @@ int CreateThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
if (thread_num > 1) { |
|
|
if (thread_num > 1) { |
|
|
thread_pool->thread_list = (ThreadList *)malloc(sizeof(ThreadList)); |
|
|
thread_pool->thread_list = (ThreadList *)malloc(sizeof(ThreadList)); |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
LOG_INFO("create thread list failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("create thread list failed"); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
@@ -681,7 +693,7 @@ int CreateThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
for (int i = 0; i < thread_pool->thread_num - 1; ++i) { |
|
|
int ret = CreateNewThread(thread_pool_id, i); |
|
|
int ret = CreateNewThread(thread_pool_id, i); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("create thread %d failed", i); |
|
|
|
|
|
|
|
|
LOG_ERROR("create thread %d failed", i); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
DestroyThreadPool(thread_pool_id); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
@@ -693,11 +705,11 @@ int ConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
LOG_INFO("config: thread_pool_id: %d, thread_num: %d, mode: %d, is_created: %d, refcount: %d", thread_pool_id, |
|
|
LOG_INFO("config: thread_pool_id: %d, thread_num: %d, mode: %d, is_created: %d, refcount: %d", thread_pool_id, |
|
|
thread_num, mode, thread_pool_is_created[thread_pool_id], thread_pool_refcount[thread_pool_id]); |
|
|
thread_num, mode, thread_pool_is_created[thread_pool_id], thread_pool_refcount[thread_pool_id]); |
|
|
if (thread_pool_id >= MAX_THREAD_POOL_NUM) { |
|
|
if (thread_pool_id >= MAX_THREAD_POOL_NUM) { |
|
|
LOG_INFO("invalid context id: %d", thread_pool_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid context id: %d", thread_pool_id); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
if (thread_num <= 0 || thread_num > MAX_THREAD_NUM) { |
|
|
LOG_INFO("invalid thread num: %d", thread_num); |
|
|
|
|
|
|
|
|
LOG_ERROR("invalid thread num: %d", thread_num); |
|
|
return RET_TP_ERROR; |
|
|
return RET_TP_ERROR; |
|
|
} |
|
|
} |
|
|
thread_pool_refcount[thread_pool_id] += 1; |
|
|
thread_pool_refcount[thread_pool_id] += 1; |
|
|
@@ -705,14 +717,14 @@ int ConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
if (thread_pool_is_created[thread_pool_id]) { |
|
|
if (thread_pool_is_created[thread_pool_id]) { |
|
|
ret = ReConfigThreadPool(thread_pool_id, thread_num, mode); |
|
|
ret = ReConfigThreadPool(thread_pool_id, thread_num, mode); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("reconfig thread pool failed, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, |
|
|
|
|
|
|
|
|
LOG_ERROR("reconfig thread pool failed, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, |
|
|
mode); |
|
|
mode); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
thread_pool_is_created[thread_pool_id] = true; |
|
|
thread_pool_is_created[thread_pool_id] = true; |
|
|
ret = CreateThreadPool(thread_pool_id, thread_num, mode); |
|
|
ret = CreateThreadPool(thread_pool_id, thread_num, mode); |
|
|
if (ret != RET_TP_OK) { |
|
|
if (ret != RET_TP_OK) { |
|
|
LOG_INFO("create thread pool failed, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, |
|
|
|
|
|
|
|
|
LOG_ERROR("create thread pool failed, thread_pool_id: %d, thread_num: %d, mode: %d", thread_pool_id, thread_num, |
|
|
mode); |
|
|
mode); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@@ -722,12 +734,12 @@ int ConfigThreadPool(int thread_pool_id, int thread_num, int mode) { |
|
|
void ActivateThreadPool(int thread_pool_id) { |
|
|
void ActivateThreadPool(int thread_pool_id) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
if (thread_list == NULL) { |
|
|
if (thread_list == NULL) { |
|
|
LOG_INFO("thread pool: %d list is null", thread_pool_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread pool: %d list is null", thread_pool_id); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
Thread *thread = thread_list->head; |
|
|
Thread *thread = thread_list->head; |
|
|
@@ -741,12 +753,12 @@ void ActivateThreadPool(int thread_pool_id) { |
|
|
void DeactivateThreadPool(int thread_pool_id) { |
|
|
void DeactivateThreadPool(int thread_pool_id) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
ThreadList *thread_list = thread_pool->thread_list; |
|
|
if (thread_list == NULL) { |
|
|
if (thread_list == NULL) { |
|
|
LOG_INFO("thread pool: %d list is null", thread_pool_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread pool: %d list is null", thread_pool_id); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
Thread *thread = thread_list->head; |
|
|
Thread *thread = thread_list->head; |
|
|
@@ -759,16 +771,17 @@ void DeactivateThreadPool(int thread_pool_id) { |
|
|
void DestroyThreadPool(int thread_pool_id) { |
|
|
void DestroyThreadPool(int thread_pool_id) { |
|
|
thread_pool_refcount[thread_pool_id]--; |
|
|
thread_pool_refcount[thread_pool_id]--; |
|
|
if (thread_pool_refcount[thread_pool_id] > 0) { |
|
|
if (thread_pool_refcount[thread_pool_id] > 0) { |
|
|
LOG_INFO("no need to free, thread_pool_id: %d, refcount: %d", thread_pool_id, thread_pool_refcount[thread_pool_id]); |
|
|
|
|
|
|
|
|
LOG_ERROR("no need to free, thread_pool_id: %d, refcount: %d", |
|
|
|
|
|
thread_pool_id, thread_pool_refcount[thread_pool_id]); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
if (thread_pool->thread_list == NULL) { |
|
|
LOG_INFO("thread pool: %d list is null", thread_pool_id); |
|
|
|
|
|
|
|
|
LOG_ERROR("thread pool: %d list is null", thread_pool_id); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
DeactivateThreadPool(thread_pool_id); |
|
|
DeactivateThreadPool(thread_pool_id); |
|
|
@@ -789,7 +802,7 @@ void DestroyThreadPool(int thread_pool_id) { |
|
|
int GetCurrentThreadNum(int thread_pool_id) { |
|
|
int GetCurrentThreadNum(int thread_pool_id) { |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
ThreadPool *thread_pool = GetInstance(thread_pool_id); |
|
|
if (thread_pool == NULL) { |
|
|
if (thread_pool == NULL) { |
|
|
LOG_INFO("get thread pool instane failed"); |
|
|
|
|
|
|
|
|
LOG_ERROR("get thread pool instane failed"); |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
return thread_pool->thread_num; |
|
|
return thread_pool->thread_num; |
|
|
|