jflib-plat 0.1.0

JFLib's platform layer: one module per operating system with the same functions
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// sync -- blocking primitives for worker threads (servers, background jobs)
//
//   Mutex<T> (lock / try_lock -> MutexGuard)     Condvar (wait / wait_timeout / notify_*)
//   RwLock<T> (read / write; writers first)      channel(cap) -> (Sender<T>, Receiver<T>)
//
// One primitive per OS: wait on a 32-bit word while it holds a value, wake one / all waiters
// (Windows WaitOnAddress, Linux private futexes).  Mutex and Condvar are the futex algorithms
// of Rust's std; RwLock and the channel are built on them.  A lock is not poisoned by a panic
// (panics abort the process here).
// Not here: rt::SpinLock (rt keeps spin locks for the allocator and for modules, where plat is
// replaced).

use alloc::collections::VecDeque;
use alloc::sync::Arc;
use core::cell::UnsafeCell;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::{AtomicU32, Ordering};

#[cfg(windows)]
mod os {
    use core::sync::atomic::AtomicU32;

    #[link(name = "api-ms-win-core-synch-l1-2-0", kind = "raw-dylib")]
    unsafe extern "system" {
        fn WaitOnAddress(addr: *const u32, cmp: *const u32, size: usize, ms: u32) -> i32;
        fn WakeByAddressSingle(addr: *const u32);
        fn WakeByAddressAll(addr: *const u32);
    }

    /// sleep while *a == expected (None: no timeout) -> false on timeout
    pub fn wait(a: &AtomicU32, expected: u32, ms: Option<u32>) -> bool {
        // INFINITE is u32::MAX: a finite wait stays below it
        let t = ms.map_or(u32::MAX, |m| m.min(u32::MAX - 1));
        // SAFETY: both pointers are valid for 4 bytes for the duration of the call.
        unsafe { WaitOnAddress(a.as_ptr(), &expected, 4, t) != 0 }
    }

    pub fn wake_one(a: &AtomicU32) {
        // SAFETY: a valid address; waking nobody is fine.
        unsafe { WakeByAddressSingle(a.as_ptr()) }
    }

    pub fn wake_all(a: &AtomicU32) {
        // SAFETY: as above.
        unsafe { WakeByAddressAll(a.as_ptr()) }
    }
}

#[cfg(target_os = "linux")]
mod os {
    use core::sync::atomic::AtomicU32;

    use rt::os::{syscall6, SYS_FUTEX};

    const FUTEX_WAIT_PRIVATE: usize = 128;
    const FUTEX_WAKE_PRIVATE: usize = 129;
    const ETIMEDOUT: isize = 110;

    pub fn wait(a: &AtomicU32, expected: u32, ms: Option<u32>) -> bool {
        let ts = ms.map(|m| [(m / 1000) as i64, (m % 1000) as i64 * 1_000_000]);
        let tp = ts.as_ref().map_or(0, |t| t.as_ptr() as usize);
        // SAFETY: the futex word and the relative timespec outlive the call.
        let r = unsafe { syscall6(SYS_FUTEX, a.as_ptr() as usize, FUTEX_WAIT_PRIVATE, expected as usize, tp, 0, 0) };
        r != -ETIMEDOUT
    }

    pub fn wake_one(a: &AtomicU32) {
        // SAFETY: waking a private futex word we own.
        unsafe { syscall6(SYS_FUTEX, a.as_ptr() as usize, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0) };
    }

    pub fn wake_all(a: &AtomicU32) {
        // SAFETY: as above.
        unsafe { syscall6(SYS_FUTEX, a.as_ptr() as usize, FUTEX_WAKE_PRIVATE, i32::MAX as usize, 0, 0, 0) };
    }
}

pub(crate) use os::{wait, wake_all, wake_one};

// ------------------------------------------------------------------ Mutex

/// a mutual-exclusion lock around a value
pub struct Mutex<T: ?Sized> {
    /// 0 unlocked, 1 locked, 2 locked with waiters
    state: AtomicU32,
    data: UnsafeCell<T>,
}

// SAFETY: the lock hands out the value to one thread at a time.
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
// SAFETY: as above; &Mutex only gives access through the lock.
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}

impl<T> Mutex<T> {
    pub const fn new(v: T) -> Mutex<T> {
        Mutex { state: AtomicU32::new(0), data: UnsafeCell::new(v) }
    }

    pub fn into_inner(self) -> T {
        self.data.into_inner()
    }
}

impl<T: Default> Default for Mutex<T> {
    fn default() -> Self {
        Mutex::new(T::default())
    }
}

impl<T: ?Sized> Mutex<T> {
    pub fn lock(&self) -> MutexGuard<'_, T> {
        if self.state.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed).is_err() {
            self.lock_contended();
        }
        MutexGuard { m: self }
    }

    pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
        self.state.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed).ok().map(|_| MutexGuard { m: self })
    }

    /// the value without locking (the borrow proves exclusivity)
    pub fn get_mut(&mut self) -> &mut T {
        self.data.get_mut()
    }

    fn spin(&self) -> u32 {
        let mut n = 100;
        loop {
            let s = self.state.load(Ordering::Relaxed);
            if s != 1 || n == 0 {
                return s;
            }
            n -= 1;
            core::hint::spin_loop();
        }
    }

    fn lock_contended(&self) {
        let mut s = self.spin();
        if s == 0 {
            match self.state.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed) {
                Ok(_) => return,
                Err(e) => s = e,
            }
        }
        loop {
            if s != 2 && self.state.swap(2, Ordering::Acquire) == 0 {
                return;
            }
            wait(&self.state, 2, None);
            s = self.spin();
        }
    }

    fn unlock(&self) {
        if self.state.swap(0, Ordering::Release) == 2 {
            wake_one(&self.state);
        }
    }
}

/// the locked value; unlocks when dropped
pub struct MutexGuard<'a, T: ?Sized> {
    m: &'a Mutex<T>,
}

impl<T: ?Sized> Deref for MutexGuard<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        // SAFETY: the guard holds the lock.
        unsafe { &*self.m.data.get() }
    }
}

impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        // SAFETY: the guard holds the lock exclusively.
        unsafe { &mut *self.m.data.get() }
    }
}

impl<T: ?Sized> Drop for MutexGuard<'_, T> {
    fn drop(&mut self) {
        self.m.unlock();
    }
}

// ------------------------------------------------------------------ Condvar

/// a condition variable for Mutex
#[derive(Default)]
pub struct Condvar {
    seq: AtomicU32,
}

impl Condvar {
    pub const fn new() -> Condvar {
        Condvar { seq: AtomicU32::new(0) }
    }

    /// unlock, sleep until notified (or spuriously), lock again
    pub fn wait<'a, T: ?Sized>(&self, g: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
        self.wait_timeout(g, None).0
    }

    /// as wait, at most `ms` milliseconds (None: forever) -> (guard, timed out)
    pub fn wait_timeout<'a, T: ?Sized>(&self, g: MutexGuard<'a, T>, ms: Option<u32>) -> (MutexGuard<'a, T>, bool) {
        let m = g.m;
        let s = self.seq.load(Ordering::Relaxed);
        drop(g);
        let woken = wait(&self.seq, s, ms);
        (m.lock(), !woken)
    }

    /// wait while `cond` holds (re-checked after every wake-up)
    pub fn wait_while<'a, T: ?Sized>(&self, mut g: MutexGuard<'a, T>, mut cond: impl FnMut(&mut T) -> bool) -> MutexGuard<'a, T> {
        while cond(&mut g) {
            g = self.wait(g);
        }
        g
    }

    pub fn notify_one(&self) {
        self.seq.fetch_add(1, Ordering::Relaxed);
        wake_one(&self.seq);
    }

    pub fn notify_all(&self) {
        self.seq.fetch_add(1, Ordering::Relaxed);
        wake_all(&self.seq);
    }
}

// ------------------------------------------------------------------ RwLock

#[derive(Default)]
struct RwState {
    readers: u32,
    writer: bool,
    writers_waiting: u32,
}

/// many readers or one writer; a waiting writer stops new readers
pub struct RwLock<T: ?Sized> {
    st: Mutex<RwState>,
    cv: Condvar,
    data: UnsafeCell<T>,
}

// SAFETY: access to the value follows the reader / writer protocol of the state.
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
// SAFETY: readers share &T (needs Sync), writers get &mut T (needs Send).
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}

impl<T> RwLock<T> {
    pub const fn new(v: T) -> RwLock<T> {
        RwLock { st: Mutex::new(RwState { readers: 0, writer: false, writers_waiting: 0 }), cv: Condvar::new(), data: UnsafeCell::new(v) }
    }
}

impl<T: ?Sized> RwLock<T> {
    pub fn read(&self) -> RwReadGuard<'_, T> {
        let mut s = self.cv.wait_while(self.st.lock(), |s| s.writer || s.writers_waiting > 0);
        s.readers += 1;
        RwReadGuard { l: self }
    }

    pub fn write(&self) -> RwWriteGuard<'_, T> {
        let mut s = self.st.lock();
        s.writers_waiting += 1;
        let mut s = self.cv.wait_while(s, |s| s.writer || s.readers > 0);
        s.writers_waiting -= 1;
        s.writer = true;
        RwWriteGuard { l: self }
    }
}

pub struct RwReadGuard<'a, T: ?Sized> {
    l: &'a RwLock<T>,
}

impl<T: ?Sized> Deref for RwReadGuard<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        // SAFETY: a reader holds the lock; no writer is active.
        unsafe { &*self.l.data.get() }
    }
}

impl<T: ?Sized> Drop for RwReadGuard<'_, T> {
    fn drop(&mut self) {
        let mut s = self.l.st.lock();
        s.readers -= 1;
        if s.readers == 0 {
            self.l.cv.notify_all();
        }
    }
}

pub struct RwWriteGuard<'a, T: ?Sized> {
    l: &'a RwLock<T>,
}

impl<T: ?Sized> Deref for RwWriteGuard<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        // SAFETY: the writer holds the lock exclusively.
        unsafe { &*self.l.data.get() }
    }
}

impl<T: ?Sized> DerefMut for RwWriteGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        // SAFETY: as above.
        unsafe { &mut *self.l.data.get() }
    }
}

impl<T: ?Sized> Drop for RwWriteGuard<'_, T> {
    fn drop(&mut self) {
        self.l.st.lock().writer = false;
        self.l.cv.notify_all();
    }
}

// ------------------------------------------------------------------ channel

struct ChanState<T> {
    items: VecDeque<T>,
    cap: usize,
    senders: usize,
    receiver: bool,
}

struct Chan<T> {
    st: Mutex<ChanState<T>>,
    not_empty: Condvar,
    not_full: Condvar,
}

/// a bounded multi-producer, single-consumer queue (cap >= 1)
pub fn channel<T>(cap: usize) -> (Sender<T>, Receiver<T>) {
    let c = Arc::new(Chan {
        st: Mutex::new(ChanState { items: VecDeque::new(), cap: cap.max(1), senders: 1, receiver: true }),
        not_empty: Condvar::new(),
        not_full: Condvar::new(),
    });
    (Sender { c: c.clone() }, Receiver { c })
}

pub struct Sender<T> {
    c: Arc<Chan<T>>,
}

pub struct Receiver<T> {
    c: Arc<Chan<T>>,
}

/// why recv_timeout returned nothing
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RecvError {
    Timeout,
    Disconnected,
}

impl<T> Sender<T> {
    /// queue a value, waiting while the queue is full -> Err(v) when the receiver is gone
    pub fn send(&self, v: T) -> Result<(), T> {
        let mut s = self.c.not_full.wait_while(self.c.st.lock(), |s| s.receiver && s.items.len() >= s.cap);
        if !s.receiver {
            return Err(v);
        }
        s.items.push_back(v);
        drop(s);
        self.c.not_empty.notify_one();
        Ok(())
    }

    /// queue a value if there is room -> Err(v) when full or the receiver is gone
    pub fn try_send(&self, v: T) -> Result<(), T> {
        let mut s = self.c.st.lock();
        if !s.receiver || s.items.len() >= s.cap {
            return Err(v);
        }
        s.items.push_back(v);
        drop(s);
        self.c.not_empty.notify_one();
        Ok(())
    }
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self {
        self.c.st.lock().senders += 1;
        Sender { c: self.c.clone() }
    }
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        let mut s = self.c.st.lock();
        s.senders -= 1;
        if s.senders == 0 {
            drop(s);
            self.c.not_empty.notify_all();
        }
    }
}

impl<T> Receiver<T> {
    /// the next value, waiting for one -> None when the queue is empty and every sender is gone
    pub fn recv(&self) -> Option<T> {
        let mut s = self.c.not_empty.wait_while(self.c.st.lock(), |s| s.items.is_empty() && s.senders > 0);
        let v = s.items.pop_front();
        drop(s);
        if v.is_some() {
            self.c.not_full.notify_one();
        }
        v
    }

    /// the next value, waiting at most `ms` milliseconds
    pub fn recv_timeout(&self, ms: u32) -> Result<T, RecvError> {
        let deadline = crate::time_us().saturating_add(ms as u64 * 1000);
        let mut s = self.c.st.lock();
        loop {
            if let Some(v) = s.items.pop_front() {
                drop(s);
                self.c.not_full.notify_one();
                return Ok(v);
            }
            if s.senders == 0 {
                return Err(RecvError::Disconnected);
            }
            let now = crate::time_us();
            if now >= deadline {
                return Err(RecvError::Timeout);
            }
            let left = ((deadline - now).div_ceil(1000)).min(u32::MAX as u64) as u32;
            s = self.c.not_empty.wait_timeout(s, Some(left)).0;
        }
    }

    /// a value if one is queued
    pub fn try_recv(&self) -> Option<T> {
        let v = self.c.st.lock().items.pop_front();
        if v.is_some() {
            self.c.not_full.notify_one();
        }
        v
    }

    /// values queued right now
    pub fn len(&self) -> usize {
        self.c.st.lock().items.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut s = self.c.st.lock();
        s.receiver = false;
        s.items.clear();
        drop(s);
        self.c.not_full.notify_all();
    }
}