2 // Copyright Oliver Kowalke 2015.
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)
7 // based on https://github.com/atemerev/skynet from Alexander Temerev
13 #include <condition_variable>
26 #include <boost/fiber/all.hpp>
27 #include <boost/predef.h>
29 #include "barrier.hpp"
31 using clock_type
= std::chrono::steady_clock
;
32 using duration_type
= clock_type::duration
;
33 using time_point_type
= clock_type::time_point
;
34 using channel_type
= boost::fibers::buffered_channel
< std::uint64_t >;
35 using allocator_type
= boost::fibers::fixedsize_stack
;
36 using lock_type
= std::unique_lock
< std::mutex
>;
38 static bool done
= false;
39 static std::mutex mtx
{};
40 static boost::fibers::condition_variable_any cnd
{};
43 void skynet( allocator_type
& salloc
, channel_type
& c
, std::size_t num
, std::size_t size
, std::size_t div
) {
47 channel_type rc
{ 16 };
48 for ( std::size_t i
= 0; i
< div
; ++i
) {
49 auto sub_num
= num
+ i
* size
/ div
;
50 boost::fibers::fiber
{ boost::fibers::launch::dispatch
,
51 std::allocator_arg
, salloc
,
53 std::ref( salloc
), std::ref( rc
), sub_num
, size
/ div
, div
}.detach();
55 std::uint64_t sum
{ 0 };
56 for ( std::size_t i
= 0; i
< div
; ++i
) {
57 sum
+= rc
.value_pop();
63 void thread( std::uint32_t thread_count
, barrier
* b
) {
64 // thread registers itself at work-stealing scheduler
65 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::work_stealing
>( thread_count
);
68 cnd
.wait( lk
, [](){ return done
; });
74 // count of logical cpus
75 std::uint32_t thread_count
= std::thread::hardware_concurrency();
76 // main-thread registers itself at work-stealing scheduler
77 boost::fibers::use_scheduling_algorithm
< boost::fibers::algo::work_stealing
>( thread_count
);
78 barrier b
{ thread_count
};
79 std::size_t size
{ 1000000 };
80 std::size_t div
{ 10 };
81 // Windows 10 and FreeBSD require a fiber stack of 8kb
82 // otherwise the stack gets exhausted
83 // stack requirements must be checked for other OS too
84 #if BOOST_OS_WINDOWS || BOOST_OS_BSD
85 allocator_type salloc
{ 2*allocator_type::traits_type::page_size() };
87 allocator_type salloc
{ allocator_type::traits_type::page_size() };
89 std::uint64_t result
{ 0 };
91 std::vector
< std::thread
> threads
;
92 for ( std::uint32_t i
= 1 /* count main-thread */; i
< thread_count
; ++i
) {
94 threads
.emplace_back( thread
, thread_count
, & b
);
97 time_point_type start
{ clock_type::now() };
98 skynet( salloc
, rc
, 0, size
, div
);
99 result
= rc
.value_pop();
100 if ( 499999500000 != result
) {
101 throw std::runtime_error("invalid result");
103 auto duration
= clock_type::now() - start
;
108 for ( std::thread
& t
: threads
) {
111 std::cout
<< "duration: " << duration
.count() / 1000000 << " ms" << std::endl
;
113 } catch ( std::exception
const& e
) {
114 std::cerr
<< "exception: " << e
.what() << std::endl
;
116 std::cerr
<< "unhandled exception" << std::endl
;