on master help sign in
Fix 'spawn' to return Maybe PID
f0d0ff5Edwin Brady
libs/base/System/Concurrency/Channels.idr+5−2
@@ -16,10 +16,13 @@ data PID : Type where
16 MkPID : (pid : Ptr) -> PID
16 MkPID : (pid : Ptr) -> PID
17
17
18||| Spawn a process in a new thread, returning the process ID
18||| 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
19export
20export
20−spawn : (process : IO ()) -> IO PID
21+spawn : (process : IO ()) -> IO (Maybe PID)
21spawn proc = do pid <- fork proc
22spawn proc = do pid <- fork proc
23+ if !(nullPtr pid)
24+ then pure Nothing
22− pure (MkPID pid)
25+ else pure (Just (MkPID pid))
23
26
24||| Create a channel which connects this process to another process
27||| Create a channel which connects this process to another process
25export
28export
rts/idris_rts.c+7−2
@@ -748,9 +748,14 @@ void* vmThread(VM* callvm, func f, VAL arg) {
748
748
749 callvm->processes++;
749 callvm->processes++;
750
750
751− pthread_create(&t, &attr, runThread, td);
751+ int ok = pthread_create(&t, &attr, runThread, td);
752// usleep(100);
752// usleep(100);
753+ if (ok == 0) {
753− return vm;
754+ return vm;
755+ } else {
756+ terminate(vm);
757+ return NULL;
758+ }
754}
759}
755
760
756void* idris_stopThread(VM* vm) {
761void* idris_stopThread(VM* vm) {