1 /**
2   Mirror _pyatomic.h
3 
4   Note this is python 3 only
5   (and very probably doesn't work period)
6   */
7 // todo: 3.7 update
8 module deimos.python.pyatomic;
9 
10 /* This is modeled after the atomics interface from C1x, according to
11  * the draft at
12  * http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1425.pdf.
13  * Operations and types are named the same except with a _Py_ prefix
14  * and have the same semantics.
15  *
16  * Beware, the implementations here are deep magic.
17  */
18 
19 version(Python_3_0_Or_Later) {
20     extern(C):
21     enum _Py_memory_order {
22         _Py_memory_order_relaxed,
23         _Py_memory_order_acquire,
24         _Py_memory_order_release,
25         _Py_memory_order_acq_rel,
26         _Py_memory_order_seq_cst
27     }
28 
29     struct _Py_atomic_address {
30         void* _value;
31     }
32 
33     struct _Py_atomic_int {
34         int _value;
35     }
36 
37     /* Fall back to other compilers and processors by assuming that simple
38        volatile accesses are atomic.  This is false, so people should port
39        this. */
40     auto _Py_atomic_signal_fence()(_Py_memory_order order) {
41         (cast(void)0);
42     }
43     auto _Py_atomic_thread_fence()(_Py_memory_order order) {
44         (cast(void)0);
45     }
46     void _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL)(ATOMIC_VAL val,
47             NEW_VAL val2, _Py_memory_order order) {
48         val._value = val2;
49     }
50     auto _Py_atomic_load_explicit(ATOMIC_VAL)(ATOMIC_VAL val,
51             _Py_memory_order order) {
52         return val._value;
53     }
54 
55     /* Standardized shortcuts. */
56     void _Py_atomic_store(ATOMIC_VAL, NEW_VAL)(ATOMIC_VAL val, NEW_VAL val2) {
57         _Py_atomic_store_explicit(val, val2,
58                 _Py_memory_order._Py_memory_order_seq_cst);
59     }
60     auto _Py_atomic_load(ATOMIC_VAL)(ATOMIC_VAL val) {
61         return _Py_atomic_load_explicit(val,
62                 _Py_memory_order._Py_memory_order_seq_cst);
63     }
64 
65     /* Python-local extensions */
66 
67     void _Py_atomic_store_relaxed(ATOMIC_VAL, NEW_VAL)(ATOMIC_VAL val,
68             NEW_VAL val2) {
69         _Py_atomic_store_explicit(val, val2, _Py_memory_order._Py_memory_order_relaxed);
70     }
71     auto _Py_atomic_load_relaxed(ATOMIC_VAL)(ATOMIC_VAL val) {
72         return _Py_atomic_load_explicit(val, _Py_memory_order._Py_memory_order_relaxed);
73     }
74 }