Skip to content

Commit

Permalink
refactor(runtime): start reimplementing the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Apr 27, 2021
1 parent 409095c commit c3fdedd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/runtime-prototype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ easier.

#### Interfaces (a.k.a. "behaviours")

* Joe Armstrong's PhD thesis "Making reliable distributed systems in the
presence of software errors" written in 2003 when he was 53 years old, he
started working on telecom systems in 1981. Quite different from a typical PhD
thesis written by a 20-something year old person with no or very little
experience from industry...
* Joe Armstrong's PhD
[thesis](https://erlang.org/download/armstrong_thesis_2003.pdf) "Making
reliable distributed systems in the presence of software errors" written in
2003 when he was 53 years old, he started working on telecom systems in 1981.
Quite different from a typical PhD thesis written by a 20-something year old
person with no or very little experience from industry...

* Anyway, the thesis can perhaps best be summarised by "the big idea in Erlang
isn't messaging/actors for concurrency, but using interfaces (a.k.a.
Expand Down
50 changes: 35 additions & 15 deletions src/runtime-prototype/src/StuntDouble/EventLoop.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module StuntDouble.EventLoop where

import Control.Monad
import Control.Concurrent
import Control.Concurrent.Async
import Control.Concurrent.STM
import Control.Concurrent.STM.TBQueue

import StuntDouble.Actor
Expand All @@ -10,28 +10,48 @@ import StuntDouble.Reference

------------------------------------------------------------------------

data EventLoopId = EventLoopId

data Event
= Spawn Actor
| Call LocalRef Message
data Command
= Spawn (Message -> Actor)
| Invoke LocalRef Message
| Send RemoteRef Message
| Receive Message
| Quit

data Response
= Receive AsyncRef Message

-- XXX:
type RequestId = Int
type AsyncRef = (Async Message, RequestId)

data Event = Command Command | Response Response

data EventLoopRef = EventLoopRef (Async ())

data LoopState = LoopState
{ loopStateQueue :: TBQueue Event }

------------------------------------------------------------------------

initLoopState :: IO LoopState
initLoopState = do
queue <- newTBQueueIO 128
return (LoopState queue)

makeEventLoop :: IO EventLoopId
makeEventLoop :: IO EventLoopRef
makeEventLoop = do
loopState <- initLoopState
tid <- forkIO $ forever $ undefined loopState
tid' <- forkIO $ forever $ undefined loopState
undefined

handleAdminCommands :: TBQueue Event -> IO ()
handleAdminCommands queue = undefined
-- tid <- forkIO $ forever $ undefined loopState
-- tid' <- forkIO $ forever $ undefined loopState
a <- async (handleEvents loopState)
return (EventLoopRef a)

handleEvents :: LoopState -> IO ()
handleEvents (LoopState queue) = go
where
go = do
e <- atomically (readTBQueue queue)
handleEvent e
go

handleEvent :: Event -> IO ()
handleEvent = undefined

0 comments on commit c3fdedd

Please sign in to comment.