Tor  0.4.7.0-alpha-dev
compat_mutex.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file compat_mutex.h
8  *
9  * \brief Header for compat_mutex.c
10  **/
11 
12 #ifndef TOR_COMPAT_MUTEX_H
13 #define TOR_COMPAT_MUTEX_H
14 
15 #include "orconfig.h"
16 #include "lib/cc/torint.h"
17 #include "lib/malloc/malloc.h"
18 
19 #if defined(HAVE_PTHREAD_H) && !defined(_WIN32)
20 #include <pthread.h>
21 #endif
22 
23 #if defined(_WIN32)
24 #include <windows.h>
25 #endif
26 
27 #if defined(_WIN32)
28 #define USE_WIN32_THREADS
29 #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
30 #define USE_PTHREADS
31 #else
32 #error "No threading system was found"
33 #endif /* defined(_WIN32) || ... */
34 
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. */
38 
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 SRW locks. */
43  SRWLOCK mutex;
44  /** For recursive lock support (SRW locks are not recursive) */
45  enum mutex_type_t {
46  NON_RECURSIVE = 0,
47  RECURSIVE
48  } type;
49  LONG lock_owner; // id of the thread that owns the lock
50  int lock_count; // number of times the lock is held recursively
51 #elif defined(USE_PTHREADS)
52  /** Pthreads-only: with pthreads, we implement locks with
53  * pthread_mutex_t. */
54  pthread_mutex_t mutex;
55 #else
56  /** No-threads only: Dummy variable so that tor_mutex_t takes up space. */
57  int _unused;
58 #endif /* defined(USE_WIN32_THREADS) || ... */
59 } tor_mutex_t;
60 
68 /**
69  * @copydoc tor_mutex_free_
70  *
71  * Additionally, set the pointer <b>m</b> to NULL.
72  **/
73 #define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
75 
76 void tor_locking_init(void);
77 
78 #endif /* !defined(TOR_COMPAT_MUTEX_H) */
tor_mutex_t * tor_mutex_new(void)
Definition: compat_mutex.c:17
void tor_mutex_free_(tor_mutex_t *m)
Definition: compat_mutex.c:42
void tor_mutex_init_nonrecursive(tor_mutex_t *m)
void tor_mutex_release(tor_mutex_t *m)
void tor_mutex_init(tor_mutex_t *m)
void tor_mutex_acquire(tor_mutex_t *m)
tor_mutex_t * tor_mutex_new_nonrecursive(void)
Definition: compat_mutex.c:26
void tor_locking_init(void)
void tor_mutex_uninit(tor_mutex_t *m)
Headers for util_malloc.c.
Integer definitions used throughout Tor.