-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcallback.c
515 lines (428 loc) · 14.6 KB
/
callback.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, 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. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Callbacks from C to OCaml */
#include <string.h>
#include "caml/alloc.h"
#include "caml/callback.h"
#include "caml/codefrag.h"
#include "caml/fail.h"
#include "caml/fiber.h"
#include "caml/memory.h"
#include "caml/mlvalues.h"
#include "caml/platform.h"
#include "caml/signals.h"
/* A note about callbacks and GC. For best performance, a callback such as
[caml_callback_exn(value closure, value arg)]
should not extend the lifetime of the values [closure]
and [arg] any farther than necessary, that is, they should not be
registered as GC roots when the function call actually happens.
This mirrors the reachability/lifetime guarantees provided by
function calls in OCaml code, where the arguments can be collected
as soon as they are not used anymore within the function body.
The closure and its arguments may still have to be registered as
GC roots, typically across a call to [alloc_and_clear_stack_parent] below,
but registration should stop before the actual callback.
See #12121 for more discussion. */
/*
* These functions are to ensure effects are handled correctly inside
* callbacks. There are two aspects:
* - we clear the stack parent for a callback to force an Effect.Unhandled
* exception rather than effects being passed over the callback
* - we register the stack parent as a local root while the callback
* is executing to ensure that the garbage collector follows the
* stack parent
*/
Caml_inline value alloc_and_clear_stack_parent(caml_domain_state* domain_state)
{
struct stack_info* parent_stack = Stack_parent(domain_state->current_stack);
if (parent_stack == NULL) {
return Val_unit;
} else {
value cont = caml_alloc_1(Cont_tag, Val_ptr(parent_stack));
Stack_parent(domain_state->current_stack) = NULL;
return cont;
}
}
Caml_inline void restore_stack_parent(caml_domain_state* domain_state,
value cont)
{
CAMLassert(Stack_parent(domain_state->current_stack) == NULL);
if (Is_block(cont)) {
struct stack_info* parent_stack = Ptr_val(Op_val(cont)[0]);
Stack_parent(domain_state->current_stack) = parent_stack;
}
}
static value raise_if_exception(value res)
{
if (Is_exception_result(res)) {
if (Caml_state->raising_async_exn) {
Caml_state->raising_async_exn = 0;
caml_raise_async(Extract_exception(res));
} else {
caml_raise(Extract_exception(res));
}
}
return res;
}
#ifndef NATIVE_CODE
/* Bytecode callbacks */
#include "caml/interp.h"
#include "caml/instruct.h"
#include "caml/fix_code.h"
#include "caml/fiber.h"
static __thread opcode_t callback_code[] = { ACC, 0, APPLY, 0, POP, 1, STOP };
static __thread int callback_code_inited = 0;
static void init_callback_code(void)
{
caml_register_code_fragment((char *) callback_code,
(char *) callback_code + sizeof(callback_code),
DIGEST_IGNORE, NULL);
#ifdef THREADED_CODE
caml_thread_code(callback_code, sizeof(callback_code));
#endif
callback_code_inited = 1;
}
/* Functions that return all exceptions, including asynchronous ones */
static value caml_callbackN_exn0(value closure, int narg, value args[])
{
CAMLparam0(); /* no need to register closure and args as roots, see below */
CAMLlocal1(cont);
value res;
int i;
caml_domain_state* domain_state = Caml_state;
CAMLassert(narg + 4 <= 256);
domain_state->current_stack->sp -= narg + 4;
for (i = 0; i < narg; i++)
domain_state->current_stack->sp[i] = args[i]; /* arguments */
if (!callback_code_inited) init_callback_code();
callback_code[1] = narg + 3;
callback_code[3] = narg;
domain_state->current_stack->sp[narg] =
(value)(callback_code + 4); /* return address */
domain_state->current_stack->sp[narg + 1] = Val_unit; /* environment */
domain_state->current_stack->sp[narg + 2] = Val_long(0); /* extra args */
domain_state->current_stack->sp[narg + 3] = closure;
cont = alloc_and_clear_stack_parent(domain_state);
/* This can call the GC and invalidate the values [closure] and [args].
However, they are never used afterwards,
as they were copied into the root [domain_state->current_stack]. */
res = caml_interprete(callback_code, sizeof(callback_code));
if (Is_exception_result(res))
domain_state->current_stack->sp += narg + 4; /* PR#3419 */
restore_stack_parent(domain_state, cont);
CAMLreturn (res);
}
CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
{
value res = caml_callbackN_exn0(closure, narg, args);
Caml_state->raising_async_exn = 0;
return res;
}
CAMLexport value caml_callback_exn(value closure, value arg1)
{
value res, arg[1];
arg[0] = arg1;
res = caml_callbackN_exn0(closure, 1, arg);
Caml_state->raising_async_exn = 0;
return res;
}
CAMLexport value caml_callback2_exn(value closure, value arg1, value arg2)
{
value res, arg[2];
arg[0] = arg1;
arg[1] = arg2;
res = caml_callbackN_exn0(closure, 2, arg);
Caml_state->raising_async_exn = 0;
return res;
}
CAMLexport value caml_callback3_exn(value closure,
value arg1, value arg2, value arg3)
{
value res, arg[3];
arg[0] = arg1;
arg[1] = arg2;
arg[2] = arg3;
res = caml_callbackN_exn0(closure, 3, arg);
Caml_state->raising_async_exn = 0;
return res;
}
/* Functions that propagate all exceptions, with any asynchronous exceptions
also being propagated asynchronously. */
CAMLexport value caml_callbackN(value closure, int narg, value args[])
{
return raise_if_exception(caml_callbackN_exn0(closure, narg, args));
}
CAMLexport value caml_callback(value closure, value arg1)
{
value arg[1];
arg[0] = arg1;
return caml_callbackN(closure, 1, arg);
}
CAMLexport value caml_callback2(value closure, value arg1, value arg2)
{
value arg[2];
arg[0] = arg1;
arg[1] = arg2;
return caml_callbackN(closure, 2, arg);
}
CAMLexport value caml_callback3(value closure,
value arg1, value arg2, value arg3)
{
value arg[3];
arg[0] = arg1;
arg[1] = arg2;
arg[2] = arg3;
return caml_callbackN(closure, 3, arg);
}
#else
/* Native-code callbacks. caml_callback[123]_asm are implemented in asm. */
static void init_callback_code(void)
{
}
typedef value (callback_stub)(caml_domain_state* state,
value closure,
value* args);
callback_stub caml_callback_asm, caml_callback2_asm, caml_callback3_asm;
static value callback(value closure, value arg)
{
Caml_check_caml_state();
caml_domain_state* domain_state = Caml_state;
caml_maybe_expand_stack();
if (Stack_parent(domain_state->current_stack)) {
value cont, res;
/* [closure] and [arg] need to be preserved across the allocation
of the stack parent, but need not and should not be registered
as roots past this allocation. */
Begin_roots2(closure, arg);
cont = alloc_and_clear_stack_parent(domain_state);
End_roots();
Begin_roots1(cont);
res = caml_callback_asm(domain_state, closure, &arg);
End_roots();
restore_stack_parent(domain_state, cont);
return res;
} else {
return caml_callback_asm(domain_state, closure, &arg);
}
}
static value callback2(value closure, value arg1, value arg2)
{
Caml_check_caml_state();
caml_domain_state* domain_state = Caml_state;
caml_maybe_expand_stack();
CAMLassert(arg1 != 0);
if (Stack_parent(domain_state->current_stack)) {
value cont, res;
/* Root registration policy: see caml_callback_exn. */
Begin_roots3(closure, arg1, arg2);
cont = alloc_and_clear_stack_parent(domain_state);
End_roots();
Begin_roots1(cont);
value args[] = {arg1, arg2};
res = caml_callback2_asm(domain_state, closure, args);
End_roots();
restore_stack_parent(domain_state, cont);
return res;
} else {
value args[] = {arg1, arg2};
return caml_callback2_asm(domain_state, closure, args);
}
}
static value callback3(value closure, value arg1, value arg2, value arg3)
{
Caml_check_caml_state();
caml_domain_state* domain_state = Caml_state;
caml_maybe_expand_stack();
if (Stack_parent(domain_state->current_stack)) {
value cont, res;
/* Root registration policy: see caml_callback_exn. */
Begin_roots4(closure, arg1, arg2, arg3);
cont = alloc_and_clear_stack_parent(domain_state);
End_roots();
Begin_root(cont);
value args[] = {arg1, arg2, arg3};
res = caml_callback3_asm(domain_state, closure, args);
End_roots();
restore_stack_parent(domain_state, cont);
return res;
} else {
value args[] = {arg1, arg2, arg3};
return caml_callback3_asm(domain_state, closure, args);
}
}
static value callbackN(value closure, int narg, value args[])
{
while (narg >= 3) {
/* We apply the first 3 arguments to get a new closure,
and continue with the remaining arguments. */
value *remaining_args = args + 3;
int remaining_narg = narg - 3;
/* We need to register the remaining arguments as roots
in case a GC occurs during [caml_callback3_exn].
Arguments 0, 1 and 2 need not and should not be registered. */
Begin_roots_block(remaining_args, remaining_narg);
closure = callback3(closure, args[0], args[1], args[2]);
End_roots();
if (Is_exception_result(closure)) return closure;
args = remaining_args;
narg = remaining_narg;
}
switch (narg) {
case 0:
return closure;
case 1:
return callback(closure, args[0]);
default: /* case 2: */
return callback2(closure, args[0], args[1]);
}
}
/* Functions that return all exceptions, including asynchronous ones */
CAMLexport value caml_callback_exn(value closure, value arg)
{
value res = callback(closure, arg);
Caml_state->raising_async_exn = 0;
return res;
}
CAMLexport value caml_callback2_exn(value closure, value arg1, value arg2)
{
value res = callback2(closure, arg1, arg2);
Caml_state->raising_async_exn = 0;
return res;
}
/* Exception-propagating variants of the above */
CAMLexport value caml_callback3_exn(value closure, value arg1, value arg2,
value arg3)
{
value res = callback3(closure, arg1, arg2, arg3);
Caml_state->raising_async_exn = 0;
return res;
}
CAMLexport value caml_callbackN_exn(value closure, int narg, value args[])
{
value res = callbackN(closure, narg, args);
Caml_state->raising_async_exn = 0;
return res;
}
/* Functions that propagate all exceptions, with any asynchronous exceptions
also being propagated asynchronously. */
CAMLexport value caml_callback (value closure, value arg)
{
return raise_if_exception(callback(closure, arg));
}
CAMLexport value caml_callback2 (value closure, value arg1, value arg2)
{
return raise_if_exception(callback2(closure, arg1, arg2));
}
CAMLexport value caml_callback3 (value closure, value arg1, value arg2,
value arg3)
{
return raise_if_exception(callback3(closure, arg1, arg2, arg3));
}
CAMLexport value caml_callbackN (value closure, int narg, value args[])
{
return raise_if_exception(callbackN(closure, narg, args));
}
#endif
/* Naming of OCaml values */
struct named_value {
value val;
struct named_value * next;
char name[1];
};
#define Named_value_size 13
static struct named_value * named_value_table[Named_value_size] = { NULL, };
static caml_plat_mutex named_value_lock = CAML_PLAT_MUTEX_INITIALIZER;
void caml_init_callbacks(void)
{
init_callback_code();
}
static unsigned int hash_value_name(char const *name)
{
unsigned int h;
/* "djb2" hash function */
for (h = 5381; *name != 0; name++) h = h * 33 + *name;
return h % Named_value_size;
}
CAMLprim value caml_register_named_value(value vname, value val)
{
struct named_value * nv;
const char * name = String_val(vname);
size_t namelen = strlen(name);
unsigned int h = hash_value_name(name);
int found = 0;
caml_plat_lock(&named_value_lock);
for (nv = named_value_table[h]; nv != NULL; nv = nv->next) {
if (strcmp(name, nv->name) == 0) {
caml_modify_generational_global_root(&nv->val, val);
found = 1;
break;
}
}
if (!found) {
nv = (struct named_value *)
caml_stat_alloc(sizeof(struct named_value) + namelen);
memcpy(nv->name, name, namelen + 1);
nv->val = val;
nv->next = named_value_table[h];
named_value_table[h] = nv;
caml_register_generational_global_root(&nv->val);
}
caml_plat_unlock(&named_value_lock);
return Val_unit;
}
CAMLexport const value* caml_named_value(char const *name)
{
struct named_value * nv;
caml_plat_lock(&named_value_lock);
for (nv = named_value_table[hash_value_name(name)];
nv != NULL;
nv = nv->next) {
if (strcmp(name, nv->name) == 0){
caml_plat_unlock(&named_value_lock);
return &nv->val;
}
}
caml_plat_unlock(&named_value_lock);
return NULL;
}
CAMLexport void caml_iterate_named_values(caml_named_action f)
{
int i;
caml_plat_lock(&named_value_lock);
for(i = 0; i < Named_value_size; i++){
struct named_value * nv;
for (nv = named_value_table[i]; nv != NULL; nv = nv->next) {
f( Op_val(nv->val), nv->name );
}
}
caml_plat_unlock(&named_value_lock);
}
CAMLprim value caml_with_async_exns(value body_callback)
{
value res;
res = caml_callback_exn(body_callback, Val_unit);
/* raised as a normal exn, even if it was asynchronous */
if (Is_exception_result(res)) {
/* Drain the queue of pending actions. We may need to do
this several times if some raise */
do {
res = Extract_exception(res);
res = caml_process_pending_actions_with_root_exn(res);
} while (Is_exception_result(res));
caml_raise(res);
}
return res;
}