This commit is contained in:
74
posix/Condition.c
Normal file
74
posix/Condition.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "Internal.h"
|
||||
#include "Timespec.h"
|
||||
#include "Codemap.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
CgeCondition *CgeConditionNew(int *result) {
|
||||
CgeCondition *condition;
|
||||
int code = CGE_THREAD_EOOM;
|
||||
|
||||
condition = malloc(sizeof(*condition));
|
||||
if (condition && (code = errorToErrorCode(pthread_cond_init(&condition->handle, NULL)))) {
|
||||
free(condition);
|
||||
condition = NULL;
|
||||
}
|
||||
|
||||
if (result)
|
||||
*result = code;
|
||||
return condition;
|
||||
}
|
||||
|
||||
void CgeConditionFree(CgeCondition *condition) {
|
||||
if (!condition)
|
||||
return;
|
||||
pthread_cond_destroy(&condition->handle);
|
||||
free(condition);
|
||||
}
|
||||
|
||||
int CgeConditionWait(CgeCondition *condition, CgeMutex *mutex, int *result) {
|
||||
int code;
|
||||
|
||||
code = errorToErrorCode(pthread_cond_wait(&condition->handle, &mutex->handle));
|
||||
if (result)
|
||||
*result = code;
|
||||
|
||||
return code == CGE_THREAD_EOK;
|
||||
}
|
||||
|
||||
int CgeConditionWaitFor(CgeCondition *condition, CgeMutex *mutex,
|
||||
uint32_t timeout, int *result) {
|
||||
struct timespec ts;
|
||||
int code;
|
||||
|
||||
if (!convertToTimespec(&ts, timeout)) {
|
||||
if (result)
|
||||
*result = CGE_THREAD_EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
code = errorToErrorCode(pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts));
|
||||
if (result)
|
||||
*result = code;
|
||||
|
||||
return code == CGE_THREAD_EOK;
|
||||
}
|
||||
|
||||
int CgeConditionSignal(CgeCondition *condition, int *result) {
|
||||
int code;
|
||||
|
||||
code = errorToErrorCode(pthread_cond_signal(&condition->handle));
|
||||
if (result)
|
||||
*result = code;
|
||||
|
||||
return code == CGE_THREAD_EOK;
|
||||
}
|
||||
|
||||
int CgeConditionBroadcast(CgeCondition *condition, int *result) {
|
||||
int code;
|
||||
|
||||
code = errorToErrorCode(pthread_cond_broadcast(&condition->handle));
|
||||
if (result)
|
||||
*result = code;
|
||||
|
||||
return code == CGE_THREAD_EOK;
|
||||
}
|
||||
Reference in New Issue
Block a user