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
|
/**************************************************************************/
/* */
/* 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>
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);
}
/* Condition variables */
typedef pthread_cond_t * 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(pthread_cond_t));
if (c == NULL) return ENOMEM;
rc = pthread_cond_init(c, NULL);
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 = pthread_cond_destroy(c);
caml_stat_free(c);
return rc;
}
Caml_inline int sync_condvar_signal(sync_condvar c)
{
return pthread_cond_signal(c);
}
Caml_inline int sync_condvar_broadcast(sync_condvar c)
{
return pthread_cond_broadcast(c);
}
Caml_inline int sync_condvar_wait(sync_condvar c, sync_mutex m)
{
return pthread_cond_wait(c, m);
}
/* Reporting errors */
static 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 */
|