context.xml 273 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  3. <library id="context" name="Context" dirname="context" last-revision="$Date: 2019/10/02 06:15:27 $"
  4. xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <libraryinfo>
  6. <authorgroup>
  7. <author>
  8. <firstname>Oliver</firstname> <surname>Kowalke</surname>
  9. </author>
  10. </authorgroup>
  11. <copyright>
  12. <year>2014</year> <holder>Oliver Kowalke</holder>
  13. </copyright>
  14. <legalnotice id="context.legal">
  15. <para>
  16. Distributed under the Boost Software License, Version 1.0. (See accompanying
  17. file LICENSE_1_0.txt or copy at <ulink url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
  18. </para>
  19. </legalnotice>
  20. <librarypurpose>
  21. C++ Library for swiching different user ctx
  22. </librarypurpose>
  23. <librarycategory name="category:text"></librarycategory>
  24. </libraryinfo>
  25. <title>Context</title>
  26. <section id="context.overview">
  27. <title><link linkend="context.overview">Overview</link></title>
  28. <para>
  29. <emphasis role="bold">Boost.Context</emphasis> is a foundational library that
  30. provides a sort of cooperative multitasking on a single thread. By providing
  31. an abstraction of the current execution state in the current thread, including
  32. the stack (with local variables) and stack pointer, all registers and CPU flags,
  33. and the instruction pointer, a execution context represents a specific point
  34. in the application's execution path. This is useful for building higher-level
  35. abstractions, like <emphasis>coroutines</emphasis>, <emphasis>cooperative threads
  36. (userland threads)</emphasis> or an equivalent to <ulink url="http://msdn.microsoft.com/en-us/library/9k7k7cf0%28v=vs.80%29.aspx">C#
  37. keyword <emphasis>yield</emphasis></ulink> in C++.
  38. </para>
  39. <para>
  40. <link linkend="cc"><emphasis>callcc()</emphasis></link>/<link linkend="cc"><emphasis>continuation</emphasis></link>
  41. provides the means to suspend the current execution path and to transfer execution
  42. control, thereby permitting another context to run on the current thread. This
  43. state full transfer mechanism enables a context to suspend execution from within
  44. nested functions and, later, to resume from where it was suspended. While the
  45. execution path represented by a <link linkend="cc"><emphasis>continuation</emphasis></link>
  46. only runs on a single thread, it can be migrated to another thread at any given
  47. time.
  48. </para>
  49. <para>
  50. A <ulink url="http://en.wikipedia.org/wiki/Context_switch">context switch</ulink>
  51. between threads requires system calls (involving the OS kernel), which can
  52. cost more than thousand CPU cycles on x86 CPUs. By contrast, transferring control
  53. vias <link linkend="cc"><emphasis>callcc()</emphasis></link>/<link linkend="cc"><emphasis>continuation</emphasis></link>
  54. requires only few CPU cycles because it does not involve system calls as it
  55. is done within a single thread.
  56. </para>
  57. <para>
  58. All functions and classes are contained in the namespace <emphasis>boost::context</emphasis>.
  59. </para>
  60. <note>
  61. <para>
  62. This library requires C++11!
  63. </para>
  64. </note>
  65. <important>
  66. <para>
  67. Windows using fcontext_t: turn off global program optimization (/GL) and
  68. change /EHsc (compiler assumes that functions declared as extern &quot;C&quot;
  69. never throw a C++ exception) to /EHs (tells compiler assumes that functions
  70. declared as extern &quot;C&quot; may throw an exception).
  71. </para>
  72. </important>
  73. </section>
  74. <section id="context.requirements">
  75. <title><link linkend="context.requirements">Requirements</link></title>
  76. <para>
  77. If <emphasis role="bold">Boost.Context</emphasis> uses fcontext_t (the default)
  78. as its implementation, it must be built for the particular compiler(s) and
  79. CPU architecture(s) being targeted. Using <link linkend="implementation"><emphasis>fcontext_t</emphasis></link>,
  80. <emphasis role="bold">Boost.Context</emphasis> includes assembly code and,
  81. therefore, requires GNU as and GNU preprocessor for supported POSIX systems,
  82. MASM for Windows/x86 systems and ARMasm for Windows/arm systems.
  83. </para>
  84. <note>
  85. <para>
  86. MASM64 (ml64.exe) is a part of Microsoft's Windows Driver Kit.
  87. </para>
  88. </note>
  89. <important>
  90. <para>
  91. Please note that <code><phrase role="identifier">address</phrase><phrase
  92. role="special">-</phrase><phrase role="identifier">model</phrase><phrase
  93. role="special">=</phrase><phrase role="number">64</phrase></code> must be
  94. given to bjam command line on 64bit Windows for 64bit build; otherwise 32bit
  95. code will be generated.
  96. </para>
  97. </important>
  98. <important>
  99. <para>
  100. For cross-compiling the lib you must specify certain additional properties
  101. at bjam command line: <code><phrase role="identifier">target</phrase><phrase
  102. role="special">-</phrase><phrase role="identifier">os</phrase></code>, <code><phrase
  103. role="identifier">abi</phrase></code>, <code><phrase role="identifier">binary</phrase><phrase
  104. role="special">-</phrase><phrase role="identifier">format</phrase></code>,
  105. <code><phrase role="identifier">architecture</phrase></code> and <code><phrase
  106. role="identifier">address</phrase><phrase role="special">-</phrase><phrase
  107. role="identifier">model</phrase></code>.
  108. </para>
  109. </important>
  110. <important>
  111. <para>
  112. Windows using fcontext_t: for safe SEH the property 'asmflags=\safeseh' must
  113. be specified at bjam command line.
  114. </para>
  115. </important>
  116. <important>
  117. <para>
  118. Windows using fcontext_t: turn off global program optimization (/GL) and
  119. change /EHsc (compiler assumes that functions declared as extern &quot;C&quot;
  120. never throw a C++ exception) to /EHs (tells compiler assumes that functions
  121. declared as extern &quot;C&quot; may throw an exception).
  122. </para>
  123. </important>
  124. <para>
  125. Because this library uses C++11 extensively, it requires a compatible compiler.
  126. Known minimum working versions are as follows: Microsoft Visual Studio 2015
  127. (msvc-14.0), GCC 4.8 (with -std=c++11), Clang 3.4 (with -std=c++11). Other
  128. compilers may work, if they support the following language features: auto declarations,
  129. constexpr, defaulted functions, final, hdr thread, hdr tuple, lambdas, noexcept,
  130. nullptr, rvalue references, template aliases. thread local, variadic templates.
  131. </para>
  132. </section>
  133. <section id="context.ff">
  134. <title><anchor id="ff"/><link linkend="context.ff">Context switching with fibers</link></title>
  135. <note>
  136. <para>
  137. <emphasis>fiber</emphasis> is the reference implementation of C++ proposal
  138. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0876r0.pdf">P0876R0:
  139. fibers without scheduler</ulink>.
  140. </para>
  141. </note>
  142. <para>
  143. A <emphasis>fiber</emphasis> represents the state of the control flow of a
  144. program at a given point in time. Fibers can be suspended and resumed later
  145. in order to change the control flow of a program.
  146. </para>
  147. <para>
  148. Modern micro-processors are registers machines; the content of processor registers
  149. represent a fiber of the executed program at a given point in time. Operating
  150. systems simulate parallel execution of programs on a single processor by switching
  151. between programs (context switch) by preserving and restoring the fiber, e.g.
  152. the content of all registers.
  153. </para>
  154. <bridgehead renderas="sect3" id="context.ff.h0">
  155. <phrase id="context.ff._link_linkend__ff___emphasis_fiber__emphasis___link_"/><link
  156. linkend="context.ff._link_linkend__ff___emphasis_fiber__emphasis___link_"><link
  157. linkend="ff"><emphasis>fiber</emphasis></link></link>
  158. </bridgehead>
  159. <para>
  160. <link linkend="ff"><emphasis>fiber</emphasis></link> captures the current fiber
  161. (the rest of the computation; code after <link linkend="ff"><emphasis>fiber</emphasis></link>)
  162. and triggers a context switch. The context switch is achieved by preserving
  163. certain registers (including instruction and stack pointer), defined by the
  164. calling convention of the ABI, of the current fiber and restoring those registers
  165. of the resumed fiber. The control flow of the resumed fiber continues. The
  166. current fiber is suspended and passed as argument to the resumed fiber.
  167. </para>
  168. <para>
  169. <link linkend="ff"><emphasis>fiber</emphasis></link> expects a <emphasis>context-function</emphasis>
  170. with signature <code><phrase role="char">'fiber(fiber &amp;&amp; f)'</phrase></code>.
  171. The parameter <code><phrase role="identifier">f</phrase></code> represents
  172. the current fiber from which this fiber was resumed (e.g. that has called
  173. <link linkend="ff"><emphasis>fiber</emphasis></link>).
  174. </para>
  175. <para>
  176. On return the <emphasis>context-function</emphasis> of the current fiber has
  177. to specify an <link linkend="ff"><emphasis>fiber</emphasis></link> to which
  178. the execution control is transferred after termination of the current fiber.
  179. </para>
  180. <para>
  181. If an instance with valid state goes out of scope and the <emphasis>context-function</emphasis>
  182. has not yet returned, the stack is traversed in order to access the control
  183. structure (address stored at the first stack frame) and fiber's stack is deallocated
  184. via the <emphasis>StackAllocator</emphasis>.
  185. </para>
  186. <note>
  187. <para>
  188. <link linkend="segmented"><emphasis>Segmented stacks</emphasis></link> are
  189. supported by <link linkend="ff"><emphasis>fiber</emphasis></link> using
  190. <link linkend="implementation"><emphasis>ucontext_t</emphasis></link>.
  191. </para>
  192. </note>
  193. <para>
  194. <link linkend="ff"><emphasis>fiber</emphasis></link> represents a <emphasis>fiber</emphasis>;
  195. it contains the content of preserved registers and manages the associated stack
  196. (allocation/deallocation). <link linkend="ff"><emphasis>fiber</emphasis></link>
  197. is a one-shot fiber - it can be used only once, after calling <emphasis>continuation::resume()</emphasis>
  198. or <emphasis>continuation::resume_with()</emphasis> it is invalidated.
  199. </para>
  200. <para>
  201. <link linkend="ff"><emphasis>fiber</emphasis></link> is only move-constructible
  202. and move-assignable.
  203. </para>
  204. <para>
  205. As a first-class object <link linkend="ff"><emphasis>fiber</emphasis></link>
  206. can be applied to and returned from a function, assigned to a variable or stored
  207. in a container.
  208. </para>
  209. <para>
  210. A fiber is continued by calling <code><phrase role="identifier">resume</phrase><phrase
  211. role="special">()</phrase></code>/<code><phrase role="identifier">resume_with</phrase><phrase
  212. role="special">()</phrase></code>.
  213. </para>
  214. <bridgehead renderas="sect3" id="context.ff.h1">
  215. <phrase id="context.ff.usage"/><link linkend="context.ff.usage">Usage</link>
  216. </bridgehead>
  217. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  218. <phrase role="keyword">int</phrase> <phrase role="identifier">a</phrase><phrase role="special">;</phrase>
  219. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">source</phrase><phrase role="special">{[&amp;</phrase><phrase role="identifier">a</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">sink</phrase><phrase role="special">){</phrase>
  220. <phrase role="identifier">a</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase>
  221. <phrase role="keyword">int</phrase> <phrase role="identifier">b</phrase><phrase role="special">=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  222. <phrase role="keyword">for</phrase><phrase role="special">(;;){</phrase>
  223. <phrase role="identifier">sink</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  224. <phrase role="keyword">int</phrase> <phrase role="identifier">next</phrase><phrase role="special">=</phrase><phrase role="identifier">a</phrase><phrase role="special">+</phrase><phrase role="identifier">b</phrase><phrase role="special">;</phrase>
  225. <phrase role="identifier">a</phrase><phrase role="special">=</phrase><phrase role="identifier">b</phrase><phrase role="special">;</phrase>
  226. <phrase role="identifier">b</phrase><phrase role="special">=</phrase><phrase role="identifier">next</phrase><phrase role="special">;</phrase>
  227. <phrase role="special">}</phrase>
  228. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">);</phrase>
  229. <phrase role="special">}};</phrase>
  230. <phrase role="keyword">for</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">j</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase><phrase role="identifier">j</phrase><phrase role="special">&lt;</phrase><phrase role="number">10</phrase><phrase role="special">;++</phrase><phrase role="identifier">j</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  231. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">source</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  232. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">a</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase>
  233. <phrase role="special">}</phrase>
  234. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  235. <phrase role="number">0</phrase> <phrase role="number">1</phrase> <phrase role="number">1</phrase> <phrase role="number">2</phrase> <phrase role="number">3</phrase> <phrase role="number">5</phrase> <phrase role="number">8</phrase> <phrase role="number">13</phrase> <phrase role="number">21</phrase> <phrase role="number">34</phrase>
  236. </programlisting>
  237. <para>
  238. This simple example demonstrates the basic usage of <link linkend="ff"><emphasis>fiber</emphasis></link>
  239. as a <emphasis>generator</emphasis>. The fiber <code><phrase role="identifier">sink</phrase></code>
  240. represents the <emphasis>main</emphasis>-fiber (function <code><phrase role="identifier">main</phrase><phrase
  241. role="special">()</phrase></code>). <code><phrase role="identifier">sink</phrase></code>
  242. is captured (current-fiber) by invoking <link linkend="ff"><emphasis>fiber</emphasis></link>
  243. and passed as parameter to the lambda.
  244. </para>
  245. <para>
  246. Because the state is invalidated (one-shot fiber) by each call of <emphasis>continuation::resume()</emphasis>,
  247. the new state of the <link linkend="ff"><emphasis>fiber</emphasis></link>,
  248. returned by <emphasis>continuation::resume()</emphasis>, needs to be assigned
  249. to <code><phrase role="identifier">sink</phrase></code> after each call. In
  250. order to express the invalidation of the resumed fiber, the member functions
  251. <code><phrase role="identifier">resume</phrase><phrase role="special">()</phrase></code>
  252. and <code><phrase role="identifier">resume_with</phrase><phrase role="special">()</phrase></code>
  253. are rvalue-ref qualified. Both functions bind only to rvalues. Thus an lvalue
  254. fiber must be casted to an rvalue via <code><phrase role="identifier">std</phrase><phrase
  255. role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">()</phrase></code>.
  256. </para>
  257. <para>
  258. The lambda that calculates the Fibonacci numbers is executed inside the fiber
  259. represented by <code><phrase role="identifier">source</phrase></code>. Calculated
  260. Fibonacci numbers are transferred between the two fibers via variable <code><phrase
  261. role="identifier">a</phrase></code> (lambda capture reference).
  262. </para>
  263. <para>
  264. The locale variables <code><phrase role="identifier">b</phrase></code> and
  265. <code> <phrase role="identifier">next</phrase></code> remain their values during
  266. each context switch. This is possible due <code><phrase role="identifier">source</phrase></code>
  267. has its own stack and the stack is exchanged by each context switch.
  268. </para>
  269. <bridgehead renderas="sect3" id="context.ff.h2">
  270. <phrase id="context.ff.parameter_passing"/><link linkend="context.ff.parameter_passing">Parameter
  271. passing</link>
  272. </bridgehead>
  273. <para>
  274. Data can be transferred between two fibers via global pointers, calling wrappers
  275. (like <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase
  276. role="identifier">bind</phrase></code>) or lambda captures.
  277. </para>
  278. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  279. <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase><phrase role="special">=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  280. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f1</phrase><phrase role="special">{[&amp;</phrase><phrase role="identifier">i</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f2</phrase><phrase role="special">){</phrase>
  281. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;inside f1,i==%d\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
  282. <phrase role="identifier">i</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  283. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f2</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  284. <phrase role="special">}};</phrase>
  285. <phrase role="identifier">f1</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f1</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  286. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;i==%d\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
  287. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  288. <phrase role="identifier">inside</phrase> <phrase role="identifier">c1</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">==</phrase><phrase role="number">1</phrase>
  289. <phrase role="identifier">i</phrase><phrase role="special">==</phrase><phrase role="number">2</phrase>
  290. </programlisting>
  291. <para>
  292. <code><phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase
  293. role="identifier">resume</phrase><phrase role="special">()</phrase></code>
  294. enters the lambda in fiber represented by <code><phrase role="identifier">f1</phrase></code>
  295. with lambda capture reference <code><phrase role="identifier">i</phrase><phrase
  296. role="special">=</phrase><phrase role="number">1</phrase></code>. The expression
  297. <code><phrase role="identifier">f2</phrase><phrase role="special">.</phrase><phrase
  298. role="identifier">resume</phrase><phrase role="special">()</phrase></code>
  299. resumes the fiber <code><phrase role="identifier">f2</phrase></code>. On return
  300. of <code><phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase
  301. role="identifier">resume</phrase><phrase role="special">()</phrase></code>,
  302. the variable <code><phrase role="identifier">i</phrase></code> has the value
  303. of <code><phrase role="identifier">i</phrase><phrase role="special">+</phrase><phrase
  304. role="number">1</phrase></code>.
  305. </para>
  306. <bridgehead renderas="sect3" id="context.ff.h3">
  307. <phrase id="context.ff.exception_handling"/><link linkend="context.ff.exception_handling">Exception
  308. handling</link>
  309. </bridgehead>
  310. <para>
  311. If the function executed inside a <emphasis>context-function</emphasis> emits
  312. ans exception, the application is terminated by calling <code><phrase role="identifier">std</phrase><phrase
  313. role="special">::</phrase><phrase role="identifier">terminate</phrase><phrase
  314. role="special">()</phrase></code>. <code><phrase role="identifier">std</phrase><phrase
  315. role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>
  316. can be used to transfer exceptions between different fibers.
  317. </para>
  318. <important>
  319. <para>
  320. Do not jump from inside a catch block and then re-throw the exception in
  321. another fiber.
  322. </para>
  323. </important>
  324. <anchor id="ff_ontop"/>
  325. <bridgehead renderas="sect3" id="context.ff.h4">
  326. <phrase id="context.ff.executing_function_on_top_of_a_fiber"/><link linkend="context.ff.executing_function_on_top_of_a_fiber">Executing
  327. function on top of a fiber</link>
  328. </bridgehead>
  329. <para>
  330. Sometimes it is useful to execute a new function on top of a resumed fiber.
  331. For this purpose <emphasis>continuation::resume_with()</emphasis> has to be
  332. used. The function passed as argument must accept a rvalue reference to <link
  333. linkend="ff"><emphasis>fiber</emphasis></link> and return <code><phrase role="keyword">void</phrase></code>.
  334. </para>
  335. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  336. <phrase role="keyword">int</phrase> <phrase role="identifier">data</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase>
  337. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f1</phrase><phrase role="special">{[&amp;</phrase><phrase role="identifier">data</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f2</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  338. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered first time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  339. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  340. <phrase role="identifier">f2</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f2</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  341. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered second time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  342. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  343. <phrase role="identifier">f2</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f2</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  344. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered third time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  345. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f2</phrase><phrase role="special">);</phrase>
  346. <phrase role="special">}};</phrase>
  347. <phrase role="identifier">f1</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f1</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  348. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned first time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  349. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  350. <phrase role="identifier">f1</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f1</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  351. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned second time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  352. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  353. <phrase role="identifier">f1</phrase><phrase role="special">=</phrase><phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase role="identifier">resume_with</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">data</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f2</phrase><phrase role="special">){</phrase>
  354. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f2: entered: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  355. <phrase role="identifier">data</phrase><phrase role="special">=-</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  356. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f2</phrase><phrase role="special">);</phrase>
  357. <phrase role="special">});</phrase>
  358. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned third time&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  359. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  360. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">first</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">0</phrase>
  361. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">first</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  362. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">second</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">2</phrase>
  363. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">second</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">3</phrase>
  364. <phrase role="identifier">f2</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase><phrase role="special">:</phrase> <phrase role="number">4</phrase>
  365. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">third</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="special">-</phrase><phrase role="number">1</phrase>
  366. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">third</phrase> <phrase role="identifier">time</phrase>
  367. </programlisting>
  368. <para>
  369. The expression <code><phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase
  370. role="identifier">resume_with</phrase><phrase role="special">(...)</phrase></code>
  371. executes a lambda on top of fiber <code><phrase role="identifier">f1</phrase></code>,
  372. e.g. an additional stack frame is allocated on top of the stack. This lambda
  373. assigns <code><phrase role="special">-</phrase><phrase role="number">1</phrase></code>
  374. to <code><phrase role="identifier">data</phrase></code> and returns to the
  375. second invocation of <code><phrase role="identifier">f1</phrase><phrase role="special">.</phrase><phrase
  376. role="identifier">resume</phrase><phrase role="special">()</phrase></code>.
  377. </para>
  378. <para>
  379. Another option is to execute a function on top of the fiber that throws an
  380. exception.
  381. </para>
  382. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  383. <phrase role="keyword">struct</phrase> <phrase role="identifier">my_exception</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase> <phrase role="special">{</phrase>
  384. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">;</phrase>
  385. <phrase role="identifier">my_exception</phrase><phrase role="special">(</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f_</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">what</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  386. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">{</phrase> <phrase role="identifier">what</phrase> <phrase role="special">},</phrase>
  387. <phrase role="identifier">f</phrase><phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f_</phrase><phrase role="special">)</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase>
  388. <phrase role="special">}</phrase>
  389. <phrase role="special">};</phrase>
  390. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">{[](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="special">-&gt;</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">{</phrase>
  391. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;entered&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  392. <phrase role="keyword">try</phrase> <phrase role="special">{</phrase>
  393. <phrase role="identifier">f</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  394. <phrase role="special">}</phrase> <phrase role="keyword">catch</phrase> <phrase role="special">(</phrase><phrase role="identifier">my_exception</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">ex</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  395. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;my_exception: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">ex</phrase><phrase role="special">.</phrase><phrase role="identifier">what</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  396. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">ex</phrase><phrase role="special">.</phrase><phrase role="identifier">f</phrase><phrase role="special">);</phrase>
  397. <phrase role="special">}</phrase>
  398. <phrase role="keyword">return</phrase> <phrase role="special">{};</phrase>
  399. <phrase role="special">});</phrase>
  400. <phrase role="identifier">f</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  401. <phrase role="identifier">f</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">).</phrase><phrase role="identifier">resume_with</phrase><phrase role="special">([](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="special">-&gt;</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">{</phrase>
  402. <phrase role="keyword">throw</phrase> <phrase role="identifier">my_exception</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">),</phrase><phrase role="string">&quot;abc&quot;</phrase><phrase role="special">);</phrase>
  403. <phrase role="keyword">return</phrase> <phrase role="special">{};</phrase>
  404. <phrase role="special">});</phrase>
  405. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  406. <phrase role="identifier">entered</phrase>
  407. <phrase role="identifier">my_exception</phrase><phrase role="special">:</phrase> <phrase role="identifier">abc</phrase>
  408. </programlisting>
  409. <para>
  410. In this exception <code><phrase role="identifier">my_exception</phrase></code>
  411. is throw from a function invoked on-top of fiber <code><phrase role="identifier">f</phrase></code>
  412. and catched inside the <code><phrase role="keyword">for</phrase></code>-loop.
  413. </para>
  414. <bridgehead renderas="sect3" id="context.ff.h5">
  415. <phrase id="context.ff.stack_unwinding"/><link linkend="context.ff.stack_unwinding">Stack
  416. unwinding</link>
  417. </bridgehead>
  418. <para>
  419. On construction of <link linkend="ff"><emphasis>fiber</emphasis></link> a stack
  420. is allocated. If the <emphasis>context-function</emphasis> returns the stack
  421. will be destructed. If the <emphasis>context-function</emphasis> has not yet
  422. returned and the destructor of an valid <link linkend="ff"><emphasis>fiber</emphasis></link>
  423. instance (e.g. <emphasis>fiber::operator bool()</emphasis> returns <code><phrase
  424. role="keyword">true</phrase></code>) is called, the stack will be destructed
  425. too.
  426. </para>
  427. <important>
  428. <para>
  429. Code executed by <emphasis>context-function</emphasis> must not prevent the
  430. propagation ofs the <emphasis>detail::forced_unwind</emphasis> exception.
  431. Absorbing that exception will cause stack unwinding to fail. Thus, any code
  432. that catches all exceptions must re-throw any pending <emphasis>detail::forced_unwind</emphasis>
  433. exception.
  434. </para>
  435. </important>
  436. <anchor id="ff_prealloc"/>
  437. <bridgehead renderas="sect3" id="context.ff.h6">
  438. <phrase id="context.ff.allocating_control_structures_on_top_of_stack"/><link
  439. linkend="context.ff.allocating_control_structures_on_top_of_stack">Allocating
  440. control structures on top of stack</link>
  441. </bridgehead>
  442. <para>
  443. Allocating control structures on top of the stack requires to allocated the
  444. <emphasis>stack_context</emphasis> and create the control structure with placement
  445. new before <link linkend="ff"><emphasis>fiber</emphasis></link> is created.
  446. </para>
  447. <note>
  448. <para>
  449. The user is responsible for destructing the control structure at the top
  450. of the stack.
  451. </para>
  452. </note>
  453. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  454. <phrase role="comment">// stack-allocator used for (de-)allocating stack</phrase>
  455. <phrase role="identifier">fixedsize_stack</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">(</phrase><phrase role="number">4048</phrase><phrase role="special">);</phrase>
  456. <phrase role="comment">// allocate stack space</phrase>
  457. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">(</phrase><phrase role="identifier">salloc</phrase><phrase role="special">.</phrase><phrase role="identifier">allocate</phrase><phrase role="special">());</phrase>
  458. <phrase role="comment">// reserve space for control structure on top of the stack</phrase>
  459. <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">=</phrase><phrase role="keyword">static_cast</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">char</phrase><phrase role="special">*&gt;(</phrase><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase><phrase role="special">)-</phrase><phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">);</phrase>
  460. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">=</phrase><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">-</phrase><phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">);</phrase>
  461. <phrase role="comment">// placement new creates control structure on reserved space</phrase>
  462. <phrase role="identifier">my_control_structure</phrase> <phrase role="special">*</phrase> <phrase role="identifier">cs</phrase><phrase role="special">=</phrase><phrase role="keyword">new</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">)</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">sctx</phrase><phrase role="special">,</phrase><phrase role="identifier">salloc</phrase><phrase role="special">);</phrase>
  463. <phrase role="special">...</phrase>
  464. <phrase role="comment">// destructing the control structure</phrase>
  465. <phrase role="identifier">cs</phrase><phrase role="special">-&gt;~</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">();</phrase>
  466. <phrase role="special">...</phrase>
  467. <phrase role="keyword">struct</phrase> <phrase role="identifier">my_control_structure</phrase> <phrase role="special">{</phrase>
  468. <phrase role="comment">// captured fiber</phrase>
  469. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">f</phrase><phrase role="special">;</phrase>
  470. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&gt;</phrase>
  471. <phrase role="identifier">my_control_structure</phrase><phrase role="special">(</phrase><phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">stack_context</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">,</phrase><phrase role="identifier">StackAllocator</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  472. <phrase role="comment">// create captured fiber</phrase>
  473. <phrase role="identifier">f</phrase><phrase role="special">{</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg</phrase><phrase role="special">,</phrase><phrase role="identifier">preallocated</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">sctx</phrase><phrase role="special">),</phrase><phrase role="identifier">salloc</phrase><phrase role="special">,</phrase><phrase role="identifier">entry_func</phrase><phrase role="special">}</phrase> <phrase role="special">{</phrase>
  474. <phrase role="special">}</phrase>
  475. <phrase role="special">...</phrase>
  476. <phrase role="special">};</phrase>
  477. </programlisting>
  478. <bridgehead renderas="sect3" id="context.ff.h7">
  479. <phrase id="context.ff.inverting_the_control_flow"/><link linkend="context.ff.inverting_the_control_flow">Inverting
  480. the control flow</link>
  481. </bridgehead>
  482. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  483. <phrase role="comment">/*
  484. * grammar:
  485. * P ---&gt; E '\0'
  486. * E ---&gt; T {('+'|'-') T}
  487. * T ---&gt; S {('*'|'/') S}
  488. * S ---&gt; digit | '(' E ')'
  489. */</phrase>
  490. <phrase role="keyword">class</phrase> <phrase role="identifier">Parser</phrase><phrase role="special">{</phrase>
  491. <phrase role="keyword">char</phrase> <phrase role="identifier">next</phrase><phrase role="special">;</phrase>
  492. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istream</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">is</phrase><phrase role="special">;</phrase>
  493. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase><phrase role="special">)&gt;</phrase> <phrase role="identifier">cb</phrase><phrase role="special">;</phrase>
  494. <phrase role="keyword">char</phrase> <phrase role="identifier">pull</phrase><phrase role="special">(){</phrase>
  495. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">char_traits</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">char</phrase><phrase role="special">&gt;::</phrase><phrase role="identifier">to_char_type</phrase><phrase role="special">(</phrase><phrase role="identifier">is</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">());</phrase>
  496. <phrase role="special">}</phrase>
  497. <phrase role="keyword">void</phrase> <phrase role="identifier">scan</phrase><phrase role="special">(){</phrase>
  498. <phrase role="keyword">do</phrase><phrase role="special">{</phrase>
  499. <phrase role="identifier">next</phrase><phrase role="special">=</phrase><phrase role="identifier">pull</phrase><phrase role="special">();</phrase>
  500. <phrase role="special">}</phrase>
  501. <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">isspace</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">));</phrase>
  502. <phrase role="special">}</phrase>
  503. <phrase role="keyword">public</phrase><phrase role="special">:</phrase>
  504. <phrase role="identifier">Parser</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istream</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">is_</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase><phrase role="special">)&gt;</phrase> <phrase role="identifier">cb_</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  505. <phrase role="identifier">next</phrase><phrase role="special">(),</phrase> <phrase role="identifier">is</phrase><phrase role="special">(</phrase><phrase role="identifier">is_</phrase><phrase role="special">),</phrase> <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">cb_</phrase><phrase role="special">)</phrase>
  506. <phrase role="special">{}</phrase>
  507. <phrase role="keyword">void</phrase> <phrase role="identifier">run</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
  508. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  509. <phrase role="identifier">E</phrase><phrase role="special">();</phrase>
  510. <phrase role="special">}</phrase>
  511. <phrase role="keyword">private</phrase><phrase role="special">:</phrase>
  512. <phrase role="keyword">void</phrase> <phrase role="identifier">E</phrase><phrase role="special">(){</phrase>
  513. <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
  514. <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'+'</phrase><phrase role="special">||</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'-'</phrase><phrase role="special">){</phrase>
  515. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  516. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  517. <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
  518. <phrase role="special">}</phrase>
  519. <phrase role="special">}</phrase>
  520. <phrase role="keyword">void</phrase> <phrase role="identifier">T</phrase><phrase role="special">(){</phrase>
  521. <phrase role="identifier">S</phrase><phrase role="special">();</phrase>
  522. <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'*'</phrase><phrase role="special">||</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'/'</phrase><phrase role="special">){</phrase>
  523. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  524. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  525. <phrase role="identifier">S</phrase><phrase role="special">();</phrase>
  526. <phrase role="special">}</phrase>
  527. <phrase role="special">}</phrase>
  528. <phrase role="keyword">void</phrase> <phrase role="identifier">S</phrase><phrase role="special">(){</phrase>
  529. <phrase role="keyword">if</phrase> <phrase role="special">(</phrase><phrase role="identifier">isdigit</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">)){</phrase>
  530. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  531. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  532. <phrase role="special">}</phrase>
  533. <phrase role="keyword">else</phrase> <phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'('</phrase><phrase role="special">){</phrase>
  534. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  535. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  536. <phrase role="identifier">E</phrase><phrase role="special">();</phrase>
  537. <phrase role="keyword">if</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">')'</phrase><phrase role="special">){</phrase>
  538. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  539. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  540. <phrase role="special">}</phrase><phrase role="keyword">else</phrase><phrase role="special">{</phrase>
  541. <phrase role="keyword">throw</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase><phrase role="string">&quot;parsing failed&quot;</phrase><phrase role="special">);</phrase>
  542. <phrase role="special">}</phrase>
  543. <phrase role="special">}</phrase>
  544. <phrase role="keyword">else</phrase><phrase role="special">{</phrase>
  545. <phrase role="keyword">throw</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase><phrase role="string">&quot;parsing failed&quot;</phrase><phrase role="special">);</phrase>
  546. <phrase role="special">}</phrase>
  547. <phrase role="special">}</phrase>
  548. <phrase role="special">};</phrase>
  549. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istringstream</phrase> <phrase role="identifier">is</phrase><phrase role="special">(</phrase><phrase role="string">&quot;1+1&quot;</phrase><phrase role="special">);</phrase>
  550. <phrase role="comment">// user-code pulls parsed data from parser</phrase>
  551. <phrase role="comment">// invert control flow</phrase>
  552. <phrase role="keyword">char</phrase> <phrase role="identifier">c</phrase><phrase role="special">;</phrase>
  553. <phrase role="keyword">bool</phrase> <phrase role="identifier">done</phrase><phrase role="special">=</phrase><phrase role="keyword">false</phrase><phrase role="special">;</phrase>
  554. <phrase role="comment">// execute parser in new fiber</phrase>
  555. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase> <phrase role="identifier">source</phrase><phrase role="special">{[&amp;</phrase><phrase role="identifier">is</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">c</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">done</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">fiber</phrase><phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">sink</phrase><phrase role="special">){</phrase>
  556. <phrase role="comment">// create parser with callback function</phrase>
  557. <phrase role="identifier">Parser</phrase> <phrase role="identifier">p</phrase><phrase role="special">(</phrase><phrase role="identifier">is</phrase><phrase role="special">,</phrase>
  558. <phrase role="special">[&amp;</phrase><phrase role="identifier">sink</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">c</phrase><phrase role="special">](</phrase><phrase role="keyword">char</phrase> <phrase role="identifier">c_</phrase><phrase role="special">){</phrase>
  559. <phrase role="comment">// resume main fiber</phrase>
  560. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c_</phrase><phrase role="special">;</phrase>
  561. <phrase role="identifier">sink</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  562. <phrase role="special">});</phrase>
  563. <phrase role="comment">// start recursive parsing</phrase>
  564. <phrase role="identifier">p</phrase><phrase role="special">.</phrase><phrase role="identifier">run</phrase><phrase role="special">();</phrase>
  565. <phrase role="comment">// signal termination</phrase>
  566. <phrase role="identifier">done</phrase><phrase role="special">=</phrase><phrase role="keyword">true</phrase><phrase role="special">;</phrase>
  567. <phrase role="comment">// resume main fiber</phrase>
  568. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">);</phrase>
  569. <phrase role="special">}};</phrase>
  570. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">source</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  571. <phrase role="keyword">while</phrase><phrase role="special">(!</phrase><phrase role="identifier">done</phrase><phrase role="special">){</phrase>
  572. <phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;Parsed: %c\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  573. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">Move</phrase><phrase role="special">(</phrase><phrase role="identifier">source</phrase><phrase role="special">).</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  574. <phrase role="special">}</phrase>
  575. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  576. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  577. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="special">+</phrase>
  578. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  579. </programlisting>
  580. <para>
  581. In this example a recursive descent parser uses a callback to emit a newly
  582. passed symbol. Using <link linkend="ff"><emphasis>fiber</emphasis></link> the
  583. control flow can be inverted, e.g. the user-code pulls parsed symbols from
  584. the parser - instead to get pushed from the parser (via callback).
  585. </para>
  586. <para>
  587. The data (character) is transferred between the two fibers.
  588. </para>
  589. <section id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber">
  590. <title><anchor id="implementation"/><link linkend="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber">Implementations:
  591. fcontext_t, ucontext_t and WinFiber</link></title>
  592. <bridgehead renderas="sect4" id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.h0">
  593. <phrase id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t"/><link
  594. linkend="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t">fcontext_t</link>
  595. </bridgehead>
  596. <para>
  597. The implementation uses <emphasis>fcontext_t</emphasis> per default. fcontext_t
  598. is based on assembler and not available for all platforms. It provides a
  599. much better performance than <emphasis>ucontext_t</emphasis> (the context
  600. switch takes two magnitudes of order less CPU cycles; see section <link linkend="performance"><emphasis>performance</emphasis></link>)
  601. and <emphasis>WinFiber</emphasis>.
  602. </para>
  603. <note>
  604. <para>
  605. Because the TIB (thread information block on Windows) is not fully described
  606. in the MSDN, it might be possible that not all required TIB-parts are swapped.
  607. Using WinFiber implementation migh be an alternative.
  608. </para>
  609. </note>
  610. <bridgehead renderas="sect4" id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.h1">
  611. <phrase id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t"/><link
  612. linkend="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t">ucontext_t</link>
  613. </bridgehead>
  614. <para>
  615. As an alternative, <ulink url="https://en.wikipedia.org/wiki/Setcontext"><emphasis>ucontext_t</emphasis></ulink>
  616. can be used by compiling with <code><phrase role="identifier">BOOST_USE_UCONTEXT</phrase></code>
  617. and b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase
  618. role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">ucontext</phrase></code>.
  619. <emphasis>ucontext_t</emphasis> might be available on a broader range of
  620. POSIX-platforms but has some <link linkend="ucontext"><emphasis>disadvantages</emphasis></link>
  621. (for instance deprecated since POSIX.1-2003, not C99 conform).
  622. </para>
  623. <note>
  624. <para>
  625. <link linkend="ff"><emphasis>fiber</emphasis></link> supports <link linkend="segmented"><emphasis>Segmented
  626. stacks</emphasis></link> only with <emphasis>ucontext_t</emphasis> as its
  627. implementation.
  628. </para>
  629. </note>
  630. <bridgehead renderas="sect4" id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.h2">
  631. <phrase id="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber"/><link
  632. linkend="context.ff.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber">WinFiber</link>
  633. </bridgehead>
  634. <para>
  635. With <code><phrase role="identifier">BOOST_USE_WINFIB</phrase></code> and
  636. b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase
  637. role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">winfib</phrase></code>
  638. Win32-Fibers are used as implementation for <link linkend="ff"><emphasis>fiber</emphasis></link>.
  639. </para>
  640. <note>
  641. <para>
  642. The first call of <link linkend="ff"><emphasis>fiber</emphasis></link>
  643. converts the thread into a Windows fiber by invoking <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase
  644. role="special">()</phrase></code>. If desired, <code><phrase role="identifier">ConvertFiberToThread</phrase><phrase
  645. role="special">()</phrase></code> has to be called by the user explicitly
  646. in order to release resources allocated by <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase
  647. role="special">()</phrase></code> (e.g. after using boost.context).
  648. </para>
  649. </note>
  650. </section>
  651. <section id="context.ff.class__fiber_">
  652. <title><link linkend="context.ff.class__fiber_">Class <code><phrase role="identifier">fiber</phrase></code></link></title>
  653. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">fiber</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  654. <phrase role="keyword">class</phrase> <phrase role="identifier">fiber</phrase> <phrase role="special">{</phrase>
  655. <phrase role="keyword">public</phrase><phrase role="special">:</phrase>
  656. <phrase role="identifier">fiber</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  657. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  658. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  659. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">StackAlloc</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  660. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">StackAlloc</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  661. <phrase role="special">~</phrase><phrase role="identifier">fiber</phrase><phrase role="special">();</phrase>
  662. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  663. <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  664. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
  665. <phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
  666. <phrase role="identifier">fiber</phrase> <phrase role="identifier">resume</phrase><phrase role="special">()</phrase> <phrase role="special">&amp;&amp;;</phrase>
  667. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  668. <phrase role="identifier">fiber</phrase> <phrase role="identifier">resume_with</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">)</phrase> <phrase role="special">&amp;&amp;;</phrase>
  669. <phrase role="keyword">explicit</phrase> <phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  670. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  671. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  672. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  673. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  674. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  675. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  676. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  677. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase>
  678. <phrase role="keyword">friend</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase>
  679. <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">os</phrase><phrase role="special">,</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  680. <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  681. <phrase role="special">};</phrase>
  682. </programlisting>
  683. <para>
  684. <bridgehead renderas="sect4" id="ff_constructor1_bridgehead">
  685. <phrase id="ff_constructor1"/>
  686. <link linkend="ff_constructor1">Constructor</link>
  687. </bridgehead>
  688. </para>
  689. <programlisting><phrase role="identifier">fiber</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  690. </programlisting>
  691. <variablelist>
  692. <title></title>
  693. <varlistentry>
  694. <term>Effects:</term>
  695. <listitem>
  696. <para>
  697. Creates a invalid fiber.
  698. </para>
  699. </listitem>
  700. </varlistentry>
  701. <varlistentry>
  702. <term>Throws:</term>
  703. <listitem>
  704. <para>
  705. Nothing.
  706. </para>
  707. </listitem>
  708. </varlistentry>
  709. </variablelist>
  710. <para>
  711. <bridgehead renderas="sect4" id="ff_constructor2_bridgehead">
  712. <phrase id="ff_constructor2"/>
  713. <link linkend="ff_constructor2">Constructor</link>
  714. </bridgehead>
  715. </para>
  716. <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  717. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  718. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">StackAlloc</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  719. <phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase><phrase role="special">,</phrase> <phrase role="identifier">StackAlloc</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase> <phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  720. </programlisting>
  721. <variablelist>
  722. <title></title>
  723. <varlistentry>
  724. <term>Effects:</term>
  725. <listitem>
  726. <para>
  727. Creates a new fiber and prepares the context to execute <code><phrase
  728. role="identifier">fn</phrase></code>. <code><phrase role="identifier">fixedsize_stack</phrase></code>
  729. is used as default stack allocator (stack size == fixedsize_stack::traits::default_size()).
  730. The constructor with argument type <code><phrase role="identifier">preallocated</phrase></code>,
  731. is used to create a user defined data <link linkend="ff_prealloc">(for
  732. instance additional control structures)</link> on top of the stack.
  733. </para>
  734. </listitem>
  735. </varlistentry>
  736. </variablelist>
  737. <para>
  738. <bridgehead renderas="sect4" id="ff_destructor destructor_bridgehead">
  739. <phrase id="ff_destructor destructor"/>
  740. <link linkend="ff_destructor
  741. destructor">Destructor</link>
  742. </bridgehead>
  743. </para>
  744. <programlisting><phrase role="special">~</phrase><phrase role="identifier">fiber</phrase><phrase role="special">();</phrase>
  745. </programlisting>
  746. <variablelist>
  747. <title></title>
  748. <varlistentry>
  749. <term>Effects:</term>
  750. <listitem>
  751. <para>
  752. Destructs the associated stack if <code><phrase role="special">*</phrase><phrase
  753. role="keyword">this</phrase></code> is a valid fiber, e.g. <emphasis>fiber::operator
  754. bool()</emphasis> returns <code><phrase role="keyword">true</phrase></code>.
  755. </para>
  756. </listitem>
  757. </varlistentry>
  758. <varlistentry>
  759. <term>Throws:</term>
  760. <listitem>
  761. <para>
  762. Nothing.
  763. </para>
  764. </listitem>
  765. </varlistentry>
  766. </variablelist>
  767. <para>
  768. <bridgehead renderas="sect4" id="ff_move constructor_bridgehead">
  769. <phrase id="ff_move constructor"/>
  770. <link linkend="ff_move constructor">Move
  771. constructor</link>
  772. </bridgehead>
  773. </para>
  774. <programlisting><phrase role="identifier">fiber</phrase><phrase role="special">(</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  775. </programlisting>
  776. <variablelist>
  777. <title></title>
  778. <varlistentry>
  779. <term>Effects:</term>
  780. <listitem>
  781. <para>
  782. Moves underlying capture fiber to <code><phrase role="special">*</phrase><phrase
  783. role="keyword">this</phrase></code>.
  784. </para>
  785. </listitem>
  786. </varlistentry>
  787. <varlistentry>
  788. <term>Throws:</term>
  789. <listitem>
  790. <para>
  791. Nothing.
  792. </para>
  793. </listitem>
  794. </varlistentry>
  795. </variablelist>
  796. <para>
  797. <bridgehead renderas="sect4" id="ff_move assignment_bridgehead">
  798. <phrase id="ff_move assignment"/>
  799. <link linkend="ff_move assignment">Move assignment
  800. operator</link>
  801. </bridgehead>
  802. </para>
  803. <programlisting><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  804. </programlisting>
  805. <variablelist>
  806. <title></title>
  807. <varlistentry>
  808. <term>Effects:</term>
  809. <listitem>
  810. <para>
  811. Moves the state of <code><phrase role="identifier">other</phrase></code>
  812. to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>
  813. using move semantics.
  814. </para>
  815. </listitem>
  816. </varlistentry>
  817. <varlistentry>
  818. <term>Throws:</term>
  819. <listitem>
  820. <para>
  821. Nothing.
  822. </para>
  823. </listitem>
  824. </varlistentry>
  825. </variablelist>
  826. <para>
  827. <bridgehead renderas="sect4" id="ff_operator_call_bridgehead">
  828. <phrase id="ff_operator_call"/>
  829. <link linkend="ff_operator_call">Member function
  830. <code>operator()</code>()</link>
  831. </bridgehead>
  832. </para>
  833. <programlisting><phrase role="identifier">fiber</phrase> <phrase role="identifier">resume</phrase><phrase role="special">()</phrase> <phrase role="special">&amp;&amp;;</phrase>
  834. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  835. <phrase role="identifier">fiber</phrase> <phrase role="identifier">resume_with</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">)</phrase> <phrase role="special">&amp;&amp;;</phrase>
  836. </programlisting>
  837. <variablelist>
  838. <title></title>
  839. <varlistentry>
  840. <term>Effects:</term>
  841. <listitem>
  842. <para>
  843. Captures current fiber and resumes <code><phrase role="special">*</phrase><phrase
  844. role="keyword">this</phrase></code>. The function <code><phrase role="identifier">resume_with</phrase></code>,
  845. is used to execute function <code><phrase role="identifier">fn</phrase></code>
  846. in the execution context of <code><phrase role="special">*</phrase><phrase
  847. role="keyword">this</phrase></code> (e.g. the stack frame of <code><phrase
  848. role="identifier">fn</phrase></code> is allocated on stack of <code><phrase
  849. role="special">*</phrase><phrase role="keyword">this</phrase></code>).
  850. </para>
  851. </listitem>
  852. </varlistentry>
  853. <varlistentry>
  854. <term>Returns:</term>
  855. <listitem>
  856. <para>
  857. The fiber representing the fiber that has been suspended.
  858. </para>
  859. </listitem>
  860. </varlistentry>
  861. <varlistentry>
  862. <term>Note:</term>
  863. <listitem>
  864. <para>
  865. Because <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>
  866. gets invalidated, <code><phrase role="identifier">resume</phrase><phrase
  867. role="special">()</phrase></code> and <code><phrase role="identifier">resume_with</phrase><phrase
  868. role="special">()</phrase></code> are rvalue-ref qualified and bind
  869. only to rvalues.
  870. </para>
  871. </listitem>
  872. </varlistentry>
  873. <varlistentry>
  874. <term>Note:</term>
  875. <listitem>
  876. <para>
  877. Function <code><phrase role="identifier">fn</phrase></code> needs to
  878. return <code><phrase role="identifier">fiber</phrase></code>.
  879. </para>
  880. </listitem>
  881. </varlistentry>
  882. <varlistentry>
  883. <term>Note:</term>
  884. <listitem>
  885. <para>
  886. The returned fiber indicates if the suspended fiber has terminated
  887. (return from context-function) via <code><phrase role="keyword">bool</phrase>
  888. <phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code>.
  889. </para>
  890. </listitem>
  891. </varlistentry>
  892. </variablelist>
  893. <para>
  894. <bridgehead renderas="sect4" id="ff_operator_bool_bridgehead">
  895. <phrase id="ff_operator_bool"/>
  896. <link linkend="ff_operator_bool">Member function
  897. <code>operator bool</code>()</link>
  898. </bridgehead>
  899. </para>
  900. <programlisting><phrase role="keyword">explicit</phrase> <phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  901. </programlisting>
  902. <variablelist>
  903. <title></title>
  904. <varlistentry>
  905. <term>Returns:</term>
  906. <listitem>
  907. <para>
  908. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  909. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  910. points to a captured fiber.
  911. </para>
  912. </listitem>
  913. </varlistentry>
  914. <varlistentry>
  915. <term>Throws:</term>
  916. <listitem>
  917. <para>
  918. Nothing.
  919. </para>
  920. </listitem>
  921. </varlistentry>
  922. </variablelist>
  923. <para>
  924. <bridgehead renderas="sect4" id="ff_operator_not_bridgehead">
  925. <phrase id="ff_operator_not"/>
  926. <link linkend="ff_operator_not">Member function <code>operator!</code>()</link>
  927. </bridgehead>
  928. </para>
  929. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  930. </programlisting>
  931. <variablelist>
  932. <title></title>
  933. <varlistentry>
  934. <term>Returns:</term>
  935. <listitem>
  936. <para>
  937. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  938. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  939. does not point to a captured fiber.
  940. </para>
  941. </listitem>
  942. </varlistentry>
  943. <varlistentry>
  944. <term>Throws:</term>
  945. <listitem>
  946. <para>
  947. Nothing.
  948. </para>
  949. </listitem>
  950. </varlistentry>
  951. </variablelist>
  952. <para>
  953. <bridgehead renderas="sect4" id="ff_operator_equal_bridgehead">
  954. <phrase id="ff_operator_equal"/>
  955. <link linkend="ff_operator_equal">Member function
  956. <code>operator==</code>()</link>
  957. </bridgehead>
  958. </para>
  959. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  960. </programlisting>
  961. <variablelist>
  962. <title></title>
  963. <varlistentry>
  964. <term>Returns:</term>
  965. <listitem>
  966. <para>
  967. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  968. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  969. and <code><phrase role="identifier">other</phrase></code> represent
  970. the same fiber, <code><phrase role="keyword">false</phrase></code>
  971. otherwise.
  972. </para>
  973. </listitem>
  974. </varlistentry>
  975. <varlistentry>
  976. <term>Throws:</term>
  977. <listitem>
  978. <para>
  979. Nothing.
  980. </para>
  981. </listitem>
  982. </varlistentry>
  983. </variablelist>
  984. <para>
  985. <bridgehead renderas="sect4" id="ff_operator_notequal_bridgehead">
  986. <phrase id="ff_operator_notequal"/>
  987. <link linkend="ff_operator_notequal">Member
  988. function <code>operator!=</code>()</link>
  989. </bridgehead>
  990. </para>
  991. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  992. </programlisting>
  993. <variablelist>
  994. <title></title>
  995. <varlistentry>
  996. <term>Returns:</term>
  997. <listitem>
  998. <para>
  999. <code>! (other == * this)</code>
  1000. </para>
  1001. </listitem>
  1002. </varlistentry>
  1003. <varlistentry>
  1004. <term>Throws:</term>
  1005. <listitem>
  1006. <para>
  1007. Nothing.
  1008. </para>
  1009. </listitem>
  1010. </varlistentry>
  1011. </variablelist>
  1012. <para>
  1013. <bridgehead renderas="sect4" id="ff_operator_less_bridgehead">
  1014. <phrase id="ff_operator_less"/>
  1015. <link linkend="ff_operator_less">Member function
  1016. <code>operator&lt;</code>()</link>
  1017. </bridgehead>
  1018. </para>
  1019. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1020. </programlisting>
  1021. <variablelist>
  1022. <title></title>
  1023. <varlistentry>
  1024. <term>Returns:</term>
  1025. <listitem>
  1026. <para>
  1027. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  1028. role="special">*</phrase><phrase role="keyword">this</phrase> <phrase
  1029. role="special">!=</phrase> <phrase role="identifier">other</phrase></code>
  1030. is true and the implementation-defined total order of <code><phrase
  1031. role="identifier">fiber</phrase></code> values places <code><phrase
  1032. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  1033. before <code><phrase role="identifier">other</phrase></code>, false
  1034. otherwise.
  1035. </para>
  1036. </listitem>
  1037. </varlistentry>
  1038. <varlistentry>
  1039. <term>Throws:</term>
  1040. <listitem>
  1041. <para>
  1042. Nothing.
  1043. </para>
  1044. </listitem>
  1045. </varlistentry>
  1046. </variablelist>
  1047. <para>
  1048. <bridgehead renderas="sect4" id="ff_operator_greater_bridgehead">
  1049. <phrase id="ff_operator_greater"/>
  1050. <link linkend="ff_operator_greater">Member
  1051. function <code>operator&gt;</code>()</link>
  1052. </bridgehead>
  1053. </para>
  1054. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1055. </programlisting>
  1056. <variablelist>
  1057. <title></title>
  1058. <varlistentry>
  1059. <term>Returns:</term>
  1060. <listitem>
  1061. <para>
  1062. <code><phrase role="identifier">other</phrase> <phrase role="special">&lt;</phrase>
  1063. <phrase role="special">*</phrase> <phrase role="keyword">this</phrase></code>
  1064. </para>
  1065. </listitem>
  1066. </varlistentry>
  1067. <varlistentry>
  1068. <term>Throws:</term>
  1069. <listitem>
  1070. <para>
  1071. Nothing.
  1072. </para>
  1073. </listitem>
  1074. </varlistentry>
  1075. </variablelist>
  1076. <para>
  1077. <bridgehead renderas="sect4" id="ff_operator_lesseq_bridgehead">
  1078. <phrase id="ff_operator_lesseq"/>
  1079. <link linkend="ff_operator_lesseq">Member function
  1080. <code>operator&lt;=</code>()</link>
  1081. </bridgehead>
  1082. </para>
  1083. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1084. </programlisting>
  1085. <variablelist>
  1086. <title></title>
  1087. <varlistentry>
  1088. <term>Returns:</term>
  1089. <listitem>
  1090. <para>
  1091. <code><phrase role="special">!</phrase> <phrase role="special">(</phrase><phrase
  1092. role="identifier">other</phrase> <phrase role="special">&lt;</phrase>
  1093. <phrase role="special">*</phrase> <phrase role="keyword">this</phrase><phrase
  1094. role="special">)</phrase></code>
  1095. </para>
  1096. </listitem>
  1097. </varlistentry>
  1098. <varlistentry>
  1099. <term>Throws:</term>
  1100. <listitem>
  1101. <para>
  1102. Nothing.
  1103. </para>
  1104. </listitem>
  1105. </varlistentry>
  1106. </variablelist>
  1107. <para>
  1108. <bridgehead renderas="sect4" id="ff_operator_greatereq_bridgehead">
  1109. <phrase id="ff_operator_greatereq"/>
  1110. <link linkend="ff_operator_greatereq">Member
  1111. function <code>operator&gt;=</code>()</link>
  1112. </bridgehead>
  1113. </para>
  1114. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1115. </programlisting>
  1116. <variablelist>
  1117. <title></title>
  1118. <varlistentry>
  1119. <term>Returns:</term>
  1120. <listitem>
  1121. <para>
  1122. <code><phrase role="special">!</phrase> <phrase role="special">(*</phrase>
  1123. <phrase role="keyword">this</phrase> <phrase role="special">&lt;</phrase>
  1124. <phrase role="identifier">other</phrase><phrase role="special">)</phrase></code>
  1125. </para>
  1126. </listitem>
  1127. </varlistentry>
  1128. <varlistentry>
  1129. <term>Throws:</term>
  1130. <listitem>
  1131. <para>
  1132. Nothing.
  1133. </para>
  1134. </listitem>
  1135. </varlistentry>
  1136. </variablelist>
  1137. <para>
  1138. <bridgehead renderas="sect4" id="ff__bridgehead">
  1139. <phrase id="ff_"/>
  1140. <link linkend="ff_">Non-member function <code>operator&lt;&lt;()</code></link>
  1141. </bridgehead>
  1142. </para>
  1143. <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase>
  1144. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase>
  1145. <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">os</phrase><phrase role="special">,</phrase><phrase role="identifier">fiber</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase>
  1146. </programlisting>
  1147. <variablelist>
  1148. <title></title>
  1149. <varlistentry>
  1150. <term>Effects:</term>
  1151. <listitem>
  1152. <para>
  1153. Writes the representation of <code><phrase role="identifier">other</phrase></code>
  1154. to stream <code><phrase role="identifier">os</phrase></code>.
  1155. </para>
  1156. </listitem>
  1157. </varlistentry>
  1158. <varlistentry>
  1159. <term>Returns:</term>
  1160. <listitem>
  1161. <para>
  1162. <code><phrase role="identifier">os</phrase></code>
  1163. </para>
  1164. </listitem>
  1165. </varlistentry>
  1166. </variablelist>
  1167. </section>
  1168. </section>
  1169. <section id="context.cc">
  1170. <title><anchor id="cc"/><link linkend="context.cc">Context switching with call/cc</link></title>
  1171. <note>
  1172. <para>
  1173. <emphasis>call/cc</emphasis> is the reference implementation of C++ proposal
  1174. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0534r3.pdf">P0534R3:
  1175. call/cc (call-with-current-continuation): A low-level API for stackful context
  1176. switching</ulink>.
  1177. </para>
  1178. </note>
  1179. <para>
  1180. <emphasis>call/cc</emphasis> (call with current continuation) is a universal
  1181. control operator (well-known from the programming language Scheme) that captures
  1182. the current continuation as a first-class object and pass it as an argument
  1183. to another continuation.
  1184. </para>
  1185. <para>
  1186. A continuation (abstract concept of functional programming languages) represents
  1187. the state of the control flow of a program at a given point in time. Continuations
  1188. can be suspended and resumed later in order to change the control flow of a
  1189. program.
  1190. </para>
  1191. <para>
  1192. Modern micro-processors are registers machines; the content of processor registers
  1193. represent a continuation of the executed program at a given point in time.
  1194. Operating systems simulate parallel execution of programs on a single processor
  1195. by switching between programs (context switch) by preserving and restoring
  1196. the continuation, e.g. the content of all registers.
  1197. </para>
  1198. <bridgehead renderas="sect3" id="context.cc.h0">
  1199. <phrase id="context.cc._link_linkend__cc___emphasis_callcc____emphasis___link_"/><link
  1200. linkend="context.cc._link_linkend__cc___emphasis_callcc____emphasis___link_"><link
  1201. linkend="cc"><emphasis>callcc()</emphasis></link></link>
  1202. </bridgehead>
  1203. <para>
  1204. <link linkend="cc"><emphasis>callcc()</emphasis></link> is the C++ equivalent
  1205. to Scheme's <emphasis>call/cc</emphasis> operator. It captures the current
  1206. continuation (the rest of the computation; code after <link linkend="cc"><emphasis>callcc()</emphasis></link>)
  1207. and triggers a context switch. The context switch is achieved by preserving
  1208. certain registers (including instruction and stack pointer), defined by the
  1209. calling convention of the ABI, of the current continuation and restoring those
  1210. registers of the resumed continuation. The control flow of the resumed continuation
  1211. continues. The current continuation is suspended and passed as argument to
  1212. the resumed continuation.
  1213. </para>
  1214. <para>
  1215. <link linkend="cc"><emphasis>callcc()</emphasis></link> expects a <emphasis>context-function</emphasis>
  1216. with signature <code><phrase role="char">'continuation(continuation &amp;&amp;
  1217. c)'</phrase></code>. The parameter <code><phrase role="identifier">c</phrase></code>
  1218. represents the current continuation from which this continuation was resumed
  1219. (e.g. that has called <link linkend="cc"><emphasis>callcc()</emphasis></link>).
  1220. </para>
  1221. <para>
  1222. On return the <emphasis>context-function</emphasis> of the current continuation
  1223. has to specify an <link linkend="cc"><emphasis>continuation</emphasis></link>
  1224. to which the execution control is transferred after termination of the current
  1225. continuation.
  1226. </para>
  1227. <para>
  1228. If an instance with valid state goes out of scope and the <emphasis>context-function</emphasis>
  1229. has not yet returned, the stack is traversed in order to access the control
  1230. structure (address stored at the first stack frame) and continuation's stack
  1231. is deallocated via the <emphasis>StackAllocator</emphasis>.
  1232. </para>
  1233. <note>
  1234. <para>
  1235. <link linkend="segmented"><emphasis>Segmented stacks</emphasis></link> are
  1236. supported by <link linkend="cc"><emphasis>callcc()</emphasis></link> using
  1237. <link linkend="implementation"><emphasis>ucontext_t</emphasis></link>.
  1238. </para>
  1239. </note>
  1240. <bridgehead renderas="sect3" id="context.cc.h1">
  1241. <phrase id="context.cc._link_linkend__cc___emphasis_continuation__emphasis___link_"/><link
  1242. linkend="context.cc._link_linkend__cc___emphasis_continuation__emphasis___link_"><link
  1243. linkend="cc"><emphasis>continuation</emphasis></link></link>
  1244. </bridgehead>
  1245. <para>
  1246. <link linkend="cc"><emphasis>continuation</emphasis></link> represents a continuation;
  1247. it contains the content of preserved registers and manages the associated stack
  1248. (allocation/deallocation). <link linkend="cc"><emphasis>continuation</emphasis></link>
  1249. is a one-shot continuation - it can be used only once, after calling <emphasis>continuation::resume()</emphasis>
  1250. or <emphasis>continuation::resume_with()</emphasis> it is invalidated.
  1251. </para>
  1252. <para>
  1253. <link linkend="cc"><emphasis>continuation</emphasis></link> is only move-constructible
  1254. and move-assignable.
  1255. </para>
  1256. <para>
  1257. As a first-class object <link linkend="cc"><emphasis>continuation</emphasis></link>
  1258. can be applied to and returned from a function, assigned to a variable or stored
  1259. in a container.
  1260. </para>
  1261. <para>
  1262. A continuation is continued by calling <code><phrase role="identifier">resume</phrase><phrase
  1263. role="special">()</phrase></code>/<code><phrase role="identifier">resume_with</phrase><phrase
  1264. role="special">()</phrase></code>.
  1265. </para>
  1266. <bridgehead renderas="sect3" id="context.cc.h2">
  1267. <phrase id="context.cc.usage"/><link linkend="context.cc.usage">Usage</link>
  1268. </bridgehead>
  1269. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1270. <phrase role="keyword">int</phrase> <phrase role="identifier">a</phrase><phrase role="special">;</phrase>
  1271. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">callcc</phrase><phrase role="special">(</phrase>
  1272. <phrase role="special">[&amp;</phrase><phrase role="identifier">a</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">sink</phrase><phrase role="special">){</phrase>
  1273. <phrase role="identifier">a</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase>
  1274. <phrase role="keyword">int</phrase> <phrase role="identifier">b</phrase><phrase role="special">=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1275. <phrase role="keyword">for</phrase><phrase role="special">(;;){</phrase>
  1276. <phrase role="identifier">sink</phrase><phrase role="special">=</phrase><phrase role="identifier">sink</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1277. <phrase role="keyword">int</phrase> <phrase role="identifier">next</phrase><phrase role="special">=</phrase><phrase role="identifier">a</phrase><phrase role="special">+</phrase><phrase role="identifier">b</phrase><phrase role="special">;</phrase>
  1278. <phrase role="identifier">a</phrase><phrase role="special">=</phrase><phrase role="identifier">b</phrase><phrase role="special">;</phrase>
  1279. <phrase role="identifier">b</phrase><phrase role="special">=</phrase><phrase role="identifier">next</phrase><phrase role="special">;</phrase>
  1280. <phrase role="special">}</phrase>
  1281. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">);</phrase>
  1282. <phrase role="special">});</phrase>
  1283. <phrase role="keyword">for</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">j</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase><phrase role="identifier">j</phrase><phrase role="special">&lt;</phrase><phrase role="number">10</phrase><phrase role="special">;++</phrase><phrase role="identifier">j</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  1284. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">a</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot; &quot;</phrase><phrase role="special">;</phrase>
  1285. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">source</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1286. <phrase role="special">}</phrase>
  1287. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  1288. <phrase role="number">0</phrase> <phrase role="number">1</phrase> <phrase role="number">1</phrase> <phrase role="number">2</phrase> <phrase role="number">3</phrase> <phrase role="number">5</phrase> <phrase role="number">8</phrase> <phrase role="number">13</phrase> <phrase role="number">21</phrase> <phrase role="number">34</phrase>
  1289. </programlisting>
  1290. <para>
  1291. This simple example demonstrates the basic usage of <emphasis>call/cc</emphasis>
  1292. as a <emphasis>generator</emphasis>. The continuation <code><phrase role="identifier">sink</phrase></code>
  1293. represents the <emphasis>main</emphasis>-continuation (function <code><phrase
  1294. role="identifier">main</phrase><phrase role="special">()</phrase></code>).
  1295. <code><phrase role="identifier">sink</phrase></code> is captured (current-continuation)
  1296. by invoking <link linkend="cc"><emphasis>callcc()</emphasis></link> and passed
  1297. as parameter to the lambda.
  1298. </para>
  1299. <para>
  1300. Because the state is invalidated (one-shot continuation) by each call of <emphasis>continuation::resume()</emphasis>,
  1301. the new state of the <link linkend="cc"><emphasis>continuation</emphasis></link>,
  1302. returned by <emphasis>continuation::resume()</emphasis>, needs to be assigned
  1303. to <code><phrase role="identifier">sink</phrase></code> after each call.
  1304. </para>
  1305. <para>
  1306. The lambda that calculates the Fibonacci numbers is executed inside the continuation
  1307. represented by <code><phrase role="identifier">source</phrase></code>. Calculated
  1308. Fibonacci numbers are transferred between the two continuations via variable
  1309. <code><phrase role="identifier">a</phrase></code> (lambda capture reference).
  1310. </para>
  1311. <para>
  1312. The locale variables <code><phrase role="identifier">b</phrase></code> and
  1313. <code> <phrase role="identifier">next</phrase></code> remain their values during
  1314. each context switch. This is possible due <code><phrase role="identifier">source</phrase></code>
  1315. has its own stack and the stack is exchanged by each context switch.
  1316. </para>
  1317. <bridgehead renderas="sect3" id="context.cc.h3">
  1318. <phrase id="context.cc.parameter_passing"/><link linkend="context.cc.parameter_passing">Parameter
  1319. passing</link>
  1320. </bridgehead>
  1321. <para>
  1322. Data can be transferred between two continuations via global pointers, calling
  1323. wrappers (like <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase
  1324. role="identifier">bind</phrase></code>) or lambda captures.
  1325. </para>
  1326. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1327. <phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase><phrase role="special">=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1328. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">c1</phrase><phrase role="special">=</phrase><phrase role="identifier">callcc</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">i</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c2</phrase><phrase role="special">){</phrase>
  1329. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;inside c1,i==%d\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
  1330. <phrase role="identifier">i</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1331. <phrase role="keyword">return</phrase> <phrase role="identifier">c2</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1332. <phrase role="special">});</phrase>
  1333. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;i==%d\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
  1334. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  1335. <phrase role="identifier">inside</phrase> <phrase role="identifier">c1</phrase><phrase role="special">,</phrase><phrase role="identifier">i</phrase><phrase role="special">==</phrase><phrase role="number">1</phrase>
  1336. <phrase role="identifier">i</phrase><phrase role="special">==</phrase><phrase role="number">2</phrase>
  1337. </programlisting>
  1338. <para>
  1339. <code><phrase role="identifier">callcc</phrase><phrase role="special">(&lt;</phrase><phrase
  1340. role="identifier">lambda</phrase><phrase role="special">&gt;)</phrase></code>
  1341. enters the lambda in continuation represented by <code><phrase role="identifier">c1</phrase></code>
  1342. with lambda capture reference <code><phrase role="identifier">i</phrase><phrase
  1343. role="special">=</phrase><phrase role="number">1</phrase></code>. The expression
  1344. <code><phrase role="identifier">c2</phrase><phrase role="special">.</phrase><phrase
  1345. role="identifier">resume</phrase><phrase role="special">()</phrase></code>
  1346. resumes the continuation <code><phrase role="identifier">c2</phrase></code>.
  1347. On return of <code><phrase role="identifier">callcc</phrase><phrase role="special">(&lt;</phrase><phrase
  1348. role="identifier">lambda</phrase><phrase role="special">&gt;)</phrase></code>,
  1349. the variable <code><phrase role="identifier">i</phrase></code> has the value
  1350. of <code><phrase role="identifier">i</phrase><phrase role="special">+</phrase><phrase
  1351. role="number">1</phrase></code>.
  1352. </para>
  1353. <bridgehead renderas="sect3" id="context.cc.h4">
  1354. <phrase id="context.cc.exception_handling"/><link linkend="context.cc.exception_handling">Exception
  1355. handling</link>
  1356. </bridgehead>
  1357. <para>
  1358. If the function executed inside a <emphasis>context-function</emphasis> emits
  1359. an exception, the application is terminated by calling <code><phrase role="identifier">std</phrase><phrase
  1360. role="special">::</phrase><phrase role="identifier">terminate</phrase><phrase
  1361. role="special">()</phrase></code>. <code><phrase role="identifier">std</phrase><phrase
  1362. role="special">::</phrase><phrase role="identifier">exception_ptr</phrase></code>
  1363. can be used to transfer exceptions between different continuations.
  1364. </para>
  1365. <important>
  1366. <para>
  1367. Do not jump from inside a catch block and then re-throw the exception in
  1368. another continuation.
  1369. </para>
  1370. </important>
  1371. <anchor id="cc_ontop"/>
  1372. <bridgehead renderas="sect3" id="context.cc.h5">
  1373. <phrase id="context.cc.executing_function_on_top_of_a_continuation"/><link
  1374. linkend="context.cc.executing_function_on_top_of_a_continuation">Executing
  1375. function on top of a continuation</link>
  1376. </bridgehead>
  1377. <para>
  1378. Sometimes it is useful to execute a new function on top of a resumed continuation.
  1379. For this purpose <emphasis>continuation::resume_with()</emphasis> has to be
  1380. used. The function passed as argument must accept a rvalue reference to <link
  1381. linkend="cc"><emphasis>continuation</emphasis></link> and return <code><phrase
  1382. role="keyword">void</phrase></code>.
  1383. </para>
  1384. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1385. <phrase role="keyword">int</phrase> <phrase role="identifier">data</phrase><phrase role="special">=</phrase><phrase role="number">0</phrase><phrase role="special">;</phrase>
  1386. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">callcc</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">data</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  1387. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered first time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1388. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1389. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1390. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered second time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1391. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1392. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1393. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: entered third time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1394. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1395. <phrase role="special">});</phrase>
  1396. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned first time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1397. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1398. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1399. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned second time: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1400. <phrase role="identifier">data</phrase><phrase role="special">+=</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1401. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume_with</phrase><phrase role="special">([&amp;</phrase><phrase role="identifier">data</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c</phrase><phrase role="special">){</phrase>
  1402. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f2: entered: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">data</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1403. <phrase role="identifier">data</phrase><phrase role="special">=-</phrase><phrase role="number">1</phrase><phrase role="special">;</phrase>
  1404. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1405. <phrase role="special">});</phrase>
  1406. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;f1: returned third time&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1407. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  1408. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">first</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">0</phrase>
  1409. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">first</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  1410. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">second</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">2</phrase>
  1411. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">second</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="number">3</phrase>
  1412. <phrase role="identifier">f2</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase><phrase role="special">:</phrase> <phrase role="number">4</phrase>
  1413. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">entered</phrase> <phrase role="identifier">third</phrase> <phrase role="identifier">time</phrase><phrase role="special">:</phrase> <phrase role="special">-</phrase><phrase role="number">1</phrase>
  1414. <phrase role="identifier">f1</phrase><phrase role="special">:</phrase> <phrase role="identifier">returned</phrase> <phrase role="identifier">third</phrase> <phrase role="identifier">time</phrase>
  1415. </programlisting>
  1416. <para>
  1417. The expression <code><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase
  1418. role="identifier">resume_with</phrase><phrase role="special">(...)</phrase></code>
  1419. executes a lambda on top of continuation <code><phrase role="identifier">c</phrase></code>,
  1420. e.g. an additional stack frame is allocated on top of the stack. This lambda
  1421. assigns <code><phrase role="special">-</phrase><phrase role="number">1</phrase></code>
  1422. to <code><phrase role="identifier">data</phrase></code> and returns to the
  1423. second invocation of <code><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase
  1424. role="identifier">resume</phrase><phrase role="special">()</phrase></code>.
  1425. </para>
  1426. <para>
  1427. Another option is to execute a function on top of the continuation that throws
  1428. an exception.
  1429. </para>
  1430. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1431. <phrase role="keyword">struct</phrase> <phrase role="identifier">my_exception</phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase> <phrase role="special">{</phrase>
  1432. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">c</phrase><phrase role="special">;</phrase>
  1433. <phrase role="identifier">my_exception</phrase><phrase role="special">(</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c_</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">what</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  1434. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">{</phrase> <phrase role="identifier">what</phrase> <phrase role="special">},</phrase>
  1435. <phrase role="identifier">c</phrase><phrase role="special">{</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">c_</phrase><phrase role="special">)</phrase> <phrase role="special">}</phrase> <phrase role="special">{</phrase>
  1436. <phrase role="special">}</phrase>
  1437. <phrase role="special">};</phrase>
  1438. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">callcc</phrase><phrase role="special">([](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  1439. <phrase role="keyword">for</phrase> <phrase role="special">(;;)</phrase> <phrase role="special">{</phrase>
  1440. <phrase role="keyword">try</phrase> <phrase role="special">{</phrase>
  1441. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;entered&quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1442. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1443. <phrase role="special">}</phrase> <phrase role="keyword">catch</phrase> <phrase role="special">(</phrase><phrase role="identifier">my_exception</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">ex</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  1444. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="string">&quot;my_exception: &quot;</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">ex</phrase><phrase role="special">.</phrase><phrase role="identifier">what</phrase><phrase role="special">()</phrase> <phrase role="special">&lt;&lt;</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
  1445. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">ex</phrase><phrase role="special">.</phrase><phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1446. <phrase role="special">}</phrase>
  1447. <phrase role="special">}</phrase>
  1448. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1449. <phrase role="special">});</phrase>
  1450. <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">resume_with</phrase><phrase role="special">(</phrase>
  1451. <phrase role="special">[](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">c</phrase><phrase role="special">){</phrase>
  1452. <phrase role="keyword">throw</phrase> <phrase role="identifier">my_exception</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">c</phrase><phrase role="special">),</phrase><phrase role="string">&quot;abc&quot;</phrase><phrase role="special">);</phrase>
  1453. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase> <phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1454. <phrase role="special">});</phrase>
  1455. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  1456. <phrase role="identifier">entered</phrase>
  1457. <phrase role="identifier">my_exception</phrase><phrase role="special">:</phrase> <phrase role="identifier">abc</phrase>
  1458. </programlisting>
  1459. <para>
  1460. In this exception <code><phrase role="identifier">my_exception</phrase></code>
  1461. is throw from a function invoked on-top of continuation <code><phrase role="identifier">c</phrase></code>
  1462. and catched inside the <code><phrase role="keyword">for</phrase></code>-loop.
  1463. </para>
  1464. <bridgehead renderas="sect3" id="context.cc.h6">
  1465. <phrase id="context.cc.stack_unwinding"/><link linkend="context.cc.stack_unwinding">Stack
  1466. unwinding</link>
  1467. </bridgehead>
  1468. <para>
  1469. On construction of <link linkend="cc"><emphasis>continuation</emphasis></link>
  1470. a stack is allocated. If the <emphasis>context-function</emphasis> returns
  1471. the stack will be destructed. If the <emphasis>context-function</emphasis>
  1472. has not yet returned and the destructor of an valid <link linkend="cc"><emphasis>continuation</emphasis></link>
  1473. instance (e.g. <emphasis>continuation::operator bool()</emphasis> returns
  1474. <code><phrase role="keyword">true</phrase></code>) is called, the stack will
  1475. be destructed too.
  1476. </para>
  1477. <important>
  1478. <para>
  1479. Code executed by <emphasis>context-function</emphasis> must not prevent the
  1480. propagation ofs the <emphasis>detail::forced_unwind</emphasis> exception.
  1481. Absorbing that exception will cause stack unwinding to fail. Thus, any code
  1482. that catches all exceptions must re-throw any pending <emphasis>detail::forced_unwind</emphasis>
  1483. exception.
  1484. </para>
  1485. </important>
  1486. <anchor id="cc_prealloc"/>
  1487. <bridgehead renderas="sect3" id="context.cc.h7">
  1488. <phrase id="context.cc.allocating_control_structures_on_top_of_stack"/><link
  1489. linkend="context.cc.allocating_control_structures_on_top_of_stack">Allocating
  1490. control structures on top of stack</link>
  1491. </bridgehead>
  1492. <para>
  1493. Allocating control structures on top of the stack requires to allocated the
  1494. <emphasis>stack_context</emphasis> and create the control structure with placement
  1495. new before <link linkend="cc"><emphasis>continuation</emphasis></link> is created.
  1496. </para>
  1497. <note>
  1498. <para>
  1499. The user is responsible for destructing the control structure at the top
  1500. of the stack.
  1501. </para>
  1502. </note>
  1503. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1504. <phrase role="comment">// stack-allocator used for (de-)allocating stack</phrase>
  1505. <phrase role="identifier">fixedsize_stack</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">(</phrase><phrase role="number">4048</phrase><phrase role="special">);</phrase>
  1506. <phrase role="comment">// allocate stack space</phrase>
  1507. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">(</phrase><phrase role="identifier">salloc</phrase><phrase role="special">.</phrase><phrase role="identifier">allocate</phrase><phrase role="special">());</phrase>
  1508. <phrase role="comment">// reserve space for control structure on top of the stack</phrase>
  1509. <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">=</phrase><phrase role="keyword">static_cast</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">char</phrase><phrase role="special">*&gt;(</phrase><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">sp</phrase><phrase role="special">)-</phrase><phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">);</phrase>
  1510. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">=</phrase><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">-</phrase><phrase role="keyword">sizeof</phrase><phrase role="special">(</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">);</phrase>
  1511. <phrase role="comment">// placement new creates control structure on reserved space</phrase>
  1512. <phrase role="identifier">my_control_structure</phrase> <phrase role="special">*</phrase> <phrase role="identifier">cs</phrase><phrase role="special">=</phrase><phrase role="keyword">new</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">)</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">sctx</phrase><phrase role="special">,</phrase><phrase role="identifier">salloc</phrase><phrase role="special">);</phrase>
  1513. <phrase role="special">...</phrase>
  1514. <phrase role="comment">// destructing the control structure</phrase>
  1515. <phrase role="identifier">cs</phrase><phrase role="special">-&gt;~</phrase><phrase role="identifier">my_control_structure</phrase><phrase role="special">();</phrase>
  1516. <phrase role="special">...</phrase>
  1517. <phrase role="keyword">struct</phrase> <phrase role="identifier">my_control_structure</phrase> <phrase role="special">{</phrase>
  1518. <phrase role="comment">// captured continuation</phrase>
  1519. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">c</phrase><phrase role="special">;</phrase>
  1520. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">StackAllocator</phrase> <phrase role="special">&gt;</phrase>
  1521. <phrase role="identifier">my_control_structure</phrase><phrase role="special">(</phrase><phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">stack_context</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">,</phrase><phrase role="identifier">StackAllocator</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  1522. <phrase role="comment">// create captured continuation</phrase>
  1523. <phrase role="identifier">c</phrase><phrase role="special">{}</phrase> <phrase role="special">{</phrase>
  1524. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">callcc</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg</phrase><phrase role="special">,</phrase><phrase role="identifier">preallocated</phrase><phrase role="special">(</phrase><phrase role="identifier">sp</phrase><phrase role="special">,</phrase><phrase role="identifier">size</phrase><phrase role="special">,</phrase><phrase role="identifier">sctx</phrase><phrase role="special">),</phrase><phrase role="identifier">salloc</phrase><phrase role="special">,</phrase><phrase role="identifier">entry_func</phrase><phrase role="special">);</phrase>
  1525. <phrase role="special">}</phrase>
  1526. <phrase role="special">...</phrase>
  1527. <phrase role="special">};</phrase>
  1528. </programlisting>
  1529. <bridgehead renderas="sect3" id="context.cc.h8">
  1530. <phrase id="context.cc.inverting_the_control_flow"/><link linkend="context.cc.inverting_the_control_flow">Inverting
  1531. the control flow</link>
  1532. </bridgehead>
  1533. <programlisting><phrase role="keyword">namespace</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">=</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">context</phrase><phrase role="special">;</phrase>
  1534. <phrase role="comment">/*
  1535. * grammar:
  1536. * P ---&gt; E '\0'
  1537. * E ---&gt; T {('+'|'-') T}
  1538. * T ---&gt; S {('*'|'/') S}
  1539. * S ---&gt; digit | '(' E ')'
  1540. */</phrase>
  1541. <phrase role="keyword">class</phrase> <phrase role="identifier">Parser</phrase><phrase role="special">{</phrase>
  1542. <phrase role="keyword">char</phrase> <phrase role="identifier">next</phrase><phrase role="special">;</phrase>
  1543. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istream</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">is</phrase><phrase role="special">;</phrase>
  1544. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase><phrase role="special">)&gt;</phrase> <phrase role="identifier">cb</phrase><phrase role="special">;</phrase>
  1545. <phrase role="keyword">char</phrase> <phrase role="identifier">pull</phrase><phrase role="special">(){</phrase>
  1546. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">char_traits</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">char</phrase><phrase role="special">&gt;::</phrase><phrase role="identifier">to_char_type</phrase><phrase role="special">(</phrase><phrase role="identifier">is</phrase><phrase role="special">.</phrase><phrase role="identifier">get</phrase><phrase role="special">());</phrase>
  1547. <phrase role="special">}</phrase>
  1548. <phrase role="keyword">void</phrase> <phrase role="identifier">scan</phrase><phrase role="special">(){</phrase>
  1549. <phrase role="keyword">do</phrase><phrase role="special">{</phrase>
  1550. <phrase role="identifier">next</phrase><phrase role="special">=</phrase><phrase role="identifier">pull</phrase><phrase role="special">();</phrase>
  1551. <phrase role="special">}</phrase>
  1552. <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">isspace</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">));</phrase>
  1553. <phrase role="special">}</phrase>
  1554. <phrase role="keyword">public</phrase><phrase role="special">:</phrase>
  1555. <phrase role="identifier">Parser</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istream</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">is_</phrase><phrase role="special">,</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase><phrase role="special">)&gt;</phrase> <phrase role="identifier">cb_</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
  1556. <phrase role="identifier">next</phrase><phrase role="special">(),</phrase> <phrase role="identifier">is</phrase><phrase role="special">(</phrase><phrase role="identifier">is_</phrase><phrase role="special">),</phrase> <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">cb_</phrase><phrase role="special">)</phrase>
  1557. <phrase role="special">{}</phrase>
  1558. <phrase role="keyword">void</phrase> <phrase role="identifier">run</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
  1559. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1560. <phrase role="identifier">E</phrase><phrase role="special">();</phrase>
  1561. <phrase role="special">}</phrase>
  1562. <phrase role="keyword">private</phrase><phrase role="special">:</phrase>
  1563. <phrase role="keyword">void</phrase> <phrase role="identifier">E</phrase><phrase role="special">(){</phrase>
  1564. <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
  1565. <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'+'</phrase><phrase role="special">||</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'-'</phrase><phrase role="special">){</phrase>
  1566. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  1567. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1568. <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
  1569. <phrase role="special">}</phrase>
  1570. <phrase role="special">}</phrase>
  1571. <phrase role="keyword">void</phrase> <phrase role="identifier">T</phrase><phrase role="special">(){</phrase>
  1572. <phrase role="identifier">S</phrase><phrase role="special">();</phrase>
  1573. <phrase role="keyword">while</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'*'</phrase><phrase role="special">||</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'/'</phrase><phrase role="special">){</phrase>
  1574. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  1575. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1576. <phrase role="identifier">S</phrase><phrase role="special">();</phrase>
  1577. <phrase role="special">}</phrase>
  1578. <phrase role="special">}</phrase>
  1579. <phrase role="keyword">void</phrase> <phrase role="identifier">S</phrase><phrase role="special">(){</phrase>
  1580. <phrase role="keyword">if</phrase> <phrase role="special">(</phrase><phrase role="identifier">isdigit</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">)){</phrase>
  1581. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  1582. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1583. <phrase role="special">}</phrase>
  1584. <phrase role="keyword">else</phrase> <phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">'('</phrase><phrase role="special">){</phrase>
  1585. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  1586. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1587. <phrase role="identifier">E</phrase><phrase role="special">();</phrase>
  1588. <phrase role="keyword">if</phrase> <phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">==</phrase><phrase role="char">')'</phrase><phrase role="special">){</phrase>
  1589. <phrase role="identifier">cb</phrase><phrase role="special">(</phrase><phrase role="identifier">next</phrase><phrase role="special">);</phrase>
  1590. <phrase role="identifier">scan</phrase><phrase role="special">();</phrase>
  1591. <phrase role="special">}</phrase><phrase role="keyword">else</phrase><phrase role="special">{</phrase>
  1592. <phrase role="keyword">throw</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase><phrase role="string">&quot;parsing failed&quot;</phrase><phrase role="special">);</phrase>
  1593. <phrase role="special">}</phrase>
  1594. <phrase role="special">}</phrase>
  1595. <phrase role="keyword">else</phrase><phrase role="special">{</phrase>
  1596. <phrase role="keyword">throw</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">runtime_error</phrase><phrase role="special">(</phrase><phrase role="string">&quot;parsing failed&quot;</phrase><phrase role="special">);</phrase>
  1597. <phrase role="special">}</phrase>
  1598. <phrase role="special">}</phrase>
  1599. <phrase role="special">};</phrase>
  1600. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">istringstream</phrase> <phrase role="identifier">is</phrase><phrase role="special">(</phrase><phrase role="string">&quot;1+1&quot;</phrase><phrase role="special">);</phrase>
  1601. <phrase role="comment">// execute parser in new continuation</phrase>
  1602. <phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="identifier">source</phrase><phrase role="special">;</phrase>
  1603. <phrase role="comment">// user-code pulls parsed data from parser</phrase>
  1604. <phrase role="comment">// invert control flow</phrase>
  1605. <phrase role="keyword">char</phrase> <phrase role="identifier">c</phrase><phrase role="special">;</phrase>
  1606. <phrase role="keyword">bool</phrase> <phrase role="identifier">done</phrase><phrase role="special">=</phrase><phrase role="keyword">false</phrase><phrase role="special">;</phrase>
  1607. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">callcc</phrase><phrase role="special">(</phrase>
  1608. <phrase role="special">[&amp;</phrase><phrase role="identifier">is</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">c</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">done</phrase><phrase role="special">](</phrase><phrase role="identifier">ctx</phrase><phrase role="special">::</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">sink</phrase><phrase role="special">){</phrase>
  1609. <phrase role="comment">// create parser with callback function</phrase>
  1610. <phrase role="identifier">Parser</phrase> <phrase role="identifier">p</phrase><phrase role="special">(</phrase><phrase role="identifier">is</phrase><phrase role="special">,</phrase>
  1611. <phrase role="special">[&amp;</phrase><phrase role="identifier">sink</phrase><phrase role="special">,&amp;</phrase><phrase role="identifier">c</phrase><phrase role="special">](</phrase><phrase role="keyword">char</phrase> <phrase role="identifier">c_</phrase><phrase role="special">){</phrase>
  1612. <phrase role="comment">// resume main continuation</phrase>
  1613. <phrase role="identifier">c</phrase><phrase role="special">=</phrase><phrase role="identifier">c_</phrase><phrase role="special">;</phrase>
  1614. <phrase role="identifier">sink</phrase><phrase role="special">=</phrase><phrase role="identifier">sink</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1615. <phrase role="special">});</phrase>
  1616. <phrase role="comment">// start recursive parsing</phrase>
  1617. <phrase role="identifier">p</phrase><phrase role="special">.</phrase><phrase role="identifier">run</phrase><phrase role="special">();</phrase>
  1618. <phrase role="comment">// signal termination</phrase>
  1619. <phrase role="identifier">done</phrase><phrase role="special">=</phrase><phrase role="keyword">true</phrase><phrase role="special">;</phrase>
  1620. <phrase role="comment">// resume main continuation</phrase>
  1621. <phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">sink</phrase><phrase role="special">);</phrase>
  1622. <phrase role="special">});</phrase>
  1623. <phrase role="keyword">while</phrase><phrase role="special">(!</phrase><phrase role="identifier">done</phrase><phrase role="special">){</phrase>
  1624. <phrase role="identifier">printf</phrase><phrase role="special">(</phrase><phrase role="string">&quot;Parsed: %c\n&quot;</phrase><phrase role="special">,</phrase><phrase role="identifier">c</phrase><phrase role="special">);</phrase>
  1625. <phrase role="identifier">source</phrase><phrase role="special">=</phrase><phrase role="identifier">source</phrase><phrase role="special">.</phrase><phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1626. <phrase role="special">}</phrase>
  1627. <phrase role="identifier">output</phrase><phrase role="special">:</phrase>
  1628. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  1629. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="special">+</phrase>
  1630. <phrase role="identifier">Parsed</phrase><phrase role="special">:</phrase> <phrase role="number">1</phrase>
  1631. </programlisting>
  1632. <para>
  1633. In this example a recursive descent parser uses a callback to emit a newly
  1634. passed symbol. Using <emphasis>call/cc</emphasis> the control flow can be inverted,
  1635. e.g. the user-code pulls parsed symbols from the parser - instead to get pushed
  1636. from the parser (via callback).
  1637. </para>
  1638. <para>
  1639. The data (character) is transferred between the two continuations.
  1640. </para>
  1641. <section id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber">
  1642. <title><anchor id="implementation0"/><link linkend="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber">Implementations:
  1643. fcontext_t, ucontext_t and WinFiber</link></title>
  1644. <bridgehead renderas="sect4" id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.h0">
  1645. <phrase id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t"/><link
  1646. linkend="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.fcontext_t">fcontext_t</link>
  1647. </bridgehead>
  1648. <para>
  1649. The implementation uses <emphasis>fcontext_t</emphasis> per default. fcontext_t
  1650. is based on assembler and not available for all platforms. It provides a
  1651. much better performance than <emphasis>ucontext_t</emphasis> (the context
  1652. switch takes two magnitudes of order less CPU cycles; see section <link linkend="performance"><emphasis>performance</emphasis></link>)
  1653. and <emphasis>WinFiber</emphasis>.
  1654. </para>
  1655. <note>
  1656. <para>
  1657. Because the TIB (thread information block on Windows) is not fully described
  1658. in the MSDN, it might be possible that not all required TIB-parts are swapped.
  1659. Using WinFiber implementation migh be an alternative.
  1660. </para>
  1661. </note>
  1662. <bridgehead renderas="sect4" id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.h1">
  1663. <phrase id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t"/><link
  1664. linkend="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.ucontext_t">ucontext_t</link>
  1665. </bridgehead>
  1666. <para>
  1667. As an alternative, <ulink url="https://en.wikipedia.org/wiki/Setcontext"><emphasis>ucontext_t</emphasis></ulink>
  1668. can be used by compiling with <code><phrase role="identifier">BOOST_USE_UCONTEXT</phrase></code>
  1669. and b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase
  1670. role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">ucontext</phrase></code>.
  1671. <emphasis>ucontext_t</emphasis> might be available on a broader range of
  1672. POSIX-platforms but has some <link linkend="ucontext"><emphasis>disadvantages</emphasis></link>
  1673. (for instance deprecated since POSIX.1-2003, not C99 conform).
  1674. </para>
  1675. <note>
  1676. <para>
  1677. <link linkend="cc"><emphasis>callcc()</emphasis></link> supports <link
  1678. linkend="segmented"><emphasis>Segmented stacks</emphasis></link> only with
  1679. <emphasis>ucontext_t</emphasis> as its implementation.
  1680. </para>
  1681. </note>
  1682. <bridgehead renderas="sect4" id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.h2">
  1683. <phrase id="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber"/><link
  1684. linkend="context.cc.implementations__fcontext_t__ucontext_t_and_winfiber.winfiber">WinFiber</link>
  1685. </bridgehead>
  1686. <para>
  1687. With <code><phrase role="identifier">BOOST_USE_WINFIB</phrase></code> and
  1688. b2 property <code><phrase role="identifier">context</phrase><phrase role="special">-</phrase><phrase
  1689. role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">winfib</phrase></code>
  1690. Win32-Fibers are used as implementation for <link linkend="cc"><emphasis>callcc()</emphasis></link>.
  1691. </para>
  1692. <note>
  1693. <para>
  1694. The first call of <link linkend="cc"><emphasis>callcc()</emphasis></link>
  1695. converts the thread into a Windows fiber by invoking <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase
  1696. role="special">()</phrase></code>. If desired, <code><phrase role="identifier">ConvertFiberToThread</phrase><phrase
  1697. role="special">()</phrase></code> has to be called by the user explicitly
  1698. in order to release resources allocated by <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase
  1699. role="special">()</phrase></code> (e.g. after using boost.context).
  1700. </para>
  1701. </note>
  1702. </section>
  1703. <section id="context.cc.class__continuation_">
  1704. <title><link linkend="context.cc.class__continuation_">Class <code><phrase
  1705. role="identifier">continuation</phrase></code></link></title>
  1706. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">continuation</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  1707. <phrase role="keyword">class</phrase> <phrase role="identifier">continuation</phrase> <phrase role="special">{</phrase>
  1708. <phrase role="keyword">public</phrase><phrase role="special">:</phrase>
  1709. <phrase role="identifier">continuation</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="keyword">default</phrase><phrase role="special">;</phrase>
  1710. <phrase role="special">~</phrase><phrase role="identifier">continuation</phrase><phrase role="special">();</phrase>
  1711. <phrase role="identifier">continuation</phrase><phrase role="special">(</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1712. <phrase role="identifier">continuation</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1713. <phrase role="identifier">continuation</phrase><phrase role="special">(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
  1714. <phrase role="identifier">continuation</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
  1715. <phrase role="identifier">continuation</phrase> <phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1716. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  1717. <phrase role="identifier">continuation</phrase> <phrase role="identifier">resume_with</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  1718. <phrase role="keyword">explicit</phrase> <phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1719. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1720. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1721. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1722. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1723. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1724. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1725. <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1726. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase>
  1727. <phrase role="keyword">friend</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase>
  1728. <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">os</phrase><phrase role="special">,</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
  1729. <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1730. <phrase role="special">};</phrase>
  1731. </programlisting>
  1732. <para>
  1733. <bridgehead renderas="sect4" id="cc_constructor_bridgehead">
  1734. <phrase id="cc_constructor"/>
  1735. <link linkend="cc_constructor">Constructor</link>
  1736. </bridgehead>
  1737. </para>
  1738. <programlisting><phrase role="identifier">continuation</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1739. </programlisting>
  1740. <variablelist>
  1741. <title></title>
  1742. <varlistentry>
  1743. <term>Effects:</term>
  1744. <listitem>
  1745. <para>
  1746. Creates a invalid continuation.
  1747. </para>
  1748. </listitem>
  1749. </varlistentry>
  1750. <varlistentry>
  1751. <term>Throws:</term>
  1752. <listitem>
  1753. <para>
  1754. Nothing.
  1755. </para>
  1756. </listitem>
  1757. </varlistentry>
  1758. </variablelist>
  1759. <para>
  1760. <bridgehead renderas="sect4" id="cc_destructor destructor_bridgehead">
  1761. <phrase id="cc_destructor destructor"/>
  1762. <link linkend="cc_destructor
  1763. destructor">Destructor</link>
  1764. </bridgehead>
  1765. </para>
  1766. <programlisting><phrase role="special">~</phrase><phrase role="identifier">continuation</phrase><phrase role="special">();</phrase>
  1767. </programlisting>
  1768. <variablelist>
  1769. <title></title>
  1770. <varlistentry>
  1771. <term>Effects:</term>
  1772. <listitem>
  1773. <para>
  1774. Destructs the associated stack if <code><phrase role="special">*</phrase><phrase
  1775. role="keyword">this</phrase></code> is a valid continuation, e.g.
  1776. <emphasis>continuation::operator bool()</emphasis> returns <code><phrase
  1777. role="keyword">true</phrase></code>.
  1778. </para>
  1779. </listitem>
  1780. </varlistentry>
  1781. <varlistentry>
  1782. <term>Throws:</term>
  1783. <listitem>
  1784. <para>
  1785. Nothing.
  1786. </para>
  1787. </listitem>
  1788. </varlistentry>
  1789. </variablelist>
  1790. <para>
  1791. <bridgehead renderas="sect4" id="cc_move constructor_bridgehead">
  1792. <phrase id="cc_move constructor"/>
  1793. <link linkend="cc_move constructor">Move
  1794. constructor</link>
  1795. </bridgehead>
  1796. </para>
  1797. <programlisting><phrase role="identifier">continuation</phrase><phrase role="special">(</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1798. </programlisting>
  1799. <variablelist>
  1800. <title></title>
  1801. <varlistentry>
  1802. <term>Effects:</term>
  1803. <listitem>
  1804. <para>
  1805. Moves underlying capture continuation to <code><phrase role="special">*</phrase><phrase
  1806. role="keyword">this</phrase></code>.
  1807. </para>
  1808. </listitem>
  1809. </varlistentry>
  1810. <varlistentry>
  1811. <term>Throws:</term>
  1812. <listitem>
  1813. <para>
  1814. Nothing.
  1815. </para>
  1816. </listitem>
  1817. </varlistentry>
  1818. </variablelist>
  1819. <para>
  1820. <bridgehead renderas="sect4" id="cc_move assignment_bridgehead">
  1821. <phrase id="cc_move assignment"/>
  1822. <link linkend="cc_move assignment">Move assignment
  1823. operator</link>
  1824. </bridgehead>
  1825. </para>
  1826. <programlisting><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1827. </programlisting>
  1828. <variablelist>
  1829. <title></title>
  1830. <varlistentry>
  1831. <term>Effects:</term>
  1832. <listitem>
  1833. <para>
  1834. Moves the state of <code><phrase role="identifier">other</phrase></code>
  1835. to <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>
  1836. using move semantics.
  1837. </para>
  1838. </listitem>
  1839. </varlistentry>
  1840. <varlistentry>
  1841. <term>Throws:</term>
  1842. <listitem>
  1843. <para>
  1844. Nothing.
  1845. </para>
  1846. </listitem>
  1847. </varlistentry>
  1848. </variablelist>
  1849. <para>
  1850. <bridgehead renderas="sect4" id="cc_operator_call_bridgehead">
  1851. <phrase id="cc_operator_call"/>
  1852. <link linkend="cc_operator_call">Member function
  1853. <code>operator()</code>()</link>
  1854. </bridgehead>
  1855. </para>
  1856. <programlisting><phrase role="identifier">continuation</phrase> <phrase role="identifier">resume</phrase><phrase role="special">();</phrase>
  1857. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  1858. <phrase role="identifier">continuation</phrase> <phrase role="identifier">resume_with</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  1859. </programlisting>
  1860. <variablelist>
  1861. <title></title>
  1862. <varlistentry>
  1863. <term>Effects:</term>
  1864. <listitem>
  1865. <para>
  1866. Captures current continuation and resumes <code><phrase role="special">*</phrase><phrase
  1867. role="keyword">this</phrase></code>. The function <code><phrase role="identifier">resume_with</phrase></code>,
  1868. is used to execute function <code><phrase role="identifier">fn</phrase></code>
  1869. in the execution context of <code><phrase role="special">*</phrase><phrase
  1870. role="keyword">this</phrase></code> (e.g. the stack frame of <code><phrase
  1871. role="identifier">fn</phrase></code> is allocated on stack of <code><phrase
  1872. role="special">*</phrase><phrase role="keyword">this</phrase></code>).
  1873. </para>
  1874. </listitem>
  1875. </varlistentry>
  1876. <varlistentry>
  1877. <term>Returns:</term>
  1878. <listitem>
  1879. <para>
  1880. The continuation representing the continuation that has been suspended.
  1881. </para>
  1882. </listitem>
  1883. </varlistentry>
  1884. <varlistentry>
  1885. <term>Note:</term>
  1886. <listitem>
  1887. <para>
  1888. Function <code><phrase role="identifier">fn</phrase></code> needs to
  1889. return <code><phrase role="identifier">continuation</phrase></code>.
  1890. </para>
  1891. </listitem>
  1892. </varlistentry>
  1893. <varlistentry>
  1894. <term>Note:</term>
  1895. <listitem>
  1896. <para>
  1897. The returned continuation indicates if the suspended continuation has
  1898. terminated (return from context-function) via <code><phrase role="keyword">bool</phrase>
  1899. <phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code>.
  1900. </para>
  1901. </listitem>
  1902. </varlistentry>
  1903. </variablelist>
  1904. <para>
  1905. <bridgehead renderas="sect4" id="cc_operator_bool_bridgehead">
  1906. <phrase id="cc_operator_bool"/>
  1907. <link linkend="cc_operator_bool">Member function
  1908. <code>operator bool</code>()</link>
  1909. </bridgehead>
  1910. </para>
  1911. <programlisting><phrase role="keyword">explicit</phrase> <phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1912. </programlisting>
  1913. <variablelist>
  1914. <title></title>
  1915. <varlistentry>
  1916. <term>Returns:</term>
  1917. <listitem>
  1918. <para>
  1919. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  1920. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  1921. points to a captured continuation.
  1922. </para>
  1923. </listitem>
  1924. </varlistentry>
  1925. <varlistentry>
  1926. <term>Throws:</term>
  1927. <listitem>
  1928. <para>
  1929. Nothing.
  1930. </para>
  1931. </listitem>
  1932. </varlistentry>
  1933. </variablelist>
  1934. <para>
  1935. <bridgehead renderas="sect4" id="cc_operator_not_bridgehead">
  1936. <phrase id="cc_operator_not"/>
  1937. <link linkend="cc_operator_not">Member function <code>operator!</code>()</link>
  1938. </bridgehead>
  1939. </para>
  1940. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1941. </programlisting>
  1942. <variablelist>
  1943. <title></title>
  1944. <varlistentry>
  1945. <term>Returns:</term>
  1946. <listitem>
  1947. <para>
  1948. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  1949. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  1950. does not point to a captured continuation.
  1951. </para>
  1952. </listitem>
  1953. </varlistentry>
  1954. <varlistentry>
  1955. <term>Throws:</term>
  1956. <listitem>
  1957. <para>
  1958. Nothing.
  1959. </para>
  1960. </listitem>
  1961. </varlistentry>
  1962. </variablelist>
  1963. <para>
  1964. <bridgehead renderas="sect4" id="cc_operator_equal_bridgehead">
  1965. <phrase id="cc_operator_equal"/>
  1966. <link linkend="cc_operator_equal">Member function
  1967. <code>operator==</code>()</link>
  1968. </bridgehead>
  1969. </para>
  1970. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  1971. </programlisting>
  1972. <variablelist>
  1973. <title></title>
  1974. <varlistentry>
  1975. <term>Returns:</term>
  1976. <listitem>
  1977. <para>
  1978. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  1979. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  1980. and <code><phrase role="identifier">other</phrase></code> represent
  1981. the same continuation, <code><phrase role="keyword">false</phrase></code>
  1982. otherwise.
  1983. </para>
  1984. </listitem>
  1985. </varlistentry>
  1986. <varlistentry>
  1987. <term>Throws:</term>
  1988. <listitem>
  1989. <para>
  1990. Nothing.
  1991. </para>
  1992. </listitem>
  1993. </varlistentry>
  1994. </variablelist>
  1995. <para>
  1996. <bridgehead renderas="sect4" id="cc_operator_notequal_bridgehead">
  1997. <phrase id="cc_operator_notequal"/>
  1998. <link linkend="cc_operator_notequal">Member
  1999. function <code>operator!=</code>()</link>
  2000. </bridgehead>
  2001. </para>
  2002. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2003. </programlisting>
  2004. <variablelist>
  2005. <title></title>
  2006. <varlistentry>
  2007. <term>Returns:</term>
  2008. <listitem>
  2009. <para>
  2010. <code>! (other == * this)</code>
  2011. </para>
  2012. </listitem>
  2013. </varlistentry>
  2014. <varlistentry>
  2015. <term>Throws:</term>
  2016. <listitem>
  2017. <para>
  2018. Nothing.
  2019. </para>
  2020. </listitem>
  2021. </varlistentry>
  2022. </variablelist>
  2023. <para>
  2024. <bridgehead renderas="sect4" id="cc_operator_less_bridgehead">
  2025. <phrase id="cc_operator_less"/>
  2026. <link linkend="cc_operator_less">Member function
  2027. <code>operator&lt;</code>()</link>
  2028. </bridgehead>
  2029. </para>
  2030. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2031. </programlisting>
  2032. <variablelist>
  2033. <title></title>
  2034. <varlistentry>
  2035. <term>Returns:</term>
  2036. <listitem>
  2037. <para>
  2038. <code><phrase role="keyword">true</phrase></code> if <code><phrase
  2039. role="special">*</phrase><phrase role="keyword">this</phrase> <phrase
  2040. role="special">!=</phrase> <phrase role="identifier">other</phrase></code>
  2041. is true and the implementation-defined total order of <code><phrase
  2042. role="identifier">continuation</phrase></code> values places <code><phrase
  2043. role="special">*</phrase><phrase role="keyword">this</phrase></code>
  2044. before <code><phrase role="identifier">other</phrase></code>, false
  2045. otherwise.
  2046. </para>
  2047. </listitem>
  2048. </varlistentry>
  2049. <varlistentry>
  2050. <term>Throws:</term>
  2051. <listitem>
  2052. <para>
  2053. Nothing.
  2054. </para>
  2055. </listitem>
  2056. </varlistentry>
  2057. </variablelist>
  2058. <para>
  2059. <bridgehead renderas="sect4" id="cc_operator_greater_bridgehead">
  2060. <phrase id="cc_operator_greater"/>
  2061. <link linkend="cc_operator_greater">Member
  2062. function <code>operator&gt;</code>()</link>
  2063. </bridgehead>
  2064. </para>
  2065. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2066. </programlisting>
  2067. <variablelist>
  2068. <title></title>
  2069. <varlistentry>
  2070. <term>Returns:</term>
  2071. <listitem>
  2072. <para>
  2073. <code><phrase role="identifier">other</phrase> <phrase role="special">&lt;</phrase>
  2074. <phrase role="special">*</phrase> <phrase role="keyword">this</phrase></code>
  2075. </para>
  2076. </listitem>
  2077. </varlistentry>
  2078. <varlistentry>
  2079. <term>Throws:</term>
  2080. <listitem>
  2081. <para>
  2082. Nothing.
  2083. </para>
  2084. </listitem>
  2085. </varlistentry>
  2086. </variablelist>
  2087. <para>
  2088. <bridgehead renderas="sect4" id="cc_operator_lesseq_bridgehead">
  2089. <phrase id="cc_operator_lesseq"/>
  2090. <link linkend="cc_operator_lesseq">Member function
  2091. <code>operator&lt;=</code>()</link>
  2092. </bridgehead>
  2093. </para>
  2094. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&lt;=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2095. </programlisting>
  2096. <variablelist>
  2097. <title></title>
  2098. <varlistentry>
  2099. <term>Returns:</term>
  2100. <listitem>
  2101. <para>
  2102. <code><phrase role="special">!</phrase> <phrase role="special">(</phrase><phrase
  2103. role="identifier">other</phrase> <phrase role="special">&lt;</phrase>
  2104. <phrase role="special">*</phrase> <phrase role="keyword">this</phrase><phrase
  2105. role="special">)</phrase></code>
  2106. </para>
  2107. </listitem>
  2108. </varlistentry>
  2109. <varlistentry>
  2110. <term>Throws:</term>
  2111. <listitem>
  2112. <para>
  2113. Nothing.
  2114. </para>
  2115. </listitem>
  2116. </varlistentry>
  2117. </variablelist>
  2118. <para>
  2119. <bridgehead renderas="sect4" id="cc_operator_greatereq_bridgehead">
  2120. <phrase id="cc_operator_greatereq"/>
  2121. <link linkend="cc_operator_greatereq">Member
  2122. function <code>operator&gt;=</code>()</link>
  2123. </bridgehead>
  2124. </para>
  2125. <programlisting><phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">&gt;=(</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2126. </programlisting>
  2127. <variablelist>
  2128. <title></title>
  2129. <varlistentry>
  2130. <term>Returns:</term>
  2131. <listitem>
  2132. <para>
  2133. <code><phrase role="special">!</phrase> <phrase role="special">(*</phrase>
  2134. <phrase role="keyword">this</phrase> <phrase role="special">&lt;</phrase>
  2135. <phrase role="identifier">other</phrase><phrase role="special">)</phrase></code>
  2136. </para>
  2137. </listitem>
  2138. </varlistentry>
  2139. <varlistentry>
  2140. <term>Throws:</term>
  2141. <listitem>
  2142. <para>
  2143. Nothing.
  2144. </para>
  2145. </listitem>
  2146. </varlistentry>
  2147. </variablelist>
  2148. <para>
  2149. <bridgehead renderas="sect4" id="cc__bridgehead">
  2150. <phrase id="cc_"/>
  2151. <link linkend="cc_">Non-member function <code>operator&lt;&lt;()</code></link>
  2152. </bridgehead>
  2153. </para>
  2154. <programlisting><phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="keyword">class</phrase> <phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase>
  2155. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase>
  2156. <phrase role="keyword">operator</phrase><phrase role="special">&lt;&lt;(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">basic_ostream</phrase><phrase role="special">&lt;</phrase><phrase role="identifier">charT</phrase><phrase role="special">,</phrase><phrase role="identifier">traitsT</phrase><phrase role="special">&gt;</phrase> <phrase role="special">&amp;</phrase> <phrase role="identifier">os</phrase><phrase role="special">,</phrase><phrase role="identifier">continuation</phrase> <phrase role="keyword">const</phrase><phrase role="special">&amp;</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase>
  2157. </programlisting>
  2158. <variablelist>
  2159. <title></title>
  2160. <varlistentry>
  2161. <term>Effects:</term>
  2162. <listitem>
  2163. <para>
  2164. Writes the representation of <code><phrase role="identifier">other</phrase></code>
  2165. to stream <code><phrase role="identifier">os</phrase></code>.
  2166. </para>
  2167. </listitem>
  2168. </varlistentry>
  2169. <varlistentry>
  2170. <term>Returns:</term>
  2171. <listitem>
  2172. <para>
  2173. <code><phrase role="identifier">os</phrase></code>
  2174. </para>
  2175. </listitem>
  2176. </varlistentry>
  2177. </variablelist>
  2178. <bridgehead renderas="sect4" id="context.cc.class__continuation_.h0">
  2179. <phrase id="context.cc.class__continuation_.call_with_current_continuation"/><link
  2180. linkend="context.cc.class__continuation_.call_with_current_continuation">Call
  2181. with current continuation</link>
  2182. </bridgehead>
  2183. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">continuation</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2184. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  2185. <phrase role="identifier">continuation</phrase> <phrase role="identifier">callcc</phrase><phrase role="special">(</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  2186. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">StackAlloc</phrase><phrase role="special">,</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  2187. <phrase role="identifier">continuation</phrase> <phrase role="identifier">callcc</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase><phrase role="special">,</phrase><phrase role="identifier">StackAlloc</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  2188. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">StackAlloc</phrase><phrase role="special">,</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Fn</phrase><phrase role="special">&gt;</phrase>
  2189. <phrase role="identifier">continuation</phrase> <phrase role="identifier">callcc</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator_arg_t</phrase><phrase role="special">,</phrase><phrase role="identifier">preallocated</phrase> <phrase role="identifier">palloc</phrase><phrase role="special">,</phrase><phrase role="identifier">StackAlloc</phrase> <phrase role="identifier">salloc</phrase><phrase role="special">,</phrase><phrase role="identifier">Fn</phrase> <phrase role="special">&amp;&amp;</phrase> <phrase role="identifier">fn</phrase><phrase role="special">);</phrase>
  2190. </programlisting>
  2191. <variablelist>
  2192. <title></title>
  2193. <varlistentry>
  2194. <term>Effects:</term>
  2195. <listitem>
  2196. <para>
  2197. Captures current continuation and creates a new continuation prepared
  2198. to execute <code><phrase role="identifier">fn</phrase></code>. <code><phrase
  2199. role="identifier">fixedsize_stack</phrase></code> is used as default
  2200. stack allocator (stack size == fixedsize_stack::traits::default_size()).
  2201. The function with argument type <code><phrase role="identifier">preallocated</phrase></code>,
  2202. is used to create a user defined data <link linkend="cc_prealloc">(for
  2203. instance additional control structures)</link> on top of the stack.
  2204. </para>
  2205. </listitem>
  2206. </varlistentry>
  2207. <varlistentry>
  2208. <term>Returns:</term>
  2209. <listitem>
  2210. <para>
  2211. The continuation representing the contexcontinuation that has been
  2212. suspended.
  2213. </para>
  2214. </listitem>
  2215. </varlistentry>
  2216. <varlistentry>
  2217. <term>Note:</term>
  2218. <listitem>
  2219. <para>
  2220. The returned continuation indicates if the suspended continuation has
  2221. terminated (return from context-function) via <code><phrase role="keyword">bool</phrase>
  2222. <phrase role="keyword">operator</phrase><phrase role="special">()</phrase></code>.
  2223. </para>
  2224. </listitem>
  2225. </varlistentry>
  2226. </variablelist>
  2227. </section>
  2228. </section>
  2229. <section id="context.stack">
  2230. <title><anchor id="stack"/><link linkend="context.stack">Stack allocation</link></title>
  2231. <para>
  2232. The memory used by the stack is allocated/deallocated via a <emphasis>StackAllocator</emphasis>
  2233. which is required to model a <emphasis>stack-allocator concept</emphasis>.
  2234. </para>
  2235. <bridgehead renderas="sect3" id="context.stack.h0">
  2236. <phrase id="context.stack._emphasis_stack_allocator_concept__emphasis_"/><link
  2237. linkend="context.stack._emphasis_stack_allocator_concept__emphasis_"><emphasis>stack-allocator
  2238. concept</emphasis></link>
  2239. </bridgehead>
  2240. <para>
  2241. A <emphasis>StackAllocator</emphasis> must satisfy the <emphasis>stack-allocator
  2242. concept</emphasis> requirements shown in the following table, in which <code><phrase
  2243. role="identifier">a</phrase></code> is an object of a <emphasis>StackAllocator</emphasis>
  2244. type, <code><phrase role="identifier">sctx</phrase></code> is a <code><phrase
  2245. role="identifier">stack_context</phrase></code>, and <code><phrase role="identifier">size</phrase></code>
  2246. is a <code><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase
  2247. role="identifier">size_t</phrase></code>:
  2248. </para>
  2249. <informaltable frame="all">
  2250. <tgroup cols="3">
  2251. <thead>
  2252. <row>
  2253. <entry>
  2254. <para>
  2255. expression
  2256. </para>
  2257. </entry>
  2258. <entry>
  2259. <para>
  2260. return type
  2261. </para>
  2262. </entry>
  2263. <entry>
  2264. <para>
  2265. notes
  2266. </para>
  2267. </entry>
  2268. </row>
  2269. </thead>
  2270. <tbody>
  2271. <row>
  2272. <entry>
  2273. <para>
  2274. <code><phrase role="identifier">a</phrase><phrase role="special">(</phrase><phrase
  2275. role="identifier">size</phrase><phrase role="special">)</phrase></code>
  2276. </para>
  2277. </entry>
  2278. <entry>
  2279. </entry>
  2280. <entry>
  2281. <para>
  2282. creates a stack allocator
  2283. </para>
  2284. </entry>
  2285. </row>
  2286. <row>
  2287. <entry>
  2288. <para>
  2289. <code><phrase role="identifier">a</phrase><phrase role="special">.</phrase><phrase
  2290. role="identifier">allocate</phrase><phrase role="special">()</phrase></code>
  2291. </para>
  2292. </entry>
  2293. <entry>
  2294. <para>
  2295. <code><phrase role="identifier">stack_context</phrase></code>
  2296. </para>
  2297. </entry>
  2298. <entry>
  2299. <para>
  2300. creates a stack
  2301. </para>
  2302. </entry>
  2303. </row>
  2304. <row>
  2305. <entry>
  2306. <para>
  2307. <code><phrase role="identifier">a</phrase><phrase role="special">.</phrase><phrase
  2308. role="identifier">deallocate</phrase><phrase role="special">(</phrase>
  2309. <phrase role="identifier">sctx</phrase><phrase role="special">)</phrase></code>
  2310. </para>
  2311. </entry>
  2312. <entry>
  2313. <para>
  2314. <code><phrase role="keyword">void</phrase></code>
  2315. </para>
  2316. </entry>
  2317. <entry>
  2318. <para>
  2319. deallocates the stack created by <code><phrase role="identifier">a</phrase><phrase
  2320. role="special">.</phrase><phrase role="identifier">allocate</phrase><phrase
  2321. role="special">()</phrase></code>
  2322. </para>
  2323. </entry>
  2324. </row>
  2325. </tbody>
  2326. </tgroup>
  2327. </informaltable>
  2328. <important>
  2329. <para>
  2330. The implementation of <code><phrase role="identifier">allocate</phrase><phrase
  2331. role="special">()</phrase></code> might include logic to protect against
  2332. exceeding the context's available stack size rather than leaving it as undefined
  2333. behaviour.
  2334. </para>
  2335. </important>
  2336. <important>
  2337. <para>
  2338. Calling <code><phrase role="identifier">deallocate</phrase><phrase role="special">()</phrase></code>
  2339. with a <code><phrase role="identifier">stack_context</phrase></code> not
  2340. set by <code><phrase role="identifier">allocate</phrase><phrase role="special">()</phrase></code>
  2341. results in undefined behaviour.
  2342. </para>
  2343. </important>
  2344. <note>
  2345. <para>
  2346. Depending on the architecture <code><phrase role="identifier">allocate</phrase><phrase
  2347. role="special">()</phrase></code> stores an address from the top of the stack
  2348. (growing downwards) or the bottom of the stack (growing upwards).
  2349. </para>
  2350. </note>
  2351. <section id="context.stack.protected_fixedsize">
  2352. <title><link linkend="context.stack.protected_fixedsize">Class <emphasis>protected_fixedsize</emphasis></link></title>
  2353. <para>
  2354. <emphasis role="bold">Boost.Context</emphasis> provides the class <emphasis>protected_fixedsize_stack</emphasis>
  2355. which models the <emphasis>stack-allocator concept</emphasis>. It appends
  2356. a guard page at the end of each stack to protect against exceeding the stack.
  2357. If the guard page is accessed (read or write operation) a segmentation fault/access
  2358. violation is generated by the operating system.
  2359. </para>
  2360. <important>
  2361. <para>
  2362. Using <emphasis>protected_fixedsize_stack</emphasis> is expensive. That
  2363. is, launching a new coroutine with a new stack is expensive; the allocated
  2364. stack is just as efficient to use as any other stack.
  2365. </para>
  2366. </important>
  2367. <note>
  2368. <para>
  2369. The appended <code><phrase role="identifier">guard</phrase> <phrase role="identifier">page</phrase></code>
  2370. is <emphasis role="bold">not</emphasis> mapped to physical memory, only
  2371. virtual addresses are used.
  2372. </para>
  2373. </note>
  2374. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">protected_fixedsize</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2375. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase>
  2376. <phrase role="keyword">struct</phrase> <phrase role="identifier">basic_protected_fixedsize</phrase> <phrase role="special">{</phrase>
  2377. <phrase role="keyword">typedef</phrase> <phrase role="identifier">traitT</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">;</phrase>
  2378. <phrase role="identifier">basic_protected_fixesize</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase>
  2379. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase>
  2380. <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase>
  2381. <phrase role="special">}</phrase>
  2382. <phrase role="keyword">typedef</phrase> <phrase role="identifier">basic_protected_fixedsize</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">stack_traits</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">protected_fixedsize</phrase>
  2383. </programlisting>
  2384. <bridgehead renderas="sect4" id="context.stack.protected_fixedsize.h0">
  2385. <phrase id="context.stack.protected_fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"/><link
  2386. linkend="context.stack.protected_fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"><code><phrase
  2387. role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase
  2388. role="special">()</phrase></code></link>
  2389. </bridgehead>
  2390. <variablelist>
  2391. <title></title>
  2392. <varlistentry>
  2393. <term>Preconditions:</term>
  2394. <listitem>
  2395. <para>
  2396. <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2397. role="identifier">minimum</phrase><phrase role="special">:</phrase><phrase
  2398. role="identifier">size</phrase><phrase role="special">()</phrase>
  2399. <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code>
  2400. and <code><phrase role="special">!</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2401. role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase
  2402. role="special">()</phrase> <phrase role="special">&amp;&amp;</phrase>
  2403. <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2404. role="special">::</phrase><phrase role="identifier">maximum</phrase><phrase
  2405. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2406. role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase
  2407. role="identifier">size</phrase><phrase role="special">)</phrase></code>.
  2408. </para>
  2409. </listitem>
  2410. </varlistentry>
  2411. <varlistentry>
  2412. <term>Effects:</term>
  2413. <listitem>
  2414. <para>
  2415. Allocates memory of at least <code><phrase role="identifier">size</phrase></code>
  2416. Bytes and stores a pointer to the stack and its actual size in <code><phrase
  2417. role="identifier">sctx</phrase></code>. Depending on the architecture
  2418. (the stack grows downwards/upwards) the stored address is the highest/lowest
  2419. address of the stack.
  2420. </para>
  2421. </listitem>
  2422. </varlistentry>
  2423. </variablelist>
  2424. <bridgehead renderas="sect4" id="context.stack.protected_fixedsize.h1">
  2425. <phrase id="context.stack.protected_fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"/><link
  2426. linkend="context.stack.protected_fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"><code><phrase
  2427. role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase
  2428. role="special">(</phrase> <phrase role="identifier">stack_context</phrase>
  2429. <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase
  2430. role="special">)</phrase></code></link>
  2431. </bridgehead>
  2432. <variablelist>
  2433. <title></title>
  2434. <varlistentry>
  2435. <term>Preconditions:</term>
  2436. <listitem>
  2437. <para>
  2438. <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2439. role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase
  2440. role="special">::</phrase><phrase role="identifier">minimum</phrase><phrase
  2441. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2442. role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase
  2443. role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2444. role="identifier">size</phrase></code> and <code><phrase role="special">!</phrase>
  2445. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2446. role="identifier">is_unbounded</phrase><phrase role="special">()</phrase>
  2447. <phrase role="special">&amp;&amp;</phrase> <phrase role="special">(</phrase>
  2448. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2449. role="identifier">maximum</phrase><phrase role="special">:</phrase><phrase
  2450. role="identifier">size</phrase><phrase role="special">()</phrase>
  2451. <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase
  2452. role="special">.</phrase><phrase role="identifier">size</phrase><phrase
  2453. role="special">)</phrase></code>.
  2454. </para>
  2455. </listitem>
  2456. </varlistentry>
  2457. <varlistentry>
  2458. <term>Effects:</term>
  2459. <listitem>
  2460. <para>
  2461. Deallocates the stack space.
  2462. </para>
  2463. </listitem>
  2464. </varlistentry>
  2465. </variablelist>
  2466. </section>
  2467. <section id="context.stack.pooled_fixedsize">
  2468. <title><link linkend="context.stack.pooled_fixedsize">Class <emphasis>pooled_fixedsize_stack</emphasis></link></title>
  2469. <para>
  2470. <emphasis role="bold">Boost.Context</emphasis> provides the class <emphasis>pooled_fixedsize_stack</emphasis>
  2471. which models the <emphasis>stack-allocator concept</emphasis>. In contrast
  2472. to <emphasis>protected_fixedsize_stack</emphasis> it does not append a guard
  2473. page at the end of each stack. The memory is managed internally by <ulink
  2474. url="http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html"><code><phrase
  2475. role="identifier">boost</phrase><phrase role="special">::</phrase><phrase
  2476. role="identifier">pool</phrase><phrase role="special">&lt;&gt;</phrase></code></ulink>.
  2477. </para>
  2478. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">pooled_fixedsize_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2479. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase>
  2480. <phrase role="keyword">struct</phrase> <phrase role="identifier">basic_pooled_fixedsize_stack</phrase> <phrase role="special">{</phrase>
  2481. <phrase role="keyword">typedef</phrase> <phrase role="identifier">traitT</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">;</phrase>
  2482. <phrase role="identifier">basic_pooled_fixedsize_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">stack_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">(),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">next_size</phrase> <phrase role="special">=</phrase> <phrase role="number">32</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">max_size</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
  2483. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase>
  2484. <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase>
  2485. <phrase role="special">}</phrase>
  2486. <phrase role="keyword">typedef</phrase> <phrase role="identifier">basic_pooled_fixedsize_stack</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">stack_traits</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">pooled_fixedsize_stack</phrase><phrase role="special">;</phrase>
  2487. </programlisting>
  2488. <bridgehead renderas="sect4" id="context.stack.pooled_fixedsize.h0">
  2489. <phrase id="context.stack.pooled_fixedsize._code__phrase_role__identifier__basic_pooled_fixedsize_stack__phrase__phrase_role__special_____phrase__phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__stack_size__phrase__phrase_role__special_____phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__next_size__phrase__phrase_role__special_____phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__max_size__phrase__phrase_role__special_____phrase___code_"/><link
  2490. linkend="context.stack.pooled_fixedsize._code__phrase_role__identifier__basic_pooled_fixedsize_stack__phrase__phrase_role__special_____phrase__phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__stack_size__phrase__phrase_role__special_____phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__next_size__phrase__phrase_role__special_____phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__max_size__phrase__phrase_role__special_____phrase___code_"><code><phrase
  2491. role="identifier">basic_pooled_fixedsize_stack</phrase><phrase role="special">(</phrase><phrase
  2492. role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase>
  2493. <phrase role="identifier">stack_size</phrase><phrase role="special">,</phrase>
  2494. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase
  2495. role="identifier">size_t</phrase> <phrase role="identifier">next_size</phrase><phrase
  2496. role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase
  2497. role="identifier">size_t</phrase> <phrase role="identifier">max_size</phrase><phrase
  2498. role="special">)</phrase></code></link>
  2499. </bridgehead>
  2500. <variablelist>
  2501. <title></title>
  2502. <varlistentry>
  2503. <term>Preconditions:</term>
  2504. <listitem>
  2505. <para>
  2506. <code><phrase role="special">!</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2507. role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase
  2508. role="special">()</phrase> <phrase role="special">&amp;&amp;</phrase>
  2509. <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2510. role="special">::</phrase><phrase role="identifier">maximum</phrase><phrase
  2511. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2512. role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase
  2513. role="identifier">stack_size</phrase><phrase role="special">)</phrase></code>
  2514. and <code><phrase role="number">0</phrase> <phrase role="special">&lt;</phrase>
  2515. <phrase role="identifier">nest_size</phrase></code>.
  2516. </para>
  2517. </listitem>
  2518. </varlistentry>
  2519. <varlistentry>
  2520. <term>Effects:</term>
  2521. <listitem>
  2522. <para>
  2523. Allocates memory of at least <code><phrase role="identifier">stack_size</phrase></code>
  2524. Bytes and stores a pointer to the stack and its actual size in <code><phrase
  2525. role="identifier">sctx</phrase></code>. Depending on the architecture
  2526. (the stack grows downwards/upwards) the stored address is the highest/lowest
  2527. address of the stack. Argument <code><phrase role="identifier">next_size</phrase></code>
  2528. determines the number of stacks to request from the system the first
  2529. time that <code><phrase role="special">*</phrase><phrase role="keyword">this</phrase></code>
  2530. needs to allocate system memory. The third argument <code><phrase role="identifier">max_size</phrase></code>
  2531. controls how many memory might be allocated for stacks - a value of
  2532. zero means no uper limit.
  2533. </para>
  2534. </listitem>
  2535. </varlistentry>
  2536. </variablelist>
  2537. <bridgehead renderas="sect4" id="context.stack.pooled_fixedsize.h1">
  2538. <phrase id="context.stack.pooled_fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"/><link
  2539. linkend="context.stack.pooled_fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"><code><phrase
  2540. role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase
  2541. role="special">()</phrase></code></link>
  2542. </bridgehead>
  2543. <variablelist>
  2544. <title></title>
  2545. <varlistentry>
  2546. <term>Preconditions:</term>
  2547. <listitem>
  2548. <para>
  2549. <code><phrase role="special">!</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2550. role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase
  2551. role="special">()</phrase> <phrase role="special">&amp;&amp;</phrase>
  2552. <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2553. role="special">::</phrase><phrase role="identifier">maximum</phrase><phrase
  2554. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2555. role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase
  2556. role="identifier">stack_size</phrase><phrase role="special">)</phrase></code>.
  2557. </para>
  2558. </listitem>
  2559. </varlistentry>
  2560. <varlistentry>
  2561. <term>Effects:</term>
  2562. <listitem>
  2563. <para>
  2564. Allocates memory of at least <code><phrase role="identifier">stack_size</phrase></code>
  2565. Bytes and stores a pointer to the stack and its actual size in <code><phrase
  2566. role="identifier">sctx</phrase></code>. Depending on the architecture
  2567. (the stack grows downwards/upwards) the stored address is the highest/lowest
  2568. address of the stack.
  2569. </para>
  2570. </listitem>
  2571. </varlistentry>
  2572. </variablelist>
  2573. <bridgehead renderas="sect4" id="context.stack.pooled_fixedsize.h2">
  2574. <phrase id="context.stack.pooled_fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"/><link
  2575. linkend="context.stack.pooled_fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"><code><phrase
  2576. role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase
  2577. role="special">(</phrase> <phrase role="identifier">stack_context</phrase>
  2578. <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase
  2579. role="special">)</phrase></code></link>
  2580. </bridgehead>
  2581. <variablelist>
  2582. <title></title>
  2583. <varlistentry>
  2584. <term>Preconditions:</term>
  2585. <listitem>
  2586. <para>
  2587. <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2588. role="identifier">sp</phrase></code> is valid, <code><phrase role="special">!</phrase>
  2589. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2590. role="identifier">is_unbounded</phrase><phrase role="special">()</phrase>
  2591. <phrase role="special">&amp;&amp;</phrase> <phrase role="special">(</phrase>
  2592. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2593. role="identifier">maximum</phrase><phrase role="special">:</phrase><phrase
  2594. role="identifier">size</phrase><phrase role="special">()</phrase>
  2595. <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase
  2596. role="special">.</phrase><phrase role="identifier">size</phrase><phrase
  2597. role="special">)</phrase></code>.
  2598. </para>
  2599. </listitem>
  2600. </varlistentry>
  2601. <varlistentry>
  2602. <term>Effects:</term>
  2603. <listitem>
  2604. <para>
  2605. Deallocates the stack space.
  2606. </para>
  2607. </listitem>
  2608. </varlistentry>
  2609. </variablelist>
  2610. </section>
  2611. <section id="context.stack.fixedsize">
  2612. <title><link linkend="context.stack.fixedsize">Class <emphasis>fixedsize_stack</emphasis></link></title>
  2613. <para>
  2614. <emphasis role="bold">Boost.Context</emphasis> provides the class <emphasis>fixedsize_stack</emphasis>
  2615. which models the <emphasis>stack-allocator concept</emphasis>. In contrast
  2616. to <emphasis>protected_fixedsize_stack</emphasis> it does not append a guard
  2617. page at the end of each stack. The memory is simply managed by <code><phrase
  2618. role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">malloc</phrase><phrase
  2619. role="special">()</phrase></code> and <code><phrase role="identifier">std</phrase><phrase
  2620. role="special">::</phrase><phrase role="identifier">free</phrase><phrase
  2621. role="special">()</phrase></code>.
  2622. </para>
  2623. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">fixedsize_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2624. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase>
  2625. <phrase role="keyword">struct</phrase> <phrase role="identifier">basic_fixedsize_stack</phrase> <phrase role="special">{</phrase>
  2626. <phrase role="keyword">typedef</phrase> <phrase role="identifier">traitT</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">;</phrase>
  2627. <phrase role="identifier">basic_fixesize_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase>
  2628. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase>
  2629. <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase>
  2630. <phrase role="special">}</phrase>
  2631. <phrase role="keyword">typedef</phrase> <phrase role="identifier">basic_fixedsize_stack</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">stack_traits</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">fixedsize_stack</phrase><phrase role="special">;</phrase>
  2632. </programlisting>
  2633. <bridgehead renderas="sect4" id="context.stack.fixedsize.h0">
  2634. <phrase id="context.stack.fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"/><link
  2635. linkend="context.stack.fixedsize._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"><code><phrase
  2636. role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase
  2637. role="special">()</phrase></code></link>
  2638. </bridgehead>
  2639. <variablelist>
  2640. <title></title>
  2641. <varlistentry>
  2642. <term>Preconditions:</term>
  2643. <listitem>
  2644. <para>
  2645. <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2646. role="identifier">minimum</phrase><phrase role="special">:</phrase><phrase
  2647. role="identifier">size</phrase><phrase role="special">()</phrase>
  2648. <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code>
  2649. and <code><phrase role="special">!</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2650. role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase
  2651. role="special">()</phrase> <phrase role="special">&amp;&amp;</phrase>
  2652. <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2653. role="special">::</phrase><phrase role="identifier">maximum</phrase><phrase
  2654. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2655. role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase
  2656. role="identifier">size</phrase><phrase role="special">)</phrase></code>.
  2657. </para>
  2658. </listitem>
  2659. </varlistentry>
  2660. <varlistentry>
  2661. <term>Effects:</term>
  2662. <listitem>
  2663. <para>
  2664. Allocates memory of at least <code><phrase role="identifier">size</phrase></code>
  2665. Bytes and stores a pointer to the stack and its actual size in <code><phrase
  2666. role="identifier">sctx</phrase></code>. Depending on the architecture
  2667. (the stack grows downwards/upwards) the stored address is the highest/lowest
  2668. address of the stack.
  2669. </para>
  2670. </listitem>
  2671. </varlistentry>
  2672. </variablelist>
  2673. <bridgehead renderas="sect4" id="context.stack.fixedsize.h1">
  2674. <phrase id="context.stack.fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"/><link
  2675. linkend="context.stack.fixedsize._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"><code><phrase
  2676. role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase
  2677. role="special">(</phrase> <phrase role="identifier">stack_context</phrase>
  2678. <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase
  2679. role="special">)</phrase></code></link>
  2680. </bridgehead>
  2681. <variablelist>
  2682. <title></title>
  2683. <varlistentry>
  2684. <term>Preconditions:</term>
  2685. <listitem>
  2686. <para>
  2687. <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2688. role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase
  2689. role="special">::</phrase><phrase role="identifier">minimum</phrase><phrase
  2690. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2691. role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase
  2692. role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2693. role="identifier">size</phrase></code> and <code><phrase role="special">!</phrase>
  2694. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2695. role="identifier">is_unbounded</phrase><phrase role="special">()</phrase>
  2696. <phrase role="special">&amp;&amp;</phrase> <phrase role="special">(</phrase>
  2697. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2698. role="identifier">maximum</phrase><phrase role="special">:</phrase><phrase
  2699. role="identifier">size</phrase><phrase role="special">()</phrase>
  2700. <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase
  2701. role="special">.</phrase><phrase role="identifier">size</phrase><phrase
  2702. role="special">)</phrase></code>.
  2703. </para>
  2704. </listitem>
  2705. </varlistentry>
  2706. <varlistentry>
  2707. <term>Effects:</term>
  2708. <listitem>
  2709. <para>
  2710. Deallocates the stack space.
  2711. </para>
  2712. </listitem>
  2713. </varlistentry>
  2714. </variablelist>
  2715. </section>
  2716. <section id="context.stack.segmented">
  2717. <title><anchor id="segmented"/><link linkend="context.stack.segmented">Class
  2718. <emphasis>segmented_stack</emphasis></link></title>
  2719. <para>
  2720. <emphasis role="bold">Boost.Context</emphasis> supports usage of a <link
  2721. linkend="segmented"><emphasis>segmented_stack</emphasis></link>, e. g. the
  2722. size of the stack grows on demand. The coroutine is created with a minimal
  2723. stack size and will be increased as required. Class <link linkend="segmented"><emphasis>segmented_stack</emphasis></link>
  2724. models the <emphasis>stack-allocator concept</emphasis>. In contrast to
  2725. <emphasis>protected_fixedsize_stack</emphasis> and <emphasis>fixedsize_stack</emphasis>
  2726. it creates a stack which grows on demand.
  2727. </para>
  2728. <note>
  2729. <para>
  2730. Segmented stacks are currently only supported by <emphasis role="bold">gcc</emphasis>
  2731. from version <emphasis role="bold">4.7</emphasis> <emphasis role="bold">clang</emphasis>
  2732. from version <emphasis role="bold">3.4</emphasis> onwards. In order to
  2733. use a <emphasis>segmented_stack</emphasis> <emphasis role="bold">Boost.Context</emphasis>
  2734. must be built with property <code><phrase role="identifier">segmented</phrase><phrase
  2735. role="special">-</phrase><phrase role="identifier">stacks</phrase></code>,
  2736. e.g. <emphasis role="bold">toolset=gcc segmented-stacks=on</emphasis> and
  2737. applying <code><phrase role="identifier">BOOST_USE_SEGMENTED_STACKS</phrase></code>
  2738. at b2/bjam command line.
  2739. </para>
  2740. </note>
  2741. <note>
  2742. <para>
  2743. Segmented stacks can only be used with <link linkend="cc"><emphasis>callcc()</emphasis></link>
  2744. (using <link linkend="implementation"><emphasis>ucontext_t</emphasis></link>)
  2745. </para>
  2746. </note>
  2747. <para>
  2748. .
  2749. </para>
  2750. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">segmented_stack</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2751. <phrase role="keyword">template</phrase><phrase role="special">&lt;</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">traitsT</phrase> <phrase role="special">&gt;</phrase>
  2752. <phrase role="keyword">struct</phrase> <phrase role="identifier">basic_segmented_stack</phrase> <phrase role="special">{</phrase>
  2753. <phrase role="keyword">typedef</phrase> <phrase role="identifier">traitT</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">;</phrase>
  2754. <phrase role="identifier">basic_segmented_stack</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase role="identifier">default_size</phrase><phrase role="special">());</phrase>
  2755. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase role="special">();</phrase>
  2756. <phrase role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase role="special">(</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">&amp;);</phrase>
  2757. <phrase role="special">}</phrase>
  2758. <phrase role="keyword">typedef</phrase> <phrase role="identifier">basic_segmented_stack</phrase><phrase role="special">&lt;</phrase> <phrase role="identifier">stack_traits</phrase> <phrase role="special">&gt;</phrase> <phrase role="identifier">segmented_stack</phrase><phrase role="special">;</phrase>
  2759. </programlisting>
  2760. <bridgehead renderas="sect4" id="context.stack.segmented.h0">
  2761. <phrase id="context.stack.segmented._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"/><link
  2762. linkend="context.stack.segmented._code__phrase_role__identifier__stack_context__phrase___phrase_role__identifier__allocate__phrase__phrase_role__special______phrase___code_"><code><phrase
  2763. role="identifier">stack_context</phrase> <phrase role="identifier">allocate</phrase><phrase
  2764. role="special">()</phrase></code></link>
  2765. </bridgehead>
  2766. <variablelist>
  2767. <title></title>
  2768. <varlistentry>
  2769. <term>Preconditions:</term>
  2770. <listitem>
  2771. <para>
  2772. <code><phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2773. role="identifier">minimum</phrase><phrase role="special">:</phrase><phrase
  2774. role="identifier">size</phrase><phrase role="special">()</phrase>
  2775. <phrase role="special">&lt;=</phrase> <phrase role="identifier">size</phrase></code>
  2776. and <code><phrase role="special">!</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2777. role="special">::</phrase><phrase role="identifier">is_unbounded</phrase><phrase
  2778. role="special">()</phrase> <phrase role="special">&amp;&amp;</phrase>
  2779. <phrase role="special">(</phrase> <phrase role="identifier">traits_type</phrase><phrase
  2780. role="special">::</phrase><phrase role="identifier">maximum</phrase><phrase
  2781. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2782. role="special">()</phrase> <phrase role="special">&gt;=</phrase> <phrase
  2783. role="identifier">size</phrase><phrase role="special">)</phrase></code>.
  2784. </para>
  2785. </listitem>
  2786. </varlistentry>
  2787. <varlistentry>
  2788. <term>Effects:</term>
  2789. <listitem>
  2790. <para>
  2791. Allocates memory of at least <code><phrase role="identifier">size</phrase></code>
  2792. Bytes and stores a pointer to the stack and its actual size in <code><phrase
  2793. role="identifier">sctx</phrase></code>. Depending on the architecture
  2794. (the stack grows downwards/upwards) the stored address is the highest/lowest
  2795. address of the stack.
  2796. </para>
  2797. </listitem>
  2798. </varlistentry>
  2799. </variablelist>
  2800. <bridgehead renderas="sect4" id="context.stack.segmented.h1">
  2801. <phrase id="context.stack.segmented._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"/><link
  2802. linkend="context.stack.segmented._code__phrase_role__keyword__void__phrase___phrase_role__identifier__deallocate__phrase__phrase_role__special_____phrase___phrase_role__identifier__stack_context__phrase___phrase_role__special___amp___phrase___phrase_role__identifier__sctx__phrase__phrase_role__special_____phrase___code_"><code><phrase
  2803. role="keyword">void</phrase> <phrase role="identifier">deallocate</phrase><phrase
  2804. role="special">(</phrase> <phrase role="identifier">stack_context</phrase>
  2805. <phrase role="special">&amp;</phrase> <phrase role="identifier">sctx</phrase><phrase
  2806. role="special">)</phrase></code></link>
  2807. </bridgehead>
  2808. <variablelist>
  2809. <title></title>
  2810. <varlistentry>
  2811. <term>Preconditions:</term>
  2812. <listitem>
  2813. <para>
  2814. <code><phrase role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2815. role="identifier">sp</phrase></code> is valid, <code><phrase role="identifier">traits_type</phrase><phrase
  2816. role="special">::</phrase><phrase role="identifier">minimum</phrase><phrase
  2817. role="special">:</phrase><phrase role="identifier">size</phrase><phrase
  2818. role="special">()</phrase> <phrase role="special">&lt;=</phrase> <phrase
  2819. role="identifier">sctx</phrase><phrase role="special">.</phrase><phrase
  2820. role="identifier">size</phrase></code> and <code><phrase role="special">!</phrase>
  2821. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2822. role="identifier">is_unbounded</phrase><phrase role="special">()</phrase>
  2823. <phrase role="special">&amp;&amp;</phrase> <phrase role="special">(</phrase>
  2824. <phrase role="identifier">traits_type</phrase><phrase role="special">::</phrase><phrase
  2825. role="identifier">maximum</phrase><phrase role="special">:</phrase><phrase
  2826. role="identifier">size</phrase><phrase role="special">()</phrase>
  2827. <phrase role="special">&gt;=</phrase> <phrase role="identifier">sctx</phrase><phrase
  2828. role="special">.</phrase><phrase role="identifier">size</phrase><phrase
  2829. role="special">)</phrase></code>.
  2830. </para>
  2831. </listitem>
  2832. </varlistentry>
  2833. <varlistentry>
  2834. <term>Effects:</term>
  2835. <listitem>
  2836. <para>
  2837. Deallocates the stack space.
  2838. </para>
  2839. </listitem>
  2840. </varlistentry>
  2841. </variablelist>
  2842. <note>
  2843. <para>
  2844. If the library is compiled for segmented stacks, <emphasis>segmented_stack</emphasis>
  2845. is the only available stack allocator.
  2846. </para>
  2847. </note>
  2848. </section>
  2849. <section id="context.stack.stack_traits">
  2850. <title><link linkend="context.stack.stack_traits">Class <emphasis>stack_traits</emphasis></link></title>
  2851. <para>
  2852. <emphasis>stack_traits</emphasis> models a <emphasis>stack-traits</emphasis>
  2853. providing a way to access certain properites defined by the enironment. Stack
  2854. allocators use <emphasis>stack-traits</emphasis> to allocate stacks.
  2855. </para>
  2856. <programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special">&lt;</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">context</phrase><phrase role="special">/</phrase><phrase role="identifier">stack_traits</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">&gt;</phrase>
  2857. <phrase role="keyword">struct</phrase> <phrase role="identifier">stack_traits</phrase> <phrase role="special">{</phrase>
  2858. <phrase role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2859. <phrase role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">page_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2860. <phrase role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">default_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2861. <phrase role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2862. <phrase role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">maximum_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  2863. <phrase role="special">}</phrase>
  2864. </programlisting>
  2865. <bridgehead renderas="sect4" id="context.stack.stack_traits.h0">
  2866. <phrase id="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__keyword__bool__phrase___phrase_role__identifier__is_unbounded__phrase__phrase_role__special______phrase___code_"/><link
  2867. linkend="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__keyword__bool__phrase___phrase_role__identifier__is_unbounded__phrase__phrase_role__special______phrase___code_"><code><phrase
  2868. role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase
  2869. role="identifier">is_unbounded</phrase><phrase role="special">()</phrase></code></link>
  2870. </bridgehead>
  2871. <variablelist>
  2872. <title></title>
  2873. <varlistentry>
  2874. <term>Returns:</term>
  2875. <listitem>
  2876. <para>
  2877. Returns <code><phrase role="keyword">true</phrase></code> if the environment
  2878. defines no limit for the size of a stack.
  2879. </para>
  2880. </listitem>
  2881. </varlistentry>
  2882. <varlistentry>
  2883. <term>Throws:</term>
  2884. <listitem>
  2885. <para>
  2886. Nothing.
  2887. </para>
  2888. </listitem>
  2889. </varlistentry>
  2890. </variablelist>
  2891. <bridgehead renderas="sect4" id="context.stack.stack_traits.h1">
  2892. <phrase id="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__page_size__phrase__phrase_role__special______phrase___code_"/><link
  2893. linkend="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__page_size__phrase__phrase_role__special______phrase___code_"><code><phrase
  2894. role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase
  2895. role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase
  2896. role="identifier">page_size</phrase><phrase role="special">()</phrase></code></link>
  2897. </bridgehead>
  2898. <variablelist>
  2899. <title></title>
  2900. <varlistentry>
  2901. <term>Returns:</term>
  2902. <listitem>
  2903. <para>
  2904. Returns the page size in bytes.
  2905. </para>
  2906. </listitem>
  2907. </varlistentry>
  2908. <varlistentry>
  2909. <term>Throws:</term>
  2910. <listitem>
  2911. <para>
  2912. Nothing.
  2913. </para>
  2914. </listitem>
  2915. </varlistentry>
  2916. </variablelist>
  2917. <bridgehead renderas="sect4" id="context.stack.stack_traits.h2">
  2918. <phrase id="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__default_size__phrase__phrase_role__special______phrase___code_"/><link
  2919. linkend="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__default_size__phrase__phrase_role__special______phrase___code_"><code><phrase
  2920. role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase
  2921. role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase
  2922. role="identifier">default_size</phrase><phrase role="special">()</phrase></code></link>
  2923. </bridgehead>
  2924. <variablelist>
  2925. <title></title>
  2926. <varlistentry>
  2927. <term>Returns:</term>
  2928. <listitem>
  2929. <para>
  2930. Returns a default stack size, which may be platform specific. If the
  2931. stack is unbounded then the present implementation returns the maximum
  2932. of <code><phrase role="number">64</phrase> <phrase role="identifier">kB</phrase></code>
  2933. and <code><phrase role="identifier">minimum_size</phrase><phrase role="special">()</phrase></code>.
  2934. </para>
  2935. </listitem>
  2936. </varlistentry>
  2937. <varlistentry>
  2938. <term>Throws:</term>
  2939. <listitem>
  2940. <para>
  2941. Nothing.
  2942. </para>
  2943. </listitem>
  2944. </varlistentry>
  2945. </variablelist>
  2946. <bridgehead renderas="sect4" id="context.stack.stack_traits.h3">
  2947. <phrase id="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__minimum_size__phrase__phrase_role__special______phrase___code_"/><link
  2948. linkend="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__minimum_size__phrase__phrase_role__special______phrase___code_"><code><phrase
  2949. role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase
  2950. role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase
  2951. role="identifier">minimum_size</phrase><phrase role="special">()</phrase></code></link>
  2952. </bridgehead>
  2953. <variablelist>
  2954. <title></title>
  2955. <varlistentry>
  2956. <term>Returns:</term>
  2957. <listitem>
  2958. <para>
  2959. Returns the minimum size in bytes of stack defined by the environment
  2960. (Win32 4kB/Win64 8kB, defined by rlimit on POSIX).
  2961. </para>
  2962. </listitem>
  2963. </varlistentry>
  2964. <varlistentry>
  2965. <term>Throws:</term>
  2966. <listitem>
  2967. <para>
  2968. Nothing.
  2969. </para>
  2970. </listitem>
  2971. </varlistentry>
  2972. </variablelist>
  2973. <bridgehead renderas="sect4" id="context.stack.stack_traits.h4">
  2974. <phrase id="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__maximum_size__phrase__phrase_role__special______phrase___code_"/><link
  2975. linkend="context.stack.stack_traits._code__phrase_role__keyword__static__phrase___phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__maximum_size__phrase__phrase_role__special______phrase___code_"><code><phrase
  2976. role="keyword">static</phrase> <phrase role="identifier">std</phrase><phrase
  2977. role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase
  2978. role="identifier">maximum_size</phrase><phrase role="special">()</phrase></code></link>
  2979. </bridgehead>
  2980. <variablelist>
  2981. <title></title>
  2982. <varlistentry>
  2983. <term>Preconditions:</term>
  2984. <listitem>
  2985. <para>
  2986. <code><phrase role="identifier">is_unbounded</phrase><phrase role="special">()</phrase></code>
  2987. returns <code><phrase role="keyword">false</phrase></code>.
  2988. </para>
  2989. </listitem>
  2990. </varlistentry>
  2991. <varlistentry>
  2992. <term>Returns:</term>
  2993. <listitem>
  2994. <para>
  2995. Returns the maximum size in bytes of stack defined by the environment.
  2996. </para>
  2997. </listitem>
  2998. </varlistentry>
  2999. <varlistentry>
  3000. <term>Throws:</term>
  3001. <listitem>
  3002. <para>
  3003. Nothing.
  3004. </para>
  3005. </listitem>
  3006. </varlistentry>
  3007. </variablelist>
  3008. </section>
  3009. <section id="context.stack.stack_context">
  3010. <title><link linkend="context.stack.stack_context">Class <emphasis>stack_context</emphasis></link></title>
  3011. <para>
  3012. <emphasis role="bold">Boost.Context</emphasis> provides the class <emphasis>stack_context</emphasis>
  3013. which will contain the stack pointer and the size of the stack. In case of
  3014. a <link linkend="segmented"><emphasis>segmented_stack</emphasis></link>,
  3015. <emphasis>stack_context</emphasis> contains some extra control structures.
  3016. </para>
  3017. <programlisting><phrase role="keyword">struct</phrase> <phrase role="identifier">stack_context</phrase> <phrase role="special">{</phrase>
  3018. <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">;</phrase>
  3019. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">;</phrase>
  3020. <phrase role="comment">// might contain additional control structures</phrase>
  3021. <phrase role="comment">// for segmented stacks</phrase>
  3022. <phrase role="special">}</phrase>
  3023. </programlisting>
  3024. <bridgehead renderas="sect4" id="context.stack.stack_context.h0">
  3025. <phrase id="context.stack.stack_context._code__phrase_role__keyword__void__phrase___phrase_role__special_____phrase___phrase_role__identifier__sp__phrase___code_"/><link
  3026. linkend="context.stack.stack_context._code__phrase_role__keyword__void__phrase___phrase_role__special_____phrase___phrase_role__identifier__sp__phrase___code_"><code><phrase
  3027. role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase></code></link>
  3028. </bridgehead>
  3029. <variablelist>
  3030. <title></title>
  3031. <varlistentry>
  3032. <term>Value:</term>
  3033. <listitem>
  3034. <para>
  3035. Pointer to the beginning of the stack.
  3036. </para>
  3037. </listitem>
  3038. </varlistentry>
  3039. </variablelist>
  3040. <bridgehead renderas="sect4" id="context.stack.stack_context.h1">
  3041. <phrase id="context.stack.stack_context._code__phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__size__phrase___code_"/><link
  3042. linkend="context.stack.stack_context._code__phrase_role__identifier__std__phrase__phrase_role__special______phrase__phrase_role__identifier__size_t__phrase___phrase_role__identifier__size__phrase___code_"><code><phrase
  3043. role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase>
  3044. <phrase role="identifier">size</phrase></code></link>
  3045. </bridgehead>
  3046. <variablelist>
  3047. <title></title>
  3048. <varlistentry>
  3049. <term>Value:</term>
  3050. <listitem>
  3051. <para>
  3052. Actual size of the stack.
  3053. </para>
  3054. </listitem>
  3055. </varlistentry>
  3056. </variablelist>
  3057. </section>
  3058. <section id="context.stack.valgrind">
  3059. <title><link linkend="context.stack.valgrind">Support for valgrind</link></title>
  3060. <para>
  3061. Running programs that switch stacks under valgrind causes problems. Property
  3062. (b2 command-line) <code><phrase role="identifier">valgrind</phrase><phrase
  3063. role="special">=</phrase><phrase role="identifier">on</phrase></code> let
  3064. valgrind treat the memory regions as stack space which suppresses the errors.
  3065. Users must define <code><phrase role="identifier">BOOST_USE_VALGRIND</phrase></code>
  3066. before including any Boost.Context headers when linking against Boost binaries
  3067. compiled with <code><phrase role="identifier">valgrind</phrase><phrase role="special">=</phrase><phrase
  3068. role="identifier">on</phrase></code>.
  3069. </para>
  3070. </section>
  3071. <section id="context.stack.sanitizers">
  3072. <title><link linkend="context.stack.sanitizers">Support for sanitizers</link></title>
  3073. <para>
  3074. Sanitizers (GCC/Clang) are confused by the stack switches. The library is
  3075. required to be compiled with property (b2 command-line) <code><phrase role="identifier">context</phrase><phrase
  3076. role="special">-</phrase><phrase role="identifier">impl</phrase><phrase role="special">=</phrase><phrase
  3077. role="identifier">ucontext</phrase></code> and compilers santizer options.
  3078. Users must define <code><phrase role="identifier">BOOST_USE_ASAN</phrase></code>
  3079. before including any Boost.Context headers when linking against Boost binaries.
  3080. </para>
  3081. </section>
  3082. </section>
  3083. <section id="context.struct__preallocated_">
  3084. <title><link linkend="context.struct__preallocated_">Struct <code><phrase role="identifier">preallocated</phrase></code></link></title>
  3085. <programlisting><phrase role="keyword">struct</phrase> <phrase role="identifier">preallocated</phrase> <phrase role="special">{</phrase>
  3086. <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">;</phrase>
  3087. <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">;</phrase>
  3088. <phrase role="identifier">stack_context</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">;</phrase>
  3089. <phrase role="identifier">preallocated</phrase><phrase role="special">(</phrase> <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">:</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="identifier">stack_allocator</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  3090. <phrase role="special">};</phrase>
  3091. </programlisting>
  3092. <bridgehead renderas="sect3" id="context.struct__preallocated_.h0">
  3093. <phrase id="context.struct__preallocated_.constructor"/><link linkend="context.struct__preallocated_.constructor">Constructor</link>
  3094. </bridgehead>
  3095. <programlisting><phrase role="identifier">preallocated</phrase><phrase role="special">(</phrase> <phrase role="keyword">void</phrase> <phrase role="special">*</phrase> <phrase role="identifier">sp</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">:</phrase><phrase role="identifier">size_t</phrase> <phrase role="identifier">size</phrase><phrase role="special">,</phrase> <phrase role="identifier">stack_allocator</phrase> <phrase role="identifier">sctx</phrase><phrase role="special">)</phrase> <phrase role="keyword">noexcept</phrase><phrase role="special">;</phrase>
  3096. </programlisting>
  3097. <variablelist>
  3098. <title></title>
  3099. <varlistentry>
  3100. <term>Effects:</term>
  3101. <listitem>
  3102. <para>
  3103. Creates an object of preallocated.
  3104. </para>
  3105. </listitem>
  3106. </varlistentry>
  3107. </variablelist>
  3108. </section>
  3109. <section id="context.performance">
  3110. <title><anchor id="performance"/><link linkend="context.performance">Performance</link></title>
  3111. <para>
  3112. Performance measurements were taken using <code><phrase role="identifier">std</phrase><phrase
  3113. role="special">::</phrase><phrase role="identifier">chrono</phrase><phrase
  3114. role="special">::</phrase><phrase role="identifier">highresolution_clock</phrase></code>,
  3115. with overhead corrections. The code was compiled with gcc-6.3.1, using build
  3116. options: variant = release, optimization = speed. Tests were executed on dual
  3117. Intel XEON E5 2620v4 2.2GHz, 16C/32T, 64GB RAM, running Linux (x86_64).
  3118. </para>
  3119. <table frame="all" id="context.performance.performance_of_context_switch">
  3120. <title>Performance of context switch</title>
  3121. <tgroup cols="3">
  3122. <thead>
  3123. <row>
  3124. <entry>
  3125. <para>
  3126. callcc()/continuation (fcontext_t)
  3127. </para>
  3128. </entry>
  3129. <entry>
  3130. <para>
  3131. callcc()/continuation (ucontext_t)
  3132. </para>
  3133. </entry>
  3134. <entry>
  3135. <para>
  3136. callcc()/continuation (Windows-Fiber)
  3137. </para>
  3138. </entry>
  3139. </row>
  3140. </thead>
  3141. <tbody>
  3142. <row>
  3143. <entry>
  3144. <para>
  3145. 9 ns / 19 CPU cycles
  3146. </para>
  3147. </entry>
  3148. <entry>
  3149. <para>
  3150. 547 ns / 1130 CPU cycles
  3151. </para>
  3152. </entry>
  3153. <entry>
  3154. <para>
  3155. 49 ns / 98 CPU cycles
  3156. </para>
  3157. </entry>
  3158. </row>
  3159. </tbody>
  3160. </tgroup>
  3161. </table>
  3162. </section>
  3163. <section id="context.architectures">
  3164. <title><link linkend="context.architectures">Architectures</link></title>
  3165. <para>
  3166. <emphasis role="bold">Boost.Context</emphasis>, using <link linkend="implementation"><emphasis>fcontext_t</emphasis></link>,
  3167. supports following architectures:
  3168. </para>
  3169. <table frame="all" id="context.architectures.supported_architectures___abi_binary_format__">
  3170. <title>Supported architectures (&lt;ABI|binary format&gt;)</title>
  3171. <tgroup cols="5">
  3172. <thead>
  3173. <row>
  3174. <entry>
  3175. <para>
  3176. Architecture
  3177. </para>
  3178. </entry>
  3179. <entry>
  3180. <para>
  3181. LINUX (UNIX)
  3182. </para>
  3183. </entry>
  3184. <entry>
  3185. <para>
  3186. Windows
  3187. </para>
  3188. </entry>
  3189. <entry>
  3190. <para>
  3191. MacOS X
  3192. </para>
  3193. </entry>
  3194. <entry>
  3195. <para>
  3196. iOS
  3197. </para>
  3198. </entry>
  3199. </row>
  3200. </thead>
  3201. <tbody>
  3202. <row>
  3203. <entry>
  3204. <para>
  3205. arm (aarch32)
  3206. </para>
  3207. </entry>
  3208. <entry>
  3209. <para>
  3210. AAPCS|ELF
  3211. </para>
  3212. </entry>
  3213. <entry>
  3214. <para>
  3215. AAPCS|PE
  3216. </para>
  3217. </entry>
  3218. <entry>
  3219. <para>
  3220. -
  3221. </para>
  3222. </entry>
  3223. <entry>
  3224. <para>
  3225. AAPCS|MACH-O
  3226. </para>
  3227. </entry>
  3228. </row>
  3229. <row>
  3230. <entry>
  3231. <para>
  3232. arm (aarch64)
  3233. </para>
  3234. </entry>
  3235. <entry>
  3236. <para>
  3237. AAPCS|ELF
  3238. </para>
  3239. </entry>
  3240. <entry>
  3241. <para>
  3242. -
  3243. </para>
  3244. </entry>
  3245. <entry>
  3246. <para>
  3247. -
  3248. </para>
  3249. </entry>
  3250. <entry>
  3251. <para>
  3252. AAPCS|MACH-O
  3253. </para>
  3254. </entry>
  3255. </row>
  3256. <row>
  3257. <entry>
  3258. <para>
  3259. i386
  3260. </para>
  3261. </entry>
  3262. <entry>
  3263. <para>
  3264. SYSV|ELF
  3265. </para>
  3266. </entry>
  3267. <entry>
  3268. <para>
  3269. MS|PE
  3270. </para>
  3271. </entry>
  3272. <entry>
  3273. <para>
  3274. SYSV|MACH-O
  3275. </para>
  3276. </entry>
  3277. <entry>
  3278. <para>
  3279. -
  3280. </para>
  3281. </entry>
  3282. </row>
  3283. <row>
  3284. <entry>
  3285. <para>
  3286. mips1
  3287. </para>
  3288. </entry>
  3289. <entry>
  3290. <para>
  3291. O32|ELF
  3292. </para>
  3293. </entry>
  3294. <entry>
  3295. <para>
  3296. -
  3297. </para>
  3298. </entry>
  3299. <entry>
  3300. <para>
  3301. -
  3302. </para>
  3303. </entry>
  3304. <entry>
  3305. <para>
  3306. -
  3307. </para>
  3308. </entry>
  3309. </row>
  3310. <row>
  3311. <entry>
  3312. <para>
  3313. ppc32
  3314. </para>
  3315. </entry>
  3316. <entry>
  3317. <para>
  3318. SYSV|ELF,XCOFF
  3319. </para>
  3320. </entry>
  3321. <entry>
  3322. <para>
  3323. -
  3324. </para>
  3325. </entry>
  3326. <entry>
  3327. <para>
  3328. SYSV|MACH-O
  3329. </para>
  3330. </entry>
  3331. <entry>
  3332. <para>
  3333. -
  3334. </para>
  3335. </entry>
  3336. </row>
  3337. <row>
  3338. <entry>
  3339. <para>
  3340. ppc64
  3341. </para>
  3342. </entry>
  3343. <entry>
  3344. <para>
  3345. SYSV|ELF,XCOFF
  3346. </para>
  3347. </entry>
  3348. <entry>
  3349. <para>
  3350. -
  3351. </para>
  3352. </entry>
  3353. <entry>
  3354. <para>
  3355. SYSV|MACH-O
  3356. </para>
  3357. </entry>
  3358. <entry>
  3359. <para>
  3360. -
  3361. </para>
  3362. </entry>
  3363. </row>
  3364. <row>
  3365. <entry>
  3366. <para>
  3367. riscv64
  3368. </para>
  3369. </entry>
  3370. <entry>
  3371. <para>
  3372. SYSV|ELF
  3373. </para>
  3374. </entry>
  3375. <entry>
  3376. <para>
  3377. -
  3378. </para>
  3379. </entry>
  3380. <entry>
  3381. <para>
  3382. SYSV
  3383. </para>
  3384. </entry>
  3385. <entry>
  3386. <para>
  3387. -
  3388. </para>
  3389. </entry>
  3390. </row>
  3391. <row>
  3392. <entry>
  3393. <para>
  3394. s390x
  3395. </para>
  3396. </entry>
  3397. <entry>
  3398. <para>
  3399. SYSV|ELF
  3400. </para>
  3401. </entry>
  3402. <entry>
  3403. <para>
  3404. -
  3405. </para>
  3406. </entry>
  3407. <entry>
  3408. <para>
  3409. -
  3410. </para>
  3411. </entry>
  3412. <entry>
  3413. <para>
  3414. -
  3415. </para>
  3416. </entry>
  3417. </row>
  3418. <row>
  3419. <entry>
  3420. <para>
  3421. sparc
  3422. </para>
  3423. </entry>
  3424. <entry>
  3425. <para>
  3426. -
  3427. </para>
  3428. </entry>
  3429. <entry>
  3430. <para>
  3431. -
  3432. </para>
  3433. </entry>
  3434. <entry>
  3435. <para>
  3436. -
  3437. </para>
  3438. </entry>
  3439. <entry>
  3440. <para>
  3441. -
  3442. </para>
  3443. </entry>
  3444. </row>
  3445. <row>
  3446. <entry>
  3447. <para>
  3448. x86_64
  3449. </para>
  3450. </entry>
  3451. <entry>
  3452. <para>
  3453. SYSV,X32|ELF
  3454. </para>
  3455. </entry>
  3456. <entry>
  3457. <para>
  3458. MS|PE
  3459. </para>
  3460. </entry>
  3461. <entry>
  3462. <para>
  3463. SYSV|MACH-O
  3464. </para>
  3465. </entry>
  3466. <entry>
  3467. <para>
  3468. -
  3469. </para>
  3470. </entry>
  3471. </row>
  3472. </tbody>
  3473. </tgroup>
  3474. </table>
  3475. <note>
  3476. <para>
  3477. If the architecture is not supported but the platform provides <link linkend="implementation"><emphasis>ucontext_t</emphasis></link>,
  3478. <emphasis role="bold">Boost.Context</emphasis> should be compiled with <code><phrase
  3479. role="identifier">BOOST_USE_UCONTEXT</phrase></code> and b2 property <code><phrase
  3480. role="identifier">context</phrase><phrase role="special">-</phrase><phrase
  3481. role="identifier">impl</phrase><phrase role="special">=</phrase><phrase role="identifier">ucontext</phrase></code>.
  3482. </para>
  3483. </note>
  3484. <section id="context.architectures.crosscompiling">
  3485. <title><link linkend="context.architectures.crosscompiling">Cross compiling</link></title>
  3486. <para>
  3487. Cross compiling the library requires to specify the build properties &lt;architecture&gt;,
  3488. &lt;address-model&gt;, &lt;binary-format&gt; and &lt;abi&gt; at b2 command
  3489. line.
  3490. </para>
  3491. </section>
  3492. </section>
  3493. <section id="context.rationale">
  3494. <title><link linkend="context.rationale">Rationale</link></title>
  3495. <bridgehead renderas="sect3" id="context.rationale.h0">
  3496. <phrase id="context.rationale.no_inline_assembler"/><link linkend="context.rationale.no_inline_assembler">No
  3497. inline-assembler</link>
  3498. </bridgehead>
  3499. <para>
  3500. Some newer compiler (for instance MSVC 10 for x86_64 and itanium) do not support
  3501. inline assembler. <footnote id="context.rationale.f0">
  3502. <para>
  3503. <ulink url="http://msdn.microsoft.com/en-us/library/4ks26t93.aspx">MSDN article
  3504. 'Inline Assembler'</ulink>
  3505. </para>
  3506. </footnote>. Inlined assembler generates code bloating which is not welcome
  3507. on embedded systems.
  3508. </para>
  3509. <bridgehead renderas="sect3" id="context.rationale.h1">
  3510. <phrase id="context.rationale.fcontext_t"/><link linkend="context.rationale.fcontext_t">fcontext_t</link>
  3511. </bridgehead>
  3512. <para>
  3513. <emphasis role="bold">Boost.Context</emphasis> provides the low level API fcontext_t
  3514. which is implemented in assembler to provide context swapping operations. fcontext_t
  3515. is the part to port to new platforms.
  3516. </para>
  3517. <note>
  3518. <para>
  3519. Context switches do not preserve the signal mask on UNIX systems.
  3520. </para>
  3521. </note>
  3522. <para>
  3523. <emphasis>fcontext_t</emphasis> is an opaque pointer.
  3524. </para>
  3525. <section id="context.rationale.other_apis_">
  3526. <title><link linkend="context.rationale.other_apis_">Other APIs </link></title>
  3527. <bridgehead renderas="sect4" id="context.rationale.other_apis_.h0">
  3528. <phrase id="context.rationale.other_apis_.setjmp___longjmp__"/><link linkend="context.rationale.other_apis_.setjmp___longjmp__">setjmp()/longjmp()</link>
  3529. </bridgehead>
  3530. <para>
  3531. C99 defines <code><phrase role="identifier">setjmp</phrase><phrase role="special">()</phrase></code>/<code><phrase
  3532. role="identifier">longjmp</phrase><phrase role="special">()</phrase></code>
  3533. to provide non-local jumps but it does not require that <emphasis>longjmp()</emphasis>
  3534. preserves the current stack frame. Therefore, jumping into a function which
  3535. was exited via a call to <emphasis>longjmp()</emphasis> is undefined <footnote
  3536. id="context.rationale.other_apis_.f0">
  3537. <para>
  3538. ISO/IEC 9899:1999, 2005, 7.13.2.1:2
  3539. </para>
  3540. </footnote>.
  3541. </para>
  3542. <anchor id="ucontext"/>
  3543. <bridgehead renderas="sect4" id="context.rationale.other_apis_.h1">
  3544. <phrase id="context.rationale.other_apis_.ucontext_t"/><link linkend="context.rationale.other_apis_.ucontext_t">ucontext_t</link>
  3545. </bridgehead>
  3546. <para>
  3547. Since POSIX.1-2004 <code><phrase role="identifier">ucontext_t</phrase></code>
  3548. is deprecated and was removed in POSIX.1-2008! The function signature of
  3549. <code><phrase role="identifier">makecontext</phrase><phrase role="special">()</phrase></code>
  3550. is:
  3551. </para>
  3552. <programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">makecontext</phrase><phrase role="special">(</phrase><phrase role="identifier">ucontext_t</phrase> <phrase role="special">*</phrase><phrase role="identifier">ucp</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase> <phrase role="special">(*</phrase><phrase role="identifier">func</phrase><phrase role="special">)(),</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">argc</phrase><phrase role="special">,</phrase> <phrase role="special">...);</phrase>
  3553. </programlisting>
  3554. <para>
  3555. The third argument of <code><phrase role="identifier">makecontext</phrase><phrase
  3556. role="special">()</phrase></code> specifies the number of integer arguments
  3557. that follow which will require function pointer cast if <code><phrase role="identifier">func</phrase></code>
  3558. will accept those arguments which is undefined in C99 <footnote id="context.rationale.other_apis_.f1">
  3559. <para>
  3560. ISO/IEC 9899:1999, 2005, J.2
  3561. </para>
  3562. </footnote>.
  3563. </para>
  3564. <para>
  3565. The arguments in the var-arg list are required to be integers, passing pointers
  3566. in var-arg list is not guaranteed to work, especially it will fail for architectures
  3567. where pointers are larger than integers.
  3568. </para>
  3569. <para>
  3570. <code><phrase role="identifier">ucontext_t</phrase></code> preserves signal
  3571. mask between context switches which involves system calls consuming a lot
  3572. of CPU cycles (ucontext_t is slower; a context switch takes <link linkend="performance"><emphasis>two
  3573. magnitutes of order more CPU cycles</emphasis></link> more than <emphasis>fcontext_t</emphasis>).
  3574. </para>
  3575. <bridgehead renderas="sect4" id="context.rationale.other_apis_.h2">
  3576. <phrase id="context.rationale.other_apis_.windows_fibers"/><link linkend="context.rationale.other_apis_.windows_fibers">Windows
  3577. fibers</link>
  3578. </bridgehead>
  3579. <para>
  3580. A drawback of Windows Fiber API is that <code><phrase role="identifier">CreateFiber</phrase><phrase
  3581. role="special">()</phrase></code> does not accept a pointer to user allocated
  3582. stack space preventing the reuse of stacks for other context instances. Because
  3583. the Windows Fiber API requires to call <code><phrase role="identifier">ConvertThreadToFiber</phrase><phrase
  3584. role="special">()</phrase></code> if <code><phrase role="identifier">SwitchFiber</phrase><phrase
  3585. role="special">()</phrase></code> is called for a thread which has not been
  3586. converted to a fiber. For the same reason <code><phrase role="identifier">ConvertFiberToThread</phrase><phrase
  3587. role="special">()</phrase></code> must be called after return from <code><phrase
  3588. role="identifier">SwitchFiber</phrase><phrase role="special">()</phrase></code>
  3589. if the thread was forced to be converted to a fiber before (which is inefficient).
  3590. </para>
  3591. <programlisting><phrase role="keyword">if</phrase> <phrase role="special">(</phrase> <phrase role="special">!</phrase> <phrase role="identifier">is_a_fiber</phrase><phrase role="special">()</phrase> <phrase role="special">)</phrase>
  3592. <phrase role="special">{</phrase>
  3593. <phrase role="identifier">ConvertThreadToFiber</phrase><phrase role="special">(</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
  3594. <phrase role="identifier">SwitchToFiber</phrase><phrase role="special">(</phrase> <phrase role="identifier">ctx</phrase><phrase role="special">);</phrase>
  3595. <phrase role="identifier">ConvertFiberToThread</phrase><phrase role="special">();</phrase>
  3596. <phrase role="special">}</phrase>
  3597. </programlisting>
  3598. <para>
  3599. If the condition <code><phrase role="identifier">_WIN32_WINNT</phrase> <phrase
  3600. role="special">&gt;=</phrase> <phrase role="identifier">_WIN32_WINNT_VISTA</phrase></code>
  3601. is met function <code><phrase role="identifier">IsThreadAFiber</phrase><phrase
  3602. role="special">()</phrase></code> is provided in order to detect if the current
  3603. thread was already converted. Unfortunately Windows XP + SP 2/3 defines
  3604. <code><phrase role="identifier">_WIN32_WINNT</phrase> <phrase role="special">&gt;=</phrase>
  3605. <phrase role="identifier">_WIN32_WINNT_VISTA</phrase></code> without providing
  3606. <code><phrase role="identifier">IsThreadAFiber</phrase><phrase role="special">()</phrase></code>.
  3607. </para>
  3608. </section>
  3609. <section id="context.rationale.x86_and_floating_point_env">
  3610. <title><link linkend="context.rationale.x86_and_floating_point_env">x86 and
  3611. floating-point env</link></title>
  3612. <bridgehead renderas="sect4" id="context.rationale.x86_and_floating_point_env.h0">
  3613. <phrase id="context.rationale.x86_and_floating_point_env.i386"/><link linkend="context.rationale.x86_and_floating_point_env.i386">i386</link>
  3614. </bridgehead>
  3615. <para>
  3616. &quot;The FpCsr and the MxCsr register must be saved and restored before
  3617. any call or return by any procedure that needs to modify them ...&quot;
  3618. <footnote id="context.rationale.x86_and_floating_point_env.f0">
  3619. <para>
  3620. 'Calling Conventions', Agner Fog
  3621. </para>
  3622. </footnote>.
  3623. </para>
  3624. <bridgehead renderas="sect4" id="context.rationale.x86_and_floating_point_env.h1">
  3625. <phrase id="context.rationale.x86_and_floating_point_env.x86_64"/><link linkend="context.rationale.x86_and_floating_point_env.x86_64">x86_64</link>
  3626. </bridgehead>
  3627. <bridgehead renderas="sect4" id="context.rationale.x86_and_floating_point_env.h2">
  3628. <phrase id="context.rationale.x86_and_floating_point_env.windows"/><link
  3629. linkend="context.rationale.x86_and_floating_point_env.windows">Windows</link>
  3630. </bridgehead>
  3631. <para>
  3632. MxCsr - &quot;A callee that modifies any of the non-volatile fields within
  3633. MxCsr must restore them before returning to its caller. Furthermore, a caller
  3634. that has modified any of these fields must restore them to their standard
  3635. values before invoking a callee ...&quot; <footnote id="context.rationale.x86_and_floating_point_env.f1">
  3636. <para>
  3637. <ulink url="http://http://msdn.microsoft.com/en-us/library/yxty7t75.aspx">MSDN
  3638. article 'MxCsr'</ulink>
  3639. </para>
  3640. </footnote>.
  3641. </para>
  3642. <para>
  3643. FpCsr - &quot;A callee that modifies any of the fields within FpCsr must
  3644. restore them before returning to its caller. Furthermore, a caller that has
  3645. modified any of these fields must restore them to their standard values before
  3646. invoking a callee ...&quot; <footnote id="context.rationale.x86_and_floating_point_env.f2">
  3647. <para>
  3648. <ulink url="http://http://msdn.microsoft.com/en-us/library/ms235300.aspx">MSDN
  3649. article 'FpCsr'</ulink>
  3650. </para>
  3651. </footnote>.
  3652. </para>
  3653. <para>
  3654. &quot;The MMX and floating-point stack registers (MM0-MM7/ST0-ST7) are preserved
  3655. across context switches. There is no explicit calling convention for these
  3656. registers.&quot; <footnote id="context.rationale.x86_and_floating_point_env.f3">
  3657. <para>
  3658. <ulink url="http://msdn.microsoft.com/en-us/library/a32tsf7t%28VS.80%29.aspx">MSDN
  3659. article 'Legacy Floating-Point Support'</ulink>
  3660. </para>
  3661. </footnote>.
  3662. </para>
  3663. <para>
  3664. &quot;The 64-bit Microsoft compiler does not use ST(0)-ST(7)/MM0-MM7&quot;.
  3665. <footnote id="context.rationale.x86_and_floating_point_env.f4">
  3666. <para>
  3667. 'Calling Conventions', Agner Fog
  3668. </para>
  3669. </footnote>.
  3670. </para>
  3671. <para>
  3672. &quot;XMM6-XMM15 must be preserved&quot; <footnote id="context.rationale.x86_and_floating_point_env.f5">
  3673. <para>
  3674. <ulink url="http://msdn.microsoft.com/en-us/library/9z1stfyw%28v=vs.100%29.aspx">MSDN
  3675. article 'Register Usage'</ulink>
  3676. </para>
  3677. </footnote>
  3678. </para>
  3679. <bridgehead renderas="sect4" id="context.rationale.x86_and_floating_point_env.h3">
  3680. <phrase id="context.rationale.x86_and_floating_point_env.sysv"/><link linkend="context.rationale.x86_and_floating_point_env.sysv">SysV</link>
  3681. </bridgehead>
  3682. <para>
  3683. &quot;The control bits of the MxCsr register are callee-saved (preserved
  3684. across calls), while the status bits are caller-saved (not preserved). The
  3685. x87 status word register is caller-saved, whereas the x87 control word (FpCsr)
  3686. is callee-saved.&quot; <footnote id="context.rationale.x86_and_floating_point_env.f6">
  3687. <para>
  3688. SysV ABI AMD64 Architecture Processor Supplement Draft Version 0.99.4,
  3689. 3.2.1
  3690. </para>
  3691. </footnote>.
  3692. </para>
  3693. </section>
  3694. </section>
  3695. <section id="context.reference">
  3696. <title><link linkend="context.reference">Reference</link></title>
  3697. <bridgehead renderas="sect3" id="context.reference.h0">
  3698. <phrase id="context.reference.arm"/><link linkend="context.reference.arm">ARM</link>
  3699. </bridgehead>
  3700. <itemizedlist>
  3701. <listitem>
  3702. <simpara>
  3703. AAPCS ABI: Procedure Call Standard for the ARM Architecture
  3704. </simpara>
  3705. </listitem>
  3706. <listitem>
  3707. <simpara>
  3708. AAPCS/LINUX: ARM GNU/Linux Application Binary Interface Supplement
  3709. </simpara>
  3710. </listitem>
  3711. </itemizedlist>
  3712. <bridgehead renderas="sect3" id="context.reference.h1">
  3713. <phrase id="context.reference.mips"/><link linkend="context.reference.mips">MIPS</link>
  3714. </bridgehead>
  3715. <itemizedlist>
  3716. <listitem>
  3717. <simpara>
  3718. O32 ABI: SYSTEM V APPLICATION BINARY INTERFACE, MIPS RISC Processor Supplement
  3719. </simpara>
  3720. </listitem>
  3721. </itemizedlist>
  3722. <bridgehead renderas="sect3" id="context.reference.h2">
  3723. <phrase id="context.reference.powerpc32"/><link linkend="context.reference.powerpc32">PowerPC32</link>
  3724. </bridgehead>
  3725. <itemizedlist>
  3726. <listitem>
  3727. <simpara>
  3728. SYSV ABI: SYSTEM V APPLICATION BINARY INTERFACE PowerPC Processor Supplement
  3729. </simpara>
  3730. </listitem>
  3731. </itemizedlist>
  3732. <bridgehead renderas="sect3" id="context.reference.h3">
  3733. <phrase id="context.reference.powerpc64"/><link linkend="context.reference.powerpc64">PowerPC64</link>
  3734. </bridgehead>
  3735. <itemizedlist>
  3736. <listitem>
  3737. <simpara>
  3738. SYSV ABI: PowerPC User Instruction Set Architecture, Book I
  3739. </simpara>
  3740. </listitem>
  3741. </itemizedlist>
  3742. <bridgehead renderas="sect3" id="context.reference.h4">
  3743. <phrase id="context.reference.x86_32"/><link linkend="context.reference.x86_32">X86-32</link>
  3744. </bridgehead>
  3745. <itemizedlist>
  3746. <listitem>
  3747. <simpara>
  3748. SYSV ABI: SYSTEM V APPLICATION BINARY INTERFACE, Intel386TM Architecture
  3749. Processor Supplement
  3750. </simpara>
  3751. </listitem>
  3752. <listitem>
  3753. <simpara>
  3754. MS PE: <ulink url="http://msdn.microsoft.com/en-us/library/k2b2ssfy.aspx">Calling
  3755. Conventions</ulink>
  3756. </simpara>
  3757. </listitem>
  3758. </itemizedlist>
  3759. <bridgehead renderas="sect3" id="context.reference.h5">
  3760. <phrase id="context.reference.x86_64"/><link linkend="context.reference.x86_64">X86-64</link>
  3761. </bridgehead>
  3762. <itemizedlist>
  3763. <listitem>
  3764. <simpara>
  3765. SYSV ABI: System V Application Binary Interface, AMD64 Architecture Processor
  3766. Supplement
  3767. </simpara>
  3768. </listitem>
  3769. <listitem>
  3770. <simpara>
  3771. MS PE: <ulink url="http://msdn.microsoft.com/en-us/library/7kcdt6fy%28VS.80%29.aspx">x64
  3772. Software Conventions</ulink>
  3773. </simpara>
  3774. </listitem>
  3775. </itemizedlist>
  3776. </section>
  3777. <section id="context.acknowledgements">
  3778. <title><link linkend="context.acknowledgements">Acknowledgments</link></title>
  3779. <para>
  3780. I'd like to thank Adreas Fett, Artyom Beilis, Daniel Larimer, David Deakins,
  3781. Evgeny Shapovalov, Fernando Pelliccioni, Giovanni Piero Deretta, Gordon Woodhull,
  3782. Helge Bahmann, Holger Grund, Jeffrey Lee Hellrung (Jr.), Keith Jeffery, Martin
  3783. Husemann, Phil Endecott, Robert Stewart, Sergey Cheban, Steven Watanabe, Vicente
  3784. J. Botet Escriba, Wayne Piekarski.
  3785. </para>
  3786. </section>
  3787. </library>