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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! A mutual exclusion primitive useful for protecting shared data.
use crate::{
    core::{optional::NSTDOptional, result::NSTDResult, time::NSTDDuration},
    heap_ptr::{
        nstd_heap_ptr_drop, nstd_heap_ptr_get, nstd_heap_ptr_get_mut, NSTDHeapPtr,
        NSTDOptionalHeapPtr,
    },
    thread::nstd_thread_is_panicking,
    NSTDAny, NSTDAnyMut, NSTDBool, NSTD_FALSE, NSTD_TRUE,
};
use core::{
    cell::{Cell, UnsafeCell},
    marker::PhantomData,
    mem::MaybeUninit,
};
use libc::{
    pthread_mutex_destroy, pthread_mutex_init, pthread_mutex_lock, pthread_mutex_t,
    pthread_mutex_trylock, pthread_mutex_unlock, pthread_mutexattr_destroy, pthread_mutexattr_init,
    pthread_mutexattr_settype, pthread_mutexattr_t, PTHREAD_MUTEX_INITIALIZER,
    PTHREAD_MUTEX_NORMAL,
};
use nstdapi::nstdapi;

/// A raw mutex wrapping `pthread_mutex_t`.
///
/// This type has the same in-memory representation as `pthread_mutex_t`.
#[repr(transparent)]
struct RawMutex(UnsafeCell<pthread_mutex_t>);
impl Drop for RawMutex {
    /// [RawMutex]'s destructor.
    fn drop(&mut self) {
        // SAFETY: Destroying a locked mutex results in undefined behavior, so here we check if the
        // mutex is locked. If the mutex *is* locked then it's guard must have been leaked, in this
        // case we will leak the raw mutex data as well.
        unsafe {
            if pthread_mutex_trylock(self.0.get()) == 0 {
                // This shall only fail if the mutex is either robust,
                // `PTHREAD_MUTEX_ERRORCHECK`, or `PTHREAD_MUTEX_RECURSIVE` and the thread does
                // not own the mutex.
                pthread_mutex_unlock(self.0.get());
                pthread_mutex_destroy(self.0.get());
            }
        }
    }
}

/// A mutex attribute builder.
struct MutexAttrs(pthread_mutexattr_t);
impl MutexAttrs {
    /// Creates a new instance of [MutexAttrs].
    fn new() -> Option<Self> {
        let mut attr = MaybeUninit::uninit();
        // SAFETY: All operations are thread-safe, errors are checked.
        unsafe {
            if pthread_mutexattr_init(attr.as_mut_ptr()) == 0 {
                // This shall never fail, PTHREAD_MUTEX_NORMAL is a valid type.
                pthread_mutexattr_settype(attr.as_mut_ptr(), PTHREAD_MUTEX_NORMAL);
                return Some(Self(attr.assume_init()));
            }
        }
        None
    }
}
impl Drop for MutexAttrs {
    /// [MutexAttrs] destructor.
    #[inline]
    fn drop(&mut self) {
        // SAFETY: Rust's type system will ensure that `Self` is properly initialized.
        unsafe { pthread_mutexattr_destroy(&mut self.0) };
    }
}

/// A mutual exclusion primitive useful for protecting shared data.
#[nstdapi]
pub struct NSTDUnixMutex<'a> {
    /// The underlying mutex.
    inner: RawMutex,
    /// The protected data.
    data: UnsafeCell<NSTDHeapPtr<'a>>,
    /// Determines whether or not the mutex is poisoned.
    poisoned: Cell<NSTDBool>,
}
/// # Safety
///
/// The data that the mutex is protecting must be able to be safely sent between threads.
// SAFETY: The user guarantees that the data is thread-safe.
unsafe impl Send for NSTDUnixMutex<'_> {}
/// # Safety
///
/// The data that the mutex is protecting must be able to be safely shared between threads.
// SAFETY: The user guarantees that the data is thread-safe.
unsafe impl Sync for NSTDUnixMutex<'_> {}

/// Represents an optional value of type `NSTDUnixMutex`.
pub type NSTDUnixOptionalMutex<'a> = NSTDOptional<NSTDUnixMutex<'a>>;

/// A handle to a mutex's protected data.
#[nstdapi]
pub struct NSTDUnixMutexGuard<'m, 'a> {
    /// A reference to the mutex.
    mutex: &'m NSTDUnixMutex<'a>,
    /// Ensures that the guard is not [Send].
    pd: PhantomData<*const ()>,
}
impl<'m, 'a> NSTDUnixMutexGuard<'m, 'a> {
    /// Constructs a new mutex guard.
    #[inline]
    fn new(mutex: &'m NSTDUnixMutex<'a>) -> Self {
        Self {
            mutex,
            pd: Default::default(),
        }
    }
}
impl Drop for NSTDUnixMutexGuard<'_, '_> {
    /// Drops the guard, releasing the lock for the mutex.
    fn drop(&mut self) {
        #[allow(unused_unsafe)]
        // SAFETY: This operation is safe.
        if unsafe { nstd_thread_is_panicking() } {
            self.mutex.poisoned.set(NSTD_TRUE);
        }
        // SAFETY: `self` has a valid reference to the mutex.
        // This shall only fail if the mutex is either robust, `PTHREAD_MUTEX_ERRORCHECK`, or
        // `PTHREAD_MUTEX_RECURSIVE` and the thread does not own the mutex.
        unsafe { pthread_mutex_unlock(self.mutex.inner.0.get()) };
    }
}
/// # Safety
///
/// The data that the guard is protecting must be able to be safely shared between threads.
// SAFETY: The user guarantees that the data is thread-safe.
unsafe impl Sync for NSTDUnixMutexGuard<'_, '_> {}

/// A result type returned from `nstd_os_unix_mutex_lock` containing the mutex guard whether or not
/// the data is poisoned.
pub type NSTDUnixMutexLockResult<'m, 'a> =
    NSTDResult<NSTDUnixMutexGuard<'m, 'a>, NSTDUnixMutexGuard<'m, 'a>>;

/// An optional value of type `NSTDUnixMutexLockResult`.
///
/// This type is returned from the `nstd_os_unix_mutex_try_lock` where the uninitialized variant
/// means that the function would block.
pub type NSTDUnixOptionalMutexLockResult<'m, 'a> = NSTDOptional<NSTDUnixMutexLockResult<'m, 'a>>;

/// Creates a new mutex in an unlocked state.
///
/// # Parameters:
///
/// - `NSTDHeapPtr data` - The data to be protected by the mutex.
///
/// # Returns
///
/// `NSTDUnixOptionalMutex mutex` - The new initialized mutex on success, or an uninitialized "none"
/// value if the OS was unable to create and initialize the mutex.
#[nstdapi]
pub fn nstd_os_unix_mutex_new(data: NSTDHeapPtr) -> NSTDUnixOptionalMutex {
    let mutex = RawMutex(UnsafeCell::new(PTHREAD_MUTEX_INITIALIZER));
    if let Some(attrs) = MutexAttrs::new() {
        // SAFETY: `attrs` is properly initialized.
        if unsafe { pthread_mutex_init(mutex.0.get(), &attrs.0) } == 0 {
            return NSTDOptional::Some(NSTDUnixMutex {
                inner: mutex,
                data: UnsafeCell::new(data),
                poisoned: Cell::new(NSTD_FALSE),
            });
        }
    }
    NSTDOptional::None
}

/// Returns a Unix mutex's native OS handle.
///
/// # Parameters:
///
/// - `const NSTDUnixMutex *mutex` - The mutex.
///
/// # Returns
///
/// `pthread_mutex_t raw` - The native mutex handle.
#[inline]
#[nstdapi]
pub fn nstd_os_unix_mutex_handle(mutex: &NSTDUnixMutex) -> pthread_mutex_t {
    // SAFETY: `mutex` is behind an initialized reference.
    unsafe { *mutex.inner.0.get() }
}

/// Determines whether or not a mutex's data is poisoned.
///
/// # Parameters:
///
/// - `const NSTDUnixMutex *mutex` - The mutex to check.
///
/// # Returns
///
/// `NSTDBool is_poisoned` - `NSTD_TRUE` if the mutex's data is poisoned.
#[inline]
#[nstdapi]
pub fn nstd_os_unix_mutex_is_poisoned(mutex: &NSTDUnixMutex) -> NSTDBool {
    mutex.poisoned.get()
}

/// Waits for a mutex lock to become acquired, returning a guard wrapping the protected data.
///
/// # Parameters:
///
/// - `const NSTDUnixMutex *mutex` - The mutex to lock.
///
/// # Returns
///
/// `NSTDUnixOptionalMutexLockResult guard` - A handle to the mutex's protected data on success, or
/// an uninitialized "none" value if the OS failed to lock the mutex.
#[nstdapi]
pub fn nstd_os_unix_mutex_lock<'m, 'a>(
    mutex: &'m NSTDUnixMutex<'a>,
) -> NSTDUnixOptionalMutexLockResult<'m, 'a> {
    // SAFETY: `mutex` is behind an initialized reference.
    if unsafe { pthread_mutex_lock(mutex.inner.0.get()) } == 0 {
        let guard = NSTDUnixMutexGuard::new(mutex);
        return NSTDOptional::Some(match mutex.poisoned.get() {
            true => NSTDResult::Err(guard),
            false => NSTDResult::Ok(guard),
        });
    }
    NSTDOptional::None
}

/// The non-blocking variant of `nstd_os_unix_mutex_lock`. This will return immediately with an
/// uninitialized "none" value if the mutex is locked.
///
/// # Note
///
/// This operation may return a "none" value in the case that the OS fails to lock the mutex.
///
/// # Parameters:
///
/// - `const NSTDUnixMutex *mutex` - The mutex to lock.
///
/// # Returns
///
/// `NSTDUnixOptionalMutexLockResult guard` - A handle to the mutex's data, or "none" if the mutex
/// is locked.
#[nstdapi]
pub fn nstd_os_unix_mutex_try_lock<'m, 'a>(
    mutex: &'m NSTDUnixMutex<'a>,
) -> NSTDUnixOptionalMutexLockResult<'m, 'a> {
    // SAFETY: `mutex` is behind an initialized reference.
    if unsafe { pthread_mutex_trylock(mutex.inner.0.get()) } == 0 {
        let guard = NSTDUnixMutexGuard::new(mutex);
        return NSTDOptional::Some(match mutex.poisoned.get() {
            true => NSTDResult::Err(guard),
            false => NSTDResult::Ok(guard),
        });
    }
    NSTDOptional::None
}

/// The timed variant of `nstd_os_unix_mutex_lock`. This will return with an uninitialized "none"
/// value if the mutex remains locked for the time span of `duration`.
///
/// # Notes
///
/// This operation may return a "none" value in the case that the OS fails to lock the mutex.
///
/// This function will return immediately with a "none" value on unsupported platforms.
/// Supported platforms include Android, DragonFly BSD, FreeBSD, NetBSD, OpenBSD, Haiku, illumos,
/// Linux, QNX Neutrino, and Oracle Solaris.
///
/// # Parameters:
///
/// - `const NSTDUnixMutex *mutex` - The mutex to lock.
///
/// - `NSTDDuration duration` - The amount of time to block for.
///
/// # Returns
///
/// `NSTDUnixOptionalMutexLockResult guard` - A handle to the mutex's data, or "none" if the mutex
/// remains locked for the time span of `duration`.
#[nstdapi]
#[allow(unused_variables)]
pub fn nstd_os_unix_mutex_timed_lock<'m, 'a>(
    mutex: &'m NSTDUnixMutex<'a>,
    duration: NSTDDuration,
) -> NSTDUnixOptionalMutexLockResult<'m, 'a> {
    #[cfg(any(
        target_os = "android",
        target_os = "dragonfly",
        target_os = "freebsd",
        target_os = "haiku",
        target_os = "illumos",
        target_os = "linux",
        target_os = "netbsd",
        target_os = "nto",
        target_os = "openbsd",
        target_os = "solaris"
    ))]
    {
        use crate::os::unix::time::{
            nstd_os_unix_time_add, nstd_os_unix_time_nanoseconds, nstd_os_unix_time_now,
            nstd_os_unix_time_seconds,
        };
        use libc::{pthread_mutex_timedlock, timespec};
        if let NSTDOptional::Some(mut time) = nstd_os_unix_time_now() {
            time = nstd_os_unix_time_add(time, duration);
            let duration = timespec {
                tv_sec: nstd_os_unix_time_seconds(time) as _,
                tv_nsec: nstd_os_unix_time_nanoseconds(time) as _,
            };
            // SAFETY: `mutex` is behind an initialized reference.
            if unsafe { pthread_mutex_timedlock(mutex.inner.0.get(), &duration) } == 0 {
                let guard = NSTDUnixMutexGuard::new(mutex);
                return NSTDOptional::Some(match mutex.poisoned.get() {
                    true => NSTDResult::Err(guard),
                    false => NSTDResult::Ok(guard),
                });
            }
        }
    }
    NSTDOptional::None
}

/// Returns a pointer to a mutex's raw data.
///
/// # Parameters:
///
/// - `const NSTDUnixMutexGuard *guard` - A handle to the mutex's protected data.
///
/// # Returns
///
/// `NSTDAny data` - A pointer to the mutex's data.
#[inline]
#[nstdapi]
pub fn nstd_os_unix_mutex_get(guard: &NSTDUnixMutexGuard) -> NSTDAny {
    // SAFETY: `mutex` is behind a valid reference.
    nstd_heap_ptr_get(unsafe { &*guard.mutex.data.get() })
}

/// Returns a mutable pointer to a mutex's raw data.
///
/// # Parameters:
///
/// - `NSTDUnixMutexGuard *guard` - A handle to the mutex's protected data.
///
/// # Returns
///
/// `NSTDAnyMut data` - A pointer to the mutex's data.
#[inline]
#[nstdapi]
pub fn nstd_os_unix_mutex_get_mut(guard: &mut NSTDUnixMutexGuard) -> NSTDAnyMut {
    // SAFETY: `mutex` is behind a valid reference.
    nstd_heap_ptr_get_mut(unsafe { &mut *guard.mutex.data.get() })
}

/// Consumes a mutex and returns the data it was protecting.
///
/// # Parameters:
///
/// - `NSTDUnixMutex mutex` - The mutex to take ownership of.
///
/// # Returns
///
/// `NSTDOptionalHeapPtr data` - Ownership of the mutex's data, or an uninitialized "none" variant
/// if the mutex was poisoned.
#[inline]
#[nstdapi]
pub fn nstd_os_unix_mutex_into_inner(mutex: NSTDUnixMutex) -> NSTDOptionalHeapPtr {
    match nstd_os_unix_mutex_is_poisoned(&mutex) {
        false => NSTDOptional::Some(mutex.data.into_inner()),
        true => NSTDOptional::None,
    }
}

/// Unlocks a mutex by consuming it's guard.
///
/// # Parameters:
///
/// - `NSTDUnixMutexGuard guard` - The mutex guard to take ownership of.
#[inline]
#[nstdapi]
#[allow(unused_variables)]
pub fn nstd_os_unix_mutex_unlock(guard: NSTDUnixMutexGuard) {}

/// Frees an instance of `NSTDUnixMutex`.
///
/// # Parameters:
///
/// - `NSTDUnixMutex mutex` - The mutex to free.
#[inline]
#[nstdapi]
#[allow(unused_variables)]
pub fn nstd_os_unix_mutex_free(mutex: NSTDUnixMutex) {}

/// Frees an instance of `NSTDUnixMutex` after invoking `callback` with the mutex's data.
///
/// `callback` will not be called if the mutex is poisoned.
///
/// # Parameters:
///
/// - `NSTDUnixMutex mutex` - The mutex to free.
///
/// - `void (*callback)(NSTDAnyMut)` - The mutex data's destructor.
///
/// # Safety
///
/// This operation makes a direct call on a C function pointer (`callback`).
#[inline]
#[nstdapi]
pub unsafe fn nstd_os_unix_mutex_drop(
    mutex: NSTDUnixMutex,
    callback: unsafe extern "C" fn(NSTDAnyMut),
) {
    if !mutex.poisoned.get() {
        nstd_heap_ptr_drop(mutex.data.into_inner(), callback);
    }
}