2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
Lets just make sure we can start a thread
|
|
|
|
|
|
|
|
```unison
|
|
|
|
otherThread : '{io2.IO}()
|
|
|
|
otherThread = 'let
|
|
|
|
watch "I'm the other Thread" ()
|
|
|
|
|
|
|
|
testBasicFork : '{io2.IO} [Result]
|
|
|
|
testBasicFork = 'let
|
|
|
|
test = 'let
|
|
|
|
watch "I'm the parent thread" ()
|
2021-02-02 01:10:22 +03:00
|
|
|
threadId = .builtin.io2.IO.forkComp otherThread
|
2021-01-14 01:20:57 +03:00
|
|
|
emit (Ok "created thread")
|
|
|
|
|
|
|
|
runTest test
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```ucm
|
|
|
|
|
|
|
|
I found and typechecked these definitions in scratch.u. If you
|
|
|
|
do an `add` or `update`, here's how your codebase would
|
|
|
|
change:
|
|
|
|
|
|
|
|
⍟ These new definitions are ok to `add`:
|
|
|
|
|
|
|
|
otherThread : '{io2.IO} ()
|
|
|
|
testBasicFork : '{io2.IO} [Result]
|
|
|
|
|
|
|
|
```
|
|
|
|
See if we can get another thread to stuff a value into a MVar
|
|
|
|
|
|
|
|
```unison
|
2021-02-26 20:44:12 +03:00
|
|
|
thread1 : Nat -> MVar Nat -> '{io2.IO}()
|
|
|
|
thread1 x mv = 'let
|
2021-01-14 01:20:57 +03:00
|
|
|
go = 'let
|
2021-02-24 10:56:04 +03:00
|
|
|
put mv (increment x)
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
match (toEither go) with
|
2021-01-29 00:57:03 +03:00
|
|
|
Left (Failure _ t _) -> watch t ()
|
2021-01-14 01:20:57 +03:00
|
|
|
_ -> ()
|
|
|
|
|
|
|
|
|
|
|
|
testBasicMultiThreadMVar : '{io2.IO} [Result]
|
|
|
|
testBasicMultiThreadMVar = 'let
|
|
|
|
test = 'let
|
2021-02-26 20:44:12 +03:00
|
|
|
mv = !newEmpty
|
|
|
|
.builtin.io2.IO.forkComp (thread1 10 mv)
|
2021-02-24 10:56:04 +03:00
|
|
|
next = take mv
|
|
|
|
expectU "other thread should have incremented" 11 next
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
runTest test
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```ucm
|
|
|
|
|
|
|
|
I found and typechecked these definitions in scratch.u. If you
|
|
|
|
do an `add` or `update`, here's how your codebase would
|
|
|
|
change:
|
|
|
|
|
|
|
|
⍟ These new definitions are ok to `add`:
|
|
|
|
|
|
|
|
testBasicMultiThreadMVar : '{io2.IO} [Result]
|
2021-02-26 20:44:12 +03:00
|
|
|
thread1 : Nat -> MVar Nat -> '{io2.IO} ()
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
```ucm
|
|
|
|
.> add
|
|
|
|
|
|
|
|
⍟ I've added these definitions:
|
|
|
|
|
|
|
|
testBasicMultiThreadMVar : '{io2.IO} [Result]
|
2021-02-26 20:44:12 +03:00
|
|
|
thread1 : Nat -> MVar Nat -> '{io2.IO} ()
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
.> io.test testBasicMultiThreadMVar
|
|
|
|
|
|
|
|
New test results:
|
|
|
|
|
|
|
|
◉ testBasicMultiThreadMVar other thread should have incremented
|
|
|
|
|
|
|
|
✅ 1 test(s) passing
|
|
|
|
|
|
|
|
Tip: Use view testBasicMultiThreadMVar to view the source of a
|
|
|
|
test.
|
|
|
|
|
|
|
|
```
|
|
|
|
```unison
|
|
|
|
sendingThread: Nat -> MVar Nat -> '{io2.IO}()
|
|
|
|
sendingThread toSend mv = 'let
|
|
|
|
go = 'let
|
2021-02-24 10:56:04 +03:00
|
|
|
put mv (increment toSend)
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
match (toEither go) with
|
2021-01-29 00:57:03 +03:00
|
|
|
Left (Failure _ t _) -> watch t ()
|
2021-01-14 01:20:57 +03:00
|
|
|
_ -> ()
|
|
|
|
|
|
|
|
|
|
|
|
receivingThread: MVar Nat -> MVar Text -> '{io2.IO}()
|
|
|
|
receivingThread recv send = 'let
|
|
|
|
go = 'let
|
2021-02-24 10:56:04 +03:00
|
|
|
recvd = take recv
|
|
|
|
put send (toText recvd)
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
match (toEither go) with
|
2021-01-29 00:57:03 +03:00
|
|
|
Left (Failure _ t _) -> watch t ()
|
2021-01-14 01:20:57 +03:00
|
|
|
_ -> ()
|
|
|
|
|
|
|
|
testTwoThreads: '{io2.IO}[Result]
|
|
|
|
testTwoThreads = 'let
|
|
|
|
test = 'let
|
|
|
|
send = !MVar.newEmpty
|
|
|
|
recv = !MVar.newEmpty
|
|
|
|
|
2021-02-02 01:10:22 +03:00
|
|
|
.builtin.io2.IO.forkComp (sendingThread 6 send)
|
|
|
|
.builtin.io2.IO.forkComp (receivingThread send recv)
|
2021-01-14 01:20:57 +03:00
|
|
|
|
2021-02-24 10:56:04 +03:00
|
|
|
recvd = take recv
|
2021-01-14 01:20:57 +03:00
|
|
|
|
2021-02-24 10:56:04 +03:00
|
|
|
expectU "" "7" recvd
|
2021-01-14 01:20:57 +03:00
|
|
|
|
|
|
|
runTest test
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
```ucm
|
|
|
|
|
|
|
|
I found and typechecked these definitions in scratch.u. If you
|
|
|
|
do an `add` or `update`, here's how your codebase would
|
|
|
|
change:
|
|
|
|
|
|
|
|
⍟ These new definitions are ok to `add`:
|
|
|
|
|
|
|
|
receivingThread : MVar Nat -> MVar Text -> '{io2.IO} ()
|
|
|
|
sendingThread : Nat -> MVar Nat -> '{io2.IO} ()
|
2021-02-26 20:44:12 +03:00
|
|
|
(also named thread1)
|
2021-01-14 01:20:57 +03:00
|
|
|
testTwoThreads : '{io2.IO} [Result]
|
|
|
|
|
|
|
|
```
|
|
|
|
```ucm
|
|
|
|
.> add
|
|
|
|
|
|
|
|
⍟ I've added these definitions:
|
|
|
|
|
|
|
|
receivingThread : MVar Nat -> MVar Text -> '{io2.IO} ()
|
|
|
|
sendingThread : Nat -> MVar Nat -> '{io2.IO} ()
|
2021-02-26 20:44:12 +03:00
|
|
|
(also named thread1)
|
2021-01-14 01:20:57 +03:00
|
|
|
testTwoThreads : '{io2.IO} [Result]
|
|
|
|
|
|
|
|
.> io.test testTwoThreads
|
|
|
|
|
|
|
|
New test results:
|
|
|
|
|
|
|
|
◉ testTwoThreads
|
|
|
|
|
|
|
|
✅ 1 test(s) passing
|
|
|
|
|
|
|
|
Tip: Use view testTwoThreads to view the source of a test.
|
|
|
|
|
|
|
|
```
|