diff --git a/gc.c b/gc.c index be4bc20d..04eb126a 100644 --- a/gc.c +++ b/gc.c @@ -17,8 +17,6 @@ //////////////////// // Global variables -static const int NANOSECONDS_PER_MILLISECOND = 1000000; - // Note: will need to use atomics and/or locking to access any // variables shared between threads static int gc_color_mark = 1; // Black, is swapped during GC diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index 30cbe7a1..7a2aa7a4 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -189,6 +189,7 @@ object memqp(void *,object,list); object Cyc_spawn_thread(object thunk); void Cyc_start_thread(gc_thread_data *thd); void Cyc_end_thread(gc_thread_data *thd); +object Cyc_thread_sleep(void *data, object timeout); void GC(void *,closure,object*,int); void Cyc_st_add(void *data, char *frame); @@ -228,6 +229,7 @@ extern const object primitive_Cyc_91cvar_127; extern const object primitive_Cyc_91has_91cycle_127; extern const object primitive_Cyc_91spawn_91thread_67; extern const object primitive_Cyc_91end_91thread_67; +extern const object primitive_Cyc_91thread_91sleep_67; extern const object primitive__87; extern const object primitive__91; extern const object primitive__85; diff --git a/include/cyclone/types.h b/include/cyclone/types.h index 26e05d82..7d3b7006 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -43,6 +43,8 @@ necessary if GC is working correctly. */ #define GC_SAFETY_CHECKS 1 +// General constants +#define NANOSECONDS_PER_MILLISECOND 1000000 /* Define general object type. */ typedef void *object; diff --git a/runtime.c b/runtime.c index a908eecb..77a4132d 100644 --- a/runtime.c +++ b/runtime.c @@ -1709,6 +1709,9 @@ void _Cyc_91spawn_91thread_67(void *data, object cont, object args) { void _Cyc_91end_91thread_67(void *data, object cont, object args) { Cyc_end_thread((gc_thread_data *)data); return_closcall1(data, cont, boolean_f); } +void _Cyc_91thread_91sleep_67(void *data, object cont, object args) { + Cyc_check_num_args(data, "Cyc-thread-sleep!", 1, args); + return_closcall1(data, cont, Cyc_thread_sleep(data, car(args))); } void __87(void *data, object cont, object args) { integer_type argc = Cyc_length(data, args); dispatch(data, argc.value, (function_type)dispatch_sum, cont, cont, args); } @@ -2900,6 +2903,7 @@ static primitive_type Cyc_91cvar_127_primitive = {{0}, primitive_tag, "Cyc-cvar? static primitive_type Cyc_91has_91cycle_127_primitive = {{0}, primitive_tag, "Cyc-has-cycle?", &_Cyc_91has_91cycle_127}; static primitive_type Cyc_91spawn_91thread_67_primitive = {{0}, primitive_tag, "Cyc-spawn-thread!", &_Cyc_91spawn_91thread_67}; static primitive_type Cyc_91end_91thread_67_primitive = {{0}, primitive_tag, "Cyc-end-thread!", &_Cyc_91end_91thread_67}; +static primitive_type Cyc_91thread_91sleep_67_primitive = {{0}, primitive_tag, "Cyc-thread-sleep!", &_Cyc_91thread_91sleep_67}; static primitive_type _87_primitive = {{0}, primitive_tag, "+", &__87}; static primitive_type _91_primitive = {{0}, primitive_tag, "-", &__91}; static primitive_type _85_primitive = {{0}, primitive_tag, "*", &__85}; @@ -3019,6 +3023,7 @@ const object primitive_Cyc_91cvar_127 = &Cyc_91cvar_127_primitive; const object primitive_Cyc_91has_91cycle_127 = &Cyc_91has_91cycle_127_primitive; const object primitive_Cyc_91spawn_91thread_67 = &Cyc_91spawn_91thread_67_primitive; const object primitive_Cyc_91end_91thread_67 = &Cyc_91end_91thread_67_primitive; +const object primitive_Cyc_91thread_91sleep_67 = &Cyc_91thread_91sleep_67_primitive; const object primitive__87 = &_87_primitive; const object primitive__91 = &_91_primitive; const object primitive__85 = &_85_primitive; @@ -3203,3 +3208,14 @@ void Cyc_end_thread(gc_thread_data *thd) pthread_exit(NULL); // For now, just a proof of concept } +// For now, accept a number of milliseconds to sleep +object Cyc_thread_sleep(void *data, object timeout) +{ + struct timespec tim; + Cyc_check_num(data, timeout); + tim.tv_sec = 0; + tim.tv_nsec = ((integer_type *)timeout)->value * NANOSECONDS_PER_MILLISECOND; + nanosleep(&tim, NULL); + return boolean_t; +} +