Changes between Version 41 and Version 42 of LightweightConcurrency


Ignore:
Timestamp:
May 18, 2012 8:17:45 PM (2 years ago)
Author:
kc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LightweightConcurrency

    v41 v42  
    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  
    140 Now 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. Notice that the type signatures are similar to their counterparts in Control.Concurrent. 
    141  
    142 {{{ 
    143 ------ Helper Definitions ------ 
     138== User-level concurrency == 
     139 
     140Now that we have defined an abstract interface, lets look at how to construct user-level concurrency primitives using the scheduler actions.  
     141 
     142=== Schedulers === 
     143 
     144Since our first goal is to implement GHC's concurrency support in Haskell, let us start with`forkIO` and `yield`. These two primitives are sufficient for a simple cooperatively scheduled lightweight thread system. In order to make the presentation cleaner, assume that we have the following helper functions. 
     145 
     146{{{ 
    144147getSSA = getScheduleSContAction 
    145148setSSA = setScheduleSContAction 
    146149getYCA = getYieldControlAction 
    147150setYCA = setYieldControlAction 
    148  
     151}}} 
     152 
     153{{{ 
    149154yield :: IO () 
    150155yield = atomically $ do 
     
    157162  switchToNext :: PTM () <- getYCA s 
    158163  switchToNext 
    159  
     164}}} 
     165 
     166{{{ 
    160167forkIO :: IO () -> IO SCont 
    161168forkIO f = do 
    162   ns <- newSCont f 
     169  -- Switch to next thread after completion 
     170  let epilogue = atomically $ do { 
     171    sc <- getCurrentSCont; 
     172    setSContSwitchReason Completed; 
     173    switchToNext <- getYCA sc; 
     174    switchToNext 
     175  } 
     176  ns <- newSCont (f >> epilogue)   
    163177  atomically $ do { 
    164178    s <- getCurrentSCont; 
     
    175189}}} 
    176190 
     191Notice that the type signatures are similar to their counterparts in Control.Concurrent. 
     192 
     193=== MVars === 
     194 
    177195== Capabilities and Tasks == 
    178196