Skip to content

Commit f655684

Browse files
committed
Scoped threads in runtime
1 parent 011bccf commit f655684

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

src/runtime/runtime.rs

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,61 +57,55 @@ impl<'runtime> Runtime<'runtime> {
5757
threadable.install(&installer);
5858
}
5959
}
60-
let mut handles = vec![];
6160

62-
for (name, op) in installer.into_ops().drain() {
63-
handles.push(
64-
thread::Builder::new()
61+
thread::scope(|scope| {
62+
for (name, op) in installer.into_ops().drain() {
63+
let _handle = thread::Builder::new()
6564
.name(String::from(name.as_str()))
66-
.spawn(op)
67-
.map_err(|_err| RuntimeError::ThreadSpawnError(name))?,
68-
);
69-
}
70-
71-
let mut result = Ok(());
72-
73-
for (name, status) in &self.receiver {
74-
match status {
75-
Status::Error(err) => {
76-
// since we entered an error state, we attempt to shutdown the other threads, but
77-
// they could fail due to the error state, but keeping the shutdown error is less
78-
// important than the original error.
79-
let _result = self.shutdown();
80-
result = Err(err);
81-
break;
82-
},
83-
Status::RequestPause => {
84-
for threadable in self.threadables.lock().iter() {
85-
threadable.pause();
86-
}
87-
},
88-
Status::RequestResume => {
89-
for threadable in self.threadables.lock().iter() {
90-
threadable.resume();
91-
}
92-
},
93-
Status::RequestEnd => {
94-
self.thread_statuses.update_thread(RUNTIME_THREAD_NAME, Status::Ended);
95-
for threadable in self.threadables.lock().iter() {
96-
threadable.end();
97-
}
98-
},
99-
Status::New | Status::Busy | Status::Waiting | Status::Ended => {},
65+
.spawn_scoped(scope, op)
66+
.map_err(|_err| RuntimeError::ThreadSpawnError(name))?;
10067
}
10168

102-
self.thread_statuses.update_thread(name.as_str(), status);
103-
104-
if self.thread_statuses.all_ended() {
105-
result = self.shutdown();
106-
break;
107-
}
108-
}
69+
let mut result = Ok(());
70+
71+
for (name, status) in &self.receiver {
72+
match status {
73+
Status::Error(err) => {
74+
// since we entered an error state, we attempt to shutdown the other threads, but
75+
// they could fail due to the error state, but keeping the shutdown error is less
76+
// important than the original error.
77+
let _result = self.shutdown();
78+
result = Err(err);
79+
break;
80+
},
81+
Status::RequestPause => {
82+
for threadable in self.threadables.lock().iter() {
83+
threadable.pause();
84+
}
85+
},
86+
Status::RequestResume => {
87+
for threadable in self.threadables.lock().iter() {
88+
threadable.resume();
89+
}
90+
},
91+
Status::RequestEnd => {
92+
self.thread_statuses.update_thread(RUNTIME_THREAD_NAME, Status::Ended);
93+
for threadable in self.threadables.lock().iter() {
94+
threadable.end();
95+
}
96+
},
97+
Status::New | Status::Busy | Status::Waiting | Status::Ended => {},
98+
}
10999

110-
while let Some(handle) = handles.pop() {
111-
let _result = handle.join();
112-
}
100+
self.thread_statuses.update_thread(name.as_str(), status);
113101

114-
result
102+
if self.thread_statuses.all_ended() {
103+
result = self.shutdown();
104+
break;
105+
}
106+
}
107+
result
108+
})
115109
}
116110

117111
fn shutdown(&self) -> Result<(), RuntimeError> {

0 commit comments

Comments
 (0)