File tree 2 files changed +18
-12
lines changed
2 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -160,16 +160,18 @@ impl Threadpool {
160
160
if let Some ( stack_size) = data. stack_size {
161
161
builder = builder. stack_size ( stack_size) ;
162
162
}
163
+ // Increase the thread count counter
164
+ data. thread_count . fetch_add ( 1 , Ordering :: SeqCst ) ;
165
+ // Create a new sentry watcher
166
+ let sentry = Sentry :: new ( coreid, Arc :: downgrade ( & data) ) ;
167
+ // Clone receiver
168
+ let receiver = data. receiver . clone ( ) ;
163
169
// Spawn a new worker thread
164
170
let _ = builder. spawn ( move || {
165
- // Create a new sentry watcher
166
- let sentry = Sentry :: new ( coreid, & data) ;
167
- // Increase the thread count counter
168
- data. thread_count . fetch_add ( 1 , Ordering :: SeqCst ) ;
169
171
// Loop continuously, processing any jobs
170
172
loop {
171
173
// Pull a message from the job channel
172
- let job = match data . receiver . recv_blocking ( ) {
174
+ let job = match receiver. recv_blocking ( ) {
173
175
// We received a job to process
174
176
Ok ( job) => job,
175
177
// This threadpool was dropped
Original file line number Diff line number Diff line change 1
1
use crate :: Data ;
2
2
use crate :: Threadpool ;
3
- use std:: sync:: Arc ;
3
+ use std:: sync:: Weak ;
4
4
5
- pub ( crate ) struct Sentry < ' a > {
5
+ pub ( crate ) struct Sentry {
6
6
active : bool ,
7
7
coreid : Option < usize > ,
8
- data : & ' a Arc < Data > ,
8
+ data : Weak < Data > ,
9
9
}
10
10
11
- impl < ' a > Sentry < ' a > {
11
+ impl Sentry {
12
12
/// Create a new sentry tracker
13
- pub fn new ( coreid : Option < usize > , data : & ' a Arc < Data > ) -> Sentry < ' a > {
13
+ pub fn new ( coreid : Option < usize > , data : Weak < Data > ) -> Sentry {
14
14
Sentry {
15
15
data,
16
16
coreid,
@@ -23,15 +23,19 @@ impl<'a> Sentry<'a> {
23
23
}
24
24
}
25
25
26
- impl Drop for Sentry < ' _ > {
26
+ impl Drop for Sentry {
27
27
fn drop ( & mut self ) {
28
+ let Some ( data) = self . data . upgrade ( ) else {
29
+ return ;
30
+ } ;
31
+
28
32
// If this sentry was still active,
29
33
// then the task panicked without
30
34
// properly cancelling the sentry,
31
35
// so we should start a new thread.
32
36
if self . active {
33
37
// Spawn another new thread
34
- Threadpool :: spin_up ( self . coreid , self . data . clone ( ) ) ;
38
+ Threadpool :: spin_up ( self . coreid , data. clone ( ) ) ;
35
39
}
36
40
}
37
41
}
You can’t perform that action at this time.
0 commit comments