- cancellation disabled by default
This commit is contained in:
@@ -96,19 +96,22 @@ status_t get_count(private_job_queue_t *this, int *count)
|
|||||||
status_t get(private_job_queue_t *this, job_t **job)
|
status_t get(private_job_queue_t *this, job_t **job)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
|
int oldstate;
|
||||||
pthread_mutex_lock(&(this->mutex));
|
pthread_mutex_lock(&(this->mutex));
|
||||||
// add mutex unlock handler for cancellation
|
/* go to wait while no jobs available */
|
||||||
pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
|
|
||||||
// go to wait while no jobs available
|
|
||||||
this->list->get_count(this->list,&count);
|
this->list->get_count(this->list,&count);
|
||||||
while(count == 0)
|
while(count == 0)
|
||||||
{
|
{
|
||||||
|
/* add mutex unlock handler for cancellation, enable cancellation */
|
||||||
|
pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
|
||||||
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
|
||||||
pthread_cond_wait( &(this->condvar), &(this->mutex));
|
pthread_cond_wait( &(this->condvar), &(this->mutex));
|
||||||
|
|
||||||
|
/* reset cancellation, remove mutex-unlock handler (without executing) */
|
||||||
|
pthread_setcancelstate(oldstate, NULL);
|
||||||
|
pthread_cleanup_pop(0);
|
||||||
this->list->get_count(this->list,&count);
|
this->list->get_count(this->list,&count);
|
||||||
}
|
}
|
||||||
// remove mutex-unlock handler (without executing)
|
|
||||||
pthread_cleanup_pop(0);
|
|
||||||
|
|
||||||
this->list->remove_first(this->list,(void **) job);
|
this->list->remove_first(this->list,(void **) job);
|
||||||
pthread_mutex_unlock(&(this->mutex));
|
pthread_mutex_unlock(&(this->mutex));
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ typedef struct {
|
|||||||
|
|
||||||
void job_processing(private_thread_pool_t *this)
|
void job_processing(private_thread_pool_t *this)
|
||||||
{
|
{
|
||||||
|
/* cancellation disabled by default */
|
||||||
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
job_t *job;
|
job_t *job;
|
||||||
job_queue->get(job_queue, &job);
|
job_queue->get(job_queue, &job);
|
||||||
@@ -113,6 +116,7 @@ thread_pool_t *thread_pool_create(size_t pool_size)
|
|||||||
this->pool_size = pool_size;
|
this->pool_size = pool_size;
|
||||||
this->threads = alloc_bytes(sizeof(pthread_t) * pool_size, "pthread_t[] of private_thread_pool_t");
|
this->threads = alloc_bytes(sizeof(pthread_t) * pool_size, "pthread_t[] of private_thread_pool_t");
|
||||||
|
|
||||||
|
|
||||||
/* try to create as many threads as possible, up tu pool_size */
|
/* try to create as many threads as possible, up tu pool_size */
|
||||||
for (current = 0; current < pool_size; current++) {
|
for (current = 0; current < pool_size; current++) {
|
||||||
if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))job_processing, this)) {
|
if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))job_processing, this)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user