diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c
index 0e5d33a659..e58b3f7b57 100644
--- a/src/common/compat_pthreads.c
+++ b/src/common/compat_pthreads.c
@@ -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 cond. */
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 cond.
* All waiters on the condition must wait holding the same mutex.
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c
index 84a8a21fe2..e0cbf5c1d8 100644
--- a/src/common/compat_threads.c
+++ b/src/common/compat_threads.c
@@ -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;
diff --git a/src/common/compat_threads.h b/src/common/compat_threads.h
index bbd782fd45..6d3ba3ae21 100644
--- a/src/common/compat_threads.h
+++ b/src/common/compat_threads.h
@@ -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);
diff --git a/src/common/compat_winthreads.c b/src/common/compat_winthreads.c
index 634dfbed30..11f91c63df 100644
--- a/src/common/compat_winthreads.c
+++ b/src/common/compat_winthreads.c
@@ -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