Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,11 @@
import Control.Concurrent (threadDelay, forkIO)
import Control.Concurrent.SampleVar
-- An Event is defined as a SampleVar with no data.
-- http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-SampleVar.html
newtype Event = Event (SampleVar ())
newEvent = fmap Event (newEmptySampleVar)
signalEvent (Event sv) = writeSampleVar sv ()
resetEvent (Event sv) = emptySampleVar sv
waitEvent (Event sv) = readSampleVar sv

View file

@ -0,0 +1,11 @@
main = do e <- newEvent
forkIO (waitTask e)
putStrLn "[1] Waiting 1 second..."
threadDelay 1000000 {- µs -}
putStrLn "[1] Signaling event."
signalEvent e
threadDelay 1000000 {- µs -} -- defer program exit for reception
waitTask e = do putStrLn "[2] Waiting for event..."
waitEvent e
putStrLn "[2] Received event."

View file

@ -0,0 +1,20 @@
record Event(cond, value)
procedure main()
event := Event(condvar())
t1 := thread {
write("Task one waiting for event....")
critical event.cond: while /(event.value) do wait(event.cond)
write("Task one received event.")
}
t2 := thread {
write("Task two waiting for event....")
critical event.cond: while /(event.value) do wait(event.cond)
write("Task two received event.")
}
delay(1000) # Let main thread post the event.
event.value := "yes"
write("Signalling event.")
signal(event.cond,0)
every wait(t1|t2)
end