Skip to content

Commit

Permalink
Use sig_atomic_t Instead of bool for Signal Handling
Browse files Browse the repository at this point in the history
- Using `bool` in signal handlers leads to undefined behavior.
- `sig_atomic_t` ensures the flag is set reliably when catching signals.
  • Loading branch information
RaphiaRa committed Sep 30, 2024
1 parent c457ff9 commit 64ebad3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
7 changes: 3 additions & 4 deletions examples/custom_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#include <stdio.h>
#include <stdlib.h>

static bool running = true;
static sig_atomic_t stop = 0;

static void
sigint_handler(int signum)
{
(void)signum;
running = false;
stop = signum;
}

static th_err
Expand Down Expand Up @@ -64,7 +63,7 @@ int main(int argc, char** argv)
goto cleanup;
if ((err = th_route(server, TH_METHOD_GET, "/", handler, NULL)) != TH_ERR_OK)
goto cleanup;
while (running) {
while (!stop) {
th_poll(server, 1000);
}
fprintf(stderr, "Shutting down...\n");
Expand Down
10 changes: 5 additions & 5 deletions examples/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <stdlib.h>
#include <string.h>

static bool running = true;
static sig_atomic_t stop = 0;

static void sigint_handler(int signum)
static void
sigint_handler(int signum)
{
(void)signum;
running = false;
stop = signum;
}

const char* method_strings[] = {
Expand Down Expand Up @@ -68,7 +68,7 @@ int main(int argc, char** argv)
goto cleanup;
if ((err = th_route(server, TH_METHOD_ANY, "/{path:path}", handler, NULL)) != TH_ERR_OK)
goto cleanup;
while (running) {
while (!stop) {
th_poll(server, 1000);
}
fprintf(stderr, "Shutting down...\n");
Expand Down
7 changes: 3 additions & 4 deletions examples/file_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#include <stdlib.h>
#include <string.h>

static bool running = true;
static sig_atomic_t stop = 0;

static void
sigint_handler(int signum)
{
(void)signum;
running = false;
stop = signum;
}

static th_err
Expand Down Expand Up @@ -89,7 +88,7 @@ int main(int argc, char** argv)
goto cleanup;
if ((err = th_route(server, TH_METHOD_GET, "/", handle_index, NULL)) != TH_ERR_OK)
goto cleanup;
while (running) {
while (!stop) {
th_poll(server, 1000);
}
fprintf(stderr, "Shutting down...\n");
Expand Down
7 changes: 3 additions & 4 deletions examples/hello_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#include <stdlib.h>
#include <string.h>

static bool running = true;
static sig_atomic_t stop = 0;

static void
sigint_handler(int signum)
{
(void)signum;
running = false;
stop = signum;
}

static th_err
Expand All @@ -37,7 +36,7 @@ int main(int argc, char** argv)
goto cleanup;
if ((err = th_route(server, TH_METHOD_GET, "/", handler, NULL)) != TH_ERR_OK)
goto cleanup;
while (running) {
while (!stop) {
th_poll(server, 1000);
}
fprintf(stderr, "Shutting down...\n");
Expand Down

0 comments on commit 64ebad3

Please sign in to comment.