[or-cvs] r16623: {tor} Make definition of tor_mutex_t go into compat.h, so that it (in tor/trunk: . src/common)

nickm at seul.org nickm at seul.org
Fri Aug 22 16:24:52 UTC 2008


Author: nickm
Date: 2008-08-22 12:24:52 -0400 (Fri, 22 Aug 2008)
New Revision: 16623

Modified:
   tor/trunk/
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
Log:
 r17848 at tombo:  nickm | 2008-08-22 12:10:11 -0400
 Make definition of tor_mutex_t go into compat.h, so that it is possible to inline mutexes in critical objects.  Add init/uninit functions for mutexes allocated inside other structs.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r17848] on 49666b30-7950-49c5-bedf-9dc8f3168102

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2008-08-22 16:24:47 UTC (rev 16622)
+++ tor/trunk/src/common/compat.c	2008-08-22 16:24:52 UTC (rev 16623)
@@ -1524,73 +1524,16 @@
 #endif
 #endif
 
-#if defined(USE_WIN32_THREADS) && 0
-/** A generic lock structure for multithreaded builds. */
-struct tor_mutex_t {
-  HANDLE handle;
-};
-tor_mutex_t *
-tor_mutex_new(void)
-{
-  tor_mutex_t *m;
-  m = tor_malloc_zero(sizeof(tor_mutex_t));
-  m->handle = CreateMutex(NULL, FALSE, NULL);
-  tor_assert(m->handle != NULL);
-  return m;
-}
+#if defined(USE_WIN32_THREADS)
 void
-tor_mutex_free(tor_mutex_t *m)
+tor_mutex_init(tor_mutex_t *m)
 {
-  CloseHandle(m->handle);
-  tor_free(m);
-}
-void
-tor_mutex_acquire(tor_mutex_t *m)
-{
-  DWORD r;
-  r = WaitForSingleObject(m->handle, INFINITE);
-  switch (r) {
-    case WAIT_ABANDONED: /* holding thread exited. */
-    case WAIT_OBJECT_0: /* we got the mutex normally. */
-      break;
-    case WAIT_TIMEOUT: /* Should never happen. */
-      tor_assert(0);
-      break;
-    case WAIT_FAILED:
-      log_warn(LD_GENERAL, "Failed to acquire mutex: %d",(int) GetLastError());
-  }
-}
-void
-tor_mutex_release(tor_mutex_t *m)
-{
-  BOOL r;
-  r = ReleaseMutex(m->handle);
-  if (!r) {
-    log_warn(LD_GENERAL, "Failed to release mutex: %d", (int) GetLastError());
-  }
-}
-unsigned long
-tor_get_thread_id(void)
-{
-  return (unsigned long)GetCurrentThreadId();
-}
-#elif defined(USE_WIN32_THREADS)
-/** A generic lock structure for multithreaded builds. */
-struct tor_mutex_t {
-  CRITICAL_SECTION mutex;
-};
-tor_mutex_t *
-tor_mutex_new(void)
-{
-  tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
   InitializeCriticalSection(&m->mutex);
-  return m;
 }
 void
-tor_mutex_free(tor_mutex_t *m)
+tor_mutex_uninit(tor_mutex_t *m)
 {
   DeleteCriticalSection(&m->mutex);
-  tor_free(m);
 }
 void
 tor_mutex_acquire(tor_mutex_t *m)
@@ -1609,18 +1552,12 @@
   return (unsigned long)GetCurrentThreadId();
 }
 #elif defined(USE_PTHREADS)
-/** A generic lock structure for multithreaded builds. */
-struct tor_mutex_t {
-  pthread_mutex_t mutex;
-};
 static pthread_mutexattr_t attr_reentrant;
 static int threads_initialized = 0;
-/** Allocate and return new lock. */
-tor_mutex_t *
-tor_mutex_new(void)
+void
+tor_mutex_init(tor_mutex_t *mutex)
 {
   int err;
-  tor_mutex_t *mutex = tor_malloc_zero(sizeof(tor_mutex_t));
   if (PREDICT_UNLIKELY(!threads_initialized))
     tor_threads_init();
   err = pthread_mutex_init(&mutex->mutex, &attr_reentrant);
@@ -1628,7 +1565,6 @@
     log_err(LD_GENERAL, "Error %d creating a mutex.", err);
     tor_fragile_assert();
   }
-  return mutex;
 }
 /** Wait until <b>m</b> is free, then acquire it. */
 void
@@ -1654,9 +1590,8 @@
     tor_fragile_assert();
   }
 }
-/** Free all storage held by the lock <b>m</b>. */
 void
-tor_mutex_free(tor_mutex_t *m)
+tor_mutex_uninit(tor_mutex_t *m)
 {
   int err;
   tor_assert(m);
@@ -1665,7 +1600,6 @@
     log_err(LD_GENERAL, "Error %d destroying a mutex.", err);
     tor_fragile_assert();
   }
-  tor_free(m);
 }
 /** Return an integer representing this thread. */
 unsigned long
@@ -1678,13 +1612,24 @@
   r.thr = pthread_self();
   return r.id;
 }
-#else
-/** A generic lock structure for multithreaded builds. */
-struct tor_mutex_t {
-  int _unused;
-};
 #endif
 
+#ifdef TOR_IS_MULTITHREADED
+tor_mutex_t *
+tor_mutex_new(void)
+{
+  tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
+  tor_mutex_init(m);
+  return m;
+}
+void
+tor_mutex_free(tor_mutex_t *m)
+{
+  tor_mutex_uninit(m);
+  tor_free(m);
+}
+#endif
+
 /* Conditions. */
 #ifdef USE_PTHREADS
 #if 0

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2008-08-22 16:24:47 UTC (rev 16622)
+++ tor/trunk/src/common/compat.h	2008-08-22 16:24:52 UTC (rev 16623)
@@ -426,19 +426,33 @@
  * Linux, etc), we need locking for them.  On platforms with poor thread
  * support or broken gethostbyname_r, these functions are no-ops. */
 
-typedef struct tor_mutex_t tor_mutex_t;
+/** A generic lock structure for multithreaded builds. */
+typedef struct tor_mutex_t {
+#if defined(USE_WIN32_THREADS)
+  CRITICAL_SECTION mutex;
+#elif defined(USE_PTHREADS)
+  pthread_mutex_t mutex;
+#else
+  int _unused;
+#endif
+} tor_mutex_t;
+
 #ifdef TOR_IS_MULTITHREADED
 tor_mutex_t *tor_mutex_new(void);
+void tor_mutex_init(tor_mutex_t *m);
 void tor_mutex_acquire(tor_mutex_t *m);
 void tor_mutex_release(tor_mutex_t *m);
 void tor_mutex_free(tor_mutex_t *m);
+void tor_mutex_uninit(tor_mutex_t *m);
 unsigned long tor_get_thread_id(void);
 void tor_threads_init(void);
 #else
 #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
+#define tor_mutex_init(m) STMT_NIL
 #define tor_mutex_acquire(m) STMT_NIL
 #define tor_mutex_release(m) STMT_NIL
 #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
+#define tor_mutex_uninit(m) STMT_NIL
 #define tor_get_thread_id() (1UL)
 #define tor_threads_init() STMT_NIL
 #endif



More information about the tor-commits mailing list