forked from MagicStack/uvloop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork_handler.h
38 lines (28 loc) · 863 Bytes
/
fork_handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
volatile uint64_t MAIN_THREAD_ID = 0;
volatile int8_t MAIN_THREAD_ID_SET = 0;
typedef void (*OnForkHandler)();
OnForkHandler __forkHandler = NULL;
/* Auxiliary function to call global fork handler if defined.
Note: Fork handler needs to be in C (not cython) otherwise it would require
GIL to be present, but some forks can exec non-python processes.
*/
void handleAtFork(void) {
// Reset the MAIN_THREAD_ID on fork, because the main thread ID is not
// always the same after fork, especially when forked from within a thread.
MAIN_THREAD_ID_SET = 0;
if (__forkHandler != NULL) {
__forkHandler();
}
}
void setForkHandler(OnForkHandler handler)
{
__forkHandler = handler;
}
void resetForkHandler(void)
{
__forkHandler = NULL;
}
void setMainThreadID(uint64_t id) {
MAIN_THREAD_ID = id;
MAIN_THREAD_ID_SET = 1;
}