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
473
474
475
476
477
478
479
480
481
482
483
484
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;

use core::cell::UnsafeCell;
use core::fmt;
use core::hint;
use core::ops::Index;
use core::sync::atomic::{AtomicUsize, Ordering};

////////////////////////////////////////////////////////////////////////////////////////////////////
// BoundedThreadLocal
////////////////////////////////////////////////////////////////////////////////////////////////////

/// A one-shot, lock-free, bounded per-value thread local storage.
///
/// # Examples
///
/// ```
/// use std::thread;
/// use std::sync::Arc;
///
/// use conquer_util::BoundedThreadLocal;
///
/// const THREADS: usize = 4;
/// let counter = Arc::new(BoundedThreadLocal::<usize>::new(THREADS));
///
/// let handles: Vec<_> = (0..THREADS)
///     .map(|id| {
///         let counter = Arc::clone(&counter);
///         thread::spawn(move || {
///             let mut token = counter.thread_token().unwrap();
///             *token.get_mut() += id;
///         })
///     })
///     .collect();
///
/// for handle in handles {
///     handle.join().unwrap();
/// }
///
/// let mut counter = Arc::try_unwrap(counter).unwrap();
///
/// let sum: usize = counter.into_iter().sum();
/// assert_eq!(sum, (0..4).sum());
/// ```
pub struct BoundedThreadLocal<'s, T> {
    storage: Storage<'s, T>,
    registered: AtomicUsize,
    completed: AtomicUsize,
}

/********** impl Send + Sync **********************************************************************/

unsafe impl<T> Send for BoundedThreadLocal<'_, T> {}
unsafe impl<T> Sync for BoundedThreadLocal<'_, T> {}

/********** impl inherent *************************************************************************/

impl<'s, T: Default> BoundedThreadLocal<'s, T> {
    /// Creates a new [`Default`] initialized [`BoundedThreadLocal`] that
    /// internally allocates a buffer of `max_size`.
    ///
    /// # Panics
    ///
    /// This method panics, if `max_size` is 0.
    #[inline]
    pub fn new(max_threads: usize) -> Self {
        Self::with_init(max_threads, Default::default)
    }
}

impl<'s, T> BoundedThreadLocal<'s, T> {
    /// Creates a new [`BoundedThreadLocal`] that internally allocates a buffer
    /// of `max_size` and initializes each [`Local`] with `init`.
    ///
    /// # Panics
    ///
    /// This method panics, if `max_size` is 0.
    #[inline]
    pub fn with_init(max_threads: usize, init: impl Fn() -> T) -> Self {
        assert!(max_threads > 0, "`max_threads` must be greater than 0");
        Self {
            storage: Storage::Heap(
                (0..max_threads).map(|_| Local(UnsafeCell::new(Some(init())))).collect(),
            ),
            registered: AtomicUsize::new(0),
            completed: AtomicUsize::new(0),
        }
    }
}

impl<'s, T> BoundedThreadLocal<'s, T> {
    /// Creates a new [`BoundedThreadLocal`] that is based on a separate `buffer`.
    ///
    /// # Safety
    ///
    /// The given `buf` must be treated as if it were mutably, i.e. it **must
    /// not** be used or otherwise accessed during the lifetime of the
    /// [`BoundedThreadLocal`] that borrows it.
    ///
    /// # Examples
    ///
    /// ```
    /// use conquer_util::{BoundedThreadLocal, Local};
    ///
    /// static BUF: [Local<usize>; 4] =
    ///     [Local::new(0), Local::new(0), Local::new(0), Local::new(0)];
    /// static TLS: BoundedThreadLocal<usize> = unsafe { BoundedThreadLocal::with_buffer(&BUF) };
    /// assert_eq!(TLS.thread_token().unwrap().get(), &0);
    /// ```
    #[inline]
    pub const unsafe fn with_buffer(buf: &'s [Local<T>]) -> Self {
        Self {
            storage: Storage::Buffer(buf),
            registered: AtomicUsize::new(0),
            completed: AtomicUsize::new(0),
        }
    }

    /// Returns a thread local token to a unique instance of `T`.
    ///
    /// The thread local instance will **not** be dropped, when the token itself
    /// is dropped and can e.g. be iterated afterwards.
    ///
    /// # Errors
    ///
    /// This method fails, if the maximum number of tokens for this
    /// [`BoundedThreadLocal`] has already been acquired.
    ///
    /// # Examples
    ///
    /// ```
    /// use conquer_util::BoundedThreadLocal;
    ///
    /// # fn main() -> Result<(), conquer_util::BoundsError> {
    ///
    /// let tls = BoundedThreadLocal::<usize>::new(1);
    /// let mut token = tls.thread_token()?;
    /// *token.get_mut() += 1;
    /// assert_eq!(token.get(), &1);
    ///
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn thread_token(&self) -> Result<Token<'_, '_, T>, BoundsError> {
        let token: usize = self.registered.fetch_add(1, Ordering::Relaxed);
        assert!(token <= isize::max_value() as usize, "thread counter close to overflow");

        if token < self.storage.len() {
            let local = unsafe { self.storage[token].as_ptr_unchecked() };
            Ok(Token { local, tls: self })
        } else {
            Err(BoundsError(()))
        }
    }

    /// Attempts to create an [`Iter`] over all [`Local`] instances.
    ///
    /// # Errors
    ///
    /// Fails, if there are still outstanding thread tokens, that might
    /// concurrently access any of the thread local state instances.
    #[inline]
    pub fn try_iter(&self) -> Result<Iter<T>, ConcurrentAccessErr> {
        let (completed, len) = (self.completed.load(Ordering::Relaxed), self.storage.len());
        if completed == len || completed == self.registered.load(Ordering::Relaxed) {
            Ok(Iter { idx: 0, tls: self })
        } else {
            Err(ConcurrentAccessErr(()))
        }
    }
}

/********** impl IntoIterator *********************************************************************/

impl<'s, T> IntoIterator for BoundedThreadLocal<'s, T> {
    type Item = T;
    type IntoIter = IntoIter<'s, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        IntoIter { tls: self, idx: 0 }
    }
}

/********** impl Debug ****************************************************************************/

impl<T> fmt::Debug for BoundedThreadLocal<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BoundedThreadLocal")
            .field("max_size", &self.storage.len())
            .field("access_count", &self.registered.load(Ordering::Relaxed))
            .finish()
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Local
////////////////////////////////////////////////////////////////////////////////////////////////////

/// A wrapper for an instance of `T` that can be managed by a
/// [`BoundedThreadLocal`].
#[derive(Debug, Default)]
#[repr(align(64))]
pub struct Local<T>(UnsafeCell<Option<T>>);

/********** impl Send + Sync **********************************************************************/

unsafe impl<T> Send for Local<T> {}
unsafe impl<T> Sync for Local<T> {}

/********** impl inherent *************************************************************************/

impl<T> Local<T> {
    /// Creates a new [`Local`].
    #[inline]
    pub const fn new(local: T) -> Self {
        Self(UnsafeCell::new(Some(local)))
    }

    #[inline]
    unsafe fn as_ptr_unchecked(&self) -> *mut T {
        (*self.0.get()).as_mut().unwrap_or_else(|| hint::unreachable_unchecked())
    }

    #[inline]
    unsafe fn take_unchecked(&self) -> T {
        (*self.0.get()).take().unwrap_or_else(|| hint::unreachable_unchecked())
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Token
////////////////////////////////////////////////////////////////////////////////////////////////////

/// A thread local token granting unique access to an instance of `T` that is
/// contained in a [`BoundedThreadLocal`]
pub struct Token<'s, 'tls, T> {
    local: *mut T,
    tls: &'tls BoundedThreadLocal<'s, T>,
}

/********** impl inherent *************************************************************************/

impl<T> Token<'_, '_, T> {
    /// Returns a reference to the thread local state.
    #[inline]
    pub fn get(&self) -> &T {
        unsafe { &(*self.local) }
    }

    /// Returns a mutable reference to the thread local state.
    #[inline]
    pub fn get_mut(&mut self) -> &mut T {
        unsafe { &mut (*self.local) }
    }
}

/********** impl Debug ****************************************************************************/

impl<T: fmt::Debug> fmt::Debug for Token<'_, '_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Token").field("slot", &self.get()).finish()
    }
}

/********** impl Display **************************************************************************/

impl<T: fmt::Display> fmt::Display for Token<'_, '_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.get(), f)
    }
}

/********** impl Drop *****************************************************************************/

impl<T> Drop for Token<'_, '_, T> {
    fn drop(&mut self) {
        self.tls.completed.fetch_add(1, Ordering::Relaxed);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Iter
////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub struct Iter<'s, 'tls, T> {
    idx: usize,
    tls: &'tls BoundedThreadLocal<'s, T>,
}

/********** impl Iterator *************************************************************************/

impl<'s, 'tls, T> Iterator for Iter<'s, 'tls, T> {
    type Item = &'tls T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let idx = self.idx;
        if idx < self.tls.storage.len() {
            self.idx += 1;
            let local = unsafe { &*self.tls.storage[idx].as_ptr_unchecked() };
            Some(local)
        } else {
            None
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// IntoIter
////////////////////////////////////////////////////////////////////////////////////////////////////

/// An owning iterator that can be created from an owned [`BoundedThreadLocal`].
#[derive(Debug)]
pub struct IntoIter<'s, T> {
    idx: usize,
    tls: BoundedThreadLocal<'s, T>,
}

/********** impl Iterator *************************************************************************/

impl<T> Iterator for IntoIter<'_, T> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let idx = self.idx;
        if idx < self.tls.storage.len() {
            self.idx += 1;
            let local = unsafe { self.tls.storage[idx].take_unchecked() };
            Some(local)
        } else {
            None
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// BoundsError
////////////////////////////////////////////////////////////////////////////////////////////////////

/// An Error for signalling than more than the specified maximum number of
/// threads attempted to access a [`BoundedThreadLocal`].
#[derive(Copy, Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct BoundsError(());

/********** impl Display **************************************************************************/

impl fmt::Display for BoundsError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "exceeded bounds for `BoundedThreadLocal`")
    }
}

/********** impl Error ****************************************************************************/

#[cfg(feature = "std")]
impl std::error::Error for BoundsError {}

////////////////////////////////////////////////////////////////////////////////////////////////////
// ConcurrentAccessErr
////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Copy, Clone, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct ConcurrentAccessErr(());

/********** impl Display **************************************************************************/

impl fmt::Display for ConcurrentAccessErr {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "concurrent access from live thread token (not all tokens have yet been dropped")
    }
}

/********** impl Error ****************************************************************************/

#[cfg(feature = "std")]
impl std::error::Error for ConcurrentAccessErr {}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Storage
////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
enum Storage<'s, T> {
    Buffer(&'s [Local<T>]),
    Heap(Box<[Local<T>]>),
}

/********** impl inherent *************************************************************************/

impl<T> Storage<'_, T> {
    #[inline]
    fn len(&self) -> usize {
        match self {
            Storage::Buffer(slice) => slice.len(),
            Storage::Heap(boxed) => boxed.len(),
        }
    }
}

/********** impl Index ****************************************************************************/

impl<T> Index<usize> for Storage<'_, T> {
    type Output = Local<T>;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        match self {
            &Storage::Buffer(slice) => &slice[index],
            &Storage::Heap(ref boxed) => &boxed[index],
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    #[cfg(any(feature = "alloc", feature = "std"))]
    use std::sync::Arc;
    use std::thread;
    use std::vec::Vec;

    use super::BoundedThreadLocal;
    use crate::Local;

    #[test]
    fn static_buffer() {
        static BUF: [Local<usize>; 4] =
            [Local::new(0), Local::new(0), Local::new(0), Local::new(0)];
        static TLS: BoundedThreadLocal<usize> = unsafe { BoundedThreadLocal::with_buffer(&BUF) };

        let handles: Vec<_> = (0..BUF.len())
            .map(|_| {
                thread::spawn(move || {
                    let mut token = TLS.thread_token().unwrap();
                    for _ in 0..10 {
                        *token.get_mut() += 1;
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        assert!(TLS.try_iter().unwrap().all(|&count| count == 10));
    }

    #[test]
    fn into_iter() {
        const THREADS: usize = 4;
        let tls: Arc<BoundedThreadLocal<usize>> = Arc::new(BoundedThreadLocal::new(THREADS));

        let handles: Vec<_> = (0..THREADS)
            .map(|_| {
                let tls = Arc::clone(&tls);
                thread::spawn(move || {
                    let mut token = tls.thread_token().unwrap();
                    for _ in 0..10 {
                        *token.get_mut() += 1;
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        let counter = Arc::try_unwrap(tls).unwrap();
        assert_eq!(counter.into_iter().sum::<usize>(), THREADS * 10);
    }
}