summaryrefslogtreecommitdiff
path: root/announce.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-22 20:20:08 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-22 20:20:08 +0200
commit3d7ae10a541629727844163f7d64507baedd6c78 (patch)
treea31d9274c64c743cd944414ae4833b880283ffcc /announce.c
parentdc83b9bf92439f0472333dca0bfa1f7edda689b4 (diff)
Add a shared timer, remove timerfd usage from idle and announce
Diffstat (limited to 'announce.c')
-rw-r--r--announce.c91
1 files changed, 32 insertions, 59 deletions
diff --git a/announce.c b/announce.c
index 2510d5d..eea668c 100644
--- a/announce.c
+++ b/announce.c
@@ -1,48 +1,38 @@
#include <inttypes.h>
-#include <sys/timerfd.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
-#include <time.h>
#include "main.h"
#include "uring.h"
#include "announce.h"
#include "server.h"
+#include "ptimer.h"
struct announce {
uint64_t value;
struct uring_task task;
+ struct ptimer_task ptask;
int mcast_fd;
- time_t until;
+ bool active;
};
+#define ANNOUNCE_INTERVAL 3
+
static void
-announce_cb(struct uring_task *task, int res)
+announce_cb(struct ptimer_task *ptask)
{
- struct announce *announce = container_of(task, struct announce, task);
+ struct announce *announce = container_of(ptask, struct announce, ptask);
struct server *server;
- assert_return(task);
- assert_task_alive(DBG_ANN, task);
-
- if (res != sizeof(announce->value)) {
- error("timerfd_read: %m");
- return;
- }
+ assert_return(ptask);
+ assert_task_alive(DBG_ANN, &announce->task);
- if (announce->until != 0 && time(NULL) > announce->until) {
- debug(DBG_ANN, "stopping announcements");
- announce_stop();
- } else {
- debug(DBG_ANN, "announcing servers");
- list_for_each_entry(server, &cfg->servers, list)
- server_announce(server, announce->mcast_fd);
- }
-
- uring_read(&announce->task, &announce->value, sizeof(announce->value), announce_cb);
+ debug(DBG_ANN, "announcing servers");
+ list_for_each_entry(server, &cfg->servers, list)
+ server_announce(server, announce->mcast_fd);
}
static void
@@ -70,7 +60,8 @@ announce_delete()
{
assert_return(cfg->announce);
- debug(DBG_ANN, "closing fd %i", cfg->announce->task.fd);
+ debug(DBG_ANN, "called");
+ announce_stop();
uring_task_destroy(&cfg->announce->task);
cfg->announce = NULL;
}
@@ -78,53 +69,38 @@ announce_delete()
void
announce_stop()
{
- struct itimerspec tspec = {
- .it_interval = {
- .tv_sec = 0,
- .tv_nsec = 0
- },
- .it_value = {
- .tv_sec = 0,
- .tv_nsec = 0
- }
- };
+ struct announce *announce = cfg->announce;
- assert_return(cfg->announce);
+ assert_return_silent(announce && announce->active);
- if (timerfd_settime(cfg->announce->task.fd, 0, &tspec, NULL) != 0)
- error("timerfd_settime: %m");
+ ptimer_del_task(&announce->ptask);
}
void
announce_start(unsigned duration)
{
- struct itimerspec tspec = {
- .it_interval = {
- .tv_sec = 3,
- .tv_nsec = 0
- },
- .it_value = {
- .tv_sec = 3,
- .tv_nsec = 0
- }
- };
+ struct announce *announce = cfg->announce;
+ unsigned times;
- assert_return(cfg->announce);
+ assert_return_silent(announce);
- if (duration > 0)
- cfg->announce->until = time(NULL) + duration;
+ if (duration == 0)
+ times = 0;
else
- cfg->announce->until = 0;
+ times = MAX(announce->ptask.times,
+ DIV_ROUND_UP(duration, ANNOUNCE_INTERVAL));
- if (timerfd_settime(cfg->announce->task.fd, 0, &tspec, NULL) != 0)
- error("timerfd_settime: %m");
+ announce->ptask.times = times;
+ if (!announce->active) {
+ ptimer_add_task(&announce->ptask);
+ announce->active = true;
+ }
}
void
announce_init()
{
struct announce *announce;
- int afd;
int sfd;
assert_return(!cfg->announce);
@@ -133,18 +109,15 @@ announce_init()
if (!announce)
die("malloc: %m");
- afd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
- if (afd < 0)
- die("timerfd_create: %m");
-
sfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sfd < 0)
die("socket: %m");
uring_task_init(&announce->task, "announce", uring_parent(), announce_free);
- uring_task_set_fd(&announce->task, afd);
+ announce->ptask.interval = ANNOUNCE_INTERVAL;
+ announce->ptask.cb = announce_cb;
+ announce->active = false;
announce->mcast_fd = sfd;
cfg->announce = announce;
- uring_read(&announce->task, &announce->value, sizeof(announce->value), announce_cb);
}