We propose a new concurrent skip list algorithm distinguish ed by a combination of simplicity and scalability The algorit hm employs optimistic synchronization searching without acquiring locks followed by short lockbased validation before adding or ID: 23105
Download Pdf The PPT/PDF document "A Provably Correct Scalable Concurrent S..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
--------------- 157813152225+1Fig.1.Askiplistwithmaximumheightof4.Thenumberbeloweachnode(i.e.,arrayofnextpointers)isthekeyofthatnode,with 1and+1asthekeysfortheleftandrightsentinelnodesrespectively.2BackgroundAskiplist[7]isalinkedlistthatissortedbykey,andinwhichnodesareassignedarandomheight,uptosomemaximumheight,wherethefrequencyofnodesofaparticularheightdecreasesexponentiallywiththeheight.Anodeinaskiplisthasnotjustonesuccessor,butanumberofsuccessorsequaltoitsheight:eachnodestoresapointertothenextnodeinthelistofeachheightuptoitsown.Forexample,anodeofheight3storesthree\nextpointers",onetothenextnodeofheight1,onetothenextnodeofheight2,andonetothenextnodeofheight3.Figure1illustratesaskiplistinwhichthekeysareintegers.Wethinkofaskiplistashavingseverallayersoflists,andwetalkaboutthepredecessorandsuccessorofanodeateachlayer.Thelistateachlayer,otherthanthebottomlayer,isasublistofthelistatthelayerbeneathit.Becausethereareexponentiallyfewernodesofgreaterheights,wecanndakeyquicklybysearchingrstathigherlayers,skippingoverlargenumbersofshorternodesandprogressivelyworkingdownwarduntilanodewiththedesiredkeyisfound,orelsethebottomlayerisreached.Thus,theexpectedtimecomplexityofskip-listoperationsislogarithmicinthelengthofthelist.Itisconvenienttohaveleftsentinelandrightsentinelnodes,atthebeginningandendofthelistsrespectively.Thesenodeshavethemaximumheight,andinitially,whentheskiplistisempty,therightsentinelisthesuccessoroftheleftsentinelateverylayer.Theleftsentinel'skeyissmaller,andtherightsentinel'skeyisgreater,thananykeythatmaybeaddedtotheset.Searchingtheskiplistthusalwaysbeginsattheleftsentinel.3OurAlgorithmWepresentourconcurrentskip-listalgorithminthecontextofanimplementa-tionofasetobjectsupportingthreemethods,add,removeandcontains:add(v)addsvtothesetandreturnstrueivwasnotalreadyintheset;remove(v)removesvfromthesetandreturnstrueivwasintheset;andcontains(v)returnstrueivisintheset.Weshowthatourimplementationislinearizable[5];thatis,everyoperationappearstotakeplaceatomicallyatsomepoint(the2 33intfindNode(intv,34Nodepreds[],35Nodesuccs[])f36intlFound= 1;37Nodepred=&LSentinel;38for(intlayer=MaxHeight 1;39layer0;40layer )f41Nodecurr=pred nexts[layer];42while(vcurr key)f43pred=curr;curr=pred nexts[layer];44g45if(lFound== 1&&v==curr key)f46lFound=layer;47g48preds[layer]=pred;49succs[layer]=curr;50g51returnlFound;52gFigure1.3.ThefindNodehelperfunctionsuccsofnodepointers,andsearchesexactlyasinasequentialskiplist,startingatthehighestlayerandproceedingtothenextlowerlayereachtimeitencountersanodewhosekeyisgreaterthanorequaltov.Thethreadrecordsinthepredsarraythelastnodewithakeylessthanvthatitencounteredateachlayer,andthatnode'ssuccessor(whichmusthaveakeygreaterthanorequaltov)inthesuccsarray.Ifitndsanodewiththesought-afterkey,findNodereturnstheindexoftherstlayeratwhichsuchanodewasfound;otherwise,itreturns 1.Forsimplicityofpresentation,wehavefindNodecontinuetothebottomlayerevenifitndsanodewiththesought-afterkeyatahigherlevel,soalltheentriesinbothpredsandsuccsarraysarelledinafterfindNodeterminates(seeSection3.4foroptimizationsusedintherealimplementation).NotethatfindNodedoesnotacquireanylocks,nordoesitretryincaseofcon\rictingaccesswithsomeotherthread.Wenowconsidereachoftheoperationsinturn.3.1TheaddoperationTheaddoperation,showninFigure1.4,callsfindNodetodeterminewhetheranodewiththekeyisalreadyinthelist.Ifso(lines59{66),andthenodeisnotmarked,thentheaddoperationreturnsfalse,indicatingthatthekeyisalreadyintheset.However,ifthatnodeisnotyetfullylinked,thenthethreadwaitsuntilitis(becausethekeyisnotintheabstractsetuntilthenodeisfullylinked).Ifthenodeismarked,thensomeotherthreadisintheprocessofdeletingthatnode,sothethreaddoingtheaddoperationsimplyretries.4 54booladd(intv)f55inttopLayer=randomLevel(MaxHeight);56Nodepreds[MaxHeight],succs[MaxHeight];57while(true)f58intlFound=findNode(v,preds,succs);59if(lFound= 1)f60NodenodeFound=succs[lFound];61if(!nodeFound marked)f62while(!nodeFound fullyLinked)f;g63returnfalse;64g65continue;66g67inthighestLocked= 1;68tryf69Nodepred,succ,prevPred=null;70boolvalid=true;71for(intlayer=0;72valid&&(layertopLayer);73layer++)f74pred=preds[layer];75succ=succs[layer];76if(pred=prevPred)f77pred lock.lock();78highestLocked=layer;79prevPred=pred;80g81valid=!pred marked&&!succ marked&&82pred nexts[layer]==succ;83g84if(!valid)continue;86NodenewNode=newNode(v,topLayer);87for(intlayer=0;88layertopLayer;89layer++)f90newNode nexts[layer]=succs[layer];91preds[layer] nexts[layer]=newNode;92g94newNode fullyLinked=true;95returntrue;96g97finallyfunlock(preds,highestLocked);g98gFigure1.4.Theaddmethod5 Ifnonodewasfoundwiththeappropriatekey,thenthethreadlocksandvalidatesallthepredecessorsreturnedbyfindNodeuptotheheightofthenewnode(lines69{84).Thisheight,denotedbytopNodeLayer,isdeterminedattheverybeginningoftheaddoperationusingtherandomLevelfunction.3Validation(lines81{83)checksthatforeachlayeritopNodeLayer,preds[i]andsuccs[i]arestilladjacentatlayeri,andthatneitherismarked.Ifvalidationfails,thethreadencounteredacon\rictingoperation,soitreleasesthelocksitacquired(inthefinallyblockatline97)andretries.IfthethreadsuccessfullylocksandvalidatestheresultsoffindNodeuptotheheightofthenewnode,thentheaddoperationisguaranteedtosucceedbecausethethreadholdsallthelocksuntilitfullylinksitsnewnode.Inthiscase,thethreadallocatesanewnodewiththeappropriatekeyandheight,linksitin,setsthefullyLinked\ragofthenewnode(thisisthelinearizationpointoftheaddoperation),andthenreturnstrueafterreleasingallitslocks(lines86{97).ThethreadwritingnewNode-nexts[i]istheonecaseinwhichathreadmodiesthenextseldforanodeithasnotlocked.ItissafebecausenewNodewillnotbelinkedintothelistatlayeriuntilthethreadsetspreds[i]-nexts[i]tonewNode,afteritwritesnewNode-nexts[i].3.2TheremoveoperationTheremoveoperationshowninFigure1.5,likewisecallsfindNodetodeterminewhetheranodewiththeappropriatekeyisinthelist.Ifso,thethreadcheckswhetherthenodeis\okaytodelete"(Figure1.6),whichmeansitisfullylinked,notmarked,anditwasfoundatitstoplayer.4Ifthenodemeetstheserequire-ments,thethreadlocksthenodeandveriesthatitisstillnotmarked.Ifso,thethreadmarksthenode,whichlogicallydeletesit(lines111{121);thatis,themarkingofthenodeisthelinearizationpointoftheremoveoperation.Therestoftheprocedureaccomplishesthe\physical"deletion,removingthenodefromthelistbyrstlockingitspredecessorsatalllayersuptotheheightofthedeletednode(lines124{138),andsplicingthenodeoutonelayeratatime(lines140{142).Tomaintaintheskip-liststructure,thenodeissplicedoutofhigherlayersbeforebeingsplicedoutoflowerones(though,toensurefreedomfromdeadlock,asdiscussedinSection4,thelocksareacquiredintheoppositeorder,fromlowerlayersup).Asintheaddoperation,beforechanginganyofthedeletednode'spredecessors,thethreadvalidatesthatthosenodesareindeedstillthedeletednode'spredecessors.ThisisdoneusingtheweakValidatefunction,whichisthesameasvalidateexceptthatitdoesnotfailifthesuccessor3ThisfunctionistakenfromLea'salgorithmtoensureafaircomparisonintheex-perimentspresentedinSection5.Itreturns0withprobability34,iwithprobability2 (i+2)fori2[1;30],and31withprobability2 32.4Anodefoundnotinitstoplayerwaseithernotyetfullylinked,ormarkedandpartiallyunlinked,atsomepointwhenthethreadtraversedthelistatthatlayer.Wecouldhavecontinuedwiththeremoveoperation,butthesubsequentvalidationwouldfail.6 101boolremove(intv)f102NodenodeToDelete=null;103boolisMarked=false;104inttopLayer= 1;105Nodepreds[MaxHeight],succs[MaxHeight];106while(true)f107intlFound=findNode(v,preds,succs);108if(isMarkedjj109(lFound= 1&&okToDelete(succs[lFound],lFound)))f111if(!isMarked)f112nodeToDelete=succs[lFound];113topLayer=nodeToDelete topLayer;114nodeToDelete lock.lock();115if(nodeToDelete marked)f116nodeToDelete lock.unlock();117returnfalse;118g119nodeToDelete marked=true;120isMarked=true;121g122inthighestLocked= 1;123tryf124Nodepred,succ,prevPred=null;125boolvalid=true;126for(intlayer=0;127valid&&(layertopLayer);128layer++)f129pred=preds[layer];130succ=succs[layer];131if(pred=prevPred)f132pred lock.lock();133highestLocked=layer;134prevPred=pred;135g136valid=!pred marked&&pred