Skip to content
Snippets Groups Projects
Commit 6613c1ec authored by Glauber Costa's avatar Glauber Costa Committed by Pekka Enberg
Browse files

sched: provide thread exit notifiers


Functions to be run when a thread finishes.

Signed-off-by: default avatarGlauber Costa <glommer@cloudius-systems.com>
Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
parent 508762bc
No related branches found
No related tags found
No related merge requests found
...@@ -652,6 +652,14 @@ thread::thread(std::function<void ()> func, attr attr, bool main) ...@@ -652,6 +652,14 @@ thread::thread(std::function<void ()> func, attr attr, bool main)
} }
} }
static std::list<std::function<void (thread *)>> exit_notifiers;
void thread::register_exit_notifier(std::function<void (thread *)> &&n)
{
WITH_LOCK(thread_map_mutex) {
exit_notifiers.push_front(std::move(n));
}
}
thread::~thread() thread::~thread()
{ {
cancel_this_thread_alarm(); cancel_this_thread_alarm();
...@@ -662,6 +670,10 @@ thread::~thread() ...@@ -662,6 +670,10 @@ thread::~thread()
WITH_LOCK(thread_map_mutex) { WITH_LOCK(thread_map_mutex) {
thread_map.erase(_id); thread_map.erase(_id);
total_app_time_exited += _total_cpu_time; total_app_time_exited += _total_cpu_time;
for (auto& notifier : exit_notifiers) {
notifier(this);
}
} }
if (_attr._stack.deleter) { if (_attr._stack.deleter) {
_attr._stack.deleter(_attr._stack); _attr._stack.deleter(_attr._stack);
......
...@@ -585,6 +585,18 @@ public: ...@@ -585,6 +585,18 @@ public:
static thread *find_by_id(unsigned int id); static thread *find_by_id(unsigned int id);
static int numthreads(); static int numthreads();
/**
* Registers an std::function that will be called when a thread is torn
* down. This is useful, for example, to run code that needs to cleanup
* resources acquired by a given thread, about which the thread has no
* knowledge about
*
* In general, this will not run in the same context as the dying thread,
* but rather from special scheduler methods. Therefore, one needs to be
* careful about stack usage in here. Do not register notifiers that use a
* lot of stack
*/
static void register_exit_notifier(std::function<void (thread *)> &&n);
private: private:
class reaper; class reaper;
friend class reaper; friend class reaper;
......
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