Add tor_cond_init/uninit

This commit is contained in:
Nick Mathewson 2013-09-23 01:15:30 -04:00
parent e865248156
commit 65016304d2
4 changed files with 32 additions and 23 deletions

View File

@ -148,28 +148,23 @@ tor_get_thread_id(void)
/* Conditions. */
/** Return a newly allocated condition, with nobody waiting on it. */
tor_cond_t *
tor_cond_new(void)
int
tor_cond_init(tor_cond_t *cond)
{
tor_cond_t *cond = tor_malloc_zero(sizeof(tor_cond_t));
memset(cond, 0, sizeof(tor_cond_t));
if (pthread_cond_init(&cond->cond, NULL)) {
tor_free(cond);
return NULL;
return -1;
}
return cond;
return 0;
}
/** Release all resources held by <b>cond</b>. */
void
tor_cond_free(tor_cond_t *cond)
tor_cond_uninit(tor_cond_t *cond)
{
if (!cond)
return;
if (pthread_cond_destroy(&cond->cond)) {
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
return;
}
tor_free(cond);
}
/** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
* All waiters on the condition must wait holding the same <b>mutex</b>.

View File

@ -24,6 +24,23 @@ tor_mutex_free(tor_mutex_t *m)
tor_free(m);
}
tor_cond_t *
tor_cond_new(void)
{
tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
if (tor_cond_init(cond)<0)
tor_free(cond);
return cond;
}
void
tor_cond_free(tor_cond_t *c)
{
if (!c)
return;
tor_cond_uninit(c);
tor_free(c);
}
/** Identity of the "main" thread */
static unsigned long main_thread_id = -1;

View File

@ -74,6 +74,8 @@ typedef struct tor_cond_t {
tor_cond_t *tor_cond_new(void);
void tor_cond_free(tor_cond_t *cond);
int tor_cond_init(tor_cond_t *cond);
void tor_cond_uninit(tor_cond_t *cond);
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
const struct timeval *tv);
void tor_cond_signal_one(tor_cond_t *cond);

View File

@ -70,30 +70,25 @@ tor_get_thread_id(void)
return (unsigned long)GetCurrentThreadId();
}
tor_cond_t *
tor_cond_new(void)
int
tor_cond_init(tor_cond_t *cond)
{
tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
memset(cond, 0, sizeof(tor_cond_t));
if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
tor_free(cond);
return NULL;
return -1;
}
if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
DeleteCriticalSection(&cond->lock);
tor_free(cond);
return NULL;
return -1;
}
cond->n_waiting = cond->n_to_wake = cond->generation = 0;
return cond;
return 0;
}
void
tor_cond_free(tor_cond_t *cond)
tor_cond_uninit(tor_cond_t *cond)
{
if (!cond)
return;
DeleteCriticalSection(&cond->lock);
CloseHandle(cond->event);
mm_free(cond);
}
static void