customization.qbk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. [/
  2. Copyright Oliver Kowalke 2013.
  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. [/ import path is relative to this .qbk file]
  8. [import ../examples/priority.cpp]
  9. [#custom]
  10. [section:custom Customization]
  11. [heading Overview]
  12. As noted in the [link scheduling Scheduling] section, by default
  13. __boost_fiber__ uses its own [class_link round_robin] scheduler for each
  14. thread. To control the way __boost_fiber__ schedules ready fibers on a
  15. particular thread, in general you must follow several steps. This section
  16. discusses those steps, whereas [link scheduling Scheduling] serves as a
  17. reference for the classes involved.
  18. The library's fiber manager keeps track of suspended (blocked) fibers. Only
  19. when a fiber becomes ready to run is it passed to the scheduler. Of course, if
  20. there are fewer than two ready fibers, the scheduler's job is trivial. Only
  21. when there are two or more ready fibers does the particular scheduler
  22. implementation start to influence the overall sequence of fiber execution.
  23. In this section we illustrate a simple custom scheduler that honors an integer
  24. fiber priority. We will implement it such that a fiber with higher priority is
  25. preferred over a fiber with lower priority. Any fibers with equal priority
  26. values are serviced on a round-robin basis.
  27. [/ @path link is relative to (eventual) doc/html/index.html, hence ../..]
  28. The full source code for the examples below is found in
  29. [@../../examples/priority.cpp priority.cpp].
  30. [heading Custom Property Class]
  31. The first essential point is that we must associate an integer priority with
  32. each fiber.[footnote A previous version of the Fiber library implicitly
  33. tracked an int priority for each fiber, even though the default scheduler
  34. ignored it. This has been dropped, since the library now supports arbitrary
  35. scheduler-specific fiber properties.]
  36. One might suggest deriving a custom [class_link fiber] subclass to store such
  37. properties. There are a couple of reasons for the present mechanism.
  38. # __boost_fiber__ provides a number of different ways to launch a fiber.
  39. (Consider [ns_function_link fibers..async].) Higher-level libraries might
  40. introduce additional such wrapper functions. A custom scheduler must
  41. associate its custom properties with ['every] fiber in the thread, not only
  42. the ones explicitly launched by instantiating a custom `fiber` subclass.
  43. # Consider a large existing program that launches fibers in many different
  44. places in the code. We discover a need to introduce a custom scheduler for a
  45. particular thread. If supporting that scheduler's custom properties required
  46. a particular `fiber` subclass, we would have to hunt down and modify every
  47. place that launches a fiber on that thread.
  48. # The [class_link fiber] class is actually just a handle to internal
  49. [class_link context] data. A subclass of `fiber` would not add data to
  50. `context`.
  51. The present mechanism allows you to ["drop in] a custom scheduler with its
  52. attendant custom properties ['without] altering the rest of your application.
  53. Instead of deriving a custom scheduler fiber properties subclass from
  54. [class_link fiber], you must instead derive it from [class_link
  55. fiber_properties].
  56. [priority_props]
  57. [heading Custom Scheduler Class]
  58. Now we can derive a custom scheduler from [template_link
  59. algorithm_with_properties], specifying our custom property class
  60. `priority_props` as the template parameter.
  61. [priority_scheduler]
  62. Our example `priority_scheduler` doesn't override [member_link
  63. algorithm_with_properties..new_properties]: we're content with
  64. allocating `priority_props` instances on the heap.
  65. [heading Replace Default Scheduler]
  66. You must call [function_link use_scheduling_algorithm] at the start of each
  67. thread on which you want __boost_fiber__ to use your custom scheduler rather
  68. than its own default [class_link round_robin]. Specifically, you must call
  69. `use_scheduling_algorithm()` before performing any other __boost_fiber__
  70. operations on that thread.
  71. [main]
  72. [heading Use Properties]
  73. The running fiber can access its own [class_link fiber_properties] subclass
  74. instance by calling [ns_function_link this_fiber..properties]. Although
  75. `properties<>()` is a nullary function, you must pass, as a template
  76. parameter, the `fiber_properties` subclass.
  77. [main_name]
  78. Given a [class_link fiber] instance still connected with a running fiber (that
  79. is, not [member_link fiber..detach]ed), you may access that fiber's properties
  80. using [template_member_link fiber..properties]. As with
  81. `boost::this_fiber::properties<>()`, you must pass your `fiber_properties` subclass
  82. as the template parameter.
  83. [launch]
  84. Launching a new fiber schedules that fiber as ready, but does ['not]
  85. immediately enter its ['fiber-function]. The current fiber retains control
  86. until it blocks (or yields, or terminates) for some other reason. As shown in
  87. the `launch()` function above, it is reasonable to launch a fiber and
  88. immediately set relevant properties -- such as, for instance, its priority.
  89. Your custom scheduler can then make use of this information next time the
  90. fiber manager calls [member_link algorithm_with_properties..pick_next].
  91. [endsect]