Update working thread count without allocation.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005-2011 Martin Willi
|
* Copyright (C) 2005-2011 Martin Willi
|
||||||
* Copyright (C) 2011 revosec AG
|
* Copyright (C) 2011 revosec AG
|
||||||
|
* Copyright (C) 2008-2011 Tobias Brunner
|
||||||
* Copyright (C) 2005 Jan Hutter
|
* Copyright (C) 2005 Jan Hutter
|
||||||
* Hochschule fuer Technik Rapperswil
|
* Hochschule fuer Technik Rapperswil
|
||||||
*
|
*
|
||||||
@@ -25,9 +26,9 @@
|
|||||||
#include <threading/thread.h>
|
#include <threading/thread.h>
|
||||||
#include <threading/condvar.h>
|
#include <threading/condvar.h>
|
||||||
#include <threading/mutex.h>
|
#include <threading/mutex.h>
|
||||||
|
#include <threading/thread_value.h>
|
||||||
#include <utils/linked_list.h>
|
#include <utils/linked_list.h>
|
||||||
|
|
||||||
|
|
||||||
typedef struct private_processor_t private_processor_t;
|
typedef struct private_processor_t private_processor_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,6 +72,11 @@ struct private_processor_t {
|
|||||||
*/
|
*/
|
||||||
int prio_threads[JOB_PRIO_MAX];
|
int prio_threads[JOB_PRIO_MAX];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Priority of the job executed by a thread
|
||||||
|
*/
|
||||||
|
thread_value_t *priority;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* access to job lists is locked through this mutex
|
* access to job lists is locked through this mutex
|
||||||
*/
|
*/
|
||||||
@@ -113,23 +119,14 @@ static void restart(private_processor_t *this)
|
|||||||
this->mutex->unlock(this->mutex);
|
this->mutex->unlock(this->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Data needed to decrement the working thread count of a priority class
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
private_processor_t *this;
|
|
||||||
u_int priority;
|
|
||||||
} decrement_data_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrement working thread count of a priority class
|
* Decrement working thread count of a priority class
|
||||||
*/
|
*/
|
||||||
static void decrement_working_threads(decrement_data_t *dec)
|
static void decrement_working_threads(private_processor_t *this)
|
||||||
{
|
{
|
||||||
dec->this->mutex->lock(dec->this->mutex);
|
this->mutex->lock(this->mutex);
|
||||||
dec->this->working_threads[dec->priority]--;
|
this->working_threads[(intptr_t)this->priority->get(this->priority)]--;
|
||||||
dec->this->mutex->unlock(dec->this->mutex);
|
this->mutex->unlock(this->mutex);
|
||||||
free(dec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -181,16 +178,11 @@ static void process_jobs(private_processor_t *this)
|
|||||||
if (this->jobs[i]->remove_first(this->jobs[i],
|
if (this->jobs[i]->remove_first(this->jobs[i],
|
||||||
(void**)&job) == SUCCESS)
|
(void**)&job) == SUCCESS)
|
||||||
{
|
{
|
||||||
decrement_data_t *dec;
|
|
||||||
|
|
||||||
this->working_threads[i]++;
|
this->working_threads[i]++;
|
||||||
this->mutex->unlock(this->mutex);
|
this->mutex->unlock(this->mutex);
|
||||||
INIT(dec,
|
this->priority->set(this->priority, (void*)(intptr_t)i);
|
||||||
.this = this,
|
|
||||||
.priority = i,
|
|
||||||
);
|
|
||||||
thread_cleanup_push((thread_cleanup_t)decrement_working_threads,
|
thread_cleanup_push((thread_cleanup_t)decrement_working_threads,
|
||||||
dec);
|
this);
|
||||||
/* terminated threads are restarted to get a constant pool */
|
/* terminated threads are restarted to get a constant pool */
|
||||||
thread_cleanup_push((thread_cleanup_t)restart, this);
|
thread_cleanup_push((thread_cleanup_t)restart, this);
|
||||||
job->execute(job);
|
job->execute(job);
|
||||||
@@ -198,7 +190,6 @@ static void process_jobs(private_processor_t *this)
|
|||||||
thread_cleanup_pop(FALSE);
|
thread_cleanup_pop(FALSE);
|
||||||
this->mutex->lock(this->mutex);
|
this->mutex->lock(this->mutex);
|
||||||
this->working_threads[i]--;
|
this->working_threads[i]--;
|
||||||
free(dec);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -329,6 +320,7 @@ METHOD(processor_t, destroy, void,
|
|||||||
current->join(current);
|
current->join(current);
|
||||||
}
|
}
|
||||||
this->mutex->unlock(this->mutex);
|
this->mutex->unlock(this->mutex);
|
||||||
|
this->priority->destroy(this->priority);
|
||||||
this->thread_terminated->destroy(this->thread_terminated);
|
this->thread_terminated->destroy(this->thread_terminated);
|
||||||
this->job_added->destroy(this->job_added);
|
this->job_added->destroy(this->job_added);
|
||||||
this->mutex->destroy(this->mutex);
|
this->mutex->destroy(this->mutex);
|
||||||
@@ -359,6 +351,7 @@ processor_t *processor_create()
|
|||||||
.destroy = _destroy,
|
.destroy = _destroy,
|
||||||
},
|
},
|
||||||
.threads = linked_list_create(),
|
.threads = linked_list_create(),
|
||||||
|
.priority = thread_value_create(NULL),
|
||||||
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
|
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
|
||||||
.job_added = condvar_create(CONDVAR_TYPE_DEFAULT),
|
.job_added = condvar_create(CONDVAR_TYPE_DEFAULT),
|
||||||
.thread_terminated = condvar_create(CONDVAR_TYPE_DEFAULT),
|
.thread_terminated = condvar_create(CONDVAR_TYPE_DEFAULT),
|
||||||
|
|||||||
Reference in New Issue
Block a user