void fiber_io_check(void) { if (__thread_fiber != NULL) { if (__thread_fiber->ev_fiber == NULL) { __thread_fiber->ev_fiber = acl_fiber_create( fiber_io_loop, __thread_fiber->event, STACK_SIZE); __thread_fiber->io_count = 0; __thread_fiber->nsleeping = 0; __thread_fiber->io_stop = 0; ring_init(&__thread_fiber->ev_timer); } return; } if (pthread_once(&__once_control, thread_init) != 0) { msg_fatal("%s(%d), %s: pthread_once error %s", __FILE__, __LINE__, __FUNCTION__, last_serror()); } var_maxfd = open_limit(0); if (var_maxfd <= 0) { var_maxfd = MAXFD; } __thread_fiber = (FIBER_TLS *) malloc(sizeof(FIBER_TLS)); __thread_fiber->event = event_create(var_maxfd); __thread_fiber->ev_fiber = acl_fiber_create(fiber_io_loop, __thread_fiber->event, STACK_SIZE); __thread_fiber->io_count = 0; __thread_fiber->nsleeping = 0; __thread_fiber->io_stop = 0; ring_init(&__thread_fiber->ev_timer); #ifdef SYS_WIN __thread_fiber->events = htable_create(var_maxfd); #else __thread_fiber->events = (FILE_EVENT **) calloc(var_maxfd, sizeof(FILE_EVENT*)); #endif if (__pthread_self() == main_thread_self()) { __main_fiber = __thread_fiber; atexit(fiber_io_main_free); } else if (pthread_setspecific(__fiber_key, __thread_fiber) != 0) { msg_fatal("pthread_setspecific error!"); } }
const char *last_serror(void) { char *buf; int error = acl_fiber_last_error(); static size_t __buf_size = 4096; if (pthread_once(&once_control, thread_buf_init) != 0) { abort(); } buf = (char*) pthread_getspecific(__errbuf_key); if (buf == NULL) { buf = (char*) malloc(__buf_size); assert(pthread_setspecific(__errbuf_key, buf) == 0); if (__pthread_self() == main_thread_self()) { __main_buf = buf; atexit(main_free_buf); } } return acl_fiber_strerror(error, buf, __buf_size); }
static void thread_free_buf(void *buf) { if (__pthread_self() != main_thread_self()) { free(buf); } }