-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcondvar_wrap.c
76 lines (67 loc) · 2.5 KB
/
condvar_wrap.c
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
// vim:sw=4 ts=4 sts=4 expandtab
/* Copyright 2012, Cedric Cellier
*
* This file is part of RobiNet.
*
* RobiNet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RobiNet 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with RobiNet. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include "caml/alloc.h"
#include "caml/fail.h"
#include "caml/memory.h"
#include "caml/mlvalues.h"
#include "caml/signals.h"
typedef pthread_cond_t *condvar;
#define Condvar_val(v) (*(condvar *)Data_custom_val(v))
typedef pthread_mutex_t *mutex;
#define Mutex_val(v) (*((mutex *)Data_custom_val(v)))
CAMLprim value caml_condition_timedwait(value cond_, value mut_, value timeo_)
{
CAMLparam3(cond_, mut_, timeo_);
condvar cond = Condvar_val(cond_);
mutex mut = Mutex_val(mut_);
double const timeo = Double_val(timeo_);
/* Note: time_t is "an integer type" according to POSIX, whereas suseconds_t
* is a signed integer. */
time_t const sec = (time_t)timeo;
suseconds_t const nsec = (suseconds_t)((timeo - (double)sec) * 1e9);
struct timespec const abstime = { .tv_sec = sec, .tv_nsec = nsec };
caml_enter_blocking_section();
int ret = pthread_cond_timedwait(cond, mut, &abstime);
caml_leave_blocking_section();
if (ret != 0 && ret != ETIMEDOUT) {
switch (ret) {
case ENOTRECOVERABLE:
caml_raise_sys_error(caml_copy_string("ENOTRECOVERABLE"));
break;
case EOWNERDEAD:
caml_raise_sys_error(caml_copy_string("EOWNERDEAD"));
break;
case EPERM:
caml_raise_sys_error(caml_copy_string("EPERM"));
break;
case EINVAL:
caml_raise_sys_error(caml_copy_string("EINVAL"));
break;
}
char err[200];
snprintf(err, 200-1, "Unknown error %d", ret);
caml_raise_sys_error(caml_copy_string(err));
}
CAMLlocal1(v);
v = ret == 0 ? Val_false : Val_true;
CAMLreturn(v);
}