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