coroutine.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [/
  2. Copyright Oliver Kowalke 2009.
  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 two implementations - asymmetric and symmetric
  9. coroutines.
  10. Symmetric coroutines occur usually in the context of concurrent programming
  11. in order to represent independent units of execution.
  12. Implementations that produce sequences of values typically use asymmetric
  13. coroutines.
  14. [footnote Moura, Ana Lucia De and Ierusalimschy, Roberto.
  15. "Revisiting coroutines". ACM Trans. Program. Lang. Syst., Volume 31 Issue 2,
  16. February 2009, Article No. 6]
  17. [heading stackful]
  18. Each instance of a coroutine has its own stack.
  19. In contrast to stackless coroutines, stackful coroutines allow invoking the
  20. suspend operation out of arbitrary sub-stackframes, enabling escape-and-reenter
  21. recursive operations.
  22. [heading move-only]
  23. A coroutine is moveable-only.
  24. If it were copyable, then its stack with all the objects allocated on it
  25. would be copied too. That would force undefined behaviour if some of these
  26. objects were RAII-classes (manage a resource via RAII pattern). When the first
  27. of the coroutine copies terminates (unwinds its stack), the RAII class
  28. destructors will release their managed resources. When the second copy
  29. terminates, the same destructors will try to doubly-release the same resources,
  30. leading to undefined behaviour.
  31. [heading clean-up]
  32. On coroutine destruction the associated stack will be unwound.
  33. The constructor of coroutine allows you to pass a customized ['stack-allocator].
  34. ['stack-allocator] is free to deallocate the stack or cache it for future usage
  35. (for coroutines created later).
  36. [heading segmented stack]
  37. __call_coro__, __push_coro__ and __pull_coro__ support segmented stacks
  38. (growing on demand).
  39. It is not always possible to accurately estimate the required stack size - in
  40. most cases too much memory is allocated (waste of virtual address-space).
  41. At construction a coroutine starts with a default (minimal) stack size. This
  42. minimal stack size is the maximum of page size and the canonical size for signal
  43. stack (macro SIGSTKSZ on POSIX).
  44. At this time of writing only GCC (4.7)
  45. [footnote [@http://gcc.gnu.org/wiki/SplitStacks Ian Lance Taylor, Split Stacks in GCC]]
  46. is known to support segmented stacks. With version 1.54 __boost_coroutine__
  47. provides support for segmented stacks.
  48. The destructor releases the associated stack. The implementer is free to
  49. deallocate the stack or to cache it for later usage.
  50. [heading context switch]
  51. A coroutine saves and restores registers according to the underlying ABI on
  52. each context switch (using __boost_context__).
  53. Some applications do not use floating-point registers and can disable preserving
  54. FPU registers for performance reasons.
  55. [note According to the calling convention the FPU registers are preserved by
  56. default.]
  57. On POSIX systems, the coroutine context switch does not preserve signal masks
  58. for performance reasons.
  59. A context switch is done via __call_coro_op__, __push_coro_op__ and
  60. __pull_coro_op__.
  61. [warning Calling __call_coro_op__, __push_coro_op__ and __pull_coro_op__ from
  62. inside the [_same] coroutine results in undefined behaviour.]
  63. As an example, the code below will result in undefined behaviour:
  64. boost::coroutines::symmetric_coroutine<void>::call_type coro(
  65. [&](boost::coroutines::symmetric_coroutine<void>::yield_type& yield){
  66. yield(coro); // yield to same symmetric_coroutine
  67. });
  68. coro();
  69. [include asymmetric.qbk]
  70. [include symmetric.qbk]
  71. [endsect]