Skip to content

Commit 535f512

Browse files
committed
UPD | docs: threadpoolfs & blocked signals
1 parent 3fbd398 commit 535f512

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: Blocked Signals
3+
description: Managing Blocked Signals
4+
---
5+
6+
In networking, errors can sometimes generate special signals that need to be handled. If ignored, these signals may terminate our program unexpectedly.
7+
8+
```cpp
9+
#include <manapihttp/ManapiEventLoop.hpp>
10+
#include <manapihttp/std/ManapiAsyncContext.hpp>
11+
12+
int main(int argc, char *argv[]) {
13+
auto blockedsignals = manapi::async::context::blockedsignals();
14+
15+
// Ignore SIGABRT
16+
sigaddset(blockedsignals.get(), SIGABRT);
17+
pthread_sigmask(SIG_BLOCK, blockedsignals.get(), nullptr);
18+
19+
manapi::async::context::gbs(std::move(blockedsignals));
20+
21+
return 0;
22+
}
23+
```
24+
25+
<Callout title="Information">```manapi::async::context::blockedsignals()``` returns a sigset_t with ```SIGPIPE``` already blocked by default.</Callout>
26+
27+
For most use cases, the default configuration is sufficient:
28+
29+
```cpp
30+
#include <manapihttp/ManapiEventLoop.hpp>
31+
#include <manapihttp/std/ManapiAsyncContext.hpp>
32+
33+
int main(int argc, char *argv[]) {
34+
manapi::async::context::gbs(manapi::async::context::blockedsignals());
35+
36+
return 0;
37+
}
38+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: ThreadPoolFs
3+
description: Thread Pool for Blocking Filesystem Operations
4+
---
5+
6+
Operating systems contain many blocking operations. To handle these efficiently, ManapiHttp provides the ThreadPoolFs method, which sets up a thread pool specifically for blocking operations (particularly filesystem operations). Users can also add their own tasks to ThreadPoolFs. Note that ThreadPoolFs cannot be modified while the event loop is running.
7+
8+
<Callout title="Information">The default size for ThreadPoolFs is 4 threads.</Callout>
9+
10+
### Initialize ThreadPoolFs
11+
```cpp
12+
#include <manapihttp/std/ManapiAsyncContext.hpp>
13+
14+
int main(int argc, char *argv[]) {
15+
/* Configures event loops to use 2 threads for blocking I/O system calls */
16+
manapi::async::context::threadpoolfs(2);
17+
18+
return 0;
19+
}
20+
```
21+
22+
### Create a Task
23+
```cpp
24+
#include <chrono>
25+
#include <thread>
26+
27+
#include <manapihttp/ManapiDebug.hpp>
28+
#include <manapihttp/ManapiEventLoop.hpp>
29+
#include <manapihttp/std/ManapiAsyncContext.hpp>
30+
31+
int main(int argc, char *argv[]) {
32+
manapi::async::context::threadpoolfs(2);
33+
auto ctx = manapi::async::context::create().unwrap();
34+
35+
ctx->run([](std::function<void()> bind) -> void {
36+
manapi::async::current()->eventloop()->append_task([](const manapi::ev::shared_work &w) -> void {
37+
// This executes in a worker thread
38+
assert(!manapi::async::context_exists());
39+
40+
std::chrono::seconds duration(5);
41+
std::this_thread::sleep_for(duration);
42+
43+
manapi_log_debug("Waiting has finished");
44+
}, [](const manapi::ev::shared_work &w, int status) -> void {
45+
// This executes back in the main event loop thread
46+
assert(manapi::async::context_exists());
47+
48+
manapi_log_debug("Waiting has finished with status: %d", status);
49+
50+
// Stop the event loop
51+
manapi::async::run(manapi::async::current()->eventloop()->stop());
52+
});
53+
54+
manapi_log_debug("Waiting has started");
55+
56+
bind();
57+
});
58+
59+
return 0;
60+
}
61+
```

docs/content/docs/meta.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"intro/install",
66
"intro/quick-start",
77
"intro/syslogs",
8+
"intro/threadpoolfs",
9+
"intro/blockedsignals",
810
"---Data Formats---",
911
"data-formats/json"
1012
]

0 commit comments

Comments
 (0)