From 56f6e0e62d1a2dd5a261a705536dbf8543fb67a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Sun, 1 Sep 2013 22:20:48 +0800 Subject: [PATCH] add some check for pthread api --- skynet-src/skynet_start.c | 46 +++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index d1dc3f77..f3be84ec 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -32,12 +32,21 @@ struct worker_parm { #define CHECK_ABORT if (skynet_context_total()==0) break; +static void +create_thread(pthread_t *thread, void *(*start_routine) (void *), void *arg) { + if (pthread_create(thread,NULL, start_routine, arg)) { + fprintf(stderr, "Create thread failed"); + exit(1); + } +} + static void wakeup(struct monitor *m, int busy) { if (m->sleep >= m->count - busy) { - pthread_mutex_lock(&m->mutex); - pthread_cond_signal(&m->cond); - pthread_mutex_unlock(&m->mutex); + if (pthread_mutex_lock(&m->mutex) == 0) { + pthread_cond_signal(&m->cond); + pthread_mutex_unlock(&m->mutex); + } } } @@ -98,11 +107,12 @@ _worker(void *p) { for (;;) { if (skynet_context_message_dispatch(sm)) { CHECK_ABORT - pthread_mutex_lock(&m->mutex); - ++ m->sleep; - pthread_cond_wait(&m->cond, &m->mutex); - -- m->sleep; - pthread_mutex_unlock(&m->mutex); + if (pthread_mutex_lock(&m->mutex) == 0) { + ++ m->sleep; + pthread_cond_wait(&m->cond, &m->mutex); + -- m->sleep; + pthread_mutex_unlock(&m->mutex); + } } } return NULL; @@ -122,23 +132,31 @@ _start(int thread) { for (i=0;im[i] = skynet_monitor_new(i); } - pthread_mutex_init(&m->mutex, NULL); - pthread_cond_init(&m->cond, NULL); + if (pthread_mutex_init(&m->mutex, NULL)) { + fprintf(stderr, "Init mutex error"); + exit(1); + } + if (pthread_cond_init(&m->cond, NULL)) { + fprintf(stderr, "Init cond error"); + exit(1); + } - pthread_create(&pid[0], NULL, _monitor, m); - pthread_create(&pid[1], NULL, _timer, m); - pthread_create(&pid[2], NULL, _socket, m); + create_thread(&pid[0], _monitor, m); + create_thread(&pid[1], _timer, m); + create_thread(&pid[2], _socket, m); struct worker_parm wp[thread]; for (i=0;imutex); + pthread_cond_destroy(&m->cond); } static int