blob: c5427adae651312b0c15cc86d34407bbaa61ee03 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/* Libottery by Nick Mathewson.
This software has been dedicated to the public domain under the CC0
public domain dedication.
To the extent possible under law, the person who associated CC0 with
libottery has waived all copyright and related or neighboring rights
to libottery.
You should have received a copy of the CC0 legalcode along with this
work in doc/cc0.txt. If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef OTTERY_LOCKING_H_HEADER_INCLUDED_
#define OTTERY_LOCKING_H_HEADER_INCLUDED_
/* We don't need locks when building rspamd */
#ifdef BUILD_RSPAMD
#define OTTERY_NO_LOCKS
#endif
/* Locks */
#ifdef OTTERY_NO_LOCKS
/* Nothing here. */
#elif defined(__APPLE__) && !defined(OTTERY_NO_SPINLOCKS)
#define OTTERY_OSATOMIC_LOCKS
#include <libkern/OSAtomic.h>
#elif defined(_WIN32)
#define OTTERY_CRITICAL_SECTION
#include <windows.h>
#elif defined(HAVE_PTHREAD)
#define OTTERY_PTHREADS
#include <pthread.h>
#else
#define OTTERY_NO_LOCKS
#endif
#ifdef OTTERY_NO_LOCKS
#define DECL_LOCK(mutex)
#elif defined(OTTERY_OSATOMIC_LOCKS)
#define DECL_LOCK(mutex) OSSpinLock mutex;
#elif defined(OTTERY_CRITICAL_SECTION)
#define DECL_LOCK(mutex) CRITICAL_SECTION mutex;
#elif defined(OTTERY_PTHREADS)
#define DECL_LOCK(mutex) pthread_mutex_t mutex;
#endif
#if defined(OTTERY_PTHREADS)
#define INIT_LOCK(mutex) \
(pthread_mutex_init((mutex), NULL) != 0)
/** Acquire the lock for the state "st". */
#define ACQUIRE_LOCK(mutex) do { \
pthread_mutex_lock(mutex); \
} while (0)
/** Release the lock for the state "st". */
#define RELEASE_LOCK(mutex) do { \
pthread_mutex_unlock(mutex); \
} while (0)
#define DESTROY_LOCK(mutex) do { \
pthread_mutex_destroy(mutex); \
} while (0)
#elif defined(OTTERY_CRITICAL_SECTION)
#define INIT_LOCK(mutex) \
(InitializeCriticalSectionAndSpinCount((mutex), 3000) == 0)
#define ACQUIRE_LOCK(mutex) do { \
EnterCriticalSection(mutex); \
} while (0)
#define RELEASE_LOCK(mutex) do { \
LeaveCriticalSection(mutex); \
} while (0)
#define DESTROY_LOCK(mutex) do { \
DeleteCriticalSection(mutex); \
} while (0)
#elif defined(OTTERY_OSATOMIC_LOCKS)
#define INIT_LOCK(mutex) \
((*(mutex) = 0), 0)
#define ACQUIRE_LOCK(mutex) do { \
OSSpinLockLock(mutex); \
} while (0)
#define RELEASE_LOCK(mutex) do { \
OSSpinLockUnlock(mutex); \
} while (0)
#define DESTROY_LOCK(mutex) ((void)0)
#elif defined(OTTERY_NO_LOCKS)
#define INIT_LOCK(mutex) (0)
#define DESTROY_LOCK(mutex) ((void)0)
#define ACQUIRE_LOCK(mutex) ((void)0)
#define RELEASE_LOCK(mutex) ((void)0)
#else
#error How do I lock?
#endif
#endif
|