Skip to content

Commit aae2115

Browse files
author
Dmitry Malakhov
committed
add readme
1 parent c8cfba0 commit aae2115

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 🐟 COROSIG 🐟
2+
3+
Corosig (properly spelled as \[karа́s'ik\]) is a crossplatform (POSIX) library for using C++20 coroutines safely inside C library signal handlers.
4+
5+
## Why signal handlers are ✨different✨?
6+
7+
In POSIX, signal handlers are very restrictive environments. There is not a lot of things one can do safely inside it. There should be no allocations, syscalls which are not marked and calls to global objects must be kept to the minimum since they might be invalidated by the moment sighandler is entered.
8+
9+
Most of the time when signal is received it is possible to just set some flag somewhere, exit handler and after that handle occured signal somewhere in main event loop. This aproach is recommended, since it is much easier to follow.
10+
11+
## When returning to main loop is not possible?
12+
13+
Returning to main loop is not possible if a signal was raised by your implementation due to some horrific state occuring inside your program (such as std::terminate, std::abort, assertion, zero division, etc). If something scary has happened, then you cannot return to main loop, since there are invalidated objects somewhere. And in most cases you cannot know where, so there is noone to be trusted. This exact moment you should stay inside sighandler, send valuable diagnosing data (such as logs) somewhere they can be persisted for further studying.
14+
15+
And as any IO operation, this is faster and fancier when you do it asynchronously.
16+
17+
## Brief API overview
18+
19+
Library is within it's very early stage of development and interfaces may change in future
20+
21+
Currently, you have to do this in order to get access to coroutines in SIGFPE handler:
22+
23+
```cpp
24+
corosig::Fut<void> sighandler(int sig) noexcept {
25+
// here you can co_await and do any other nasty stuff
26+
}
27+
28+
int main() {
29+
// in bytes, how much stack space can be used
30+
// to allocate coroutine frames here
31+
constexpr auto REACTOR_MEMORY = 8000;
32+
corosig::set_sighandler<REACTOR_MEMORY, sighandler>(SIGFPE);
33+
34+
return 0;
35+
}
36+
```
37+
38+
## About Windows
39+
40+
Sighandlers in Winapi are a psyop. They are even more restrictive than POSIX' ones since they do not allow ANY syscalls inside them. This means no IO at all. Set some flags and exit. That's it.
41+
42+
However, Winapi has SEH which may be useful for the same thing signals are used in POSIX: crash handling. It is possible that in future library will become more crossplatform via some wrapper around setting sighandler vs setting SEH.
43+
44+
But currently I am not even sure if such thing as async IO during crash handling is going to be useful for Windows programmers since I am not a Windows programmer at all.

0 commit comments

Comments
 (0)