mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
Merge branch 'master' of https://github.com/sogou/workflow into nossl
This commit is contained in:
@@ -16,16 +16,6 @@
|
||||
Author: Xie Han (xiehan@sogou-inc.com)
|
||||
*/
|
||||
|
||||
/*
|
||||
* This message queue originates from the project of Sogou C++ Workflow:
|
||||
* https://github.com/sogou/workflow
|
||||
*
|
||||
* The idea of this implementation is quite simple and obvious. When the
|
||||
* get_list is not empty, the consumer takes a message. Otherwise the consumer
|
||||
* waits till put_list is not empty, and swap two lists. This method performs
|
||||
* well when the queue is very busy, and the number of consumers is big.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include "msgqueue.h"
|
||||
#include "thrdpool.h"
|
||||
|
||||
@@ -126,19 +126,25 @@ static int __thrdpool_create_threads(size_t nthreads, thrdpool_t *pool)
|
||||
if (ret == 0)
|
||||
{
|
||||
if (pool->stacksize)
|
||||
pthread_attr_setstacksize(&attr, pool->stacksize);
|
||||
ret = pthread_attr_setstacksize(&attr, pool->stacksize);
|
||||
|
||||
while (pool->nthreads < nthreads)
|
||||
if (ret == 0)
|
||||
{
|
||||
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
|
||||
if (ret == 0)
|
||||
pool->nthreads++;
|
||||
else
|
||||
break;
|
||||
pthread_mutex_lock(&pool->mutex);
|
||||
while (pool->nthreads < nthreads)
|
||||
{
|
||||
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
|
||||
if (ret == 0)
|
||||
pool->nthreads++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&pool->mutex);
|
||||
}
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
if (pool->nthreads == nthreads)
|
||||
if (ret == 0)
|
||||
return 0;
|
||||
|
||||
__thrdpool_terminate(0, pool);
|
||||
@@ -227,14 +233,18 @@ int thrdpool_increase(thrdpool_t *pool)
|
||||
if (ret == 0)
|
||||
{
|
||||
if (pool->stacksize)
|
||||
pthread_attr_setstacksize(&attr, pool->stacksize);
|
||||
ret = pthread_attr_setstacksize(&attr, pool->stacksize);
|
||||
|
||||
pthread_mutex_lock(&pool->mutex);
|
||||
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
|
||||
if (ret == 0)
|
||||
pool->nthreads++;
|
||||
{
|
||||
pthread_mutex_lock(&pool->mutex);
|
||||
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
|
||||
if (ret == 0)
|
||||
pool->nthreads++;
|
||||
|
||||
pthread_mutex_unlock(&pool->mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&pool->mutex);
|
||||
pthread_attr_destroy(&attr);
|
||||
if (ret == 0)
|
||||
return 0;
|
||||
|
||||
@@ -34,18 +34,6 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Thread pool originates from project Sogou C++ Workflow
|
||||
* https://github.com/sogou/workflow
|
||||
*
|
||||
* A thread task can be scheduled by another task, which is very important,
|
||||
* even if the pool is being destroyed. Because thread task is hard to know
|
||||
* what's happening to the pool.
|
||||
* The thread pool can also be destroyed by a thread task. This may sound
|
||||
* strange, but it's very logical. Destroying thread pool in thread task
|
||||
* does not end the task thread. It'll run till the end of task.
|
||||
*/
|
||||
|
||||
thrdpool_t *thrdpool_create(size_t nthreads, size_t stacksize);
|
||||
int thrdpool_schedule(const struct thrdpool_task *task, thrdpool_t *pool);
|
||||
int thrdpool_in_pool(thrdpool_t *pool);
|
||||
|
||||
Reference in New Issue
Block a user