Michał Król | 7 Apr 2011 09:53
Picon

Destroying event from callback function

Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal



蒋浩 | 7 Apr 2011 10:14
Picon

Re: Destroying event from callback function

hi,maybe you call event_new without the flag of EV_PERSISTENT,so when cb_func is triggered ,the event is automatic deleted by event_base_loop,it's seems there is two ev_del calls in   your example ,

forgive my pool english..

2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal




Michał Król | 7 Apr 2011 10:18
Picon

Re: Destroying event from callback function

Hi!

Thanks for your response. Unfortunately I want to invoke this event more then once, and stop invoking after particular condition occurs, so it's more like:

void cb_func(agrs){

    //do some work
     if(cond)
         event_del(event);
   
}

2011/4/7 蒋浩 <jaxecn <at> gmail.com>
hi,maybe you call event_new without the flag of EV_PERSISTENT,so when cb_func is triggered ,the event is automatic deleted by event_base_loop,it's seems there is two ev_del calls in   your example ,
forgive my pool english..


2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal





oxromantic | 7 Apr 2011 10:59
Picon

Re: Re: Destroying event from callback function

why not replace the following struct with args?

typedef struct {
struct event *evt;
void *otherargs;
} Parameter;
Parameter *p = malloc(xx)
evt = event_new(xxx, p);
p->evt = evt;

...

-Song

On , Michał Król <mharnen <at> gmail.com> wrote:
> Hi!
>
> Thanks for your response. Unfortunately I want to invoke this event more then once, and stop invoking after particular condition occurs, so it's more like:
>
> void cb_func(agrs){
>
>     //do some work
>
>      if(cond)
>          event_del(event);
>    
> }
>
> 2011/4/7 蒋浩 jaxecn <at> gmail.com>
>
> hi,maybe you call event_new without the flag of EV_PERSISTENT,so when cb_func is triggered ,the event is automatic deleted by event_base_loop,it's seems there is two ev_del calls in   your example ,forgive my pool english..
>
>
>
>
> 2011/4/7 Michał Król mharnen <at> gmail.com>
>
>
> Hi,
>
> I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.
>
>
>
>
> I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.
>
> To be precise I want to do something like this:
>
>
> int main(){
>      event = event_new(base, socket, flags, cb_func, args);
>
>
>
>
>      event_add(event);
> }
>
> void cb_func(args){
>
>      //do some work
>
>      event_del(event); 
>
> Thanks in advance
>
>
>
>
> Michal
>
>
>
>
>
>
>
>
>
>
>
>
>
>

蒋浩 | 7 Apr 2011 11:07
Picon

Re: Destroying event from callback function

hi,you could do like this:

set event type without EV_PERSISTENT 

void cb_func(int fd,short types,void* arg)
{
    //do some work
  if (cond==false)
  {
   event_add(event);//will be triggered again.
  }
}

2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi!

Thanks for your response. Unfortunately I want to invoke this event more then once, and stop invoking after particular condition occurs, so it's more like:

void cb_func(agrs){

    //do some work
     if(cond)
         event_del(event);

   
}

2011/4/7 蒋浩 <jaxecn <at> gmail.com>
hi,maybe you call event_new without the flag of EV_PERSISTENT,so when cb_func is triggered ,the event is automatic deleted by event_base_loop,it's seems there is two ev_del calls in   your example ,
forgive my pool english..


2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal






Michał Król | 7 Apr 2011 11:15
Picon

Re: Destroying event from callback function

<at> 蒋浩

It doesn't matter weather I'll add or delete event, but I need event struct for both operations.

<at> song
Yes I think it'll do what I want. I thought that maybe there is a bit simpler method, but this is solution is good enough - thanks a lot!


Michal

2011/4/7 蒋浩 <jaxecn <at> gmail.com>
hi,you could do like this:
set event type without EV_PERSISTENT 

void cb_func(int fd,short types,void* arg)
{
    //do some work
  if (cond==false)
  {
   event_add(event);//will be triggered again.
  }
}

2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi!

Thanks for your response. Unfortunately I want to invoke this event more then once, and stop invoking after particular condition occurs, so it's more like:

void cb_func(agrs){

    //do some work
     if(cond)
         event_del(event);

   
}

2011/4/7 蒋浩 <jaxecn <at> gmail.com>
hi,maybe you call event_new without the flag of EV_PERSISTENT,so when cb_func is triggered ,the event is automatic deleted by event_base_loop,it's seems there is two ev_del calls in   your example ,
forgive my pool english..


2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal







JiangHao | 7 Apr 2011 11:26
Picon

Re: Destroying event from callback function

sorry ,i read your question again .

may be we could do this:
event* ev = event_new(0, -1,0, 0,0);
event_set(ev,base, socket, flags, cb_func, ev);

then you could pass the event as argument .

2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal




Michał Król | 7 Apr 2011 14:43
Picon

Re: Destroying event from callback function

Actually event_set() caused segmentation fault, but it's newer substitution - event_assign() works perfectly. Problem solved. Thank you both for help.

2011/4/7 JiangHao <jaxecn <at> gmail.com>
sorry ,i read your question again .
may be we could do this:
event* ev = event_new(0, -1,0, 0,0);
event_set(ev,base, socket, flags, cb_func, ev);

then you could pass the event as argument .

2011/4/7 Michał Król <mharnen <at> gmail.com>
Hi,

I was wondering if there is any way I can access event object within callback function in libevent2? I know I can put it in global variable, but I'll have a lot of events and I want to access particular one, so I want to avoid it.

I cannot pass it as an argument because I'll have pointer to event structure after setting callback in evet_new.

To be precise I want to do something like this:


int main(){
     event = event_new(base, socket, flags, cb_func, args);

     event_add(event);
}

void cb_func(args){

     //do some work

     event_del(event);  <-- the problem is with getting event structure which invoked this callback }


Thanks in advance

Michal





Sherif Fanous | 8 Apr 2011 09:37
Picon

Deadlock driving me nuts

Hello

I'm running into a deadlock using libevent. I've been trying to figure out why or where it is happening for the last 3 days but have failed to do so. The deadlock occurs with the event base responsible for sending network packets. Below is a gdb output showing the deadlocked threads.

Thread 32 (Thread 27389):
#0  0xb7713424 in __kernel_vsyscall ()
#1  0xb76ae169 in __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:142
#2  0xb76a95d9 in _L_lock_839 () from /lib/libpthread.so.0
#3  0xb76a946b in __pthread_mutex_lock (mutex=0x8749930) at pthread_mutex_lock.c:82
#4  0x080c10ed in event_add (ev=0x87622f8, tv=0x0) at event.c:1886
#5  0x08088831 in server_send_outgoing_packets (arguments=0xbfa7a79c) at protocol.c:2450
#6  0xb76a6cc9 in start_thread (arg=0xb4e65b70) at pthread_create.c:304
#7  0xb75f669e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 29 (Thread 27392):
#0  0xb7713424 in __kernel_vsyscall ()
#1  0xb76ae169 in __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:142
#2  0xb76a95d9 in _L_lock_839 () from /lib/libpthread.so.0
#3  0xb76a946b in __pthread_mutex_lock (mutex=0x8749930) at pthread_mutex_lock.c:82
#4  0x080c1757 in event_del (ev=0x898f678) at event.c:2108
#5  0x080c1b3e in event_free (ev=0x898f678) at event.c:1739
#6  0x0807467b in destruct_client (arguments=0xb2dc2890) at destruct.c:62
#7  0x0805ef50 in iterator_remove (iterator=0xb2853038) at linked-list.c:351
#8  0x0807f404 in server_manager_worker (arguments=0xbfa7a6e4) at protocol.c:271
#9  0xb76a6cc9 in start_thread (arg=0xb4b62b70) at pthread_create.c:304
#10 0xb75f669e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 24 (Thread 27382):
#0  0xb7713424 in __kernel_vsyscall ()
#1  0xb76ae169 in __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:142
#2  0xb76a95d9 in _L_lock_839 () from /lib/libpthread.so.0
#3  0xb76a946b in __pthread_mutex_lock (mutex=0x8749930) at pthread_mutex_lock.c:82
#4  0x080c1757 in event_del (ev=0xb27fd068) at event.c:2108
#5  0x080c1b3e in event_free (ev=0xb27fd068) at event.c:1739
#6  0x08074b0d in destruct_server (arguments=0xb2732558) at destruct.c:181
#7  0x0805ef50 in iterator_remove (iterator=0xade4f080) at linked-list.c:351
#8  0x08098118 in client_connector_connection_manager_worker (arguments=0xbfa7a614) at protocol.c:6064
#9  0xb76a6cc9 in start_thread (arg=0xb556cb70) at pthread_create.c:304
#10 0xb75f669e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 21 (Thread 27350):
#0  evmap_io_active (base=0x872b890, fd=247, events=36) at evmap.c:399
#1  0x080c84ae in epoll_dispatch (base=0x872b890, tv=0xb6e1c334) at epoll.c:436
#2  0x080c2914 in event_base_loop (base=0x872b890, flags=<value optimized out>) at event.c:1537
#3  0x080a02a9 in send_outgoing_packets (arguments=0xbfa7a80c) at worker.c:36
#4  0xb76a6cc9 in start_thread (arg=0xb6e1cb70) at pthread_create.c:304
#5  0xb75f669e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130

Thread 7 (Thread 27383):
#0  0xb7713424 in __kernel_vsyscall ()
#1  0xb76ae169 in __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/lowlevellock.S:142
#2  0xb76a95d9 in _L_lock_839 () from /lib/libpthread.so.0
#3  0xb76a946b in __pthread_mutex_lock (mutex=0x8749930) at pthread_mutex_lock.c:82
#4  0x080c10ed in event_add (ev=0xb27fd068, tv=0x0) at event.c:1886
#5  0x08095eb8 in client_connector_send_outgoing_packets (arguments=0xbfa7a7b8) at protocol.c:5471
#6  0xb76a6cc9 in start_thread (arg=0xb546bb70) at pthread_create.c:304
#7  0xb75f669e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130


Thread 21 calls event_base_loop(send_event_base, EVLOOP_NONBLOCK). The other threads are either trying to add or delete events from the event base. For some reason it seems after some random time Thread 21 never relinquishes the send_event_base lock, therefore all other threads get stuck trying to acquire the lock!!!!

I first thought that my callbacks could be getting deadlocked, but I've ensured this is not the case by having every callback print a message on entry, and a message on exit.

I'm calling evthread_use_pthreads() before calling any libevent function.

Any help or insight would be much appreciated.

Thanks

Regards

Sherif
Nick Mathewson | 8 Apr 2011 21:38

Re: Deadlock driving me nuts

On Fri, Apr 8, 2011 at 3:37 AM, Sherif Fanous <sherif.fanous <at> gmail.com> wrote:
> Hello
> I'm running into a deadlock using libevent. I've been trying to figure out
> why or where it is happening for the last 3 days but have failed to do so.
> The deadlock occurs with the event base responsible for sending network
> packets. Below is a gdb output showing the deadlocked threads.

Thread 21 (Thread 27350):
#0  evmap_io_active (base=0x872b890, fd=247, events=36) at evmap.c:399
#1  0x080c84ae in epoll_dispatch (base=0x872b890, tv=0xb6e1c334) at epoll.c:436

So, this thread ought to be holding the event_base lock, but I don't
know why it would be freaking out like this.  The place that it seems
to be stuck on is:

        TAILQ_FOREACH(ev, &ctx->events, ev_io_next) {
                if (ev->ev_events & events)
                        event_active_nolock(ev, ev->ev_events & events, 1);
        }

I'm guessing that if you let it run a while, it stays stuck at the
same place? (And not, say, in event_active_nolock)?

So I can only think of two ways that could get stuck.  The first one
is if the ctx->events list had somehow gotten corrupted (say, if it
became circular somehow).  The second is if event_active_nolock had
somehow become stuck... but if that's the case, then I would expect
event_active_nolock to appear on your stack trace.

To see if I guessed right about the first case, could you try walking
through the ctx->events list via ev_io_next and seeing whether it
actually ends, or whether it's corrupted?

--

-- 
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo <at> freehaven.net with
unsubscribe libevent-users    in the body.


Gmane