Use wrapped mutex_t/condvar_t instead of pthread_mutex/cond_t
This commit is contained in:
+16
-10
@@ -24,6 +24,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
#include <utils/mutex.h>
|
||||||
#include <utils/linked_list.h>
|
#include <utils/linked_list.h>
|
||||||
|
|
||||||
typedef struct private_dispatcher_t private_dispatcher_t;
|
typedef struct private_dispatcher_t private_dispatcher_t;
|
||||||
@@ -56,7 +57,7 @@ struct private_dispatcher_t {
|
|||||||
/**
|
/**
|
||||||
* session locking mutex
|
* session locking mutex
|
||||||
*/
|
*/
|
||||||
pthread_mutex_t mutex;
|
mutex_t *mutex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of sessions
|
* List of sessions
|
||||||
@@ -112,7 +113,7 @@ typedef struct {
|
|||||||
/** session instance */
|
/** session instance */
|
||||||
session_t *session;
|
session_t *session;
|
||||||
/** condvar to wait for session */
|
/** condvar to wait for session */
|
||||||
pthread_cond_t cond;
|
condvar_t *cond;
|
||||||
/** client host address, to prevent session hijacking */
|
/** client host address, to prevent session hijacking */
|
||||||
char *host;
|
char *host;
|
||||||
/** TRUE if session is in use */
|
/** TRUE if session is in use */
|
||||||
@@ -172,7 +173,7 @@ static session_entry_t *session_entry_create(private_dispatcher_t *this,
|
|||||||
entry = malloc_thing(session_entry_t);
|
entry = malloc_thing(session_entry_t);
|
||||||
entry->in_use = FALSE;
|
entry->in_use = FALSE;
|
||||||
entry->closed = FALSE;
|
entry->closed = FALSE;
|
||||||
pthread_cond_init(&entry->cond, NULL);
|
entry->cond = condvar_create(CONDVAR_TYPE_DEFAULT);
|
||||||
entry->session = load_session(this);
|
entry->session = load_session(this);
|
||||||
entry->used = time_monotonic(NULL);
|
entry->used = time_monotonic(NULL);
|
||||||
entry->host = strdup(host);
|
entry->host = strdup(host);
|
||||||
@@ -180,9 +181,13 @@ static session_entry_t *session_entry_create(private_dispatcher_t *this,
|
|||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* destroy a session
|
||||||
|
*/
|
||||||
static void session_entry_destroy(session_entry_t *entry)
|
static void session_entry_destroy(session_entry_t *entry)
|
||||||
{
|
{
|
||||||
entry->session->destroy(entry->session);
|
entry->session->destroy(entry->session);
|
||||||
|
entry->cond->destroy(entry->cond);
|
||||||
free(entry->host);
|
free(entry->host);
|
||||||
free(entry);
|
free(entry);
|
||||||
}
|
}
|
||||||
@@ -240,7 +245,7 @@ static void dispatch(private_dispatcher_t *this)
|
|||||||
now = time_monotonic(NULL);
|
now = time_monotonic(NULL);
|
||||||
|
|
||||||
/* find session */
|
/* find session */
|
||||||
pthread_mutex_lock(&this->mutex);
|
this->mutex->lock(this->mutex);
|
||||||
enumerator = this->sessions->create_enumerator(this->sessions);
|
enumerator = this->sessions->create_enumerator(this->sessions);
|
||||||
while (enumerator->enumerate(enumerator, ¤t))
|
while (enumerator->enumerate(enumerator, ¤t))
|
||||||
{
|
{
|
||||||
@@ -268,7 +273,7 @@ static void dispatch(private_dispatcher_t *this)
|
|||||||
/* wait until session is unused */
|
/* wait until session is unused */
|
||||||
while (found->in_use)
|
while (found->in_use)
|
||||||
{
|
{
|
||||||
pthread_cond_wait(&found->cond, &this->mutex);
|
found->cond->wait(found->cond, this->mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -277,18 +282,18 @@ static void dispatch(private_dispatcher_t *this)
|
|||||||
this->sessions->insert_first(this->sessions, found);
|
this->sessions->insert_first(this->sessions, found);
|
||||||
}
|
}
|
||||||
found->in_use = TRUE;
|
found->in_use = TRUE;
|
||||||
pthread_mutex_unlock(&this->mutex);
|
this->mutex->unlock(this->mutex);
|
||||||
|
|
||||||
/* start processing */
|
/* start processing */
|
||||||
found->session->process(found->session, request);
|
found->session->process(found->session, request);
|
||||||
found->used = time_monotonic(NULL);
|
found->used = time_monotonic(NULL);
|
||||||
|
|
||||||
/* release session */
|
/* release session */
|
||||||
pthread_mutex_lock(&this->mutex);
|
this->mutex->lock(this->mutex);
|
||||||
found->in_use = FALSE;
|
found->in_use = FALSE;
|
||||||
found->closed = request->session_closed(request);
|
found->closed = request->session_closed(request);
|
||||||
pthread_cond_signal(&found->cond);
|
this->mutex->unlock(this->mutex);
|
||||||
pthread_mutex_unlock(&this->mutex);
|
found->cond->signal(found->cond);
|
||||||
|
|
||||||
/* cleanup */
|
/* cleanup */
|
||||||
request->destroy(request);
|
request->destroy(request);
|
||||||
@@ -342,6 +347,7 @@ static void destroy(private_dispatcher_t *this)
|
|||||||
this->sessions->destroy_function(this->sessions, (void*)session_entry_destroy);
|
this->sessions->destroy_function(this->sessions, (void*)session_entry_destroy);
|
||||||
this->controllers->destroy_function(this->controllers, free);
|
this->controllers->destroy_function(this->controllers, free);
|
||||||
this->filters->destroy_function(this->filters, free);
|
this->filters->destroy_function(this->filters, free);
|
||||||
|
this->mutex->destroy(this->mutex);
|
||||||
free(this->threads);
|
free(this->threads);
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
@@ -364,7 +370,7 @@ dispatcher_t *dispatcher_create(char *socket, bool debug, int timeout,
|
|||||||
this->controllers = linked_list_create();
|
this->controllers = linked_list_create();
|
||||||
this->filters = linked_list_create();
|
this->filters = linked_list_create();
|
||||||
this->context_constructor = constructor;
|
this->context_constructor = constructor;
|
||||||
pthread_mutex_init(&this->mutex, NULL);
|
this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
|
||||||
this->param = param;
|
this->param = param;
|
||||||
this->fd = 0;
|
this->fd = 0;
|
||||||
this->timeout = timeout;
|
this->timeout = timeout;
|
||||||
|
|||||||
Reference in New Issue
Block a user