Added thread_sleep

This commit is contained in:
Justin Ethier 2015-12-18 23:53:24 -05:00
parent 621fd0abd0
commit 38c1ca7b31
4 changed files with 20 additions and 2 deletions

2
gc.c
View file

@ -17,8 +17,6 @@
//////////////////// ////////////////////
// Global variables // Global variables
static const int NANOSECONDS_PER_MILLISECOND = 1000000;
// Note: will need to use atomics and/or locking to access any // Note: will need to use atomics and/or locking to access any
// variables shared between threads // variables shared between threads
static int gc_color_mark = 1; // Black, is swapped during GC static int gc_color_mark = 1; // Black, is swapped during GC

View file

@ -189,6 +189,7 @@ object memqp(void *,object,list);
object Cyc_spawn_thread(object thunk); object Cyc_spawn_thread(object thunk);
void Cyc_start_thread(gc_thread_data *thd); void Cyc_start_thread(gc_thread_data *thd);
void Cyc_end_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 GC(void *,closure,object*,int);
void Cyc_st_add(void *data, char *frame); 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_91has_91cycle_127;
extern const object primitive_Cyc_91spawn_91thread_67; extern const object primitive_Cyc_91spawn_91thread_67;
extern const object primitive_Cyc_91end_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__87;
extern const object primitive__91; extern const object primitive__91;
extern const object primitive__85; extern const object primitive__85;

View file

@ -43,6 +43,8 @@
necessary if GC is working correctly. */ necessary if GC is working correctly. */
#define GC_SAFETY_CHECKS 1 #define GC_SAFETY_CHECKS 1
// General constants
#define NANOSECONDS_PER_MILLISECOND 1000000
/* Define general object type. */ /* Define general object type. */
typedef void *object; typedef void *object;

View file

@ -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) { void _Cyc_91end_91thread_67(void *data, object cont, object args) {
Cyc_end_thread((gc_thread_data *)data); Cyc_end_thread((gc_thread_data *)data);
return_closcall1(data, cont, boolean_f); } 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) { void __87(void *data, object cont, object args) {
integer_type argc = Cyc_length(data, args); integer_type argc = Cyc_length(data, args);
dispatch(data, argc.value, (function_type)dispatch_sum, cont, cont, 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_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_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_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 _87_primitive = {{0}, primitive_tag, "+", &__87};
static primitive_type _91_primitive = {{0}, primitive_tag, "-", &__91}; static primitive_type _91_primitive = {{0}, primitive_tag, "-", &__91};
static primitive_type _85_primitive = {{0}, primitive_tag, "*", &__85}; 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_91has_91cycle_127 = &Cyc_91has_91cycle_127_primitive;
const object primitive_Cyc_91spawn_91thread_67 = &Cyc_91spawn_91thread_67_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_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__87 = &_87_primitive;
const object primitive__91 = &_91_primitive; const object primitive__91 = &_91_primitive;
const object primitive__85 = &_85_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 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;
}