Update copyright.
[pwmd.git] / src / mutex.h
blob33088e06608e1b6ae125c14da52f7d15518c859f
1 /*
2 Copyright (C) 2006-2018 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef MUTEX_H
20 #define MUTEX_H
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
30 #ifdef HAVE_TIME_H
31 #include <time.h>
32 #endif
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
36 #else
37 #include <sys/types.h>
38 #include <unistd.h>
39 #endif
41 #ifdef HAVE_SYS_FILE_H
42 #include <sys/file.h>
43 #endif
45 #ifdef HAVE_FCNTL_H
46 #include <fcntl.h>
47 #endif
49 #include <pthread.h>
50 #include <assert.h>
51 #include <errno.h>
52 #include "rcfile.h"
54 #ifndef HAVE_PTHREAD_CANCEL
55 pthread_key_t signal_thread_key;
56 #define TEST_CANCEL() do { \
57 int *cancel = (int *) pthread_getspecific (signal_thread_key); \
58 if (cancel && *cancel) \
59 pthread_exit (NULL); \
60 } while (0)
61 #else
62 #define TEST_CANCEL()
63 #endif
65 #define INIT_TIMESPEC(t, ts) do { \
66 long s = (t*100000000)/1000000000; \
67 long l = (t*100000000)%1000000000; \
68 clock_gettime(CLOCK_REALTIME, &ts); \
69 ts.tv_sec += s; \
70 if (ts.tv_nsec + l >= 1000000000) { \
71 ts.tv_sec++; \
72 ts.tv_nsec = 0; \
73 } \
74 else \
75 ts.tv_nsec += l; \
76 } while (0)
78 #ifdef MUTEX_DEBUG
79 #define MUTEX_LOCK_DEBUG(m) do { \
80 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
81 __FUNCTION__, m); \
82 } while (0)
84 #define MUTEX_UNLOCK_DEBUG(m) do { \
85 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
86 __FUNCTION__, m); \
87 } while (0)
88 #else
89 #define MUTEX_LOCK_DEBUG(m)
90 #define MUTEX_UNLOCK_DEBUG(m)
91 #endif
93 #define MUTEX_LOCK(m) do { \
94 MUTEX_LOCK_DEBUG(m); \
95 while (pthread_mutex_trylock (m) == EBUSY) { \
96 struct timeval tv = { 0, 3000 }; \
97 TEST_CANCEL (); \
98 select (0, NULL, NULL, NULL, &tv); \
99 }; \
100 } while (0)
102 #define MUTEX_UNLOCK(m) do { \
103 MUTEX_UNLOCK_DEBUG(m); \
104 pthread_mutex_unlock(m); \
105 } while (0)
107 #define TIMED_LOCK_DURATION 100000
108 #ifdef HAVE_FLOCK
109 #define TRY_FLOCK(ctx, fd, type, rc) do { \
110 long ka = (long)config_get_integer ("global", "keepalive_interval");\
111 long to = config_get_long ("global", "lock_timeout"); \
112 for (long elapsed = 0; ; elapsed++) { \
113 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
114 int n = flock (fd, type|LOCK_NB); \
115 if (n == -1 && errno == EWOULDBLOCK) { \
116 if (to == -1 || elapsed >= to) { \
117 rc = GPG_ERR_LOCKED; \
118 break; \
120 select (0, NULL, NULL, NULL, &tv); \
121 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
122 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
123 if (rc) \
124 break; \
126 TEST_CANCEL(); \
128 else if (n == -1) \
129 rc = gpg_error_from_errno (errno); \
130 else \
131 break; \
133 } while (0)
134 #else
135 #define TRY_FLOCK(ctx, fd, type, rc) do { \
136 rc = 0; \
137 } while (0)
138 #endif
140 #define MUTEX_TIMED_LOCK(ctx, m, timeout, rc) do { \
141 long ka = (long)config_get_integer ("global", "keepalive_interval");\
142 for (long elapsed = 0; ; elapsed++) { \
143 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
144 int n = pthread_mutex_trylock (m); \
145 if (n == EBUSY) { \
146 if (timeout == -1 || elapsed >= timeout) { \
147 rc = GPG_ERR_LOCKED; \
148 break; \
150 select (0, NULL, NULL, NULL, &tv); \
151 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
152 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
153 if (rc) \
154 break; \
156 TEST_CANCEL(); \
158 else if (n) { \
159 rc = gpg_error_from_errno (n); \
160 break; \
162 else { \
163 MUTEX_LOCK_DEBUG(m); \
164 break; \
167 } while (0)
169 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
170 int err = pthread_mutex_trylock (m); \
171 rc = 0; \
172 if (err == EBUSY) { \
173 if (t != -1) { \
174 if (ctx) \
175 rc = send_status(ctx, STATUS_LOCKED, NULL); \
176 if (!rc && t != 0) \
177 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
178 else if (!rc) \
179 rc = GPG_ERR_LOCKED; \
181 else \
182 rc = GPG_ERR_LOCKED; \
184 else if (err) \
185 rc = gpg_error_from_errno (err); \
186 else \
187 MUTEX_LOCK_DEBUG(m); \
188 } while (0)
190 #define MUTEX_TIMED_RWLOCK(ctx, m, timeout, rc, w) do { \
191 long ka = (long)config_get_integer ("global", "keepalive_interval");\
192 for (long elapsed = 0; ; elapsed++) { \
193 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
194 int err; \
195 if (w) \
196 err = pthread_rwlock_trywrlock(m); \
197 else \
198 err = pthread_rwlock_tryrdlock(m); \
199 if (err == EBUSY) { \
200 if (timeout == -1 || elapsed >= timeout) { \
201 rc = GPG_ERR_LOCKED; \
202 break; \
204 select (0, NULL, NULL, NULL, &tv); \
205 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
206 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
207 if (rc) \
208 break; \
210 TEST_CANCEL(); \
212 else if (err) { \
213 rc = gpg_error_from_errno (err); \
214 break; \
216 else { \
217 MUTEX_LOCK_DEBUG(m); \
218 break; \
221 } while (0)
223 #endif