@@ -53,37 +53,3 @@ pub mod guard { |
|
|
53 |
53 |
None |
54 |
54 |
} |
55 |
55 |
} |
56 |
|
- |
57 |
|
-// We currently just use our own thread-local to store our |
58 |
|
-// current thread's ID, and then we lazily initialize it to something allocated |
59 |
|
-// from a global counter. |
60 |
|
-pub fn my_id() -> u32 { |
61 |
|
-use crate::sync::atomic::{AtomicU32, Ordering::SeqCst}; |
62 |
|
- |
63 |
|
-static NEXT_ID: AtomicU32 = AtomicU32::new(0); |
64 |
|
- |
65 |
|
-#[thread_local] |
66 |
|
-static mut MY_ID: u32 = 0; |
67 |
|
- |
68 |
|
-unsafe { |
69 |
|
-// If our thread ID isn't set yet then we need to allocate one. Do so |
70 |
|
-// with with a simple "atomically add to a global counter" strategy. |
71 |
|
-// This strategy doesn't handled what happens when the counter |
72 |
|
-// overflows, however, so just abort everything once the counter |
73 |
|
-// overflows and eventually we could have some sort of recycling scheme |
74 |
|
-// (or maybe this is all totally irrelevant by that point!). In any case |
75 |
|
-// though we're using a CAS loop instead of a `fetch_add` to ensure that |
76 |
|
-// the global counter never overflows. |
77 |
|
-if MY_ID == 0 { |
78 |
|
-let mut cur = NEXT_ID.load(SeqCst); |
79 |
|
-MY_ID = loop { |
80 |
|
-let next = cur.checked_add(1).unwrap_or_else(| |
81 |
|
-match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) { |
82 |
|
-Ok(_) => break next, |
83 |
|
-Err(i) => cur = i, |
84 |
|
-} |
85 |
|
-}; |
86 |
|
-} |
87 |
|
-MY_ID |
88 |
|
-} |
89 |
|
-} |