s/GPG_ERR_CONFLICT/GPG_ERR_EEXIST.
[libpwmd.git] / src / mutex.h
blobfe5cfb24e413debf99084778a6b0503a966eda8f
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef MUTEX_H
22 #define MUTEX_H
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
32 #ifdef HAVE_TIME_H
33 #include <time.h>
34 #endif
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
38 #else
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
43 #ifdef HAVE_SYS_FILE_H
44 #include <sys/file.h>
45 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
51 #include <pthread.h>
52 #include <assert.h>
53 #include <errno.h>
54 #include "rcfile.h"
56 #define INIT_TIMESPEC(t, ts) do { \
57 long s = (t*100000000)/1000000000; \
58 long l = (t*100000000)%1000000000; \
59 clock_gettime(CLOCK_REALTIME, &ts); \
60 ts.tv_sec += s; \
61 if (ts.tv_nsec + l >= 1000000000) { \
62 ts.tv_sec++; \
63 ts.tv_nsec = 0; \
64 } \
65 else \
66 ts.tv_nsec += l; \
67 } while (0)
69 #ifdef MUTEX_DEBUG
70 #define MUTEX_LOCK_DEBUG(m) do { \
71 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
72 __FUNCTION__, m); \
73 } while (0)
75 #define MUTEX_UNLOCK_DEBUG(m) do { \
76 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
77 __FUNCTION__, m); \
78 } while (0)
79 #else
80 #define MUTEX_LOCK_DEBUG(m)
81 #define MUTEX_UNLOCK_DEBUG(m)
82 #endif
84 #define MUTEX_LOCK(m) do { \
85 MUTEX_LOCK_DEBUG(m); \
86 pthread_mutex_lock(m); \
87 } while (0)
89 #define MUTEX_UNLOCK(m) do { \
90 MUTEX_UNLOCK_DEBUG(m); \
91 pthread_mutex_unlock(m); \
92 } while (0)
94 #define TIMED_LOCK_DURATION 100000
95 #ifdef HAVE_FLOCK
96 #define TRY_FLOCK(ctx, fd, type, rc) do { \
97 long ka = (long)config_get_integer ("global", "keepalive_interval"); \
98 int to = config_get_integer ("global", "lock_timeout"); \
99 for (long elapsed = 0; ; elapsed++) { \
100 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
101 int n = flock (fd, type|LOCK_NB); \
102 if (n == -1 && errno == EWOULDBLOCK) \
104 if (to == -1 || elapsed >= to) { \
105 rc = GPG_ERR_LOCKED; \
106 break; \
108 select (0, NULL, NULL, NULL, &tv); \
109 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
110 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
111 if (rc) \
112 break; \
115 else if (n == -1) \
116 rc = gpg_error_from_errno (errno); \
117 else \
118 break; \
120 } while (0)
121 #else
122 #define TRY_FLOCK(ctx, fd, type, rc) do { \
123 rc = 0; \
124 } while (0)
125 #endif
127 #define MUTEX_TIMED_LOCK(ctx, m, timeout, rc) do { \
128 long ka = (long)config_get_integer ("global", "keepalive_interval"); \
129 for (long elapsed = 0; ; elapsed++) { \
130 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
131 if (pthread_mutex_trylock(m) == EBUSY) { \
132 if (timeout == -1 || elapsed >= timeout) { \
133 rc = GPG_ERR_LOCKED; \
134 break; \
136 select (0, NULL, NULL, NULL, &tv); \
137 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
138 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
139 if (rc) \
140 break; \
143 else { \
144 MUTEX_LOCK_DEBUG(m); \
145 break; \
148 } while (0)
150 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
151 int n; \
152 rc = 0; \
153 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
154 if (t != -1) { \
155 if (ctx) \
156 rc = send_status(ctx, STATUS_LOCKED, NULL); \
157 if (!rc && t != 0) { \
158 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
160 else if (!rc) { \
161 MUTEX_LOCK_DEBUG(m); \
164 else \
165 rc = GPG_ERR_LOCKED; \
167 else if (n) \
168 rc = gpg_error_from_errno(n); \
169 else \
170 MUTEX_LOCK_DEBUG(m); \
171 } while (0)
173 #endif