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
#![allow(clippy::missing_panics_doc, clippy::must_use_candidate)]
use core::ffi::c_void;
use std::ptr::NonNull;
use crate::bridge_support::bridge_ptr_result;
use crate::error::LogError;
use crate::ffi;
/// Safe wrapper around a 32-bit legacy `OSAtomic` value.
pub struct OSAtomicI32 {
ptr: NonNull<c_void>,
}
impl OSAtomicI32 {
#[must_use]
pub fn new(value: i32) -> Self {
Self {
ptr: NonNull::new(unsafe {
// SAFETY: ffi::apple_log_os_atomic_i32_new is a thin wrapper that
// returns a non-null OSAtomicI32 handle. It is safe to call and always
// returns a valid pointer from the Swift bridge.
ffi::apple_log_os_atomic_i32_new(value)
})
.expect("Swift bridge never returns NULL for OSAtomicI32::new"),
}
}
#[must_use]
pub fn load(&self) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is a read-only atomic load operation.
ffi::apple_log_os_atomic_i32_load(self.ptr.as_ptr())
}
}
pub fn store(&self, value: i32) {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic store operation.
ffi::apple_log_os_atomic_i32_store(self.ptr.as_ptr(), value);
}
}
pub fn add(&self, amount: i32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic add operation.
ffi::apple_log_os_atomic_i32_add(self.ptr.as_ptr(), amount)
}
}
pub fn add_barrier(&self, amount: i32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic add with memory barrier operation.
ffi::apple_log_os_atomic_i32_add_barrier(self.ptr.as_ptr(), amount)
}
}
pub fn increment(&self) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic increment operation.
ffi::apple_log_os_atomic_i32_increment(self.ptr.as_ptr())
}
}
pub fn increment_barrier(&self) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic increment with memory barrier operation.
ffi::apple_log_os_atomic_i32_increment_barrier(self.ptr.as_ptr())
}
}
pub fn decrement(&self) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic decrement operation.
ffi::apple_log_os_atomic_i32_decrement(self.ptr.as_ptr())
}
}
pub fn decrement_barrier(&self) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic decrement with memory barrier operation.
ffi::apple_log_os_atomic_i32_decrement_barrier(self.ptr.as_ptr())
}
}
pub fn or(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise OR operation.
ffi::apple_log_os_atomic_i32_or(self.ptr.as_ptr(), mask)
}
}
pub fn or_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise OR with memory barrier operation.
ffi::apple_log_os_atomic_i32_or_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn or_orig(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise OR returning the original value.
ffi::apple_log_os_atomic_i32_or_orig(self.ptr.as_ptr(), mask)
}
}
pub fn or_orig_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise OR with barrier returning the original value.
ffi::apple_log_os_atomic_i32_or_orig_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn and(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise AND operation.
ffi::apple_log_os_atomic_i32_and(self.ptr.as_ptr(), mask)
}
}
pub fn and_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise AND with memory barrier operation.
ffi::apple_log_os_atomic_i32_and_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn and_orig(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise AND returning the original value.
ffi::apple_log_os_atomic_i32_and_orig(self.ptr.as_ptr(), mask)
}
}
pub fn and_orig_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise AND with barrier returning the original value.
ffi::apple_log_os_atomic_i32_and_orig_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn xor(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise XOR operation.
ffi::apple_log_os_atomic_i32_xor(self.ptr.as_ptr(), mask)
}
}
pub fn xor_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise XOR with memory barrier operation.
ffi::apple_log_os_atomic_i32_xor_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn xor_orig(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise XOR returning the original value.
ffi::apple_log_os_atomic_i32_xor_orig(self.ptr.as_ptr(), mask)
}
}
pub fn xor_orig_barrier(&self, mask: u32) -> i32 {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic bitwise XOR with barrier returning the original value.
ffi::apple_log_os_atomic_i32_xor_orig_barrier(self.ptr.as_ptr(), mask)
}
}
pub fn compare_and_swap(&self, old_value: i32, new_value: i32) -> bool {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic compare-and-swap operation.
ffi::apple_log_os_atomic_i32_compare_and_swap(self.ptr.as_ptr(), old_value, new_value)
}
}
pub fn compare_and_swap_barrier(&self, old_value: i32, new_value: i32) -> bool {
unsafe {
// SAFETY: self.ptr is a valid non-null OSAtomicI32 pointer.
// This is an atomic compare-and-swap with memory barrier operation.
ffi::apple_log_os_atomic_i32_compare_and_swap_barrier(
self.ptr.as_ptr(),
old_value,
new_value,
)
}
}
}
impl Drop for OSAtomicI32 {
fn drop(&mut self) {
unsafe { ffi::apple_log_os_atomic_i32_release(self.ptr.as_ptr()) };
}
}
/// Safe wrapper around a 64-bit legacy `OSAtomic` value.
pub struct OSAtomicI64 {
ptr: NonNull<c_void>,
}
impl OSAtomicI64 {
#[must_use]
pub fn new(value: i64) -> Self {
Self {
ptr: NonNull::new(unsafe { ffi::apple_log_os_atomic_i64_new(value) })
.expect("Swift bridge never returns NULL for OSAtomicI64::new"),
}
}
#[must_use]
pub fn load(&self) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_load(self.ptr.as_ptr()) }
}
pub fn store(&self, value: i64) {
unsafe { ffi::apple_log_os_atomic_i64_store(self.ptr.as_ptr(), value) };
}
pub fn add(&self, amount: i64) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_add(self.ptr.as_ptr(), amount) }
}
pub fn add_barrier(&self, amount: i64) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_add_barrier(self.ptr.as_ptr(), amount) }
}
pub fn increment(&self) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_increment(self.ptr.as_ptr()) }
}
pub fn increment_barrier(&self) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_increment_barrier(self.ptr.as_ptr()) }
}
pub fn decrement(&self) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_decrement(self.ptr.as_ptr()) }
}
pub fn decrement_barrier(&self) -> i64 {
unsafe { ffi::apple_log_os_atomic_i64_decrement_barrier(self.ptr.as_ptr()) }
}
pub fn compare_and_swap(&self, old_value: i64, new_value: i64) -> bool {
unsafe {
ffi::apple_log_os_atomic_i64_compare_and_swap(self.ptr.as_ptr(), old_value, new_value)
}
}
pub fn compare_and_swap_barrier(&self, old_value: i64, new_value: i64) -> bool {
unsafe {
ffi::apple_log_os_atomic_i64_compare_and_swap_barrier(
self.ptr.as_ptr(),
old_value,
new_value,
)
}
}
}
impl Drop for OSAtomicI64 {
fn drop(&mut self) {
unsafe { ffi::apple_log_os_atomic_i64_release(self.ptr.as_ptr()) };
}
}
/// Queue backed by `OSAtomicEnqueue` / `OSAtomicDequeue`.
pub struct OSAtomicQueue {
ptr: NonNull<c_void>,
}
impl OSAtomicQueue {
/// Creates an empty queue.
///
/// # Errors
///
/// Returns an error if the bridge cannot allocate the queue.
pub fn new() -> Result<Self, LogError> {
let ptr = bridge_ptr_result("OSAtomicQueue::new", |error_out| unsafe {
ffi::apple_log_os_atomic_queue_new(error_out)
})?;
Ok(Self { ptr })
}
pub fn enqueue(&self, value: usize) {
unsafe { ffi::apple_log_os_atomic_queue_enqueue(self.ptr.as_ptr(), value) };
}
#[must_use]
pub fn dequeue(&self) -> Option<usize> {
let mut value = 0_usize;
unsafe { ffi::apple_log_os_atomic_queue_dequeue(self.ptr.as_ptr(), &mut value) }
.then_some(value)
}
}
impl Drop for OSAtomicQueue {
fn drop(&mut self) {
unsafe { ffi::apple_log_os_atomic_queue_release(self.ptr.as_ptr()) };
}
}
/// FIFO queue backed by `OSAtomicFifoEnqueue` / `OSAtomicFifoDequeue`.
pub struct OSAtomicFifoQueue {
ptr: NonNull<c_void>,
}
impl OSAtomicFifoQueue {
/// Creates an empty FIFO queue.
///
/// # Errors
///
/// Returns an error if the bridge cannot allocate the queue.
pub fn new() -> Result<Self, LogError> {
let ptr = bridge_ptr_result("OSAtomicFifoQueue::new", |error_out| unsafe {
ffi::apple_log_os_atomic_fifo_queue_new(error_out)
})?;
Ok(Self { ptr })
}
pub fn enqueue(&self, value: usize) {
unsafe { ffi::apple_log_os_atomic_fifo_queue_enqueue(self.ptr.as_ptr(), value) };
}
#[must_use]
pub fn dequeue(&self) -> Option<usize> {
let mut value = 0_usize;
unsafe { ffi::apple_log_os_atomic_fifo_queue_dequeue(self.ptr.as_ptr(), &mut value) }
.then_some(value)
}
}
impl Drop for OSAtomicFifoQueue {
fn drop(&mut self) {
unsafe { ffi::apple_log_os_atomic_fifo_queue_release(self.ptr.as_ptr()) };
}
}