Skip to content

Commit 8b51ed5

Browse files
TheNumbatmshinwell
authored andcommitted
Implement fake atomics in runtime4
These are necessary for the atomic externals in stdlib to be usable with runtime4. They are copies of the versions in runtime/, but assume Domain_alone = true. Once we implement flambda2 support for the atomic primitives, the externals should be updated to use them, and these functions may be deleted.
1 parent 3bac47e commit 8b51ed5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ocaml/runtime4/misc.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ __declspec(noreturn) void __cdecl abort(void);
2828
#include <stdio.h>
2929
#include <string.h>
3030
#include <stdarg.h>
31+
#include "caml/alloc.h"
3132
#include "caml/config.h"
3233
#include "caml/misc.h"
3334
#include "caml/memory.h"
@@ -225,3 +226,30 @@ void caml_flambda2_invalid (value message)
225226
fprintf (stderr, "Consider using [Sys.opaque_identity].\n");
226227
abort ();
227228
}
229+
230+
/* Fake atomic operations - runtime4 is single threaded, but we need to
231+
provide these symbols for compatibility with the 5 Stdlib updates. */
232+
233+
CAMLprim value caml_atomic_make(value v)
234+
{
235+
CAMLparam1(v);
236+
value ref = caml_alloc_small(1, 0);
237+
Field(ref, 0) = v;
238+
CAMLreturn(ref);
239+
}
240+
241+
CAMLprim value caml_atomic_load(value ref)
242+
{
243+
return Field(ref, 0);
244+
}
245+
246+
CAMLprim value caml_atomic_cas (value ref, value oldv, value newv)
247+
{
248+
value* p = Op_val(ref);
249+
if (*p == oldv) {
250+
caml_modify(p, newv);
251+
return Val_int(1);
252+
} else {
253+
return Val_int(0);
254+
}
255+
}

0 commit comments

Comments
 (0)