Skip to content
Snippets Groups Projects
Commit 12875ed4 authored by Avi Kivity's avatar Avi Kivity
Browse files

sched: implement thread::join()

While generally useful, it helps now to avoid wakeups to threads that are
no longer there.
parent 6542bc76
No related branches found
No related tags found
No related merge requests found
......@@ -62,8 +62,7 @@ void thread::init_stack()
void thread::on_thread_stack(thread* t)
{
t->_func();
t->_waiting = true;
schedule();
t->complete();
}
void thread::setup_tcb()
......@@ -80,8 +79,7 @@ void thread_main_c(thread* t)
{
s_current = t;
t->main();
t->_waiting = true;
schedule();
t->complete();
}
}
......
......@@ -83,6 +83,8 @@ thread::thread(std::function<void ()> func, stack_info stack, bool main)
, _on_runqueue(!main)
, _waiting(false)
, _stack(stack)
, _terminated(false)
, _joiner()
{
with_lock(thread_list_mutex, [this] {
thread_list.push_back(*this);
......@@ -151,6 +153,24 @@ void thread::stop_wait()
_waiting = false;
}
void thread::complete()
{
_waiting = true;
_terminated = true;
if (_joiner) {
_joiner->wake();
}
while (true) {
schedule();
}
}
void thread::join()
{
_joiner = current();
wait_until([this] { return _terminated; });
}
thread::stack_info thread::get_stack_info()
{
return _stack;
......
......@@ -49,6 +49,7 @@ public:
static thread* current();
stack_info get_stack_info();
cpu* tcpu();
void join();
private:
void main();
void switch_to();
......@@ -58,6 +59,7 @@ private:
void stop_wait();
void init_stack();
void setup_tcb();
void complete();
static void on_thread_stack(thread* t);
private:
std::function<void ()> _func;
......@@ -67,6 +69,7 @@ private:
std::atomic_bool _waiting;
stack_info _stack;
cpu* _cpu;
bool _terminated;
friend void thread_main_c(thread* t);
friend class wait_guard;
friend class cpu;
......@@ -74,6 +77,7 @@ private:
friend void ::smp_launch();
friend void init(elf::tls_data tls, std::function<void ()> cont);
public:
thread* _joiner;
bi::list_member_hook<> _runqueue_link;
// for the debugger
bi::list_member_hook<> _thread_list_link;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment