Skip to content

Backport/install camlatomic.h #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions ocaml/runtime4/caml/camlatomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**************************************************************************/
/* */
/* OCaml */
/* */
/* KC Sivaramakrishnan, Indian Institute of Technology, Madras */
/* Stephen Dolan, University of Cambridge */
/* */
/* Copyright 2018 Indian Institute of Technology, Madras */
/* Copyright 2018 University of Cambridge */
/* */
/* 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. */
/* */
/**************************************************************************/
#ifndef CAML_ATOMIC_H
#define CAML_ATOMIC_H

#include "config.h"

/* On platforms supporting C11 atomics, this file just includes <stdatomic.h>.

On other platforms, this file includes platform-specific stubs for
the subset of C11 atomics needed by the OCaml runtime
*/

#ifdef __cplusplus

extern "C++" {
#include <atomic>
#define ATOMIC_UINTNAT_INIT(x) (x)
typedef std::atomic<uintnat> atomic_uintnat;
typedef std::atomic<intnat> atomic_intnat;
using std::memory_order_relaxed;
using std::memory_order_acquire;
using std::memory_order_release;
using std::memory_order_acq_rel;
using std::memory_order_seq_cst;
}

#elif defined(HAS_STDATOMIC_H)

#include <stdatomic.h>
#define ATOMIC_UINTNAT_INIT(x) (x)
typedef _Atomic uintnat atomic_uintnat;
typedef _Atomic intnat atomic_intnat;

#elif defined(__GNUC__)

/* Support for versions of gcc which have built-in atomics but do not
expose stdatomic.h (e.g. gcc 4.8) */
typedef enum memory_order {
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST
} memory_order;

#define ATOMIC_UINTNAT_INIT(x) { (x) }
typedef struct { uintnat repr; } atomic_uintnat;
typedef struct { intnat repr; } atomic_intnat;

#define atomic_load_explicit(x, m) __atomic_load_n(&(x)->repr, (m))
#define atomic_load(x) atomic_load_explicit((x), memory_order_seq_cst)
#define atomic_store_explicit(x, v, m) __atomic_store_n(&(x)->repr, (v), (m))
#define atomic_store(x, v) atomic_store_explicit((x), (v), memory_order_seq_cst)
#define atomic_compare_exchange_strong(x, oldv, newv) \
__atomic_compare_exchange_n( \
&(x)->repr, \
(oldv), (newv), 0, \
memory_order_seq_cst, memory_order_seq_cst)
#define atomic_exchange(x, newv) \
__atomic_exchange_n(&(x)->repr, (newv), memory_order_seq_cst)
#define atomic_fetch_add(x, n) \
__atomic_fetch_add(&(x)->repr, (n), memory_order_seq_cst)
#define atomic_fetch_or(x, n) \
__atomic_fetch_or(&(x)->repr, (n), memory_order_seq_cst)
#define atomic_thread_fence __atomic_thread_fence

#else
#error "C11 atomics are unavailable on this platform. See camlatomic.h"
#endif

#endif /* CAML_ATOMIC_H */
1 change: 1 addition & 0 deletions ocaml/runtime4/caml/dune
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
(backtrace_prim.h as caml/backtrace_prim.h)
(bigarray.h as caml/bigarray.h)
(callback.h as caml/callback.h)
(camlatomic.h as caml/camlatomic.h)
(codefrag.h as caml/codefrag.h)
(compact.h as caml/compact.h)
(compare.h as caml/compare.h)
Expand Down
2 changes: 2 additions & 0 deletions ocaml/runtime4/caml/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <stdlib.h>
#include <stdarg.h>

#include "camlatomic.h"

/* Deprecation warnings */

#if defined(__GNUC__) || defined(__clang__)
Expand Down
2 changes: 2 additions & 0 deletions ocaml/runtime4/caml/s.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@

#undef HAS_ISSETUGID

#undef HAS_STDATOMIC_H

/* 2. For the Unix library. */

#undef HAS_SOCKETS
Expand Down