vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

commit 587fe56108bafad114681688e78c8f8b189ccb77
parent d3cc189e0aeaf4669cedab9f2e1bf0be0ff4accd
Author: Russ Cox <rsc@swtch.com>
Date:   Thu,  3 Jul 2008 16:38:51 -0400

9vx: initialize pthread_mutex_t before use, check return values

Diffstat:
Msrc/9vx/a/dat.h | 2+-
Msrc/9vx/sched.c | 46++++++++++++++++++++++++++++++++++------------
2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/9vx/a/dat.h b/src/9vx/a/dat.h @@ -356,9 +356,9 @@ typedef struct Psleep Psleep; typedef struct Pwaiter Pwaiter; struct Psleep { + int init; pthread_mutex_t mutex; pthread_cond_t cond; - int condinit; Pwaiter *waiter; int fd[2]; vlong nread; diff --git a/src/9vx/sched.c b/src/9vx/sched.c @@ -61,7 +61,7 @@ noidlehands(void) if(m->machno == 0) return; plock(&idling); - idlewakeup++; + idlewakeup = 1; pwakeup(&idling); punlock(&idling); } @@ -154,7 +154,8 @@ runproc(void) kprocq.tail = nil; kprocq.n--; if(traceprocs) - iprint("cpu%d: runproc %ld %s [%d %d]\n", m->machno, p->pid, p->text, kprocq.n, nrunproc); + iprint("cpu%d: runproc %ld %s [%d %d]\n", + m->machno, p->pid, p->text, kprocq.n, nrunproc); unlock(&kprocq.lk); punlock(&run); return p; @@ -198,13 +199,15 @@ runproc(void) #define PIPES 0 #define WAITERS 1 -#ifdef __APPLE__ +#if 0 #undef PIPES #define PIPES 1 #undef WAITERS #define WAITERS 0 #endif +static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER; + struct Pwaiter { pthread_cond_t cond; @@ -215,11 +218,19 @@ struct Pwaiter void plock(Psleep *p) { - pthread_mutex_lock(&p->mutex); - if(!p->condinit){ - p->condinit = 1; - pthread_cond_init(&p->cond, nil); + int r; + + if(!p->init){ + pthread_mutex_lock(&initmutex); + if(!p->init){ + p->init = 1; + pthread_mutex_init(&p->mutex, nil); + pthread_cond_init(&p->cond, nil); + } + pthread_mutex_unlock(&initmutex); } + if((r = pthread_mutex_lock(&p->mutex)) != 0) + panic("pthread_mutex_lock: %d", r); #if PIPES if(p->fd[1] == 0){ pipe(p->fd); @@ -231,12 +242,17 @@ plock(Psleep *p) void punlock(Psleep *p) { - pthread_mutex_unlock(&p->mutex); + int r; + + if((r = pthread_mutex_unlock(&p->mutex)) != 0) + panic("pthread_mutex_unlock: %d", r); } void psleep(Psleep *p) { + int r; + #if PIPES p->nread++; punlock(p); @@ -256,16 +272,20 @@ psleep(Psleep *p) w.next = p->waiter; p->waiter = &w; while(!w.awake) - pthread_cond_wait(&w.cond, &p->mutex); + if((r = pthread_cond_wait(&w.cond, &p->mutex)) != 0) + panic("pthread_cond_wait: %d", r); pthread_cond_destroy(&w.cond); #else - pthread_cond_wait(&p->cond, &p->mutex); + if((r = pthread_cond_wait(&p->cond, &p->mutex)) != 0) + panic("pthread_cond_wait: %d", r); #endif } void pwakeup(Psleep *p) { + int r; + #if PIPES char c = 0; int nbad = 0; @@ -283,10 +303,12 @@ pwakeup(Psleep *p) if(w){ p->waiter = w->next; w->awake = 1; - pthread_cond_signal(&w->cond); + if((r = pthread_cond_signal(&w->cond)) != 0) + panic("pthread_cond_signal: %d", r); } #else - pthread_cond_signal(&p->cond); + if((r = pthread_cond_signal(&p->cond)) != 0) + panic("pthread_cond_signal: %d", r); #endif }