-
Notifications
You must be signed in to change notification settings - Fork 3
/
thread_pool.h
94 lines (85 loc) · 3.33 KB
/
thread_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* This file is part of the EasyDataManager Library.
*
* Copyright (C) 2013-2019 by Armink <[email protected]>
*
* Function: a thread pool base on RT-Thread
* Created on: 2013-11-14
*/
#ifndef THREAD_POOL_H_
#define THREAD_POOL_H_
#include <rtthread.h>
#include <string.h>
#define THREAD_POOL_VER "4.0.1"
#define THREAD_POOL_THREADS_INIT_TIME 30 /**< threads initialize average waiting time */
#define THREAD_POOL_JOB_DEFAULT_PRIORITY 10 /**< thread poll job's priority in rt-thread */
#define THREAD_POOL_JOB_TICK 5 /**< thread poll job's time slice in rt-thread */
#define THREAD_POOL_NAME_MAX RT_NAME_MAX /**< thread poll max name length */
/* thread pool error code */
typedef enum {
THREAD_POOL_NO_ERR, /**< no error */
THREAD_POOL_ALREADY_SHUTDOWN_ERR, /**< thread pool already shutdown */
THREAD_POOL_MEM_FULL_ERR, /**< memory full */
} thread_pool_err;
/* a task queue which run in thread pool */
typedef struct _thread_pool_task {
void (*process)(void *arg); /**< task callback function */
void *arg; /**< task callback function's arguments */
struct _thread_pool_task *next;
} thread_pool_task, *thread_pool_task_t;
/* thread pool struct */
typedef struct _thread_pool {
char name[THREAD_POOL_NAME_MAX + 1];/**< the name of ThreadPool, the end of name is include '\0' */
thread_pool_task_t queue_head; /**< task queue which place all waiting task */
rt_mutex_t user_lock; /**< a synchronized lock provided to user */
rt_mutex_t queue_lock; /**< task queue mutex lock */
rt_sem_t queue_ready; /**< a semaphore which for task queue ready */
uint8_t is_shutdown; /**< shutdown state,if shutdown the value will equal TRUE */
rt_thread_t* thread_id; /**< thread queue which in thread pool */
uint8_t max_thread_num; /**< the thread max number in thread pool */
uint8_t cur_wait_thread_num; /**< the current waiting thread number in thread pool */
/**
* This function will add a task to thread pool.
*
* @param pool the ThreadPool pointer
* @param process task function pointer
* @param arg task function arguments
*
* @return error code
*/
thread_pool_err (*add_task)(struct _thread_pool* const pool,
void (*process)(void *arg), void *arg);
/**
* This function will delete all task.
*
* @param pool
*
* @return error code
*/
thread_pool_err (*del_all)(struct _thread_pool* const pool);
/**
* This function will destroy thread pool.
*
* @param pool the ThreadPool pointer
*
* @return error code
*/
thread_pool_err (*destroy)(struct _thread_pool* pool);
/**
* This function will lock the synchronized lock.
*
* @param pool the ThreadPool pointer
*
*/
void (*lock)(struct _thread_pool* pool);
/**
* This function will unlock the synchronized lock.
*
* @param pool the ThreadPool pointer
*
*/
void (*unlock)(struct _thread_pool* pool);
} thread_pool, *thread_pool_t;
thread_pool_err init_thread_pool(thread_pool_t const pool, const char* name, uint8_t max_thread_num,
uint32_t thread_stack_size);
#endif /* THREAD_POOL_H_ */