Skip to content

Commit 1468e3f

Browse files
authored
flambda-backend: Backport/install camlatomic.h (#1980)
1 parent 01d4bc0 commit 1468e3f

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

runtime4/caml/camlatomic.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**************************************************************************/
2+
/* */
3+
/* OCaml */
4+
/* */
5+
/* KC Sivaramakrishnan, Indian Institute of Technology, Madras */
6+
/* Stephen Dolan, University of Cambridge */
7+
/* */
8+
/* Copyright 2018 Indian Institute of Technology, Madras */
9+
/* Copyright 2018 University of Cambridge */
10+
/* */
11+
/* All rights reserved. This file is distributed under the terms of */
12+
/* the GNU Lesser General Public License version 2.1, with the */
13+
/* special exception on linking described in the file LICENSE. */
14+
/* */
15+
/**************************************************************************/
16+
#ifndef CAML_ATOMIC_H
17+
#define CAML_ATOMIC_H
18+
19+
#include "config.h"
20+
21+
/* On platforms supporting C11 atomics, this file just includes <stdatomic.h>.
22+
23+
On other platforms, this file includes platform-specific stubs for
24+
the subset of C11 atomics needed by the OCaml runtime
25+
*/
26+
27+
#ifdef __cplusplus
28+
29+
extern "C++" {
30+
#include <atomic>
31+
#define ATOMIC_UINTNAT_INIT(x) (x)
32+
typedef std::atomic<uintnat> atomic_uintnat;
33+
typedef std::atomic<intnat> atomic_intnat;
34+
using std::memory_order_relaxed;
35+
using std::memory_order_acquire;
36+
using std::memory_order_release;
37+
using std::memory_order_acq_rel;
38+
using std::memory_order_seq_cst;
39+
}
40+
41+
#elif defined(HAS_STDATOMIC_H)
42+
43+
#include <stdatomic.h>
44+
#define ATOMIC_UINTNAT_INIT(x) (x)
45+
typedef _Atomic uintnat atomic_uintnat;
46+
typedef _Atomic intnat atomic_intnat;
47+
48+
#elif defined(__GNUC__)
49+
50+
/* Support for versions of gcc which have built-in atomics but do not
51+
expose stdatomic.h (e.g. gcc 4.8) */
52+
typedef enum memory_order {
53+
memory_order_relaxed = __ATOMIC_RELAXED,
54+
memory_order_acquire = __ATOMIC_ACQUIRE,
55+
memory_order_release = __ATOMIC_RELEASE,
56+
memory_order_acq_rel = __ATOMIC_ACQ_REL,
57+
memory_order_seq_cst = __ATOMIC_SEQ_CST
58+
} memory_order;
59+
60+
#define ATOMIC_UINTNAT_INIT(x) { (x) }
61+
typedef struct { uintnat repr; } atomic_uintnat;
62+
typedef struct { intnat repr; } atomic_intnat;
63+
64+
#define atomic_load_explicit(x, m) __atomic_load_n(&(x)->repr, (m))
65+
#define atomic_load(x) atomic_load_explicit((x), memory_order_seq_cst)
66+
#define atomic_store_explicit(x, v, m) __atomic_store_n(&(x)->repr, (v), (m))
67+
#define atomic_store(x, v) atomic_store_explicit((x), (v), memory_order_seq_cst)
68+
#define atomic_compare_exchange_strong(x, oldv, newv) \
69+
__atomic_compare_exchange_n( \
70+
&(x)->repr, \
71+
(oldv), (newv), 0, \
72+
memory_order_seq_cst, memory_order_seq_cst)
73+
#define atomic_exchange(x, newv) \
74+
__atomic_exchange_n(&(x)->repr, (newv), memory_order_seq_cst)
75+
#define atomic_fetch_add(x, n) \
76+
__atomic_fetch_add(&(x)->repr, (n), memory_order_seq_cst)
77+
#define atomic_fetch_or(x, n) \
78+
__atomic_fetch_or(&(x)->repr, (n), memory_order_seq_cst)
79+
#define atomic_thread_fence __atomic_thread_fence
80+
81+
#else
82+
#error "C11 atomics are unavailable on this platform. See camlatomic.h"
83+
#endif
84+
85+
#endif /* CAML_ATOMIC_H */

runtime4/caml/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
(backtrace_prim.h as caml/backtrace_prim.h)
4848
(bigarray.h as caml/bigarray.h)
4949
(callback.h as caml/callback.h)
50+
(camlatomic.h as caml/camlatomic.h)
5051
(codefrag.h as caml/codefrag.h)
5152
(compact.h as caml/compact.h)
5253
(compare.h as caml/compare.h)

runtime4/caml/misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <stdlib.h>
3030
#include <stdarg.h>
3131

32+
#include "camlatomic.h"
33+
3234
/* Deprecation warnings */
3335

3436
#if defined(__GNUC__) || defined(__clang__)

runtime4/caml/s.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070

7171
#undef HAS_ISSETUGID
7272

73+
#undef HAS_STDATOMIC_H
74+
7375
/* 2. For the Unix library. */
7476

7577
#undef HAS_SOCKETS

0 commit comments

Comments
 (0)