coroutine.qbk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [/
  2. Copyright Oliver Kowalke 2014.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. ]
  7. [section:coroutine Coroutine]
  8. __boost_coroutine__ provides asymmetric coroutines.
  9. Implementations that produce sequences of values typically use asymmetric
  10. coroutines.
  11. [footnote Moura, Ana Lucia De and Ierusalimschy, Roberto.
  12. "Revisiting coroutines". ACM Trans. Program. Lang. Syst., Volume 31 Issue 2,
  13. February 2009, Article No. 6]
  14. [heading stackful]
  15. Each instance of a coroutine has its own stack.
  16. In contrast to stackless coroutines, stackful coroutines allow invoking the
  17. suspend operation out of arbitrary sub-stackframes, enabling escape-and-reenter
  18. recursive operations.
  19. [heading move-only]
  20. A coroutine is moveable-only.
  21. If it were copyable, then its stack with all the objects allocated on it
  22. would be copied too. That would force undefined behaviour if some of these
  23. objects were RAII-classes (manage a resource via RAII pattern). When the first
  24. of the coroutine copies terminates (unwinds its stack), the RAII class
  25. destructors will release their managed resources. When the second copy
  26. terminates, the same destructors will try to doubly-release the same resources,
  27. leading to undefined behaviour.
  28. [heading clean-up]
  29. On coroutine destruction the associated stack will be unwound.
  30. The constructor of coroutine allows you to pass a customized ['stack-allocator].
  31. ['stack-allocator] is free to deallocate the stack or cache it for future usage
  32. (for coroutines created later).
  33. [heading segmented stack]
  34. __push_coro__ and __pull_coro__ support segmented stacks (growing on demand).
  35. It is not always possible to accurately estimate the required stack size - in
  36. most cases too much memory is allocated (waste of virtual address-space).
  37. At construction a coroutine starts with a default (minimal) stack size. This
  38. minimal stack size is the maximum of page size and the canonical size for signal
  39. stack (macro SIGSTKSZ on POSIX).
  40. At this time of writing only GCC (4.7)
  41. [footnote [@http://gcc.gnu.org/wiki/SplitStacks Ian Lance Taylor, Split Stacks in GCC]]
  42. is known to support segmented stacks. With version 1.54 __boost_coroutine__
  43. provides support for [link segmented ['segmented stacks]].
  44. The destructor releases the associated stack. The implementer is free to
  45. deallocate the stack or to cache it for later usage.
  46. [heading context switch]
  47. A coroutine saves and restores registers according to the underlying ABI on
  48. each context switch (using __boost_context__).
  49. A context switch is done via __push_coro_op__ and __pull_coro_op__.
  50. [warning Calling __push_coro_op__ and __pull_coro_op__ from inside the [_same]
  51. coroutine results in undefined behaviour.]
  52. As an example, the code below will result in undefined behaviour:
  53. boost::coroutines2::coroutine<void>::push_type coro(
  54. [&](boost::coroutines2::coroutine<void>::pull_type& yield){
  55. coro();
  56. });
  57. coro();
  58. [include asymmetric.qbk]
  59. [section Implementations: fcontext_t, ucontext_t and WinFiber]
  60. [heading fcontext_t]
  61. The implementation uses __fcontext__ per default. fcontext_t is based on
  62. assembler and not available for all platforms. It provides a much better
  63. performance than __ucontext__
  64. (the context switch takes two magnitudes of order less CPU cycles) and __winfib__.
  65. [heading ucontext_t]
  66. As an alternative, [@https://en.wikipedia.org/wiki/Setcontext __ucontext__]
  67. can be used by compiling with `BOOST_USE_UCONTEXT` and b2 property `context-impl=ucontext`.
  68. __ucontext__ might be available on a broader range of POSIX-platforms but has
  69. some (for instance deprecated since POSIX.1-2003, not C99 conform).
  70. [note __cc__ supports [link segmented ['Segmented stacks]] only with
  71. __ucontext__ as its implementation.]
  72. [heading WinFiber]
  73. With `BOOST_USE_WINFIB` and b2 property `context-impl=winfib` Win32-Fibers are used
  74. as implementation for __cc__.
  75. Because the TIB (thread information block) is not fully described in the MSDN,
  76. it might be possible that not all required TIB-parts are swapped.
  77. [note The first call of __cc__ converts the thread into a Windows fiber by
  78. invoking `ConvertThreadToFiber()`. If desired, `ConvertFiberToThread()` has
  79. to be called by the user explicitly in order to release resources allocated
  80. by `ConvertThreadToFiber()` (e.g. after using boost.context). ]
  81. [endsect]
  82. [endsect]