From 2e1e93c3da154b760a8b0589ae626cde011aa0f6 Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Mon, 17 Jan 2022 13:10:43 +0530 Subject: [PATCH] Refactor code and remove rate-limiting logic --- main.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 32b4611..ac3582b 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ #define _GNU_SOURCE #include -#include #include #include #include @@ -12,8 +11,6 @@ #define LEN(arr) (sizeof(arr) / sizeof(arr[0])) #define MAX(a, b) (a > b ? a : b) - -#define POLL_INTERVAL 50 #define BLOCK(cmd, interval, signal) \ { "echo \"$(" cmd ")\"", interval, signal } @@ -176,10 +173,6 @@ void signalHandler() { break; } } - - // Clear the pipe after each poll to limit number of signals handled - while (read(signalFD, &info, sizeof(info)) != -1) - ; } void termHandler() { @@ -205,19 +198,14 @@ void setupSignals() { sigaddset(&sigset, SIGRTMIN + blocks[i].signal); signalFD = signalfd(-1, &sigset, 0); - fcntl(signalFD, F_SETFL, O_NONBLOCK); sigprocmask(SIG_BLOCK, &sigset, NULL); event.data.u32 = LEN(blocks) + 1; epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event); } void statusLoop() { - // Poll every `POLL_INTERVAL` milliseconds - const struct timespec pollInterval = {.tv_nsec = POLL_INTERVAL * 1000000L}; - struct timespec toSleep = pollInterval; - while (statusContinue) { - int eventCount = epoll_wait(epollFD, events, LEN(events), 1); + int eventCount = epoll_wait(epollFD, events, LEN(events), -1); for (int i = 0; i < eventCount; i++) { unsigned int id = events[i].data.u32; @@ -233,11 +221,6 @@ void statusLoop() { } if (eventCount != -1) writeStatus(); - - // Sleep for `pollInterval` even on being interrupted - while (nanosleep(&toSleep, &toSleep) == -1) - ; - toSleep = pollInterval; } } @@ -297,10 +280,11 @@ int main(const int argc, const char* argv[]) { init(); - if (fork()) - statusLoop(); - else + // Ensure that `timerLoop()` only runs in the fork + if (fork() == 0) timerLoop(); + else + statusLoop(); close(epollFD); close(signalFD);