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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#![feature(const_fn)]
#![feature(catch_panic)] // For testing


use std::sync::atomic::{AtomicBool, Ordering};
use std::cell::RefCell;

/// A cell holding values protected by an atomic lock.
///
/// This cell is designed to be instantiated as a global variable, to
/// hold a single value and to distribute it to threads as needed.  It
/// was initially designed to distribute clones of `Sender` to
/// hundreds of clients across dozens of modules without having to
/// pass these senders as argument through hundreds of intermediate
/// functions.
///
/// # Example
///
/// ```
/// #![feature(const_fn)]
/// use std::thread;
/// use std::sync::mpsc::{channel, Sender};
/// use std::ops::Add;
///
/// use atomic_cell::StaticCell;
///
/// // Sender cannot be defined as a `static` for two reasons:
/// // - it does not implement `Sync`;
/// // - it has a destructor.
/// // So let's implement it as a `StaticCell`.
/// static CELL: StaticCell<Sender<u32>> = StaticCell::new();
///
/// fn main() {
///   let (sender, receiver) = channel();
///   let _guard = CELL.init(sender);
///   // From this point and until `_guard` is dropped, `CELL` owns `sender`.
///
///   for i in 0..10 {
///     thread::spawn(move || {
///       // Any thread can now access clones of `sender`.
///       let sender = CELL.get().unwrap();
///       sender.send(i).unwrap();
///     });
///   }
///
///   // Make sure that all data was received properly.
///   assert_eq!(receiver.iter().take(10).fold(0, Add::add), 45);
///
///   // When we leave `main()`, `_guard` will go out of scope and
///   // will be dropped. This will cause `sender` to be dropped.
/// }
/// ```
///
///
/// # Performance
///
/// This cell is optimized for low contention. If there is no
/// contention, each call to `get` is resolved as a single
/// (sequentially consistant) atomic read. In presence of high
/// contention on a single cell, though, performance may degrade
/// considerably.
///
///
///
/// # Resource-safety
///
/// Unlike other kinds of static holders, values in `StaticCell` are
/// scoped and will be dropped, once the guard is dropped.
///
///
///
pub struct StaticCell<T> where T: Clone {
    internal: RefCell<InternalAtomicCell<T>>,
    initialized: AtomicBool
}

unsafe impl<T> Sync for StaticCell<T> where T: Clone {
}

impl<T> StaticCell<T> where T: Clone {
    ///
    /// Create an empty cell.
    ///
    /// Call `init` to fill the cell.
    ///
    pub const fn new() -> Self {
        StaticCell {
            initialized: AtomicBool::new(false),
            internal: RefCell::new(InternalAtomicCell::const_new())
        }
    }

    /// Initialize the cell.
    ///
    /// This methods returns a guard, which will drop `value` once it
    /// is dropped itself.  Once this is done, `self` will return to
    /// being an empty cell. It will, however, remain initialized and
    /// cannot ever be initialized again.
    ///
    /// # Panics
    ///
    /// This method panicks if the cell is already initialized, or if
    /// initialization is racing with a call to `get`.
    pub fn init<'a>(&'a self, value: T) -> CleanGuard<'a> {
        let initialized = self.initialized.compare_and_swap(/* must be */false,
                                                            /* becomes */true,
                                                            Ordering::SeqCst);
        if initialized {
            panic!("StaticCell is already initialized.");
        }
        {
            self.internal.borrow_mut().set(value);
        }
        return CleanGuard::new(self)
    }

    /// Get a clone of the value held by the cell.
    ///
    /// Returns `None` if the cell is empty, either because it is not
    /// initialized or because the guard has been dropped.
    ///
    /// Receiving `None` is almost always a programming error, so
    /// client code is encouraged to `unwrap()` immediately.
    ///
    ///
    /// # Performance
    ///
    /// This methods uses an atomic spinlock. With low contention, it
    /// is generally quite fast (assuming that `clone()` is itself
    /// fast). In case of high contention, performance may degrade
    /// considerably. In case of doubt, ou may wish to ensure that
    /// your code calls `clone()` before entering a
    /// perfirmance-critical or high-contention section.
    ///
    ///
    /// # Panics
    ///
    /// This method panicks if the call to `value.clone()` causes a
    /// panic.  However, the cell remains usable.
    ///
    /// This method may panick if it is racing with `init()`. Just
    /// make sure that initialization is complete before using this
    /// cell, right?
    pub fn get(&self) -> Option<Box<T>> {
        self.internal.borrow().get()
    }
}

impl<T> CleanMeUp for StaticCell<T> where T: Clone {
    /// Drop the value currently held by the cell, if any.
    ///
    /// This method is called when the `CleanGuard` is dropped.
    fn clean(&self) {
        self.internal.borrow_mut().unset();
    }
}

/// An object that needs to be cleaned up.
pub trait CleanMeUp {
    /// Perform cleanup.
    fn clean(&self);
}


/// A guard used to drop the value held by a `CleanMeUp` at a
/// deterministic point in code. This is designed as an alternative to
/// `Drop` for global variables.
///
/// Once the `CleanGuard`, the value held by the cell is also dropped.
pub struct CleanGuard<'a> {
    item: &'a CleanMeUp
}

impl<'a> CleanGuard<'a> {
    /// Create a new guard in charge of cleaning up an object.
    ///
    /// Once the CleanGuard is dropped, the object's `clean` method is
    /// called.
    pub fn new(item: &'a CleanMeUp) -> Self {
        CleanGuard {
            item: item
        }
    }
}


impl<'a> Drop for CleanGuard<'a> {
    ///
    /// Call the guarded object's `clean` method.
    ///
    fn drop(&mut self) {
        self.item.clean();
    }
}

/// A cell holding values protected by an atomic lock.
///
/// This cell is designed to be instantiated as a local variable, to
/// hold a single value and to distribute it to threads as needed.
///
/// This cell cannot be allocated as a global variable. If you need a
/// global variable, use `StaticAtomicCell`.
///
///
/// # Performance
///
/// This cell is optimized for low contention. If there is no
/// contention, each call to `get` is resolved as a single
/// (sequentially consistent) atomic read. In presence of high
/// contention on a single cell, though, performance may degrade
/// considerably.
///
pub struct AtomicCell<T> where T: Clone {
    internal: InternalAtomicCell<T>
}

impl<T> AtomicCell<T> where T: Clone {
    ///
    /// Create a new empty cell.
    ///
    /// Use `set` or `swap` to add contents.
    ///
    pub fn new() -> Self {
        AtomicCell {
            internal: InternalAtomicCell::new()
        }
    }

    ///
    /// Set the contents of the cell.
    ///
    /// `value` will be dropped either when the cell is dropped, or
    /// when `set` is called once again. Property of `value` is
    /// transferred to the client if `swap` is called.
    ///
    /// If the cell already held some contents, drop these contents.
    ///
    pub fn set(&mut self, value: T) {
        self.internal.set(value)
    }

    /// Get a clone of the value held by the cell.
    ///
    /// Returns `None` if the cell is empty.
    ///
    /// # Panics
    ///
    /// A panic during the call to `value.clone()` will propagate and
    /// cause a panic in the cell.  However, the cell will remain
    /// usable.
    pub fn get(&self) -> Option<Box<T>> {
        self.internal.get()
    }

    ///
    /// Empty the cell manually.
    ///
    /// If the cell was empty, this is a noop. Otherwise, the previous
    /// value held is dropped.
    ///
    pub fn unset(&mut self) {
        self.internal.unset()
    }

    ///
    /// Swap the value held by the cell with a new value.
    ///
    pub fn swap(&mut self, value: Option<Box<T>>) -> Option<Box<T>> {
        self.internal.swap(value)
    }
}

impl<T> Drop for AtomicCell<T> where T: Clone {
    ///
    /// Drop any content present in the cell.
    ///
    fn drop(&mut self) {
        self.unset();
    }
}

/// A cell holding values protected by an atomic lock.
///
/// This cell is designed to hold values that implement `Clone` and to
/// distribute them to threads as needed. It may be instantiated as
/// a `static` value.
///
/// This cell is optimized for low contention.
///
/// This version does NOT guarantee that the values it holds are
/// dropped.
///
impl<T> InternalAtomicCell<T> where T: Clone {
    ///
    /// Create an empty cell.
    ///
    pub const fn const_new() -> Self {
        InternalAtomicCell {
            ptr: std::ptr::null_mut(),
            lock: AtomicBool::new(true),
        }
    }
    pub fn new() -> Self {
        InternalAtomicCell {
            ptr: std::ptr::null_mut(),
            lock: AtomicBool::new(true),
        }
    }

    ///
    /// Put a value in the cell.
    ///
    /// If there was a previous value, it is dropped.
    ///
    /// Argument `value` will **not** be dropped automatically. You should
    /// call `set`, `unset` or `swap` to ensure that it is dropped.
    ///
    /// See also `swap`.
    ///
    /// # Performance
    ///
    /// This method requires acquiring an atomic lock. If contention
    /// is low, it should be very fast. However, in case of high
    /// contention, performance may degrade considerably.
    ///
    pub fn set(&mut self, value: T) {
        self.swap(Some(Box::new(value)));
    }

    ///
    /// Remove whichever value is in the cell.
    ///
    /// If there was a previous value, it is dropped.
    ///
    /// Argument `value` will **not** be dropped automatically. You should
    /// call `set`, `unset` or `swap` to ensure that it is dropped.
    ///
    /// # Performance
    ///
    /// This method requires acquiring an atomic lock. If contention
    /// is low, it should be very fast. However, in case of high
    /// contention, performance may degrade considerably.
    ///
    pub fn unset(&mut self) {
        self.swap(None);
    }

    ///
    /// Get a clone of the current value in the cell.
    ///
    /// # Performance
    ///
    /// This method requires acquiring an atomic lock. If contention
    /// is low, it should be very fast. However, in case of high
    /// contention, performance may degrade considerably.
    ///
    /// # Panics
    ///
    /// If the `clone` method panics, this will cause a panic. The cell
    /// will, however, remain usable.
    ///
    pub fn get(&self) -> Option<Box<T>> {
        let maybe_clone = self.with_lock(|| {
            if self.ptr.is_null() {
                return None;
            }

            // Don't cast back to Box, as this would cause us to
            // `drop` the value in case of panic.
            Some(unsafe { (*self.ptr).clone() })
            // If `clone` panics, `with_lock` will ensure that the
            // lock is released.
        });
        // Allocate out of the lock.
        match maybe_clone {
            None => None,
            Some(clone) => Some(Box::new(clone))
        }
    }

    ///
    /// Replace the value currently in the cell with another value.
    ///
    /// # Performance
    ///
    /// This method requires acquiring an atomic lock. If contention
    /// is low, it should be very fast. However, in case of high
    /// contention, performance may degrade considerably.
    ///
    pub fn swap(&mut self, value: Option<Box<T>>) -> Option<Box<T>> {
        let new_ptr = ptr_from_opt(value);
        // We are now the owner of `value` as a pointer. From this
        // point, we are in charge of dropping it manually.
        let old_ptr = self.with_lock_mut(|mut ptr : &mut*mut T| {
            let old_ptr = *ptr;
            *ptr = new_ptr;
            old_ptr
        });
        opt_from_ptr(old_ptr)
    }

    ///
    /// Acquire the lock.
    ///
    /// We use an atomic spinlock.
    ///
    /// # Panics
    ///
    /// If `f` panics.
    ///
    fn with_lock<F, R>(&self, f: F) -> R where F: FnOnce() -> R {
        loop {
            // Attempt to acquire the lock.
            // This data structure is designed for low contention, so we can use
            // an atomic spinlock.
            let owning = self.lock.compare_and_swap(/*must be available*/true,
                                                    /*mark as unavailable*/false,
                                                    Ordering::SeqCst);
            if owning {
                // We are now the owner of the lock.

                // Make sure that we eventually release the lock.
                let _guard = GuardLock::new(&self.lock);

                let result = f();

                return result;
            }
        }
    }
    fn with_lock_mut<F, R>(&mut self, f: F) -> R where F: FnOnce(&mut*mut T) -> R {
        loop {
            // Attempt to acquire the lock.
            // This data structure is designed for low contention, so we can use
            // an atomic spinlock.
            let owning = self.lock.compare_and_swap(/*must be available*/true,
                                                    /*mark as unavailable*/false,
                                                    Ordering::SeqCst);
            if owning {
                // We are now the owner of the lock.

                // Make sure that we eventually release the lock.
                let _guard = GuardLock::new(&self.lock);

                let result = f(&mut self.ptr);

                return result;
            }
        }
    }

}

fn ptr_from_opt<T>(value: Option<Box<T>>) -> *mut T {
    match value {
        None => std::ptr::null_mut(),
        Some(b) => Box::into_raw(b)
    }
}

fn opt_from_ptr<T>(ptr: *mut T) -> Option<Box<T>> {
    if ptr.is_null() {
        None
    } else {
        unsafe { Some(Box::from_raw(ptr)) }
    }
}

struct InternalAtomicCell<T> where T: Clone {
    ///
    /// Pointer to the value held by the cell.
    ///
    /// This pointer may be `null`.
    ///
    ptr: *mut T,

    /// An atomic bool supporting a spinlock.
    ///
    /// If `true`, the lock can be acquired. If `false`, the lock
    /// is not available.
    lock: AtomicBool,
}

unsafe impl<T> Sync for InternalAtomicCell<T> where T: Clone + Send {
}

/// A guard used to ensure that we release a lock, even in case of
/// panic.
struct GuardLock<'a> {
    lock: &'a AtomicBool
}
impl<'a> GuardLock<'a> {
    fn new(lock: &AtomicBool) -> GuardLock {
        GuardLock {
            lock: lock
        }
    }
}
impl<'a> Drop for GuardLock<'a> {
    fn drop(&mut self) {
        self.lock.swap(true, Ordering::Relaxed);
    }
}

#[cfg(test)]
mod test {
    use super::*;


    use std::ops::Add;
    use std::sync::mpsc::{channel, Sender};
    use std::thread;
    use std::sync::atomic::{AtomicBool, Ordering};


    // Test that we can allocate a cell and use it to distribute value
    // to several threads.
    static CELL: StaticCell<Sender<u32>> = StaticCell::new();
    #[test]
    fn test_channels() {
        let (tx, rx) = channel();
        let _guard = CELL.init(tx);
        for i in 0..10 {
            thread::spawn(move || {
                let tx = CELL.get().unwrap();
                tx.send(i).unwrap();
            });
        }
        assert_eq!(rx.iter().take(10).fold(0, Add::add), 45);
    }

    // Test that `get()` on an empty cell returns `None`.
    static CELL2: StaticCell<u32> = StaticCell::new();
    #[test]
    fn test_empty() {
        for _ in 0..10 {
            thread::spawn(move || {
                assert_eq!(CELL2.get(), None);
            });
        }
        assert_eq!(CELL2.get(), None);
    }

    // Test that a panic does not make the cell unusable.
    static mut should_panic: AtomicBool = AtomicBool::new(false);
    struct Panicky {
        foo: u32
    }
    impl Clone for Panicky {
        fn clone(&self) -> Self {
            unsafe {
                if should_panic.load(Ordering::Relaxed) {
                    panic!("I have panicked, as expected");
                }
            }
            Panicky {
                foo: self.foo
            }
        }
    }

    static CELL_PANIC: StaticCell<Panicky> = StaticCell::new();
    #[test]
    fn test_panic() {
        let original = Panicky { foo: 500 };
        let _guard = CELL_PANIC.init(original.clone());

        // Now, cause a panic.
        unsafe {
            should_panic.swap(true, Ordering::Relaxed);
        }
        let panic = thread::catch_panic(|| {
            // This should panic.
            CELL_PANIC.get()
        });
        assert!(panic.is_err());

        // Now stop the panic.
        unsafe {
            should_panic.swap(false, Ordering::Relaxed);
        }

        // This shouldn't panic. Moreover, we should find
        // our original value.
        let result = CELL_PANIC.get().unwrap();
        assert_eq!(result.foo, original.foo);
    }

    static CELL_INIT: StaticCell<u32> = StaticCell::new();
    #[test]
    fn test_init() {
        {
            let mut _guard = CELL_INIT.init(0);
            CELL_INIT.get().unwrap();
        }
        assert_eq!(CELL_INIT.get(), None);
    }
}