Use higher-level wrappers for the __forkHandler global

This commit is contained in:
Yury Selivanov 2019-10-24 22:51:19 -04:00
parent bed926c500
commit 3e6a0430f6
No known key found for this signature in database
GPG Key ID: E6ABA2A46889EBBB
4 changed files with 24 additions and 9 deletions

View File

@ -77,7 +77,7 @@ cdef class UVProcess(UVHandle):
loop.active_process_handler = self
__forking = 1
__forking_loop = loop
__forkHandler = <OnForkHandler>&__get_fork_handler
system.setForkHandler(<system.OnForkHandler>&__get_fork_handler)
PyOS_BeforeFork()
@ -87,7 +87,7 @@ cdef class UVProcess(UVHandle):
__forking = 0
__forking_loop = None
__forkHandler = NULL
system.resetForkHandler()
loop.active_process_handler = None
PyOS_AfterFork_Parent()

View File

@ -8,8 +8,20 @@ OnForkHandler __forkHandler = NULL;
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 handleAtFork(void) {
if (__forkHandler != NULL) {
__forkHandler();
}
}
void setForkHandler(OnForkHandler handler)
{
__forkHandler = handler;
}
void resetForkHandler(void)
{
__forkHandler = NULL;
}

View File

@ -81,3 +81,11 @@ cdef extern from "includes/compat.h" nogil:
int EPOLL_CTL_DEL
int epoll_ctl(int epfd, int op, int fd, epoll_event *event)
object MakeUnixSockPyAddr(sockaddr_un *addr)
cdef extern from "includes/fork_handler.h":
ctypedef void (*OnForkHandler)()
void handleAtFork()
void setForkHandler(OnForkHandler handler)
void resetForkHandler()

View File

@ -3178,11 +3178,6 @@ cdef vint __atfork_installed = 0
cdef vint __forking = 0
cdef Loop __forking_loop = None
cdef extern from "includes/fork_handler.h":
ctypedef void (*OnForkHandler)()
cdef OnForkHandler __forkHandler
void handleAtFork()
cdef void __get_fork_handler() nogil:
global __forking
@ -3201,7 +3196,7 @@ cdef __install_atfork():
cdef int err
err = system.pthread_atfork(NULL, NULL, &handleAtFork)
err = system.pthread_atfork(NULL, NULL, &system.handleAtFork)
if err:
__atfork_installed = 0
raise convert_error(-err)