fiber.qbk 21.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
[/
  (C) Copyright 2007-8 Anthony Williams.
  (C) Copyright 2011-12 Vicente J. Botet Escriba.
  (C) Copyright 2013 Oliver Kowalke.
  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE_1_0.txt or copy at
  http://www.boost.org/LICENSE_1_0.txt).
]

[section:fiber_mgmt Fiber management]

[heading Synopsis]

        #include <boost/fiber/all.hpp>

        namespace boost {
        namespace fibers {

        class fiber;
        bool operator<( fiber const& l, fiber const& r) noexcept;
        void swap( fiber & l, fiber & r) noexcept;

        template< typename SchedAlgo, typename ... Args >
        void use_scheduling_algorithm( Args && ... args);
        bool has_ready_fibers();

        namespace algo {

        struct algorithm;
        template< typename PROPS >
        struct algorithm_with_properties;
        class round_robin;
        class shared_round_robin;

        }}

        namespace this_fiber {

        fibers::id get_id() noexcept;
        void yield();
        template< typename Clock, typename Duration >
        void sleep_until( std::chrono::time_point< Clock, Duration > const& abs_time)
        template< typename Rep, typename Period >
        void sleep_for( std::chrono::duration< Rep, Period > const& rel_time); 
        template< typename PROPS >
        PROPS & properties();

        }


[heading Tutorial]

Each __fiber__ represents a micro-thread which will be launched and managed
cooperatively by a scheduler. Objects of type __fiber__ are move-only.

        boost::fibers::fiber f1; // not-a-fiber

        void f() {
            boost::fibers::fiber f2( some_fn);

            f1 = std::move( f2); // f2 moved to f1
        }


[heading Launching]

A new fiber is launched by passing an object of a callable type that can be
invoked with no parameters.
If the object must not be copied or moved, then ['std::ref] can be used to
pass in a reference to the function object. In this case, the user must ensure
that the referenced object outlives the newly-created fiber.

        struct callable {
            void operator()();
        };

        boost::fibers::fiber copies_are_safe() {
            callable x;
            return boost::fibers::fiber( x);
        } // x is destroyed, but the newly-created fiber has a copy, so this is OK

        boost::fibers::fiber oops() {
            callable x;
            return boost::fibers::fiber( std::ref( x) );
        } // x is destroyed, but the newly-created fiber still has a reference
          // this leads to undefined behaviour

The spawned __fiber__ does not immediately start running. It is enqueued in
the list of ready-to-run fibers, and will run when the scheduler gets around
to it.


[#exceptions]
[heading Exceptions]

An exception escaping from the function or callable object passed to the __fiber__
constructor calls `std::terminate()`.
If you need to know which exception was thrown, use __future__ or
__packaged_task__.


[heading Detaching]

A __fiber__ can be detached by explicitly invoking the __detach__ member
function. After __detach__ is called on a fiber object, that object represents
__not_a_fiber__. The fiber object may then safely be destroyed.

        boost::fibers::fiber( some_fn).detach();

__boost_fiber__ provides a number of ways to wait for a running fiber to
complete. You can coordinate even with a detached fiber using a [class_link
mutex], or [class_link condition_variable], or any of the other [link
synchronization synchronization objects] provided by the library.

If a detached fiber is still running when the thread[s] main fiber terminates,
the thread will not shut down.

[heading Joining]

In order to wait for a fiber to finish, the __join__ member function of the
__fiber__ object can be used. __join__ will block until the __fiber__ object
has completed.

        void some_fn() {
            ...
        }

        boost::fibers::fiber f( some_fn);
        ...
        f.join();

If the fiber has already completed, then __join__ returns immediately and
the joined __fiber__ object becomes __not_a_fiber__.


[heading Destruction]

When a __fiber__ object representing a valid execution context (the fiber is
__joinable__) is destroyed, the program terminates. If you intend the fiber to
outlive the __fiber__ object that launched it, use the __detach__ method.

        {
            boost::fibers::fiber f( some_fn);
        } // std::terminate() will be called

        {
            boost::fibers::fiber f(some_fn);
            f.detach();
        } // okay, program continues


[#class_fiber_id]
[heading Fiber IDs]

Objects of class __fiber_id__ can be used to identify fibers. Each running
__fiber__ has a unique __fiber_id__ obtainable from the corresponding __fiber__
by calling the __get_id__ member function.
Objects of class __fiber_id__ can be copied, and used as keys in associative
containers: the full range of comparison operators is provided.
They can also be written to an output stream using the stream insertion
operator, though the output format is unspecified.

Each instance of __fiber_id__ either refers to some fiber, or __not_a_fiber__.
Instances that refer to __not_a_fiber__ compare equal to each other, but
not equal to any instances that refer to an actual fiber. The comparison
operators on __fiber_id__ yield a total order for every non-equal __fiber_id__.

[#class_launch]
[heading Enumeration `launch`]

`launch` specifies whether control passes immediately into a
newly-launched fiber.

        enum class launch {
            dispatch,
            post
        };

[heading `dispatch`]
[variablelist
[[Effects:] [A fiber launched with `launch == dispatch` is entered
immediately. In other words, launching a fiber with `dispatch` suspends the
caller (the previously-running fiber) until the fiber scheduler has a chance
to resume it later.]]
]

[heading `post`]
[variablelist
[[Effects:] [A fiber launched with `launch == post` is passed to the
fiber scheduler as ready, but it is not yet entered. The caller (the
previously-running fiber) continues executing. The newly-launched fiber will
be entered when the fiber scheduler has a chance to resume it later.]]
[[Note:] [If `launch` is not explicitly specified, `post` is the default.]]
] 


[#class_fiber]
[section:fiber Class `fiber`]

        #include <boost/fiber/fiber.hpp>

        namespace boost {
        namespace fibers {

        class fiber {
        public:
            class id;

            constexpr fiber() noexcept;

            template< typename Fn, typename ... Args >
            fiber( Fn &&, Args && ...);

            template< typename Fn, typename ... Args >
            fiber( ``[link class_launch `launch`]``, Fn &&, Args && ...);

            template< typename __StackAllocator__, typename Fn, typename ... Args >
            fiber( __allocator_arg_t__, StackAllocator &&, Fn &&, Args && ...);

            template< typename __StackAllocator__, typename Fn, typename ... Args >
            fiber( ``[link class_launch `launch`]``, __allocator_arg_t__, StackAllocator &&, Fn &&, Args && ...);

            ~fiber();

            fiber( fiber const&) = delete;

            fiber & operator=( fiber const&) = delete;

            fiber( fiber &&) noexcept;

            fiber & operator=( fiber &&) noexcept;

            void swap( fiber &) noexcept;

            bool joinable() const noexcept;

            id get_id() const noexcept;

            void detach();

            void join();

            template< typename PROPS >
            PROPS & properties();
        };

        bool operator<( fiber const&, fiber const&) noexcept;

        void swap( fiber &, fiber &) noexcept;

        template< typename SchedAlgo, typename ... Args >
        void use_scheduling_algorithm( Args && ...) noexcept;

        bool has_ready_fibers() noexcept;

        }}


[heading Default constructor]

        constexpr fiber() noexcept;

[variablelist
[[Effects:] [Constructs a __fiber__ instance that refers to __not_a_fiber__.]]
[[Postconditions:] [`this->get_id() == fiber::id()`]]
[[Throws:] [Nothing]]
]

[#fiber_fiber]
[heading Constructor]

        template< typename Fn, typename ... Args >
        fiber( Fn && fn, Args && ... args);

        template< typename Fn, typename ... Args >
        fiber( ``[link class_launch `launch`]`` policy, Fn && fn, Args && ... args);

        template< typename __StackAllocator__, typename Fn, typename ... Args >
        fiber( __allocator_arg_t__, StackAllocator && salloc, Fn && fn, Args && ... args);

        template< typename __StackAllocator__, typename Fn, typename ... Args >
        fiber( ``[link class_launch `launch`]`` policy, __allocator_arg_t__, StackAllocator && salloc,
               Fn && fn, Args && ... args);

[variablelist
[[Preconditions:] [`Fn` must be copyable or movable.]]
[[Effects:] [`fn` is copied or moved into internal storage for access by the
new fiber. If [class_link launch] is specified (or defaulted) to
`post`, the new fiber is marked ["ready] and will be entered at the next
opportunity. If `launch` is specified as `dispatch`, the calling fiber
is suspended and the new fiber is entered immediately.]]
[[Postconditions:] [`*this` refers to the newly created fiber of execution.]]
[[Throws:] [__fiber_error__ if an error occurs.]]
[[Note:] [__StackAllocator__ is required to allocate a stack for the internal
__econtext__. If `StackAllocator` is not explicitly passed, the default stack
allocator depends on `BOOST_USE_SEGMENTED_STACKS`: if defined, you will get a
__segmented_stack__, else a __fixedsize_stack__.]]
[[See also:] [__allocator_arg_t__, [link stack Stack allocation]]]
]

[heading Move constructor]

        fiber( fiber && other) noexcept;

[variablelist
[[Effects:] [Transfers ownership of the fiber managed by `other` to the newly
constructed __fiber__ instance.]]
[[Postconditions:] [`other.get_id() == fiber::id()` and `get_id()` returns the
value of `other.get_id()` prior to the construction]]
[[Throws:] [Nothing]]
]

[heading Move assignment operator]

        fiber & operator=( fiber && other) noexcept;

[variablelist
[[Effects:] [Transfers ownership of the fiber managed by `other` (if any) to
`*this`.]]
[[Postconditions:] [`other->get_id() == fiber::id()` and `get_id()` returns the
value of `other.get_id()` prior to the assignment.]]
[[Throws:] [Nothing]]
]

[heading Destructor]

        ~fiber();

[variablelist
[[Effects:] [If the fiber is __joinable__, calls std::terminate. Destroys
`*this`.]]
[[Note:] [The programmer must ensure that the destructor is never executed while
the fiber is still __joinable__. Even if you know that the fiber has completed,
you must still call either __join__ or  __detach__ before destroying the `fiber`
object.]]
]

[member_heading fiber..joinable]

        bool joinable() const noexcept;

[variablelist
[[Returns:] [`true` if `*this` refers to a fiber of execution, which may or
may not have completed; otherwise `false`.]]
[[Throws:] [Nothing]]
]

[member_heading fiber..join]

        void join();

[variablelist
[[Preconditions:] [the fiber is __joinable__.]]
[[Effects:] [Waits for the referenced fiber of execution to complete.]]
[[Postconditions:] [The fiber of execution referenced on entry has completed.
`*this` no longer refers to any fiber of execution.]]
[[Throws:] [`fiber_error`]]
[[Error Conditions:] [
[*resource_deadlock_would_occur]: if `this->get_id() == boost::this_fiber::get_id()`.
[*invalid_argument]: if the fiber is not __joinable__.]]
]

[member_heading fiber..detach]

        void detach();

[variablelist
[[Preconditions:] [the fiber is __joinable__.]]
[[Effects:] [The fiber of execution becomes detached, and no longer has an
associated __fiber__ object.]]
[[Postconditions:] [`*this` no longer refers to any fiber of execution.]]
[[Throws:] [`fiber_error`]]
[[Error Conditions:] [
[*invalid_argument]: if the fiber is not __joinable__.]]
]

[member_heading fiber..get_id]

        fiber::id get_id() const noexcept;

[variablelist
[[Returns:] [If `*this` refers to a fiber of execution, an instance of
__fiber_id__ that represents that fiber. Otherwise returns a
default-constructed __fiber_id__.]]
[[Throws:] [Nothing]]
[[See also:] [[ns_function_link this_fiber..get_id]]]
]

[template_member_heading fiber..properties]

        template< typename PROPS >
        PROPS & properties();

[variablelist
[[Preconditions:] [`*this` refers to a fiber of execution. [function_link
use_scheduling_algorithm] has been called from this thread with a subclass of
[template_link algorithm_with_properties] with the same template
argument `PROPS`.]]
[[Returns:] [a reference to the scheduler properties instance for `*this`.]]
[[Throws:] [`std::bad_cast` if `use_scheduling_algorithm()` was called with a
`algorithm_with_properties` subclass with some other template parameter
than `PROPS`.]]
[[Note:] [[template_link algorithm_with_properties] provides a way for a
user-coded scheduler to associate extended properties, such as priority, with
a fiber instance. This method allows access to those user-provided properties.]]
[[See also:] [[link custom Customization]]]
]

[member_heading fiber..swap]

        void swap( fiber & other) noexcept;

[variablelist
[[Effects:] [Exchanges the fiber of execution associated with `*this` and
`other`, so `*this` becomes associated with the fiber formerly associated with
`other`, and vice-versa.]]
[[Postconditions:] [`this->get_id()` returns the same value as `other.get_id()`
prior to the call. `other.get_id()` returns the same value as `this->get_id()`
prior to the call.]]
[[Throws:] [Nothing]]
]

[function_heading_for swap..fiber]

    void swap( fiber & l, fiber & r) noexcept;

[variablelist
[[Effects:] [Same as `l.swap( r)`.]]
[[Throws:] [Nothing]]
]

[function_heading operator<]

        bool operator<( fiber const& l, fiber const& r) noexcept;

[variablelist
[[Returns:] [`true` if `l.get_id() < r.get_id()` is `true`, false otherwise.]]
[[Throws:] [Nothing.]]
]

[function_heading use_scheduling_algorithm]

    template< typename SchedAlgo, typename ... Args >
    void use_scheduling_algorithm( Args && ... args) noexcept;

[variablelist
[[Effects:] [Directs __boost_fiber__ to use `SchedAlgo`, which must be a
concrete subclass of __algo__, as the scheduling algorithm for all fibers in
the current thread. Pass any required `SchedAlgo` constructor arguments as
`args`.]]
[[Note:] [If you want a given thread to use a non-default scheduling
algorithm, make that thread call `use_scheduling_algorithm()` before any other
__boost_fiber__ entry point. If no scheduler has been set for the current
thread by the time __boost_fiber__ needs to use it, the library will
create a default [class_link round_robin] instance for this thread.]]
[[Throws:] [Nothing]]
[[See also:] [[link scheduling Scheduling], [link custom Customization]]]
]

[function_heading has_ready_fibers]

    bool has_ready_fibers() noexcept;

[variablelist
[[Returns:] [`true` if scheduler has fibers ready to run.]]
[[Throws:] [Nothing]]
[[Note:] [Can be used for work-stealing to find an idle scheduler.]]
]

[endsect] [/ section Class fiber]


[#class_id]
[section:id Class fiber::id]

        #include <boost/fiber/fiber.hpp>

        namespace boost {
        namespace fibers {

        class id {
        public:
            constexpr id() noexcept;

            bool operator==( id const&) const noexcept;

            bool operator!=( id const&) const noexcept;
            
            bool operator<( id const&) const noexcept;
            
            bool operator>( id const&) const noexcept;
            
            bool operator<=( id const&) const noexcept;
            
            bool operator>=( id const&) const noexcept;

            template< typename charT, class traitsT >
            friend std::basic_ostream< charT, traitsT > &
            operator<<( std::basic_ostream< charT, traitsT > &, id const&);
        };

        }}

[heading Constructor]

        constexpr id() noexcept;

[variablelist
[[Effects:] [Represents an instance of __not_a_fiber__.]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_equal..operator==]

        bool operator==( id const& other) const noexcept;

[variablelist
[[Returns:] [`true` if `*this` and `other` represent the same fiber,
or both represent __not_a_fiber__, `false` otherwise.]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_not_equal..operator!=]

        bool operator!=( id const& other) const noexcept;

[variablelist
[[Returns:] [[`! (other == * this)]]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_less..operator<]

        bool operator<( id const& other) const noexcept;

[variablelist
[[Returns:] [`true` if `*this != other` is true and the
implementation-defined total order of `fiber::id` values places `*this` before
`other`, false otherwise.]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_greater..operator>]

        bool operator>( id const& other) const noexcept;

[variablelist
[[Returns:] [`other < * this`]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_less_equal..operator<=]

        bool operator<=( id const& other) const noexcept;

[variablelist
[[Returns:] [`! (other < * this)`]]
[[Throws:] [Nothing.]]
]

[operator_heading id..operator_greater_equal..operator>=]

        bool operator>=( id const& other) const noexcept;

[variablelist
[[Returns:] [`! (* this < other)`]]
[[Throws:] [Nothing.]]
]

[heading operator<<]

        template< typename charT, class traitsT >
        std::basic_ostream< charT, traitsT > &
        operator<<( std::basic_ostream< charT, traitsT > & os, id const& other);

[variablelist
[[Efects:] [Writes the representation of `other` to stream `os`. The
representation is unspecified.]]
[[Returns:] [`os`]]
]


[endsect] [/ section Class fiber::id]


[section:this_fiber Namespace this_fiber]

In general, `this_fiber` operations may be called from the ["main] fiber
[mdash] the fiber on which function `main()` is entered [mdash] as well as
from an explicitly-launched thread[s] thread-function. That is, in many
respects the main fiber on each thread can be treated like an
explicitly-launched fiber.


        namespace boost {
        namespace this_fiber {

        fibers::fiber::id get_id() noexcept;
        void yield() noexcept;
        template< typename Clock, typename Duration >
        void sleep_until( std::chrono::time_point< Clock, Duration > const&);
        template< typename Rep, typename Period >
        void sleep_for( std::chrono::duration< Rep, Period > const&);
        template< typename PROPS >
        PROPS & properties();

        }}

[ns_function_heading this_fiber..get_id]

        #include <boost/fiber/operations.hpp>

        namespace boost {
        namespace fibers {

        fiber::id get_id() noexcept;

        }}

[variablelist
[[Returns:] [An instance of __fiber_id__ that represents the currently
executing fiber.]]
[[Throws:] [Nothing.]]
]

[ns_function_heading this_fiber..sleep_until]

        #include <boost/fiber/operations.hpp>

        namespace boost {
        namespace fibers {

        template< typename Clock, typename Duration >
        void sleep_until( std::chrono::time_point< Clock, Duration > const& abs_time);

        }}

[variablelist
[[Effects:] [Suspends the current fiber until the time point specified by
`abs_time` has been reached.]]
[[Throws:] [timeout-related exceptions.]]
[[Note:] [The current fiber will not resume before `abs_time`, but there are no
guarantees about how soon after `abs_time` it might resume.]]
[[Note:] [["timeout-related exceptions] are as defined in the C++ Standard,
section [*30.2.4 Timing specifications \[thread.req.timing\]]: ["A function
that takes an argument which specifies a timeout will throw if, during its
execution, a clock, time point, or time duration throws an exception. Such
exceptions are referred to as ['timeout-related exceptions.]]]]
]

[ns_function_heading this_fiber..sleep_for]

        #include <boost/fiber/operations.hpp>

        namespace boost {
        namespace fibers {

        template< class Rep, class Period >
        void sleep_for( std::chrono::duration< Rep, Period > const& rel_time);

        }}

[variablelist
[[Effects:] [Suspends the current fiber until the time duration specified by
`rel_time` has elapsed.]]
[[Throws:] [timeout-related exceptions.]]
[[Note:][The current fiber will not resume before `rel_time` has elapsed, but
there are no guarantees about how soon after that it might resume.]]
]

[ns_function_heading this_fiber..yield]

        #include <boost/fiber/operations.hpp>

        namespace boost {
        namespace fibers {

        void yield() noexcept;

        }}

[variablelist
[[Effects:] [Relinquishes execution control, allowing other fibers to run.]]
[[Throws:] [Nothing.]]
[[Note:] [A fiber that calls
`yield()` is not suspended: it is immediately passed to the scheduler as ready
to run.]]
]

[ns_function_heading this_fiber..properties]

        #include <boost/fiber/operations.hpp>

        namespace boost {
        namespace fibers {

        template< typename PROPS >
        PROPS & properties();

        }}

[variablelist
[[Preconditions:] [[function_link use_scheduling_algorithm] has been called
from this thread with a subclass of [template_link
algorithm_with_properties] with the same template argument `PROPS`.]]
[[Returns:] [a reference to the scheduler properties instance for the
currently running fiber.]]
[[Throws:] [`std::bad_cast` if `use_scheduling_algorithm()` was called with an
`algorithm_with_properties` subclass with some other template parameter
than `PROPS`.]]
[[Note:] [[template_link algorithm_with_properties] provides a way for a
user-coded scheduler to associate extended properties, such as priority, with
a fiber instance. This function allows access to those user-provided
properties.]]
[[Note:] [The first time this function is called from the main fiber of a
thread, it may internally yield, permitting other fibers to run.]]
[[See also:] [[link custom Customization]]]
]


[endsect] [/ section Namespace this_fiber]


[endsect] [/ section Fiber Management]