Remove test for pthread_mutex_timedlock().
[libpwmd.git] / src / mutex.h
blob17adfff4fcba8cd991f3d16e6de0dfff1182f9cf
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 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
31 #ifdef HAVE_TIME_H
32 #include <time.h>
33 #endif
35 #include <pthread.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include "rcfile.h"
40 #define INIT_TIMESPEC(t, ts) do { \
41 long s = (t*100000000)/1000000000; \
42 long l = (t*100000000)%1000000000; \
43 clock_gettime(CLOCK_REALTIME, &ts); \
44 ts.tv_sec += s; \
45 if (ts.tv_nsec + l >= 1000000000) { \
46 ts.tv_sec++; \
47 ts.tv_nsec = 0; \
48 } \
49 else \
50 ts.tv_nsec += l; \
51 } while (0)
53 #ifdef MUTEX_DEBUG
54 #define MUTEX_LOCK_DEBUG(m) do { \
55 log_write("%s(%i): %s(): LOCK %p", __FILE__, __LINE__, \
56 __FUNCTION__, m); \
57 } while (0)
59 #define MUTEX_UNLOCK_DEBUG(m) do { \
60 log_write("%s(%i): %s(): UNLOCK %p", __FILE__, __LINE__, \
61 __FUNCTION__, m); \
62 } while (0)
63 #else
64 #define MUTEX_LOCK_DEBUG(m)
65 #define MUTEX_UNLOCK_DEBUG(m)
66 #endif
68 #define MUTEX_LOCK(m) do { \
69 MUTEX_LOCK_DEBUG(m); \
70 pthread_mutex_lock(m); \
71 } while (0)
73 #define MUTEX_UNLOCK(m) do { \
74 MUTEX_UNLOCK_DEBUG(m); \
75 pthread_mutex_unlock(m); \
76 } while (0)
78 #define TIMED_LOCK_DURATION 100000
79 #define MUTEX_TIMED_LOCK(ctx, m, timeout, rc) do { \
80 long ka = (long)config_get_integer ("global", "keepalive_interval"); \
81 for (long elapsed = 0; ; elapsed++) { \
82 if (pthread_mutex_trylock(m) == EBUSY) { \
83 if (timeout == -1 || elapsed >= timeout) { \
84 rc = GPG_ERR_LOCKED; \
85 break; \
86 } \
87 usleep(TIMED_LOCK_DURATION); \
88 if (ctx && ka && elapsed && !((elapsed+1*10) % (ka*10))) { \
89 rc = send_status (ctx, STATUS_KEEPALIVE, NULL); \
90 if (rc) \
91 break; \
92 } \
93 } \
94 else { \
95 MUTEX_LOCK_DEBUG(m); \
96 break; \
97 } \
98 } \
99 } while (0)
101 #define MUTEX_TRYLOCK(ctx, m, rc, t) do { \
102 int n; \
103 rc = 0; \
104 if ((n = pthread_mutex_trylock(m)) == EBUSY) { \
105 if (t != -1) { \
106 if (ctx) \
107 rc = send_status(ctx, STATUS_LOCKED, NULL); \
108 if (!rc && t != 0) { \
109 MUTEX_TIMED_LOCK(ctx, m, t, rc); \
111 else if (!rc) { \
112 MUTEX_LOCK_DEBUG(m); \
115 else \
116 rc = GPG_ERR_LOCKED; \
118 else if (n) \
119 rc = gpg_error_from_errno(n); \
120 else \
121 MUTEX_LOCK_DEBUG(m); \
122 } while (0)
124 #endif