Skip to content

Commit f3196f0

Browse files
committed
Merge atomic counter inc/dec functions and use them consistently.
1 parent c40a123 commit f3196f0

11 files changed

+74
-95
lines changed

runtime/bigarray.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "caml/memory.h"
2828
#include "caml/mlvalues.h"
2929
#include "caml/signals.h"
30-
#include "caml/atomic_refcount.h"
30+
#include "caml/camlatomic.h"
3131

3232
#define int8 caml_ba_int8
3333
#define uint8 caml_ba_uint8
@@ -293,7 +293,7 @@ CAMLexport void caml_ba_finalize(value v)
293293
free(b->data);
294294
caml_free_dependent_memory(v, caml_ba_byte_size(b));
295295
} else {
296-
if (caml_atomic_refcount_decr(&b->proxy->refcount) == 1) {
296+
if (caml_atomic_counter_decr(&b->proxy->refcount) == 1) {
297297
free(b->proxy->data);
298298
caml_free_dependent_memory(v, b->proxy->size);
299299
free(b->proxy);
@@ -1133,12 +1133,12 @@ static void caml_ba_update_proxy(struct caml_ba_array * b1,
11331133
/* If b1 is already a proxy for a larger array, increment refcount of
11341134
proxy */
11351135
b2->proxy = b1->proxy;
1136-
caml_atomic_refcount_incr(&b1->proxy->refcount);
1136+
(void)caml_atomic_counter_incr(&b1->proxy->refcount);
11371137
} else {
11381138
/* Otherwise, create proxy and attach it to both b1 and b2 */
11391139
proxy = malloc(sizeof(struct caml_ba_proxy));
11401140
if (proxy == NULL) caml_raise_out_of_memory();
1141-
caml_atomic_refcount_init(&proxy->refcount, 2);
1141+
caml_atomic_counter_init(&proxy->refcount, 2);
11421142
/* initial refcount: 2 = original array + sub array */
11431143
proxy->data = b1->data;
11441144
proxy->size = caml_ba_byte_size(b1);

runtime/caml/atomic_refcount.h

-37
This file was deleted.

runtime/caml/camlatomic.h

+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define CAML_ATOMIC_H
1818

1919
#include "config.h"
20+
#include "misc.h"
2021

2122
/* On platforms supporting C11 atomics, this file just includes <stdatomic.h>.
2223
@@ -95,6 +96,29 @@ typedef struct { intnat repr; } atomic_intnat;
9596
#define atomic_store_relaxed(p, v) \
9697
atomic_store_explicit((p), (v), memory_order_relaxed)
9798

99+
Caml_inline void caml_atomic_counter_init(atomic_uintnat* counter, uintnat n)
100+
{
101+
atomic_store_release(counter, n);
102+
}
103+
104+
/* atomically decrements the counter and returns the new value */
105+
106+
Caml_inline uintnat caml_atomic_counter_decr(atomic_uintnat* counter)
107+
{
108+
uintnat old = atomic_fetch_sub(counter, 1);
109+
CAMLassert (old > 0);
110+
return old-1;
111+
}
112+
113+
/* atomically increments the counter and returns the new value */
114+
115+
Caml_inline uintnat caml_atomic_counter_incr(atomic_uintnat* counter)
116+
{
117+
uintnat old = atomic_fetch_add(counter, 1);
118+
CAMLassert(old+1 != 0);
119+
return old+1;
120+
}
121+
98122
#endif /* CAML_INTERNALS */
99123

100124
#endif /* CAML_ATOMIC_H */

runtime/caml/dune

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
(address_class.h as caml/address_class.h)
5151
(addrmap.h as caml/addrmap.h)
5252
(alloc.h as caml/alloc.h)
53-
(atomic_refcount.h as caml/atomic_refcount.h)
5453
(backtrace_prim.h as caml/backtrace_prim.h)
5554
(backtrace.h as caml/backtrace.h)
5655
(bigarray.h as caml/bigarray.h)

runtime/caml/misc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include <stdarg.h>
2828
#include <limits.h>
2929

30-
#include "camlatomic.h"
31-
3230
/* Deprecation warnings */
3331

3432
#if defined(__GNUC__) || defined(__clang__)
@@ -201,6 +199,8 @@ CAMLdeprecated_typedef(addr, char *);
201199
can obtain the domain id with Caml_state->id. These functions must
202200
be reentrant. */
203201
#ifndef __cplusplus
202+
#include <stdatomic.h>
203+
204204
typedef void (*caml_timing_hook) (void);
205205
extern _Atomic caml_timing_hook caml_major_slice_begin_hook;
206206
extern _Atomic caml_timing_hook caml_major_slice_end_hook;
@@ -505,7 +505,7 @@ CAMLextern int caml_read_directory(char_os * dirname,
505505

506506
/* runtime message flags. Settable with v= in OCAMLRUNPARAM */
507507

508-
extern atomic_uintnat caml_verb_gc;
508+
extern _Atomic uintnat caml_verb_gc;
509509

510510
/* Bits which may be set in caml_verb_gc. The quotations are from the
511511
* OCaml manual. */

runtime/caml/mlvalues.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "config.h"
2020
#include "misc.h"
21+
#include "camlatomic.h"
2122
#include "tsan.h"
2223

2324
#ifdef __cplusplus

runtime/caml/platform.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ Caml_inline void cpu_relax(void) {
5454
}
5555

5656

57-
/* Atomic read-modify-write instructions, with full fences */
58-
59-
Caml_inline uintnat atomic_fetch_add_verify_ge0(atomic_uintnat* p, uintnat v) {
60-
uintnat result = atomic_fetch_add(p,v);
61-
CAMLassert ((intnat)result > 0);
62-
return result;
63-
}
64-
6557
/* If we're using glibc, use a custom condition variable implementation to
6658
avoid this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25847
6759
@@ -308,7 +300,7 @@ typedef uintnat barrier_status;
308300
the last arrival. */
309301
Caml_inline barrier_status caml_plat_barrier_arrive(caml_plat_barrier* barrier)
310302
{
311-
return 1 + atomic_fetch_add(&barrier->arrived, 1);
303+
return caml_atomic_counter_incr(&barrier->arrived);
312304
}
313305

314306
/* -- Single-sense --

runtime/domain.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static void domain_create(uintnat initial_minor_heap_wsize,
715715
s->unique_id = fresh_domain_unique_id();
716716
domain_state->unique_id = s->unique_id;
717717
s->running = 1;
718-
atomic_fetch_add(&caml_num_domains_running, 1);
718+
(void)caml_atomic_counter_incr(&caml_num_domains_running);
719719

720720
domain_state->c_stack = NULL;
721721
domain_state->exn_handler = NULL;
@@ -1444,7 +1444,7 @@ static void decrement_stw_domains_still_processing(void)
14441444
if so, clear the stw_leader to allow the new stw sections to start.
14451445
*/
14461446
intnat am_last =
1447-
atomic_fetch_add(&stw_request.num_domains_still_processing, -1) == 1;
1447+
caml_atomic_counter_decr(&stw_request.num_domains_still_processing) == 0;
14481448

14491449
if( am_last ) {
14501450
/* release the STW lock to allow new STW sections */
@@ -1662,8 +1662,8 @@ int caml_try_run_on_all_domains_with_spin_work(
16621662
stw_request.data = data;
16631663
stw_request.num_domains = stw_domains.participating_domains;
16641664
/* stw_request.barrier doesn't need resetting */
1665-
atomic_store_release(&stw_request.num_domains_still_processing,
1666-
stw_domains.participating_domains);
1665+
caml_atomic_counter_init(&stw_request.num_domains_still_processing,
1666+
stw_domains.participating_domains);
16671667

16681668
int is_alone = stw_request.num_domains == 1;
16691669
int should_sync = sync && !is_alone;
@@ -2144,7 +2144,7 @@ static void domain_terminate (void)
21442144
/* This is the last thing we do because we need to be able to rely
21452145
on caml_domain_alone (which uses caml_num_domains_running) in at least
21462146
the shared_heap lockfree fast paths */
2147-
atomic_fetch_add(&caml_num_domains_running, -1);
2147+
(void)caml_atomic_counter_decr(&caml_num_domains_running);
21482148
}
21492149

21502150
CAMLprim value caml_ml_domain_cpu_relax(value t)

0 commit comments

Comments
 (0)