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

Re: [Libevent-users] Paired bufferevents can't let event_base_dispatch block?



On Tue, Oct 13, 2015 at 08:51:32PM +0800, wwyyxx26 wrote:
> I want use paired bufferevents to pass message to the thread whitch called event_base_dispatch.
> 
> 
> ...
> struct bufferevent *pairbev[2];
> bufferevent_pair_new(ebase, BEV_OPT_THREADSAFE, pairbev);
> ...
> bufferevent_setcb(pairbev[1], Static_ReadCallback, NULL, Static_EventCallback, this);
> bufferevent_enable(pairbev[1], EV_READ|EV_WRITE);
> ...
> 
> 
> but I found that when the event_base has only pairbev[1] event on it, it can't let the event_base_dispatch block, this function return with value 1 Immediately.
> I had tried both in linux and windows, it looks like the same. 
> Does it a bug or was there something i Ignored?

So, exit code 1 from event_base_dispatch() is:
    /* If we have no events, we just exit */
    if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
        !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
            event_debug(("%s: no events registered.", __func__));
            retval = 1;
            goto done;
    }

IOW you don't have any events, and indeed, since bufferevent_pair_new() will
not attach any events to base, since it uses evbuffer (with callback)
internally, and there will be events only when there will be data (deferred
callback).

You can do next things:
- start loop with EVLOOP_NO_EXIT_ON_EMPTY
- replace bufferevent_pair with socketpair+bufferevent, you can find the
  example in regress_bufferevent.c:
    evutil_socket_t pair[2];
    assert(!evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair));
    assert(!evutil_make_socket_nonblocking(pair[0]));
    assert(!(evutil_make_socket_nonblocking(pair[1]));
    ...
    bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL);
    bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL);

  (Or the same with pipe(2)).

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