├── .editorconfig ├── thrdpool.h ├── msgqueue.h ├── README.md ├── msgqueue.c ├── thrdpool.c └── LICENSE /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # all files 5 | [*] 6 | indent_style = tab 7 | indent_size = 4 -------------------------------------------------------------------------------- /thrdpool.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Sogou, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | Author: Xie Han (xiehan@sogou-inc.com) 17 | */ 18 | 19 | #ifndef _THRDPOOL_H_ 20 | #define _THRDPOOL_H_ 21 | 22 | #include 23 | 24 | typedef struct __thrdpool thrdpool_t; 25 | 26 | struct thrdpool_task 27 | { 28 | void (*routine)(void *); 29 | void *context; 30 | }; 31 | 32 | #ifdef __cplusplus 33 | extern "C" 34 | { 35 | #endif 36 | 37 | thrdpool_t *thrdpool_create(size_t nthreads, size_t stacksize); 38 | int thrdpool_schedule(const struct thrdpool_task *task, thrdpool_t *pool); 39 | int thrdpool_in_pool(thrdpool_t *pool); 40 | int thrdpool_increase(thrdpool_t *pool); 41 | int thrdpool_decrease(thrdpool_t *pool); 42 | void thrdpool_exit(thrdpool_t *pool); 43 | void thrdpool_destroy(void (*pending)(const struct thrdpool_task *), 44 | thrdpool_t *pool); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif 51 | 52 | -------------------------------------------------------------------------------- /msgqueue.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020 Sogou, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | Author: Xie Han (xiehan@sogou-inc.com) 17 | */ 18 | 19 | #ifndef _MSGQUEUE_H_ 20 | #define _MSGQUEUE_H_ 21 | 22 | #include 23 | 24 | typedef struct __msgqueue msgqueue_t; 25 | 26 | #ifdef __cplusplus 27 | extern "C" 28 | { 29 | #endif 30 | 31 | /* A simple implementation of message queue. The max pending messages may 32 | * reach two times 'maxlen' when the queue is in blocking mode, and infinite 33 | * in nonblocking mode. 'linkoff' is the offset from the head of each message, 34 | * where spaces of one pointer size should be available for internal usage. 35 | * 'linkoff' can be positive or negative or zero. */ 36 | 37 | msgqueue_t *msgqueue_create(size_t maxlen, int linkoff); 38 | void *msgqueue_get(msgqueue_t *queue); 39 | void msgqueue_put(void *msg, msgqueue_t *queue); 40 | void msgqueue_put_head(void *msg, msgqueue_t *queue); 41 | void msgqueue_set_nonblock(msgqueue_t *queue); 42 | void msgqueue_set_block(msgqueue_t *queue); 43 | void msgqueue_destroy(msgqueue_t *queue); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 消息队列 2 | 这是一个C语言消息队列实现。内部使用get队列和put队列分离的机制,实现高并发。由于使用严格的加锁机制,消息严格保序。 3 | ## 接口 4 | ### 创建队列 5 | **msgqueue_t \*msgqueue_create(size_t maxlen, int linkoff);** 6 | 7 | @maxlen: put队列的最大长度。由于内部使用双队列,默认模式下实际未消费的消息数可能达到两倍maxlen。 8 | 9 | @linkoff: 指定一个消息拉链的偏移量。每一条进入消息队列的消息,用户需要在这个偏移位置保留一个指针的空间,用于内部拉链。linkoff可以是正数,还可以是负数或0。通过指定拉链偏移的方法,模块内部无需再为消息分配和释放内存了。 10 | 11 | 函数返回一个msgqueue_t \*类型的消息队列对象。或返回NULL表示失败,同时设置errno。 12 | 13 | ### 发送消息 14 | **void msgqueue_put(void \*msg, msgqueue_t \*queue);** 15 | 16 | @msg: 消息指针。注意 **(char \*)msg + linkoff**的位置,应该预留一个指针空间。 17 | 18 | @queue: 消息队列对象。 19 | 20 | 函数无返回值。在默认的堵塞模式下,如果put队列达到maxlen,则操作堵塞。而在非堵塞模式下,操作立即返回,消息被发送。 21 | 22 | ### 接收消息 23 | **void \*msgqueue_get(msgqueue_t \*queue);** 24 | 25 | @queue: 消息队列对象。 26 | 27 | 函数取出并返回最早写入一条消息。如果没有消息,在堵塞模式下将进行进行等待。在非堵塞模式下,返回NULL。 28 | 29 | ### 设置非堵塞模式 30 | **void msgqueue_set_nonblock(msgqueue_t \*queue);** 31 | 32 | @queue: 消息队列对象。 33 | 34 | 函数将消息队列设置为非堵塞。所有put或get操作,不会堵塞。在非堵塞模式下,队列长度没有限制。 35 | 36 | ### 设置堵塞模式 37 | **void msgqueue_set_block(msgqueue_t \*queue);** 38 | 39 | @queue: 消息队列对象。 40 | 41 | 函数将消息队列设置为默认的堵塞模式。 42 | 43 | ### 销毁消息队列 44 | **void msgqueue_destroy(msgqueue_t \*queue);** 45 | 46 | @queue: 要销毁的消息队列对象。 47 | 48 | 函数销毁消息队列。如果有消息还没有被接收,用户需要自行处理。 49 | 50 | ## 示例 51 | ~~~c 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include "msgqueue.h" 57 | 58 | int main(void) 59 | { 60 | struct msg 61 | { 62 | char str[64]; 63 | void *link; 64 | }; 65 | 66 | msgqueue_t *mq = msgqueue_create(10, 64/* offset of 'link' field*/); 67 | struct msg *in = (struct msg *)malloc(sizeof *in); 68 | strcpy(in->str, "hello"); 69 | msgqueue_put(in, mq); 70 | 71 | struct msg *out; 72 | out = msgqueue_get(mq); 73 | printf("%s\n", out->str); 74 | 75 | assert(in == out); 76 | free(in); 77 | return 0; 78 | } 79 | ~~~ 80 | 81 | # 线程池 82 | 线程池是基于消息队列实现的。利用了消息队列高并发高性能的特征,具备非常高的调度性能。 83 | ## 接口 84 | ### 创建线程池 85 | **thrdpool_t \*thrdpool_create(size_t nthreads, size_t stacksize);** 86 | 87 | @nthreads: 线程的数量。 88 | 89 | @stacksize: 线程的堆栈大小。0表示使用默认值。 90 | 91 | 函数成功返回thrdpoo_t \*类型的线程池对象。失败返回NULL并设置errno。 92 | 93 | ### 提交任务 94 | **int thrdpool_schedule(const struct thrdpool_task \*task, thrdpool_t \*pool);** 95 | 96 | @task: struct thrdpool_task类型任务指针,该结构包含了任务的函数入口和上下文。定义如下: 97 | ~~~c 98 | struct thrdpool_task 99 | { 100 | void (*routine)(void *); 101 | void *context; 102 | }; 103 | ~~~ 104 | @pool: 线程池对象。 105 | 106 | 函数返回0表示成功,返回-1表示提交失败,并设置errno。用户可以安全的在线程池的任务里调用该函数提交一个新的任务,哪怕此刻有另一个线程正在销毁线程池。 107 | 108 | ### 增加线程 109 | **int thrdpool_increase(thrdpool_t \*pool);** 110 | 111 | @pool:  线程池对象。 112 | 113 | 函数返回0表示成功,此时线程池增加了一个线程。返回-1表示失败,并设置errno。 114 | 115 | ### 减少线程 116 | **int thrdpool_decrease(thrdpool_t \*pool);** 117 | 118 | @pool:  线程池对象。 119 | 120 | 函数返回-1表示失败,并设置errno。当函数返回0代表成功,存在三种情况: 121 | * 当前有空闲线程,那么会有一个空闲线程退出。 122 | * 所有线程都正在工作,第一个进入空闲的线程将会退出。 123 | * 逻辑上线程数已经为负数,下一个被增加的线程会直接退出。 124 | 125 | 注意函数不会等待线程退出完毕,而总是立刻返回。 126 | 127 | ### 判断当前线程是否为线程池里的线程 128 | 129 | **int thrdpool_in_pool(thrdpool_t \*pool);** 130 | 131 | @pool:  线程池对象。 132 | 133 | 如果当前线程是线程池线程,返回1,否则返回0。 134 | 135 | ### 退出当前线程 136 | 137 | **void thrdpool_exit(thrdpool_t \*pool);** 138 | 139 | @pool:  线程池对象。 140 | 141 | 如果当前线程是线程池中的线程,立即退出并且线程池减少一个线程。如果调用线程不是在线程池中,无任何行为。 142 | 143 | ### 销毁线程池 144 | **void thrdpool_destroy(void (\*pending)(const struct thrdpool_task \*), thrdpool_t \*pool);** 145 | 146 | @pending: 用于返回未执行任务的回调函数。当pending为NULL,未执行的任务直接丢弃。 147 | 148 | @pool: 要销毁的线程池对象。 149 | 150 | 函数销毁线程池,并通过pending回调函数返回还没有被执行的任务。在一个线程任务里调用本函数是一个合法行为,这种情况下线程池正常被销毁,调用者线程不会立刻中断,而是正常运行至routine结束。 151 | 152 | 153 | -------------------------------------------------------------------------------- /msgqueue.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020 Sogou, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | Author: Xie Han (xiehan@sogou-inc.com) 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include "msgqueue.h" 23 | 24 | struct __msgqueue 25 | { 26 | size_t msg_max; 27 | size_t msg_cnt; 28 | int linkoff; 29 | int nonblock; 30 | void *head1; 31 | void *head2; 32 | void **get_head; 33 | void **put_head; 34 | void **put_tail; 35 | pthread_mutex_t get_mutex; 36 | pthread_mutex_t put_mutex; 37 | pthread_cond_t get_cond; 38 | pthread_cond_t put_cond; 39 | }; 40 | 41 | void msgqueue_set_nonblock(msgqueue_t *queue) 42 | { 43 | queue->nonblock = 1; 44 | pthread_mutex_lock(&queue->put_mutex); 45 | pthread_cond_signal(&queue->get_cond); 46 | pthread_cond_broadcast(&queue->put_cond); 47 | pthread_mutex_unlock(&queue->put_mutex); 48 | } 49 | 50 | void msgqueue_set_block(msgqueue_t *queue) 51 | { 52 | queue->nonblock = 0; 53 | } 54 | 55 | void msgqueue_put(void *msg, msgqueue_t *queue) 56 | { 57 | void **link = (void **)((char *)msg + queue->linkoff); 58 | 59 | *link = NULL; 60 | pthread_mutex_lock(&queue->put_mutex); 61 | while (queue->msg_cnt > queue->msg_max - 1 && !queue->nonblock) 62 | pthread_cond_wait(&queue->put_cond, &queue->put_mutex); 63 | 64 | *queue->put_tail = link; 65 | queue->put_tail = link; 66 | queue->msg_cnt++; 67 | pthread_mutex_unlock(&queue->put_mutex); 68 | pthread_cond_signal(&queue->get_cond); 69 | } 70 | 71 | void msgqueue_put_head(void *msg, msgqueue_t *queue) 72 | { 73 | void **link = (void **)((char *)msg + queue->linkoff); 74 | 75 | pthread_mutex_lock(&queue->put_mutex); 76 | while (*queue->get_head) 77 | { 78 | if (pthread_mutex_trylock(&queue->get_mutex) == 0) 79 | { 80 | pthread_mutex_unlock(&queue->put_mutex); 81 | *link = *queue->get_head; 82 | *queue->get_head = link; 83 | pthread_mutex_unlock(&queue->get_mutex); 84 | return; 85 | } 86 | } 87 | 88 | while (queue->msg_cnt > queue->msg_max - 1 && !queue->nonblock) 89 | pthread_cond_wait(&queue->put_cond, &queue->put_mutex); 90 | 91 | *link = *queue->put_head; 92 | if (*link == NULL) 93 | queue->put_tail = link; 94 | 95 | *queue->put_head = link; 96 | queue->msg_cnt++; 97 | pthread_mutex_unlock(&queue->put_mutex); 98 | pthread_cond_signal(&queue->get_cond); 99 | } 100 | 101 | static size_t __msgqueue_swap(msgqueue_t *queue) 102 | { 103 | void **get_head = queue->get_head; 104 | size_t cnt; 105 | 106 | pthread_mutex_lock(&queue->put_mutex); 107 | while (queue->msg_cnt == 0 && !queue->nonblock) 108 | pthread_cond_wait(&queue->get_cond, &queue->put_mutex); 109 | 110 | cnt = queue->msg_cnt; 111 | if (cnt > queue->msg_max - 1) 112 | pthread_cond_broadcast(&queue->put_cond); 113 | 114 | queue->get_head = queue->put_head; 115 | queue->put_head = get_head; 116 | queue->put_tail = get_head; 117 | queue->msg_cnt = 0; 118 | pthread_mutex_unlock(&queue->put_mutex); 119 | return cnt; 120 | } 121 | 122 | void *msgqueue_get(msgqueue_t *queue) 123 | { 124 | void *msg; 125 | 126 | pthread_mutex_lock(&queue->get_mutex); 127 | if (*queue->get_head || __msgqueue_swap(queue) > 0) 128 | { 129 | msg = (char *)*queue->get_head - queue->linkoff; 130 | *queue->get_head = *(void **)*queue->get_head; 131 | } 132 | else 133 | msg = NULL; 134 | 135 | pthread_mutex_unlock(&queue->get_mutex); 136 | return msg; 137 | } 138 | 139 | msgqueue_t *msgqueue_create(size_t maxlen, int linkoff) 140 | { 141 | msgqueue_t *queue = (msgqueue_t *)malloc(sizeof (msgqueue_t)); 142 | int ret; 143 | 144 | if (!queue) 145 | return NULL; 146 | 147 | ret = pthread_mutex_init(&queue->get_mutex, NULL); 148 | if (ret == 0) 149 | { 150 | ret = pthread_mutex_init(&queue->put_mutex, NULL); 151 | if (ret == 0) 152 | { 153 | ret = pthread_cond_init(&queue->get_cond, NULL); 154 | if (ret == 0) 155 | { 156 | ret = pthread_cond_init(&queue->put_cond, NULL); 157 | if (ret == 0) 158 | { 159 | queue->msg_max = maxlen; 160 | queue->linkoff = linkoff; 161 | queue->head1 = NULL; 162 | queue->head2 = NULL; 163 | queue->get_head = &queue->head1; 164 | queue->put_head = &queue->head2; 165 | queue->put_tail = &queue->head2; 166 | queue->msg_cnt = 0; 167 | queue->nonblock = 0; 168 | return queue; 169 | } 170 | 171 | pthread_cond_destroy(&queue->get_cond); 172 | } 173 | 174 | pthread_mutex_destroy(&queue->put_mutex); 175 | } 176 | 177 | pthread_mutex_destroy(&queue->get_mutex); 178 | } 179 | 180 | errno = ret; 181 | free(queue); 182 | return NULL; 183 | } 184 | 185 | void msgqueue_destroy(msgqueue_t *queue) 186 | { 187 | pthread_cond_destroy(&queue->put_cond); 188 | pthread_cond_destroy(&queue->get_cond); 189 | pthread_mutex_destroy(&queue->put_mutex); 190 | pthread_mutex_destroy(&queue->get_mutex); 191 | free(queue); 192 | } 193 | 194 | -------------------------------------------------------------------------------- /thrdpool.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Sogou, Inc. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | 16 | Author: Xie Han (xiehan@sogou-inc.com) 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include "msgqueue.h" 23 | #include "thrdpool.h" 24 | 25 | struct __thrdpool 26 | { 27 | msgqueue_t *msgqueue; 28 | size_t nthreads; 29 | size_t stacksize; 30 | pthread_t tid; 31 | pthread_mutex_t mutex; 32 | pthread_key_t key; 33 | pthread_cond_t *terminate; 34 | }; 35 | 36 | struct __thrdpool_task_entry 37 | { 38 | void *link; 39 | struct thrdpool_task task; 40 | }; 41 | 42 | static pthread_t __zero_tid; 43 | 44 | static void __thrdpool_exit_routine(void *context) 45 | { 46 | thrdpool_t *pool = (thrdpool_t *)context; 47 | pthread_t tid; 48 | 49 | /* One thread joins another. Don't need to keep all thread IDs. */ 50 | pthread_mutex_lock(&pool->mutex); 51 | tid = pool->tid; 52 | pool->tid = pthread_self(); 53 | if (--pool->nthreads == 0 && pool->terminate) 54 | pthread_cond_signal(pool->terminate); 55 | 56 | pthread_mutex_unlock(&pool->mutex); 57 | if (!pthread_equal(tid, __zero_tid)) 58 | pthread_join(tid, NULL); 59 | 60 | pthread_exit(NULL); 61 | } 62 | 63 | static void *__thrdpool_routine(void *arg) 64 | { 65 | thrdpool_t *pool = (thrdpool_t *)arg; 66 | struct __thrdpool_task_entry *entry; 67 | void (*task_routine)(void *); 68 | void *task_context; 69 | 70 | pthread_setspecific(pool->key, pool); 71 | while (!pool->terminate) 72 | { 73 | entry = (struct __thrdpool_task_entry *)msgqueue_get(pool->msgqueue); 74 | if (!entry) 75 | break; 76 | 77 | task_routine = entry->task.routine; 78 | task_context = entry->task.context; 79 | free(entry); 80 | task_routine(task_context); 81 | 82 | if (pool->nthreads == 0) 83 | { 84 | /* Thread pool was destroyed by the task. */ 85 | free(pool); 86 | return NULL; 87 | } 88 | } 89 | 90 | __thrdpool_exit_routine(pool); 91 | return NULL; 92 | } 93 | 94 | static void __thrdpool_terminate(int in_pool, thrdpool_t *pool) 95 | { 96 | pthread_cond_t term = PTHREAD_COND_INITIALIZER; 97 | 98 | pthread_mutex_lock(&pool->mutex); 99 | msgqueue_set_nonblock(pool->msgqueue); 100 | pool->terminate = &term; 101 | 102 | if (in_pool) 103 | { 104 | /* Thread pool destroyed in a pool thread is legal. */ 105 | pthread_detach(pthread_self()); 106 | pool->nthreads--; 107 | } 108 | 109 | while (pool->nthreads > 0) 110 | pthread_cond_wait(&term, &pool->mutex); 111 | 112 | pthread_mutex_unlock(&pool->mutex); 113 | if (!pthread_equal(pool->tid, __zero_tid)) 114 | pthread_join(pool->tid, NULL); 115 | 116 | pthread_cond_destroy(&term); 117 | } 118 | 119 | static int __thrdpool_create_threads(size_t nthreads, thrdpool_t *pool) 120 | { 121 | pthread_attr_t attr; 122 | pthread_t tid; 123 | int ret; 124 | 125 | ret = pthread_attr_init(&attr); 126 | if (ret == 0) 127 | { 128 | if (pool->stacksize) 129 | ret = pthread_attr_setstacksize(&attr, pool->stacksize); 130 | 131 | if (ret == 0) 132 | { 133 | pthread_mutex_lock(&pool->mutex); 134 | while (pool->nthreads < nthreads) 135 | { 136 | ret = pthread_create(&tid, &attr, __thrdpool_routine, pool); 137 | if (ret == 0) 138 | pool->nthreads++; 139 | else 140 | break; 141 | } 142 | 143 | pthread_mutex_unlock(&pool->mutex); 144 | } 145 | 146 | pthread_attr_destroy(&attr); 147 | if (ret == 0) 148 | return 0; 149 | 150 | __thrdpool_terminate(0, pool); 151 | } 152 | 153 | errno = ret; 154 | return -1; 155 | } 156 | 157 | thrdpool_t *thrdpool_create(size_t nthreads, size_t stacksize) 158 | { 159 | thrdpool_t *pool; 160 | int ret; 161 | 162 | pool = (thrdpool_t *)malloc(sizeof (thrdpool_t)); 163 | if (!pool) 164 | return NULL; 165 | 166 | pool->msgqueue = msgqueue_create(0, 0); 167 | if (pool->msgqueue) 168 | { 169 | ret = pthread_mutex_init(&pool->mutex, NULL); 170 | if (ret == 0) 171 | { 172 | ret = pthread_key_create(&pool->key, NULL); 173 | if (ret == 0) 174 | { 175 | pool->stacksize = stacksize; 176 | pool->nthreads = 0; 177 | pool->tid = __zero_tid; 178 | pool->terminate = NULL; 179 | if (__thrdpool_create_threads(nthreads, pool) >= 0) 180 | return pool; 181 | 182 | pthread_key_delete(pool->key); 183 | } 184 | 185 | pthread_mutex_destroy(&pool->mutex); 186 | } 187 | 188 | errno = ret; 189 | msgqueue_destroy(pool->msgqueue); 190 | } 191 | 192 | free(pool); 193 | return NULL; 194 | } 195 | 196 | void __thrdpool_schedule(const struct thrdpool_task *task, void *buf, 197 | thrdpool_t *pool) 198 | { 199 | ((struct __thrdpool_task_entry *)buf)->task = *task; 200 | msgqueue_put(buf, pool->msgqueue); 201 | } 202 | 203 | int thrdpool_schedule(const struct thrdpool_task *task, thrdpool_t *pool) 204 | { 205 | void *buf = malloc(sizeof (struct __thrdpool_task_entry)); 206 | 207 | if (buf) 208 | { 209 | __thrdpool_schedule(task, buf, pool); 210 | return 0; 211 | } 212 | 213 | return -1; 214 | } 215 | 216 | int thrdpool_in_pool(thrdpool_t *pool) 217 | { 218 | return pthread_getspecific(pool->key) == pool; 219 | } 220 | 221 | int thrdpool_increase(thrdpool_t *pool) 222 | { 223 | pthread_attr_t attr; 224 | pthread_t tid; 225 | int ret; 226 | 227 | ret = pthread_attr_init(&attr); 228 | if (ret == 0) 229 | { 230 | if (pool->stacksize) 231 | ret = pthread_attr_setstacksize(&attr, pool->stacksize); 232 | 233 | if (ret == 0) 234 | { 235 | pthread_mutex_lock(&pool->mutex); 236 | ret = pthread_create(&tid, &attr, __thrdpool_routine, pool); 237 | if (ret == 0) 238 | pool->nthreads++; 239 | 240 | pthread_mutex_unlock(&pool->mutex); 241 | } 242 | 243 | pthread_attr_destroy(&attr); 244 | if (ret == 0) 245 | return 0; 246 | } 247 | 248 | errno = ret; 249 | return -1; 250 | } 251 | 252 | int thrdpool_decrease(thrdpool_t *pool) 253 | { 254 | void *buf = malloc(sizeof (struct __thrdpool_task_entry)); 255 | struct __thrdpool_task_entry *entry; 256 | 257 | if (buf) 258 | { 259 | entry = (struct __thrdpool_task_entry *)buf; 260 | entry->task.routine = __thrdpool_exit_routine; 261 | entry->task.context = pool; 262 | msgqueue_put_head(entry, pool->msgqueue); 263 | return 0; 264 | } 265 | 266 | return -1; 267 | } 268 | 269 | void thrdpool_exit(thrdpool_t *pool) 270 | { 271 | if (thrdpool_in_pool(pool)) 272 | __thrdpool_exit_routine(pool); 273 | } 274 | 275 | void thrdpool_destroy(void (*pending)(const struct thrdpool_task *), 276 | thrdpool_t *pool) 277 | { 278 | int in_pool = thrdpool_in_pool(pool); 279 | struct __thrdpool_task_entry *entry; 280 | 281 | __thrdpool_terminate(in_pool, pool); 282 | while (1) 283 | { 284 | entry = (struct __thrdpool_task_entry *)msgqueue_get(pool->msgqueue); 285 | if (!entry) 286 | break; 287 | 288 | if (pending && entry->task.routine != __thrdpool_exit_routine) 289 | pending(&entry->task); 290 | 291 | free(entry); 292 | } 293 | 294 | pthread_key_delete(pool->key); 295 | pthread_mutex_destroy(&pool->mutex); 296 | msgqueue_destroy(pool->msgqueue); 297 | if (!in_pool) 298 | free(pool); 299 | } 300 | 301 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------