From 1d7419d8511766e05fa24eb41c68bd8287ba3c4a Mon Sep 17 00:00:00 2001 From: "Frank V. Castellucci" Date: Mon, 15 Apr 2019 06:11:54 -0400 Subject: [PATCH] Threads change state and other cleanups --- foidlrtl/fsrc/langcore.foidl | 16 +++++----- foidlrtl/headers/foidlrt.defs | 8 ++--- foidlrtl/src/foidl_work.c | 47 +++++++++++++++++++++-------- tests/selfhosted/fsrc/workfun.foidl | 40 +++++++++++++++--------- 4 files changed, 73 insertions(+), 38 deletions(-) diff --git a/foidlrtl/fsrc/langcore.foidl b/foidlrtl/fsrc/langcore.foidl index 5b7fae2..c41e1e3 100644 --- a/foidlrtl/fsrc/langcore.foidl +++ b/foidlrtl/fsrc/langcore.foidl @@ -167,17 +167,17 @@ func failWith [msg] ; Concurrency functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -func nap [timeout] - foidl_nap: timeout +func nap! [timeout] + foidl_nap!: timeout -func thrd0 [fnref] - foidl_thread0: fnref +func thrd0! [fnref] + foidl_thread0!: fnref -func thrd [fnref argvector] - foidl_thread: fnref argvector +func thrd! [fnref argvector] + foidl_thread!: fnref argvector -func wait [thrdref] - foidl_wait: thrdref +func wait! [thrdref] + foidl_wait!: thrdref ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Math functions diff --git a/foidlrtl/headers/foidlrt.defs b/foidlrtl/headers/foidlrt.defs index b9a38ea..a332587 100644 --- a/foidlrtl/headers/foidlrt.defs +++ b/foidlrtl/headers/foidlrt.defs @@ -155,10 +155,10 @@ func foidl_dispatch0 [fn] ; Concurrency functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -func foidl_nap [timeout] -func foidl_thread [fnref argvector] -func foidl_thread0 [fnref] -func foidl_wait [thrdref] +func foidl_nap! [timeout] +func foidl_thread! [fnref argvector] +func foidl_thread0! [fnref] +func foidl_wait! [thrdref] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Basic Math Functions diff --git a/foidlrtl/src/foidl_work.c b/foidlrtl/src/foidl_work.c index f73e1da..002476d 100644 --- a/foidlrtl/src/foidl_work.c +++ b/foidlrtl/src/foidl_work.c @@ -17,7 +17,7 @@ Sleeps for timeout seconds */ -PFRTAny foidl_nap(PFRTAny timeout) { +PFRTAny foidl_nap_bang(PFRTAny timeout) { PFRTAny res = nil; if(timeout->fclass == scalar_class && timeout->ftype == number_type) { @@ -25,12 +25,13 @@ PFRTAny foidl_nap(PFRTAny timeout) { Sleep(number_toft(timeout)*1000); res = zero; #else - ft timo = number_toft(timeout); - unsigned int ires = sleep(timo); - if(ires == 0) + unsigned int ires = sleep(number_toft(timeout)); + if(ires == 0) { res = zero; - else + } + else { res = foidl_reg_intnum(ires); + } #endif } else { @@ -39,10 +40,14 @@ PFRTAny foidl_nap(PFRTAny timeout) { return res; } +/* + worker0 + Invokes function with no arguments +*/ #ifdef _MSC_VER -DWORD WINAPI worker0(void* arg) +static DWORD WINAPI worker0(void* arg) #else -void *worker0(void *arg) +static void *worker0(void *arg) #endif { PFRTWorker wrk = (PFRTWorker) arg; @@ -56,10 +61,15 @@ void *worker0(void *arg) #endif } +/* + worker + Invokes function with arbitrary arguments +*/ + #ifdef _MSC_VER -DWORD WINAPI worker(void* arg) +static DWORD WINAPI worker(void* arg) #else -void *worker(void *arg) +static void *worker(void *arg) #endif { PFRTWorker wrk = (PFRTWorker) arg; @@ -84,8 +94,12 @@ void *worker(void *arg) #endif } +/* + foidl_thread + Entry point for spawning a worker with arguments +*/ -PFRTAny foidl_thread(PFRTAny funcref, PFRTAny argvector) { +PFRTAny foidl_thread_bang(PFRTAny funcref, PFRTAny argvector) { PFRTWorker wrk = allocWorker((PFRTFuncRef2)funcref); wrk->vector_args = argvector; #ifdef _MSC_VER @@ -97,7 +111,11 @@ PFRTAny foidl_thread(PFRTAny funcref, PFRTAny argvector) { return (PFRTAny) wrk; } -PFRTAny foidl_thread0(PFRTAny funcref) { +/* + foidl_thread0 + Entry point for spawning a worker with no arguments +*/ +PFRTAny foidl_thread0_bang(PFRTAny funcref) { PFRTWorker wrk = allocWorker((PFRTFuncRef2)funcref); #ifdef _MSC_VER HANDLE tid = CreateThread(NULL,0,worker0, wrk, 0, NULL); @@ -108,7 +126,12 @@ PFRTAny foidl_thread0(PFRTAny funcref) { return (PFRTAny) wrk; } -PFRTAny foidl_wait(PFRTAny thrdref) { +/* + foidl_wait + Blocks for worker indefinitly +*/ + +PFRTAny foidl_wait_bang(PFRTAny thrdref) { PFRTAny res = nil; if(thrdref->fclass == function_class && thrdref->ftype == worker_type) { diff --git a/tests/selfhosted/fsrc/workfun.foidl b/tests/selfhosted/fsrc/workfun.foidl index d45534f..45526f4 100644 --- a/tests/selfhosted/fsrc/workfun.foidl +++ b/tests/selfhosted/fsrc/workfun.foidl @@ -18,28 +18,40 @@ module workfun -func sleeper[timeout] - nap: timeout - +; A noarg function, used by 'thrd0:' invocation func noarg[] - sleeper: 1 + nap!: 1 printnl!: "In no arg from thread, returning 'zero'" "zero" +func longloop [] + fold: ^[a b] + @( + nap!: 1 + print!: format: "Evaluating {} `n" [b] + b + ) + nil [1 2 3 4 5 6 7 8 9 0] + "done" + +; A onearg function, used by 'thrd:' invocation func onearg[arg] - sleeper: 1 + nap!: 1 print!: "In one arg with " printnl!: arg "one" func main[argv] - let a [] thrd0: noarg ; Test explicit no args - let b [] thrd0: ^[] +: 1 1 ; Test lambda no args - let c [] thrd: onearg [1] ; Test explicit multi-arg - let d [] thrd: ^[a] +: a 1 [2] ; Test lamda multi-arg - - printnl!: wait: a - printnl!: wait: b - printnl!: wait: c - printnl!: wait: d + let a [] thrd0!: noarg ; Test explicit no args + let b [] thrd0!: ^[] +: 1 1 ; Test lambda no args + let c [] thrd!: onearg [1] ; Test explicit multi-arg + let d [] thrd!: ^[x] +: x 1 [2] ; Test lamda multi-arg + let e [] thrd0!: longloop + + printnl!: wait!: a + printnl!: wait!: b + printnl!: wait!: c + printnl!: wait!: d + printnl!: wait!: e +