Skip to content

Commit

Permalink
Threads change state and other cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC01 committed Apr 15, 2019
1 parent 5a0d3d8 commit 1d7419d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 38 deletions.
16 changes: 8 additions & 8 deletions foidlrtl/fsrc/langcore.foidl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions foidlrtl/headers/foidlrt.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 35 additions & 12 deletions foidlrtl/src/foidl_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
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) {
#ifdef _MSC_VER
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 {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
40 changes: 26 additions & 14 deletions tests/selfhosted/fsrc/workfun.foidl
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 1d7419d

Please sign in to comment.