Concurrent Programming with Join Patterns via STMs Satnam Singh, Microsoft WG2.8 Estonia
Overview � Nothing clever. � Bait and switch operation. � Join patterns in Comega � Joins encoded with STMs: � Synchronous and asynchronous joins. � Choice. � Dynamic Joins.
Comega asynchronous methods usi ng Syst em ; publ i c cl ass M ai nPr ogr am { publ i c cl ass Ar r aySum m er { publ i c async publ i c async sum um Ar r ay ( i nt [ ] i nt Ar r ay) Ar r ay { i nt sum = 0 ; f or each ( i nt val ue i n i nt Ar r ay) sum += val ue ; Consol e. W r i t eLi ne ( " Sum = " + sum ) ; } } st at i c voi d M ai n( ) { Sum m er = new Ar r aySum m er ( ) ; Sum m er . sum Ar r ay ( new i nt [ ] { 1, 0, 6, 6, 1, 9, 6, 6} ) ; Sum m er . sum Ar r ay ( new i nt [ ] { 3, 1, 4, 1, 5, 9, 2, 6} ) ; Consol e. W r i t eLi ne ( " M ai n m et hod done. " ) ; } }
Comega chords usi ng Syst em ; publ i c cl ass M ai nPr ogr am { publ i c cl ass Buf f er { publ i c async publ i c async Put Put ( i nt val ue) ; publ i c i nt publ i c i nt G G et ( ) & Put ( i nt et ( ) & Put ( i nt val ue) val ue) { r et ur n val ue ; } } st at i c voi d M ai n( ) { buf = new Buf f er ( ) ; buf . Put ( 42) ; buf . Put ( 66) ; Consol e. W r i t eLi ne ( buf . G et ( ) + " " + buf . G et ( ) ) ; } }
“STM”s in Haskell dat a STM a i nst ance M onad STM - - M onads suppor t " do" not at i on and sequenci ng - - Except i ons t hr ow : : Except i on - > STM a cat ch : : STM a - > ( Except i on- >STM a) - > STM a - - Runni ng STM com put at i ons at om i cal l y : : STM a - > I O a r et r y : : STM a or El se : : STM a - > STM a - > STM a - - Tr ansact i onal var i abl es dat a TVar a newTVar : : a - > STM ( TVar a) r eadTVar : : TVar a - > STM a wr i t eTVar : : TVar a - > a - > STM ( )
Join2 m odul e M ai n wher e i m por t Cont r ol . Concur r ent i m por t Cont r ol . Concur r ent . STM j oi n2 : : TChan a - > TChan b - > I O ( a, b) j oi n2 chanA chanB = at om i cal l y ( do a <- r eadTChan chanA b <- r eadTChan chanB r et ur n ( a, b) ) t askA : : TChan I nt - > TChan I nt - > I O ( ) t askA chan1 chan2 = do ( v1, v2) <- j oi n2 chan1 chan2 put St r Ln ( " t askA got : " ++ show ( v1, v2) ) m ai n = do chanA <- at om i cal l y newTChan chanB <- at om i cal l y newTChan at om i cal l y ( wr i t eTChan chanA 42) at om i cal l y ( wr i t eTChan chanB 75) t askA chanA chanB
One-Shot Synchronous Join ( &) : : TChan a - > TChan b - > STM ( a, b) ( &) chan1 chan2 = do a <- r eadTChan chan1 b <- r eadTChan chan2 r et ur n ( a, b) ( >>>) : : STM a - > ( a - > I O b) - > I O b ( >>>) j oi nPat t er n handl er = do r esul t s <- at om i cal l y j oi nPat t er n handl er r esul t s exam pl e chan1 chan2 = chan1 & chan2 >>> >>> \ ( a, b) - > put St r Ln ( show ( a, b) )
Puzzle m ai n : : I O ( ) m ai n = do do chan1 <- at om i cal l y $ newTChan at om i cal l y $ wr i t eTChan chan1 42 at om i cal l y $ wr i t eTChan chan1 74 chan1 & chan1 >>> \ ( a, b) - > put St r Ln ( show ( a, b) )
Repeating Asynchronous Join ( >! >) : : STM a - > ( a - > I O ( ) ) - > I O ( ) ( >! >) j oi ns cont = do f or kI O ( asyncJoi nLoop j oi ns cont ) r et ur n ( ) - - di scar d t hr ead I D asyncJoinLoop :: (STM a) -> (a -> IO ()) -> IO () asyncJoi nLoop j oi nPat t er n handl er = do j oi nPat t er n >>> f or kI O . handl er asyncJoi nLoop j oi nPat t er n handl er exam pl e chan1 chan2 = chan1 & chan2 >! > \ ( a, b) - > put St r Ln ( show ( ( a, b) ) )
Exploiting Overloading cl ass cl ass Joi nabl e t 1 t 2 wher e wher e ( &) : : t 1 a - > t 2 b - > STM ( a, b) i nst ance i nst ance Joi nabl e TChan TChan wher e wher e ( &) = j oi n2 i nst ance i nst ance Joi nabl e TChan STM wher e wher e ( &) = j oi n2b i nst ance i nst ance Joi nabl e STM TChan wher e wher e ( &) a b = do ( x, y) <- j oi n2b b a r et ur n ( y, x) chan1 & chan2 & chan3 >>> \ ( ( a, b) , c) - > put St r Ln ( show ( a, b, c) )
Biased Synchronous Choice ( | +| ) : : ( STM a, a - > I O c) - > ( STM b, b - > I O c) - > I O c ( | +| ) ( j oi na, act i on1) ( j oi nb, act i on2) = do do i o <- at om i cal l y ( do do a <- j oi na r et ur n ( act i on1 a) ` or El se` do do b <- j oi nb r et ur n ( act i on2 b) ) i o (chan1 & chan2 & chan3, \ ((a,b),c) -> putStrLn (show (a,b,c))) |+ | (chan1 & chan2, \ (a,b) -> putStrLn (show (a,b)))
Dynamic Joins exam pl e num Sensor s num Sensor s chan1 chan2 chan3 = i f i f num Sensor s = 2 t hen t hen chan1 & chan2 >! > \ ( a, b) - > put St r Ln ( show ( ( a, b) ) ) el se el se chan1 & chan2 & chan3 >! > \ ( a, ( b, c) ) - > put St r Ln ( show ( ( a, b, c) ) )
Conditional Joins ( ??) : : TChan a - > ( a - > Bool ) - > STM a ( ??) chan pr edi cat e = do do val ue <- r eadTChan chan i f i f pr edi cat e val ue t hen t hen r et ur n val ue el se el se r et r y ( chan1 ?? ?? \ x - > x > 3) & chan2 >>> \ ( a, b) - > put St r Ln ( show ( a, b) )
Summary and Questions � “Free” joins encoded nicely in terms of STMs. � Model for understanding join patterns in terms of STMs. � A good literal implementation (?) � Parallel execution? � Joins as statements instead of declarations. � Other work: JSR-166 library � What are joins good for anyway?
Conditional Joins ( ?) : : TChan a - > Bool - > STM a ( ?) chan pr edi cat e = i f i f pr edi cat e t hen t hen r eadTChan chan el se el se r et r y ( chan1 ? cond) & chan2 >>> \ ( a, b) - > put St r Ln ( show ( a, b) )
Conditional Joins ( ?) : : TChan a - > STM Bool - > STM a ( ?) chan pr edi cat e = do do cond <- pr edi cat e i f i f cond t hen t hen r eadTChan chan el se el se r et r y
Backup
Backup
Recommend
More recommend