[Libevent-users] using threads and event_base... Is this an issue ?

Massimo Cetra ctrix+libevent at navynet.it
Wed Mar 7 20:55:39 EST 2007


Steven Grimm wrote:
> It looks like this would be a pretty easy problem to fix -- just make 
> evsignal_init() check to see if the socketpair has already been 
> allocated, and return without doing anything if so.
yes. i have already sent a patch.. but the mail doesn't seems to have 
been accepted even if it was relayed and i had no response back. Maybe 
attachments are moderated.

> Then you just need to make sure your first call to event_init() 
> happens in only one thread. (I am assuming here that you don't 
> actually need signal handling; if you do, then that first thread's 
> event base would need to be kept around since it'd be the only one 
> handling incoming signals.)
>
I call event_init once per thread and destroy it when the thread finishes.
> Barring that, can you maintain a freelist of event bases and only 
> allocate new ones when there aren't any unused ones left? That 
> wouldn't fix the underlying bug, but would at least limit your file 
> descriptor leakage to 2x the largest number of event bases in use at a 
> time.
the patch seems to solve the problem.
I use 2 events per thread, i event_init() every thred and attach the 
events to that base. Then i delete the events and call event_base_free() 
[Note that this is not documented in the man page, afaik].

Even if experimental, now the support for threaded apps seems to work 
pretty well but i have not yet checked for memory leaks which willbe the 
1st step after the application works.

now the patch:

----------------------FIRSTLINE
+++ libevent-1.3b.patched/signal.c      2007-03-08 02:01:54.000000000 +0100
@@ -57,6 +57,7 @@
 static sig_atomic_t evsigcaught[NSIG];
 volatile sig_atomic_t evsignal_caught = 0;

+static int ev_signal_pair_done = 0;
 static struct event ev_signal;
 static int ev_signal_pair[2];
 static int ev_signal_added;
@@ -86,15 +87,17 @@
 #define FD_CLOSEONEXEC(x)
 #endif

-void
-evsignal_init(void)
+void evsignal_init(void)
 {
+
+       if ( ev_signal_pair_done )
+           return;
        /*
         * Our signal handler is going to write to one end of the socket
         * pair to wake up our event loop.  The event loop then scans for
         * signals that got delivered.
         */
-       if (socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1)
+       if ( socketpair(AF_UNIX, SOCK_STREAM, 0, ev_signal_pair) == -1)
                event_err(1, "%s: socketpair", __func__);

        FD_CLOSEONEXEC(ev_signal_pair[0]);
@@ -105,6 +108,8 @@
        event_set(&ev_signal, ev_signal_pair[1], EV_READ,
            evsignal_cb, &ev_signal);
        ev_signal.ev_flags |= EVLIST_INTERNAL;
+
+       ev_signal_pair_done = 1;
 }

 int
-------------------------------LASTLINE










More information about the Libevent-users mailing list