diff --git a/lib_fiber/c/src/fiber_io.c b/lib_fiber/c/src/fiber_io.c index 5fa43272a..7a961b9ae 100644 --- a/lib_fiber/c/src/fiber_io.c +++ b/lib_fiber/c/src/fiber_io.c @@ -532,7 +532,8 @@ void fiber_file_set(FILE_EVENT *fe) } if (__thread_fiber->events[fe->fd] != NULL) { - printf("%s(%d): exist fd=%d\r\n", __FUNCTION__, __LINE__, fe->fd); + printf("%s(%d): exist fd=%d, old=%p new=%p\r\n", __FUNCTION__, + __LINE__, fe->fd, __thread_fiber->events[fe->fd], fe); abort(); } @@ -692,9 +693,16 @@ void fiber_file_close(FILE_EVENT *fe) FILE_EVENT *fiber_file_cache_get(socket_t fd) { FILE_EVENT *fe; + int exist = 0; fiber_io_check(); - fe = (FILE_EVENT*) array_pop_back(__thread_fiber->cache); + + fe = fiber_file_get(fd); + if (fe == NULL) { + fe = (FILE_EVENT*) array_pop_back(__thread_fiber->cache); + } else { // Why? the fe wasn't removed by fiber_file_cache_put ? + exist = 1; + } if (fe == NULL) { fe = file_event_alloc(fd); } else { @@ -706,7 +714,9 @@ FILE_EVENT *fiber_file_cache_get(socket_t fd) fe->mask |= EVENT_DIRECT; } #endif - fiber_file_set(fe); + if (!exist) { + fiber_file_set(fe); + } return fe; } diff --git a/lib_fiber/c/src/sync/sync_timer.c b/lib_fiber/c/src/sync/sync_timer.c index b5ad2aac2..9b1f36274 100644 --- a/lib_fiber/c/src/sync/sync_timer.c +++ b/lib_fiber/c/src/sync/sync_timer.c @@ -12,6 +12,7 @@ struct SYNC_TIMER { MBOX *box; int stop; TIMER_CACHE *waiters; + unsigned long tid; }; static SYNC_TIMER *sync_timer_new(void) @@ -23,6 +24,7 @@ static SYNC_TIMER *sync_timer_new(void) pthread_mutex_init(&timer->lock, NULL); timer->box = mbox_create(MBOX_T_MPSC); timer->waiters = timer_cache_create(); + timer->tid = __pthread_self(); out = mbox_out(timer->box); assert(out != INVALID_SOCKET); @@ -204,12 +206,24 @@ void sync_timer_wakeup(SYNC_TIMER *timer, SYNC_OBJ *obj) if (var_hook_sys_api) { socket_t out = mbox_out(timer->box); - FILE_EVENT *fe = fiber_file_cache_get(out); + FILE_EVENT *fe; + int same_thread; + + if (__pthread_self() == timer->tid) { + fe = fiber_file_open_write(out); + same_thread = 1; + } else { + fe = fiber_file_cache_get(out); + same_thread = 0; + } fe->mask |= EVENT_SYSIO; mbox_send(timer->box, msg); - fiber_file_cache_put(fe); + + if (!same_thread) { + fiber_file_cache_put(fe); + } } else { mbox_send(timer->box, msg); }