Skip to content

Commit

Permalink
Be more explicit with Arc::clone
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed May 11, 2022
1 parent baa4057 commit d1a0a60
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
18 changes: 9 additions & 9 deletions rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Registry {
let thread = ThreadBuilder {
name: builder.get_thread_name(index),
stack_size: builder.get_stack_size(),
registry: registry.clone(),
registry: Arc::clone(&registry),
worker,
index,
};
Expand All @@ -263,17 +263,18 @@ impl Registry {
// Returning normally now, without termination.
mem::forget(t1000);

Ok(registry.clone())
Ok(registry)
}

pub(super) fn current() -> Arc<Registry> {
unsafe {
let worker_thread = WorkerThread::current();
if worker_thread.is_null() {
global_registry().clone()
let registry = if worker_thread.is_null() {
global_registry()
} else {
(*worker_thread).registry.clone()
}
&(*worker_thread).registry
};
Arc::clone(registry)
}
}

Expand Down Expand Up @@ -804,9 +805,10 @@ unsafe fn main_loop(worker: Worker<JobRef>, registry: Arc<Registry>, index: usiz
fifo: JobFifo::new(),
index,
rng: XorShift64Star::new(),
registry: registry.clone(),
registry,
};
WorkerThread::set_current(worker_thread);
let registry = &*worker_thread.registry;

// let registry know we are ready to do work
registry.thread_infos[index].primed.set();
Expand All @@ -818,7 +820,6 @@ unsafe fn main_loop(worker: Worker<JobRef>, registry: Arc<Registry>, index: usiz

// Inform a user callback that we started a thread.
if let Some(ref handler) = registry.start_handler {
let registry = registry.clone();
match unwind::halt_unwinding(|| handler(index)) {
Ok(()) => {}
Err(err) => {
Expand Down Expand Up @@ -847,7 +848,6 @@ unsafe fn main_loop(worker: Worker<JobRef>, registry: Arc<Registry>, index: usiz

// Inform a user callback that we exited a thread.
if let Some(ref handler) = registry.exit_handler {
let registry = registry.clone();
match unwind::halt_unwinding(|| handler(index)) {
Ok(()) => {}
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion rayon-core/src/spawn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
registry.increment_terminate_count();

Box::new(HeapJob::new({
let registry = registry.clone();
let registry = Arc::clone(registry);
move || {
match unwind::halt_unwinding(func) {
Ok(()) => {}
Expand Down
14 changes: 7 additions & 7 deletions rayon-core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn start_callback_called() {
// Wait for all the threads in the pool plus the one running tests.
let barrier = Arc::new(Barrier::new(n_threads + 1));

let b = barrier.clone();
let nc = n_called.clone();
let b = Arc::clone(&barrier);
let nc = Arc::clone(&n_called);
let start_handler = move |_| {
nc.fetch_add(1, Ordering::SeqCst);
b.wait();
Expand All @@ -48,8 +48,8 @@ fn exit_callback_called() {
// Wait for all the threads in the pool plus the one running tests.
let barrier = Arc::new(Barrier::new(n_threads + 1));

let b = barrier.clone();
let nc = n_called.clone();
let b = Arc::clone(&barrier);
let nc = Arc::clone(&n_called);
let exit_handler = move |_| {
nc.fetch_add(1, Ordering::SeqCst);
b.wait();
Expand Down Expand Up @@ -85,9 +85,9 @@ fn handler_panics_handled_correctly() {
panic!("ensure panic handler is called when exiting");
};

let sb = start_barrier.clone();
let eb = exit_barrier.clone();
let nc = n_called.clone();
let sb = Arc::clone(&start_barrier);
let eb = Arc::clone(&exit_barrier);
let nc = Arc::clone(&n_called);
let panic_handler = move |_| {
let val = nc.fetch_add(1, Ordering::SeqCst);
if val < n_threads {
Expand Down
6 changes: 3 additions & 3 deletions rayon-core/src/thread_pool/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn workers_stop() {
// do some work on these threads
join_a_lot(22);

thread_pool.registry.clone()
Arc::clone(&thread_pool.registry)
});
assert_eq!(registry.num_threads(), 22);
}
Expand All @@ -53,7 +53,7 @@ fn sleeper_stop() {
{
// once we exit this block, thread-pool will be dropped
let thread_pool = ThreadPoolBuilder::new().num_threads(22).build().unwrap();
registry = thread_pool.registry.clone();
registry = Arc::clone(&thread_pool.registry);

// Give time for at least some of the thread pool to fall asleep.
thread::sleep(time::Duration::from_secs(1));
Expand All @@ -67,7 +67,7 @@ fn sleeper_stop() {
/// Creates a start/exit handler that increments an atomic counter.
fn count_handler() -> (Arc<AtomicUsize>, impl Fn(usize)) {
let count = Arc::new(AtomicUsize::new(0));
(count.clone(), move |_| {
(Arc::clone(&count), move |_| {
count.fetch_add(1, Ordering::SeqCst);
})
}
Expand Down

0 comments on commit d1a0a60

Please sign in to comment.