[tor-commits] [tor/master] Add tor_cond_init/uninit

nickm at torproject.org nickm at torproject.org
Wed Jan 21 19:50:30 UTC 2015


commit 65016304d23503e230e8b097b5cdc1e4897b9b57
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon Sep 23 01:15:30 2013 -0400

    Add tor_cond_init/uninit
---
 src/common/compat_pthreads.c   |   17 ++++++-----------
 src/common/compat_threads.c    |   17 +++++++++++++++++
 src/common/compat_threads.h    |    2 ++
 src/common/compat_winthreads.c |   19 +++++++------------
 4 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c
index 0e5d33a..e58b3f7 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 <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>.
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c
index 84a8a21..e0cbf5c 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 bbd782f..6d3ba3a 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 634dfbe..11f91c6 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





More information about the tor-commits mailing list