File: thread_ruby.c

package info (click to toggle)
libixp 0.6~20121202%2Bhg148-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 968 kB
  • ctags: 1,619
  • sloc: ansic: 4,907; sh: 142; perl: 121; makefile: 110
file content (294 lines) | stat: -rw-r--r-- 12,115 bytes parent folder | download | duplicates (2)
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
290
291
292
293
294
/* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail>
 * See LICENSE file for license details.
 */
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>
#include "ixp_local.h"

static IxpThread ixp_rthread;
static char RWLock[];

/**
 * Function: ixp_rubyinit
 *
 * This function initializes libixp for use in multithreaded
 * programs embedded in the ruby interpreter. When using a pthread
 * edition of ruby, ixp_pthread_init should be used instead. When
 * using libixp in such programs, this function must be called
 * before any other libixp functions.
 *
 * This function is part of libixp_rubythread, which is part of the
 * libixp distribution, but is not built by default unless enabled
 * in config.mk.
 */
int
ixp_rubyinit(void) {
	rb_require("thread.rb");
	rb_eval_string(RWLock);
	ixp_thread = &ixp_rthread;
	return 0;
}

static char*
errbuf(void) {
	static ID key;
	volatile VALUE val;

	if(key == 0L)
		key = rb_intern("_ixp_errbuf");

	val = rb_thread_local_aref(rb_thread_current(), key);
	if(NIL_P(val)) {
		val = rb_str_new(nil, IXP_ERRMAX);
		rb_thread_local_aset(rb_thread_current(), key, val);
	}

	Check_Type(val, T_STRING);
	return RSTRING(val)->ptr;
}

static void
save(char *eval, void **place) {
	*place = (void*)rb_eval_string(eval);
	rb_gc_register_address((VALUE*)place);
}

static void
unsave(void **place) {
	rb_gc_unregister_address((VALUE*)place);
}

#define call(obj, meth, ...) rb_funcall((VALUE)obj, rb_intern(meth), __VA_ARGS__)

/* Mutex */
static int
initmutex(IxpMutex *m) {
	save("Mutex.new", &m->aux);
	return 0;
}

static void
mdestroy(IxpMutex *m) {
	unsave(&m->aux);
}

static void
mlock(IxpMutex *m) {
	call(m->aux, "lock", 0);
}

static int
mcanlock(IxpMutex *m) {
	return call(m->aux, "trylock", 0);
}

static void
munlock(IxpMutex *m) {
	call(m->aux, "unlock", 0);
}

/* RWLock */
static int
initrwlock(IxpRWLock *rw) {
	save("RWLock.new", &rw->aux);
	return 0;
}

static void
rwdestroy(IxpRWLock *rw) {
	unsave(&rw->aux);
}

static void
rlock(IxpRWLock *rw) {
	call(rw->aux, "rdlock", 0);
}

static int
canrlock(IxpRWLock *rw) {
	return call(rw->aux, "tryrdlock", 0) == Qtrue;
}

static void
wlock(IxpRWLock *rw) {
	call(rw->aux, "wrlock", 0);
}

static int
canwlock(IxpRWLock *rw) {
	return call(rw->aux, "trywrlock", 0) == Qtrue;
}

static void
rwunlock(IxpRWLock *rw) {
	call(rw->aux, "unlock", 0);
}

/* Rendez */
static int
initrendez(IxpRendez *r) {
	save("ConditionVariable.new", &r->aux);
	return 0;
}

static void
rdestroy(IxpRendez *r) {
	unsave(&r->aux);
}

static void
rsleep(IxpRendez *r) {
	call(r->aux, "wait", 1, (VALUE)r->mutex->aux);
}

static int
rwake(IxpRendez *r) {
	call(r->aux, "signal", 0);
	return 0;
}

static int
rwakeall(IxpRendez *r) {
	call(r->aux, "broadcast", 0);
	return 0;
}

/* Yielding IO */
static ssize_t
_read(int fd, void *buf, size_t size) {
	int n;

	rb_thread_wait_fd(fd);
	n = read(fd, buf, size);

	if(n < 0 && errno == EINTR)
		rb_thread_schedule();
	return n;
}

static ssize_t
_write(int fd, const void *buf, size_t size) {
	int n;

	rb_thread_fd_writable(fd);
	n = write(fd, buf, size);

	if(n < 0 && errno == EINTR)
		rb_thread_schedule();
	return n;
}

static IxpThread ixp_rthread = {
	/* Mutex */
	.initmutex = initmutex,
	.lock = mlock,
	.canlock = mcanlock,
	.unlock = munlock,
	.mdestroy = mdestroy,
	/* RWLock */
	.initrwlock = initrwlock,
	.rlock = rlock,
	.canrlock = canrlock,
	.wlock = wlock,
	.canwlock = canwlock,
	.runlock = rwunlock,
	.wunlock = rwunlock,
	.rwdestroy = rwdestroy,
	/* Rendez */
	.initrendez = initrendez,
	.sleep = rsleep,
	.wake = rwake,
	.wakeall = rwakeall,
	.rdestroy = rdestroy,
	/* Other */
	.errbuf = errbuf,
	.read = _read,
	.write = _write,
	.select = rb_thread_select,
};

static char RWLock[] =
        "class RWLock                                                                              \n"
        "       def initialize                                                                     \n"
        "               @rdqueue = []                                                              \n"
        "               @wrqueue = []                                                              \n"
        "               @wrheld = nil                                                              \n"
        "               @rdheld = []                                                               \n"
        "       end                                                                                \n"
        "                                                                                          \n"
        "       def rdlock                                                                         \n"
        "               cr = Thread.critical                                                       \n"
        "               while (Thread.critical = true; @wrheld != nil && @rwheld != Thread.current)\n"
        "                       @rdqueue.push Thread.current                                       \n"
        "                       Thread.stop                                                        \n"
        "               end                                                                        \n"
        "               @wrheld = nil                                                              \n"
        "               @rdheld.push Thread.current                                                \n"
        "                                                                                          \n"
        "               @rdqueue.each {|t| t.wakeup}                                               \n"
        "               Thread.critical = cr                                                       \n"
        "               self                                                                       \n"
        "       end                                                                                \n"
        "                                                                                          \n"
        "       def wrlock                                                                         \n"
        "               cr = Thread.critical                                                       \n"
        "               while (Thread.critical = true;                                             \n"
        "                      !@rdheld.empty? || (@wrheld != Thread.current && @wrheld != nil))   \n"
        "                       @wrqueue.push Thread.current                                       \n"
        "                       Thread.stop                                                        \n"
        "               end                                                                        \n"
        "               @wrheld = Thread.current                                                   \n"
        "               Thread.critical = cr                                                       \n"
        "               self                                                                       \n"
        "       end                                                                                \n"
        "                                                                                          \n"
        "                                                                                          \n"
        "       def tryrdlock                                                                      \n"
        "               cr = Thread.critical                                                       \n"
        "               if @wrheld == nil                                                          \n"
        "                       rdlock                                                             \n"
        "                       true                                                               \n"
        "               else                                                                       \n"
        "                       false                                                              \n"
        "               end                                                                        \n"
        "       ensure                                                                             \n"
        "               Thread.critical = cr                                                       \n"
        "       end                                                                                \n"
        "                                                                                          \n"
        "       def trywrlock                                                                      \n"
        "               cr = Thread.critical                                                       \n"
        "               if @wrheld == nil && @rdheld.empty?                                        \n"
        "                       wrlock                                                             \n"
        "                       true                                                               \n"
        "               else                                                                       \n"
        "                       false                                                              \n"
        "               end                                                                        \n"
        "       ensure                                                                             \n"
        "               Thread.critical = cr                                                       \n"
        "       end                                                                                \n"
        "                                                                                          \n"
        "       def unlock                                                                         \n"
        "               cr = Thread.critical                                                       \n"
        "               Thread.critical = true                                                     \n"
        "                                                                                          \n"
        "               if @rdheld.include?(Thread.current)                                        \n"
        "                       @rdheld.remove!(Thread.current)                                    \n"
        "                       raise if @wrheld                                                   \n"
        "               elsif @rwheld != Thread.current                                            \n"
        "                       raise                                                              \n"
        "               end                                                                        \n"
        "                                                                                          \n"
        "               @wrheld = nil                                                              \n"
        "               if !@rwqueue.empty? && @rdheld.empty?                                      \n"
        "                       @wrheld = @wrqueue.shift                                           \n"
        "               elsif !@rdqueue.empty                                                      \n"
        "                       @wrheld = @rdqueue.shift                                           \n"
        "               end                                                                        \n"
        "               @wrheld.wakeup if @wrheld                                                  \n"
        "       ensure                                                                             \n"
        "               Thread.critical = cr                                                       \n"
        "       end                                                                                \n"
        "end                                                                                       \n";