Skip to content
Permalink
Browse files
Add loop:fork() API, remove dependency on pthread, and stop calling e…
…v_default_fork automatically.
  • Loading branch information
Brian Maher committed Apr 24, 2010
1 parent 4d759e3 commit c3b08a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
11 README
@@ -54,6 +54,12 @@ Choosing a backend:

Please see the documentation for libev for more details.

WARNING:

If your program fork()s, then you will need to re-initialize
your event loop(s) in the child process. You can do this
re-initialization using the loop:fork() function.

-- ev functions --

major, minor = ev.version()
@@ -167,6 +173,11 @@ ev.SIGNAL (constant)

-- ev.Loop object methods --

loop:fork()

You *must* call this function in the child process after fork(2)
system call and before the next iteration of the event loop.

loop:loop()

Run the event loop! Returns when there are no more watchers
@@ -33,6 +33,7 @@ static int create_loop_mt(lua_State *L) {
{ "loop", loop_loop },
{ "unloop", loop_unloop },
{ "backend", loop_backend },
{ "fork", loop_fork },
{ "__gc", loop_delete },
{ NULL, NULL }
};
@@ -81,9 +82,6 @@ static struct ev_loop** check_loop_and_init(lua_State *L, int loop_i) {
" is causing it to select a bad backend?");
}
register_obj(L, loop_i, *loop_r);

/* TODO: Only enable this if linked with pthread: */
pthread_atfork(0, 0, ev_default_fork);
}
return loop_r;
}
@@ -262,3 +260,22 @@ static int loop_backend(lua_State *L) {
lua_pushinteger(L, ev_backend(*check_loop_and_init(L, 1)));
return 1;
}

/**
* Make it safe to resume an event loop after the fork(2) system call.
*
* [-0, +0, m]
*/
static int loop_fork(lua_State *L) {
struct ev_loop* loop = *check_loop(L, 1);

if ( UNINITIALIZED_DEFAULT_LOOP == loop ) {
// Do nothing!
} else if ( ev_is_default_loop(loop) ) {
ev_default_fork();
} else {
ev_loop_fork(loop);
}

return 0;
}
@@ -23,16 +23,6 @@ external_dependencies = {
header = "ev.h",
library = "libev.so"
},
PTHREADS = {
header = "pthread.h",
},
platforms = {
linux = {
PTHREADS = {
library = "libpthread.so"
}
}
}
}

build = {
@@ -52,7 +42,7 @@ build = {
modules = {
ev = {
libraries = {
"ev", "pthread"
"ev"
}
}
}
@@ -85,6 +85,7 @@ static int loop_update_now(lua_State *L);
static int loop_loop(lua_State *L);
static int loop_unloop(lua_State *L);
static int loop_backend(lua_State *L);
static int loop_fork(lua_State *L);

/**
* Object functions:

0 comments on commit c3b08a1

Please sign in to comment.