From 3a45686c3ff8104c638df7742eabbbb71fdb24b8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 18 Aug 2007 06:59:20 +0000 Subject: [PATCH] push event_del() through the libevent pipe too. --- libtransmission/timer.c | 13 ++++++++----- libtransmission/trevent.c | 35 +++++++++++++++++++++++++++-------- libtransmission/trevent.h | 19 +++++++++++-------- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/libtransmission/timer.c b/libtransmission/timer.c index fedefe802..15adf770f 100644 --- a/libtransmission/timer.c +++ b/libtransmission/timer.c @@ -27,7 +27,8 @@ typedef void tr_data_free_func( void * user_data ); struct timer_node { - struct event event; + tr_handle_t * handle; + struct event * event; tr_timer_func * func; void * user_data; tr_data_free_func * free_func; @@ -47,7 +48,7 @@ unref( struct timer_node * node, int count ) if( node->free_func != NULL ) (node->free_func)( node->user_data ); - event_del( &node->event ); + tr_event_del( node->handle, node->event ); tr_free( node ); } @@ -74,7 +75,7 @@ timerCB( int fd UNUSED, short event UNUSED, void * arg ) if( !val ) unref( node, 2 ); else { - timeout_add( &node->event, &node->tv ); + timeout_add( node->event, &node->tv ); unref( node, 1 ); } } @@ -94,13 +95,15 @@ tr_timerNew( tr_handle_t * handle, assert( timeout_milliseconds >= 0 ); node = tr_new( struct timer_node, 1 ); + node->handle = handle; + node->event = tr_new0( struct event, 1 ); node->func = func; node->user_data = user_data; node->free_func = free_func; node->refcount = 1; node->tv.tv_sec = microseconds / 1000000; node->tv.tv_usec = microseconds % 1000000; - timeout_set( &node->event, timerCB, node ); - tr_event_add( handle, &node->event, &node->tv ); + timeout_set( node->event, timerCB, node ); + tr_event_add( handle, node->event, &node->tv ); return node; } diff --git a/libtransmission/trevent.c b/libtransmission/trevent.c index 5763dbb18..3465e8b30 100644 --- a/libtransmission/trevent.c +++ b/libtransmission/trevent.c @@ -68,15 +68,11 @@ readFromPipe( int fd, short eventType UNUSED, void * unused UNUSED ) struct evhttp_request * req; enum evhttp_cmd_type type; const char * uri; - static char * buf = NULL; #ifdef DEBUG fprintf( stderr, "reading...reads: [%d] writes: [%d]\n", ++reads, writes ); #endif - if( !buf ) - buf = tr_new0( char, sizeof(void*) ); - ch = '\0'; do { ret = read( fd, &ch, 1 ); @@ -88,9 +84,14 @@ readFromPipe( int fd, short eventType UNUSED, void * unused UNUSED ) } else switch( ch ) { + case 'd': /* event_del */ + read( fd, &event, sizeof(struct event*) ); + tr_dbg( "read del event from pipe: event is %p", event ); + event_del( event ); + break; + case 'e': /* event_add */ - event = tr_new0( struct event, 1 ); - read( fd, event, sizeof(struct event) ); + read( fd, &event, sizeof(struct event*) ); read( fd, &interval, sizeof(struct timeval) ); tr_dbg( "read event from pipe: event.ev_arg is %p", event->ev_arg ); event_add( event, &interval ); @@ -150,7 +151,7 @@ tr_eventClose( tr_handle_t * handle UNUSED ) } void -tr_event_add (tr_handle_t * handle, +tr_event_add( tr_handle_t * handle, struct event * event, struct timeval * interval ) { @@ -164,11 +165,29 @@ tr_event_add (tr_handle_t * handle, fprintf( stderr, "reads: [%d] writes: [%d]\n", reads, ++writes ); #endif write( fd, &ch, 1 ); - write( fd, event, sizeof(struct event) ); + write( fd, &event, sizeof(struct event*) ); write( fd, interval, sizeof(struct timeval) ); tr_lockUnlock( lock ); } +void +tr_event_del( tr_handle_t * handle, + struct event * event ) +{ + const char ch = 'd'; + int fd = handle->events->fds[1]; + tr_lock_t * lock = handle->events->lock; + + tr_lockLock( lock ); + tr_dbg( "writing event to pipe: del event %p", event ); +#ifdef DEBUG + fprintf( stderr, "reads: [%d] writes: [%d]\n", reads, ++writes ); +#endif + write( fd, &ch, 1 ); + write( fd, &event, sizeof(struct event*) ); + tr_lockUnlock( lock ); +} + void tr_evhttp_make_request (tr_handle_t * handle, struct evhttp_connection * evcon, diff --git a/libtransmission/trevent.h b/libtransmission/trevent.h index d7fd50e63..0b156e4c9 100644 --- a/libtransmission/trevent.h +++ b/libtransmission/trevent.h @@ -25,14 +25,17 @@ enum evhttp_cmd_type; struct evhttp_request; struct evhttp_connection; -extern void tr_event_add( struct tr_handle_s * tr_handle, - struct event * event, - struct timeval * interval ); +void tr_event_add( struct tr_handle_s * tr_handle, + struct event * event, + struct timeval * interval ); -extern void tr_evhttp_make_request (struct tr_handle_s * tr_handle, - struct evhttp_connection * evcon, - struct evhttp_request * req, - enum evhttp_cmd_type type, - const char * uri); +void tr_event_del( struct tr_handle_s * tr_handle, + struct event * event ); + +void tr_evhttp_make_request (struct tr_handle_s * tr_handle, + struct evhttp_connection * evcon, + struct evhttp_request * req, + enum evhttp_cmd_type type, + const char * uri); #endif