Skip to content

Commit

Permalink
Implement external_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
rocallahan committed Apr 2, 2021
1 parent c571f8f commit e9c138a
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 47 deletions.
42 changes: 38 additions & 4 deletions rayon-core/src/latch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl<'r> Latch for SpinLatch<'r> {

/// A Latch starts as false and eventually becomes true. You can block
/// until it becomes true.
#[derive(Debug)]
pub(super) struct LockLatch {
m: Mutex<bool>,
v: Condvar,
Expand Down Expand Up @@ -297,11 +298,9 @@ impl CountLatch {

/// Decrements the latch counter by one. If this is the final
/// count, then the latch is **set**, and calls to `probe()` will
/// return true. Returns whether the latch was set. This is an
/// internal operation, as it does not tickle, and to fail to
/// tickle would lead to deadlock.
/// return true. Returns whether the latch was set.
#[inline]
fn set(&self) -> bool {
pub(super) fn set(&self) -> bool {
if self.counter.fetch_sub(1, Ordering::SeqCst) == 1 {
self.core_latch.set();
true
Expand All @@ -328,6 +327,41 @@ impl AsCoreLatch for CountLatch {
}
}

#[derive(Debug)]
pub(super) struct CountLockLatch {
lock_latch: LockLatch,
counter: AtomicUsize,
}

impl CountLockLatch {
#[inline]
pub(super) fn new() -> CountLockLatch {
CountLockLatch {
lock_latch: LockLatch::new(),
counter: AtomicUsize::new(1),
}
}

#[inline]
pub(super) fn increment(&self) {
let old_counter = self.counter.fetch_add(1, Ordering::Relaxed);
debug_assert!(old_counter != 0);
}

pub(super) fn wait(&self) {
self.lock_latch.wait();
}
}

impl Latch for CountLockLatch {
#[inline]
fn set(&self) {
if self.counter.fetch_sub(1, Ordering::SeqCst) == 1 {
self.lock_latch.set();
}
}
}

impl<'a, L> Latch for &'a L
where
L: Latch,
Expand Down
2 changes: 1 addition & 1 deletion rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mod test;

pub use self::join::{join, join_context};
pub use self::registry::ThreadBuilder;
pub use self::scope::{scope, Scope};
pub use self::scope::{external_scope, scope, Scope};
pub use self::scope::{scope_fifo, ScopeFifo};
pub use self::spawn::{spawn, spawn_fifo};
pub use self::thread_pool::current_thread_has_pending_tasks;
Expand Down
2 changes: 1 addition & 1 deletion rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static THE_REGISTRY_SET: Once = Once::new();
/// Starts the worker threads (if that has not already happened). If
/// initialization has not already occurred, use the default
/// configuration.
fn global_registry() -> &'static Arc<Registry> {
pub(super) fn global_registry() -> &'static Arc<Registry> {
set_global_registry(|| Registry::new(ThreadPoolBuilder::new()))
.or_else(|err| unsafe { THE_REGISTRY.as_ref().ok_or(err) })
.expect("The global thread pool has not been initialized.")
Expand Down
Loading

0 comments on commit e9c138a

Please sign in to comment.