-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsync_posix.h
228 lines (191 loc) · 5.88 KB
/
sync_posix.h
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* POSIX thread implementation of the user facing Mutex and Condition */
/* To be included in runtime/sync.c */
#ifndef CAML_SYNC_POSIX_H
#define CAML_SYNC_POSIX_H
#include <errno.h>
#include <pthread.h>
#include <string.h>
#ifdef __linux__
#include <features.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <limits.h>
#endif
typedef int sync_retcode;
/* Mutexes */
/* Already defined in <caml/sync.h> */
/* typedef pthread_mutex_t * sync_mutex; */
/* #define Mutex_val(v) (* ((sync_mutex *) Data_custom_val(v))) */
Caml_inline int sync_mutex_create(sync_mutex * res)
{
int rc;
pthread_mutexattr_t attr;
sync_mutex m;
rc = pthread_mutexattr_init(&attr);
if (rc != 0) goto error1;
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (rc != 0) goto error2;
m = caml_stat_alloc_noexc(sizeof(pthread_mutex_t));
if (m == NULL) { rc = ENOMEM; goto error2; }
rc = pthread_mutex_init(m, &attr);
if (rc != 0) goto error3;
pthread_mutexattr_destroy(&attr);
*res = m;
return 0;
error3:
caml_stat_free(m);
error2:
pthread_mutexattr_destroy(&attr);
error1:
return rc;
}
Caml_inline int sync_mutex_destroy(sync_mutex m)
{
int rc;
rc = pthread_mutex_destroy(m);
caml_stat_free(m);
return rc;
}
Caml_inline int sync_mutex_lock(sync_mutex m)
{
return pthread_mutex_lock(m);
}
#define MUTEX_PREVIOUSLY_UNLOCKED 0
#define MUTEX_ALREADY_LOCKED EBUSY
Caml_inline int sync_mutex_trylock(sync_mutex m)
{
return pthread_mutex_trylock(m);
}
Caml_inline int sync_mutex_unlock(sync_mutex m)
{
return pthread_mutex_unlock(m);
}
/* If we're using glibc, use a custom condition variable implementation to
avoid this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25847
For now we only have this on linux because it directly uses the linux futex
syscalls. */
#if defined(__linux__) && defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__)
static int custom_condvar_init(custom_condvar * cv)
{
cv->counter = 0;
return 0;
}
static int custom_condvar_destroy(custom_condvar * cv)
{
return 0;
}
static int custom_condvar_wait(custom_condvar * cv, pthread_mutex_t * mutex)
{
unsigned old_count = cv->counter;
pthread_mutex_unlock(mutex);
syscall(SYS_futex, &cv->counter, FUTEX_WAIT_PRIVATE, old_count, NULL, NULL, 0);
pthread_mutex_lock(mutex);
return 0;
}
static int custom_condvar_signal(custom_condvar * cv)
{
__sync_add_and_fetch(&cv->counter, 1);
syscall(SYS_futex, &cv->counter, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
return 0;
}
static int custom_condvar_broadcast(custom_condvar * cv)
{
__sync_add_and_fetch(&cv->counter, 1);
syscall(SYS_futex, &cv->counter, FUTEX_WAKE_PRIVATE, INT_MAX, NULL, NULL, 0);
return 0;
}
#else
static int custom_condvar_init(custom_condvar * cv)
{
pthread_condattr_t attr;
pthread_condattr_init(&attr);
#if defined(_POSIX_TIMERS) && \
defined(_POSIX_MONOTONIC_CLOCK) && \
_POSIX_MONOTONIC_CLOCK != (-1)
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
#endif
return pthread_cond_init(cv, &attr);
}
static int custom_condvar_destroy(custom_condvar * cv)
{
return pthread_cond_destroy(cv);
}
static int custom_condvar_wait(custom_condvar * cv, pthread_mutex_t * mutex)
{
return pthread_cond_wait(cv, mutex);
}
static int custom_condvar_signal(custom_condvar * cv)
{
return pthread_cond_signal(cv);
}
static int custom_condvar_broadcast(custom_condvar * cv)
{
return pthread_cond_broadcast(cv);
}
#endif
/* Condition variables */
typedef custom_condvar * sync_condvar;
#define Condition_val(v) (* (sync_condvar *) Data_custom_val(v))
Caml_inline int sync_condvar_create(sync_condvar * res)
{
int rc;
sync_condvar c = caml_stat_alloc_noexc(sizeof(custom_condvar));
if (c == NULL) return ENOMEM;
rc = custom_condvar_init(c);
if (rc != 0) { caml_stat_free(c); return rc; }
*res = c;
return 0;
}
Caml_inline int sync_condvar_destroy(sync_condvar c)
{
int rc;
rc = custom_condvar_destroy(c);
caml_stat_free(c);
return rc;
}
Caml_inline int sync_condvar_signal(sync_condvar c)
{
return custom_condvar_signal(c);
}
Caml_inline int sync_condvar_broadcast(sync_condvar c)
{
return custom_condvar_broadcast(c);
}
Caml_inline int sync_condvar_wait(sync_condvar c, sync_mutex m)
{
return custom_condvar_wait(c, m);
}
/* Reporting errors */
Caml_inline void sync_check_error(int retcode, char * msg)
{
char * err;
char buf[1024];
int errlen, msglen;
value str;
if (retcode == 0) return;
if (retcode == ENOMEM) caml_raise_out_of_memory();
err = caml_strerror(retcode, buf, sizeof(buf));
msglen = strlen(msg);
errlen = strlen(err);
str = caml_alloc_string(msglen + 2 + errlen);
memcpy (&Byte(str, 0), msg, msglen);
memcpy (&Byte(str, msglen), ": ", 2);
memcpy (&Byte(str, msglen + 2), err, errlen);
caml_raise_sys_error(str);
}
#endif /* CAML_SYNC_POSIX_H */