Minor HELP output fixes.
[pwmd.git] / src / mutex.h
blob3de0ec836b95e95cc684cb105fb4a6558e3bd9a9
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 #ifdef MUTEX_DEBUG
36 #define MUTEX_LOCK_DEBUG(m) do { \
37 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, __FUNCTION__, m);
38 } while (0)
39 #define MUTEX_UNLOCK_DEBUG(m) do { \
40 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, __FUNCTION__, m);
41 } while (0)
42 #else
43 #define MUTEX_LOCK_DEBUG(m)
44 #define MUTEX_UNLOCK_DEBUG(m)
45 #endif
47 #define MUTEX_LOCK(m) do { \
48 MUTEX_LOCK_DEBUG(m); \
49 pthread_mutex_lock(m); \
50 } while (0)
52 #define MUTEX_UNLOCK(m) do { \
53 MUTEX_UNLOCK_DEBUG(m); \
54 pthread_mutex_unlock(m); \
55 } while (0)
57 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
58 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
59 struct timespec ts; \
60 clock_gettime(CLOCK_REALTIME, &ts); \
61 time_t s = (timeout*100000000)/1000000000;\
62 long l = (timeout*100000000)%1000000000; \
63 ts.tv_sec += s; \
64 ts.tv_nsec += l; \
65 if (ts.tv_nsec >= 1000000000) {\
66 ts.tv_sec++; \
67 ts.tv_nsec -= 1000000000; \
68 } \
69 int n = pthread_mutex_timedlock(m, &ts); \
70 if (n == ETIMEDOUT) { \
71 rc = GPG_ERR_LOCKED;\
72 } \
73 else if (!n) { \
74 MUTEX_LOCK_DEBUG(m); \
75 } \
76 else { \
77 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(gpg_error_from_errno(n))); \
78 rc = gpg_error_from_errno(n);\
79 } \
80 } while (0)
81 #else
82 #define TIMED_LOCK_DURATION 100000
83 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
84 for (long elapsed = 0; ; elapsed++) { \
85 if (pthread_mutex_trylock(m) == EBUSY) { \
86 if (timeout == -1 || elapsed >= timeout) { \
87 rc = GPG_ERR_LOCKED; \
88 break; \
89 } \
90 usleep(TIMED_LOCK_DURATION); \
91 } \
92 else { \
93 MUTEX_LOCK_DEBUG(m); \
94 break;\
95 } \
96 } \
97 } while (0)
98 #endif
100 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
101 int n; \
102 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
103 if (t != -1 && ctx) { \
104 rc = send_status(ctx, STATUS_LOCKED, NULL); \
105 if (!rc && t != 0) { \
106 MUTEX_TIMED_LOCK(m, t, rc); \
108 else if (!rc) { \
109 MUTEX_LOCK(m); \
112 else \
113 rc = GPG_ERR_LOCKED; \
115 else if (n) \
116 rc = gpg_error_from_errno(n);\
117 else \
118 MUTEX_LOCK_DEBUG(m); \
119 } while (0)
121 #endif