Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hl_blocking calls in blocking semaphore and condition functions. #664

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/std/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,18 @@ HL_PRIM hl_semaphore *hl_semaphore_alloc(int value) {
HL_PRIM void hl_semaphore_acquire(hl_semaphore *sem) {
# if !defined(HL_THREADS)
# elif defined(HL_WIN)
hl_blocking(true);
WaitForSingleObject(sem->sem, INFINITE);
hl_blocking(false);
# else
# ifdef __APPLE__
hl_blocking(true);
dispatch_semaphore_wait(sem->sem, DISPATCH_TIME_FOREVER);
hl_blocking(false);
# else
hl_blocking(true);
sem_wait(&sem->sem);
hl_blocking(false);
# endif
# endif
}
Expand All @@ -233,16 +239,24 @@ HL_PRIM bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
# if !defined(HL_THREADS)
return true;
# elif defined(HL_WIN)
return WaitForSingleObject(sem->sem,
hl_blocking(true);
bool ret = WaitForSingleObject(sem->sem,
timeout ? (DWORD)((FLOAT)timeout->v.d * 1000.0)
: 0) == WAIT_OBJECT_0;
hl_blocking(false);
return ret;
# else
# ifdef __APPLE__
return dispatch_semaphore_wait(
hl_blocking(true);
bool ret = dispatch_semaphore_wait(
sem->sem, dispatch_time(DISPATCH_TIME_NOW,
(int64_t)((timeout ? timeout->v.d : 0) *
1000 * 1000 * 1000))) == 0;
hl_blocking(false);
return ret;
# else
hl_blocking(true);
bool ret;
if (timeout) {
struct timeval tv;
struct timespec t;
Expand All @@ -256,11 +270,12 @@ HL_PRIM bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
delta -= idelta2 * 1e9;
t.tv_sec = tv.tv_sec + idelta + idelta2;
t.tv_nsec = (long)delta;
return sem_timedwait(&sem->sem, &t) == 0;
ret = sem_timedwait(&sem->sem, &t) == 0;
} else {

return sem_trywait(&sem->sem) == 0;
ret = sem_trywait(&sem->sem) == 0;
}
hl_blocking(false);
return ret;
# endif
# endif
}
Expand Down Expand Up @@ -333,9 +348,13 @@ HL_PRIM hl_condition *hl_condition_alloc() {
HL_PRIM void hl_condition_acquire(hl_condition *cond) {
# if !defined(HL_THREADS)
# elif defined(HL_WIN)
hl_blocking(true);
EnterCriticalSection(&cond->cs);
hl_blocking(false);
# else
hl_blocking(true);
pthread_mutex_lock(&cond->mutex);
hl_blocking(false);
# endif
}

Expand All @@ -360,18 +379,24 @@ HL_PRIM void hl_condition_release(hl_condition *cond) {
HL_PRIM void hl_condition_wait(hl_condition *cond) {
# if !defined(HL_THREADS)
# elif defined(HL_WIN)
hl_blocking(true);
SleepConditionVariableCS(&cond->cond, &cond->cs, INFINITE);
hl_blocking(false);
# else
hl_blocking(true);
pthread_cond_wait(&cond->cond, &cond->mutex);
hl_blocking(false);
# endif
}

HL_PRIM bool hl_condition_timed_wait(hl_condition *cond, double timeout) {
# if !defined(HL_THREADS)
return true;
# elif defined(HL_WIN)
hl_blocking(true);
SleepConditionVariableCS(&cond->cond, &cond->cs,
(DWORD)((FLOAT)timeout * 1000.0));
hl_blocking(false);
return true;
# else
struct timeval tv;
Expand All @@ -386,7 +411,10 @@ HL_PRIM bool hl_condition_timed_wait(hl_condition *cond, double timeout) {
delta -= idelta2 * 1e9;
t.tv_sec = tv.tv_sec + idelta + idelta2;
t.tv_nsec = (long)delta;
return pthread_cond_timedwait(&cond->cond, &cond->mutex, &t) == 0;
hl_blocking(true);
bool ret = pthread_cond_timedwait(&cond->cond, &cond->mutex, &t) == 0;
hl_blocking(false);
return ret;
# endif
}

Expand Down