Changes between Version 38 and Version 39 of LightweightConcurrency


Ignore:
Timestamp:
May 18, 2012 5:03:51 PM (3 years ago)
Author:
kc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LightweightConcurrency

    v38 v39  
    136136The implementation is pretty straight-forward; scheduleSContAction appends the given scont to the back of the list, and yieldControlAction picks an SCont from the front of the list and switches to it. Notice that scheduleSContAction returns while yieldControlAction does not. We expect every user-level thread (SCont) to be associated with a scheduler. Typically, when a new SCont is created, it is immediately associated with a scheduler.
    137137
     138== Building Concurrency Primitives ==
     139
     140Now that we have defined an abstract interface, lets look at how to construct concurrency primitives using the scheduler actions. The implementation of primitives `yield` and `forkIO` using the scheduler actions is shown below.
     141
     142{{{
     143yield :: IO ()
     144yield = atomically $ do
     145  s <- getCurrentSCont
     146  -- Append current SCont to scheduler
     147  ssa <- getScheduleSContAction s
     148  enque :: PTM () <- ssa a
     149  enque
     150  -- Switch to next SCont from scheduler
     151  switchToNext :: PTM () <- getYieldControlAction s
     152  switchToNext
     153
     154forkIO :: IO () -> IO SCont
     155forkIO f = do
     156  ns <- newSCont f
     157  atomically $ do {
     158    s <- getCurrentSCont;
     159    -- Initialize scheduler actions
     160    ssa <- getScheduleSContAction s;
     161    setScheduleSContAction ns ssa;
     162    yca <- getYieldControlAction s;
     163    setYieldControlAction ns yca;
     164    -- Append the new SCont to current SCont's scheduler
     165    appendAct <- ssa ns;
     166    appendAct
     167  }
     168  return ns
     169}}}
     170
    138171== Capabilities and Tasks ==
    139172