cranpose_core/
process_ids.rs1#[cfg(target_has_atomic = "64")]
4use std::sync::atomic::{AtomicU64, Ordering};
5#[cfg(not(target_has_atomic = "64"))]
6use std::sync::{Mutex, PoisonError};
7
8pub struct ProcessIds {
15 #[cfg(target_has_atomic = "64")]
16 next: AtomicU64,
17 #[cfg(not(target_has_atomic = "64"))]
18 next: Mutex<u64>,
19}
20
21impl ProcessIds {
22 pub const fn starting_at(first: u64) -> Self {
24 Self {
25 #[cfg(target_has_atomic = "64")]
26 next: AtomicU64::new(first),
27 #[cfg(not(target_has_atomic = "64"))]
28 next: Mutex::new(first),
29 }
30 }
31
32 pub fn next(&self) -> u64 {
34 #[cfg(target_has_atomic = "64")]
35 {
36 self.next.fetch_add(1, Ordering::Relaxed)
37 }
38 #[cfg(not(target_has_atomic = "64"))]
39 {
40 let mut next = self.next.lock().unwrap_or_else(PoisonError::into_inner);
41 let id = *next;
42 *next += 1;
43 id
44 }
45 }
46}