ext/revdispatch/libdispatch-0.1/src/ev_dispatch.cc in evdispatch-0.2.4 vs ext/revdispatch/libdispatch-0.1/src/ev_dispatch.cc in evdispatch-0.2.5

- old
+ new

@@ -160,30 +160,30 @@ if( !m_loop_started ){ return ; } printf( "EVD::Dispatch stopping...\n" ); m_lock.lock(); m_loop_started = false; + m_lock.unlock(); + m_cond.broadcast(); m_requests.signal(); m_responses.signal(); - m_lock.unlock(); // send the message to unloop all - { + /*{ Guard g(m_lock); do { // unloop again - printf( "main loop is waiting on the event loop to go down\n" ); - ev_unloop( m_loop, EVUNLOOP_ALL ); - if( m_loop_down ) { break; } - ev_unref( m_loop ); // decrement the ref count + printf( "main loop is waiting on the event loop to go down: %d\n", ev_loop_count( m_loop ) ); + // sleep until the event loop tells us it's done - m_cond.timed_wait(m_lock,Timer(1,0)); // wait until we're done + m_cond.timed_wait(m_lock,Timer(2,0)); // wait until we're done }while( !m_loop_down ); - } + }*/ + ev_async_send( m_loop, &m_loop_ender ); // signal to the event loop it's time to stop - printf( "okay, we're joining the thread\n" ); + //printf( "okay, we're joining the thread\n" ); pthread_join( m_tid, NULL ); if( m_http_client ) { delete m_http_client; m_http_client = NULL; } @@ -245,32 +245,50 @@ } } } +void Dispatch::shutdown_loop_cb( struct ev_loop *loop, struct ev_async *w, int revents ) +{ +// printf( "received the loop unref message\n" ); + Dispatch *d = (Dispatch*)w->data; + ev_async_stop( d->m_loop, &(d->m_request_watcher) ); + ev_async_stop( d->m_loop, &(d->m_loop_ender) ); + ev_unloop( d->m_loop, EVUNLOOP_ALL ); +} + // the main event loop void Dispatch::event_loop_main() { m_loop = ev_loop_new(0); m_request_watcher.data = this; ev_async_init( &m_request_watcher, request_cb_start ); ev_async_start( m_loop, &m_request_watcher ); + m_loop_ender.data = this; + ev_async_init( &m_loop_ender, shutdown_loop_cb ); + ev_async_start( m_loop, &m_loop_ender ); + ev_unref( m_loop ); // don't include the ender in the ev_loop life count + m_lock.lock(); m_loop_started = true; m_loop_down = false; m_cond.signal(); // let the world know we're ready for them m_lock.unlock(); +// printf( "loop running\n" ); // start the main event loop ev_loop( m_loop, 0 ); - ev_async_stop( m_loop, &m_request_watcher ); +// printf( "ev loop is done\n" ); + + // the shutdown sequence m_http_client->stop(); ev_loop_destroy( m_loop ); +// printf( "get the lock to shutdown\n" ); m_lock.lock(); - printf( "let the world know the event loop is going down\n" ); +// printf( "let the world know the event loop is going down\n" ); m_loop_down = true; m_cond.signal(); // let the world know we're done m_lock.unlock(); m_loop = NULL;