Skip to content

Commit b06b5b3

Browse files
committed
Alignment; wait_any, wait all, fixups
1 parent d7076f9 commit b06b5b3

File tree

10 files changed

+340
-325
lines changed

10 files changed

+340
-325
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Currently only `Linux/x86_64` is supported. Other platforms to be supported in t
3232
- header-only
3333
- `std::thread` like interface
3434
- no external dependencies
35-
- easy to use, lightweight
35+
- simplicity, lightweight
3636
- make use of C++20 features
3737
- constexpr where possible
3838

@@ -64,8 +64,8 @@ Currently only `Linux/x86_64` is supported. Other platforms to be supported in t
6464

6565
# Examples
6666
## 1. A simple resumable function
67-
In the following example each iteration of `main` and `func` simply yields, printing the iteration count.
68-
Note to pass a reference to the fiber we need to use the `std::ref` wrapper. Before exiting `func` calls the built-in printer.
67+
In the following example each iteration of `main` and `func` simply yields, printing the iteration count before and after each yield.
68+
Note to pass a reference to the `flags` variable we need to use the `std::ref` wrapper. Before exiting `func` calls the built-in printer.
6969
When `func` exits, control returns to `main` where testing `f0` for non-zero returns false indicating the fiber has finished.
7070

7171
Note that you can name a fiber using `fiber_params`, and using designated initialisers (here we use `.name`).

examples/fibertest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ int main(void)
118118
std::cout << "main: " << std::dec << ii << '\n';
119119
//fibers::print(std::cout);
120120
}
121-
std::cout << "Exiting from main\nSizes\n";
121+
std::cout << "Exiting from main\n";
122+
std::cout << fibers::size_finished() << " fibers finished\n";
122123
std::cout << "fiber: " << sizeof(fiber) << '\n';
123124
std::cout << "fiber::cvars: " << sizeof(fiber::cvars) << '\n';
124125
std::cout << "fiber::all_cvars: " << sizeof(fiber::all_cvars) << '\n';
125126
std::cout << "fiber_id: " << sizeof(fiber_id) << '\n';
126127
std::cout << "fiber_base: " << sizeof(fiber_base) << '\n';
127-
std::cout <<"fiber_params: " << sizeof(fiber_params) << '\n';
128+
std::cout << "fiber_params: " << sizeof(fiber_params) << '\n';
128129
return 0;
129130
}

examples/fibertest26.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Reader : public fiber
7373
}
7474

7575
// non-blocking resumable reader
76+
// message: [header:12-16][' '][body:80-102][' '][trailer:10]
7677
void read_nb(int rdcnt)
7778
{
7879
int mread{};

examples/fibertest28.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//-----------------------------------------------------------------------------------------
2+
// fiber (header only)
3+
// Copyright (C) 2022-23 Fix8 Market Technologies Pty Ltd
4+
// by David L. Dight
5+
// see https://github.com/fix8mt/fiber
6+
//
7+
// Lightweight header-only stackful per-thread fiber
8+
// with built-in roundrobin scheduler x86_64 / linux only
9+
//
10+
// Distributed under the Boost Software License, Version 1.0 August 17th, 2003
11+
//
12+
// Permission is hereby granted, free of charge, to any person or organization
13+
// obtaining a copy of the software and accompanying documentation covered by
14+
// this license (the "Software") to use, reproduce, display, distribute,
15+
// execute, and transmit the Software, and to prepare derivative works of the
16+
// Software, and to permit third-parties to whom the Software is furnished to
17+
// do so, all subject to the following:
18+
//
19+
// The copyright notices in the Software and this entire statement, including
20+
// the above license grant, this restriction and the following disclaimer,
21+
// must be included in all copies of the Software, in whole or in part, and
22+
// all derivative works of the Software, unless such copies or derivative
23+
// works are solely in the form of machine-executable object code generated by
24+
// a source language processor.
25+
//
26+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32+
// DEALINGS IN THE SOFTWARE.
33+
//-----------------------------------------------------------------------------------------
34+
#include <iostream>
35+
#include <iomanip>
36+
#include <thread>
37+
#include <string>
38+
#include <fix8/fiber.hpp>
39+
40+
using namespace FIX8;
41+
42+
struct tell
43+
{
44+
tell() { std::cout << this_fiber::name() << ":entry (fiber id:" << this_fiber::get_id() << ")\n"; }
45+
~tell() { std::cout << this_fiber::name() << ":exit\n"; }
46+
47+
static void wrap_yield(int val)
48+
{
49+
std::cout << this_fiber::name() << ": " << val << '\n';
50+
this_fiber::yield();
51+
std::cout << this_fiber::name() << ": " << val << " (resumed)\n";
52+
}
53+
};
54+
55+
int main(int argc, char *argv[])
56+
{
57+
tell wrp;
58+
bool flag{};
59+
60+
std::cout << "flag=" << std::boolalpha << flag << '\n';
61+
fiber f0({.name="\tfunc"}, [](bool& flag, int cnt)
62+
{
63+
tell wrp1;
64+
fibers::print();
65+
for (int kk{}; kk < cnt; tell::wrap_yield(kk++));
66+
flag = true;
67+
}, std::ref(flag), 5);
68+
for (int ii{}; f0; tell::wrap_yield(ii++))
69+
;
70+
std::cout << "flag=" << std::boolalpha << flag << '\n';
71+
return 0;
72+
}
73+

examples/fibertest29.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//-----------------------------------------------------------------------------------------
2+
// fiber (header only)
3+
// Copyright (C) 2022-23 Fix8 Market Technologies Pty Ltd
4+
// by David L. Dight
5+
// see https://github.com/fix8mt/fiber
6+
//
7+
// Lightweight header-only stackful per-thread fiber
8+
// with built-in roundrobin scheduler x86_64 / linux only
9+
//
10+
// Distributed under the Boost Software License, Version 1.0 August 17th, 2003
11+
//
12+
// Permission is hereby granted, free of charge, to any person or organization
13+
// obtaining a copy of the software and accompanying documentation covered by
14+
// this license (the "Software") to use, reproduce, display, distribute,
15+
// execute, and transmit the Software, and to prepare derivative works of the
16+
// Software, and to permit third-parties to whom the Software is furnished to
17+
// do so, all subject to the following:
18+
//
19+
// The copyright notices in the Software and this entire statement, including
20+
// the above license grant, this restriction and the following disclaimer,
21+
// must be included in all copies of the Software, in whole or in part, and
22+
// all derivative works of the Software, unless such copies or derivative
23+
// works are solely in the form of machine-executable object code generated by
24+
// a source language processor.
25+
//
26+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32+
// DEALINGS IN THE SOFTWARE.
33+
//-----------------------------------------------------------------------------------------
34+
#include <iostream>
35+
#include <iomanip>
36+
#include <thread>
37+
#include <string>
38+
#include <fix8/fiber.hpp>
39+
40+
using namespace FIX8;
41+
using namespace std::literals;
42+
43+
void func (int loops)
44+
{
45+
std::cout << this_fiber::name() << ":entry (fiber id:" << this_fiber::get_id() << ")\n";
46+
for (int ii{}; ii < loops; ++ii)
47+
{
48+
std::cout << this_fiber::name() << ": " << ii << '\n';
49+
this_fiber::yield();
50+
std::this_thread::sleep_for(100ms);
51+
}
52+
this_fiber::schedule_main();
53+
std::cout << this_fiber::name() << ": exit\n";
54+
}
55+
56+
int main(int argc, char *argv[])
57+
{
58+
fiber f0({.name="func0"}, func, 5), f1({.name="func1"}, func, 10), f2({.name="func2"}, func, 15);
59+
argc > 1 ? fibers::wait_all() : fibers::wait_any();
60+
fibers::print();
61+
std::cout << fibers::size_finished() << " fibers finished, " << fibers::size() << " remaining\n";
62+
std::cout << fibers::kill_all() << " killed\n";
63+
return 0;
64+
}
65+

examples/montest1.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class foo
8585
//-----------------------------------------------------------------------------------------
8686
int main(int argc, char *argv[])
8787
{
88-
int interval{100}, fcnt{-1}, sleepval{50};
88+
constexpr int sleep_default{50}, interval_default{100};
89+
int interval{interval_default}, fcnt{-1}, sleepval{sleep_default}, val;
8990
bool lorder{true}, skip{true};
9091
fiber_monitor::sort_mode sm{ fiber_monitor::sort_mode::by_ms };
9192

@@ -98,45 +99,33 @@ int main(int argc, char *argv[])
9899
{ "fibers", 1, 0, 'f' }, { "mode", 1, 0, 'm' }
99100
}};
100101

101-
for (int opt; (opt = getopt_long (argc, argv, optstr, long_options.data(), 0)) != -1;)
102+
while ((val = getopt_long (argc, argv, optstr, long_options.data(), 0)) != -1)
102103
#else
103-
for (int opt; (opt = getopt (argc, argv, optstr)) != -1;)
104+
while ((val = getopt (argc, argv, optstr)) != -1)
104105
#endif
105106
{
106107
try
107108
{
108-
switch (opt)
109+
switch (val)
109110
{
110-
case 'f':
111-
fcnt = std::stoi(optarg);
112-
break;
113-
case 's':
114-
sleepval = std::stoi(optarg);
115-
break;
116111
case 'm':
117112
if (long unsigned ism{ std::stoul(optarg) - 1UL }; ism < static_cast<long unsigned>(fiber_monitor::sort_mode::count))
118113
sm = fiber_monitor::sort_mode(ism);
119114
break;
120-
case 'r':
121-
fibers::set_flag(global_fiber_flags::retain);
122-
break;
123-
case 'k':
124-
skip = false;
125-
break;
126-
case 'o':
127-
lorder = false;
128-
break;
129-
case 'i':
130-
interval = std::stoi(optarg);
131-
break;
115+
case 'f': fcnt = std::stoi(optarg); break;
116+
case 's': sleepval = std::stoi(optarg); break;
117+
case 'r': fibers::set_flag(global_fiber_flags::retain); break;
118+
case 'k': skip ^= true; break;
119+
case 'o': lorder ^= true; break;
120+
case 'i': interval = std::stoi(optarg); break;
132121
case '?':
133122
case ':':
134123
case 'h':
135124
std::cout << "Usage: " << argv[0] << " -[" << optstr << R"(]
136-
-i,--interval interval msecs (default 100)
125+
-i,--interval interval msecs (default )" << interval << R"()
137126
-f,--fibers fiber count (default )" << (fiber_monitor::get_terminal_dimensions().second - 4) << R"()
138-
-s,--sleep sleep msecs (default 50)
139-
-m,--mode sort mode )" << fiber_monitor::sort_help() << R"((default 3)
127+
-s,--sleep sleep msecs (default )" << sleepval << R"()
128+
-m,--mode sort mode )" << fiber_monitor::sort_help() << "(default " << (static_cast<long unsigned>(sm) + 1) << R"()
140129
-o,--order no launch order
141130
-k,--noskip no skip main
142131
-r,--interval retain finished fibers
@@ -166,16 +155,13 @@ int main(int argc, char *argv[])
166155
&foo::func, &bar, 2 * (ii + 1)))->set_params("sub"s + std::to_string(ii));
167156
}
168157

169-
while (fibers::has_fibers())
158+
fibers::wait_all([&fm]()
170159
{
171-
this_fiber::yield();
172160
if (!fm)
173-
{
174161
fibers::kill_all();
175-
break;
176-
}
177-
}
162+
return !fm;
163+
});
178164
fm.update();
179-
std::this_thread::sleep_for(1s);
165+
std::this_thread::sleep_for(2s);
180166
return 0;
181167
}

0 commit comments

Comments
 (0)