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

Re: [Libevent-users] Callback user_data



Hey Nick - thanks for the quick reply.

One more question (new to libevent so sorry if it's obvious).

In normal pollfd I can add and remove events by |= and &= with appropriate flags but I'm looking for the libevent equivalent.

I'm simply looking for a simple way in a single threaded program to add and remove events. But it seems that we need to:
1) check if the event is pending
2) event_get_assignment(..., &assigned_events, ...)
3) event_assign

Is this correct or am I missing something.

Regards,
Timir


On Fri, Aug 23, 2013 at 3:54 PM, Nick Mathewson <nickm@xxxxxxxxxxxxx> wrote:
On Fri, Aug 23, 2013 at 6:23 AM, Timir Karia <tkaria@xxxxxxxxx> wrote:
> Hi All,
>
> I'm using libevent 2.1.3-alpha-dev and was wondering how I could pass an
> event to a callback function as part of another structure. In code I would
> like the following:
>
> struct my_struct {
>   struct event* evt;
>   struct foo* f;
>   int x
> };
>
> void event_callback(evutil_socket_t sock, short what, void *user_data) {
>   struct my_struct* data = "" my_struct*)user_data;
>   another_function(what, data->evt);
> }
>
> Trouble is when I create and add the event the only option to pass the event
> itself is using event_self_cbarg() as follows:
>
> struct event* new_event;
> new_event = event_new(base, fd, EV_WRITE, event_callback,
> event_self_cbarg());
>
> My question is would it be safe to create my_struct and pass it to event_new
> and then fill in the evt member of my_struct later on by using
> my_struct->evt = event_self_cbarg()?

No, but it's fine to do this:

  struct my_struct *arg;
  struct event* new_event;
  arg = malloc(sizeof struct my_struct);
  new_event = event_new(base, fd, EV_WRITE, event_callback, arg);
  arg->evt = new_event;

The event_self_cbarg() trick is only for the case where you need to
make an event be its own user_data.

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



--

Timir Karia
tkaria@xxxxxxxxx