Update copyright year to 2015.
[libpwmd.git] / src / mutex.h
blobc570bc0ebabfced7f7c977d64940b0123ff28d39
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
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 /* Cant test thread cancellation so disable it for now. usleep() is a POSIX
28 * cancellation point. */
29 #undef HAVE_PTHREAD_MUTEX_TIMEDLOCK
31 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
32 #include <time.h>
33 #endif
35 #include <pthread.h>
36 #include <assert.h>
37 #include <errno.h>
39 #define INIT_TIMESPEC(t, ts) do { \
40 long s = (t*100000000)/1000000000; \
41 long l = (t*100000000)%1000000000; \
42 clock_gettime(CLOCK_REALTIME, &ts); \
43 ts.tv_sec += s; \
44 if (ts.tv_nsec + l >= 1000000000) { \
45 ts.tv_sec++; \
46 ts.tv_nsec = 0; \
47 } \
48 else \
49 ts.tv_nsec += l; \
50 } while (0)
52 #ifdef MUTEX_DEBUG
53 #define MUTEX_LOCK_DEBUG(m) do { \
54 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
55 __FUNCTION__, m); \
56 } while (0)
58 #define MUTEX_UNLOCK_DEBUG(m) do { \
59 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
60 __FUNCTION__, m); \
61 } while (0)
62 #else
63 #define MUTEX_LOCK_DEBUG(m)
64 #define MUTEX_UNLOCK_DEBUG(m)
65 #endif
67 #define MUTEX_LOCK(m) do { \
68 MUTEX_LOCK_DEBUG(m); \
69 pthread_mutex_lock(m); \
70 } while (0)
72 #define MUTEX_UNLOCK(m) do { \
73 MUTEX_UNLOCK_DEBUG(m); \
74 pthread_mutex_unlock(m); \
75 } while (0)
77 #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
78 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
79 struct timespec ts; \
80 INIT_TIMESPEC(timeout, ts); \
81 int n = pthread_mutex_timedlock(m, &ts); \
82 if (n == ETIMEDOUT) { \
83 rc = GPG_ERR_LOCKED; \
84 } \
85 else if (!n) { \
86 MUTEX_LOCK_DEBUG(m); \
87 } \
88 else { \
89 log_write("%s(%i): %s", __FILE__, __LINE__, \
90 pwmd_strerror(gpg_error_from_errno(n))); \
91 rc = gpg_error_from_errno(n); \
92 } \
93 } while (0)
94 #else
95 #define TIMED_LOCK_DURATION 100000
96 #define MUTEX_TIMED_LOCK(m, timeout, rc) do { \
97 for (long elapsed = 0; ; elapsed++) { \
98 if (pthread_mutex_trylock(m) == EBUSY) { \
99 if (timeout == -1 || elapsed >= timeout) { \
100 rc = GPG_ERR_LOCKED; \
101 break; \
103 usleep(TIMED_LOCK_DURATION); \
105 else { \
106 MUTEX_LOCK_DEBUG(m); \
107 break; \
110 } while (0)
111 #endif
113 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
114 int n; \
115 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
116 if (t != -1 && ctx) { \
117 rc = send_status(ctx, STATUS_LOCKED, NULL); \
118 if (!rc && t != 0) { \
119 MUTEX_TIMED_LOCK(m, t, rc); \
121 else if (!rc) { \
122 MUTEX_LOCK(m); \
125 else \
126 rc = GPG_ERR_LOCKED; \
128 else if (n) \
129 rc = gpg_error_from_errno(n); \
130 else \
131 MUTEX_LOCK_DEBUG(m); \
132 } while (0)
134 #endif