on master help sign in
Fix 'spawn' to return Maybe PID

Thread creation might fail if there aren't enough resources, so reflect this in the type

commitf0d0ff5708e155d883373fb9b3ba9e87c48df7da authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSat 29 Oct 2016, 10:28 (10 years ago) parentbb3bc16
2 files changed+12−4
libs/base/System/Concurrency/Channels.idr+5−2 rts/idris_rts.c+7−2
libs/base/System/Concurrency/Channels.idr+5−2
@@ -16,10 +16,13 @@ data PID : Type where
1616 MkPID : (pid : Ptr) -> PID
1717
1818||| Spawn a process in a new thread, returning the process ID
19+||| Returns `Nothing` if there are not enough resources to create the new thread
1920export
20−spawn : (process : IO ()) -> IO PID
21+spawn : (process : IO ()) -> IO (Maybe PID)
2122spawn proc = do pid <- fork proc
22− pure (MkPID pid)
23+ if !(nullPtr pid)
24+ then pure Nothing
25+ else pure (Just (MkPID pid))
2326
2427||| Create a channel which connects this process to another process
2528export
rts/idris_rts.c+7−2
@@ -748,9 +748,14 @@ void* vmThread(VM* callvm, func f, VAL arg) {
748748
749749 callvm->processes++;
750750
751− pthread_create(&t, &attr, runThread, td);
751+ int ok = pthread_create(&t, &attr, runThread, td);
752752// usleep(100);
753− return vm;
753+ if (ok == 0) {
754+ return vm;
755+ } else {
756+ terminate(vm);
757+ return NULL;
758+ }
754759}
755760
756761void* idris_stopThread(VM* vm) {