Skip to content

Commit

Permalink
Merge e832902 into dc36fc4
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 5, 2022
2 parents dc36fc4 + e832902 commit dba7e77
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ any_key = "0.1.1"
anyhow = "1.0.47"
auto-hash-map = { path = "../auto-hash-map" }
bitflags = "1.3.2"
concurrent-queue = "1.2.2"
dashmap = "5.4.0"
erased-serde = "0.3.20"
event-listener = "2.5.3"
Expand Down
14 changes: 10 additions & 4 deletions crates/turbo-tasks/src/id_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::{
sync::atomic::{AtomicUsize, Ordering},
};

use concurrent_queue::ConcurrentQueue;
use once_cell::sync::Lazy;

pub struct IdFactory<T> {
next_id: AtomicUsize,
// TODO
// free_ids: AtomicPtr<...>
free_ids: Lazy<ConcurrentQueue<T>>,
phantom_data: PhantomData<T>,
}

Expand All @@ -21,18 +23,22 @@ impl<T: From<usize> + Deref<Target = usize>> IdFactory<T> {
pub const fn new() -> Self {
Self {
next_id: AtomicUsize::new(1),
free_ids: Lazy::new(|| ConcurrentQueue::unbounded()),
phantom_data: PhantomData,
}
}

pub fn get(&self) -> T {
if let Ok(id) = self.free_ids.pop() {
return id;
}
self.next_id.fetch_add(1, Ordering::Relaxed).into()
}

/// # Safety
///
/// It must be ensured that the id is no longer used
pub unsafe fn reuse(&self, _id: T) {
// TODO
pub unsafe fn reuse(&self, id: T) {
let _ = self.free_ids.push(id);
}
}

0 comments on commit dba7e77

Please sign in to comment.