@@ -157,7 +157,7 @@ impl Inner {
157
157
///
158
158
/// ```no_run
159
159
/// use fork_union as fu;
160
- /// let pool = fu::spawn(4); // ! Unsafe shortcut, see below
160
+ /// let mut pool = fu::spawn(4); // ! Unsafe shortcut, see below
161
161
/// pool.broadcast(|thread_index| {
162
162
/// println!("Hello from thread {thread_index}!");
163
163
/// });
@@ -175,8 +175,8 @@ impl Inner {
175
175
/// fn heavy_math(_: usize) {}
176
176
///
177
177
/// fn main() -> Result<(), Box<dyn Error>> {
178
- /// let pool = fu::ThreadPool::try_spawn_in(4, Global)?;
179
- /// fu::for_n_dynamic(&pool, 400, |prong| {
178
+ /// let mut pool = fu::ThreadPool::try_spawn_in(4, Global)?;
179
+ /// fu::for_n_dynamic(&mut pool, 400, |prong| {
180
180
/// heavy_math(prong.task_index);
181
181
/// });
182
182
/// Ok(())
@@ -260,7 +260,7 @@ impl<A: Allocator + Clone> ThreadPool<A> {
260
260
}
261
261
262
262
/// Executes a function on each thread of the pool.
263
- pub fn broadcast < F > ( & self , function : F )
263
+ pub fn broadcast < F > ( & mut self , function : F )
264
264
where
265
265
F : Fn ( usize ) + Sync ,
266
266
{
@@ -358,7 +358,7 @@ pub struct Prong {
358
358
}
359
359
360
360
/// Distributes `n` similar duration calls between threads in slices.
361
- pub fn for_slices < A , F > ( pool : & ThreadPool < A > , n : usize , function : F )
361
+ pub fn for_slices < A , F > ( pool : & mut ThreadPool < A > , n : usize , function : F )
362
362
where
363
363
A : Allocator + Clone ,
364
364
F : Fn ( Prong , usize ) + Sync ,
@@ -403,7 +403,7 @@ where
403
403
}
404
404
405
405
/// Distributes `n` similar duration calls between threads by individual indices.
406
- pub fn for_n < A , F > ( pool : & ThreadPool < A > , n : usize , function : F )
406
+ pub fn for_n < A , F > ( pool : & mut ThreadPool < A > , n : usize , function : F )
407
407
where
408
408
A : Allocator + Clone ,
409
409
F : Fn ( Prong ) + Sync ,
@@ -419,7 +419,7 @@ where
419
419
}
420
420
421
421
/// Executes `n` uneven tasks on all threads, greedily stealing work.
422
- pub fn for_n_dynamic < A , F > ( pool : & ThreadPool < A > , n : usize , function : F )
422
+ pub fn for_n_dynamic < A , F > ( pool : & mut ThreadPool < A > , n : usize , function : F )
423
423
where
424
424
A : Allocator + Clone ,
425
425
F : Fn ( Prong ) + Sync ,
@@ -540,9 +540,9 @@ impl<F> SyncConstPtr<F> {
540
540
///
541
541
/// ```no_run
542
542
/// use fork_union as fu;
543
- /// let pool = fu::spawn(1);
543
+ /// let mut pool = fu::spawn(1);
544
544
/// let mut data = vec![0u64; 1_000_000];
545
- /// fu::for_each_prong_mut(&pool, &mut data, |x, prong| {
545
+ /// fu::for_each_prong_mut(&mut pool, &mut data, |x, prong| {
546
546
/// *x = prong.task_index as u64 * 2;
547
547
/// });
548
548
/// ```
@@ -551,7 +551,7 @@ impl<F> SyncConstPtr<F> {
551
551
/// set of elements in parallel, so this API serves as a shortcut.
552
552
///
553
553
/// Similar to Rayon's `par_chunks_mut`.
554
- pub fn for_each_prong_mut < A , T , F > ( pool : & ThreadPool < A > , data : & mut [ T ] , function : F )
554
+ pub fn for_each_prong_mut < A , T , F > ( pool : & mut ThreadPool < A > , data : & mut [ T ] , function : F )
555
555
where
556
556
A : Allocator + Clone ,
557
557
T : Send ,
@@ -576,9 +576,9 @@ where
576
576
///
577
577
/// ```no_run
578
578
/// use fork_union as fu;
579
- /// let pool = fu::spawn(1);
579
+ /// let mut pool = fu::spawn(1);
580
580
/// let mut strings = vec![String::new(); 1_000];
581
- /// fu::for_each_prong_mut_dynamic(&pool, &mut strings, |s, prong| {
581
+ /// fu::for_each_prong_mut_dynamic(&mut pool, &mut strings, |s, prong| {
582
582
/// s.push_str(&format!("hello from thread {}", prong.thread_index));
583
583
/// });
584
584
/// ```
@@ -587,7 +587,7 @@ where
587
587
/// set of elements in parallel, so this API serves as a shortcut.
588
588
///
589
589
/// Similar to Rayon's `par_iter_mut`.
590
- pub fn for_each_prong_mut_dynamic < A , T , F > ( pool : & ThreadPool < A > , data : & mut [ T ] , function : F )
590
+ pub fn for_each_prong_mut_dynamic < A , T , F > ( pool : & mut ThreadPool < A > , data : & mut [ T ] , function : F )
591
591
where
592
592
A : Allocator + Clone ,
593
593
T : Send ,
@@ -633,7 +633,7 @@ mod tests {
633
633
#[ test]
634
634
fn for_each_thread_dispatch ( ) {
635
635
let count_threads = hw_threads ( ) ;
636
- let pool = spawn ( count_threads) ;
636
+ let mut pool = spawn ( count_threads) ;
637
637
638
638
let visited = Arc :: new (
639
639
( 0 ..count_threads)
@@ -657,11 +657,11 @@ mod tests {
657
657
#[ test]
658
658
fn for_each_static_uncomfortable_input_size ( ) {
659
659
let count_threads = hw_threads ( ) ;
660
- let pool = spawn ( count_threads) ;
660
+ let mut pool = spawn ( count_threads) ;
661
661
662
662
for input_size in 0 ..count_threads {
663
663
let out_of_bounds = AtomicBool :: new ( false ) ;
664
- for_n ( & pool, input_size, |prong| {
664
+ for_n ( & mut pool, input_size, |prong| {
665
665
let task_index = prong. task_index ;
666
666
if task_index >= count_threads {
667
667
out_of_bounds. store ( true , Ordering :: Relaxed ) ;
@@ -677,7 +677,7 @@ mod tests {
677
677
#[ test]
678
678
fn for_each_static_static_scheduling ( ) {
679
679
const EXPECTED_PARTS : usize = 10_000_000 ;
680
- let pool = spawn ( hw_threads ( ) ) ;
680
+ let mut pool = spawn ( hw_threads ( ) ) ;
681
681
682
682
let visited = Arc :: new (
683
683
( 0 ..EXPECTED_PARTS )
@@ -688,7 +688,7 @@ mod tests {
688
688
let visited_ref = Arc :: clone ( & visited) ;
689
689
let duplicate_ref = Arc :: clone ( & duplicate) ;
690
690
691
- for_n ( & pool, EXPECTED_PARTS , move |prong| {
691
+ for_n ( & mut pool, EXPECTED_PARTS , move |prong| {
692
692
let task_index = prong. task_index ;
693
693
if visited_ref[ task_index] . swap ( true , Ordering :: Relaxed ) {
694
694
duplicate_ref. store ( true , Ordering :: Relaxed ) ;
@@ -707,7 +707,7 @@ mod tests {
707
707
#[ test]
708
708
fn for_each_dynamic_dynamic_scheduling ( ) {
709
709
const EXPECTED_PARTS : usize = 10_000_000 ;
710
- let pool = spawn ( hw_threads ( ) ) ;
710
+ let mut pool = spawn ( hw_threads ( ) ) ;
711
711
712
712
let visited = Arc :: new (
713
713
( 0 ..EXPECTED_PARTS )
@@ -718,7 +718,7 @@ mod tests {
718
718
let visited_ref = Arc :: clone ( & visited) ;
719
719
let duplicate_ref = Arc :: clone ( & duplicate) ;
720
720
721
- for_n_dynamic ( & pool, EXPECTED_PARTS , move |prong| {
721
+ for_n_dynamic ( & mut pool, EXPECTED_PARTS , move |prong| {
722
722
let task_index = prong. task_index ;
723
723
if visited_ref[ task_index] . swap ( true , Ordering :: Relaxed ) {
724
724
duplicate_ref. store ( true , Ordering :: Relaxed ) ;
@@ -739,7 +739,7 @@ mod tests {
739
739
const EXPECTED_PARTS : usize = 10_000_000 ;
740
740
const OVERSUBSCRIPTION : usize = 7 ;
741
741
let threads = hw_threads ( ) * OVERSUBSCRIPTION ;
742
- let pool = spawn ( threads) ;
742
+ let mut pool = spawn ( threads) ;
743
743
744
744
let visited = Arc :: new (
745
745
( 0 ..EXPECTED_PARTS )
@@ -752,7 +752,7 @@ mod tests {
752
752
753
753
thread_local ! { static LOCAL_WORK : std:: cell:: Cell <usize > = std:: cell:: Cell :: new( 0 ) ; }
754
754
755
- for_n_dynamic ( & pool, EXPECTED_PARTS , move |prong| {
755
+ for_n_dynamic ( & mut pool, EXPECTED_PARTS , move |prong| {
756
756
let task_index = prong. task_index ;
757
757
// Mildly unbalanced CPU burn
758
758
LOCAL_WORK . with ( |cell| {
@@ -787,8 +787,8 @@ mod tests {
787
787
}
788
788
789
789
TASK_COUNTER . store ( 0 , Ordering :: Relaxed ) ;
790
- let pool = spawn ( hw_threads ( ) ) ;
791
- for_n_dynamic ( & pool, EXPECTED_PARTS , |prong| tally ( prong. task_index ) ) ;
790
+ let mut pool = spawn ( hw_threads ( ) ) ;
791
+ for_n_dynamic ( & mut pool, EXPECTED_PARTS , |prong| tally ( prong. task_index ) ) ;
792
792
793
793
assert_eq ! (
794
794
TASK_COUNTER . load( Ordering :: Relaxed ) ,
@@ -801,7 +801,7 @@ mod tests {
801
801
fn concurrent_histogram_array ( ) {
802
802
const HIST_SIZE : usize = 16 ;
803
803
const ELEMENTS : usize = 1_000_000 ;
804
- let pool = spawn ( hw_threads ( ) ) ;
804
+ let mut pool = spawn ( hw_threads ( ) ) ;
805
805
806
806
let values: Vec < usize > = ( 0 ..ELEMENTS ) . map ( |i| i % HIST_SIZE ) . collect ( ) ;
807
807
let histogram = Arc :: new (
@@ -811,7 +811,7 @@ mod tests {
811
811
) ;
812
812
let hist_ref = Arc :: clone ( & histogram) ;
813
813
814
- for_n_dynamic ( & pool, ELEMENTS , |prong| {
814
+ for_n_dynamic ( & mut pool, ELEMENTS , |prong| {
815
815
let task_index = prong. task_index ;
816
816
let value = values[ task_index] ;
817
817
hist_ref[ value] . fetch_add ( 1 , Ordering :: Relaxed ) ;
@@ -826,7 +826,7 @@ mod tests {
826
826
}
827
827
}
828
828
829
- fn increment_all ( pool : & ThreadPool , data : & [ AtomicUsize ] ) {
829
+ fn increment_all ( pool : & mut ThreadPool , data : & [ AtomicUsize ] ) {
830
830
for_n ( pool, data. len ( ) , |prong| {
831
831
data[ prong. task_index ] . fetch_add ( 1 , Ordering :: Relaxed ) ;
832
832
} ) ;
@@ -835,14 +835,14 @@ mod tests {
835
835
#[ test]
836
836
fn pass_pool_and_reuse ( ) {
837
837
const ELEMENTS : usize = 128 ;
838
- let pool = spawn ( hw_threads ( ) ) ;
838
+ let mut pool = spawn ( hw_threads ( ) ) ;
839
839
840
840
let data = ( 0 ..ELEMENTS )
841
841
. map ( |_| AtomicUsize :: new ( 0 ) )
842
842
. collect :: < Vec < _ > > ( ) ;
843
843
844
- increment_all ( & pool, & data) ;
845
- increment_all ( & pool, & data) ;
844
+ increment_all ( & mut pool, & data) ;
845
+ increment_all ( & mut pool, & data) ;
846
846
847
847
for counter in data. iter ( ) {
848
848
assert_eq ! ( counter. load( Ordering :: Relaxed ) , 2 ) ;
@@ -854,7 +854,7 @@ mod tests {
854
854
let mut pool = spawn ( hw_threads ( ) ) ;
855
855
static COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
856
856
857
- for_n ( & pool, 1000 , |_| {
857
+ for_n ( & mut pool, 1000 , |_| {
858
858
COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
859
859
} ) ;
860
860
@@ -968,7 +968,7 @@ mod tests {
968
968
) ;
969
969
970
970
let large_allocator = CountingAllocator :: new ( Some ( 1024 * 1024 ) ) ;
971
- let pool = ThreadPool :: try_spawn_in ( hw_threads ( ) , large_allocator. clone ( ) )
971
+ let mut pool = ThreadPool :: try_spawn_in ( hw_threads ( ) , large_allocator. clone ( ) )
972
972
. expect ( "We should have enough memory for this!" ) ;
973
973
974
974
let visited = Arc :: new (
@@ -980,7 +980,7 @@ mod tests {
980
980
let visited_ref = Arc :: clone ( & visited) ;
981
981
let duplicate_ref = Arc :: clone ( & duplicate) ;
982
982
983
- for_n_dynamic ( & pool, EXPECTED_PARTS , move |prong| {
983
+ for_n_dynamic ( & mut pool, EXPECTED_PARTS , move |prong| {
984
984
let task_index = prong. task_index ;
985
985
if visited_ref[ task_index] . swap ( true , Ordering :: Relaxed ) {
986
986
duplicate_ref. store ( true , Ordering :: Relaxed ) ;
0 commit comments