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/>.
26 #ifdef HAVE_SYS_TIME_H
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
37 #include <sys/types.h>
41 #ifdef HAVE_SYS_FILE_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); \
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); \
70 if (ts.tv_nsec + l >= 1000000000) { \
79 #define MUTEX_LOCK_DEBUG(m) do { \
80 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
84 #define MUTEX_UNLOCK_DEBUG(m) do { \
85 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
89 #define MUTEX_LOCK_DEBUG(m)
90 #define MUTEX_UNLOCK_DEBUG(m)
93 #define MUTEX_LOCK(m) do { \
94 MUTEX_LOCK_DEBUG(m); \
95 while (pthread_mutex_trylock (m) == EBUSY) { \
96 struct timeval tv = { 0, 3000 }; \
98 select (0, NULL, NULL, NULL, &tv); \
102 #define MUTEX_UNLOCK(m) do { \
103 MUTEX_UNLOCK_DEBUG(m); \
104 pthread_mutex_unlock(m); \
107 #define TIMED_LOCK_DURATION 100000
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; \
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); \
129 rc = gpg_error_from_errno (errno); \
135 #define TRY_FLOCK(ctx, fd, type, rc) do { \
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); \
146 if (timeout == -1 || elapsed >= timeout) { \
147 rc = GPG_ERR_LOCKED; \
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); \
159 rc = gpg_error_from_errno (n); \
163 MUTEX_LOCK_DEBUG(m); \
169 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
170 int err = pthread_mutex_trylock (m); \
172 if (err == EBUSY) { \
175 rc = send_status(ctx, STATUS_LOCKED, NULL); \
177 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
179 rc = GPG_ERR_LOCKED; \
182 rc = GPG_ERR_LOCKED; \
185 rc = gpg_error_from_errno (err); \
187 MUTEX_LOCK_DEBUG(m); \
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 }; \
196 err = pthread_rwlock_trywrlock(m); \
198 err = pthread_rwlock_tryrdlock(m); \
199 if (err == EBUSY) { \
200 if (timeout == -1 || elapsed >= timeout) { \
201 rc = GPG_ERR_LOCKED; \
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); \
213 rc = gpg_error_from_errno (err); \
217 MUTEX_LOCK_DEBUG(m); \