scheduling.qbk 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. [#scheduling]
  8. [section:scheduling Scheduling]
  9. The fibers in a thread are coordinated by a fiber manager. Fibers trade
  10. control cooperatively, rather than preemptively: the currently-running fiber
  11. retains control until it invokes some operation that passes control to the
  12. manager. Each time a fiber suspends (or yields), the fiber manager consults a
  13. scheduler to determine which fiber will run next.
  14. __boost_fiber__ provides the fiber manager, but the scheduler is a
  15. customization point. (See [link custom Customization].)
  16. Each thread has its own scheduler. Different threads in a process may use
  17. different schedulers. By default, __boost_fiber__ implicitly instantiates
  18. [class_link round_robin] as the scheduler for each thread.
  19. You are explicitly permitted to code your own __algo__ subclass. For the most
  20. part, your `algorithm` subclass need not defend against cross-thread
  21. calls: the fiber manager intercepts and defers such calls. Most
  22. `algorithm` methods are only ever directly called from the thread whose
  23. fibers it is managing [mdash] with exceptions as documented below.
  24. Your `algorithm` subclass is engaged on a particular thread by calling
  25. [function_link use_scheduling_algorithm]:
  26. void thread_fn() {
  27. boost::fibers::use_scheduling_algorithm< my_fiber_scheduler >();
  28. ...
  29. }
  30. A scheduler class must implement interface __algo__. __boost_fiber__ provides
  31. schedulers: __round_robin__, __work_stealing__, __numa_work_stealing__ and
  32. __shared_work__.
  33. void thread( std::uint32_t thread_count) {
  34. // thread registers itself at work-stealing scheduler
  35. boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( thread_count);
  36. ...
  37. }
  38. // count of logical cpus
  39. std::uint32_t thread_count = std::thread::hardware_concurrency();
  40. // start worker-threads first
  41. std::vector< std::thread > threads;
  42. for ( std::uint32_t i = 1 /* count start-thread */; i < thread_count; ++i) {
  43. // spawn thread
  44. threads.emplace_back( thread, thread_count);
  45. }
  46. // start-thread registers itself at work-stealing scheduler
  47. boost::fibers::use_scheduling_algorithm< boost::fibers::algo::work_stealing >( thread_count);
  48. ...
  49. The example spawns as many threads as `std::thread::hardware_concurrency()`
  50. returns.
  51. Each thread runs a __work_stealing__ scheduler. Each instance of this
  52. scheduler needs to know how many threads run the work-stealing scheduler in the
  53. program.
  54. If the local queue of one thread runs out of ready fibers, the thread tries to
  55. steal a ready fiber from another thread running this scheduler.
  56. [class_heading algorithm]
  57. `algorithm` is the abstract base class defining the interface that a
  58. fiber scheduler must implement.
  59. #include <boost/fiber/algo/algorithm.hpp>
  60. namespace boost {
  61. namespace fibers {
  62. namespace algo {
  63. struct algorithm {
  64. virtual ~algorithm();
  65. virtual void awakened( context *) noexcept = 0;
  66. virtual context * pick_next() noexcept = 0;
  67. virtual bool has_ready_fibers() const noexcept = 0;
  68. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
  69. virtual void notify() noexcept = 0;
  70. };
  71. }}}
  72. [member_heading algorithm..awakened]
  73. virtual void awakened( context * f) noexcept = 0;
  74. [variablelist
  75. [[Effects:] [Informs the scheduler that fiber `f` is ready to run. Fiber `f`
  76. might be newly launched, or it might have been blocked but has just been
  77. awakened, or it might have called [ns_function_link this_fiber..yield].]]
  78. [[Note:] [This method advises the scheduler to add fiber `f` to its collection
  79. of fibers ready to run. A typical scheduler implementation places `f` into a
  80. queue.]]
  81. [[See also:] [[class_link round_robin]]]
  82. ]
  83. [member_heading algorithm..pick_next]
  84. virtual context * pick_next() noexcept = 0;
  85. [variablelist
  86. [[Returns:] [the fiber which is to be resumed next, or `nullptr` if there is no
  87. ready fiber.]]
  88. [[Note:] [This is where the scheduler actually specifies the fiber which is to
  89. run next. A typical scheduler implementation chooses the head of the ready
  90. queue.]]
  91. [[See also:] [[class_link round_robin]]]
  92. ]
  93. [member_heading algorithm..has_ready_fibers]
  94. virtual bool has_ready_fibers() const noexcept = 0;
  95. [variablelist
  96. [[Returns:] [`true` if scheduler has fibers ready to run.]]
  97. ]
  98. [member_heading algorithm..suspend_until]
  99. virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept = 0;
  100. [variablelist
  101. [[Effects:] [Informs the scheduler that no fiber will be ready until
  102. time-point `abs_time`.]]
  103. [[Note:] [This method allows a custom scheduler to yield control to the
  104. containing environment in whatever way makes sense. The fiber manager is
  105. stating that `suspend_until()` need not return until `abs_time` [mdash] or
  106. [member_link algorithm..notify] is called [mdash] whichever comes first.
  107. The interaction with `notify()` means that, for instance, calling
  108. [@http://en.cppreference.com/w/cpp/thread/sleep_until
  109. `std::this_thread::sleep_until(abs_time)`] would be too simplistic.
  110. [member_link round_robin..suspend_until] uses a
  111. [@http://en.cppreference.com/w/cpp/thread/condition_variable
  112. `std::condition_variable`] to coordinate with [member_link
  113. round_robin..notify].]]
  114. [[Note:] [Given that `notify()` might be called from another thread, your
  115. `suspend_until()` implementation [mdash] like the rest of your
  116. `algorithm` implementation [mdash] must guard any data it shares with
  117. your `notify()` implementation.]]
  118. ]
  119. [member_heading algorithm..notify]
  120. virtual void notify() noexcept = 0;
  121. [variablelist
  122. [[Effects:] [Requests the scheduler to return from a pending call to
  123. [member_link algorithm..suspend_until].]]
  124. [[Note:] [Alone among the `algorithm` methods, `notify()` may be called
  125. from another thread. Your `notify()` implementation must guard any data it
  126. shares with the rest of your `algorithm` implementation.]]
  127. ]
  128. [class_heading round_robin]
  129. This class implements __algo__, scheduling fibers in round-robin fashion.
  130. #include <boost/fiber/algo/round_robin.hpp>
  131. namespace boost {
  132. namespace fibers {
  133. namespace algo {
  134. class round_robin : public algorithm {
  135. virtual void awakened( context *) noexcept;
  136. virtual context * pick_next() noexcept;
  137. virtual bool has_ready_fibers() const noexcept;
  138. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept;
  139. virtual void notify() noexcept;
  140. };
  141. }}}
  142. [member_heading round_robin..awakened]
  143. virtual void awakened( context * f) noexcept;
  144. [variablelist
  145. [[Effects:] [Enqueues fiber `f` onto a ready queue.]]
  146. [[Throws:] [Nothing.]]
  147. ]
  148. [member_heading round_robin..pick_next]
  149. virtual context * pick_next() noexcept;
  150. [variablelist
  151. [[Returns:] [the fiber at the head of the ready queue, or `nullptr` if the
  152. queue is empty.]]
  153. [[Throws:] [Nothing.]]
  154. [[Note:] [Placing ready fibers onto the tail of a queue, and returning them
  155. from the head of that queue, shares the thread between ready fibers in
  156. round-robin fashion.]]
  157. ]
  158. [member_heading round_robin..has_ready_fibers]
  159. virtual bool has_ready_fibers() const noexcept;
  160. [variablelist
  161. [[Returns:] [`true` if scheduler has fibers ready to run.]]
  162. [[Throws:] [Nothing.]]
  163. ]
  164. [member_heading round_robin..suspend_until]
  165. virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept;
  166. [variablelist
  167. [[Effects:] [Informs `round_robin` that no ready fiber will be available until
  168. time-point `abs_time`. This implementation blocks in
  169. [@http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until
  170. `std::condition_variable::wait_until()`].]]
  171. [[Throws:] [Nothing.]]
  172. ]
  173. [member_heading round_robin..notify]
  174. virtual void notify() noexcept = 0;
  175. [variablelist
  176. [[Effects:] [Wake up a pending call to [member_link
  177. round_robin..suspend_until], some fibers might be ready. This implementation
  178. wakes `suspend_until()` via
  179. [@http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all
  180. `std::condition_variable::notify_all()`].]]
  181. [[Throws:] [Nothing.]]
  182. ]
  183. [class_heading work_stealing]
  184. This class implements __algo__; if the local ready-queue runs out of ready fibers, ready fibers are stolen
  185. from other schedulers.[br]
  186. The victim scheduler (from which a ready fiber is stolen) is selected at random.
  187. [note Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported.]
  188. #include <boost/fiber/algo/work_stealing.hpp>
  189. namespace boost {
  190. namespace fibers {
  191. namespace algo {
  192. class work_stealing : public algorithm {
  193. public:
  194. work_stealing( std::uint32_t thread_count, bool suspend = false);
  195. work_stealing( work_stealing const&) = delete;
  196. work_stealing( work_stealing &&) = delete;
  197. work_stealing & operator=( work_stealing const&) = delete;
  198. work_stealing & operator=( work_stealing &&) = delete;
  199. virtual void awakened( context *) noexcept;
  200. virtual context * pick_next() noexcept;
  201. virtual bool has_ready_fibers() const noexcept;
  202. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept;
  203. virtual void notify() noexcept;
  204. };
  205. }}}
  206. [heading Constructor]
  207. work_stealing( std::uint32_t thread_count, bool suspend = false);
  208. [variablelist
  209. [[Effects:] [Constructs work-stealing scheduling algorithm. `thread_count` represents the number of threads
  210. running this algorithm.]]
  211. [[Throws:] [`system_error`]]
  212. [[Note:][If `suspend` is set to `true`, then the scheduler suspends if no ready fiber could be stolen.
  213. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote (other thread or
  214. fiber scheduler).]]
  215. ]
  216. [member_heading work_stealing..awakened]
  217. virtual void awakened( context * f) noexcept;
  218. [variablelist
  219. [[Effects:] [Enqueues fiber `f` onto the shared ready queue.]]
  220. [[Throws:] [Nothing.]]
  221. ]
  222. [member_heading work_stealing..pick_next]
  223. virtual context * pick_next() noexcept;
  224. [variablelist
  225. [[Returns:] [the fiber at the head of the ready queue, or `nullptr` if the
  226. queue is empty.]]
  227. [[Throws:] [Nothing.]]
  228. [[Note:] [Placing ready fibers onto the tail of the sahred queue, and returning them
  229. from the head of that queue, shares the thread between ready fibers in
  230. round-robin fashion.]]
  231. ]
  232. [member_heading work_stealing..has_ready_fibers]
  233. virtual bool has_ready_fibers() const noexcept;
  234. [variablelist
  235. [[Returns:] [`true` if scheduler has fibers ready to run.]]
  236. [[Throws:] [Nothing.]]
  237. ]
  238. [member_heading work_stealing..suspend_until]
  239. virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept;
  240. [variablelist
  241. [[Effects:] [Informs `work_stealing` that no ready fiber will be available until
  242. time-point `abs_time`. This implementation blocks in
  243. [@http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until
  244. `std::condition_variable::wait_until()`].]]
  245. [[Throws:] [Nothing.]]
  246. ]
  247. [member_heading work_stealing..notify]
  248. virtual void notify() noexcept = 0;
  249. [variablelist
  250. [[Effects:] [Wake up a pending call to [member_link
  251. work_stealing..suspend_until], some fibers might be ready. This implementation
  252. wakes `suspend_until()` via
  253. [@http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all
  254. `std::condition_variable::notify_all()`].]]
  255. [[Throws:] [Nothing.]]
  256. ]
  257. [class_heading shared_work]
  258. [note Because of the non-locality of data, ['shared_work] is less performant
  259. than __work_stealing__.]
  260. This class implements __algo__, scheduling fibers in round-robin fashion.
  261. Ready fibers are shared between all instances (running on different threads)
  262. of shared_work, thus the work is distributed equally over all threads.
  263. [note Worker-threads are stored in a static variable, dynamically adding/removing worker threads is not supported.]
  264. #include <boost/fiber/algo/shared_work.hpp>
  265. namespace boost {
  266. namespace fibers {
  267. namespace algo {
  268. class shared_work : public algorithm {
  269. shared_work();
  270. shared_work( bool suspend);
  271. virtual void awakened( context *) noexcept;
  272. virtual context * pick_next() noexcept;
  273. virtual bool has_ready_fibers() const noexcept;
  274. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept;
  275. virtual void notify() noexcept;
  276. };
  277. }}}
  278. [heading Constructor]
  279. shared_work();
  280. shared_work( bool suspend);
  281. [variablelist
  282. [[Effects:] [Constructs work-sharing scheduling algorithm.]]
  283. [[Throws:] [`system_error`]]
  284. [[Note:][If `suspend` is set to `true` (default is `false`), then the scheduler suspends if no ready fiber
  285. could be stolen. The scheduler will by woken up if a sleeping fiber times out or it was notified from remote
  286. (other thread or fiber scheduler).]]
  287. ]
  288. [member_heading shared_work..awakened]
  289. virtual void awakened( context * f) noexcept;
  290. [variablelist
  291. [[Effects:] [Enqueues fiber `f` onto the shared ready queue.]]
  292. [[Throws:] [Nothing.]]
  293. ]
  294. [member_heading shared_work..pick_next]
  295. virtual context * pick_next() noexcept;
  296. [variablelist
  297. [[Returns:] [the fiber at the head of the ready queue, or `nullptr` if the
  298. queue is empty.]]
  299. [[Throws:] [Nothing.]]
  300. [[Note:] [Placing ready fibers onto the tail of the shared queue, and returning them
  301. from the head of that queue, shares the thread between ready fibers in
  302. round-robin fashion.]]
  303. ]
  304. [member_heading shared_work..has_ready_fibers]
  305. virtual bool has_ready_fibers() const noexcept;
  306. [variablelist
  307. [[Returns:] [`true` if scheduler has fibers ready to run.]]
  308. [[Throws:] [Nothing.]]
  309. ]
  310. [member_heading shared_work..suspend_until]
  311. virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept;
  312. [variablelist
  313. [[Effects:] [Informs `shared_work` that no ready fiber will be available until
  314. time-point `abs_time`. This implementation blocks in
  315. [@http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until
  316. `std::condition_variable::wait_until()`].]]
  317. [[Throws:] [Nothing.]]
  318. ]
  319. [member_heading shared_work..notify]
  320. virtual void notify() noexcept = 0;
  321. [variablelist
  322. [[Effects:] [Wake up a pending call to [member_link
  323. shared_work..suspend_until], some fibers might be ready. This implementation
  324. wakes `suspend_until()` via
  325. [@http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all
  326. `std::condition_variable::notify_all()`].]]
  327. [[Throws:] [Nothing.]]
  328. ]
  329. [heading Custom Scheduler Fiber Properties]
  330. A scheduler class directly derived from __algo__ can use any information
  331. available from [class_link context] to implement the `algorithm`
  332. interface. But a custom scheduler might need to track additional properties
  333. for a fiber. For instance, a priority-based scheduler would need to track a
  334. fiber[s] priority.
  335. __boost_fiber__ provides a mechanism by which your custom scheduler can
  336. associate custom properties with each fiber.
  337. [class_heading fiber_properties]
  338. A custom fiber properties class must be derived from `fiber_properties`.
  339. #include <boost/fiber/properties.hpp>
  340. namespace boost {
  341. namespace fibers {
  342. class fiber_properties {
  343. public:
  344. fiber_properties( context *) noexcept;
  345. virtual ~fiber_properties();
  346. protected:
  347. void notify() noexcept;
  348. };
  349. }}
  350. [heading Constructor]
  351. fiber_properties( context * f) noexcept;
  352. [variablelist
  353. [[Effects:] [Constructs base-class component of custom subclass.]]
  354. [[Throws:] [Nothing.]]
  355. [[Note:] [Your subclass constructor must accept a `context*` and pass it
  356. to the base-class `fiber_properties` constructor.]]
  357. ]
  358. [member_heading fiber_properties..notify]
  359. void notify() noexcept;
  360. [variablelist
  361. [[Effects:] [Pass control to the custom [template_link
  362. algorithm_with_properties] subclass[s] [member_link
  363. algorithm_with_properties..property_change] method.]]
  364. [[Throws:] [Nothing.]]
  365. [[Note:] [A custom scheduler[s] [member_link
  366. algorithm_with_properties..pick_next] method might dynamically select
  367. from the ready fibers, or [member_link
  368. algorithm_with_properties..awakened] might instead insert each ready
  369. fiber into some form of ready queue for `pick_next()`. In the latter case, if
  370. application code modifies a fiber property (e.g. priority) that should affect
  371. that fiber[s] relationship to other ready fibers, the custom scheduler must be
  372. given the opportunity to reorder its ready queue. The custom property subclass
  373. should implement an access method to modify such a property; that access
  374. method should call `notify()` once the new property value has been stored.
  375. This passes control to the custom scheduler[s] `property_change()` method,
  376. allowing the custom scheduler to reorder its ready queue appropriately. Use at
  377. your discretion. Of course, if you define a property which does not affect the
  378. behavior of the `pick_next()` method, you need not call `notify()` when that
  379. property is modified.]]
  380. ]
  381. [template_heading algorithm_with_properties]
  382. A custom scheduler that depends on a custom properties class `PROPS` should be
  383. derived from `algorithm_with_properties<PROPS>`. `PROPS` should be
  384. derived from [class_link fiber_properties].
  385. #include <boost/fiber/algorithm.hpp>
  386. namespace boost {
  387. namespace fibers {
  388. namespace algo {
  389. template< typename PROPS >
  390. struct algorithm_with_properties {
  391. virtual void awakened( context *, PROPS &) noexcept = 0;
  392. virtual context * pick_next() noexcept;
  393. virtual bool has_ready_fibers() const noexcept;
  394. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
  395. virtual void notify() noexcept = 0;
  396. PROPS & properties( context *) noexcept;
  397. virtual void property_change( context *, PROPS &) noexcept;
  398. virtual fiber_properties * new_properties( context *);
  399. };
  400. }}}
  401. [member_heading algorithm_with_properties..awakened]
  402. virtual void awakened( context * f, PROPS & properties) noexcept;
  403. [variablelist
  404. [[Effects:] [Informs the scheduler that fiber `f` is ready to run, like
  405. [member_link algorithm..awakened]. Passes the fiber[s] associated `PROPS`
  406. instance.]]
  407. [[Throws:] [Nothing.]]
  408. [[Note:] [An `algorithm_with_properties<>` subclass must override this
  409. method instead of `algorithm::awakened()`.]]
  410. ]
  411. [member_heading algorithm_with_properties..pick_next]
  412. virtual context * pick_next() noexcept;
  413. [variablelist
  414. [[Returns:] [the fiber which is to be resumed next, or `nullptr` if there is no
  415. ready fiber.]]
  416. [[Throws:] [Nothing.]]
  417. [[Note:] [same as [member_link algorithm..pick_next]]]
  418. ]
  419. [member_heading algorithm_with_properties..has_ready_fibers]
  420. virtual bool has_ready_fibers() const noexcept;
  421. [variablelist
  422. [[Returns:] [`true` if scheduler has fibers ready to run.]]
  423. [[Throws:] [Nothing.]]
  424. [[Note:] [same as [member_link algorithm..has_ready_fibers]]]
  425. ]
  426. [member_heading algorithm_with_properties..suspend_until]
  427. virtual void suspend_until( std::chrono::steady_clock::time_point const& abs_time) noexcept = 0;
  428. [variablelist
  429. [[Effects:] [Informs the scheduler that no fiber will be ready until
  430. time-point `abs_time`.]]
  431. [[Note:] [same as [member_link algorithm..suspend_until]]]
  432. ]
  433. [member_heading algorithm_with_properties..notify]
  434. virtual void notify() noexcept = 0;
  435. [variablelist
  436. [[Effects:] [Requests the scheduler to return from a pending call to
  437. [member_link algorithm_with_properties..suspend_until].]]
  438. [[Note:] [same as [member_link algorithm..notify]]]
  439. ]
  440. [member_heading algorithm_with_properties..properties]
  441. PROPS& properties( context * f) noexcept;
  442. [variablelist
  443. [[Returns:] [the `PROPS` instance associated with fiber `f`.]]
  444. [[Throws:] [Nothing.]]
  445. [[Note:] [The fiber[s] associated `PROPS` instance is already passed to
  446. [member_link algorithm_with_properties..awakened] and [member_link
  447. algorithm_with_properties..property_change]. However, every [class_link
  448. algorithm] subclass is expected to track a collection of ready
  449. [class_link context] instances. This method allows your custom scheduler
  450. to retrieve the [class_link fiber_properties] subclass instance for any
  451. `context` in its collection.]]
  452. ]
  453. [member_heading algorithm_with_properties..property_change]
  454. virtual void property_change( context * f, PROPS & properties) noexcept;
  455. [variablelist
  456. [[Effects:] [Notify the custom scheduler of a possibly-relevant change to a
  457. property belonging to fiber `f`. `properties` contains the new values of
  458. all relevant properties.]]
  459. [[Throws:] [Nothing.]]
  460. [[Note:] [This method is only called when a custom [class_link
  461. fiber_properties] subclass explicitly calls [member_link
  462. fiber_properties..notify].]]
  463. ]
  464. [member_heading algorithm_with_properties..new_properties]
  465. virtual fiber_properties * new_properties( context * f);
  466. [variablelist
  467. [[Returns:] [A new instance of [class_link fiber_properties] subclass
  468. `PROPS`.]]
  469. [[Note:] [By default, `algorithm_with_properties<>::new_properties()`
  470. simply returns `new PROPS(f)`, placing the `PROPS` instance on the heap.
  471. Override this method to allocate `PROPS` some other way. The returned
  472. `fiber_properties` pointer must point to the `PROPS` instance to be associated
  473. with fiber `f`.]]
  474. ]
  475. [#context]
  476. [class_heading context]
  477. While you are free to treat `context*` as an opaque token, certain
  478. `context` members may be useful to a custom scheduler implementation.
  479. [#ready_queue_t]
  480. Of particular note is the fact that `context` contains a hook to participate
  481. in a [@http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html
  482. `boost::intrusive::list`] [^typedef][,]ed as
  483. `boost::fibers::scheduler::ready_queue_t`. This hook is reserved for use by
  484. [class_link algorithm] implementations. (For instance, [class_link
  485. round_robin] contains a `ready_queue_t` instance to manage its ready fibers.)
  486. See [member_link context..ready_is_linked], [member_link context..ready_link],
  487. [member_link context..ready_unlink].
  488. Your `algorithm` implementation may use any container you desire to
  489. manage passed `context` instances. `ready_queue_t` avoids some of the overhead
  490. of typical STL containers.
  491. #include <boost/fiber/context.hpp>
  492. namespace boost {
  493. namespace fibers {
  494. enum class type {
  495. none = ``['unspecified]``,
  496. main_context = ``['unspecified]``, // fiber associated with thread's stack
  497. dispatcher_context = ``['unspecified]``, // special fiber for maintenance operations
  498. worker_context = ``['unspecified]``, // fiber not special to the library
  499. pinned_context = ``['unspecified]`` // fiber must not be migrated to another thread
  500. };
  501. class context {
  502. public:
  503. class id;
  504. static context * active() noexcept;
  505. context( context const&) = delete;
  506. context & operator=( context const&) = delete;
  507. id get_id() const noexcept;
  508. void detach() noexcept;
  509. void attach( context *) noexcept;
  510. bool is_context( type) const noexcept;
  511. bool is_terminated() const noexcept;
  512. bool ready_is_linked() const noexcept;
  513. bool remote_ready_is_linked() const noexcept;
  514. bool wait_is_linked() const noexcept;
  515. template< typename List >
  516. void ready_link( List &) noexcept;
  517. template< typename List >
  518. void remote_ready_link( List &) noexcept;
  519. template< typename List >
  520. void wait_link( List &) noexcept;
  521. void ready_unlink() noexcept;
  522. void remote_ready_unlink() noexcept;
  523. void wait_unlink() noexcept;
  524. void suspend() noexcept;
  525. void schedule( context *) noexcept;
  526. };
  527. bool operator<( context const& l, context const& r) noexcept;
  528. }}
  529. [static_member_heading context..active]
  530. static context * active() noexcept;
  531. [variablelist
  532. [[Returns:] [Pointer to instance of current fiber.]]
  533. [[Throws:] [Nothing]]
  534. ]
  535. [member_heading context..get_id]
  536. context::id get_id() const noexcept;
  537. [variablelist
  538. [[Returns:] [If `*this` refers to a fiber of execution, an instance of
  539. __fiber_id__ that represents that fiber. Otherwise returns a
  540. default-constructed __fiber_id__.]]
  541. [[Throws:] [Nothing]]
  542. [[See also:] [[member_link fiber..get_id]]]
  543. ]
  544. [member_heading context..attach]
  545. void attach( context * f) noexcept;
  546. [variablelist
  547. [[Precondition:] [`this->get_scheduler() == nullptr`]]
  548. [[Effects:] [Attach fiber `f` to scheduler running `*this`.]]
  549. [[Postcondition:] [`this->get_scheduler() != nullptr`]]
  550. [[Throws:] [Nothing]]
  551. [[Note:] [A typical call: `boost::fibers::context::active()->attach(f);`]]
  552. [[Note:] [`f` must not be the running fiber[s] context. It must not be
  553. __blocked__ or terminated. It must not be a `pinned_context`. It must be
  554. currently detached. It must not currently be linked into an [class_link
  555. algorithm] implementation[s] ready queue. Most of these conditions are
  556. implied by `f` being owned by an `algorithm` implementation: that is, it
  557. has been passed to [member_link algorithm..awakened] but has not yet
  558. been returned by [member_link algorithm..pick_next]. Typically a
  559. `pick_next()` implementation would call `attach()` with the `context*` it is
  560. about to return. It must first remove `f` from its ready queue. You should
  561. never pass a `pinned_context` to `attach()` because you should never have
  562. called its `detach()` method in the first place.]]
  563. ]
  564. [member_heading context..detach]
  565. void detach() noexcept;
  566. [variablelist
  567. [[Precondition:] [`(this->get_scheduler() != nullptr) && ! this->is_context(pinned_context)`]]
  568. [[Effects:] [Detach fiber `*this` from its scheduler running `*this`.]]
  569. [[Throws:] [Nothing]]
  570. [[Postcondition:] [`this->get_scheduler() == nullptr`]]
  571. [[Note:] [This method must be called on the thread with which the fiber is
  572. currently associated. `*this` must not be the running fiber[s] context. It
  573. must not be __blocked__ or terminated. It must not be a `pinned_context`. It
  574. must not be detached already. It must not already be linked into an [class_link
  575. algorithm] implementation[s] ready queue. Most of these conditions are
  576. implied by `*this` being passed to [member_link algorithm..awakened]; an
  577. `awakened()` implementation must, however, test for `pinned_context`. It must
  578. call `detach()` ['before] linking `*this` into its ready queue.]]
  579. [[Note:] [In particular, it is erroneous to attempt to migrate a fiber from
  580. one thread to another by calling both `detach()` and `attach()` in the
  581. [member_link algorithm..pick_next] method. `pick_next()` is called on
  582. the intended destination thread. `detach()` must be called on the fiber[s]
  583. original thread. You must call `detach()` in the corresponding `awakened()`
  584. method.]]
  585. [[Note:] [Unless you intend make a fiber available for potential migration to
  586. a different thread, you should call neither `detach()` nor `attach()` with its
  587. `context`.]]
  588. ]
  589. [member_heading context..is_context]
  590. bool is_context( type t) const noexcept;
  591. [variablelist
  592. [[Returns:] [`true` if `*this` is of the specified type.]]
  593. [[Throws:] [Nothing]]
  594. [[Note:] [`type::worker_context` here means any fiber not special to the
  595. library. For `type::main_context` the `context` is associated with the ["main]
  596. fiber of the thread: the one implicitly created by the thread itself, rather
  597. than one explicitly created by __boost_fiber__. For `type::dispatcher_context`
  598. the `context` is associated with a ["dispatching] fiber, responsible for
  599. dispatching awakened fibers to a scheduler[s] ready-queue. The ["dispatching]
  600. fiber is an implementation detail of the fiber manager. The context of the
  601. ["main] or ["dispatching] fiber [mdash] any fiber for which
  602. `is_context(pinned_context)` is `true` [mdash] must never be passed to
  603. [member_link context..detach].]]
  604. ]
  605. [member_heading context..is_terminated]
  606. bool is_terminated() const noexcept;
  607. [variablelist
  608. [[Returns:] [`true` if `*this` is no longer a valid context.]]
  609. [[Throws:] [Nothing]]
  610. [[Note:] [The `context` has returned from its fiber-function and is
  611. no longer considered a valid context.]]
  612. ]
  613. [member_heading context..ready_is_linked]
  614. bool ready_is_linked() const noexcept;
  615. [variablelist
  616. [[Returns:] [`true` if `*this` is stored in an [class_link algorithm]
  617. implementation[s] ready-queue.]]
  618. [[Throws:] [Nothing]]
  619. [[Note:] [Specifically, this method indicates whether [member_link
  620. context..ready_link] has been called on `*this`. `ready_is_linked()` has
  621. no information about participation in any other containers.]]
  622. ]
  623. [member_heading context..remote_ready_is_linked]
  624. bool remote_ready_is_linked() const noexcept;
  625. [variablelist
  626. [[Returns:] [`true` if `*this` is stored in the fiber manager[s]
  627. remote-ready-queue.]]
  628. [[Throws:] [Nothing]]
  629. [[Note:] [A `context` signaled as ready by another thread is first stored in
  630. the fiber manager[s] remote-ready-queue. This is the mechanism by which the
  631. fiber manager protects an [class_link algorithm] implementation from
  632. cross-thread [member_link algorithm..awakened] calls.]]
  633. ]
  634. [member_heading context..wait_is_linked]
  635. bool wait_is_linked() const noexcept;
  636. [variablelist
  637. [[Returns:] [`true` if `*this` is stored in the wait-queue of some
  638. synchronization object.]]
  639. [[Throws:] [Nothing]]
  640. [[Note:] [The `context` of a fiber waiting on a synchronization object (e.g.
  641. `mutex`, `condition_variable` etc.) is stored in the wait-queue of that
  642. synchronization object.]]
  643. ]
  644. [member_heading context..ready_link]
  645. template< typename List >
  646. void ready_link( List & lst) noexcept;
  647. [variablelist
  648. [[Effects:] [Stores `*this` in ready-queue `lst`.]]
  649. [[Throws:] [Nothing]]
  650. [[Note:] [Argument `lst` must be a doubly-linked list from
  651. __boost_intrusive__, e.g. an instance of
  652. `boost::fibers::scheduler::ready_queue_t`. Specifically, it must be a
  653. [@http://www.boost.org/doc/libs/release/doc/html/intrusive/list.html
  654. `boost::intrusive::list`] compatible with the `list_member_hook` stored in the
  655. `context` object.]]
  656. ]
  657. [member_heading context..remote_ready_link]
  658. template< typename List >
  659. void remote_ready_link( List & lst) noexcept;
  660. [variablelist
  661. [[Effects:] [Stores `*this` in remote-ready-queue `lst`.]]
  662. [[Throws:] [Nothing]]
  663. [[Note:] [Argument `lst` must be a doubly-linked list from
  664. __boost_intrusive__.]]
  665. ]
  666. [member_heading context..wait_link]
  667. template< typename List >
  668. void wait_link( List & lst) noexcept;
  669. [variablelist
  670. [[Effects:] [Stores `*this` in wait-queue `lst`.]]
  671. [[Throws:] [Nothing]]
  672. [[Note:] [Argument `lst` must be a doubly-linked list from
  673. __boost_intrusive__.]]
  674. ]
  675. [member_heading context..ready_unlink]
  676. void ready_unlink() noexcept;
  677. [variablelist
  678. [[Effects:] [Removes `*this` from ready-queue: undoes the effect of
  679. [member_link context..ready_link].]]
  680. [[Throws:] [Nothing]]
  681. ]
  682. [member_heading context..remote_ready_unlink]
  683. void remote_ready_unlink() noexcept;
  684. [variablelist
  685. [[Effects:] [Removes `*this` from remote-ready-queue.]]
  686. [[Throws:] [Nothing]]
  687. ]
  688. [member_heading context..wait_unlink]
  689. void wait_unlink() noexcept;
  690. [variablelist
  691. [[Effects:] [Removes `*this` from wait-queue.]]
  692. [[Throws:] [Nothing]]
  693. ]
  694. [member_heading context..suspend]
  695. void suspend() noexcept;
  696. [variablelist
  697. [[Effects:] [Suspends the running fiber (the fiber associated with `*this`)
  698. until some other fiber passes `this` to [member_link context..schedule].
  699. `*this` is marked as not-ready, and control passes to the scheduler to select
  700. another fiber to run.]]
  701. [[Throws:] [Nothing]]
  702. [[Note:] [This is a low-level API potentially useful for integration with
  703. other frameworks. It is not intended to be directly invoked by a typical
  704. application program.]]
  705. [[Note:] [The burden is on the caller to arrange for a call to `schedule()`
  706. with a pointer to `this` at some future time.]]
  707. ]
  708. [member_heading context..schedule]
  709. void schedule( context * ctx ) noexcept;
  710. [variablelist
  711. [[Effects:] [Mark the fiber associated with context `*ctx` as being ready to
  712. run. This does not immediately resume that fiber; rather it passes the fiber
  713. to the scheduler for subsequent resumption. If the scheduler is idle (has not
  714. returned from a call to [member_link algorithm..suspend_until]),
  715. [member_link algorithm..notify] is called to wake it up.]]
  716. [[Throws:] [Nothing]]
  717. [[Note:] [This is a low-level API potentially useful for integration with
  718. other frameworks. It is not intended to be directly invoked by a typical
  719. application program.]]
  720. [[Note:] [It is explicitly supported to call `schedule(ctx)` from a thread
  721. other than the one on which `*ctx` is currently suspended. The corresponding
  722. fiber will be resumed on its original thread in due course.]]
  723. [/[[Note:] [See [member_link context..migrate] for a way to migrate the
  724. suspended thread to the thread calling `schedule()`.]]]
  725. ]
  726. [hding context_less..Non-member function [`operator<()]]
  727. bool operator<( context const& l, context const& r) noexcept;
  728. [variablelist
  729. [[Returns:] [`true` if `l.get_id() < r.get_id()` is `true`, `false`
  730. otherwise.]]
  731. [[Throws:] [Nothing.]]
  732. ]
  733. [endsect]