[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Putting event on the top of the queue



On Tue, Apr 26, 2011 at 6:10 PM, Gordiychuck Oleg
<mybrokenbeat@xxxxxxxxx> wrote:
> Hello!
>
> I'm using event2 libs and i want to know if it is possible to put new event on the top of the queue.
>
> For example, i have next events queue:
>
> ev1 -> ev2 -> ev3
>
> While processing ev1 an error occured. Now i want to create new event(err_ev) that will close all application's modules and insert it before ev2.
>
> ev1 -> err_ev -> ev2-> ev3
>
> So i'll be able to free all allocated data in ev1 and close application "normally".
>
> Some pseudo-code:
>
> void ScheduleFree()
> {
>        event *ev;
>
>        ev = event_new(base,-1,EV_TIMEOUT,err_ev,data);
>        event_base_set(base,ev);

This is needless: you don't need to call event_base_set() unless the
event has no event_base, or unless you need to change the event_base.
That will typically only happen if you're using the deprecated
event_set() code.

Also, event_new() doesn't make an event pending:  it only creates it.
If you want an evnt to be pending or active, you need to use
event_add() or event_active() respectively.

> }
>
> void ev1()
> {
>        uint8_t *some_awesome_data = (uint8_t*)malloc(100);
>
> ... do some stuff
>
>        if (error)
>        {
>                ScheduleFree();
>                free(some_awesome_data);
>                return;
>        }
> ... do some stuff
>
>        free(some_awesome_data);
> }
>
> void err_ev()

I know this is just pseudocode, but remember that the types for event
callbacks need to be specific.  C and C++ are not forgiving about
substituting one function pointer for another.

> {
> ... stop all modules and free data
>        event_base_free(base);
> }

This is going to be a problem for you: you can't free the event_base
from inside a callback, no matter what order you're putting the events
in.  (You're not allowed to free an event_base while its loop is
running.)

> Unfortunately, event_new will put ev to end of the queue.

Not quite; event_active() will put it at the end of the queue of
active events and event_add() will make it happen in the "right" order
depending on its conditions. ... but event_new() just creates the
event.

> Is there any method to put event exactly after ev1? Or may be, is there another solution that avoids such memory leaks? (stl::auto_ptr or boost::scoped_ptr also will not solve this problem)

I'd generally suggest that you do the cleanup outside of the event
loop.  You can do this by having the function that wants to exit the
loop call "event_base_loopbreak()" which makes the event loop stop
running immediately after the current callback finishes.  At that
point, after the event_base_dispatch() or event_base_loop() call
exits, is a great time to clean stuff up.

-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.