Blame view

3rdparty/boost_1_81_0/libs/fiber/src/fiber.cpp 1.83 KB
977ed18d   Hu Chunming   提交三方库
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
  
  //          Copyright Oliver Kowalke 2013.
  // 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)
  
  #include "boost/fiber/fiber.hpp"
  
  #include <system_error>
  
  #include <boost/assert.hpp>
  
  #include "boost/fiber/exceptions.hpp"
  #include "boost/fiber/scheduler.hpp"
  
  #ifdef BOOST_HAS_ABI_HEADERS
  #  include BOOST_ABI_PREFIX
  #endif
  
  namespace boost {
  namespace fibers {
  
  void
  fiber::start_() noexcept {
      context * ctx = context::active();
      ctx->attach( impl_.get() );
      switch ( impl_->get_policy() ) {
      case launch::post:
          // push new fiber to ready-queue
          // resume executing current fiber
          ctx->get_scheduler()->schedule( impl_.get() );
          break;
      case launch::dispatch:
          // resume new fiber and push current fiber
          // to ready-queue
          impl_->resume( ctx);
          break;
      default:
          BOOST_ASSERT_MSG( false, "unknown launch-policy");
      }
  }
  
  void
  fiber::join() {
      // FIXME: must fiber::join() be synchronized?
      if ( BOOST_UNLIKELY( context::active()->get_id() == get_id() ) ) {
          throw fiber_error{ std::make_error_code( std::errc::resource_deadlock_would_occur),
                             "boost fiber: trying to join itself" };
      }
      if ( BOOST_UNLIKELY( ! joinable() ) ) {
          throw fiber_error{ std::make_error_code( std::errc::invalid_argument),
                             "boost fiber: fiber not joinable" };
      }
      impl_->join();
      impl_.reset();
  }
  
  void
  fiber::detach() {
      if ( BOOST_UNLIKELY( ! joinable() ) ) {
          throw fiber_error{ std::make_error_code( std::errc::invalid_argument),
                             "boost fiber: fiber not joinable" };
      }
      impl_.reset();
  }
  
  }}
  
  #ifdef BOOST_HAS_ABI_HEADERS
  #  include BOOST_ABI_SUFFIX
  #endif