Using the thread wrapper in charon, libstrongswan and their plugins.
This commit is contained in:
@@ -15,13 +15,14 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "processor.h"
|
||||
|
||||
#include <daemon.h>
|
||||
#include <threading/thread.h>
|
||||
#include <threading/condvar.h>
|
||||
#include <threading/mutex.h>
|
||||
#include <utils/linked_list.h>
|
||||
|
||||
@@ -80,17 +81,21 @@ static void process_jobs(private_processor_t *this);
|
||||
*/
|
||||
static void restart(private_processor_t *this)
|
||||
{
|
||||
pthread_t thread;
|
||||
thread_t *thread;
|
||||
|
||||
/* respawn thread if required */
|
||||
this->mutex->lock(this->mutex);
|
||||
if (this->desired_threads == 0 ||
|
||||
pthread_create(&thread, NULL, (void*)process_jobs, this) != 0)
|
||||
(thread = thread_create((thread_main_t)process_jobs, this)) == NULL)
|
||||
{
|
||||
this->mutex->lock(this->mutex);
|
||||
this->total_threads--;
|
||||
this->thread_terminated->broadcast(this->thread_terminated);
|
||||
this->mutex->unlock(this->mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
thread->detach(thread);
|
||||
}
|
||||
this->mutex->unlock(this->mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,11 +103,11 @@ static void restart(private_processor_t *this)
|
||||
*/
|
||||
static void process_jobs(private_processor_t *this)
|
||||
{
|
||||
int oldstate;
|
||||
bool oldstate;
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
|
||||
oldstate = thread_cancelability(FALSE);
|
||||
|
||||
DBG2(DBG_JOB, "started worker thread, thread_ID: %06u", (int)pthread_self());
|
||||
DBG2(DBG_JOB, "started worker thread, thread_ID: %u", thread_current_id());
|
||||
|
||||
this->mutex->lock(this->mutex);
|
||||
while (this->desired_threads >= this->total_threads)
|
||||
@@ -119,9 +124,9 @@ static void process_jobs(private_processor_t *this)
|
||||
this->list->remove_first(this->list, (void**)&job);
|
||||
this->mutex->unlock(this->mutex);
|
||||
/* terminated threads are restarted, so we have a constant pool */
|
||||
pthread_cleanup_push((void*)restart, this);
|
||||
thread_cleanup_push((thread_cleanup_t)restart, this);
|
||||
job->execute(job);
|
||||
pthread_cleanup_pop(0);
|
||||
thread_cleanup_pop(FALSE);
|
||||
this->mutex->lock(this->mutex);
|
||||
}
|
||||
this->total_threads--;
|
||||
@@ -185,14 +190,16 @@ static void set_threads(private_processor_t *this, u_int count)
|
||||
if (count > this->total_threads)
|
||||
{ /* increase thread count */
|
||||
int i;
|
||||
pthread_t current;
|
||||
thread_t *current;
|
||||
|
||||
this->desired_threads = count;
|
||||
DBG1(DBG_JOB, "spawning %d worker threads", count - this->total_threads);
|
||||
for (i = this->total_threads; i < count; i++)
|
||||
{
|
||||
if (pthread_create(¤t, NULL, (void*)process_jobs, this) == 0)
|
||||
current = thread_create((thread_main_t)process_jobs, this);
|
||||
if (current)
|
||||
{
|
||||
current->detach(current);
|
||||
this->total_threads++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,14 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "scheduler.h"
|
||||
|
||||
#include <daemon.h>
|
||||
#include <processing/processor.h>
|
||||
#include <processing/jobs/callback_job.h>
|
||||
#include <threading/thread.h>
|
||||
#include <threading/condvar.h>
|
||||
#include <threading/mutex.h>
|
||||
|
||||
/* the initial size of the heap */
|
||||
@@ -185,8 +186,7 @@ static job_requeue_t schedule(private_scheduler_t * this)
|
||||
{
|
||||
timeval_t now;
|
||||
event_t *event;
|
||||
int oldstate;
|
||||
bool timed = FALSE;
|
||||
bool timed = FALSE, oldstate;
|
||||
|
||||
this->mutex->lock(this->mutex);
|
||||
|
||||
@@ -215,8 +215,8 @@ static job_requeue_t schedule(private_scheduler_t * this)
|
||||
}
|
||||
timed = TRUE;
|
||||
}
|
||||
pthread_cleanup_push((void*)this->mutex->unlock, this->mutex);
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
|
||||
thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
|
||||
oldstate = thread_cancelability(TRUE);
|
||||
|
||||
if (timed)
|
||||
{
|
||||
@@ -227,8 +227,8 @@ static job_requeue_t schedule(private_scheduler_t * this)
|
||||
DBG2(DBG_JOB, "no events, waiting");
|
||||
this->condvar->wait(this->condvar, this->mutex);
|
||||
}
|
||||
pthread_setcancelstate(oldstate, NULL);
|
||||
pthread_cleanup_pop(TRUE);
|
||||
thread_cancelability(oldstate);
|
||||
thread_cleanup_pop(TRUE);
|
||||
return JOB_REQUEUE_DIRECT;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user