Skip to content
Merged
Show file tree
Hide file tree
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
74 changes: 74 additions & 0 deletions libs/std/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#else
# include <pthread.h>
# include <sys/time.h>
# include <time.h>
typedef struct _vlock {
pthread_mutex_t lock;
pthread_cond_t cond;
Expand Down Expand Up @@ -683,7 +684,19 @@ static void _cond_init(vcondition *cond) {
InitializeConditionVariable(&cond->cond);
#else
pthread_mutex_init(&cond->lock, NULL);
# ifdef NEKO_MAC
// macOS has no pthread_condattr_setclock; _cond_timed_wait uses a
// relative timeout there instead
pthread_cond_init(&cond->cond, NULL);
# else
// measure timed waits on the monotonic clock, so that changes to the
// system time don't shorten or lengthen them
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&cond->cond, &attr);
pthread_condattr_destroy(&attr);
# endif
#endif
}

Expand Down Expand Up @@ -719,6 +732,53 @@ static void _cond_wait(vcondition *cond) {
#endif
}

// Upper bound for timed waits in seconds (about 31 years), so that deadlines
// and millisecond counts do not overflow. Non-positive and NaN timeouts become 0.
#define MAX_WAIT_SECONDS 1e9

static double _clamp_wait_timeout(double timeout) {
if (!(timeout > 0))
return 0;
if (timeout > MAX_WAIT_SECONDS)
return MAX_WAIT_SECONDS;
return timeout;
}

static bool _cond_timed_wait(vcondition *cond, double timeout) {
#ifdef NEKO_WINDOWS
double ms = _clamp_wait_timeout(timeout) * 1000.0;
// INFINITE (0xFFFFFFFF) would mean no timeout at all, so waits of more than
// about 49.7 days are cut short
bool capped = ms >= (double)(INFINITE - 1);
DWORD dms = capped ? INFINITE - 1 : (DWORD)ms;
if (SleepConditionVariableSRW(&cond->cond, &cond->lock, dms, 0))
return true;
// a capped wait that runs out did not reach the requested timeout, so it
// counts as a spurious wakeup. Other errors return false like on POSIX.
return capped && GetLastError() == ERROR_TIMEOUT;
#else
struct timespec t;
double delta = _clamp_wait_timeout(timeout);
long long sec = (long long)delta;
long long nsec = (long long)((delta - (double)sec) * 1e9);
# ifdef NEKO_MAC
t.tv_sec = (time_t)sec;
t.tv_nsec = (long)nsec;
return pthread_cond_timedwait_relative_np(&cond->cond, &cond->lock, &t) == 0;
# else
struct timespec now;
// the condition variable uses CLOCK_MONOTONIC, see _cond_init
clock_gettime(CLOCK_MONOTONIC, &now);
nsec += now.tv_nsec;
sec += now.tv_sec + nsec / 1000000000;
nsec %= 1000000000;
t.tv_sec = (time_t)sec;
t.tv_nsec = (long)nsec;
return pthread_cond_timedwait(&cond->cond, &cond->lock, &t) == 0;
# endif
#endif
}

static void _cond_signal(vcondition *cond) {
#ifdef NEKO_WINDOWS
WakeConditionVariable(&cond->cond);
Expand Down Expand Up @@ -781,6 +841,19 @@ static value cond_wait(value *cond) {
return val_null;
}

/**
cond_timed_wait : 'condition -> timeout:number -> bool
<doc>
Waits on the condition variable for at most [timeout] seconds.
Returns false if the timeout expired, true otherwise.
</doc>
**/
static value cond_timed_wait(value cond, value timeout) {
val_check_kind(cond, k_condition);
val_check(timeout, number);
return alloc_bool(_cond_timed_wait(val_cond(cond), val_number(timeout)));
}

static value cond_signal(value *cond) {
val_check_kind(cond, k_condition);
_cond_signal(val_cond(cond));
Expand All @@ -798,5 +871,6 @@ DEFINE_PRIM(cond_acquire, 1)
DEFINE_PRIM(cond_try_acquire, 1)
DEFINE_PRIM(cond_release, 1)
DEFINE_PRIM(cond_wait, 1)
DEFINE_PRIM(cond_timed_wait, 2)
DEFINE_PRIM(cond_signal, 1)
DEFINE_PRIM(cond_broadcast, 1)
20 changes: 20 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ set_tests_properties(run_goto.n PROPERTIES
PASS_REGULAR_EXPRESSION "startcontinue1continue2end"
FIXTURES_SETUP build_goto.n
)

add_test(NAME build_cond_timed_wait.n
COMMAND nekoc -o ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/ ${CMAKE_SOURCE_DIR}/tests/cond_timed_wait.neko
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

set_tests_properties(build_cond_timed_wait.n PROPERTIES
FIXTURES_SETUP cond_timed_wait
)

add_test(NAME run_cond_timed_wait.n
COMMAND nekovm tests/cond_timed_wait.n
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

set_tests_properties(run_cond_timed_wait.n PROPERTIES
PASS_REGULAR_EXPRESSION "timeout:false,true;held:false;nonpositive:false,false,false,false;huge:true,false,true;"
FIXTURES_REQUIRED cond_timed_wait
Comment thread
Simn marked this conversation as resolved.
Resolved
TIMEOUT 60
)
33 changes: 33 additions & 0 deletions tests/cond_timed_wait.neko
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Tests std@cond_timed_wait: timing out, keeping the mutex, non-positive
// timeouts and huge timeouts that are signaled.
// Closures capture locals by value, so shared state lives in an array.
var create = $loader.loadprim("std@cond_create",0);
var acquire = $loader.loadprim("std@cond_acquire",1);
var try_acquire = $loader.loadprim("std@cond_try_acquire",1);
var release = $loader.loadprim("std@cond_release",1);
var timed_wait = $loader.loadprim("std@cond_timed_wait",2);
var signal = $loader.loadprim("std@cond_signal",1);
var thread_create = $loader.loadprim("std@thread_create",2);
var sleep = $loader.loadprim("std@sys_sleep",1);
var time = $loader.loadprim("std@sys_time",0);
var lock_create = $loader.loadprim("std@lock_create",0);
var lock_wait = $loader.loadprim("std@lock_wait",2);
var lock_release = $loader.loadprim("std@lock_release",1);

var c = create();
acquire(c);
var t = time();
var r = timed_wait(c, 0.1);
$print("timeout:", r, ",", time() - t >= 0.09, ";");
var l = lock_create();
var st = $array(null, false);
thread_create(function(_) { st[0] = try_acquire(c); if( st[0] ) release(c); lock_release(l); }, null);
lock_wait(l, null);
$print("held:", st[0], ";");
$print("nonpositive:", timed_wait(c, 0.0), ",", timed_wait(c, -1), ",", timed_wait(c, 0.0/0.0), ",", timed_wait(c, 0), ";");
thread_create(function(_) { sleep(0.1); acquire(c); st[1] = true; signal(c); release(c); }, null);
var timedOut = false;
t = time();
while( $not(st[1]) && $not(timedOut) ) if( $not(timed_wait(c, 1000000000000.0)) ) timedOut = true;
$print("huge:", st[1], ",", timedOut, ",", time() - t < 5, ";");
release(c);
Loading