-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Description
Some programs require (1) the ability to initialize per-thread resources whenever a new thread starts executing, and (2) the ability to clean up resources just before the thread exits. For example, performance monitoring tools relying on code instrumentation often initialize per-thread counters on thread start-up and flush them to an output device just before the thread exits.
In libuv there appear to be 3 ways threads can be created:
- The user directly calls uv_thread_create.
- The thread pool creates a number of worker threads in response to various libuv work scheduling requests (e.g., fs jobs, DNS address lookups).
- On OSX, uv__stream_try_select can create a new thread.
In only the first case can the user control the initialization and clean-up of per-thread resources. In the remaining two cases, it's impossible because there is no direct call into the user's code when the thread starts or exits.
To remedy this, I'd like to propose two new libuv APIs:
- One to set an optional callback to be called from within a new thread right after it begins executing.
- One to set an optional callback to be called from within a thread right before it exits.
I've created a branch with the proposed change. See https://github.com/beevik/libuv/releases/tag/thread-cb for the first draft. If you think this is a change worth considering for inclusion in libuv, let me know and I'll make a pull request out of it.
It's possible I missed a way to do this within the current libuv API, so please let me know if that's the case.