S. A language-based approach to unifying events an

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ALanguage-basedApproachtoUnifyingEventsandThreadsPengLiUniversityofPennsylvaniaSteveZdancewicUniversityofPennsylvaniaAbstractThispaperpresentsalanguage-basedtechniquetounifytwoseeminglyoppositeprogrammingmodelsforbuild-ingmassivelyconcurrentnetworkservices:theevent-drivenmodelandthemultithreadedmodel.Theresultisaunifiedconcurrencymodelprovidingboththreadabstractionsandeventabstractions.Usingthismodel,eachcomponentinanapplicationcanbeimplementedusingtheappropriateabstraction,simplifyingthedesignofcomplex,multithreadedsystemssoftware.Thispapershowshowtoimplementtheunifiedcon-currencymodelinHaskell,apure,lazy,functionalpro-gramminglanguage.Italsodemonstrateshowtousethesetechniquestobuildanapplication-levelthreadli-brarywithsupportformultiprocessingandasynchronousI/OmechanismsinLinux.Thethreadlibraryistype-safe,isrelativelysimpletoimplement,andhasgoodperformance.Application-levelthreadsareextremelylightweight(scalingtotenmillionthreads)andourscheduler,whichisimplementedasamodularandex-tensibleevent-drivensystem,outperformsNPTLinI/Obenchmarks.1IntroductionModernnetworkservicespresentsoftwareengineerswithanumberofdesignchallenges.Webservers,mul-tiplayergames,andInternet-scaledatastorageappli-cationsmustaccommodatethousandsofsimultaneousclientconnections.Suchmassively-concurrentprogramsaredifficulttoimplement,especiallywhenotherrequire-ments,suchashighperformanceandstrongsecurity,mustalsobemet.Eventsvs.threads:Twoimplementationstrategiesforbuildingsuchinherentlyconcurrentsystemshavebeensuccessful.Boththemultithreadedandevent-drivenapproacheshavetheirproponentsanddetractors.Thedebateoverwhichmodelis“better”haswagedformanyyears,withlittleresolution.Ousterhout[17]hasarguedthat“threadsareabadidea(formostpurposes),”citingthedifficultiesofensuringpropersynchronizationanddebuggingwiththread-basedapproaches.Acounterargument,byvonBehren,Condit,andBrewer[23],ar-guesthat“eventsareabadidea(forhigh-concurrencyservers),”essentiallybecausereasoningaboutcontrolflowinevent-basedsystemsisdifficultandtheappar-entperformancewinsoftheevent-drivenapproachcanbecompletelyrecoupedbycarefulengineering[22].Fromtheprogrammer’sperspective,bothmodelsareattractive.Sometimesitisdesirabletothinkaboutthesystemintermsofthreads(forexample,todescribethesequenceofeventsthatoccurwhenprocessingaclient’srequests),andsometimesitisdesirabletothinkaboutthesystemintermsofeventsandeventhandlers(forexam-ple,tosimplifyreasoningaboutasynchronouscommu-nicationsandresourcescheduling).Aunifiedconcurrencymodel:Thispapershowsthateventsandthreadscanbeunifiedinasingleconcur-rencymodel,allowingtheprogrammertodesignpartsoftheapplicationasifshewereusingthreads,wherethreadsaretheappropriateabstraction,andpartsofthesystemusingevents,wheretheyaremoresuitable.Sec-tion2givessomeadditionalbackgroundaboutthemulti-threadedandevent-drivenmodelsandmotivatesthede-signofourunifiedconcurrencymodel.Inourmodel,forhigher-levelapplicationcode,theprogrammercanuseuseamultithreadedprogrammingstylesimilartoCandJava,withfamiliarcontrol-flowel-ementssuchassequencing,functionscalls,conditionalsandexceptions,andwithuser-definedsystemcallsforI/Oandthreadcontrol.Figure1showstwosamplefunc-tionswritteninthemultithreadedstyle.Forlower-levelI/Ocode,theprogrammercanconve-nientlyuseasynchronousOSinterfacessuchasepollandAIOinLinux.Thethreadschedulerhasanex-tensible,modularevent-drivenarchitectureinwhichtheapplication-levelthreadscanbeseenaseventhandlers.Alanguage-basedapproach:LanguageslikeCandC++havehistoricallybeenusedtoimplementhigh-performance,concurrentsoftware.However,theysuf-ferfromwellknownsecurityandreliabilityproblemsthathavepromptedamovetowardtype-safelanguageslikeJavaandC#.Theirgeneral-purposethreadspack-agesaretypicallyquiteheavyweightthough,andnoneoftheselanguagesprovideappropriateabstractionstosim-plifyevent-drivingprogramming.Implementingscal-ablethreadoreventsystemsisfeasibleusingtheselan-guages,buttheresultscanbecumbersometouse.Tech-niquessuchascompilertransformationscanaddresstheseproblemstosomeextent[22],buteventhentheen-gineeringchallengestypicallyforcetheprogrammertochoosebetweenthreadsorevents—theydon’tgetboth.Ourcase-studyimplementationoftheunifiedconcur-rencymodel,describedindetailinSection3,iswrit-tenintheprogramminglanguageHaskell[12].Haskellisapure,lazy,strongly-typed,functionallanguagewithmanyadvancedlanguagefeatures,suchastypeclasses,thatmakeitveryconvenienttoimplementtheunifiedconcurrencymodel.OurimplementationisbasedontechniquesdevelopedsometimeagobyKoenClaessen[8]intheprogramminglanguagesresearchcommunity.Application-levelthreads:Usingthislanguagesup-port,wehavebuiltanapplication-levelthreadlibrary,inwhichthethreadedcodeandthreadschedulerarewrit-teninsidetheapplication.Section4describesthethreadlibrary,whichusesbothevent-drivenandmultithreadedprogrammingmodels,andshowshowitcanflexiblysup-portseveralsynchronizationmechanismsforinterthreadcommunicationandsharedstate.Comparedtotraditionalapproaches(bothmulti-threadedandevent-driven),ourapplication-levelthreadlibraryhasmanyadvantagesforbuil

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功