Remove unused code.
[libpwmd.git] / src / mutex.h
blobc3f65f8850ec81e2aceebba9cc2750d937fe814a
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 while (pthread_mutex_trylock (m) == EBUSY) { \
87 struct timeval tv = { 0, 3000 }; \
88 TEST_CANCEL (); \
89 select (0, NULL, NULL, NULL, &tv); \
90 }; \
91 } while (0)
93 #define MUTEX_UNLOCK(m) do { \
94 MUTEX_UNLOCK_DEBUG(m); \
95 pthread_mutex_unlock(m); \
96 } while (0)
98 #define TIMED_LOCK_DURATION 100000
99 #ifdef HAVE_FLOCK
100 #define TRY_FLOCK(ctx, fd, type, rc) do { \
101 long ka = (long)config_get_integer ("global", "keepalive_interval"); \
102 int to = config_get_integer ("global", "lock_timeout"); \
103 for (long elapsed = 0; ; elapsed++) { \
104 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
105 int n = flock (fd, type|LOCK_NB); \
106 if (n == -1 && errno == EWOULDBLOCK) \
108 if (to == -1 || elapsed >= to) { \
109 rc = GPG_ERR_LOCKED; \
110 break; \
112 select (0, NULL, NULL, NULL, &tv); \
113 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
114 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
115 if (rc) \
116 break; \
118 TEST_CANCEL(); \
120 else if (n == -1) \
121 rc = gpg_error_from_errno (errno); \
122 else \
123 break; \
125 } while (0)
126 #else
127 #define TRY_FLOCK(ctx, fd, type, rc) do { \
128 rc = 0; \
129 } while (0)
130 #endif
132 #define MUTEX_TIMED_LOCK(ctx, m, timeout, rc) do { \
133 long ka = (long)config_get_integer ("global", "keepalive_interval"); \
134 for (long elapsed = 0; ; elapsed++) { \
135 struct timeval tv = { 0, TIMED_LOCK_DURATION }; \
136 if (pthread_mutex_trylock(m) == EBUSY) { \
137 if (timeout == -1 || elapsed >= timeout) { \
138 rc = GPG_ERR_LOCKED; \
139 break; \
141 select (0, NULL, NULL, NULL, &tv); \
142 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
143 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
144 if (rc) \
145 break; \
147 TEST_CANCEL(); \
149 else { \
150 MUTEX_LOCK_DEBUG(m); \
151 break; \
154 } while (0)
156 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
157 int n; \
158 rc = 0; \
159 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
160 if (t != -1) { \
161 if (ctx) \
162 rc = send_status(ctx, STATUS_LOCKED, NULL); \
163 if (!rc && t != 0) { \
164 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
166 else if (!rc) { \
167 MUTEX_LOCK_DEBUG(m); \
170 else \
171 rc = GPG_ERR_LOCKED; \
173 else if (n) \
174 rc = gpg_error_from_errno(n); \
175 else \
176 MUTEX_LOCK_DEBUG(m); \
177 } while (0)
179 #endif