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

Re: [Libevent-users] Better support for user-triggered events



Hi Philip,

Here is a simple example of how I create events that I want to fire explicitly (ie: user triggered). I just happen to be working on this code right now. I do this sort of thing all over the place. Especially useful to wakeup a base on a different thread (assuming you are set up for thread safe operation).

// this is the callback that does the work
void udp_check_txq(int fd, short event, void *arg)
{
   UdpChan_t *udp = (UdpChan_t *)arg;
   process_tx_queues(udp);  // my function not shown here
}

// arm the "user event" to be executed 'now'
void udp_arm_check_txq(UdpChan_t *udp)
{
   // add the event with a 0 timeval 
  // it will be called on next event loop 
   struct timeval tv={0,0};
   evtimer_add(udp->check_txq_ev, &tv); 
}

// Create the event in some init code

if ( !(udp->check_txq_ev = event_new(base, -1, 0, udp_check_txq, udp)) )
{
    break;
}

// Call udp_arm_check_txq() when you want to make the event immediately ready
// It will called on the very next event loop

udp_arm_check_txq(udp);

-Bill


On Sun, Sep 11, 2016 at 1:31 PM, Philip Prindeville <philipp_subx@xxxxxxxxxxxxxxxxxxxxx> wrote:
Figured I'd copy the list on an email I sent Nick about user-triggered events in case anyone else wanted to chime in (or contribute a patch!).

-Philip

To: Nick Mathewson <nickm@xxxxxxxxxxxx>
From: Philp Prindeville <philipp@xxxxxxxxxxxxxxxxxxxxx>
Subject: Question about libevent documentation
Message-ID: <8ba6e993-e887-983e-f06e-b88dfc3aac94@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 11 Sep 2016 11:18:40 -0600

Hi.

Thanks for providing this document.  It's been really helpful. However,
I'm looking at:

http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html

where it says:


     Working with events

     Libevent's basic unit of operation is the event. Every event
     represents a set of conditions, including:

       * A file descriptor being ready to read from or write to.

       * A file descriptor becoming ready to read from or write to
         (Edge-triggered IO only).

       * A timeout expiring.

       * A signal occurring.

       * A user-triggered event.

but there's no description of user-triggered events.  I'm working with
some overly complicated legacy pthreaded code (which uses DBus) and I'm
trying to convert it piecemeal (since my boss doesn't want to allocate
the time to a big-bang re-write) to be event-driven.

Problem is that there are certain events which I need to be able to
model (like queues becoming empty/non-empty/full) and these will need to
be "user-triggered" events, but I can't see from the documentation how
to do this that fits well with our requirements:

I looked for some documentation elsewhere how to do this and saw your
stackoverflow posting:

http://stackoverflow.com/questions/7645217/user-triggered-event-in-libevent

but nothing that's "clean" comes up (either using an unneeded timer
interval, or else using undocumented functions which might change/go
away in the future).

Any chance of adding a couple of paragraphs on the best way to do this?

And it would be nice if the event could be added to the loop via
event_add(), and that the handler for that event was subject to the same
constraints (like priorities, max-dispatch interval, debugging with
event_base_dump_events(), etc).  Also, if you're doing profiling, then
you don't want the event that's setting the user-trigger to be "charged"
the time it takes the user-triggered event to run in addition to its own
time... it's misleading.

Lastly, you might have several high-priority events that run quickly and
want to "fire" the user-triggered event, but not actually have it run
until low-priority events can be dispatched... and then only once (i.e.
once per event-base loop dispatching iteration).

What do you think?

Thanks,

-Philip

P.S. I might be sending you a patch to bind a source address to a
bufferevent socket...

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