1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 * \brief Header for compat_mutex.c
12 #ifndef TOR_COMPAT_MUTEX_H
13 #define TOR_COMPAT_MUTEX_H
16 #include "lib/cc/torint.h"
17 #include "lib/malloc/malloc.h"
19 #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
28 #define USE_WIN32_THREADS
29 #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
32 #error "No threading system was found"
33 #endif /* defined(_WIN32) || ... */
35 /* Because we use threads instead of processes on most platforms (Windows,
36 * Linux, etc), we need locking for them. On platforms with poor thread
37 * support or broken gethostbyname_r, these functions are no-ops. */
39 /** A generic lock structure for multithreaded builds. */
40 typedef struct tor_mutex_t
{
41 #if defined(USE_WIN32_THREADS)
42 /** Windows-only: on windows, we implement locks with CRITICAL_SECTIONS. */
43 CRITICAL_SECTION mutex
;
44 #elif defined(USE_PTHREADS)
45 /** Pthreads-only: with pthreads, we implement locks with
47 pthread_mutex_t mutex
;
49 /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
51 #endif /* defined(USE_WIN32_MUTEX) || ... */
54 tor_mutex_t
*tor_mutex_new(void);
55 tor_mutex_t
*tor_mutex_new_nonrecursive(void);
56 void tor_mutex_init(tor_mutex_t
*m
);
57 void tor_mutex_init_nonrecursive(tor_mutex_t
*m
);
58 void tor_mutex_acquire(tor_mutex_t
*m
);
59 void tor_mutex_release(tor_mutex_t
*m
);
60 void tor_mutex_free_(tor_mutex_t
*m
);
61 #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
62 void tor_mutex_uninit(tor_mutex_t
*m
);
64 void tor_locking_init(void);
66 #endif /* !defined(TOR_COMPAT_MUTEX_H) */