monitor.c
6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Tell GLIBC that we want UNIX98 pthread_rwlock_xx() functions. */
#define _XOPEN_SOURCE 500
/* Tell QNX/Neutrino to define pthread_rwlock_xx() functions. */
#define _QNX_SOURCE
#define _POSIX_C_SOURCE 199506
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef WITH_PTHREAD
# define _REENTRANT
# ifdef HAVE_PTHREAD_H
# include <pthread.h>
# endif
#endif
#include <errno.h>
#include <stdlib.h>
#include <monitor0.h>
#ifdef WITH_PTHREAD
pthread_mutex_t monitor_lock = PTHREAD_MUTEX_INITIALIZER;
# define STATIC_LOCK(m) pthread_mutex_lock(m)
# define STATIC_UNLOCK(m) pthread_mutex_unlock(m)
#else
# define STATIC_LOCK(m) 0
# define STATIC_UNLOCK(m) 0
int monitor_lock;
#endif
union _p_lock
{
#ifdef WITH_PTHREAD
# ifdef HAVE_PTHREAD_RWLOCK_INIT
pthread_rwlock_t mutex;
# else
pthread_mutex_t mutex;
# endif
#else
int dummy;
#endif
};
typedef union _p_lock *p_lock_t;
static int monitor_pthread_create (p_lock_t *);
static void monitor_pthread_destroy (p_lock_t *);
static int monitor_pthread_rdlock (p_lock_t);
static int monitor_pthread_wrlock (p_lock_t);
static int monitor_pthread_unlock (p_lock_t);
/* The idea was to have a general/portable object monitor_t.
The monitor_t object could have different implementation (on the fly ?)
of locking. Also the rest of the library would not have to know about
different threading implementation. So far we've pretty much hardcoded
the concrete implementation of monitor on pthread and read/write locks,
but changing to a different concrete implementation will not be hard if
the need arise.
For static initializers we take a small penality and since we have
a global static lock.
*/
int
monitor_create (monitor_t *pmonitor, int flags, void *owner)
{
monitor_t monitor;
if (pmonitor == NULL)
return EINVAL;
monitor = calloc (1, sizeof (*monitor));
if (monitor == NULL)
return ENOMEM;
if (flags == MU_MONITOR_PTHREAD)
{
int status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
if (status != 0)
{
free (monitor);
return status;
}
}
monitor->owner = owner;
monitor->allocated = 1;
monitor->flags = flags;
*pmonitor = monitor;
return 0;
}
void *
monitor_get_owner (monitor_t monitor)
{
return (monitor == NULL) ? NULL : monitor->owner;
}
void
monitor_destroy (monitor_t *pmonitor, void *owner)
{
if (pmonitor && *pmonitor)
{
monitor_t monitor = *pmonitor;
if (monitor->owner == owner)
{
if (monitor->flags == MU_MONITOR_PTHREAD)
monitor_pthread_destroy ((p_lock_t *)&(monitor->data));
}
free (monitor);
*pmonitor = NULL;
}
}
int
monitor_rdlock (monitor_t monitor)
{
if (monitor)
{
if (!monitor->allocated)
{
int status = STATIC_LOCK (&monitor_lock);
if (monitor->data == NULL)
{
if (monitor->flags == MU_MONITOR_PTHREAD)
status = monitor_pthread_create ((p_lock_t*)&(monitor->data));
if (status != 0)
{
STATIC_UNLOCK (&monitor_lock);
return status;
}
}
monitor->allocated = 1;
STATIC_UNLOCK (&monitor_lock);
}
if (monitor->flags == MU_MONITOR_PTHREAD)
return monitor_pthread_rdlock ((p_lock_t)monitor->data);
}
return 0;
}
int
monitor_wrlock (monitor_t monitor)
{
if (monitor)
{
if (!monitor->allocated)
{
int status = STATIC_LOCK (&monitor_lock);
if (monitor->data == NULL)
{
if (monitor->flags == MU_MONITOR_PTHREAD)
status = monitor_pthread_create ((p_lock_t *)&(monitor->data));
if (status != 0)
{
STATIC_UNLOCK (&monitor_lock);
return status;
}
}
monitor->allocated = 1;
STATIC_UNLOCK (&monitor_lock);
}
if (monitor->flags == MU_MONITOR_PTHREAD)
return monitor_pthread_wrlock ((p_lock_t)monitor->data);
}
return 0;
}
int
monitor_unlock (monitor_t monitor)
{
if (monitor)
{
if (monitor->flags == MU_MONITOR_PTHREAD)
return monitor_pthread_unlock ((p_lock_t)monitor->data);
}
return 0;
}
int
monitor_wait (monitor_t monitor)
{
(void)monitor;
return ENOSYS;
}
int
monitor_notify (monitor_t monitor)
{
(void)monitor;
return ENOSYS;
}
/* Concrete Implementation of pthread base on rwlocks. */
#ifdef WITH_PTHREAD
# ifdef HAVE_PTHREAD_RWLOCK_INIT
# define RWLOCK_INIT(rwl, attr) pthread_rwlock_init (rwl, attr)
# define RWLOCK_DESTROY(rwl) pthread_rwlock_destroy (rwl)
# define RWLOCK_RDLOCK(rwl) pthread_rwlock_rdlock (rwl)
# define RWLOCK_TRYRDLOCK(rwl) pthread_rwlock_tryrdlock (rwl)
# define RWLOCK_WRLOCK(rwl) pthread_rwlock_wrlock (rwl)
# define RWLOCK_TRYWRLOCK(rwl) pthread_rwlock_trywrlock (rwl)
# define RWLOCK_UNLOCK(rwl) pthread_rwlock_unlock (rwl)
# else
# define RWLOCK_INIT(rwl, attr) pthread_mutex_init (rwl, attr)
# define RWLOCK_DESTROY(rwl) pthread_mutex_destroy (rwl)
# define RWLOCK_RDLOCK(rwl) pthread_mutex_lock (rwl)
# define RWLOCK_TRYRDLOCK(rwl) pthread_mutex_trylock (rwl)
# define RWLOCK_WRLOCK(rwl) pthread_mutex_lock (rwl)
# define RWLOCK_TRYWRLOCK(rwl) pthread_mutex_trylock (rwl)
# define RWLOCK_UNLOCK(rwl) pthread_mutex_unlock (rwl)
# endif
#else
# define RWLOCK_INIT(rwl, attr) 0
# define RWLOCK_DESTROY(rwl) 0
# define RWLOCK_RDLOCK(rwl) 0
# define RWLOCK_TRYRDLOCK(rwl) 0
# define RWLOCK_WRLOCK(rwl) 0
# define RWLOCK_TRYWRLOCK(rwl) 0
# define RWLOCK_UNLOCK(rwl) 0
# define flockfile(arg) 0
# define funlockfile(arg) 0
#endif
static int
monitor_pthread_create (p_lock_t *plock)
{
int status;
p_lock_t lock = calloc (1, sizeof (*lock));
if (lock == NULL)
return ENOMEM;
status = RWLOCK_INIT (&(lock->mutex), NULL);
if (status != 0)
{
free (lock);
return status;
}
*plock = lock;
return 0;
}
static void
monitor_pthread_destroy (p_lock_t *plock)
{
p_lock_t lock = *plock;
if (lock)
{
RWLOCK_DESTROY (&(lock->mutex));
free (lock);
}
*plock = NULL;
}
static int
monitor_pthread_rdlock (p_lock_t lock)
{
return RWLOCK_RDLOCK (&(lock->mutex));
}
static int
monitor_pthread_wrlock (p_lock_t lock)
{
return RWLOCK_WRLOCK (&(lock->mutex));
}
static int
monitor_pthread_unlock (p_lock_t lock)
{
return RWLOCK_UNLOCK (&(lock->mutex));
}