Improvements to pthread condition variable code

This commit is contained in:
Ali Mashtizadeh 2015-02-03 14:26:51 -08:00
parent f0d5e791ba
commit f067ec6252

View File

@ -345,18 +345,22 @@ pthread_cond_destroy(pthread_cond_t *cond)
int int
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ {
int status;
struct pthread_cond *cnd; struct pthread_cond *cnd;
uint64_t level; uint64_t level;
if (*cond == NULL) { if (*cond == NULL) {
int status = pthread_cond_init(cond, NULL); status = pthread_cond_init(cond, NULL);
if (status != 0) if (status != 0)
return status; return status;
} }
cnd = *cond; cnd = *cond;
if (mutex) if (mutex) {
pthread_mutex_unlock(mutex); status = pthread_mutex_unlock(mutex);
if (status != 0)
return status;
}
CoreMutex_Lock(&cnd->mtx); CoreMutex_Lock(&cnd->mtx);
level = cnd->enter; level = cnd->enter;
@ -367,8 +371,11 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
OSThreadSleep(0); OSThreadSleep(0);
} }
if (mutex) if (mutex) {
pthread_mutex_lock(mutex); status = pthread_mutex_lock(mutex);
if (status != 0)
return status;
}
return 0; return 0;
} }
@ -377,18 +384,24 @@ int
pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime) const struct timespec *abstime)
{ {
int status = 0;
int rstatus = 0;
struct pthread_cond *cnd; struct pthread_cond *cnd;
uint64_t level; uint64_t level;
uint64_t endtime = abstime->tv_sec * 1000000000 + abstime->tv_nsec;
if (*cond == NULL) { if (*cond == NULL) {
int status = pthread_cond_init(cond, NULL); status = pthread_cond_init(cond, NULL);
if (status != 0) if (status != 0)
return status; return status;
} }
cnd = *cond; cnd = *cond;
if (mutex) if (mutex) {
pthread_mutex_unlock(mutex); status = pthread_mutex_unlock(mutex);
if (status != 0)
return status;
}
CoreMutex_Lock(&cnd->mtx); CoreMutex_Lock(&cnd->mtx);
level = cnd->enter; level = cnd->enter;
@ -398,13 +411,19 @@ pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
while (level >= cnd->exit) { while (level >= cnd->exit) {
OSThreadSleep(0); OSThreadSleep(0);
// XXX: check time if (endtime < OSTime()) {
rstatus = ETIMEDOUT;
break;
}
} }
if (mutex) if (mutex) {
pthread_mutex_lock(mutex); status = pthread_mutex_lock(mutex);
if (status != 0)
return status;
}
return 0; return rstatus;
} }