pthread condition variable implementation

This commit is contained in:
Ali Mashtizadeh 2015-02-01 19:21:16 -08:00
parent d729ee868d
commit 87e3cde79b

View File

@ -301,40 +301,112 @@ pthread_mutex_unlock(pthread_mutex_t *mutex)
* Condition Variables * Condition Variables
*/ */
struct pthread_cond { struct pthread_condattr {
uint64_t _unused;
}; };
struct pthread_condattr { struct pthread_cond {
CoreMutex mtx;
uint64_t enter;
uint64_t exit;
}; };
int int
pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{ {
return EINVAL; struct pthread_cond *cnd = (struct pthread_cond *)malloc(sizeof(*cnd));
if (cnd == NULL) {
return ENOMEM;
}
CoreMutex_Init(&cnd->mtx);
cnd->enter = 0;
cnd->exit = 0;
*cond = cnd;
return 0;
} }
int int
pthread_cond_destroy(pthread_cond_t *cond) pthread_cond_destroy(pthread_cond_t *cond)
{ {
struct pthread_cond *cnd = *cond;
*cond = NULL;
free(cnd);
return EINVAL; return EINVAL;
} }
int int
pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ {
return EINVAL; struct pthread_cond *cnd;
uint64_t level;
if (*cond == NULL) {
int status = pthread_cond_init(cond, NULL);
if (status != 0)
return status;
}
cnd = *cond;
if (mutex)
pthread_mutex_unlock(mutex);
CoreMutex_Lock(&cnd->mtx);
level = cnd->enter;
cnd->enter++;
CoreMutex_Unlock(&cnd->mtx);
while (level >= cnd->exit) {
OSThreadSleep(0);
}
if (mutex)
pthread_mutex_lock(mutex);
return 0;
} }
int int
pthread_cond_signal(pthread_cond_t *cond) pthread_cond_signal(pthread_cond_t *cond)
{ {
return EINVAL; struct pthread_cond *cnd;
if (*cond == NULL) {
int status = pthread_cond_init(cond, NULL);
if (status != 0)
return status;
}
cnd = *cond;
CoreMutex_Lock(&cnd->mtx);
cnd->exit++;
CoreMutex_Unlock(&cnd->mtx);
return 0;
} }
int int
pthread_cond_broadcast(pthread_cond_t *cond) pthread_cond_broadcast(pthread_cond_t *cond)
{ {
return EINVAL; struct pthread_cond *cnd;
if (*cond == NULL) {
int status = pthread_cond_init(cond, NULL);
if (status != 0)
return status;
}
cnd = *cond;
CoreMutex_Lock(&cnd->mtx);
cnd->exit = cnd->enter;
CoreMutex_Unlock(&cnd->mtx);
return 0;
} }