future.hpp 199 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897
  1. // (C) Copyright 2008-10 Anthony Williams
  2. // (C) Copyright 2011-2015 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_THREAD_FUTURE_HPP
  8. #define BOOST_THREAD_FUTURE_HPP
  9. #include <boost/thread/detail/config.hpp>
  10. // boost::thread::future requires exception handling
  11. // due to boost::exception::exception_ptr dependency
  12. //#define BOOST_THREAD_CONTINUATION_SYNC
  13. #ifdef BOOST_NO_EXCEPTIONS
  14. namespace boost
  15. {
  16. namespace detail {
  17. struct shared_state_base {
  18. void notify_deferred() {}
  19. };
  20. }
  21. }
  22. #else
  23. #include <boost/thread/condition_variable.hpp>
  24. #include <boost/thread/detail/move.hpp>
  25. #include <boost/thread/detail/invoker.hpp>
  26. #include <boost/thread/detail/invoke.hpp>
  27. #include <boost/thread/detail/is_convertible.hpp>
  28. #include <boost/thread/exceptional_ptr.hpp>
  29. #include <boost/thread/futures/future_error.hpp>
  30. #include <boost/thread/futures/future_error_code.hpp>
  31. #include <boost/thread/futures/future_status.hpp>
  32. #include <boost/thread/futures/is_future_type.hpp>
  33. #include <boost/thread/futures/launch.hpp>
  34. #include <boost/thread/futures/wait_for_all.hpp>
  35. #include <boost/thread/futures/wait_for_any.hpp>
  36. #include <boost/thread/lock_algorithms.hpp>
  37. #include <boost/thread/lock_types.hpp>
  38. #include <boost/thread/mutex.hpp>
  39. #include <boost/thread/thread_only.hpp>
  40. #include <boost/thread/thread_time.hpp>
  41. #include <boost/thread/executor.hpp>
  42. #include <boost/thread/executors/generic_executor_ref.hpp>
  43. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  44. #include <boost/optional.hpp>
  45. #else
  46. #include <boost/thread/csbl/memory/unique_ptr.hpp>
  47. #endif
  48. #include <boost/assert.hpp>
  49. #include <boost/bind/bind.hpp>
  50. #ifdef BOOST_THREAD_USES_CHRONO
  51. #include <boost/chrono/system_clocks.hpp>
  52. #endif
  53. #include <boost/core/enable_if.hpp>
  54. #include <boost/core/ref.hpp>
  55. #include <boost/enable_shared_from_this.hpp>
  56. #include <boost/exception_ptr.hpp>
  57. #include <boost/function.hpp>
  58. #include <boost/scoped_array.hpp>
  59. #include <boost/shared_ptr.hpp>
  60. #include <boost/smart_ptr/make_shared.hpp>
  61. #include <boost/throw_exception.hpp>
  62. #include <boost/type_traits/conditional.hpp>
  63. #include <boost/type_traits/decay.hpp>
  64. #include <boost/type_traits/is_copy_constructible.hpp>
  65. #include <boost/type_traits/is_fundamental.hpp>
  66. #include <boost/type_traits/is_void.hpp>
  67. #include <boost/utility/result_of.hpp>
  68. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  69. #include <boost/thread/detail/memory.hpp>
  70. #include <boost/container/scoped_allocator.hpp>
  71. #if ! defined BOOST_NO_CXX11_ALLOCATOR
  72. #include <memory>
  73. #endif
  74. #endif
  75. #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  76. #include <boost/thread/csbl/tuple.hpp>
  77. #include <boost/thread/csbl/vector.hpp>
  78. #endif
  79. #include <algorithm>
  80. #include <list>
  81. #include <vector>
  82. #include <utility>
  83. #if defined BOOST_THREAD_PROVIDES_FUTURE
  84. #define BOOST_THREAD_FUTURE future
  85. #else
  86. #define BOOST_THREAD_FUTURE unique_future
  87. #endif
  88. namespace boost
  89. {
  90. template <class T>
  91. shared_ptr<T> static_shared_from_this(T* that)
  92. {
  93. return static_pointer_cast<T>(that->shared_from_this());
  94. }
  95. template <class T>
  96. shared_ptr<T const> static_shared_from_this(T const* that)
  97. {
  98. return static_pointer_cast<T const>(that->shared_from_this());
  99. }
  100. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  101. #else
  102. namespace executors {
  103. class executor;
  104. }
  105. using executors::executor;
  106. #endif
  107. typedef shared_ptr<executor> executor_ptr_type;
  108. namespace detail
  109. {
  110. struct relocker
  111. {
  112. boost::unique_lock<boost::mutex>& lock_;
  113. relocker(boost::unique_lock<boost::mutex>& lk):
  114. lock_(lk)
  115. {
  116. lock_.unlock();
  117. }
  118. ~relocker()
  119. {
  120. if (! lock_.owns_lock()) {
  121. lock_.lock();
  122. }
  123. }
  124. void lock() {
  125. if (! lock_.owns_lock()) {
  126. lock_.lock();
  127. }
  128. }
  129. private:
  130. relocker& operator=(relocker const&);
  131. };
  132. struct shared_state_base : enable_shared_from_this<shared_state_base>
  133. {
  134. typedef std::list<boost::condition_variable_any*> waiter_list;
  135. typedef waiter_list::iterator notify_when_ready_handle;
  136. // This type should be only included conditionally if interruptions are allowed, but is included to maintain the same layout.
  137. typedef shared_ptr<shared_state_base> continuation_ptr_type;
  138. typedef std::vector<continuation_ptr_type> continuations_type;
  139. boost::exception_ptr exception;
  140. bool done;
  141. bool is_valid_;
  142. bool is_deferred_;
  143. bool is_constructed;
  144. launch policy_;
  145. mutable boost::mutex mutex;
  146. boost::condition_variable waiters;
  147. waiter_list external_waiters;
  148. boost::function<void()> callback;
  149. // This declaration should be only included conditionally, but is included to maintain the same layout.
  150. continuations_type continuations;
  151. executor_ptr_type ex_;
  152. // This declaration should be only included conditionally, but is included to maintain the same layout.
  153. virtual void launch_continuation()
  154. {
  155. }
  156. shared_state_base():
  157. done(false),
  158. is_valid_(true),
  159. is_deferred_(false),
  160. is_constructed(false),
  161. policy_(launch::none),
  162. continuations(),
  163. ex_()
  164. {}
  165. shared_state_base(exceptional_ptr const& ex):
  166. exception(ex.ptr_),
  167. done(true),
  168. is_valid_(true),
  169. is_deferred_(false),
  170. is_constructed(false),
  171. policy_(launch::none),
  172. continuations(),
  173. ex_()
  174. {}
  175. virtual ~shared_state_base()
  176. {
  177. }
  178. bool is_done()
  179. {
  180. return done;
  181. }
  182. executor_ptr_type get_executor()
  183. {
  184. return ex_;
  185. }
  186. void set_executor_policy(executor_ptr_type aex)
  187. {
  188. set_executor();
  189. ex_ = aex;
  190. }
  191. void set_executor_policy(executor_ptr_type aex, boost::lock_guard<boost::mutex>&)
  192. {
  193. set_executor();
  194. ex_ = aex;
  195. }
  196. void set_executor_policy(executor_ptr_type aex, boost::unique_lock<boost::mutex>&)
  197. {
  198. set_executor();
  199. ex_ = aex;
  200. }
  201. bool valid(boost::unique_lock<boost::mutex>&) { return is_valid_; }
  202. bool valid() {
  203. boost::unique_lock<boost::mutex> lk(this->mutex);
  204. return valid(lk);
  205. }
  206. void invalidate(boost::unique_lock<boost::mutex>&) { is_valid_ = false; }
  207. void invalidate() {
  208. boost::unique_lock<boost::mutex> lk(this->mutex);
  209. invalidate(lk);
  210. }
  211. void validate(boost::unique_lock<boost::mutex>&) { is_valid_ = true; }
  212. void validate() {
  213. boost::unique_lock<boost::mutex> lk(this->mutex);
  214. validate(lk);
  215. }
  216. void set_deferred()
  217. {
  218. is_deferred_ = true;
  219. policy_ = launch::deferred;
  220. }
  221. void set_async()
  222. {
  223. is_deferred_ = false;
  224. policy_ = launch::async;
  225. }
  226. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  227. void set_executor()
  228. {
  229. is_deferred_ = false;
  230. policy_ = launch::executor;
  231. }
  232. #else
  233. void set_executor()
  234. {
  235. }
  236. #endif
  237. notify_when_ready_handle notify_when_ready(boost::condition_variable_any& cv)
  238. {
  239. boost::unique_lock<boost::mutex> lock(this->mutex);
  240. do_callback(lock);
  241. return external_waiters.insert(external_waiters.end(),&cv);
  242. }
  243. void unnotify_when_ready(notify_when_ready_handle it)
  244. {
  245. boost::lock_guard<boost::mutex> lock(this->mutex);
  246. external_waiters.erase(it);
  247. }
  248. #if 0
  249. // this inline definition results in ODR. See https://github.com/boostorg/thread/issues/193
  250. // to avoid it, we define the function on the derived templates using the macro BOOST_THREAD_DO_CONTINUATION
  251. #define BOOST_THREAD_DO_CONTINUATION
  252. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  253. void do_continuation(boost::unique_lock<boost::mutex>& lock)
  254. {
  255. if (! continuations.empty()) {
  256. continuations_type the_continuations = continuations;
  257. continuations.clear();
  258. relocker rlk(lock);
  259. for (continuations_type::iterator it = the_continuations.begin(); it != the_continuations.end(); ++it) {
  260. (*it)->launch_continuation();
  261. }
  262. }
  263. }
  264. #else
  265. void do_continuation(boost::unique_lock<boost::mutex>&)
  266. {
  267. }
  268. #endif
  269. #else
  270. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  271. #define BOOST_THREAD_DO_CONTINUATION \
  272. void do_continuation(boost::unique_lock<boost::mutex>& lock) \
  273. { \
  274. if (! this->continuations.empty()) { \
  275. continuations_type the_continuations = this->continuations; \
  276. this->continuations.clear(); \
  277. relocker rlk(lock); \
  278. for (continuations_type::iterator it = the_continuations.begin(); it != the_continuations.end(); ++it) { \
  279. (*it)->launch_continuation(); \
  280. } \
  281. } \
  282. }
  283. #else
  284. #define BOOST_THREAD_DO_CONTINUATION \
  285. void do_continuation(boost::unique_lock<boost::mutex>&) \
  286. { \
  287. }
  288. #endif
  289. virtual void do_continuation(boost::unique_lock<boost::mutex>&) = 0;
  290. #endif
  291. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  292. virtual void set_continuation_ptr(continuation_ptr_type continuation, boost::unique_lock<boost::mutex>& lock)
  293. {
  294. continuations.push_back(continuation);
  295. if (done) {
  296. do_continuation(lock);
  297. }
  298. }
  299. #endif
  300. void mark_finished_internal(boost::unique_lock<boost::mutex>& lock)
  301. {
  302. done=true;
  303. waiters.notify_all();
  304. for(waiter_list::const_iterator it=external_waiters.begin(),
  305. end=external_waiters.end();it!=end;++it)
  306. {
  307. (*it)->notify_all();
  308. }
  309. do_continuation(lock);
  310. }
  311. void notify_deferred()
  312. {
  313. boost::unique_lock<boost::mutex> lock(this->mutex);
  314. mark_finished_internal(lock);
  315. }
  316. void do_callback(boost::unique_lock<boost::mutex>& lock)
  317. {
  318. if(callback && !done)
  319. {
  320. boost::function<void()> local_callback=callback;
  321. relocker relock(lock);
  322. local_callback();
  323. }
  324. }
  325. virtual bool run_if_is_deferred()
  326. {
  327. boost::unique_lock<boost::mutex> lk(this->mutex);
  328. if (is_deferred_)
  329. {
  330. is_deferred_=false;
  331. execute(lk);
  332. return true;
  333. }
  334. else
  335. return false;
  336. }
  337. virtual bool run_if_is_deferred_or_ready()
  338. {
  339. boost::unique_lock<boost::mutex> lk(this->mutex);
  340. if (is_deferred_)
  341. {
  342. is_deferred_=false;
  343. execute(lk);
  344. return true;
  345. }
  346. else
  347. return done;
  348. }
  349. void wait_internal(boost::unique_lock<boost::mutex> &lk, bool rethrow=true)
  350. {
  351. do_callback(lk);
  352. if (is_deferred_)
  353. {
  354. is_deferred_=false;
  355. execute(lk);
  356. }
  357. waiters.wait(lk, boost::bind(&shared_state_base::is_done, boost::ref(*this)));
  358. if(rethrow && exception)
  359. {
  360. boost::rethrow_exception(exception);
  361. }
  362. }
  363. virtual void wait(boost::unique_lock<boost::mutex>& lock, bool rethrow=true)
  364. {
  365. wait_internal(lock, rethrow);
  366. }
  367. void wait(bool rethrow=true)
  368. {
  369. boost::unique_lock<boost::mutex> lock(this->mutex);
  370. wait(lock, rethrow);
  371. }
  372. #if defined BOOST_THREAD_USES_DATETIME
  373. template<typename Duration>
  374. bool timed_wait(Duration const& rel_time)
  375. {
  376. boost::unique_lock<boost::mutex> lock(this->mutex);
  377. if (is_deferred_)
  378. return false;
  379. do_callback(lock);
  380. return waiters.timed_wait(lock, rel_time, boost::bind(&shared_state_base::is_done, boost::ref(*this)));
  381. }
  382. bool timed_wait_until(boost::system_time const& target_time)
  383. {
  384. boost::unique_lock<boost::mutex> lock(this->mutex);
  385. if (is_deferred_)
  386. return false;
  387. do_callback(lock);
  388. return waiters.timed_wait(lock, target_time, boost::bind(&shared_state_base::is_done, boost::ref(*this)));
  389. }
  390. #endif
  391. #ifdef BOOST_THREAD_USES_CHRONO
  392. template <class Clock, class Duration>
  393. future_status
  394. wait_until(const chrono::time_point<Clock, Duration>& abs_time)
  395. {
  396. boost::unique_lock<boost::mutex> lock(this->mutex);
  397. if (is_deferred_)
  398. return future_status::deferred;
  399. do_callback(lock);
  400. if(!waiters.wait_until(lock, abs_time, boost::bind(&shared_state_base::is_done, boost::ref(*this))))
  401. {
  402. return future_status::timeout;
  403. }
  404. return future_status::ready;
  405. }
  406. #endif
  407. void mark_exceptional_finish_internal(boost::exception_ptr const& e, boost::unique_lock<boost::mutex>& lock)
  408. {
  409. exception=e;
  410. mark_finished_internal(lock);
  411. }
  412. void mark_exceptional_finish()
  413. {
  414. boost::unique_lock<boost::mutex> lock(this->mutex);
  415. mark_exceptional_finish_internal(boost::current_exception(), lock);
  416. }
  417. void set_exception_deferred(exception_ptr e)
  418. {
  419. unique_lock<boost::mutex> lk(this->mutex);
  420. if (has_value(lk))
  421. {
  422. throw_exception(promise_already_satisfied());
  423. }
  424. exception=e;
  425. this->is_constructed = true;
  426. }
  427. void set_exception_at_thread_exit(exception_ptr e)
  428. {
  429. set_exception_deferred(e);
  430. // unique_lock<boost::mutex> lk(this->mutex);
  431. // if (has_value(lk))
  432. // {
  433. // throw_exception(promise_already_satisfied());
  434. // }
  435. // exception=e;
  436. // this->is_constructed = true;
  437. detail::make_ready_at_thread_exit(shared_from_this());
  438. }
  439. bool has_value() const
  440. {
  441. boost::lock_guard<boost::mutex> lock(this->mutex);
  442. return done && ! exception;
  443. }
  444. bool has_value(unique_lock<boost::mutex>& ) const
  445. {
  446. return done && ! exception;
  447. }
  448. bool has_exception() const
  449. {
  450. boost::lock_guard<boost::mutex> lock(this->mutex);
  451. return done && exception;
  452. }
  453. launch launch_policy(boost::unique_lock<boost::mutex>&) const
  454. {
  455. return policy_;
  456. }
  457. future_state::state get_state(boost::unique_lock<boost::mutex>&) const
  458. {
  459. if(!done)
  460. {
  461. return future_state::waiting;
  462. }
  463. else
  464. {
  465. return future_state::ready;
  466. }
  467. }
  468. future_state::state get_state() const
  469. {
  470. boost::lock_guard<boost::mutex> guard(this->mutex);
  471. if(!done)
  472. {
  473. return future_state::waiting;
  474. }
  475. else
  476. {
  477. return future_state::ready;
  478. }
  479. }
  480. exception_ptr get_exception_ptr()
  481. {
  482. boost::unique_lock<boost::mutex> lock(this->mutex);
  483. wait_internal(lock, false);
  484. return exception;
  485. }
  486. template<typename F,typename U>
  487. void set_wait_callback(F f,U* u)
  488. {
  489. boost::lock_guard<boost::mutex> lock(this->mutex);
  490. callback=boost::bind(f,boost::ref(*u));
  491. }
  492. virtual void execute(boost::unique_lock<boost::mutex>&) {}
  493. private:
  494. shared_state_base(shared_state_base const&);
  495. shared_state_base& operator=(shared_state_base const&);
  496. };
  497. // Used to create stand-alone futures
  498. template<typename T>
  499. struct shared_state:
  500. detail::shared_state_base
  501. {
  502. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  503. typedef boost::optional<T> storage_type;
  504. #else
  505. typedef boost::csbl::unique_ptr<T> storage_type;
  506. #endif
  507. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  508. typedef T const& source_reference_type;
  509. typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
  510. typedef T move_dest_type;
  511. #elif defined BOOST_THREAD_USES_MOVE
  512. typedef typename conditional<boost::is_fundamental<T>::value,T,T const&>::type source_reference_type;
  513. typedef BOOST_THREAD_RV_REF(T) rvalue_source_type;
  514. typedef T move_dest_type;
  515. #else
  516. typedef T& source_reference_type;
  517. typedef typename conditional<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >::value, BOOST_THREAD_RV_REF(T),T const&>::type rvalue_source_type;
  518. typedef typename conditional<boost::thread_detail::is_convertible<T&,BOOST_THREAD_RV_REF(T) >::value, BOOST_THREAD_RV_REF(T),T>::type move_dest_type;
  519. #endif
  520. typedef const T& shared_future_get_result_type;
  521. storage_type result;
  522. shared_state():
  523. result()
  524. {}
  525. shared_state(exceptional_ptr const& ex):
  526. detail::shared_state_base(ex), result()
  527. {}
  528. // locating this definition on the template avoid the ODR issue. See https://github.com/boostorg/thread/issues/193
  529. BOOST_THREAD_DO_CONTINUATION
  530. void mark_finished_with_result_internal(source_reference_type result_, boost::unique_lock<boost::mutex>& lock)
  531. {
  532. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  533. result = result_;
  534. #else
  535. result.reset(new T(result_));
  536. #endif
  537. this->mark_finished_internal(lock);
  538. }
  539. void mark_finished_with_result_internal(rvalue_source_type result_, boost::unique_lock<boost::mutex>& lock)
  540. {
  541. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  542. result = boost::move(result_);
  543. #elif ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  544. result.reset(new T(boost::move(result_)));
  545. #else
  546. result.reset(new T(static_cast<rvalue_source_type>(result_)));
  547. #endif
  548. this->mark_finished_internal(lock);
  549. }
  550. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  551. template <class ...Args>
  552. void mark_finished_with_result_internal(boost::unique_lock<boost::mutex>& lock, BOOST_THREAD_FWD_REF(Args)... args)
  553. {
  554. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  555. result.emplace(boost::forward<Args>(args)...);
  556. #else
  557. result.reset(new T(boost::forward<Args>(args)...));
  558. #endif
  559. this->mark_finished_internal(lock);
  560. }
  561. #endif
  562. void mark_finished_with_result(source_reference_type result_)
  563. {
  564. boost::unique_lock<boost::mutex> lock(this->mutex);
  565. this->mark_finished_with_result_internal(result_, lock);
  566. }
  567. void mark_finished_with_result(rvalue_source_type result_)
  568. {
  569. boost::unique_lock<boost::mutex> lock(this->mutex);
  570. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  571. mark_finished_with_result_internal(boost::move(result_), lock);
  572. #else
  573. mark_finished_with_result_internal(static_cast<rvalue_source_type>(result_), lock);
  574. #endif
  575. }
  576. storage_type& get_storage(boost::unique_lock<boost::mutex>& lk)
  577. {
  578. wait_internal(lk);
  579. return result;
  580. }
  581. virtual move_dest_type get(boost::unique_lock<boost::mutex>& lk)
  582. {
  583. return boost::move(*get_storage(lk));
  584. }
  585. move_dest_type get()
  586. {
  587. boost::unique_lock<boost::mutex> lk(this->mutex);
  588. return this->get(lk);
  589. }
  590. virtual shared_future_get_result_type get_sh(boost::unique_lock<boost::mutex>& lk)
  591. {
  592. return *get_storage(lk);
  593. }
  594. shared_future_get_result_type get_sh()
  595. {
  596. boost::unique_lock<boost::mutex> lk(this->mutex);
  597. return this->get_sh(lk);
  598. }
  599. void set_value_deferred(source_reference_type result_)
  600. {
  601. unique_lock<boost::mutex> lk(this->mutex);
  602. if (this->has_value(lk))
  603. {
  604. throw_exception(promise_already_satisfied());
  605. }
  606. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  607. result = result_;
  608. #else
  609. result.reset(new T(result_));
  610. #endif
  611. this->is_constructed = true;
  612. }
  613. void set_value_deferred(rvalue_source_type result_)
  614. {
  615. unique_lock<boost::mutex> lk(this->mutex);
  616. if (this->has_value(lk))
  617. {
  618. throw_exception(promise_already_satisfied());
  619. }
  620. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  621. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  622. result = boost::move(result_);
  623. #else
  624. result.reset(new T(boost::move(result_)));
  625. #endif
  626. #else
  627. #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  628. result = boost::move(result_);
  629. #else
  630. result.reset(new T(static_cast<rvalue_source_type>(result_)));
  631. #endif
  632. #endif
  633. this->is_constructed = true;
  634. }
  635. void set_value_at_thread_exit(source_reference_type result_)
  636. {
  637. set_value_deferred(result_);
  638. // unique_lock<boost::mutex> lk(this->mutex);
  639. // if (this->has_value(lk))
  640. // {
  641. // throw_exception(promise_already_satisfied());
  642. // }
  643. //#if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  644. // result = result_;
  645. //#else
  646. // result.reset(new T(result_));
  647. //#endif
  648. //
  649. // this->is_constructed = true;
  650. detail::make_ready_at_thread_exit(shared_from_this());
  651. }
  652. void set_value_at_thread_exit(rvalue_source_type result_)
  653. {
  654. set_value_deferred(boost::move(result_));
  655. // unique_lock<boost::mutex> lk(this->mutex);
  656. // if (this->has_value(lk))
  657. // throw_exception(promise_already_satisfied());
  658. //
  659. //#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  660. //#if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  661. // result = boost::move(result_);
  662. //#else
  663. // result.reset(new T(boost::move(result_)));
  664. //#endif
  665. //#else
  666. //#if defined BOOST_THREAD_FUTURE_USES_OPTIONAL
  667. // result = boost::move(result_);
  668. //#else
  669. // result.reset(new T(static_cast<rvalue_source_type>(result_)));
  670. //#endif
  671. //#endif
  672. // this->is_constructed = true;
  673. detail::make_ready_at_thread_exit(shared_from_this());
  674. }
  675. private:
  676. shared_state(shared_state const&);
  677. shared_state& operator=(shared_state const&);
  678. };
  679. template<typename T>
  680. struct shared_state<T&>:
  681. detail::shared_state_base
  682. {
  683. typedef T* storage_type;
  684. typedef T& source_reference_type;
  685. typedef T& move_dest_type;
  686. typedef T& shared_future_get_result_type;
  687. T* result;
  688. shared_state():
  689. result(0)
  690. {}
  691. shared_state(exceptional_ptr const& ex):
  692. detail::shared_state_base(ex), result(0)
  693. {}
  694. // locating this definition on the template avoid the ODR issue. See https://github.com/boostorg/thread/issues/193
  695. BOOST_THREAD_DO_CONTINUATION
  696. void mark_finished_with_result_internal(source_reference_type result_, boost::unique_lock<boost::mutex>& lock)
  697. {
  698. result= &result_;
  699. mark_finished_internal(lock);
  700. }
  701. void mark_finished_with_result(source_reference_type result_)
  702. {
  703. boost::unique_lock<boost::mutex> lock(this->mutex);
  704. mark_finished_with_result_internal(result_, lock);
  705. }
  706. virtual T& get(boost::unique_lock<boost::mutex>& lock)
  707. {
  708. wait_internal(lock);
  709. return *result;
  710. }
  711. T& get()
  712. {
  713. boost::unique_lock<boost::mutex> lk(this->mutex);
  714. return get(lk);
  715. }
  716. virtual T& get_sh(boost::unique_lock<boost::mutex>& lock)
  717. {
  718. wait_internal(lock);
  719. return *result;
  720. }
  721. T& get_sh()
  722. {
  723. boost::unique_lock<boost::mutex> lock(this->mutex);
  724. return get_sh(lock);
  725. }
  726. void set_value_deferred(T& result_)
  727. {
  728. unique_lock<boost::mutex> lk(this->mutex);
  729. if (this->has_value(lk))
  730. {
  731. throw_exception(promise_already_satisfied());
  732. }
  733. result= &result_;
  734. this->is_constructed = true;
  735. }
  736. void set_value_at_thread_exit(T& result_)
  737. {
  738. set_value_deferred(result_);
  739. // unique_lock<boost::mutex> lk(this->mutex);
  740. // if (this->has_value(lk))
  741. // throw_exception(promise_already_satisfied());
  742. // result= &result_;
  743. // this->is_constructed = true;
  744. detail::make_ready_at_thread_exit(shared_from_this());
  745. }
  746. private:
  747. shared_state(shared_state const&);
  748. shared_state& operator=(shared_state const&);
  749. };
  750. template<>
  751. struct shared_state<void>:
  752. detail::shared_state_base
  753. {
  754. typedef void shared_future_get_result_type;
  755. typedef void move_dest_type;
  756. shared_state()
  757. {}
  758. shared_state(exceptional_ptr const& ex):
  759. detail::shared_state_base(ex)
  760. {}
  761. // locating this definition on the template avoid the ODR issue. See https://github.com/boostorg/thread/issues/193
  762. BOOST_THREAD_DO_CONTINUATION
  763. void mark_finished_with_result_internal(boost::unique_lock<boost::mutex>& lock)
  764. {
  765. mark_finished_internal(lock);
  766. }
  767. void mark_finished_with_result()
  768. {
  769. boost::unique_lock<boost::mutex> lock(this->mutex);
  770. mark_finished_with_result_internal(lock);
  771. }
  772. virtual void get(boost::unique_lock<boost::mutex>& lock)
  773. {
  774. this->wait_internal(lock);
  775. }
  776. void get()
  777. {
  778. boost::unique_lock<boost::mutex> lock(this->mutex);
  779. this->get(lock);
  780. }
  781. virtual void get_sh(boost::unique_lock<boost::mutex>& lock)
  782. {
  783. this->wait_internal(lock);
  784. }
  785. void get_sh()
  786. {
  787. boost::unique_lock<boost::mutex> lock(this->mutex);
  788. this->get_sh(lock);
  789. }
  790. void set_value_deferred()
  791. {
  792. unique_lock<boost::mutex> lk(this->mutex);
  793. if (this->has_value(lk))
  794. {
  795. throw_exception(promise_already_satisfied());
  796. }
  797. this->is_constructed = true;
  798. }
  799. void set_value_at_thread_exit()
  800. {
  801. set_value_deferred();
  802. // unique_lock<boost::mutex> lk(this->mutex);
  803. // if (this->has_value(lk))
  804. // {
  805. // throw_exception(promise_already_satisfied());
  806. // }
  807. // this->is_constructed = true;
  808. detail::make_ready_at_thread_exit(shared_from_this());
  809. }
  810. private:
  811. shared_state(shared_state const&);
  812. shared_state& operator=(shared_state const&);
  813. };
  814. /////////////////////////
  815. /// future_async_shared_state_base
  816. /////////////////////////
  817. template<typename Rp>
  818. struct future_async_shared_state_base: shared_state<Rp>
  819. {
  820. typedef shared_state<Rp> base_type;
  821. protected:
  822. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  823. boost::thread thr_;
  824. void join()
  825. {
  826. if (this_thread::get_id() == thr_.get_id())
  827. {
  828. thr_.detach();
  829. return;
  830. }
  831. if (thr_.joinable()) thr_.join();
  832. }
  833. #endif
  834. public:
  835. future_async_shared_state_base()
  836. {
  837. this->set_async();
  838. }
  839. ~future_async_shared_state_base()
  840. {
  841. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  842. join();
  843. #elif defined BOOST_THREAD_ASYNC_FUTURE_WAITS
  844. unique_lock<boost::mutex> lk(this->mutex);
  845. this->waiters.wait(lk, boost::bind(&shared_state_base::is_done, boost::ref(*this)));
  846. #endif
  847. }
  848. virtual void wait(boost::unique_lock<boost::mutex>& lk, bool rethrow)
  849. {
  850. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  851. {
  852. relocker rlk(lk);
  853. join();
  854. }
  855. #endif
  856. this->base_type::wait(lk, rethrow);
  857. }
  858. };
  859. /////////////////////////
  860. /// future_async_shared_state
  861. /////////////////////////
  862. template<typename Rp, typename Fp>
  863. struct future_async_shared_state: future_async_shared_state_base<Rp>
  864. {
  865. future_async_shared_state()
  866. {
  867. }
  868. void init(BOOST_THREAD_FWD_REF(Fp) f)
  869. {
  870. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  871. this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::forward<Fp>(f));
  872. #else
  873. boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::forward<Fp>(f)).detach();
  874. #endif
  875. }
  876. static void run(shared_ptr<future_async_shared_state> that, BOOST_THREAD_FWD_REF(Fp) f)
  877. {
  878. try
  879. {
  880. that->mark_finished_with_result(f());
  881. }
  882. catch(...)
  883. {
  884. that->mark_exceptional_finish();
  885. }
  886. }
  887. };
  888. template<typename Fp>
  889. struct future_async_shared_state<void, Fp>: public future_async_shared_state_base<void>
  890. {
  891. void init(BOOST_THREAD_FWD_REF(Fp) f)
  892. {
  893. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  894. this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f));
  895. #else
  896. boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)).detach();
  897. #endif
  898. }
  899. static void run(shared_ptr<future_async_shared_state> that, BOOST_THREAD_FWD_REF(Fp) f)
  900. {
  901. try
  902. {
  903. f();
  904. that->mark_finished_with_result();
  905. }
  906. catch(...)
  907. {
  908. that->mark_exceptional_finish();
  909. }
  910. }
  911. };
  912. template<typename Rp, typename Fp>
  913. struct future_async_shared_state<Rp&, Fp>: future_async_shared_state_base<Rp&>
  914. {
  915. void init(BOOST_THREAD_FWD_REF(Fp) f)
  916. {
  917. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  918. this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f));
  919. #else
  920. boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)).detach();
  921. #endif
  922. }
  923. static void run(shared_ptr<future_async_shared_state> that, BOOST_THREAD_FWD_REF(Fp) f)
  924. {
  925. try
  926. {
  927. that->mark_finished_with_result(f());
  928. }
  929. catch(...)
  930. {
  931. that->mark_exceptional_finish();
  932. }
  933. }
  934. };
  935. //////////////////////////
  936. /// future_deferred_shared_state
  937. //////////////////////////
  938. template<typename Rp, typename Fp>
  939. struct future_deferred_shared_state: shared_state<Rp>
  940. {
  941. Fp func_;
  942. explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f)
  943. : func_(boost::move(f))
  944. {
  945. this->set_deferred();
  946. }
  947. virtual void execute(boost::unique_lock<boost::mutex>& lck) {
  948. try
  949. {
  950. Fp local_fuct=boost::move(func_);
  951. relocker relock(lck);
  952. Rp res = local_fuct();
  953. relock.lock();
  954. this->mark_finished_with_result_internal(boost::move(res), lck);
  955. }
  956. catch (...)
  957. {
  958. this->mark_exceptional_finish_internal(current_exception(), lck);
  959. }
  960. }
  961. };
  962. template<typename Rp, typename Fp>
  963. struct future_deferred_shared_state<Rp&,Fp>: shared_state<Rp&>
  964. {
  965. Fp func_;
  966. explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f)
  967. : func_(boost::move(f))
  968. {
  969. this->set_deferred();
  970. }
  971. virtual void execute(boost::unique_lock<boost::mutex>& lck) {
  972. try
  973. {
  974. this->mark_finished_with_result_internal(func_(), lck);
  975. }
  976. catch (...)
  977. {
  978. this->mark_exceptional_finish_internal(current_exception(), lck);
  979. }
  980. }
  981. };
  982. template<typename Fp>
  983. struct future_deferred_shared_state<void,Fp>: shared_state<void>
  984. {
  985. Fp func_;
  986. explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f)
  987. : func_(boost::move(f))
  988. {
  989. this->set_deferred();
  990. }
  991. virtual void execute(boost::unique_lock<boost::mutex>& lck) {
  992. try
  993. {
  994. Fp local_fuct=boost::move(func_);
  995. relocker relock(lck);
  996. local_fuct();
  997. relock.lock();
  998. this->mark_finished_with_result_internal(lck);
  999. }
  1000. catch (...)
  1001. {
  1002. this->mark_exceptional_finish_internal(current_exception(), lck);
  1003. }
  1004. }
  1005. };
  1006. class future_waiter
  1007. {
  1008. public:
  1009. typedef std::vector<int>::size_type count_type;
  1010. private:
  1011. struct registered_waiter
  1012. {
  1013. boost::shared_ptr<detail::shared_state_base> future_;
  1014. detail::shared_state_base::notify_when_ready_handle handle;
  1015. count_type index;
  1016. registered_waiter(boost::shared_ptr<detail::shared_state_base> const& a_future,
  1017. detail::shared_state_base::notify_when_ready_handle handle_,
  1018. count_type index_):
  1019. future_(a_future),handle(handle_),index(index_)
  1020. {}
  1021. };
  1022. struct all_futures_lock
  1023. {
  1024. #ifdef _MANAGED
  1025. typedef std::ptrdiff_t count_type_portable;
  1026. #else
  1027. typedef count_type count_type_portable;
  1028. #endif
  1029. count_type_portable count;
  1030. boost::scoped_array<boost::unique_lock<boost::mutex> > locks;
  1031. all_futures_lock(std::vector<registered_waiter>& futures):
  1032. count(futures.size()),locks(new boost::unique_lock<boost::mutex>[count])
  1033. {
  1034. for(count_type_portable i=0;i<count;++i)
  1035. {
  1036. locks[i]=BOOST_THREAD_MAKE_RV_REF(boost::unique_lock<boost::mutex>(futures[i].future_->mutex));
  1037. }
  1038. }
  1039. void lock()
  1040. {
  1041. boost::lock(locks.get(),locks.get()+count);
  1042. }
  1043. void unlock()
  1044. {
  1045. for(count_type_portable i=0;i<count;++i)
  1046. {
  1047. locks[i].unlock();
  1048. }
  1049. }
  1050. };
  1051. boost::condition_variable_any cv;
  1052. std::vector<registered_waiter> futures_;
  1053. count_type future_count;
  1054. public:
  1055. future_waiter():
  1056. future_count(0)
  1057. {}
  1058. template<typename F>
  1059. void add(F& f)
  1060. {
  1061. if(f.future_)
  1062. {
  1063. registered_waiter waiter(f.future_,f.future_->notify_when_ready(cv),future_count);
  1064. try {
  1065. futures_.push_back(waiter);
  1066. } catch(...) {
  1067. f.future_->unnotify_when_ready(waiter.handle);
  1068. throw;
  1069. }
  1070. }
  1071. ++future_count;
  1072. }
  1073. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  1074. template<typename F1, typename... Fs>
  1075. void add(F1& f1, Fs&... fs)
  1076. {
  1077. add(f1); add(fs...);
  1078. }
  1079. #endif
  1080. count_type wait()
  1081. {
  1082. all_futures_lock lk(futures_);
  1083. for(;;)
  1084. {
  1085. for(count_type i=0;i<futures_.size();++i)
  1086. {
  1087. if(futures_[i].future_->done)
  1088. {
  1089. return futures_[i].index;
  1090. }
  1091. }
  1092. cv.wait(lk);
  1093. }
  1094. }
  1095. ~future_waiter()
  1096. {
  1097. for(count_type i=0;i<futures_.size();++i)
  1098. {
  1099. futures_[i].future_->unnotify_when_ready(futures_[i].handle);
  1100. }
  1101. }
  1102. };
  1103. }
  1104. template <typename R>
  1105. class BOOST_THREAD_FUTURE;
  1106. template <typename R>
  1107. class shared_future;
  1108. template<typename T>
  1109. struct is_future_type<BOOST_THREAD_FUTURE<T> > : true_type
  1110. {
  1111. };
  1112. template<typename T>
  1113. struct is_future_type<shared_future<T> > : true_type
  1114. {
  1115. };
  1116. // template<typename Iterator>
  1117. // typename boost::disable_if<is_future_type<Iterator>,Iterator>::type wait_for_any(Iterator begin,Iterator end)
  1118. // {
  1119. // if(begin==end)
  1120. // return end;
  1121. //
  1122. // detail::future_waiter waiter;
  1123. // for(Iterator current=begin;current!=end;++current)
  1124. // {
  1125. // waiter.add(*current);
  1126. // }
  1127. // return boost::next(begin,waiter.wait());
  1128. // }
  1129. #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  1130. template<typename F1,typename F2>
  1131. typename boost::enable_if<is_future_type<F1>,typename detail::future_waiter::count_type>::type wait_for_any(F1& f1,F2& f2)
  1132. {
  1133. detail::future_waiter waiter;
  1134. waiter.add(f1);
  1135. waiter.add(f2);
  1136. return waiter.wait();
  1137. }
  1138. template<typename F1,typename F2,typename F3>
  1139. typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3)
  1140. {
  1141. detail::future_waiter waiter;
  1142. waiter.add(f1);
  1143. waiter.add(f2);
  1144. waiter.add(f3);
  1145. return waiter.wait();
  1146. }
  1147. template<typename F1,typename F2,typename F3,typename F4>
  1148. typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3,F4& f4)
  1149. {
  1150. detail::future_waiter waiter;
  1151. waiter.add(f1);
  1152. waiter.add(f2);
  1153. waiter.add(f3);
  1154. waiter.add(f4);
  1155. return waiter.wait();
  1156. }
  1157. template<typename F1,typename F2,typename F3,typename F4,typename F5>
  1158. typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3,F4& f4,F5& f5)
  1159. {
  1160. detail::future_waiter waiter;
  1161. waiter.add(f1);
  1162. waiter.add(f2);
  1163. waiter.add(f3);
  1164. waiter.add(f4);
  1165. waiter.add(f5);
  1166. return waiter.wait();
  1167. }
  1168. #else
  1169. template<typename F1, typename... Fs>
  1170. typename boost::enable_if<is_future_type<F1>, typename detail::future_waiter::count_type>::type
  1171. wait_for_any(F1& f1, Fs&... fs)
  1172. {
  1173. detail::future_waiter waiter;
  1174. waiter.add(f1, fs...);
  1175. return waiter.wait();
  1176. }
  1177. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1178. template <typename R>
  1179. class promise;
  1180. template <typename R>
  1181. class packaged_task;
  1182. namespace detail
  1183. {
  1184. /// Common implementation for all the futures independently of the return type
  1185. class base_future
  1186. {
  1187. public:
  1188. };
  1189. /// Common implementation for future and shared_future.
  1190. template <typename R>
  1191. class basic_future : public base_future
  1192. {
  1193. protected:
  1194. public:
  1195. typedef boost::shared_ptr<detail::shared_state<R> > future_ptr;
  1196. typedef typename detail::shared_state<R>::move_dest_type move_dest_type;
  1197. static //BOOST_CONSTEXPR
  1198. future_ptr make_exceptional_future_ptr(exceptional_ptr const& ex) {
  1199. return future_ptr(new detail::shared_state<R>(ex));
  1200. }
  1201. future_ptr future_;
  1202. basic_future(future_ptr a_future):
  1203. future_(a_future)
  1204. {
  1205. }
  1206. public:
  1207. typedef future_state::state state;
  1208. BOOST_THREAD_MOVABLE_ONLY(basic_future)
  1209. basic_future(): future_() {}
  1210. //BOOST_CONSTEXPR
  1211. basic_future(exceptional_ptr const& ex)
  1212. : future_(make_exceptional_future_ptr(ex))
  1213. {
  1214. }
  1215. ~basic_future() {
  1216. }
  1217. basic_future(BOOST_THREAD_RV_REF(basic_future) other) BOOST_NOEXCEPT:
  1218. future_(BOOST_THREAD_RV(other).future_)
  1219. {
  1220. BOOST_THREAD_RV(other).future_.reset();
  1221. }
  1222. basic_future& operator=(BOOST_THREAD_RV_REF(basic_future) other) BOOST_NOEXCEPT
  1223. {
  1224. future_=BOOST_THREAD_RV(other).future_;
  1225. BOOST_THREAD_RV(other).future_.reset();
  1226. return *this;
  1227. }
  1228. void swap(basic_future& that) BOOST_NOEXCEPT
  1229. {
  1230. future_.swap(that.future_);
  1231. }
  1232. // functions to check state, and wait for ready
  1233. state get_state(boost::unique_lock<boost::mutex>& lk) const
  1234. {
  1235. if(!future_)
  1236. {
  1237. return future_state::uninitialized;
  1238. }
  1239. return future_->get_state(lk);
  1240. }
  1241. state get_state() const
  1242. {
  1243. if(!future_)
  1244. {
  1245. return future_state::uninitialized;
  1246. }
  1247. return future_->get_state();
  1248. }
  1249. bool is_ready() const
  1250. {
  1251. return get_state()==future_state::ready;
  1252. }
  1253. bool is_ready(boost::unique_lock<boost::mutex>& lk) const
  1254. {
  1255. return get_state(lk)==future_state::ready;
  1256. }
  1257. bool has_exception() const
  1258. {
  1259. return future_ && future_->has_exception();
  1260. }
  1261. bool has_value() const
  1262. {
  1263. return future_ && future_->has_value();
  1264. }
  1265. launch launch_policy(boost::unique_lock<boost::mutex>& lk) const
  1266. {
  1267. if ( future_ ) return future_->launch_policy(lk);
  1268. else return launch(launch::none);
  1269. }
  1270. launch launch_policy() const
  1271. {
  1272. if ( future_ ) {
  1273. boost::unique_lock<boost::mutex> lk(this->future_->mutex);
  1274. return future_->launch_policy(lk);
  1275. }
  1276. else return launch(launch::none);
  1277. }
  1278. exception_ptr get_exception_ptr()
  1279. {
  1280. return future_
  1281. ? future_->get_exception_ptr()
  1282. : exception_ptr();
  1283. }
  1284. bool valid() const BOOST_NOEXCEPT
  1285. {
  1286. return future_.get() != 0 && future_->valid();
  1287. }
  1288. void wait() const
  1289. {
  1290. if(!future_)
  1291. {
  1292. boost::throw_exception(future_uninitialized());
  1293. }
  1294. future_->wait(false);
  1295. }
  1296. typedef detail::shared_state_base::notify_when_ready_handle notify_when_ready_handle;
  1297. boost::mutex& mutex() {
  1298. if(!future_)
  1299. {
  1300. boost::throw_exception(future_uninitialized());
  1301. }
  1302. return future_->mutex;
  1303. }
  1304. notify_when_ready_handle notify_when_ready(boost::condition_variable_any& cv)
  1305. {
  1306. if(!future_)
  1307. {
  1308. boost::throw_exception(future_uninitialized());
  1309. }
  1310. return future_->notify_when_ready(cv);
  1311. }
  1312. void unnotify_when_ready(notify_when_ready_handle h)
  1313. {
  1314. if(!future_)
  1315. {
  1316. boost::throw_exception(future_uninitialized());
  1317. }
  1318. return future_->unnotify_when_ready(h);
  1319. }
  1320. #if defined BOOST_THREAD_USES_DATETIME
  1321. template<typename Duration>
  1322. bool timed_wait(Duration const& rel_time) const
  1323. {
  1324. if(!future_)
  1325. {
  1326. boost::throw_exception(future_uninitialized());
  1327. }
  1328. return future_->timed_wait(rel_time);
  1329. }
  1330. bool timed_wait_until(boost::system_time const& abs_time) const
  1331. {
  1332. if(!future_)
  1333. {
  1334. boost::throw_exception(future_uninitialized());
  1335. }
  1336. return future_->timed_wait_until(abs_time);
  1337. }
  1338. #endif
  1339. #ifdef BOOST_THREAD_USES_CHRONO
  1340. template <class Rep, class Period>
  1341. future_status
  1342. wait_for(const chrono::duration<Rep, Period>& rel_time) const
  1343. {
  1344. return wait_until(chrono::steady_clock::now() + rel_time);
  1345. }
  1346. template <class Clock, class Duration>
  1347. future_status
  1348. wait_until(const chrono::time_point<Clock, Duration>& abs_time) const
  1349. {
  1350. if(!future_)
  1351. {
  1352. boost::throw_exception(future_uninitialized());
  1353. }
  1354. return future_->wait_until(abs_time);
  1355. }
  1356. #endif
  1357. };
  1358. } // detail
  1359. BOOST_THREAD_DCL_MOVABLE_BEG(R) detail::basic_future<R> BOOST_THREAD_DCL_MOVABLE_END
  1360. namespace detail
  1361. {
  1362. #if (!defined _MSC_VER || _MSC_VER >= 1400) // _MSC_VER == 1400 on MSVC 2005
  1363. template <class Rp, class Fp>
  1364. BOOST_THREAD_FUTURE<Rp>
  1365. make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1366. template <class Rp, class Fp>
  1367. BOOST_THREAD_FUTURE<Rp>
  1368. make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1369. #endif // #if (!defined _MSC_VER || _MSC_VER >= 1400)
  1370. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1371. template<typename F, typename Rp, typename Fp>
  1372. struct future_deferred_continuation_shared_state;
  1373. template<typename F, typename Rp, typename Fp>
  1374. struct future_async_continuation_shared_state;
  1375. template <class F, class Rp, class Fp>
  1376. BOOST_THREAD_FUTURE<Rp>
  1377. make_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1378. template <class F, class Rp, class Fp>
  1379. BOOST_THREAD_FUTURE<Rp>
  1380. make_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1381. template <class F, class Rp, class Fp>
  1382. BOOST_THREAD_FUTURE<Rp>
  1383. make_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1384. template<typename F, typename Rp, typename Fp>
  1385. BOOST_THREAD_FUTURE<Rp>
  1386. make_shared_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1387. template<typename F, typename Rp, typename Fp>
  1388. BOOST_THREAD_FUTURE<Rp>
  1389. make_shared_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1390. template<typename F, typename Rp, typename Fp>
  1391. BOOST_THREAD_FUTURE<Rp>
  1392. make_shared_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1393. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1394. template<typename Ex, typename F, typename Rp, typename Fp>
  1395. BOOST_THREAD_FUTURE<Rp>
  1396. make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1397. template<typename Ex, typename F, typename Rp, typename Fp>
  1398. BOOST_THREAD_FUTURE<Rp>
  1399. make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1400. template <class Rp, class Fp, class Executor>
  1401. BOOST_THREAD_FUTURE<Rp>
  1402. make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f);
  1403. #endif
  1404. #endif
  1405. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  1406. template<typename F, typename Rp>
  1407. struct future_unwrap_shared_state;
  1408. template <class F, class Rp>
  1409. inline BOOST_THREAD_FUTURE<Rp>
  1410. make_future_unwrap_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f);
  1411. #endif
  1412. }
  1413. #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY)
  1414. template< typename InputIterator>
  1415. typename boost::disable_if<is_future_type<InputIterator>,
  1416. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1417. >::type
  1418. when_all(InputIterator first, InputIterator last);
  1419. inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_all();
  1420. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1421. template< typename T0, typename ...T>
  1422. BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1423. when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1424. #endif
  1425. template< typename InputIterator>
  1426. typename boost::disable_if<is_future_type<InputIterator>,
  1427. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1428. >::type
  1429. when_any(InputIterator first, InputIterator last);
  1430. inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_any();
  1431. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1432. template< typename T0, typename ...T>
  1433. BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1434. when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1435. #endif
  1436. #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  1437. template <typename R>
  1438. class BOOST_THREAD_FUTURE : public detail::basic_future<R>
  1439. {
  1440. private:
  1441. typedef detail::basic_future<R> base_type;
  1442. typedef typename base_type::future_ptr future_ptr;
  1443. friend class shared_future<R>;
  1444. friend class promise<R>;
  1445. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1446. template <typename, typename, typename>
  1447. friend struct detail::future_async_continuation_shared_state;
  1448. template <typename, typename, typename>
  1449. friend struct detail::future_deferred_continuation_shared_state;
  1450. template <class F, class Rp, class Fp>
  1451. friend BOOST_THREAD_FUTURE<Rp>
  1452. detail::make_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1453. template <class F, class Rp, class Fp>
  1454. friend BOOST_THREAD_FUTURE<Rp>
  1455. detail::make_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1456. template <class F, class Rp, class Fp>
  1457. friend BOOST_THREAD_FUTURE<Rp>
  1458. detail::make_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1459. template<typename F, typename Rp, typename Fp>
  1460. friend BOOST_THREAD_FUTURE<Rp>
  1461. detail::make_shared_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1462. template<typename F, typename Rp, typename Fp>
  1463. friend BOOST_THREAD_FUTURE<Rp>
  1464. detail::make_shared_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1465. template<typename F, typename Rp, typename Fp>
  1466. friend BOOST_THREAD_FUTURE<Rp>
  1467. detail::make_shared_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1468. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1469. template<typename Ex, typename F, typename Rp, typename Fp>
  1470. friend BOOST_THREAD_FUTURE<Rp>
  1471. detail::make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1472. template<typename Ex, typename F, typename Rp, typename Fp>
  1473. friend BOOST_THREAD_FUTURE<Rp>
  1474. detail::make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1475. template <class Rp, class Fp, class Executor>
  1476. friend BOOST_THREAD_FUTURE<Rp>
  1477. detail::make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f);
  1478. #endif
  1479. #endif
  1480. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  1481. template<typename F, typename Rp>
  1482. friend struct detail::future_unwrap_shared_state;
  1483. template <class F, class Rp>
  1484. friend BOOST_THREAD_FUTURE<Rp>
  1485. detail::make_future_unwrap_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f);
  1486. #endif
  1487. #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY)
  1488. template< typename InputIterator>
  1489. friend typename boost::disable_if<is_future_type<InputIterator>,
  1490. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1491. >::type
  1492. when_all(InputIterator first, InputIterator last);
  1493. //friend inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_all();
  1494. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1495. template< typename T0, typename ...T>
  1496. friend BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1497. when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1498. #endif
  1499. template< typename InputIterator>
  1500. friend typename boost::disable_if<is_future_type<InputIterator>,
  1501. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1502. >::type
  1503. when_any(InputIterator first, InputIterator last);
  1504. //friend inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_any();
  1505. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1506. template< typename T0, typename ...T>
  1507. friend BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1508. when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1509. #endif
  1510. #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  1511. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  1512. template <class> friend class packaged_task; // todo check if this works in windows
  1513. #else
  1514. friend class packaged_task<R>;
  1515. #endif
  1516. friend class detail::future_waiter;
  1517. template <class Rp, class Fp>
  1518. friend BOOST_THREAD_FUTURE<Rp>
  1519. detail::make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1520. template <class Rp, class Fp>
  1521. friend BOOST_THREAD_FUTURE<Rp>
  1522. detail::make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1523. typedef typename base_type::move_dest_type move_dest_type;
  1524. BOOST_THREAD_FUTURE(future_ptr a_future):
  1525. base_type(a_future)
  1526. {
  1527. }
  1528. public:
  1529. BOOST_THREAD_MOVABLE_ONLY(BOOST_THREAD_FUTURE)
  1530. typedef future_state::state state;
  1531. typedef R value_type; // EXTENSION
  1532. BOOST_CONSTEXPR BOOST_THREAD_FUTURE() {}
  1533. //BOOST_CONSTEXPR
  1534. BOOST_THREAD_FUTURE(exceptional_ptr const& ex):
  1535. base_type(ex) {}
  1536. ~BOOST_THREAD_FUTURE() {
  1537. }
  1538. BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT:
  1539. base_type(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))))
  1540. {
  1541. }
  1542. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  1543. inline explicit BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R> >) other); // EXTENSION
  1544. #endif
  1545. explicit BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(shared_future<R>) other) :
  1546. base_type(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))))
  1547. {}
  1548. BOOST_THREAD_FUTURE& operator=(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT
  1549. {
  1550. this->base_type::operator=(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))));
  1551. return *this;
  1552. }
  1553. shared_future<R> share()
  1554. {
  1555. return shared_future<R>(::boost::move(*this));
  1556. }
  1557. void swap(BOOST_THREAD_FUTURE& other)
  1558. {
  1559. static_cast<base_type*>(this)->swap(other);
  1560. }
  1561. // todo this function must be private and friendship provided to the internal users.
  1562. void set_async()
  1563. {
  1564. this->future_->set_async();
  1565. }
  1566. // todo this function must be private and friendship provided to the internal users.
  1567. void set_deferred()
  1568. {
  1569. this->future_->set_deferred();
  1570. }
  1571. bool run_if_is_deferred() {
  1572. return this->future_->run_if_is_deferred();
  1573. }
  1574. bool run_if_is_deferred_or_ready() {
  1575. return this->future_->run_if_is_deferred_or_ready();
  1576. }
  1577. // retrieving the value
  1578. move_dest_type get()
  1579. {
  1580. if (this->future_.get() == 0)
  1581. {
  1582. boost::throw_exception(future_uninitialized());
  1583. }
  1584. unique_lock<boost::mutex> lk(this->future_->mutex);
  1585. if (! this->future_->valid(lk))
  1586. {
  1587. boost::throw_exception(future_uninitialized());
  1588. }
  1589. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1590. this->future_->invalidate(lk);
  1591. #endif
  1592. return this->future_->get(lk);
  1593. }
  1594. template <typename R2>
  1595. typename boost::disable_if< is_void<R2>, move_dest_type>::type
  1596. get_or(BOOST_THREAD_RV_REF(R2) v)
  1597. {
  1598. if (this->future_.get() == 0)
  1599. {
  1600. boost::throw_exception(future_uninitialized());
  1601. }
  1602. unique_lock<boost::mutex> lk(this->future_->mutex);
  1603. if (! this->future_->valid(lk))
  1604. {
  1605. boost::throw_exception(future_uninitialized());
  1606. }
  1607. this->future_->wait(lk, false);
  1608. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1609. this->future_->invalidate(lk);
  1610. #endif
  1611. if (this->future_->has_value(lk)) {
  1612. return this->future_->get(lk);
  1613. }
  1614. else {
  1615. return boost::move(v);
  1616. }
  1617. }
  1618. template <typename R2>
  1619. typename boost::disable_if< is_void<R2>, move_dest_type>::type
  1620. get_or(R2 const& v) // EXTENSION
  1621. {
  1622. if (this->future_.get() == 0)
  1623. {
  1624. boost::throw_exception(future_uninitialized());
  1625. }
  1626. unique_lock<boost::mutex> lk(this->future_->mutex);
  1627. if (! this->future_->valid(lk))
  1628. {
  1629. boost::throw_exception(future_uninitialized());
  1630. }
  1631. this->future_->wait(lk, false);
  1632. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1633. this->future_->invalidate(lk);
  1634. #endif
  1635. if (this->future_->has_value(lk)) {
  1636. return this->future_->get(lk);
  1637. }
  1638. else {
  1639. return v;
  1640. }
  1641. }
  1642. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1643. template<typename F>
  1644. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1645. then(BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1646. template<typename F>
  1647. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1648. then(launch policy, BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1649. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1650. template<typename Ex, typename F>
  1651. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1652. then(Ex& ex, BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1653. #endif
  1654. template <typename R2>
  1655. inline typename boost::disable_if< is_void<R2>, BOOST_THREAD_FUTURE<R> >::type
  1656. fallback_to(BOOST_THREAD_RV_REF(R2) v); // EXTENSION
  1657. template <typename R2>
  1658. inline typename boost::disable_if< is_void<R2>, BOOST_THREAD_FUTURE<R> >::type
  1659. fallback_to(R2 const& v); // EXTENSION
  1660. #endif
  1661. };
  1662. BOOST_THREAD_DCL_MOVABLE_BEG(T) BOOST_THREAD_FUTURE<T> BOOST_THREAD_DCL_MOVABLE_END
  1663. template <typename R2>
  1664. class BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> > : public detail::basic_future<BOOST_THREAD_FUTURE<R2> >
  1665. {
  1666. typedef BOOST_THREAD_FUTURE<R2> R;
  1667. private:
  1668. typedef detail::basic_future<R> base_type;
  1669. typedef typename base_type::future_ptr future_ptr;
  1670. friend class shared_future<R>;
  1671. friend class promise<R>;
  1672. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1673. template <typename, typename, typename>
  1674. friend struct detail::future_async_continuation_shared_state;
  1675. template <typename, typename, typename>
  1676. friend struct detail::future_deferred_continuation_shared_state;
  1677. template <class F, class Rp, class Fp>
  1678. friend BOOST_THREAD_FUTURE<Rp>
  1679. detail::make_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1680. template <class F, class Rp, class Fp>
  1681. friend BOOST_THREAD_FUTURE<Rp>
  1682. detail::make_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1683. template <class F, class Rp, class Fp>
  1684. friend BOOST_THREAD_FUTURE<Rp>
  1685. detail::make_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1686. template<typename F, typename Rp, typename Fp>
  1687. friend BOOST_THREAD_FUTURE<Rp>
  1688. detail::make_shared_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1689. template<typename F, typename Rp, typename Fp>
  1690. friend BOOST_THREAD_FUTURE<Rp>
  1691. detail::make_shared_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1692. template<typename F, typename Rp, typename Fp>
  1693. friend BOOST_THREAD_FUTURE<Rp>
  1694. detail::make_shared_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1695. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1696. template<typename Ex, typename F, typename Rp, typename Fp>
  1697. friend BOOST_THREAD_FUTURE<Rp>
  1698. detail::make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1699. template<typename Ex, typename F, typename Rp, typename Fp>
  1700. friend BOOST_THREAD_FUTURE<Rp>
  1701. detail::make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock<boost::mutex> &lock, F f, BOOST_THREAD_FWD_REF(Fp) c);
  1702. template <class Rp, class Fp, class Executor>
  1703. friend BOOST_THREAD_FUTURE<Rp>
  1704. detail::make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f);
  1705. #endif
  1706. #endif
  1707. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  1708. template<typename F, typename Rp>
  1709. friend struct detail::future_unwrap_shared_state;
  1710. template <class F, class Rp>
  1711. friend BOOST_THREAD_FUTURE<Rp>
  1712. detail::make_future_unwrap_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f);
  1713. #endif
  1714. #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY)
  1715. template< typename InputIterator>
  1716. friend typename boost::disable_if<is_future_type<InputIterator>,
  1717. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1718. >::type
  1719. when_all(InputIterator first, InputIterator last);
  1720. friend inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_all();
  1721. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1722. template< typename T0, typename ...T>
  1723. friend BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1724. when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1725. #endif
  1726. template< typename InputIterator>
  1727. friend typename boost::disable_if<is_future_type<InputIterator>,
  1728. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  1729. >::type
  1730. when_any(InputIterator first, InputIterator last);
  1731. friend inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_any();
  1732. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1733. template< typename T0, typename ...T>
  1734. friend BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  1735. when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures);
  1736. #endif
  1737. #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  1738. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  1739. template <class> friend class packaged_task; // todo check if this works in windows
  1740. #else
  1741. friend class packaged_task<R>;
  1742. #endif
  1743. friend class detail::future_waiter;
  1744. template <class Rp, class Fp>
  1745. friend BOOST_THREAD_FUTURE<Rp>
  1746. detail::make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1747. template <class Rp, class Fp>
  1748. friend BOOST_THREAD_FUTURE<Rp>
  1749. detail::make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f);
  1750. typedef typename base_type::move_dest_type move_dest_type;
  1751. BOOST_THREAD_FUTURE(future_ptr a_future):
  1752. base_type(a_future)
  1753. {
  1754. }
  1755. public:
  1756. BOOST_THREAD_MOVABLE_ONLY(BOOST_THREAD_FUTURE)
  1757. typedef future_state::state state;
  1758. typedef R value_type; // EXTENSION
  1759. BOOST_CONSTEXPR BOOST_THREAD_FUTURE() {}
  1760. //BOOST_CONSTEXPR
  1761. BOOST_THREAD_FUTURE(exceptional_ptr const& ex):
  1762. base_type(ex) {}
  1763. ~BOOST_THREAD_FUTURE() {
  1764. }
  1765. BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT:
  1766. base_type(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))))
  1767. {
  1768. }
  1769. BOOST_THREAD_FUTURE& operator=(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT
  1770. {
  1771. this->base_type::operator=(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))));
  1772. return *this;
  1773. }
  1774. shared_future<R> share()
  1775. {
  1776. return shared_future<R>(::boost::move(*this));
  1777. }
  1778. void swap(BOOST_THREAD_FUTURE& other)
  1779. {
  1780. static_cast<base_type*>(this)->swap(other);
  1781. }
  1782. // todo this function must be private and friendship provided to the internal users.
  1783. void set_async()
  1784. {
  1785. this->future_->set_async();
  1786. }
  1787. // todo this function must be private and friendship provided to the internal users.
  1788. void set_deferred()
  1789. {
  1790. this->future_->set_deferred();
  1791. }
  1792. bool run_if_is_deferred() {
  1793. return this->future_->run_if_is_deferred();
  1794. }
  1795. bool run_if_is_deferred_or_ready() {
  1796. return this->future_->run_if_is_deferred_or_ready();
  1797. }
  1798. // retrieving the value
  1799. move_dest_type get()
  1800. {
  1801. if (this->future_.get() == 0)
  1802. {
  1803. boost::throw_exception(future_uninitialized());
  1804. }
  1805. unique_lock<boost::mutex> lk(this->future_->mutex);
  1806. if (! this->future_->valid(lk))
  1807. {
  1808. boost::throw_exception(future_uninitialized());
  1809. }
  1810. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1811. this->future_->invalidate(lk);
  1812. #endif
  1813. return this->future_->get(lk);
  1814. }
  1815. move_dest_type get_or(BOOST_THREAD_RV_REF(R) v) // EXTENSION
  1816. {
  1817. if (this->future_.get() == 0)
  1818. {
  1819. boost::throw_exception(future_uninitialized());
  1820. }
  1821. unique_lock<boost::mutex> lk(this->future_->mutex);
  1822. if (! this->future_->valid(lk))
  1823. {
  1824. boost::throw_exception(future_uninitialized());
  1825. }
  1826. this->future_->wait(lk, false);
  1827. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1828. this->future_->invalidate(lk);
  1829. #endif
  1830. if (this->future_->has_value(lk)) return this->future_->get(lk);
  1831. else return boost::move(v);
  1832. }
  1833. move_dest_type get_or(R const& v) // EXTENSION
  1834. {
  1835. if (this->future_.get() == 0)
  1836. {
  1837. boost::throw_exception(future_uninitialized());
  1838. }
  1839. unique_lock<boost::mutex> lk(this->future_->mutex);
  1840. if (! this->future_->valid(lk))
  1841. {
  1842. boost::throw_exception(future_uninitialized());
  1843. }
  1844. this->future_->wait(lk, false);
  1845. #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
  1846. this->future_->invalidate(lk);
  1847. #endif
  1848. if (this->future_->has_value(lk)) return this->future_->get(lk);
  1849. else return v;
  1850. }
  1851. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1852. template<typename F>
  1853. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1854. then(BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1855. template<typename F>
  1856. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1857. then(launch policy, BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1858. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1859. template<typename Ex, typename F>
  1860. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE)>::type>
  1861. then(Ex &ex, BOOST_THREAD_FWD_REF(F) func); // EXTENSION
  1862. #endif
  1863. #endif
  1864. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  1865. inline
  1866. BOOST_THREAD_FUTURE<R2>
  1867. unwrap(); // EXTENSION
  1868. #endif
  1869. };
  1870. template <typename R>
  1871. class shared_future : public detail::basic_future<R>
  1872. {
  1873. typedef detail::basic_future<R> base_type;
  1874. typedef typename base_type::future_ptr future_ptr;
  1875. friend class detail::future_waiter;
  1876. friend class promise<R>;
  1877. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1878. template <typename, typename, typename>
  1879. friend struct detail::future_async_continuation_shared_state;
  1880. template <typename, typename, typename>
  1881. friend struct detail::future_deferred_continuation_shared_state;
  1882. template <class F, class Rp, class Fp>
  1883. friend BOOST_THREAD_FUTURE<Rp>
  1884. detail::make_future_async_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1885. template <class F, class Rp, class Fp>
  1886. friend BOOST_THREAD_FUTURE<Rp>
  1887. detail::make_future_sync_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1888. template <class F, class Rp, class Fp>
  1889. friend BOOST_THREAD_FUTURE<Rp>
  1890. detail::make_future_deferred_continuation_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c);
  1891. #endif
  1892. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  1893. template <class> friend class packaged_task;// todo check if this works in windows
  1894. #else
  1895. friend class packaged_task<R>;
  1896. #endif
  1897. shared_future(future_ptr a_future):
  1898. base_type(a_future)
  1899. {}
  1900. public:
  1901. BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_future)
  1902. typedef R value_type; // EXTENSION
  1903. shared_future(shared_future const& other):
  1904. base_type(other.future_)
  1905. {}
  1906. typedef future_state::state state;
  1907. BOOST_CONSTEXPR shared_future()
  1908. {}
  1909. //BOOST_CONSTEXPR
  1910. shared_future(exceptional_ptr const& ex):
  1911. base_type(ex) {}
  1912. ~shared_future()
  1913. {}
  1914. shared_future& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_future) other)
  1915. {
  1916. this->future_ = other.future_;
  1917. return *this;
  1918. }
  1919. shared_future(BOOST_THREAD_RV_REF(shared_future) other) BOOST_NOEXCEPT :
  1920. base_type(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))))
  1921. {
  1922. }
  1923. shared_future(BOOST_THREAD_RV_REF( BOOST_THREAD_FUTURE<R> ) other) BOOST_NOEXCEPT :
  1924. base_type(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))))
  1925. {
  1926. }
  1927. shared_future& operator=(BOOST_THREAD_RV_REF(shared_future) other) BOOST_NOEXCEPT
  1928. {
  1929. base_type::operator=(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))));
  1930. return *this;
  1931. }
  1932. shared_future& operator=(BOOST_THREAD_RV_REF( BOOST_THREAD_FUTURE<R> ) other) BOOST_NOEXCEPT
  1933. {
  1934. base_type::operator=(boost::move(static_cast<base_type&>(BOOST_THREAD_RV(other))));
  1935. return *this;
  1936. }
  1937. void swap(shared_future& other) BOOST_NOEXCEPT
  1938. {
  1939. static_cast<base_type*>(this)->swap(other);
  1940. }
  1941. bool run_if_is_deferred() {
  1942. return this->future_->run_if_is_deferred();
  1943. }
  1944. bool run_if_is_deferred_or_ready() {
  1945. return this->future_->run_if_is_deferred_or_ready();
  1946. }
  1947. // retrieving the value
  1948. typename detail::shared_state<R>::shared_future_get_result_type get() const
  1949. {
  1950. if(!this->future_)
  1951. {
  1952. boost::throw_exception(future_uninitialized());
  1953. }
  1954. return this->future_->get_sh();
  1955. }
  1956. template <typename R2>
  1957. typename boost::disable_if< is_void<R2>, typename detail::shared_state<R>::shared_future_get_result_type>::type
  1958. get_or(BOOST_THREAD_RV_REF(R2) v) const // EXTENSION
  1959. {
  1960. if(!this->future_)
  1961. {
  1962. boost::throw_exception(future_uninitialized());
  1963. }
  1964. this->future_->wait();
  1965. if (this->future_->has_value()) return this->future_->get_sh();
  1966. else return boost::move(v);
  1967. }
  1968. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  1969. template<typename F>
  1970. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future)>::type>
  1971. then(BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION
  1972. template<typename F>
  1973. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future)>::type>
  1974. then(launch policy, BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION
  1975. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  1976. template<typename Ex, typename F>
  1977. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future)>::type>
  1978. then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION
  1979. #endif
  1980. #endif
  1981. };
  1982. BOOST_THREAD_DCL_MOVABLE_BEG(T) shared_future<T> BOOST_THREAD_DCL_MOVABLE_END
  1983. template <typename R>
  1984. class promise
  1985. {
  1986. typedef boost::shared_ptr<detail::shared_state<R> > future_ptr;
  1987. typedef typename detail::shared_state<R>::source_reference_type source_reference_type;
  1988. typedef typename detail::shared_state<R>::rvalue_source_type rvalue_source_type;
  1989. typedef typename detail::shared_state<R>::move_dest_type move_dest_type;
  1990. typedef typename detail::shared_state<R>::shared_future_get_result_type shared_future_get_result_type;
  1991. future_ptr future_;
  1992. bool future_obtained;
  1993. void lazy_init()
  1994. {
  1995. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  1996. #include <boost/thread/detail/atomic_undef_macros.hpp>
  1997. if(!atomic_load(&future_))
  1998. {
  1999. future_ptr blank;
  2000. atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state<R>));
  2001. }
  2002. #include <boost/thread/detail/atomic_redef_macros.hpp>
  2003. #endif
  2004. }
  2005. public:
  2006. BOOST_THREAD_MOVABLE_ONLY(promise)
  2007. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  2008. template <class Allocator>
  2009. promise(boost::allocator_arg_t, Allocator a)
  2010. {
  2011. typedef typename Allocator::template rebind<detail::shared_state<R> >::other A2;
  2012. A2 a2(a);
  2013. typedef thread_detail::allocator_destructor<A2> D;
  2014. future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state<R>(), D(a2, 1) );
  2015. future_obtained = false;
  2016. }
  2017. #endif
  2018. promise():
  2019. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  2020. future_(),
  2021. #else
  2022. future_(new detail::shared_state<R>()),
  2023. #endif
  2024. future_obtained(false)
  2025. {}
  2026. ~promise()
  2027. {
  2028. if(future_)
  2029. {
  2030. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2031. if(!future_->done && !future_->is_constructed)
  2032. {
  2033. future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock);
  2034. }
  2035. }
  2036. }
  2037. // Assignment
  2038. promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT :
  2039. future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained)
  2040. {
  2041. BOOST_THREAD_RV(rhs).future_.reset();
  2042. BOOST_THREAD_RV(rhs).future_obtained=false;
  2043. }
  2044. promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT
  2045. {
  2046. future_=BOOST_THREAD_RV(rhs).future_;
  2047. future_obtained=BOOST_THREAD_RV(rhs).future_obtained;
  2048. BOOST_THREAD_RV(rhs).future_.reset();
  2049. BOOST_THREAD_RV(rhs).future_obtained=false;
  2050. return *this;
  2051. }
  2052. void swap(promise& other)
  2053. {
  2054. future_.swap(other.future_);
  2055. std::swap(future_obtained,other.future_obtained);
  2056. }
  2057. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  2058. void set_executor(executor_ptr_type aex)
  2059. {
  2060. lazy_init();
  2061. if (future_.get()==0)
  2062. {
  2063. boost::throw_exception(promise_moved());
  2064. }
  2065. boost::lock_guard<boost::mutex> lk(future_->mutex);
  2066. future_->set_executor_policy(aex, lk);
  2067. }
  2068. #endif
  2069. // Result retrieval
  2070. BOOST_THREAD_FUTURE<R> get_future()
  2071. {
  2072. lazy_init();
  2073. if (future_.get()==0)
  2074. {
  2075. boost::throw_exception(promise_moved());
  2076. }
  2077. if (future_obtained)
  2078. {
  2079. boost::throw_exception(future_already_retrieved());
  2080. }
  2081. future_obtained=true;
  2082. return BOOST_THREAD_FUTURE<R>(future_);
  2083. }
  2084. #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2085. template <class TR>
  2086. typename boost::enable_if_c<is_copy_constructible<TR>::value && is_same<R, TR>::value, void>::type
  2087. set_value(TR const & r)
  2088. {
  2089. lazy_init();
  2090. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2091. if(future_->done)
  2092. {
  2093. boost::throw_exception(promise_already_satisfied());
  2094. }
  2095. future_->mark_finished_with_result_internal(r, lock);
  2096. }
  2097. #else
  2098. void set_value(source_reference_type r)
  2099. {
  2100. lazy_init();
  2101. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2102. if(future_->done)
  2103. {
  2104. boost::throw_exception(promise_already_satisfied());
  2105. }
  2106. future_->mark_finished_with_result_internal(r, lock);
  2107. }
  2108. #endif
  2109. void set_value(rvalue_source_type r)
  2110. {
  2111. lazy_init();
  2112. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2113. if(future_->done)
  2114. {
  2115. boost::throw_exception(promise_already_satisfied());
  2116. }
  2117. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2118. future_->mark_finished_with_result_internal(boost::move(r), lock);
  2119. #else
  2120. future_->mark_finished_with_result_internal(static_cast<rvalue_source_type>(r), lock);
  2121. #endif
  2122. }
  2123. #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2124. template <class TR>
  2125. typename boost::enable_if_c<is_copy_constructible<TR>::value && is_same<R, TR>::value, void>::type
  2126. set_value_deferred(TR const & r)
  2127. {
  2128. lazy_init();
  2129. if (future_.get()==0)
  2130. {
  2131. boost::throw_exception(promise_moved());
  2132. }
  2133. future_->set_value_deferred(r);
  2134. }
  2135. #else
  2136. void set_value_deferred(source_reference_type r)
  2137. {
  2138. lazy_init();
  2139. if (future_.get()==0)
  2140. {
  2141. boost::throw_exception(promise_moved());
  2142. }
  2143. future_->set_value_deferred(r);
  2144. }
  2145. #endif
  2146. void set_value_deferred(rvalue_source_type r)
  2147. {
  2148. lazy_init();
  2149. if (future_.get()==0)
  2150. {
  2151. boost::throw_exception(promise_moved());
  2152. }
  2153. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2154. future_->set_value_deferred(boost::move(r));
  2155. #else
  2156. future_->set_value_deferred(static_cast<rvalue_source_type>(r));
  2157. #endif
  2158. }
  2159. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  2160. template <class ...Args>
  2161. void emplace(BOOST_THREAD_FWD_REF(Args) ...args)
  2162. {
  2163. lazy_init();
  2164. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2165. if(future_->done)
  2166. {
  2167. boost::throw_exception(promise_already_satisfied());
  2168. }
  2169. future_->mark_finished_with_result_internal(lock, boost::forward<Args>(args)...);
  2170. }
  2171. #endif
  2172. void set_exception(boost::exception_ptr p)
  2173. {
  2174. lazy_init();
  2175. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2176. if(future_->done)
  2177. {
  2178. boost::throw_exception(promise_already_satisfied());
  2179. }
  2180. future_->mark_exceptional_finish_internal(p, lock);
  2181. }
  2182. template <typename E>
  2183. void set_exception(E ex)
  2184. {
  2185. set_exception(boost::copy_exception(ex));
  2186. }
  2187. void set_exception_deferred(boost::exception_ptr p)
  2188. {
  2189. lazy_init();
  2190. if (future_.get()==0)
  2191. {
  2192. boost::throw_exception(promise_moved());
  2193. }
  2194. future_->set_exception_deferred(p);
  2195. }
  2196. template <typename E>
  2197. void set_exception_deferred(E ex)
  2198. {
  2199. set_exception_deferred(boost::copy_exception(ex));
  2200. }
  2201. // setting the result with deferred notification
  2202. #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2203. template <class TR>
  2204. typename boost::enable_if_c<is_copy_constructible<TR>::value && is_same<R, TR>::value, void>::type set_value_at_thread_exit(TR const& r)
  2205. {
  2206. if (future_.get()==0)
  2207. {
  2208. boost::throw_exception(promise_moved());
  2209. }
  2210. future_->set_value_at_thread_exit(r);
  2211. }
  2212. #else
  2213. void set_value_at_thread_exit(source_reference_type r)
  2214. {
  2215. if (future_.get()==0)
  2216. {
  2217. boost::throw_exception(promise_moved());
  2218. }
  2219. future_->set_value_at_thread_exit(r);
  2220. }
  2221. #endif
  2222. void set_value_at_thread_exit(BOOST_THREAD_RV_REF(R) r)
  2223. {
  2224. if (future_.get()==0)
  2225. {
  2226. boost::throw_exception(promise_moved());
  2227. }
  2228. future_->set_value_at_thread_exit(boost::move(r));
  2229. }
  2230. void set_exception_at_thread_exit(exception_ptr e)
  2231. {
  2232. if (future_.get()==0)
  2233. {
  2234. boost::throw_exception(promise_moved());
  2235. }
  2236. future_->set_exception_at_thread_exit(e);
  2237. }
  2238. template <typename E>
  2239. void set_exception_at_thread_exit(E ex)
  2240. {
  2241. set_exception_at_thread_exit(boost::copy_exception(ex));
  2242. }
  2243. template<typename F>
  2244. void set_wait_callback(F f)
  2245. {
  2246. lazy_init();
  2247. future_->set_wait_callback(f,this);
  2248. }
  2249. void notify_deferred()
  2250. {
  2251. if (future_.get()==0)
  2252. {
  2253. boost::throw_exception(promise_moved());
  2254. }
  2255. future_->notify_deferred();
  2256. }
  2257. };
  2258. template <typename R>
  2259. class promise<R&>
  2260. {
  2261. typedef boost::shared_ptr<detail::shared_state<R&> > future_ptr;
  2262. future_ptr future_;
  2263. bool future_obtained;
  2264. void lazy_init()
  2265. {
  2266. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  2267. #include <boost/thread/detail/atomic_undef_macros.hpp>
  2268. if(!atomic_load(&future_))
  2269. {
  2270. future_ptr blank;
  2271. atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state<R&>));
  2272. }
  2273. #include <boost/thread/detail/atomic_redef_macros.hpp>
  2274. #endif
  2275. }
  2276. public:
  2277. BOOST_THREAD_MOVABLE_ONLY(promise)
  2278. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  2279. template <class Allocator>
  2280. promise(boost::allocator_arg_t, Allocator a)
  2281. {
  2282. typedef typename Allocator::template rebind<detail::shared_state<R&> >::other A2;
  2283. A2 a2(a);
  2284. typedef thread_detail::allocator_destructor<A2> D;
  2285. future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state<R&>(), D(a2, 1) );
  2286. future_obtained = false;
  2287. }
  2288. #endif
  2289. promise():
  2290. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  2291. future_(),
  2292. #else
  2293. future_(new detail::shared_state<R&>()),
  2294. #endif
  2295. future_obtained(false)
  2296. {}
  2297. ~promise()
  2298. {
  2299. if(future_)
  2300. {
  2301. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2302. if(!future_->done && !future_->is_constructed)
  2303. {
  2304. future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock);
  2305. }
  2306. }
  2307. }
  2308. // Assignment
  2309. promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT :
  2310. future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained)
  2311. {
  2312. BOOST_THREAD_RV(rhs).future_.reset();
  2313. BOOST_THREAD_RV(rhs).future_obtained=false;
  2314. }
  2315. promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT
  2316. {
  2317. future_=BOOST_THREAD_RV(rhs).future_;
  2318. future_obtained=BOOST_THREAD_RV(rhs).future_obtained;
  2319. BOOST_THREAD_RV(rhs).future_.reset();
  2320. BOOST_THREAD_RV(rhs).future_obtained=false;
  2321. return *this;
  2322. }
  2323. void swap(promise& other)
  2324. {
  2325. future_.swap(other.future_);
  2326. std::swap(future_obtained,other.future_obtained);
  2327. }
  2328. // Result retrieval
  2329. BOOST_THREAD_FUTURE<R&> get_future()
  2330. {
  2331. lazy_init();
  2332. if (future_.get()==0)
  2333. {
  2334. boost::throw_exception(promise_moved());
  2335. }
  2336. if (future_obtained)
  2337. {
  2338. boost::throw_exception(future_already_retrieved());
  2339. }
  2340. future_obtained=true;
  2341. return BOOST_THREAD_FUTURE<R&>(future_);
  2342. }
  2343. void set_value(R& r)
  2344. {
  2345. lazy_init();
  2346. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2347. if(future_->done)
  2348. {
  2349. boost::throw_exception(promise_already_satisfied());
  2350. }
  2351. future_->mark_finished_with_result_internal(r, lock);
  2352. }
  2353. void set_value_deferred(R& r)
  2354. {
  2355. lazy_init();
  2356. if (future_.get()==0)
  2357. {
  2358. boost::throw_exception(promise_already_satisfied());
  2359. }
  2360. future_->set_value_deferred(r);
  2361. }
  2362. void set_exception(boost::exception_ptr p)
  2363. {
  2364. lazy_init();
  2365. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2366. if(future_->done)
  2367. {
  2368. boost::throw_exception(promise_already_satisfied());
  2369. }
  2370. future_->mark_exceptional_finish_internal(p, lock);
  2371. }
  2372. template <typename E>
  2373. void set_exception(E ex)
  2374. {
  2375. set_exception(boost::copy_exception(ex));
  2376. }
  2377. void set_exception_deferred(boost::exception_ptr p)
  2378. {
  2379. lazy_init();
  2380. if (future_.get()==0)
  2381. {
  2382. boost::throw_exception(promise_moved());
  2383. }
  2384. future_->set_exception_deferred(p);
  2385. }
  2386. template <typename E>
  2387. void set_exception_deferred(E ex)
  2388. {
  2389. set_exception_deferred(boost::copy_exception(ex));
  2390. }
  2391. // setting the result with deferred notification
  2392. void set_value_at_thread_exit(R& r)
  2393. {
  2394. if (future_.get()==0)
  2395. {
  2396. boost::throw_exception(promise_moved());
  2397. }
  2398. future_->set_value_at_thread_exit(r);
  2399. }
  2400. void set_exception_at_thread_exit(exception_ptr e)
  2401. {
  2402. if (future_.get()==0)
  2403. {
  2404. boost::throw_exception(promise_moved());
  2405. }
  2406. future_->set_exception_at_thread_exit(e);
  2407. }
  2408. template <typename E>
  2409. void set_exception_at_thread_exit(E ex)
  2410. {
  2411. set_exception_at_thread_exit(boost::copy_exception(ex));
  2412. }
  2413. template<typename F>
  2414. void set_wait_callback(F f)
  2415. {
  2416. lazy_init();
  2417. future_->set_wait_callback(f,this);
  2418. }
  2419. void notify_deferred()
  2420. {
  2421. if (future_.get()==0)
  2422. {
  2423. boost::throw_exception(promise_moved());
  2424. }
  2425. future_->notify_deferred();
  2426. }
  2427. };
  2428. template <>
  2429. class promise<void>
  2430. {
  2431. typedef boost::shared_ptr<detail::shared_state<void> > future_ptr;
  2432. future_ptr future_;
  2433. bool future_obtained;
  2434. void lazy_init()
  2435. {
  2436. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  2437. if(!atomic_load(&future_))
  2438. {
  2439. future_ptr blank;
  2440. atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state<void>));
  2441. }
  2442. #endif
  2443. }
  2444. public:
  2445. BOOST_THREAD_MOVABLE_ONLY(promise)
  2446. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  2447. template <class Allocator>
  2448. promise(boost::allocator_arg_t, Allocator a)
  2449. {
  2450. typedef typename Allocator::template rebind<detail::shared_state<void> >::other A2;
  2451. A2 a2(a);
  2452. typedef thread_detail::allocator_destructor<A2> D;
  2453. future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state<void>(), D(a2, 1) );
  2454. future_obtained = false;
  2455. }
  2456. #endif
  2457. promise():
  2458. #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
  2459. future_(),
  2460. #else
  2461. future_(new detail::shared_state<void>),
  2462. #endif
  2463. future_obtained(false)
  2464. {}
  2465. ~promise()
  2466. {
  2467. if(future_)
  2468. {
  2469. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2470. if(!future_->done && !future_->is_constructed)
  2471. {
  2472. future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock);
  2473. }
  2474. }
  2475. }
  2476. // Assignment
  2477. promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT :
  2478. future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained)
  2479. {
  2480. // we need to release the future as shared_ptr doesn't implements move semantics
  2481. BOOST_THREAD_RV(rhs).future_.reset();
  2482. BOOST_THREAD_RV(rhs).future_obtained=false;
  2483. }
  2484. promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT
  2485. {
  2486. future_=BOOST_THREAD_RV(rhs).future_;
  2487. future_obtained=BOOST_THREAD_RV(rhs).future_obtained;
  2488. BOOST_THREAD_RV(rhs).future_.reset();
  2489. BOOST_THREAD_RV(rhs).future_obtained=false;
  2490. return *this;
  2491. }
  2492. void swap(promise& other)
  2493. {
  2494. future_.swap(other.future_);
  2495. std::swap(future_obtained,other.future_obtained);
  2496. }
  2497. // Result retrieval
  2498. BOOST_THREAD_FUTURE<void> get_future()
  2499. {
  2500. lazy_init();
  2501. if (future_.get()==0)
  2502. {
  2503. boost::throw_exception(promise_moved());
  2504. }
  2505. if(future_obtained)
  2506. {
  2507. boost::throw_exception(future_already_retrieved());
  2508. }
  2509. future_obtained=true;
  2510. //return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE<void>(future_));
  2511. return BOOST_THREAD_FUTURE<void>(future_);
  2512. }
  2513. void set_value()
  2514. {
  2515. lazy_init();
  2516. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2517. if(future_->done)
  2518. {
  2519. boost::throw_exception(promise_already_satisfied());
  2520. }
  2521. future_->mark_finished_with_result_internal(lock);
  2522. }
  2523. void set_value_deferred()
  2524. {
  2525. lazy_init();
  2526. if (future_.get()==0)
  2527. {
  2528. boost::throw_exception(promise_moved());
  2529. }
  2530. future_->set_value_deferred();
  2531. }
  2532. void set_exception(boost::exception_ptr p)
  2533. {
  2534. lazy_init();
  2535. boost::unique_lock<boost::mutex> lock(future_->mutex);
  2536. if(future_->done)
  2537. {
  2538. boost::throw_exception(promise_already_satisfied());
  2539. }
  2540. future_->mark_exceptional_finish_internal(p,lock);
  2541. }
  2542. template <typename E>
  2543. void set_exception(E ex)
  2544. {
  2545. set_exception(boost::copy_exception(ex));
  2546. }
  2547. void set_exception_deferred(boost::exception_ptr p)
  2548. {
  2549. lazy_init();
  2550. if (future_.get()==0)
  2551. {
  2552. boost::throw_exception(promise_moved());
  2553. }
  2554. future_->set_exception_deferred(p);
  2555. }
  2556. template <typename E>
  2557. void set_exception_deferred(E ex)
  2558. {
  2559. set_exception_deferred(boost::copy_exception(ex));
  2560. }
  2561. // setting the result with deferred notification
  2562. void set_value_at_thread_exit()
  2563. {
  2564. if (future_.get()==0)
  2565. {
  2566. boost::throw_exception(promise_moved());
  2567. }
  2568. future_->set_value_at_thread_exit();
  2569. }
  2570. void set_exception_at_thread_exit(exception_ptr e)
  2571. {
  2572. if (future_.get()==0)
  2573. {
  2574. boost::throw_exception(promise_moved());
  2575. }
  2576. future_->set_exception_at_thread_exit(e);
  2577. }
  2578. template <typename E>
  2579. void set_exception_at_thread_exit(E ex)
  2580. {
  2581. set_exception_at_thread_exit(boost::copy_exception(ex));
  2582. }
  2583. template<typename F>
  2584. void set_wait_callback(F f)
  2585. {
  2586. lazy_init();
  2587. future_->set_wait_callback(f,this);
  2588. }
  2589. void notify_deferred()
  2590. {
  2591. if (future_.get()==0)
  2592. {
  2593. boost::throw_exception(promise_moved());
  2594. }
  2595. future_->notify_deferred();
  2596. }
  2597. };
  2598. }
  2599. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  2600. namespace boost { namespace container {
  2601. template <class R, class Alloc>
  2602. struct uses_allocator< ::boost::promise<R> , Alloc> : true_type
  2603. {
  2604. };
  2605. }}
  2606. #if ! defined BOOST_NO_CXX11_ALLOCATOR
  2607. namespace std {
  2608. template <class R, class Alloc>
  2609. struct uses_allocator< ::boost::promise<R> , Alloc> : true_type
  2610. {
  2611. };
  2612. }
  2613. #endif
  2614. #endif
  2615. namespace boost
  2616. {
  2617. BOOST_THREAD_DCL_MOVABLE_BEG(T) promise<T> BOOST_THREAD_DCL_MOVABLE_END
  2618. namespace detail
  2619. {
  2620. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  2621. template<typename R>
  2622. struct task_base_shared_state;
  2623. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2624. template<typename R, typename ...ArgTypes>
  2625. struct task_base_shared_state<R(ArgTypes...)>:
  2626. #else
  2627. template<typename R>
  2628. struct task_base_shared_state<R()>:
  2629. #endif
  2630. #else
  2631. template<typename R>
  2632. struct task_base_shared_state:
  2633. #endif
  2634. detail::shared_state<R>
  2635. {
  2636. bool started;
  2637. task_base_shared_state():
  2638. started(false)
  2639. {}
  2640. void reset()
  2641. {
  2642. // todo The packaged_task::reset must be as if an assignemnt froma new packaged_task with the same function
  2643. // the reset function is an optimization that avoids reallocating a new task.
  2644. started=false;
  2645. this->validate();
  2646. }
  2647. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2648. virtual void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)=0;
  2649. void run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2650. #else
  2651. virtual void do_run()=0;
  2652. void run()
  2653. #endif
  2654. {
  2655. {
  2656. boost::lock_guard<boost::mutex> lk(this->mutex);
  2657. if(started)
  2658. {
  2659. boost::throw_exception(task_already_started());
  2660. }
  2661. started=true;
  2662. }
  2663. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2664. do_run(boost::move(args)...);
  2665. #else
  2666. do_run();
  2667. #endif
  2668. }
  2669. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2670. virtual void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)=0;
  2671. void apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2672. #else
  2673. virtual void do_apply()=0;
  2674. void apply()
  2675. #endif
  2676. {
  2677. {
  2678. boost::lock_guard<boost::mutex> lk(this->mutex);
  2679. if(started)
  2680. {
  2681. boost::throw_exception(task_already_started());
  2682. }
  2683. started=true;
  2684. }
  2685. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2686. do_apply(boost::move(args)...);
  2687. #else
  2688. do_apply();
  2689. #endif
  2690. }
  2691. void owner_destroyed()
  2692. {
  2693. boost::unique_lock<boost::mutex> lk(this->mutex);
  2694. if(!started)
  2695. {
  2696. started=true;
  2697. this->mark_exceptional_finish_internal(boost::copy_exception(boost::broken_promise()), lk);
  2698. }
  2699. }
  2700. };
  2701. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  2702. template<typename F, typename R>
  2703. struct task_shared_state;
  2704. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2705. template<typename F, typename R, typename ...ArgTypes>
  2706. struct task_shared_state<F, R(ArgTypes...)>:
  2707. task_base_shared_state<R(ArgTypes...)>
  2708. #else
  2709. template<typename F, typename R>
  2710. struct task_shared_state<F, R()>:
  2711. task_base_shared_state<R()>
  2712. #endif
  2713. #else
  2714. template<typename F, typename R>
  2715. struct task_shared_state:
  2716. task_base_shared_state<R>
  2717. #endif
  2718. {
  2719. private:
  2720. task_shared_state(task_shared_state&);
  2721. public:
  2722. F f;
  2723. task_shared_state(F const& f_):
  2724. f(f_)
  2725. {}
  2726. task_shared_state(BOOST_THREAD_RV_REF(F) f_):
  2727. f(boost::move(f_))
  2728. {}
  2729. F callable()
  2730. {
  2731. return boost::move(f);
  2732. }
  2733. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2734. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2735. {
  2736. try
  2737. {
  2738. this->set_value_at_thread_exit(f(boost::move(args)...));
  2739. }
  2740. #else
  2741. void do_apply()
  2742. {
  2743. try
  2744. {
  2745. this->set_value_at_thread_exit(f());
  2746. }
  2747. #endif
  2748. catch(...)
  2749. {
  2750. this->set_exception_at_thread_exit(current_exception());
  2751. }
  2752. }
  2753. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2754. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2755. {
  2756. try
  2757. {
  2758. this->mark_finished_with_result(f(boost::move(args)...));
  2759. }
  2760. #else
  2761. void do_run()
  2762. {
  2763. try
  2764. {
  2765. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  2766. R res((f()));
  2767. this->mark_finished_with_result(boost::move(res));
  2768. #else
  2769. this->mark_finished_with_result(f());
  2770. #endif
  2771. }
  2772. #endif
  2773. catch(...)
  2774. {
  2775. this->mark_exceptional_finish();
  2776. }
  2777. }
  2778. };
  2779. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  2780. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2781. template<typename F, typename R, typename ...ArgTypes>
  2782. struct task_shared_state<F, R&(ArgTypes...)>:
  2783. task_base_shared_state<R&(ArgTypes...)>
  2784. #else
  2785. template<typename F, typename R>
  2786. struct task_shared_state<F, R&()>:
  2787. task_base_shared_state<R&()>
  2788. #endif
  2789. #else
  2790. template<typename F, typename R>
  2791. struct task_shared_state<F,R&>:
  2792. task_base_shared_state<R&>
  2793. #endif
  2794. {
  2795. private:
  2796. task_shared_state(task_shared_state&);
  2797. public:
  2798. F f;
  2799. task_shared_state(F const& f_):
  2800. f(f_)
  2801. {}
  2802. task_shared_state(BOOST_THREAD_RV_REF(F) f_):
  2803. f(boost::move(f_))
  2804. {}
  2805. F callable()
  2806. {
  2807. return f;
  2808. }
  2809. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2810. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2811. {
  2812. try
  2813. {
  2814. this->set_value_at_thread_exit(f(boost::move(args)...));
  2815. }
  2816. #else
  2817. void do_apply()
  2818. {
  2819. try
  2820. {
  2821. this->set_value_at_thread_exit(f());
  2822. }
  2823. #endif
  2824. catch(...)
  2825. {
  2826. this->set_exception_at_thread_exit(current_exception());
  2827. }
  2828. }
  2829. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2830. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2831. {
  2832. try
  2833. {
  2834. this->mark_finished_with_result(f(boost::move(args)...));
  2835. }
  2836. #else
  2837. void do_run()
  2838. {
  2839. try
  2840. {
  2841. R& res((f()));
  2842. this->mark_finished_with_result(res);
  2843. }
  2844. #endif
  2845. catch(...)
  2846. {
  2847. this->mark_exceptional_finish();
  2848. }
  2849. }
  2850. };
  2851. #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  2852. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  2853. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2854. template<typename R, typename ...ArgTypes>
  2855. struct task_shared_state<R (*)(ArgTypes...), R(ArgTypes...)>:
  2856. task_base_shared_state<R(ArgTypes...)>
  2857. #else
  2858. template<typename R>
  2859. struct task_shared_state<R (*)(), R()>:
  2860. task_base_shared_state<R()>
  2861. #endif
  2862. #else
  2863. template<typename R>
  2864. struct task_shared_state<R (*)(), R> :
  2865. task_base_shared_state<R>
  2866. #endif
  2867. {
  2868. private:
  2869. task_shared_state(task_shared_state&);
  2870. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2871. typedef R (*CallableType)(ArgTypes ... );
  2872. #else
  2873. typedef R (*CallableType)();
  2874. #endif
  2875. public:
  2876. CallableType f;
  2877. task_shared_state(CallableType f_):
  2878. f(f_)
  2879. {}
  2880. CallableType callable()
  2881. {
  2882. return f;
  2883. }
  2884. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2885. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2886. {
  2887. try
  2888. {
  2889. this->set_value_at_thread_exit(f(boost::move(args)...));
  2890. }
  2891. #else
  2892. void do_apply()
  2893. {
  2894. try
  2895. {
  2896. R r((f()));
  2897. this->set_value_at_thread_exit(boost::move(r));
  2898. }
  2899. #endif
  2900. catch(...)
  2901. {
  2902. this->set_exception_at_thread_exit(current_exception());
  2903. }
  2904. }
  2905. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2906. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2907. {
  2908. try
  2909. {
  2910. this->mark_finished_with_result(f(boost::move(args)...));
  2911. }
  2912. #else
  2913. void do_run()
  2914. {
  2915. try
  2916. {
  2917. R res((f()));
  2918. this->mark_finished_with_result(boost::move(res));
  2919. }
  2920. #endif
  2921. catch(...)
  2922. {
  2923. this->mark_exceptional_finish();
  2924. }
  2925. }
  2926. };
  2927. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  2928. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2929. template<typename R, typename ...ArgTypes>
  2930. struct task_shared_state<R& (*)(ArgTypes...), R&(ArgTypes...)>:
  2931. task_base_shared_state<R&(ArgTypes...)>
  2932. #else
  2933. template<typename R>
  2934. struct task_shared_state<R& (*)(), R&()>:
  2935. task_base_shared_state<R&()>
  2936. #endif
  2937. #else
  2938. template<typename R>
  2939. struct task_shared_state<R& (*)(), R&> :
  2940. task_base_shared_state<R&>
  2941. #endif
  2942. {
  2943. private:
  2944. task_shared_state(task_shared_state&);
  2945. public:
  2946. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2947. typedef R& (*CallableType)(BOOST_THREAD_RV_REF(ArgTypes) ... );
  2948. #else
  2949. typedef R& (*CallableType)();
  2950. #endif
  2951. CallableType f;
  2952. task_shared_state(CallableType f_):
  2953. f(f_)
  2954. {}
  2955. CallableType callable()
  2956. {
  2957. return boost::move(f);
  2958. }
  2959. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2960. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2961. {
  2962. try
  2963. {
  2964. this->set_value_at_thread_exit(f(boost::move(args)...));
  2965. }
  2966. #else
  2967. void do_apply()
  2968. {
  2969. try
  2970. {
  2971. this->set_value_at_thread_exit(f());
  2972. }
  2973. #endif
  2974. catch(...)
  2975. {
  2976. this->set_exception_at_thread_exit(current_exception());
  2977. }
  2978. }
  2979. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  2980. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  2981. {
  2982. try
  2983. {
  2984. this->mark_finished_with_result(f(boost::move(args)...));
  2985. }
  2986. #else
  2987. void do_run()
  2988. {
  2989. try
  2990. {
  2991. this->mark_finished_with_result(f());
  2992. }
  2993. #endif
  2994. catch(...)
  2995. {
  2996. this->mark_exceptional_finish();
  2997. }
  2998. }
  2999. };
  3000. #endif
  3001. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3002. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3003. template<typename F, typename ...ArgTypes>
  3004. struct task_shared_state<F, void(ArgTypes...)>:
  3005. task_base_shared_state<void(ArgTypes...)>
  3006. #else
  3007. template<typename F>
  3008. struct task_shared_state<F, void()>:
  3009. task_base_shared_state<void()>
  3010. #endif
  3011. #else
  3012. template<typename F>
  3013. struct task_shared_state<F,void>:
  3014. task_base_shared_state<void>
  3015. #endif
  3016. {
  3017. private:
  3018. task_shared_state(task_shared_state&);
  3019. public:
  3020. typedef F CallableType;
  3021. F f;
  3022. task_shared_state(F const& f_):
  3023. f(f_)
  3024. {}
  3025. task_shared_state(BOOST_THREAD_RV_REF(F) f_):
  3026. f(boost::move(f_))
  3027. {}
  3028. F callable()
  3029. {
  3030. return boost::move(f);
  3031. }
  3032. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3033. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  3034. {
  3035. try
  3036. {
  3037. f(boost::move(args)...);
  3038. #else
  3039. void do_apply()
  3040. {
  3041. try
  3042. {
  3043. f();
  3044. #endif
  3045. this->set_value_at_thread_exit();
  3046. }
  3047. catch(...)
  3048. {
  3049. this->set_exception_at_thread_exit(current_exception());
  3050. }
  3051. }
  3052. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3053. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  3054. {
  3055. try
  3056. {
  3057. f(boost::move(args)...);
  3058. #else
  3059. void do_run()
  3060. {
  3061. try
  3062. {
  3063. f();
  3064. #endif
  3065. this->mark_finished_with_result();
  3066. }
  3067. catch(...)
  3068. {
  3069. this->mark_exceptional_finish();
  3070. }
  3071. }
  3072. };
  3073. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3074. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3075. template<typename ...ArgTypes>
  3076. struct task_shared_state<void (*)(ArgTypes...), void(ArgTypes...)>:
  3077. task_base_shared_state<void(ArgTypes...)>
  3078. #else
  3079. template<>
  3080. struct task_shared_state<void (*)(), void()>:
  3081. task_base_shared_state<void()>
  3082. #endif
  3083. #else
  3084. template<>
  3085. struct task_shared_state<void (*)(),void>:
  3086. task_base_shared_state<void>
  3087. #endif
  3088. {
  3089. private:
  3090. task_shared_state(task_shared_state&);
  3091. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3092. typedef void (*CallableType)(ArgTypes...);
  3093. #else
  3094. typedef void (*CallableType)();
  3095. #endif
  3096. public:
  3097. CallableType f;
  3098. task_shared_state(CallableType f_):
  3099. f(f_)
  3100. {}
  3101. CallableType callable()
  3102. {
  3103. return f;
  3104. }
  3105. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3106. void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  3107. {
  3108. try
  3109. {
  3110. f(boost::move(args)...);
  3111. #else
  3112. void do_apply()
  3113. {
  3114. try
  3115. {
  3116. f();
  3117. #endif
  3118. this->set_value_at_thread_exit();
  3119. }
  3120. catch(...)
  3121. {
  3122. this->set_exception_at_thread_exit(current_exception());
  3123. }
  3124. }
  3125. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3126. void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)
  3127. {
  3128. try
  3129. {
  3130. f(boost::move(args)...);
  3131. #else
  3132. void do_run()
  3133. {
  3134. try
  3135. {
  3136. f();
  3137. #endif
  3138. this->mark_finished_with_result();
  3139. }
  3140. catch(...)
  3141. {
  3142. this->mark_exceptional_finish();
  3143. }
  3144. }
  3145. };
  3146. }
  3147. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3148. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3149. template<typename R, typename ...ArgTypes>
  3150. class packaged_task<R(ArgTypes...)>
  3151. {
  3152. typedef boost::shared_ptr<detail::task_base_shared_state<R(ArgTypes...)> > task_ptr;
  3153. boost::shared_ptr<detail::task_base_shared_state<R(ArgTypes...)> > task;
  3154. #else
  3155. template<typename R>
  3156. class packaged_task<R()>
  3157. {
  3158. typedef boost::shared_ptr<detail::task_base_shared_state<R()> > task_ptr;
  3159. boost::shared_ptr<detail::task_base_shared_state<R()> > task;
  3160. #endif
  3161. #else
  3162. template<typename R>
  3163. class packaged_task
  3164. {
  3165. typedef boost::shared_ptr<detail::task_base_shared_state<R> > task_ptr;
  3166. boost::shared_ptr<detail::task_base_shared_state<R> > task;
  3167. #endif
  3168. bool future_obtained;
  3169. struct dummy;
  3170. public:
  3171. typedef R result_type;
  3172. BOOST_THREAD_MOVABLE_ONLY(packaged_task)
  3173. packaged_task():
  3174. future_obtained(false)
  3175. {}
  3176. // construction and destruction
  3177. #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  3178. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3179. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3180. explicit packaged_task(R(*f)(), BOOST_THREAD_FWD_REF(ArgTypes)... args)
  3181. {
  3182. typedef R(*FR)(BOOST_THREAD_FWD_REF(ArgTypes)...);
  3183. typedef detail::task_shared_state<FR,R(ArgTypes...)> task_shared_state_type;
  3184. task= task_ptr(new task_shared_state_type(f, boost::move(args)...));
  3185. future_obtained=false;
  3186. }
  3187. #else
  3188. explicit packaged_task(R(*f)())
  3189. {
  3190. typedef R(*FR)();
  3191. typedef detail::task_shared_state<FR,R()> task_shared_state_type;
  3192. task= task_ptr(new task_shared_state_type(f));
  3193. future_obtained=false;
  3194. }
  3195. #endif
  3196. #else
  3197. explicit packaged_task(R(*f)())
  3198. {
  3199. typedef R(*FR)();
  3200. typedef detail::task_shared_state<FR,R> task_shared_state_type;
  3201. task= task_ptr(new task_shared_state_type(f));
  3202. future_obtained=false;
  3203. }
  3204. #endif
  3205. #endif
  3206. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  3207. template <class F>
  3208. explicit packaged_task(BOOST_THREAD_FWD_REF(F) f
  3209. , typename boost::disable_if<is_same<typename decay<F>::type, packaged_task>, dummy* >::type=0
  3210. )
  3211. {
  3212. typedef typename decay<F>::type FR;
  3213. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3214. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3215. typedef detail::task_shared_state<FR,R(ArgTypes...)> task_shared_state_type;
  3216. #else
  3217. typedef detail::task_shared_state<FR,R()> task_shared_state_type;
  3218. #endif
  3219. #else
  3220. typedef detail::task_shared_state<FR,R> task_shared_state_type;
  3221. #endif
  3222. task = task_ptr(new task_shared_state_type(boost::forward<F>(f)));
  3223. future_obtained = false;
  3224. }
  3225. #else
  3226. template <class F>
  3227. explicit packaged_task(F const& f
  3228. , typename boost::disable_if<is_same<typename decay<F>::type, packaged_task>, dummy* >::type=0
  3229. )
  3230. {
  3231. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3232. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3233. typedef detail::task_shared_state<F,R(ArgTypes...)> task_shared_state_type;
  3234. #else
  3235. typedef detail::task_shared_state<F,R()> task_shared_state_type;
  3236. #endif
  3237. #else
  3238. typedef detail::task_shared_state<F,R> task_shared_state_type;
  3239. #endif
  3240. task = task_ptr(new task_shared_state_type(f));
  3241. future_obtained=false;
  3242. }
  3243. template <class F>
  3244. explicit packaged_task(BOOST_THREAD_RV_REF(F) f)
  3245. {
  3246. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3247. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3248. typedef detail::task_shared_state<F,R(ArgTypes...)> task_shared_state_type;
  3249. task = task_ptr(new task_shared_state_type(boost::move(f)));
  3250. #else
  3251. typedef detail::task_shared_state<F,R()> task_shared_state_type;
  3252. task = task_ptr(new task_shared_state_type(boost::move(f)));
  3253. #endif
  3254. #else
  3255. typedef detail::task_shared_state<F,R> task_shared_state_type;
  3256. task = task_ptr(new task_shared_state_type(boost::move(f)));
  3257. #endif
  3258. future_obtained=false;
  3259. }
  3260. #endif
  3261. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  3262. #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  3263. template <class Allocator>
  3264. packaged_task(boost::allocator_arg_t, Allocator a, R(*f)())
  3265. {
  3266. typedef R(*FR)();
  3267. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3268. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3269. typedef detail::task_shared_state<FR,R(ArgTypes...)> task_shared_state_type;
  3270. #else
  3271. typedef detail::task_shared_state<FR,R()> task_shared_state_type;
  3272. #endif
  3273. #else
  3274. typedef detail::task_shared_state<FR,R> task_shared_state_type;
  3275. #endif
  3276. typedef typename Allocator::template rebind<task_shared_state_type>::other A2;
  3277. A2 a2(a);
  3278. typedef thread_detail::allocator_destructor<A2> D;
  3279. task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(f), D(a2, 1) );
  3280. future_obtained = false;
  3281. }
  3282. #endif // BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3283. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  3284. template <class F, class Allocator>
  3285. packaged_task(boost::allocator_arg_t, Allocator a, BOOST_THREAD_FWD_REF(F) f)
  3286. {
  3287. typedef typename decay<F>::type FR;
  3288. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3289. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3290. typedef detail::task_shared_state<FR,R(ArgTypes...)> task_shared_state_type;
  3291. #else
  3292. typedef detail::task_shared_state<FR,R()> task_shared_state_type;
  3293. #endif
  3294. #else
  3295. typedef detail::task_shared_state<FR,R> task_shared_state_type;
  3296. #endif
  3297. typedef typename Allocator::template rebind<task_shared_state_type>::other A2;
  3298. A2 a2(a);
  3299. typedef thread_detail::allocator_destructor<A2> D;
  3300. task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(boost::forward<F>(f)), D(a2, 1) );
  3301. future_obtained = false;
  3302. }
  3303. #else // ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  3304. template <class F, class Allocator>
  3305. packaged_task(boost::allocator_arg_t, Allocator a, const F& f)
  3306. {
  3307. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3308. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3309. typedef detail::task_shared_state<F,R(ArgTypes...)> task_shared_state_type;
  3310. #else
  3311. typedef detail::task_shared_state<F,R()> task_shared_state_type;
  3312. #endif
  3313. #else
  3314. typedef detail::task_shared_state<F,R> task_shared_state_type;
  3315. #endif
  3316. typedef typename Allocator::template rebind<task_shared_state_type>::other A2;
  3317. A2 a2(a);
  3318. typedef thread_detail::allocator_destructor<A2> D;
  3319. task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(f), D(a2, 1) );
  3320. future_obtained = false;
  3321. }
  3322. template <class F, class Allocator>
  3323. packaged_task(boost::allocator_arg_t, Allocator a, BOOST_THREAD_RV_REF(F) f)
  3324. {
  3325. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3326. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3327. typedef detail::task_shared_state<F,R(ArgTypes...)> task_shared_state_type;
  3328. #else
  3329. typedef detail::task_shared_state<F,R()> task_shared_state_type;
  3330. #endif
  3331. #else
  3332. typedef detail::task_shared_state<F,R> task_shared_state_type;
  3333. #endif
  3334. typedef typename Allocator::template rebind<task_shared_state_type>::other A2;
  3335. A2 a2(a);
  3336. typedef thread_detail::allocator_destructor<A2> D;
  3337. task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(boost::move(f)), D(a2, 1) );
  3338. future_obtained = false;
  3339. }
  3340. #endif //BOOST_NO_CXX11_RVALUE_REFERENCES
  3341. #endif // BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  3342. ~packaged_task() {
  3343. if(task) {
  3344. task->owner_destroyed();
  3345. }
  3346. }
  3347. // assignment
  3348. packaged_task(BOOST_THREAD_RV_REF(packaged_task) other) BOOST_NOEXCEPT
  3349. : future_obtained(BOOST_THREAD_RV(other).future_obtained) {
  3350. task.swap(BOOST_THREAD_RV(other).task);
  3351. BOOST_THREAD_RV(other).future_obtained=false;
  3352. }
  3353. packaged_task& operator=(BOOST_THREAD_RV_REF(packaged_task) other) BOOST_NOEXCEPT {
  3354. #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
  3355. packaged_task temp(boost::move(other));
  3356. #else
  3357. packaged_task temp(static_cast<BOOST_THREAD_RV_REF(packaged_task)>(other));
  3358. #endif
  3359. swap(temp);
  3360. return *this;
  3361. }
  3362. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  3363. void set_executor(executor_ptr_type aex)
  3364. {
  3365. if (!valid())
  3366. boost::throw_exception(task_moved());
  3367. boost::lock_guard<boost::mutex> lk(task->mutex);
  3368. task->set_executor_policy(aex, lk);
  3369. }
  3370. #endif
  3371. void reset() {
  3372. if (!valid())
  3373. boost::throw_exception(future_error(system::make_error_code(future_errc::no_state)));
  3374. // As if *this = packaged_task(task->callable());
  3375. task->reset();
  3376. future_obtained=false;
  3377. }
  3378. void swap(packaged_task& other) BOOST_NOEXCEPT {
  3379. task.swap(other.task);
  3380. std::swap(future_obtained,other.future_obtained);
  3381. }
  3382. bool valid() const BOOST_NOEXCEPT {
  3383. return task.get()!=0;
  3384. }
  3385. // result retrieval
  3386. BOOST_THREAD_FUTURE<R> get_future() {
  3387. if(!task) {
  3388. boost::throw_exception(task_moved());
  3389. } else if(!future_obtained) {
  3390. future_obtained=true;
  3391. return BOOST_THREAD_FUTURE<R>(task);
  3392. } else {
  3393. boost::throw_exception(future_already_retrieved());
  3394. }
  3395. }
  3396. // execution
  3397. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3398. void operator()(ArgTypes... args) {
  3399. if(!task) {
  3400. boost::throw_exception(task_moved());
  3401. }
  3402. task->run(boost::move(args)...);
  3403. }
  3404. void make_ready_at_thread_exit(ArgTypes... args) {
  3405. if(!task) {
  3406. boost::throw_exception(task_moved());
  3407. }
  3408. if (task->has_value()) {
  3409. boost::throw_exception(promise_already_satisfied());
  3410. }
  3411. task->apply(boost::move(args)...);
  3412. }
  3413. #else
  3414. void operator()() {
  3415. if(!task) {
  3416. boost::throw_exception(task_moved());
  3417. }
  3418. task->run();
  3419. }
  3420. void make_ready_at_thread_exit() {
  3421. if(!task) {
  3422. boost::throw_exception(task_moved());
  3423. }
  3424. if (task->has_value()) boost::throw_exception(promise_already_satisfied());
  3425. task->apply();
  3426. }
  3427. #endif
  3428. template<typename F>
  3429. void set_wait_callback(F f) {
  3430. task->set_wait_callback(f,this);
  3431. }
  3432. };
  3433. }
  3434. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  3435. namespace boost { namespace container {
  3436. template <class R, class Alloc>
  3437. struct uses_allocator< ::boost::packaged_task<R> , Alloc> : true_type
  3438. {};
  3439. }}
  3440. #if ! defined BOOST_NO_CXX11_ALLOCATOR
  3441. namespace std {
  3442. template <class R, class Alloc>
  3443. struct uses_allocator< ::boost::packaged_task<R> , Alloc> : true_type
  3444. {};
  3445. }
  3446. #endif
  3447. #endif
  3448. namespace boost
  3449. {
  3450. BOOST_THREAD_DCL_MOVABLE_BEG(T) packaged_task<T> BOOST_THREAD_DCL_MOVABLE_END
  3451. namespace detail
  3452. {
  3453. ////////////////////////////////
  3454. // make_future_deferred_shared_state
  3455. ////////////////////////////////
  3456. template <class Rp, class Fp>
  3457. BOOST_THREAD_FUTURE<Rp>
  3458. make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f) {
  3459. shared_ptr<future_deferred_shared_state<Rp, Fp> >
  3460. h(new future_deferred_shared_state<Rp, Fp>(boost::forward<Fp>(f)));
  3461. return BOOST_THREAD_FUTURE<Rp>(h);
  3462. }
  3463. ////////////////////////////////
  3464. // make_future_async_shared_state
  3465. ////////////////////////////////
  3466. template <class Rp, class Fp>
  3467. BOOST_THREAD_FUTURE<Rp>
  3468. make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f) {
  3469. shared_ptr<future_async_shared_state<Rp, Fp> >
  3470. h(new future_async_shared_state<Rp, Fp>());
  3471. h->init(boost::forward<Fp>(f));
  3472. return BOOST_THREAD_FUTURE<Rp>(h);
  3473. }
  3474. }
  3475. ////////////////////////////////
  3476. // template <class F, class... ArgTypes>
  3477. // future<R> async(launch policy, F&&, ArgTypes&&...);
  3478. ////////////////////////////////
  3479. #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3480. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3481. template <class R, class... ArgTypes>
  3482. BOOST_THREAD_FUTURE<R>
  3483. async(launch policy, R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3484. typedef R(*F)(BOOST_THREAD_FWD_REF(ArgTypes)...);
  3485. typedef detail::invoker<typename decay<F>::type, typename decay<ArgTypes>::type...> BF;
  3486. typedef typename BF::result_type Rp;
  3487. if (underlying_cast<int>(policy) & int(launch::async)) {
  3488. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_async_shared_state<Rp>(
  3489. BF(
  3490. f
  3491. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3492. )
  3493. ));
  3494. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  3495. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_deferred_shared_state<Rp>(
  3496. BF(
  3497. f
  3498. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3499. )
  3500. ));
  3501. } else {
  3502. std::terminate();
  3503. //BOOST_THREAD_FUTURE<R> ret;
  3504. //return ::boost::move(ret);
  3505. }
  3506. }
  3507. #else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3508. template <class R>
  3509. BOOST_THREAD_FUTURE<R>
  3510. async(launch policy, R(*f)()) {
  3511. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3512. typedef packaged_task<R()> packaged_task_type;
  3513. #else
  3514. typedef packaged_task<R> packaged_task_type;
  3515. #endif
  3516. if (underlying_cast<int>(policy) & int(launch::async)) {
  3517. packaged_task_type pt( f );
  3518. BOOST_THREAD_FUTURE<R> ret = BOOST_THREAD_MAKE_RV_REF(pt.get_future());
  3519. ret.set_async();
  3520. boost::thread( boost::move(pt) ).detach();
  3521. return ::boost::move(ret);
  3522. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  3523. std::terminate();
  3524. //BOOST_THREAD_FUTURE<R> ret;
  3525. //return ::boost::move(ret);
  3526. } else {
  3527. std::terminate();
  3528. //BOOST_THREAD_FUTURE<R> ret;
  3529. //return ::boost::move(ret);
  3530. }
  3531. }
  3532. #endif
  3533. #endif // defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR)
  3534. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3535. template <class F, class ...ArgTypes>
  3536. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
  3537. typename decay<ArgTypes>::type...
  3538. )>::type>
  3539. async(launch policy, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3540. typedef detail::invoker<typename decay<F>::type, typename decay<ArgTypes>::type...> BF;
  3541. typedef typename BF::result_type Rp;
  3542. if (underlying_cast<int>(policy) & int(launch::async)) {
  3543. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_async_shared_state<Rp>(
  3544. BF(
  3545. thread_detail::decay_copy(boost::forward<F>(f))
  3546. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3547. )
  3548. ));
  3549. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  3550. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_deferred_shared_state<Rp>(
  3551. BF(
  3552. thread_detail::decay_copy(boost::forward<F>(f))
  3553. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3554. )
  3555. ));
  3556. } else {
  3557. std::terminate();
  3558. //BOOST_THREAD_FUTURE<R> ret;
  3559. //return ::boost::move(ret);
  3560. }
  3561. }
  3562. #else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3563. template <class F>
  3564. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type()>::type>
  3565. async(launch policy, BOOST_THREAD_FWD_REF(F) f) {
  3566. typedef typename boost::result_of<typename decay<F>::type()>::type R;
  3567. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3568. typedef packaged_task<R()> packaged_task_type;
  3569. #else // defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3570. typedef packaged_task<R> packaged_task_type;
  3571. #endif // defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
  3572. if (underlying_cast<int>(policy) & int(launch::async)) {
  3573. packaged_task_type pt( boost::forward<F>(f) );
  3574. BOOST_THREAD_FUTURE<R> ret = pt.get_future();
  3575. ret.set_async();
  3576. boost::thread( boost::move(pt) ).detach();
  3577. return ::boost::move(ret);
  3578. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  3579. std::terminate();
  3580. //BOOST_THREAD_FUTURE<R> ret;
  3581. //return ::boost::move(ret);
  3582. // return boost::detail::make_future_deferred_shared_state<Rp>(
  3583. // BF(
  3584. // thread_detail::decay_copy(boost::forward<F>(f))
  3585. // )
  3586. // );
  3587. } else {
  3588. std::terminate();
  3589. //BOOST_THREAD_FUTURE<R> ret;
  3590. //return ::boost::move(ret);
  3591. }
  3592. }
  3593. #endif // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3594. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  3595. namespace detail {
  3596. /////////////////////////
  3597. /// shared_state_nullary_task
  3598. /////////////////////////
  3599. template<typename Rp, typename Fp>
  3600. struct shared_state_nullary_task
  3601. {
  3602. typedef shared_ptr<shared_state_base > storage_type;
  3603. storage_type that;
  3604. Fp f_;
  3605. public:
  3606. shared_state_nullary_task(storage_type st, BOOST_THREAD_FWD_REF(Fp) f)
  3607. : that(st), f_(boost::move(f))
  3608. {};
  3609. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  3610. BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_state_nullary_task)
  3611. shared_state_nullary_task(shared_state_nullary_task const& x) //BOOST_NOEXCEPT
  3612. : that(x.that), f_(x.f_)
  3613. {}
  3614. shared_state_nullary_task& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT
  3615. {
  3616. if (this != &x) {
  3617. that=x.that;
  3618. f_=x.f_;
  3619. }
  3620. return *this;
  3621. }
  3622. // move
  3623. shared_state_nullary_task(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT
  3624. : that(x.that), f_(boost::move(x.f_))
  3625. {
  3626. x.that.reset();
  3627. }
  3628. shared_state_nullary_task& operator=(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT
  3629. {
  3630. if (this != &x) {
  3631. that=x.that;
  3632. f_=boost::move(x.f_);
  3633. x.that.reset();
  3634. }
  3635. return *this;
  3636. }
  3637. #endif
  3638. void operator()() {
  3639. shared_ptr<shared_state<Rp> > that_ = static_pointer_cast<shared_state<Rp> >(that);
  3640. try {
  3641. that_->mark_finished_with_result(f_());
  3642. } catch(...) {
  3643. that_->mark_exceptional_finish();
  3644. }
  3645. }
  3646. ~shared_state_nullary_task()
  3647. {
  3648. }
  3649. };
  3650. template<typename Fp>
  3651. struct shared_state_nullary_task<void, Fp>
  3652. {
  3653. typedef shared_ptr<shared_state_base > storage_type;
  3654. storage_type that;
  3655. Fp f_;
  3656. public:
  3657. shared_state_nullary_task(storage_type st, BOOST_THREAD_FWD_REF(Fp) f)
  3658. : that(st), f_(boost::move(f))
  3659. {};
  3660. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  3661. BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_state_nullary_task)
  3662. shared_state_nullary_task(shared_state_nullary_task const& x) //BOOST_NOEXCEPT
  3663. : that(x.that), f_(x.f_)
  3664. {}
  3665. shared_state_nullary_task& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT
  3666. {
  3667. if (this != &x) {
  3668. that=x.that;
  3669. f_=x.f_;
  3670. }
  3671. return *this;
  3672. }
  3673. // move
  3674. shared_state_nullary_task(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) BOOST_NOEXCEPT
  3675. : that(x.that), f_(boost::move(x.f_))
  3676. {
  3677. x.that.reset();
  3678. }
  3679. shared_state_nullary_task& operator=(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) BOOST_NOEXCEPT {
  3680. if (this != &x) {
  3681. that=x.that;
  3682. f_=boost::move(x.f_);
  3683. x.that.reset();
  3684. }
  3685. return *this;
  3686. }
  3687. #endif
  3688. void operator()() {
  3689. shared_ptr<shared_state<void> > that_ = static_pointer_cast<shared_state<void> >(that);
  3690. try {
  3691. f_();
  3692. that_->mark_finished_with_result();
  3693. } catch(...) {
  3694. that_->mark_exceptional_finish();
  3695. }
  3696. }
  3697. };
  3698. }
  3699. BOOST_THREAD_DCL_MOVABLE_BEG2(R,F) detail::shared_state_nullary_task<R,F> BOOST_THREAD_DCL_MOVABLE_END
  3700. namespace detail {
  3701. /////////////////////////
  3702. /// future_executor_shared_state_base
  3703. /////////////////////////
  3704. template<typename Rp>
  3705. struct future_executor_shared_state: shared_state<Rp>
  3706. {
  3707. typedef shared_state<Rp> base_type;
  3708. protected:
  3709. public:
  3710. future_executor_shared_state() {
  3711. }
  3712. template <class Fp, class Executor>
  3713. void init(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f)
  3714. {
  3715. typedef typename decay<Fp>::type Cont;
  3716. this->set_executor_policy(executor_ptr_type(new executor_ref<Executor>(ex)));
  3717. shared_state_nullary_task<Rp,Cont> t(this->shared_from_this(), boost::forward<Fp>(f));
  3718. ex.submit(boost::move(t));
  3719. }
  3720. ~future_executor_shared_state() {}
  3721. };
  3722. ////////////////////////////////
  3723. // make_future_executor_shared_state
  3724. ////////////////////////////////
  3725. template <class Rp, class Fp, class Executor>
  3726. BOOST_THREAD_FUTURE<Rp>
  3727. make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f) {
  3728. shared_ptr<future_executor_shared_state<Rp> >
  3729. h(new future_executor_shared_state<Rp>());
  3730. h->init(ex, boost::forward<Fp>(f));
  3731. return BOOST_THREAD_FUTURE<Rp>(h);
  3732. }
  3733. } // detail
  3734. ////////////////////////////////
  3735. // template <class Executor, class F, class... ArgTypes>
  3736. // future<R> async(Executor& ex, F&&, ArgTypes&&...);
  3737. ////////////////////////////////
  3738. //#if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3739. #if defined(BOOST_THREAD_PROVIDES_INVOKE) && ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && ! defined(BOOST_NO_CXX11_HDR_TUPLE)
  3740. #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3741. template <class Executor, class R, class... ArgTypes>
  3742. BOOST_THREAD_FUTURE<R>
  3743. async(Executor& ex, R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3744. typedef R(*F)(BOOST_THREAD_FWD_REF(ArgTypes)...);
  3745. typedef detail::invoker<typename decay<F>::type, typename decay<ArgTypes>::type...> BF;
  3746. typedef typename BF::result_type Rp;
  3747. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3748. BF(
  3749. f
  3750. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3751. )
  3752. ));
  3753. }
  3754. #endif // defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3755. template <class Executor, class F, class ...ArgTypes>
  3756. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
  3757. typename decay<ArgTypes>::type...
  3758. )>::type>
  3759. async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3760. typedef detail::invoker<typename decay<F>::type, typename decay<ArgTypes>::type...> BF;
  3761. typedef typename BF::result_type Rp;
  3762. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3763. BF(
  3764. thread_detail::decay_copy(boost::forward<F>(f))
  3765. , thread_detail::decay_copy(boost::forward<ArgTypes>(args))...
  3766. )
  3767. ));
  3768. }
  3769. #else // ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3770. #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3771. template <class Executor, class R>
  3772. BOOST_THREAD_FUTURE<R>
  3773. async(Executor& ex, R(*f)()) {
  3774. typedef R(*F)();
  3775. typedef detail::invoker<F> BF;
  3776. typedef typename BF::result_type Rp;
  3777. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3778. BF(
  3779. f
  3780. )
  3781. ));
  3782. }
  3783. template <class Executor, class R, class A1>
  3784. BOOST_THREAD_FUTURE<R>
  3785. async(Executor& ex, R(*f)(BOOST_THREAD_FWD_REF(A1)), BOOST_THREAD_FWD_REF(A1) a1) {
  3786. typedef R(*F)(BOOST_THREAD_FWD_REF(A1));
  3787. typedef detail::invoker<F, typename decay<A1>::type> BF;
  3788. typedef typename BF::result_type Rp;
  3789. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3790. BF(
  3791. f
  3792. , thread_detail::decay_copy(boost::forward<A1>(a1))
  3793. )
  3794. ));
  3795. }
  3796. #endif // defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3797. template <class Executor, class F>
  3798. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type()>::type>
  3799. async(Executor& ex, BOOST_THREAD_FWD_REF(F) f) {
  3800. typedef detail::invoker<typename decay<F>::type> BF;
  3801. typedef typename BF::result_type Rp;
  3802. return boost::detail::make_future_executor_shared_state<Rp>(ex,
  3803. BF(
  3804. thread_detail::decay_copy(boost::forward<F>(f))
  3805. )
  3806. );
  3807. }
  3808. template <class Executor, class F, class A1>
  3809. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
  3810. typename decay<A1>::type
  3811. )>::type>
  3812. async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) {
  3813. typedef detail::invoker<typename decay<F>::type, typename decay<A1>::type> BF;
  3814. typedef typename BF::result_type Rp;
  3815. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3816. BF(
  3817. thread_detail::decay_copy(boost::forward<F>(f))
  3818. , thread_detail::decay_copy(boost::forward<A1>(a1))
  3819. )
  3820. ));
  3821. }
  3822. template <class Executor, class F, class A1, class A2>
  3823. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
  3824. typename decay<A1>::type, typename decay<A2>::type
  3825. )>::type>
  3826. async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) {
  3827. typedef detail::invoker<typename decay<F>::type, typename decay<A1>::type, typename decay<A2>::type> BF;
  3828. typedef typename BF::result_type Rp;
  3829. return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state<Rp>(ex,
  3830. BF(
  3831. thread_detail::decay_copy(boost::forward<F>(f))
  3832. , thread_detail::decay_copy(boost::forward<A1>(a1))
  3833. , thread_detail::decay_copy(boost::forward<A2>(a2))
  3834. )
  3835. ));
  3836. }
  3837. #endif //! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3838. #endif
  3839. ////////////////////////////////
  3840. // template <class F, class... ArgTypes>
  3841. // future<R> async(F&&, ArgTypes&&...);
  3842. ////////////////////////////////
  3843. #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNCTION_PTR
  3844. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3845. template <class R, class... ArgTypes>
  3846. BOOST_THREAD_FUTURE<R>
  3847. async(R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3848. return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f, boost::forward<ArgTypes>(args)...));
  3849. }
  3850. #else
  3851. template <class R>
  3852. BOOST_THREAD_FUTURE<R>
  3853. async(R(*f)()) {
  3854. return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f));
  3855. }
  3856. #endif
  3857. #endif
  3858. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  3859. template <class F, class ...ArgTypes>
  3860. BOOST_THREAD_FUTURE<typename boost::result_of<typename decay<F>::type(
  3861. typename decay<ArgTypes>::type...
  3862. )>::type>
  3863. async(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) {
  3864. return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f), boost::forward<ArgTypes>(args)...));
  3865. }
  3866. #else
  3867. template <class F>
  3868. BOOST_THREAD_FUTURE<typename boost::result_of<F()>::type>
  3869. async(BOOST_THREAD_FWD_REF(F) f) {
  3870. return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward<F>(f)));
  3871. }
  3872. #endif
  3873. ////////////////////////////////
  3874. // make_future deprecated
  3875. ////////////////////////////////
  3876. template <typename T>
  3877. BOOST_THREAD_FUTURE<typename decay<T>::type> make_future(BOOST_THREAD_FWD_REF(T) value) {
  3878. typedef typename decay<T>::type future_value_type;
  3879. promise<future_value_type> p;
  3880. p.set_value(boost::forward<future_value_type>(value));
  3881. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3882. }
  3883. #if defined BOOST_THREAD_USES_MOVE
  3884. inline BOOST_THREAD_FUTURE<void> make_future() {
  3885. promise<void> p;
  3886. p.set_value();
  3887. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3888. }
  3889. #endif
  3890. ////////////////////////////////
  3891. // make_ready_future
  3892. ////////////////////////////////
  3893. namespace detail {
  3894. template <class T>
  3895. struct deduced_type_impl
  3896. {
  3897. typedef T type;
  3898. };
  3899. template <class T>
  3900. struct deduced_type_impl<reference_wrapper<T> const>
  3901. {
  3902. typedef T& type;
  3903. };
  3904. template <class T>
  3905. struct deduced_type_impl<reference_wrapper<T> >
  3906. {
  3907. typedef T& type;
  3908. };
  3909. #if __cplusplus > 201103L
  3910. template <class T>
  3911. struct deduced_type_impl<std::reference_wrapper<T> >
  3912. {
  3913. typedef T& type;
  3914. };
  3915. #endif
  3916. template <class T>
  3917. struct deduced_type
  3918. {
  3919. typedef typename detail::deduced_type_impl<typename decay<T>::type>::type type;
  3920. };
  3921. }
  3922. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3923. template <int = 0, int..., class T>
  3924. #else
  3925. template <class T>
  3926. #endif
  3927. BOOST_THREAD_FUTURE<typename detail::deduced_type<T>::type> make_ready_future(BOOST_THREAD_FWD_REF(T) value) {
  3928. typedef typename detail::deduced_type<T>::type future_value_type;
  3929. promise<future_value_type> p;
  3930. p.set_value(boost::forward<T>(value));
  3931. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3932. }
  3933. // explicit overloads
  3934. template <class T>
  3935. BOOST_THREAD_FUTURE<T> make_ready_future(typename remove_reference<T>::type & x)
  3936. {
  3937. promise<T> p;
  3938. p.set_value(x);
  3939. return p.get_future();
  3940. }
  3941. template <class T>
  3942. BOOST_THREAD_FUTURE<T> make_ready_future(BOOST_THREAD_FWD_REF(typename remove_reference<T>::type) x)
  3943. {
  3944. promise<T> p;
  3945. p.set_value(forward<typename remove_reference<T>::type>(x));
  3946. return p.get_future();
  3947. }
  3948. // variadic overload
  3949. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  3950. template <class T, class ...Args>
  3951. BOOST_THREAD_FUTURE<T> make_ready_future(Args&&... args)
  3952. {
  3953. promise<T> p;
  3954. p.emplace(forward<Args>(args)...);
  3955. return p.get_future();
  3956. }
  3957. #endif
  3958. template <typename T, typename T1>
  3959. BOOST_THREAD_FUTURE<T> make_ready_no_decay_future(T1 value) {
  3960. typedef T future_value_type;
  3961. promise<future_value_type> p;
  3962. p.set_value(value);
  3963. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3964. }
  3965. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined BOOST_THREAD_USES_MOVE
  3966. inline BOOST_THREAD_FUTURE<void> make_ready_future() {
  3967. promise<void> p;
  3968. p.set_value();
  3969. return p.get_future();
  3970. }
  3971. #endif
  3972. template <typename T>
  3973. BOOST_THREAD_FUTURE<T> make_exceptional_future(exception_ptr ex) {
  3974. promise<T> p;
  3975. p.set_exception(ex);
  3976. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3977. }
  3978. template <typename T, typename E>
  3979. BOOST_THREAD_FUTURE<T> make_exceptional_future(E ex) {
  3980. promise<T> p;
  3981. p.set_exception(boost::copy_exception(ex));
  3982. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3983. }
  3984. template <typename T>
  3985. BOOST_THREAD_FUTURE<T> make_exceptional_future() {
  3986. promise<T> p;
  3987. p.set_exception(boost::current_exception());
  3988. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  3989. }
  3990. template <typename T>
  3991. BOOST_THREAD_FUTURE<T> make_ready_future(exception_ptr ex) {
  3992. return make_exceptional_future<T>(ex);
  3993. }
  3994. #if 0
  3995. template<typename CLOSURE>
  3996. make_future(CLOSURE closure) -> BOOST_THREAD_FUTURE<decltype(closure())> {
  3997. typedef decltype(closure()) T;
  3998. promise<T> p;
  3999. try {
  4000. p.set_value(closure());
  4001. } catch(...) {
  4002. p.set_exception(std::current_exception());
  4003. }
  4004. return BOOST_THREAD_MAKE_RV_REF(p.get_future());
  4005. }
  4006. #endif
  4007. ////////////////////////////////
  4008. // make_shared_future deprecated
  4009. ////////////////////////////////
  4010. template <typename T>
  4011. shared_future<typename decay<T>::type> make_shared_future(BOOST_THREAD_FWD_REF(T) value) {
  4012. typedef typename decay<T>::type future_type;
  4013. promise<future_type> p;
  4014. p.set_value(boost::forward<T>(value));
  4015. return BOOST_THREAD_MAKE_RV_REF(p.get_future().share());
  4016. }
  4017. inline shared_future<void> make_shared_future() {
  4018. promise<void> p;
  4019. return BOOST_THREAD_MAKE_RV_REF(p.get_future().share());
  4020. }
  4021. ////////////////////////////////
  4022. // detail::future_async_continuation_shared_state
  4023. ////////////////////////////////
  4024. #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  4025. namespace detail
  4026. {
  4027. //////////////////////
  4028. // detail::continuation_shared_state
  4029. //////////////////////
  4030. template<typename F, typename Rp, typename Fp, class ShSt=shared_state<Rp> >
  4031. struct continuation_shared_state: ShSt
  4032. {
  4033. F parent;
  4034. Fp continuation;
  4035. public:
  4036. continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4037. : parent(boost::move(f)),
  4038. continuation(boost::move(c))
  4039. {
  4040. }
  4041. void init(boost::unique_lock<boost::mutex> &lock)
  4042. {
  4043. parent.future_->set_continuation_ptr(this->shared_from_this(), lock);
  4044. }
  4045. void call() {
  4046. try {
  4047. this->mark_finished_with_result(this->continuation(boost::move(this->parent)));
  4048. } catch(...) {
  4049. this->mark_exceptional_finish();
  4050. }
  4051. // make sure parent is really cleared to prevent memory "leaks"
  4052. this->parent = F();
  4053. }
  4054. void call(boost::unique_lock<boost::mutex>& lck) {
  4055. try {
  4056. relocker relock(lck);
  4057. // neither continuation nor parent are protected by the lock - call() must only
  4058. // be called once, and no one else must modify it.
  4059. Rp res = this->continuation(boost::move(this->parent));
  4060. // make sure parent is really cleared to prevent memory "leaks"
  4061. this->parent = F();
  4062. relock.lock();
  4063. this->mark_finished_with_result_internal(boost::move(res), lck);
  4064. } catch (...) {
  4065. this->mark_exceptional_finish_internal(current_exception(), lck);
  4066. // make sure parent is really cleared to prevent memory "leaks"
  4067. relocker relock(lck);
  4068. this->parent = F();
  4069. }
  4070. }
  4071. static void run(shared_ptr<boost::detail::shared_state_base> that_)
  4072. {
  4073. continuation_shared_state* that = static_cast<continuation_shared_state*>(that_.get());
  4074. that->call();
  4075. }
  4076. ~continuation_shared_state() {}
  4077. };
  4078. template<typename F, typename Fp, class ShSt>
  4079. struct continuation_shared_state<F, void, Fp, ShSt>: ShSt
  4080. {
  4081. F parent;
  4082. Fp continuation;
  4083. public:
  4084. continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4085. : parent(boost::move(f)),
  4086. continuation(boost::move(c))
  4087. {
  4088. }
  4089. void init(boost::unique_lock<boost::mutex> &lock)
  4090. {
  4091. parent.future_->set_continuation_ptr(this->shared_from_this(), lock);
  4092. }
  4093. void call()
  4094. {
  4095. try {
  4096. this->continuation(boost::move(this->parent));
  4097. this->mark_finished_with_result();
  4098. } catch(...) {
  4099. this->mark_exceptional_finish();
  4100. }
  4101. // make sure parent is really cleared to prevent memory "leaks"
  4102. this->parent = F();
  4103. }
  4104. void call(boost::unique_lock<boost::mutex>& lck) {
  4105. try {
  4106. {
  4107. relocker relock(lck);
  4108. // neither continuation nor parent are protected by the lock - call() must only
  4109. // be called once, and no one else must modify it.
  4110. this->continuation(boost::move(this->parent));
  4111. // make sure parent is really cleared to prevent memory "leaks"
  4112. this->parent = F();
  4113. }
  4114. this->mark_finished_with_result_internal(lck);
  4115. } catch (...) {
  4116. this->mark_exceptional_finish_internal(current_exception(), lck);
  4117. // make sure parent is really cleared to prevent memory "leaks"
  4118. relocker relock(lck);
  4119. this->parent = F();
  4120. }
  4121. }
  4122. static void run(shared_ptr<boost::detail::shared_state_base> that_)
  4123. {
  4124. continuation_shared_state* that = static_cast<continuation_shared_state*>(that_.get());
  4125. that->call();
  4126. }
  4127. ~continuation_shared_state() {}
  4128. };
  4129. /////////////////////////
  4130. /// future_async_continuation_shared_state
  4131. /////////////////////////
  4132. template<typename F, typename Rp, typename Fp>
  4133. struct future_async_continuation_shared_state: continuation_shared_state<F,Rp,Fp,future_async_shared_state_base<Rp> >
  4134. {
  4135. typedef continuation_shared_state<F,Rp,Fp,future_async_shared_state_base<Rp> > base_type;
  4136. public:
  4137. future_async_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4138. : base_type(boost::move(f), boost::forward<Fp>(c))
  4139. { }
  4140. void launch_continuation() {
  4141. #if defined BOOST_THREAD_FUTURE_BLOCKING
  4142. boost::lock_guard<boost::mutex> lk(this->mutex);
  4143. this->thr_ = boost::thread(&future_async_continuation_shared_state::run, static_shared_from_this(this));
  4144. #else
  4145. boost::thread(&base_type::run, static_shared_from_this(this)).detach();
  4146. #endif
  4147. }
  4148. };
  4149. /////////////////////////
  4150. /// future_sync_continuation_shared_state
  4151. /////////////////////////
  4152. template<typename F, typename Rp, typename Fp>
  4153. struct future_sync_continuation_shared_state: continuation_shared_state<F,Rp,Fp,shared_state<Rp> >
  4154. {
  4155. typedef continuation_shared_state<F,Rp,Fp,shared_state<Rp> > base_type;
  4156. public:
  4157. future_sync_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4158. : base_type(boost::move(f), boost::forward<Fp>(c))
  4159. { }
  4160. void launch_continuation() {
  4161. this->call();
  4162. }
  4163. };
  4164. /////////////////////////
  4165. /// future_executor_continuation_shared_state
  4166. /////////////////////////
  4167. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4168. template <typename FutureExecutorContinuationSharedState>
  4169. struct run_it {
  4170. shared_ptr<FutureExecutorContinuationSharedState> that_;
  4171. #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  4172. BOOST_THREAD_COPYABLE_AND_MOVABLE(run_it)
  4173. run_it(run_it const& x) //BOOST_NOEXCEPT
  4174. : that_(x.that_)
  4175. {}
  4176. run_it& operator=(BOOST_THREAD_COPY_ASSIGN_REF(run_it) x) //BOOST_NOEXCEPT
  4177. {
  4178. if (this != &x) {
  4179. that_=x.that_;
  4180. }
  4181. return *this;
  4182. }
  4183. // move
  4184. run_it(BOOST_THREAD_RV_REF(run_it) x) BOOST_NOEXCEPT
  4185. : that_(x.that_)
  4186. {
  4187. x.that_.reset();
  4188. }
  4189. run_it& operator=(BOOST_THREAD_RV_REF(run_it) x) BOOST_NOEXCEPT {
  4190. if (this != &x) {
  4191. that_=x.that;
  4192. x.that_.reset();
  4193. }
  4194. return *this;
  4195. }
  4196. #endif
  4197. run_it(shared_ptr<FutureExecutorContinuationSharedState> that) : that_ (that) {}
  4198. void operator()()
  4199. {
  4200. that_->run(that_);
  4201. }
  4202. };
  4203. }
  4204. BOOST_THREAD_DCL_MOVABLE_BEG(F) detail::run_it<F> BOOST_THREAD_DCL_MOVABLE_END
  4205. namespace detail {
  4206. template<typename F, typename Rp, typename Fp>
  4207. struct future_executor_continuation_shared_state: continuation_shared_state<F,Rp,Fp>
  4208. {
  4209. typedef continuation_shared_state<F,Rp,Fp> base_type;
  4210. public:
  4211. future_executor_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4212. : base_type(boost::move(f), boost::forward<Fp>(c))
  4213. {
  4214. }
  4215. template <class Ex>
  4216. void init(boost::unique_lock<boost::mutex> &lk, Ex& ex)
  4217. {
  4218. this->set_executor_policy(executor_ptr_type(new executor_ref<Ex>(ex)), lk);
  4219. this->base_type::init(lk);
  4220. }
  4221. void launch_continuation() {
  4222. run_it<base_type> fct(static_shared_from_this(this));
  4223. this->get_executor()->submit(boost::move(fct));
  4224. }
  4225. ~future_executor_continuation_shared_state() {}
  4226. };
  4227. #endif
  4228. /////////////////////////
  4229. /// shared_future_async_continuation_shared_state
  4230. /////////////////////////
  4231. template<typename F, typename Rp, typename Fp>
  4232. struct shared_future_async_continuation_shared_state: continuation_shared_state<F,Rp,Fp,future_async_shared_state_base<Rp> >
  4233. {
  4234. typedef continuation_shared_state<F,Rp,Fp,future_async_shared_state_base<Rp> > base_type;
  4235. public:
  4236. shared_future_async_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c)
  4237. : base_type(boost::move(f), boost::forward<Fp>(c))
  4238. {
  4239. }
  4240. void launch_continuation() {
  4241. #if defined BOOST_THREAD_FUTURE_BLOCKING
  4242. boost::lock_guard<boost::mutex> lk(this->mutex);
  4243. this->thr_ = boost::thread(&base_type::run, static_shared_from_this(this));
  4244. #else
  4245. boost::thread(&base_type::run, static_shared_from_this(this)).detach();
  4246. #endif
  4247. }
  4248. };
  4249. /////////////////////////
  4250. /// shared_future_async_continuation_shared_state
  4251. /////////////////////////
  4252. template<typename F, typename Rp, typename Fp>
  4253. struct shared_future_sync_continuation_shared_state: continuation_shared_state<F,Rp,Fp,shared_state<Rp> >
  4254. {
  4255. typedef continuation_shared_state<F,Rp,Fp,shared_state<Rp> > base_type;
  4256. public:
  4257. shared_future_sync_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c)
  4258. : base_type(boost::move(f), boost::forward<Fp>(c))
  4259. {
  4260. }
  4261. void launch_continuation() {
  4262. this->call();
  4263. }
  4264. };
  4265. /////////////////////////
  4266. /// shared_future_executor_continuation_shared_state
  4267. /////////////////////////
  4268. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4269. template<typename F, typename Rp, typename Fp>
  4270. struct shared_future_executor_continuation_shared_state: continuation_shared_state<F,Rp,Fp>
  4271. {
  4272. typedef continuation_shared_state<F,Rp,Fp> base_type;
  4273. public:
  4274. shared_future_executor_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c)
  4275. : base_type(boost::move(f), boost::forward<Fp>(c))
  4276. {
  4277. }
  4278. template <class Ex>
  4279. void init(boost::unique_lock<boost::mutex> &lk, Ex& ex)
  4280. {
  4281. this->set_executor_policy(executor_ptr_type(new executor_ref<Ex>(ex)), lk);
  4282. this->base_type::init(lk);
  4283. }
  4284. void launch_continuation() {
  4285. run_it<base_type> fct(static_shared_from_this(this));
  4286. this->get_executor()->submit(boost::move(fct));
  4287. }
  4288. ~shared_future_executor_continuation_shared_state() {}
  4289. };
  4290. #endif
  4291. //////////////////////////
  4292. /// future_deferred_continuation_shared_state
  4293. //////////////////////////
  4294. template<typename F, typename Rp, typename Fp>
  4295. struct future_deferred_continuation_shared_state: continuation_shared_state<F,Rp,Fp>
  4296. {
  4297. typedef continuation_shared_state<F,Rp,Fp> base_type;
  4298. public:
  4299. future_deferred_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c)
  4300. : base_type(boost::move(f), boost::forward<Fp>(c))
  4301. {
  4302. this->set_deferred();
  4303. }
  4304. virtual void execute(boost::unique_lock<boost::mutex>& lk) {
  4305. this->parent.wait();
  4306. this->call(lk);
  4307. }
  4308. virtual void launch_continuation() { }
  4309. };
  4310. //////////////////////////
  4311. /// shared_future_deferred_continuation_shared_state
  4312. //////////////////////////
  4313. template<typename F, typename Rp, typename Fp>
  4314. struct shared_future_deferred_continuation_shared_state: continuation_shared_state<F,Rp,Fp>
  4315. {
  4316. typedef continuation_shared_state<F,Rp,Fp> base_type;
  4317. public:
  4318. shared_future_deferred_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c)
  4319. : base_type(boost::move(f), boost::forward<Fp>(c))
  4320. {
  4321. this->set_deferred();
  4322. }
  4323. virtual void execute(boost::unique_lock<boost::mutex>& lk) {
  4324. this->parent.wait();
  4325. this->call(lk);
  4326. }
  4327. virtual void launch_continuation() { }
  4328. };
  4329. ////////////////////////////////
  4330. // make_future_deferred_continuation_shared_state
  4331. ////////////////////////////////
  4332. template<typename F, typename Rp, typename Fp>
  4333. BOOST_THREAD_FUTURE<Rp>
  4334. make_future_deferred_continuation_shared_state(
  4335. boost::unique_lock<boost::mutex> &lock,
  4336. BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) {
  4337. typedef typename decay<Fp>::type Cont;
  4338. shared_ptr<future_deferred_continuation_shared_state<F, Rp, Cont> >
  4339. h(new future_deferred_continuation_shared_state<F, Rp, Cont>(boost::move(f), boost::forward<Fp>(c)));
  4340. h->init(lock);
  4341. return BOOST_THREAD_FUTURE<Rp>(h);
  4342. }
  4343. ////////////////////////////////
  4344. // make_future_async_continuation_shared_state
  4345. ////////////////////////////////
  4346. template<typename F, typename Rp, typename Fp>
  4347. BOOST_THREAD_FUTURE<Rp>
  4348. make_future_async_continuation_shared_state(
  4349. boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f,
  4350. BOOST_THREAD_FWD_REF(Fp) c) {
  4351. typedef typename decay<Fp>::type Cont;
  4352. shared_ptr<future_async_continuation_shared_state<F,Rp, Cont> >
  4353. h(new future_async_continuation_shared_state<F,Rp, Cont>(boost::move(f), boost::forward<Fp>(c)));
  4354. h->init(lock);
  4355. return BOOST_THREAD_FUTURE<Rp>(h);
  4356. }
  4357. ////////////////////////////////
  4358. // make_future_sync_continuation_shared_state
  4359. ////////////////////////////////
  4360. template<typename F, typename Rp, typename Fp>
  4361. BOOST_THREAD_FUTURE<Rp>
  4362. make_future_sync_continuation_shared_state(
  4363. boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f,
  4364. BOOST_THREAD_FWD_REF(Fp) c) {
  4365. typedef typename decay<Fp>::type Cont;
  4366. shared_ptr<future_sync_continuation_shared_state<F,Rp, Cont> >
  4367. h(new future_sync_continuation_shared_state<F,Rp, Cont>(boost::move(f), boost::forward<Fp>(c)));
  4368. h->init(lock);
  4369. return BOOST_THREAD_FUTURE<Rp>(h);
  4370. }
  4371. ////////////////////////////////
  4372. // make_future_executor_continuation_shared_state
  4373. ////////////////////////////////
  4374. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4375. template<typename Ex, typename F, typename Rp, typename Fp>
  4376. BOOST_THREAD_FUTURE<Rp>
  4377. make_future_executor_continuation_shared_state(Ex& ex,
  4378. boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f,
  4379. BOOST_THREAD_FWD_REF(Fp) c) {
  4380. typedef typename decay<Fp>::type Cont;
  4381. shared_ptr<future_executor_continuation_shared_state<F,Rp, Cont> >
  4382. h(new future_executor_continuation_shared_state<F,Rp, Cont>(boost::move(f), boost::forward<Fp>(c)));
  4383. h->init(lock, ex);
  4384. return BOOST_THREAD_FUTURE<Rp>(h);
  4385. }
  4386. #endif
  4387. ////////////////////////////////
  4388. // make_shared_future_deferred_continuation_shared_state
  4389. ////////////////////////////////
  4390. template<typename F, typename Rp, typename Fp>
  4391. BOOST_THREAD_FUTURE<Rp>
  4392. make_shared_future_deferred_continuation_shared_state(
  4393. boost::unique_lock<boost::mutex> &lock,
  4394. F f, BOOST_THREAD_FWD_REF(Fp) c) {
  4395. typedef typename decay<Fp>::type Cont;
  4396. shared_ptr<shared_future_deferred_continuation_shared_state<F, Rp, Cont> >
  4397. h(new shared_future_deferred_continuation_shared_state<F, Rp, Cont>(f, boost::forward<Fp>(c)));
  4398. h->init(lock);
  4399. return BOOST_THREAD_FUTURE<Rp>(h);
  4400. }
  4401. ////////////////////////////////
  4402. // make_shared_future_async_continuation_shared_state
  4403. ////////////////////////////////
  4404. template<typename F, typename Rp, typename Fp>
  4405. BOOST_THREAD_FUTURE<Rp>
  4406. make_shared_future_async_continuation_shared_state(
  4407. boost::unique_lock<boost::mutex> &lock, F f,
  4408. BOOST_THREAD_FWD_REF(Fp) c) {
  4409. typedef typename decay<Fp>::type Cont;
  4410. shared_ptr<shared_future_async_continuation_shared_state<F,Rp, Cont> >
  4411. h(new shared_future_async_continuation_shared_state<F,Rp, Cont>(f, boost::forward<Fp>(c)));
  4412. h->init(lock);
  4413. return BOOST_THREAD_FUTURE<Rp>(h);
  4414. }
  4415. ////////////////////////////////
  4416. // make_shared_future_sync_continuation_shared_state
  4417. ////////////////////////////////
  4418. template<typename F, typename Rp, typename Fp>
  4419. BOOST_THREAD_FUTURE<Rp>
  4420. make_shared_future_sync_continuation_shared_state(
  4421. boost::unique_lock<boost::mutex> &lock, F f,
  4422. BOOST_THREAD_FWD_REF(Fp) c) {
  4423. typedef typename decay<Fp>::type Cont;
  4424. shared_ptr<shared_future_sync_continuation_shared_state<F,Rp, Cont> >
  4425. h(new shared_future_sync_continuation_shared_state<F,Rp, Cont>(f, boost::forward<Fp>(c)));
  4426. h->init(lock);
  4427. return BOOST_THREAD_FUTURE<Rp>(h);
  4428. }
  4429. ////////////////////////////////
  4430. // make_shared_future_executor_continuation_shared_state
  4431. ////////////////////////////////
  4432. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4433. template<typename Ex, typename F, typename Rp, typename Fp>
  4434. BOOST_THREAD_FUTURE<Rp>
  4435. make_shared_future_executor_continuation_shared_state(Ex& ex,
  4436. boost::unique_lock<boost::mutex> &lock, F f,
  4437. BOOST_THREAD_FWD_REF(Fp) c) {
  4438. typedef typename decay<Fp>::type Cont;
  4439. shared_ptr<shared_future_executor_continuation_shared_state<F, Rp, Cont> >
  4440. h(new shared_future_executor_continuation_shared_state<F, Rp, Cont>(f, boost::forward<Fp>(c)));
  4441. h->init(lock, ex);
  4442. return BOOST_THREAD_FUTURE<Rp>(h);
  4443. }
  4444. #endif
  4445. }
  4446. ////////////////////////////////
  4447. // template<typename F>
  4448. // auto future<R>::then(launch policy, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4449. ////////////////////////////////
  4450. template <typename R>
  4451. template <typename F>
  4452. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type>
  4453. BOOST_THREAD_FUTURE<R>::then(launch policy, BOOST_THREAD_FWD_REF(F) func) {
  4454. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4455. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4456. // keep state alive as we move ourself but hold the lock
  4457. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4458. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4459. if (underlying_cast<int>(policy) & int(launch::async)) {
  4460. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4461. lock, boost::move(*this), boost::forward<F>(func)
  4462. )));
  4463. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4464. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4465. lock, boost::move(*this), boost::forward<F>(func)
  4466. )));
  4467. } else if (underlying_cast<int>(policy) & int(launch::sync)) {
  4468. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4469. lock, boost::move(*this), boost::forward<F>(func)
  4470. )));
  4471. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4472. } else if (underlying_cast<int>(policy) & int(launch::executor)) {
  4473. assert(this->future_->get_executor());
  4474. typedef executor Ex;
  4475. Ex& ex = *(this->future_->get_executor());
  4476. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4477. lock, boost::move(*this), boost::forward<F>(func)
  4478. )));
  4479. #endif
  4480. } else if (underlying_cast<int>(policy) & int(launch::inherit)) {
  4481. launch policy_ = this->launch_policy(lock);
  4482. if (underlying_cast<int>(policy_) & int(launch::async)) {
  4483. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4484. lock, boost::move(*this), boost::forward<F>(func)
  4485. )));
  4486. } else if (underlying_cast<int>(policy_) & int(launch::deferred)) {
  4487. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4488. lock, boost::move(*this), boost::forward<F>(func)
  4489. )));
  4490. } else if (underlying_cast<int>(policy_) & int(launch::sync)) {
  4491. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4492. lock, boost::move(*this), boost::forward<F>(func)
  4493. )));
  4494. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4495. } else if (underlying_cast<int>(policy_) & int(launch::executor)) {
  4496. assert(this->future_->get_executor());
  4497. typedef executor Ex;
  4498. Ex& ex = *(this->future_->get_executor());
  4499. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4500. lock, boost::move(*this), boost::forward<F>(func)
  4501. )));
  4502. #endif
  4503. } else {
  4504. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4505. lock, boost::move(*this), boost::forward<F>(func)
  4506. )));
  4507. }
  4508. } else {
  4509. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4510. lock, boost::move(*this), boost::forward<F>(func)
  4511. )));
  4512. }
  4513. }
  4514. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4515. ////////////////////////////////
  4516. // template<typename Ex, typename F>
  4517. // auto future<future<R2> >::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4518. ////////////////////////////////
  4519. template <typename R>
  4520. template <typename Ex, typename F>
  4521. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type>
  4522. BOOST_THREAD_FUTURE<R>::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) {
  4523. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4524. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4525. // keep state alive as we move ourself but hold the lock
  4526. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4527. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4528. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4529. lock, boost::move(*this), boost::forward<F>(func)
  4530. )));
  4531. }
  4532. #endif
  4533. ////////////////////////////////
  4534. // template<typename F>
  4535. // auto future<future<R2> >::then(F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4536. ////////////////////////////////
  4537. template <typename R>
  4538. template <typename F>
  4539. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type>
  4540. BOOST_THREAD_FUTURE<R>::then(BOOST_THREAD_FWD_REF(F) func) {
  4541. #ifndef BOOST_THREAD_CONTINUATION_SYNC
  4542. return this->then(this->launch_policy(), boost::forward<F>(func));
  4543. #else
  4544. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4545. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4546. // keep state alive as we move ourself but hold the lock
  4547. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4548. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4549. launch policy = this->launch_policy(lock);
  4550. if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4551. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4552. lock, boost::move(*this), boost::forward<F>(func)
  4553. )));
  4554. } else {
  4555. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4556. lock, boost::move(*this), boost::forward<F>(func)
  4557. )));
  4558. }
  4559. #endif
  4560. }
  4561. ////////////////////////////////
  4562. // template<typename F>
  4563. // auto future<future<R2> >::then(launch, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4564. ////////////////////////////////
  4565. template <typename R2>
  4566. template <typename F>
  4567. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >)>::type>
  4568. BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >::then(launch policy, BOOST_THREAD_FWD_REF(F) func) {
  4569. typedef BOOST_THREAD_FUTURE<R2> R;
  4570. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4571. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4572. // keep state alive as we move ourself but hold the lock
  4573. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4574. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4575. if (underlying_cast<int>(policy) & int(launch::async)) {
  4576. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4577. lock, boost::move(*this), boost::forward<F>(func)
  4578. )));
  4579. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4580. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4581. lock, boost::move(*this), boost::forward<F>(func)
  4582. )));
  4583. } else if (underlying_cast<int>(policy) & int(launch::sync)) {
  4584. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4585. lock, boost::move(*this), boost::forward<F>(func)
  4586. )));
  4587. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4588. } else if (underlying_cast<int>(policy) & int(launch::executor)) {
  4589. assert(this->future_->get_executor());
  4590. typedef executor Ex;
  4591. Ex& ex = *(this->future_->get_executor());
  4592. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4593. lock, boost::move(*this), boost::forward<F>(func)
  4594. )));
  4595. #endif
  4596. } else if (underlying_cast<int>(policy) & int(launch::inherit)) {
  4597. launch policy_ = this->launch_policy(lock);
  4598. if (underlying_cast<int>(policy_) & int(launch::async)) {
  4599. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4600. lock, boost::move(*this), boost::forward<F>(func)
  4601. )));
  4602. } else if (underlying_cast<int>(policy_) & int(launch::deferred)) {
  4603. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4604. lock, boost::move(*this), boost::forward<F>(func)
  4605. )));
  4606. } else if (underlying_cast<int>(policy_) & int(launch::sync)) {
  4607. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4608. lock, boost::move(*this), boost::forward<F>(func)
  4609. )));
  4610. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4611. } else if (underlying_cast<int>(policy_) & int(launch::executor)) {
  4612. assert(this->future_->get_executor());
  4613. typedef executor Ex;
  4614. Ex& ex = *(this->future_->get_executor());
  4615. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4616. lock, boost::move(*this), boost::forward<F>(func)
  4617. )));
  4618. #endif
  4619. } else {
  4620. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4621. lock, boost::move(*this), boost::forward<F>(func)
  4622. )));
  4623. }
  4624. } else {
  4625. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4626. lock, boost::move(*this), boost::forward<F>(func)
  4627. )));
  4628. }
  4629. }
  4630. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4631. ////////////////////////////////
  4632. // template<typename Ex, typename F>
  4633. // auto future<future<R2> >::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4634. ////////////////////////////////
  4635. template <typename R2>
  4636. template <typename Ex, typename F>
  4637. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >)>::type>
  4638. BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) {
  4639. typedef BOOST_THREAD_FUTURE<R2> R;
  4640. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4641. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4642. // keep state alive as we move ourself but hold the lock
  4643. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4644. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4645. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state<Ex, BOOST_THREAD_FUTURE<R>, future_type>(ex,
  4646. lock, boost::move(*this), boost::forward<F>(func)
  4647. )));
  4648. }
  4649. #endif
  4650. ////////////////////////////////
  4651. // template<typename F>
  4652. // auto future<future<R2> >::then(F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4653. ////////////////////////////////
  4654. template <typename R2>
  4655. template <typename F>
  4656. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >)>::type>
  4657. BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >::then(BOOST_THREAD_FWD_REF(F) func) {
  4658. #ifndef BOOST_THREAD_CONTINUATION_SYNC
  4659. return this->then(this->launch_policy(), boost::forward<F>(func));
  4660. #else
  4661. typedef BOOST_THREAD_FUTURE<R2> R;
  4662. typedef typename boost::result_of<F(BOOST_THREAD_FUTURE<R>)>::type future_type;
  4663. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4664. // keep state alive as we move ourself but hold the lock
  4665. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4666. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4667. launch policy = this->launch_policy(lock);
  4668. if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4669. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4670. lock, boost::move(*this), boost::forward<F>(func)
  4671. )));
  4672. } else {
  4673. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state<BOOST_THREAD_FUTURE<R>, future_type>(
  4674. lock, boost::move(*this), boost::forward<F>(func)
  4675. )));
  4676. }
  4677. #endif
  4678. }
  4679. ////////////////////////////////
  4680. // template<typename F>
  4681. // auto shared_future<R>::then(launch policy, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4682. ////////////////////////////////
  4683. template <typename R>
  4684. template <typename F>
  4685. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future<R>)>::type>
  4686. shared_future<R>::then(launch policy, BOOST_THREAD_FWD_REF(F) func) const
  4687. {
  4688. typedef typename boost::result_of<F(shared_future<R>)>::type future_type;
  4689. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4690. boost::unique_lock<boost::mutex> lock(this->future_->mutex);
  4691. if (underlying_cast<int>(policy) & int(launch::async)) {
  4692. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state<shared_future<R>, future_type>(
  4693. lock, *this, boost::forward<F>(func)
  4694. )));
  4695. } else if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4696. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state<shared_future<R>, future_type>(
  4697. lock, *this, boost::forward<F>(func)
  4698. )));
  4699. } else if (underlying_cast<int>(policy) & int(launch::sync)) {
  4700. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_continuation_shared_state<shared_future<R>, future_type>(
  4701. lock, *this, boost::forward<F>(func)
  4702. )));
  4703. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4704. } else if (underlying_cast<int>(policy) & int(launch::executor)) {
  4705. typedef executor Ex;
  4706. Ex& ex = *(this->future_->get_executor());
  4707. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state<Ex, shared_future<R>, future_type>(ex,
  4708. lock, *this, boost::forward<F>(func)
  4709. )));
  4710. #endif
  4711. } else if (underlying_cast<int>(policy) & int(launch::inherit)) {
  4712. launch policy_ = this->launch_policy(lock);
  4713. if (underlying_cast<int>(policy_) & int(launch::async)) {
  4714. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state<shared_future<R>, future_type>(
  4715. lock, *this, boost::forward<F>(func)
  4716. )));
  4717. } else if (underlying_cast<int>(policy_) & int(launch::deferred)) {
  4718. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state<shared_future<R>, future_type>(
  4719. lock, *this, boost::forward<F>(func)
  4720. )));
  4721. } else if (underlying_cast<int>(policy_) & int(launch::sync)) {
  4722. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_continuation_shared_state<shared_future<R>, future_type>(
  4723. lock, *this, boost::forward<F>(func)
  4724. )));
  4725. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4726. } else if (underlying_cast<int>(policy_) & int(launch::executor)) {
  4727. typedef executor Ex;
  4728. Ex& ex = *(this->future_->get_executor());
  4729. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state<Ex, shared_future<R>, future_type>(ex,
  4730. lock, *this, boost::forward<F>(func)
  4731. )));
  4732. #endif
  4733. } else {
  4734. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state<shared_future<R>, future_type>(
  4735. lock, *this, boost::forward<F>(func)
  4736. )));
  4737. }
  4738. } else {
  4739. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state<shared_future<R>, future_type>(
  4740. lock, *this, boost::forward<F>(func)
  4741. )));
  4742. }
  4743. }
  4744. #ifdef BOOST_THREAD_PROVIDES_EXECUTORS
  4745. ////////////////////////////////
  4746. // template<typename Ex, typename F>
  4747. // auto shared_future<R>::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4748. ////////////////////////////////
  4749. template <typename R>
  4750. template <typename Ex, typename F>
  4751. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future<R>)>::type>
  4752. shared_future<R>::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) const
  4753. {
  4754. typedef typename boost::result_of<F(shared_future<R>)>::type future_type;
  4755. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4756. boost::unique_lock<boost::mutex> lock(this->future_->mutex);
  4757. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state<Ex, shared_future<R>, future_type>(ex,
  4758. lock, *this, boost::forward<F>(func)
  4759. )));
  4760. }
  4761. #endif
  4762. ////////////////////////////////
  4763. // template<typename F>
  4764. // auto shared_future<R>::then(F&& func) -> BOOST_THREAD_FUTURE<decltype(func(*this))>;
  4765. ////////////////////////////////
  4766. template <typename R>
  4767. template <typename F>
  4768. inline BOOST_THREAD_FUTURE<typename boost::result_of<F(shared_future<R>)>::type>
  4769. shared_future<R>::then(BOOST_THREAD_FWD_REF(F) func) const {
  4770. #ifndef BOOST_THREAD_CONTINUATION_SYNC
  4771. return this->then(this->launch_policy(), boost::forward<F>(func));
  4772. #else
  4773. typedef typename boost::result_of<F(shared_future<R>)>::type future_type;
  4774. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4775. boost::unique_lock<boost::mutex> lock(this->future_->mutex);
  4776. launch policy = this->launch_policy(lock);
  4777. if (underlying_cast<int>(policy) & int(launch::deferred)) {
  4778. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state<shared_future<R>, future_type>(
  4779. lock, *this, boost::forward<F>(func)
  4780. )));
  4781. } else {
  4782. return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_continuation_shared_state<shared_future<R>, future_type>(
  4783. lock, *this, boost::forward<F>(func)
  4784. )));
  4785. }
  4786. #endif
  4787. }
  4788. namespace detail
  4789. {
  4790. template <typename T>
  4791. struct mfallbacker_to
  4792. {
  4793. T value_;
  4794. typedef T result_type;
  4795. mfallbacker_to(BOOST_THREAD_RV_REF(T) v)
  4796. : value_(boost::move(v))
  4797. {}
  4798. T operator()(BOOST_THREAD_FUTURE<T> fut) {
  4799. return fut.get_or(boost::move(value_));
  4800. }
  4801. };
  4802. template <typename T>
  4803. struct cfallbacker_to
  4804. {
  4805. T value_;
  4806. typedef T result_type;
  4807. cfallbacker_to(T const& v)
  4808. : value_(v)
  4809. {}
  4810. T operator()(BOOST_THREAD_FUTURE<T> fut) const {
  4811. return fut.get_or(value_);
  4812. }
  4813. };
  4814. }
  4815. ////////////////////////////////
  4816. // future<R> future<R>::fallback_to(R&& v);
  4817. ////////////////////////////////
  4818. template <typename R>
  4819. template <typename R2>
  4820. inline typename boost::disable_if< is_void<R2>, BOOST_THREAD_FUTURE<R> >::type
  4821. BOOST_THREAD_FUTURE<R>::fallback_to(BOOST_THREAD_RV_REF(R2) v) {
  4822. return then(detail::mfallbacker_to<R>(boost::move(v)));
  4823. }
  4824. template <typename R>
  4825. template <typename R2>
  4826. inline typename boost::disable_if< is_void<R2>, BOOST_THREAD_FUTURE<R> >::type
  4827. BOOST_THREAD_FUTURE<R>::fallback_to(R2 const& v) {
  4828. return then(detail::cfallbacker_to<R>(v));
  4829. }
  4830. #endif
  4831. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  4832. namespace detail
  4833. {
  4834. /////////////////////////
  4835. /// future_unwrap_shared_state
  4836. /////////////////////////
  4837. template<typename F, typename Rp>
  4838. struct future_unwrap_shared_state: shared_state<Rp>
  4839. {
  4840. F wrapped;
  4841. typename F::value_type unwrapped;
  4842. public:
  4843. explicit future_unwrap_shared_state(BOOST_THREAD_RV_REF(F) f)
  4844. : wrapped(boost::move(f)) {
  4845. }
  4846. void launch_continuation()
  4847. {
  4848. boost::unique_lock<boost::mutex> lk(this->mutex);
  4849. // assert(wrapped.is_ready());
  4850. if (! unwrapped.valid() )
  4851. {
  4852. if (wrapped.has_exception()) {
  4853. this->mark_exceptional_finish_internal(wrapped.get_exception_ptr(), lk);
  4854. } else {
  4855. unwrapped = wrapped.get();
  4856. if (unwrapped.valid())
  4857. {
  4858. lk.unlock();
  4859. boost::unique_lock<boost::mutex> lk2(unwrapped.future_->mutex);
  4860. unwrapped.future_->set_continuation_ptr(this->shared_from_this(), lk2);
  4861. } else {
  4862. this->mark_exceptional_finish_internal(boost::copy_exception(future_uninitialized()), lk);
  4863. }
  4864. }
  4865. } else {
  4866. // assert(unwrapped.is_ready());
  4867. if (unwrapped.has_exception()) {
  4868. this->mark_exceptional_finish_internal(unwrapped.get_exception_ptr(), lk);
  4869. } else {
  4870. this->mark_finished_with_result_internal(unwrapped.get(), lk);
  4871. }
  4872. }
  4873. }
  4874. };
  4875. template<typename F>
  4876. struct future_unwrap_shared_state<F,void>: shared_state<void>
  4877. {
  4878. F wrapped;
  4879. typename F::value_type unwrapped;
  4880. public:
  4881. explicit future_unwrap_shared_state(BOOST_THREAD_RV_REF(F) f)
  4882. : wrapped(boost::move(f)) {
  4883. }
  4884. void launch_continuation()
  4885. {
  4886. boost::unique_lock<boost::mutex> lk(this->mutex);
  4887. // assert(wrapped.is_ready());
  4888. if (! unwrapped.valid() )
  4889. {
  4890. if (wrapped.has_exception()) {
  4891. this->mark_exceptional_finish_internal(wrapped.get_exception_ptr(), lk);
  4892. } else {
  4893. unwrapped = wrapped.get();
  4894. if (unwrapped.valid())
  4895. {
  4896. lk.unlock();
  4897. boost::unique_lock<boost::mutex> lk2(unwrapped.future_->mutex);
  4898. unwrapped.future_->set_continuation_ptr(this->shared_from_this(), lk2);
  4899. } else {
  4900. this->mark_exceptional_finish_internal(boost::copy_exception(future_uninitialized()), lk);
  4901. }
  4902. }
  4903. } else {
  4904. // assert(unwrapped.is_ready());
  4905. if (unwrapped.has_exception()) {
  4906. this->mark_exceptional_finish_internal(unwrapped.get_exception_ptr(), lk);
  4907. } else {
  4908. this->mark_finished_with_result_internal(lk);
  4909. }
  4910. }
  4911. }
  4912. };
  4913. template <class F, class Rp>
  4914. BOOST_THREAD_FUTURE<Rp>
  4915. make_future_unwrap_shared_state(boost::unique_lock<boost::mutex> &lock, BOOST_THREAD_RV_REF(F) f) {
  4916. shared_ptr<future_unwrap_shared_state<F, Rp> >
  4917. h(new future_unwrap_shared_state<F, Rp>(boost::move(f)));
  4918. h->wrapped.future_->set_continuation_ptr(h, lock);
  4919. return BOOST_THREAD_FUTURE<Rp>(h);
  4920. }
  4921. }
  4922. template <typename R>
  4923. inline BOOST_THREAD_FUTURE<R>::BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R> >) other)
  4924. : base_type(other.unwrap()) {}
  4925. template <typename R2>
  4926. BOOST_THREAD_FUTURE<R2>
  4927. BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >::unwrap()
  4928. {
  4929. BOOST_THREAD_ASSERT_PRECONDITION(this->future_.get()!=0, future_uninitialized());
  4930. // keep state alive as we move ourself but hold the lock
  4931. shared_ptr<detail::shared_state_base> sentinel(this->future_);
  4932. boost::unique_lock<boost::mutex> lock(sentinel->mutex);
  4933. return boost::detail::make_future_unwrap_shared_state<BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R2> >, R2>(lock, boost::move(*this));
  4934. }
  4935. #endif
  4936. #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  4937. namespace detail
  4938. {
  4939. struct input_iterator_tag {};
  4940. struct vector_tag {};
  4941. struct values_tag {};
  4942. template <typename T>
  4943. struct alias_t { typedef T type; };
  4944. BOOST_CONSTEXPR_OR_CONST input_iterator_tag input_iterator_tag_value = {};
  4945. BOOST_CONSTEXPR_OR_CONST vector_tag vector_tag_value = {};
  4946. BOOST_CONSTEXPR_OR_CONST values_tag values_tag_value = {};
  4947. ////////////////////////////////
  4948. // detail::future_async_when_all_shared_state
  4949. ////////////////////////////////
  4950. template<typename F>
  4951. struct future_when_all_vector_shared_state: future_async_shared_state_base<csbl::vector<F> >
  4952. {
  4953. typedef csbl::vector<F> vector_type;
  4954. typedef typename F::value_type value_type;
  4955. vector_type vec_;
  4956. static void run(shared_ptr<boost::detail::shared_state_base> that_) {
  4957. future_when_all_vector_shared_state* that = static_cast<future_when_all_vector_shared_state*>(that_.get());
  4958. try {
  4959. boost::wait_for_all(that->vec_.begin(), that->vec_.end());
  4960. that->mark_finished_with_result(boost::move(that->vec_));
  4961. } catch(...) {
  4962. that->mark_exceptional_finish();
  4963. }
  4964. }
  4965. bool run_deferred() {
  4966. bool res = false;
  4967. for (typename csbl::vector<F>::iterator it = vec_.begin(); it != vec_.end(); ++it) {
  4968. if (! it->run_if_is_deferred())
  4969. {
  4970. res = true;
  4971. }
  4972. }
  4973. return res;
  4974. }
  4975. void init() {
  4976. if (! run_deferred())
  4977. {
  4978. future_when_all_vector_shared_state::run(this->shared_from_this());
  4979. return;
  4980. }
  4981. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  4982. this->thr_ = boost::thread(&future_when_all_vector_shared_state::run, this->shared_from_this());
  4983. #else
  4984. boost::thread(&future_when_all_vector_shared_state::run, this->shared_from_this()).detach();
  4985. #endif
  4986. }
  4987. public:
  4988. template< typename InputIterator>
  4989. future_when_all_vector_shared_state(input_iterator_tag, InputIterator first, InputIterator last)
  4990. : vec_(std::make_move_iterator(first), std::make_move_iterator(last))
  4991. {
  4992. }
  4993. future_when_all_vector_shared_state(vector_tag, BOOST_THREAD_RV_REF(csbl::vector<F>) v)
  4994. : vec_(boost::move(v))
  4995. {
  4996. }
  4997. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  4998. template< typename T0, typename ...T>
  4999. future_when_all_vector_shared_state(values_tag, BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) {
  5000. vec_.push_back(boost::forward<T0>(f));
  5001. typename alias_t<char[]>::type{
  5002. ( //first part of magic unpacker
  5003. vec_.push_back(boost::forward<T>(futures)),'0'
  5004. )..., '0'
  5005. }; //second part of magic unpacker
  5006. }
  5007. #endif
  5008. ~future_when_all_vector_shared_state() {}
  5009. };
  5010. ////////////////////////////////
  5011. // detail::future_async_when_any_shared_state
  5012. ////////////////////////////////
  5013. template<typename F>
  5014. struct future_when_any_vector_shared_state: future_async_shared_state_base<csbl::vector<F> >
  5015. {
  5016. typedef csbl::vector<F> vector_type;
  5017. typedef typename F::value_type value_type;
  5018. vector_type vec_;
  5019. static void run(shared_ptr<boost::detail::shared_state_base> that_)
  5020. {
  5021. future_when_any_vector_shared_state* that = static_cast<future_when_any_vector_shared_state*>(that_.get());
  5022. try {
  5023. boost::wait_for_any(that->vec_.begin(), that->vec_.end());
  5024. that->mark_finished_with_result(boost::move(that->vec_));
  5025. } catch(...) {
  5026. that->mark_exceptional_finish();
  5027. }
  5028. }
  5029. bool run_deferred() {
  5030. for (typename csbl::vector<F>::iterator it = vec_.begin(); it != vec_.end(); ++it) {
  5031. if (it->run_if_is_deferred_or_ready())
  5032. {
  5033. return true;
  5034. }
  5035. }
  5036. return false;
  5037. }
  5038. void init() {
  5039. if (run_deferred())
  5040. {
  5041. future_when_any_vector_shared_state::run(this->shared_from_this());
  5042. return;
  5043. }
  5044. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  5045. this->thr_ = boost::thread(&future_when_any_vector_shared_state::run, this->shared_from_this());
  5046. #else
  5047. boost::thread(&future_when_any_vector_shared_state::run, this->shared_from_this()).detach();
  5048. #endif
  5049. }
  5050. public:
  5051. template< typename InputIterator>
  5052. future_when_any_vector_shared_state(input_iterator_tag, InputIterator first, InputIterator last)
  5053. : vec_(std::make_move_iterator(first), std::make_move_iterator(last))
  5054. {
  5055. }
  5056. future_when_any_vector_shared_state(vector_tag, BOOST_THREAD_RV_REF(csbl::vector<F>) v)
  5057. : vec_(boost::move(v))
  5058. {
  5059. }
  5060. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  5061. template< typename T0, typename ...T>
  5062. future_when_any_vector_shared_state(values_tag,
  5063. BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures
  5064. ) {
  5065. vec_.push_back(boost::forward<T0>(f));
  5066. typename alias_t<char[]>::type{
  5067. ( //first part of magic unpacker
  5068. vec_.push_back(boost::forward<T>(futures))
  5069. ,'0'
  5070. )...,
  5071. '0'
  5072. }; //second part of magic unpacker
  5073. }
  5074. #endif
  5075. ~future_when_any_vector_shared_state() {}
  5076. };
  5077. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  5078. struct wait_for_all_fctr {
  5079. template <class ...T>
  5080. void operator()(T&&... v) {
  5081. boost::wait_for_all(boost::forward<T>(v)...);
  5082. }
  5083. };
  5084. struct wait_for_any_fctr {
  5085. template <class ...T>
  5086. void operator()(T&&... v) {
  5087. boost::wait_for_any(boost::forward<T>(v)...);
  5088. }
  5089. };
  5090. template <class Tuple, std::size_t i=csbl::tuple_size<Tuple>::value>
  5091. struct accumulate_run_if_is_deferred {
  5092. bool operator ()(Tuple& t)
  5093. {
  5094. return (! csbl::get<i-1>(t).run_if_is_deferred()) || accumulate_run_if_is_deferred<Tuple,i-1>()(t);
  5095. }
  5096. };
  5097. template <class Tuple>
  5098. struct accumulate_run_if_is_deferred<Tuple, 0> {
  5099. bool operator ()(Tuple& )
  5100. {
  5101. return false;
  5102. }
  5103. };
  5104. template< typename Tuple, typename T0, typename ...T>
  5105. struct future_when_all_tuple_shared_state: future_async_shared_state_base<Tuple>
  5106. {
  5107. Tuple tup_;
  5108. typedef typename make_tuple_indices<1+sizeof...(T)>::type Index;
  5109. static void run(shared_ptr<boost::detail::shared_state_base> that_) {
  5110. future_when_all_tuple_shared_state* that = static_cast<future_when_all_tuple_shared_state*>(that_.get());
  5111. try {
  5112. // TODO make use of apply(that->tup_, boost::detail::wait_for_all_fctor());
  5113. that->wait_for_all(Index());
  5114. that->mark_finished_with_result(boost::move(that->tup_));
  5115. } catch(...) {
  5116. that->mark_exceptional_finish();
  5117. }
  5118. }
  5119. template <size_t ...Indices>
  5120. void wait_for_all(tuple_indices<Indices...>) {
  5121. #if defined BOOST_THREAD_PROVIDES_INVOKE
  5122. return invoke<void>(wait_for_all_fctr(), csbl::get<Indices>(tup_)...);
  5123. #else
  5124. return wait_for_all_fctr()(csbl::get<Indices>(tup_)...);
  5125. #endif
  5126. }
  5127. bool run_deferred() {
  5128. return accumulate_run_if_is_deferred<Tuple>()(tup_);
  5129. }
  5130. void init() {
  5131. if (! run_deferred())
  5132. {
  5133. future_when_all_tuple_shared_state::run(this->shared_from_this());
  5134. return;
  5135. }
  5136. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  5137. this->thr_ = boost::thread(&future_when_all_tuple_shared_state::run, this->shared_from_this());
  5138. #else
  5139. boost::thread(&future_when_all_tuple_shared_state::run, this->shared_from_this()).detach();
  5140. #endif
  5141. }
  5142. public:
  5143. template< typename F, typename ...Fs>
  5144. future_when_all_tuple_shared_state(values_tag, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Fs) ... futures) :
  5145. tup_(boost::csbl::make_tuple(boost::forward<F>(f), boost::forward<Fs>(futures)...))
  5146. {
  5147. }
  5148. ~future_when_all_tuple_shared_state() {}
  5149. };
  5150. template <class Tuple, std::size_t i=csbl::tuple_size<Tuple>::value>
  5151. struct apply_any_run_if_is_deferred_or_ready {
  5152. bool operator ()(Tuple& t)
  5153. {
  5154. if (csbl::get<i-1>(t).run_if_is_deferred_or_ready()) return true;
  5155. return apply_any_run_if_is_deferred_or_ready<Tuple,i-1>()(t);
  5156. }
  5157. };
  5158. template <class Tuple>
  5159. struct apply_any_run_if_is_deferred_or_ready<Tuple, 0> {
  5160. bool operator ()(Tuple& )
  5161. {
  5162. return false;
  5163. }
  5164. };
  5165. template< typename Tuple, typename T0, typename ...T >
  5166. struct future_when_any_tuple_shared_state: future_async_shared_state_base<Tuple>
  5167. {
  5168. Tuple tup_;
  5169. typedef typename make_tuple_indices<1+sizeof...(T)>::type Index;
  5170. static void run(shared_ptr<boost::detail::shared_state_base> that_)
  5171. {
  5172. future_when_any_tuple_shared_state* that = static_cast<future_when_any_tuple_shared_state*>(that_.get());
  5173. try {
  5174. // TODO make use of apply(that->tup_, wait_for_any_fctr);
  5175. that->wait_for_any(Index());
  5176. that->mark_finished_with_result(boost::move(that->tup_));
  5177. } catch(...) {
  5178. that->mark_exceptional_finish();
  5179. }
  5180. }
  5181. template <size_t ...Indices>
  5182. void wait_for_any(tuple_indices<Indices...>) {
  5183. #if defined BOOST_THREAD_PROVIDES_INVOKE
  5184. return invoke<void>(wait_for_any_fctr(), csbl::get<Indices>(tup_)...);
  5185. #else
  5186. return wait_for_any_fctr()(csbl::get<Indices>(tup_)...);
  5187. #endif
  5188. }
  5189. bool run_deferred() {
  5190. return apply_any_run_if_is_deferred_or_ready<Tuple>()(tup_);
  5191. }
  5192. void init() {
  5193. if (run_deferred())
  5194. {
  5195. future_when_any_tuple_shared_state::run(this->shared_from_this());
  5196. return;
  5197. }
  5198. #ifdef BOOST_THREAD_FUTURE_BLOCKING
  5199. this->thr_ = boost::thread(&future_when_any_tuple_shared_state::run, this->shared_from_this());
  5200. #else
  5201. boost::thread(&future_when_any_tuple_shared_state::run, this->shared_from_this()).detach();
  5202. #endif
  5203. }
  5204. public:
  5205. template< typename F, typename ...Fs>
  5206. future_when_any_tuple_shared_state(values_tag,
  5207. BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Fs) ... futures
  5208. ) :
  5209. tup_(boost::csbl::make_tuple(boost::forward<F>(f), boost::forward<Fs>(futures)...))
  5210. {
  5211. }
  5212. ~future_when_any_tuple_shared_state() {}
  5213. };
  5214. #endif
  5215. }
  5216. template< typename InputIterator>
  5217. typename boost::disable_if<is_future_type<InputIterator>,
  5218. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  5219. >::type
  5220. when_all(InputIterator first, InputIterator last) {
  5221. typedef typename InputIterator::value_type value_type;
  5222. typedef csbl::vector<value_type> container_type;
  5223. typedef detail::future_when_all_vector_shared_state<value_type> factory_type;
  5224. if (first==last) return make_ready_future(container_type());
  5225. shared_ptr<factory_type >
  5226. h(new factory_type(detail::input_iterator_tag_value, first,last));
  5227. h->init();
  5228. return BOOST_THREAD_FUTURE<container_type>(h);
  5229. }
  5230. inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_all() {
  5231. return make_ready_future(csbl::tuple<>());
  5232. }
  5233. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  5234. template< typename T0, typename ...T>
  5235. BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  5236. when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) {
  5237. typedef csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> container_type;
  5238. typedef detail::future_when_all_tuple_shared_state<container_type, typename decay<T0>::type, typename decay<T>::type...> factory_type;
  5239. shared_ptr<factory_type>
  5240. h(new factory_type(detail::values_tag_value, boost::forward<T0>(f), boost::forward<T>(futures)...));
  5241. h->init();
  5242. return BOOST_THREAD_FUTURE<container_type>(h);
  5243. }
  5244. #endif
  5245. template< typename InputIterator>
  5246. typename boost::disable_if<is_future_type<InputIterator>,
  5247. BOOST_THREAD_FUTURE<csbl::vector<typename InputIterator::value_type> >
  5248. >::type
  5249. when_any(InputIterator first, InputIterator last) {
  5250. typedef typename InputIterator::value_type value_type;
  5251. typedef csbl::vector<value_type> container_type;
  5252. typedef detail::future_when_any_vector_shared_state<value_type> factory_type;
  5253. if (first==last) return make_ready_future(container_type());
  5254. shared_ptr<factory_type >
  5255. h(new factory_type(detail::input_iterator_tag_value, first,last));
  5256. h->init();
  5257. return BOOST_THREAD_FUTURE<container_type>(h);
  5258. }
  5259. inline BOOST_THREAD_FUTURE<csbl::tuple<> > when_any() {
  5260. return make_ready_future(csbl::tuple<>());
  5261. }
  5262. #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  5263. template< typename T0, typename ...T>
  5264. BOOST_THREAD_FUTURE<csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> >
  5265. when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) {
  5266. typedef csbl::tuple<typename decay<T0>::type, typename decay<T>::type...> container_type;
  5267. typedef detail::future_when_any_tuple_shared_state<container_type, typename decay<T0>::type, typename decay<T>::type...> factory_type;
  5268. shared_ptr<factory_type>
  5269. h(new factory_type(detail::values_tag_value, boost::forward<T0>(f), boost::forward<T>(futures)...));
  5270. h->init();
  5271. return BOOST_THREAD_FUTURE<container_type>(h);
  5272. }
  5273. #endif
  5274. #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  5275. }
  5276. #endif // BOOST_NO_EXCEPTIONS
  5277. #endif // header