We describe Triveni, a framework and API for integrating threads and events. The design of Triveni is based on an algebra, including preemption combinators, of processes. Triveni is compatible with existing threads standards, such as Pthreads and Java thre
Thecombinatorinthethermostatimplementsan“eventloop”;thebodyoftheloopterminatesafterhan-dlinganyeventandrestartswiththenextevent.Thebodyoftheloopisaparallelcomposition(usingthecom-binator)oftwoprocesses.The rstprocessrespondsif
thecurrenteventisoftheform
(i.e.,aphysi-caltemperaturereading);onanyotherevent,itterminatessilently.Itissimilarforthesecondprocessandeventsof
form
.Thus,thebodyoftheloopisessen-tiallyaselectionconstructontheinputevents,
.Inbothparallelcomponents,twothings
happenonreceiptofthespeci edevent:anassignmenttakesplaceandaneventisemittedtocontrolaheater.Theassignmentisanaction,writtenbetweenbraces,andmayingeneralbeanycodeinthehostprogramminglanguage(typicallysomethingthatterminatesquickly).The
combinatoremitsanevent.Eventsaredeliveredeven-tually(andsimultaneously)toallinterestedlistenersandtheemittingprocessterminates.TrivenithusdistinguisheseventemissionfromarbitraryJavaactions.
Athermostatisattachedtoanof ceI/Osimplybycom-posingtheminparallel,yieldingatemperature-stableof- ceprocessasillustratedabove.Theparallelcomposi-tionautomaticallyensuresthatthe
,,andeventsaretransmittedbetweenthetwosubpro-cesses.Inthiscase,thesethreeeventsarehiddenwiththe
combinatorsothattheyarenotaccessibleexternallyaseitherinputsoroutputs,asshowninthediagramabove.Theoccupantofanof ceshouldhavemanualcontrolovertheheatandlights.Thisisdonewiththeoccupantcontrolprocessthatessentiallyrenamesevents.
occupant_control:LOOP
RequestTemp(t)->EMITSetTemp(t)||SwitchOn->EMITLightOn||SwitchOff->EMITLightOff
Upon,theaboveprocesswilleventuallyemit
.Trivenimakesnoguaranteeastothetimingof
eventemission,soitispossiblethatcouldar-rivebeforeisemittedandthuswouldnotactu-allyturnoffthelight.Later,wewillshowaprogram-mingstyletobulletproofagainstsuchcases.Butinthis
case,
andoriginatefromhumanac-tions,andbecausewecanreasonablyassumethatthelightcomesonfasterthanahumancan iptheswitch,wewouldnotexpectthebadcaseevertooccur.Trivenisupports
anotionof“
”statementsappropriateforconcur-rentprograms,namelytemporal-logicformulas,toexpresssuchsafetyproperties.Thesepropertiesexpresstheas-sumptionsunderwhichapieceofTrivenicodefunctionscorrectly,inthespiritofpreconditionsinHoare-stylerules
forsequentialprograms.Forinstance,theformula
expressesthepropertyofasinglepointduringanexecu-tionrunthat“didnotoccursincethemostrecent
.”Then,theformula
expressesthepropertyofanentireexecution(readas
“always”)that“whenever
occurs,thereisnopending”.AddingSwOffSafety(andthesymmet-ricpropertyfor)totheof ceprogramgeneratesarun-timeerrorwheneverthepropertyisviolated.Sim-ilarpropertieswouldbeappropriatefortheprocess.
Anof cecanbeintwomodes,occupantmodeandeconomymode.Occupantmodeisthenormalmodeofop-eration,asimplementedbytheoccupant-controlprocessabove.Ineconomymode,thetemperatureisreducedtoandheldataspeci edvalue,despiteanyrequestsoth-erwise,andthelightsareturnedoffandtheswitchdis-abled.The
eventputsanof ceintoeconomymode,loweringthetemperatureto,andthe
eventreturnstheof cetooccupantmode,
restoringtherequestedtemperaturetothemostrecentob-servedrequest.Inaddition,ifanof ceisineconomymode,itshouldtemporarilyreverttooccupantmodewhenthedoorisopen,incasesomeonearrivesinthemiddleofthenighttowork;inthatcase,theof cereturnstoecon-omymodewhenthedoorisclosed.
Theeconomycontrolprocessimplementsthiscontrol,emittingwhenevertheof ceshouldenterecon-omymode,loweringthetemperatureto,andemitting
whenevertheof ceshouldreturntooccupant
mode,restoringthetemperatureto.Theprocessrunsthreesubprocessesinparallel.The rstonemonitorscon-tinuouslythelastrequestedtemperature(isthe“skip”ofTriveni;itdoesnothingandterminatesimmediately).Thesecondandthirdparallelcomponentstodeterminewhentheof ceshouldchangemodes.Thecodestructure
LOOP
EconomyMode(t)->DO
....
WATCHINGOccupantMode
||LOOP
OccupantMode
->DO
....
WATCHINGEconomyMode
establishesmutualexclusionbetweentheoccupantmodeandeconomymode.Theinvariantmaintainedisthatthemodeisdeterminedbythelastoccurrenceoftheevents
and.Thisstructurealso
热门文档