future.hpp 190 KB

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