summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-08-22 16:24:52 +0000
committerNick Mathewson <nickm@torproject.org>2008-08-22 16:24:52 +0000
commit88e61626492741ee045cef1e716abf6ca580eb88 (patch)
tree0f4096860cd8e12e6e397c3c06ea921574cae01e
parent0800b332a0ccd82ad63fcd1b7bf42b3a8f854422 (diff)
downloadtor-88e61626492741ee045cef1e716abf6ca580eb88.tar.gz
tor-88e61626492741ee045cef1e716abf6ca580eb88.zip
r17848@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. svn:r16623
-rw-r--r--src/common/compat.c99
-rw-r--r--src/common/compat.h16
2 files changed, 37 insertions, 78 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 927a3eebbb..fcb6f9888d 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1524,73 +1524,16 @@ tor_gmtime_r(const time_t *timep, struct tm *result)
#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;
-}
-void
-tor_mutex_free(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());
- }
-}
+#if defined(USE_WIN32_THREADS)
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)
+tor_mutex_init(tor_mutex_t *m)
{
- 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 @@ tor_get_thread_id(void)
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 @@ tor_mutex_new(void)
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_mutex_release(tor_mutex_t *m)
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 @@ tor_mutex_free(tor_mutex_t *m)
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,11 +1612,22 @@ tor_get_thread_id(void)
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. */
diff --git a/src/common/compat.h b/src/common/compat.h
index da181ad399..f21a208ed3 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -426,19 +426,33 @@ void spawn_exit(void) ATTR_NORETURN;
* 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