1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::{
    ffi::c_void,
    sync::atomic::{AtomicUsize, Ordering},
};

#[repr(C)]
pub struct AwsCAtomicVar {
    pub value: *mut c_void,
}

impl AwsCAtomicVar {
    pub fn from_int(n: usize) -> Self {
        Self {
            value: n as *mut c_void,
        }
    }
}

impl AwsCAtomicVar {
    // Interpret this atomic variable as an integer (usize).
    pub fn as_atomic_usize(&self) -> &AtomicUsize {
        let self_ptr = self as *const Self;
        let atomic_ptr = self_ptr as *const AtomicUsize;
        let atomic_ref = unsafe { atomic_ptr.as_ref().unwrap() };
        atomic_ref
    }

    // Interpret this atomic variable as a mutable integer (usize).
    pub fn as_mut_atomic_usize(&mut self) -> &mut AtomicUsize {
        let self_ptr = self as *mut Self;
        let atomic_ptr = self_ptr as *mut AtomicUsize;
        let atomic_ref = unsafe { atomic_ptr.as_mut().unwrap() };
        atomic_ref
    }
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum AwsCMemoryOrder {
    Relaxed = 0,
    Acquire = 2,
    Release = 3,
    AcqRel = 4,
    SeqCst = 5,
}

impl From<AwsCMemoryOrder> for Ordering {
    fn from(order: AwsCMemoryOrder) -> Self {
        match order {
            AwsCMemoryOrder::Relaxed => Ordering::Relaxed,
            AwsCMemoryOrder::Acquire => Ordering::Acquire,
            AwsCMemoryOrder::Release => Ordering::Release,
            AwsCMemoryOrder::AcqRel => Ordering::AcqRel,
            AwsCMemoryOrder::SeqCst => Ordering::SeqCst,
        }
    }
}

/// Initializes an atomic variable with an integer value. This operation should be done before any
/// other operations on this atomic variable, and must be done before attempting any parallel operations.
///
/// This operation does not imply a barrier. Ensure that you use an acquire-release barrier (or stronger)
/// when communicating the fact that initialization is complete to the other thread. Launching the thread
/// implies a sufficiently strong barrier.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_init_int(var: *mut AwsCAtomicVar, n: usize) {
    let usize_ptr = var as *mut usize;
    *usize_ptr = n;
}

 /// Initializes an atomic variable with a pointer value. This operation should be done before any
/// other operations on this atomic variable, and must be done before attempting any parallel operations.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_init_ptr(var: *mut AwsCAtomicVar, p: *mut c_void) {
    let cvoid_ptr = var as *mut *mut c_void;
    *cvoid_ptr = p;
}


/// Reads an atomic var as an integer, using sequentially consistent ordering, and returns the result.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_load_int(var: *const AwsCAtomicVar) -> usize {
    aws_atomic_load_int_explicit(var, AwsCMemoryOrder::SeqCst)
}

/// Stores an integer into an atomic var, using sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_store_int(var: *mut AwsCAtomicVar, val: usize) {
    aws_atomic_store_int_explicit(var, val, AwsCMemoryOrder::SeqCst)
}

/// Exchanges an integer with the value in an atomic_var, using sequentially consistent ordering.
/// Returns the value that was previously in the atomic_var.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_exchange_int(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_exchange_int_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically compares `*var` to `*expected`; if they are equal, atomically sets `*var = desired`. Otherwise, `*expected` is set
/// to the value in `*var`. Uses sequentially consistent memory ordering, regardless of success or failure.
/// Returns `true` if the compare was successful and the variable updated to desired.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_compare_exchange_int(var: *mut AwsCAtomicVar, expected: *mut usize, desired: usize) -> bool {
    aws_atomic_compare_exchange_int_explicit(var, expected, desired, AwsCMemoryOrder::SeqCst, AwsCMemoryOrder::SeqCst)
}

/// Atomically adds `n` to `*var`, and returns the previous value of `*var`.
/// Uses sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_fetch_add(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_fetch_add_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically subtracts `n` from `*var`, and returns the previous value of `*var`.
/// Uses sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_fetch_sub(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_fetch_sub_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically ANDs `n` into `*var`, and returns the previous value of `*var`.
/// Uses sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_fetch_and(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_fetch_and_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically ORs `n` into *var, and returns the previous value of `*var`.
/// Uses sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_fetch_or(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_fetch_or_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically XORs `n` into `*var`, and returns the previous value of `*var`.
/// Uses sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_fetch_xor(var: *mut AwsCAtomicVar, n: usize) -> usize {
    aws_atomic_fetch_xor_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Reads an atomic var as an integer, using the specified ordering, and returns the result.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_load_int_explicit(var: *const AwsCAtomicVar, order: AwsCMemoryOrder) -> usize {
    var.as_ref().unwrap().as_atomic_usize().load(order.into())
}

/// Stores an integer into an atomic var, using the specified ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_store_int_explicit(var: *mut AwsCAtomicVar, val: usize, order: AwsCMemoryOrder) {
    var.as_mut().unwrap().as_mut_atomic_usize().store(val, order.into())
}

/// Exchanges an integer with the value in an atomic_var, using the specified ordering.
/// Returns the value that was previously in the atomic_var.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_exchange_int_explicit(var: *mut AwsCAtomicVar, n: usize, memory_order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().swap(n, memory_order.into())
}

/// Atomically compares `*var` to `*expected`; if they are equal, atomically sets `*var = desired`. Otherwise, `*expected` is set
/// to the value in `*var`. On success, the memory ordering used was `order_success`; otherwise, it was `order_failure`.
/// `order_failure` must be no stronger than `order_success`, and must not be `release` or `acq_rel`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_compare_exchange_int_explicit(
    var: *mut AwsCAtomicVar,
    expected: *mut usize,
    desired: usize,
    success: AwsCMemoryOrder,
    failure: AwsCMemoryOrder,
) -> bool {
    let result = var.as_mut()
        .unwrap()
        .as_mut_atomic_usize()
        .compare_exchange(*expected, desired, success.into(), failure.into());

    match result {
        Ok(_) => true,
        Err(actual) => {
            *expected = actual;
            false
        }
    }
}

/// Atomically adds `n` to `*var`, and returns the previous value of `*var`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_fetch_add_explicit(var: *mut AwsCAtomicVar, n: usize, order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().fetch_add(n, order.into())
}

/// Atomically subtracts `n` from `*var`, and returns the previous value of `*var`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_fetch_sub_explicit(var: *mut AwsCAtomicVar, n: usize, order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().fetch_sub(n, order.into())
}

/// Atomically ORs `n` with `*var`, and returns the previous value of `*var`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_fetch_or_explicit(var: *mut AwsCAtomicVar, n: usize, order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().fetch_or(n, order.into())
}

/// Atomically ANDs `n` with `*var`, and returns the previous value of `*var`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_fetch_and_explicit(var: *mut AwsCAtomicVar, n: usize, order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().fetch_and(n, order.into())
}

/// Atomically XORs `n` with `*var`, and returns the previous value of `*var`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_fetch_xor_explicit(var: *mut AwsCAtomicVar, n: usize, order: AwsCMemoryOrder) -> usize {
    var.as_mut().unwrap().as_mut_atomic_usize().fetch_xor(n, order.into())
}

// Pointer operations

/// Reads an atomic var as a pointer, using sequentially consistent ordering, and returns the result.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_load_ptr(var: *const AwsCAtomicVar) -> *mut c_void {
    aws_atomic_load_ptr_explicit(var, AwsCMemoryOrder::SeqCst)
}

/// Stores a pointer into an atomic var, using sequentially consistent ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_store_ptr(var: *mut AwsCAtomicVar, val: *mut c_void) {
    aws_atomic_store_ptr_explicit(var, val, AwsCMemoryOrder::SeqCst)
}

/// Exchanges an integer with the value in an atomic_var, using sequentially consistent ordering.
/// Returns the value that was previously in the atomic_var.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_exchange_ptr(var: *mut AwsCAtomicVar, n: *mut c_void) -> *mut c_void {
    aws_atomic_exchange_ptr_explicit(var, n, AwsCMemoryOrder::SeqCst)
}

/// Atomically compares `*var` to `*expected`; if they are equal, atomically sets `*var = desired`. Otherwise, `*expected` is set
/// to the value in `*var`. Uses sequentially consistent memory ordering, regardless of success or failure.
/// Returns `true` if the compare was successful and the variable updated to desired.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid.
pub unsafe fn aws_atomic_compare_exchange_ptr(
    var: *mut AwsCAtomicVar,
    expected: *mut *mut c_void,
    desired: *mut c_void,
) -> bool {
    aws_atomic_compare_exchange_ptr_explicit(var, expected, desired, AwsCMemoryOrder::SeqCst, AwsCMemoryOrder::SeqCst)
}

/// Reads an atomic var as a pointer, using the specified ordering, and returns the result.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_load_ptr_explicit(var: *const AwsCAtomicVar, order: AwsCMemoryOrder) -> *mut c_void {
    var.as_ref().unwrap().as_atomic_usize().load(order.into()) as *mut c_void
}

/// Stores an pointer into an atomic var, using the specified ordering.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_store_ptr_explicit(var: *mut AwsCAtomicVar, val: *mut c_void, order: AwsCMemoryOrder) {
    var.as_mut().unwrap().as_mut_atomic_usize().store(val as usize, order.into())
}

/// Exchanges a pointer with the value in an atomic_var, using the specified ordering.
/// Returns the value that was previously in the atomic_var.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_exchange_ptr_explicit(var: *mut AwsCAtomicVar, n: *mut c_void, memory_order: AwsCMemoryOrder) -> *mut c_void {
    var.as_mut().unwrap().as_mut_atomic_usize().swap(n as usize, memory_order.into()) as *mut c_void
}

/// Atomically compares `*var` to `*expected`; if they are equal, atomically sets `*var = desired`. Otherwise, `*expected` is set
/// to the value in `*var`. On success, the memory ordering used was `order_success`; otherwise, it was `order_failure`.
/// `order_failure` must be no stronger than `order_success`, and must not be `release` or `acq_rel`.
///
/// # Safety
/// The caller must ensure that the `AwsCAtomicVar` pointer is valid. 
pub unsafe fn aws_atomic_compare_exchange_ptr_explicit(
    var: *mut AwsCAtomicVar,
    expected: *mut *mut c_void,
    desired: *mut c_void,
    success: AwsCMemoryOrder,
    failure: AwsCMemoryOrder,
) -> bool {
    let result = var.as_mut()
        .unwrap()
        .as_mut_atomic_usize()
        .compare_exchange(*expected as usize, desired as usize, success.into(), failure.into());

    match result {
        Ok(_) => true,
        Err(actual) => {
            *expected = actual as *mut c_void;
            false
        }
    }
}