Skip to content

Commit 43ebb82

Browse files
committed
[breaking] Change the prototype of [caml_atomic_cas_field].
This is a breaking change because this function was (unfortunately) exposed outside CAML_INTERNALS, and is used by exactly one external user, you guessed it: https://github.com/ocaml-multicore/multicore-magic/blob/360c2e829c9addeca9ccaee1c71f4ad36bb14a79/src/Multicore_magic.mli#L181-L185 https://github.com/ocaml-multicore/multicore-magic/blob/360c2e829c9addeca9ccaee1c71f4ad36bb14a79/src/unboxed5/multicore_magic_atomic_array.ml#L36-L43 We chose to change the prototype to remain consistent with the naming convention for the new caml_atomic_*_field primitives, which will be added to support atomic record fields. User code can easily adapt to this new prototype we are using, but not in a way that is compatible with both old and new versions of OCaml (not without some preprocessing at least). Another option would be to expose int caml_atomic_cas_field(value obj, intnat fld, value, value) value caml_atomic_cas_field_boxed(value obj, value vfld, value, value) but no other group of primitives in the runtime uses this _boxed terminology, they instead use int caml_atomic_cas_field_unboxed(value obj, intnat fld, value, value) value caml_atomic_cas_field(value obj, value vfld, value, value) and this would again break compatiblity -- it is not easier to convert code to that two-version proposal, and not noticeably more efficient. So in this case we decided to break compatibility (of an obscure, experimental, undocumented but exposed feature) in favor of consistency and simplificity of the result.
1 parent 8c4bd1a commit 43ebb82

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

runtime/caml/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CAMLextern void caml_alloc_dependent_memory (mlsize_t bsz);
4343
CAMLextern void caml_free_dependent_memory (mlsize_t bsz);
4444
CAMLextern void caml_modify (volatile value *, value);
4545
CAMLextern void caml_initialize (volatile value *, value);
46-
CAMLextern int caml_atomic_cas_field (value, intnat, value, value);
46+
CAMLextern value caml_atomic_cas_field (value, value, value, value);
4747
CAMLextern value caml_check_urgent_gc (value);
4848

4949
/* [caml_stat_*] functions below provide an interface to the static memory

runtime/memory.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,19 @@ CAMLprim value caml_atomic_exchange (value ref, value v)
345345
return ret;
346346
}
347347

348-
CAMLexport int caml_atomic_cas_field (
349-
value obj, intnat field, value oldval, value newval)
348+
CAMLexport value caml_atomic_cas_field (
349+
value obj, value vfield, value oldval, value newval)
350350
{
351+
intnat field = Long_val(vfield);
351352
if (caml_domain_alone()) {
352353
/* non-atomic CAS since only this thread can access the object */
353354
volatile value* p = &Field(obj, field);
354355
if (*p == oldval) {
355356
*p = newval;
356357
write_barrier(obj, field, oldval, newval);
357-
return 1;
358+
return Val_true;
358359
} else {
359-
return 0;
360+
return Val_false;
360361
}
361362
} else {
362363
/* need a real CAS */
@@ -365,15 +366,15 @@ CAMLexport int caml_atomic_cas_field (
365366
atomic_thread_fence(memory_order_release); /* generates `dmb ish` on Arm64*/
366367
if (cas_ret) {
367368
write_barrier(obj, field, oldval, newval);
368-
return 1;
369+
return Val_true;
369370
} else {
370-
return 0;
371+
return Val_false;
371372
}
372373
}
373374
}
374375
CAMLprim value caml_atomic_cas (value ref, value oldval, value newval)
375376
{
376-
return Val_bool(caml_atomic_cas_field(ref, 0, oldval, newval));
377+
return caml_atomic_cas_field(ref, Val_long(0), oldval, newval);
377378
}
378379

379380
CAMLprim value caml_atomic_fetch_add (value ref, value incr)

runtime/obj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ CAMLprim value caml_obj_add_offset (value v, value offset)
173173
CAMLprim value caml_obj_compare_and_swap (value v, value f,
174174
value oldv, value newv)
175175
{
176-
int res = caml_atomic_cas_field(v, Int_val(f), oldv, newv);
176+
value res = caml_atomic_cas_field(v, f, oldv, newv);
177177
caml_check_urgent_gc(Val_unit);
178-
return Val_int(res);
178+
return res;
179179
}
180180

181181
CAMLprim value caml_obj_is_shared (value obj)

0 commit comments

Comments
 (0)