]> git.proxmox.com Git - cargo.git/blob - vendor/crossbeam-0.3.0/src/epoch/local.rs
New upstream version 0.23.0
[cargo.git] / vendor / crossbeam-0.3.0 / src / epoch / local.rs
1 // Manage the thread-local state, providing access to a `Participant` record.
2
3 use std::sync::atomic::Ordering::Relaxed;
4
5 use epoch::participant::Participant;
6 use epoch::global;
7
8 #[derive(Debug)]
9 struct LocalEpoch {
10 participant: *const Participant,
11 }
12
13 impl LocalEpoch {
14 fn new() -> LocalEpoch {
15 LocalEpoch { participant: global::get().participants.enroll() }
16 }
17
18 fn get(&self) -> &Participant {
19 unsafe { &*self.participant }
20 }
21 }
22
23 // FIXME: avoid leaking when all threads have exited
24 impl Drop for LocalEpoch {
25 fn drop(&mut self) {
26 let p = self.get();
27 p.enter();
28 p.migrate_garbage();
29 p.exit();
30 p.active.store(false, Relaxed);
31 }
32 }
33
34 thread_local!(static LOCAL_EPOCH: LocalEpoch = LocalEpoch::new() );
35
36 pub fn with_participant<F, T>(f: F) -> T where F: FnOnce(&Participant) -> T {
37 LOCAL_EPOCH.with(|e| f(e.get()))
38 }