Version 3.0.12.
[pwmd.git] / src / mutex.h
blob3b239359c5a45dae7e1f6cb84c49280b0f19007a
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef MUTEX_H
21 #define MUTEX_H
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
28 #include <time.h>
29 #endif
31 #include <pthread.h>
32 #include <assert.h>
33 #include <errno.h>
35 #define INIT_TIMESPEC(t, ts) do { \
36 long s = (t*100000000)/1000000000; \
37 long l = (t*100000000)%1000000000; \
38 clock_gettime(CLOCK_REALTIME, &ts); \
39 ts.tv_sec += s; \
40 if (ts.tv_nsec + l >= 1000000000) { \
41 ts.tv_sec++; \
42 ts.tv_nsec = 0; \
43 } \
44 else \
45 ts.tv_nsec += l; \
46 } while (0)
48 #ifdef MUTEX_DEBUG
49 #define MUTEX_LOCK_DEBUG(m) do { \
50 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
51 __FUNCTION__, m); \
52 } while (0)
54 #define MUTEX_UNLOCK_DEBUG(m) do { \
55 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
56 __FUNCTION__, m); \
57 } while (0)
58 #else
59 #define MUTEX_LOCK_DEBUG(m)
60 #define MUTEX_UNLOCK_DEBUG(m)
61 #endif
63 #define MUTEX_LOCK(m) do { \
64 MUTEX_LOCK_DEBUG(m); \
65 pthread_mutex_lock(m); \
66 } while (0)
68 #define MUTEX_UNLOCK(m) do { \
69 MUTEX_UNLOCK_DEBUG(m); \
70 pthread_mutex_unlock(m); \
71 } while (0)
73 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
74 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
75 struct timespec ts; \
76 INIT_TIMESPEC(timeout, ts); \
77 int n = pthread_mutex_timedlock(m, &ts); \
78 if (n == ETIMEDOUT) { \
79 rc = GPG_ERR_LOCKED; \
80 } \
81 else if (!n) { \
82 MUTEX_LOCK_DEBUG(m); \
83 } \
84 else { \
85 log_write("%s(%i): %s", __FILE__, __LINE__, \
86 pwmd_strerror(gpg_error_from_errno(n))); \
87 rc = gpg_error_from_errno(n); \
88 } \
89 } while (0)
90 #else
91 #define TIMED_LOCK_DURATION 100000
92 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
93 for (long elapsed = 0; ; elapsed++) { \
94 if (pthread_mutex_trylock(m) == EBUSY) { \
95 if (timeout == -1 || elapsed >= timeout) { \
96 rc = GPG_ERR_LOCKED; \
97 break; \
98 } \
99 usleep(TIMED_LOCK_DURATION); \
101 else { \
102 MUTEX_LOCK_DEBUG(m); \
103 break; \
106 } while (0)
107 #endif
109 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
110 int n; \
111 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
112 if (t != -1 && ctx) { \
113 rc = send_status(ctx, STATUS_LOCKED, NULL); \
114 if (!rc && t != 0) { \
115 MUTEX_TIMED_LOCK(m, t, rc); \
117 else if (!rc) { \
118 MUTEX_LOCK(m); \
121 else \
122 rc = GPG_ERR_LOCKED; \
124 else if (n) \
125 rc = gpg_error_from_errno(n); \
126 else \
127 MUTEX_LOCK_DEBUG(m); \
128 } while (0)
130 #endif