valenok/mongoose — pull request #226 by yzt (2013-08-25 14:07:55+00:00)
Title: Support for per-thread setup/teardown callbacks.

It is sometimes necessary to initialize some per-thread data or systems, e.g. thread-local storage or `CoInitializeEx` for COM on Windows, or even opening and maintaining a single database connection per-thread.

This commit adds support for two new callbacks: `thread_setup` and `thread_teardown` that, if not NULL, are called at the very beginning and the very end of all threads created by Mongoose. Each of these callbacks takes a single integer parameter that is 0 for the master thread and 1 for the worker threads.

The implementation is minimal and as non-intrusive as possible. However, care must be taken to set the new callbacks to NULL whenever they are not used. Of course, since these are added to the end of the `mg_callbacks` struct, filling the rest of this struct will set these two to NULL automatically.

Note that ensuring thread safety inside these callbacks is the user's responsibility.