future.hpp 199 KB

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