summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-06 14:19:26 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-06 14:19:26 +0200
commitf7804084f43403b1370851de9d459c49f55baa9a (patch)
tree1e5c3760c52190f489fb5f4f2d79a0baf77d2685
parente2c3fd4f88b095aca492c0df1a278c774b00492d (diff)
Clean up the refcounting to allow a proper exit on signal
-rw-r--r--announce.c1
-rw-r--r--main.c30
-rw-r--r--main.h2
-rw-r--r--uring.c22
-rw-r--r--uring.h2
5 files changed, 47 insertions, 10 deletions
diff --git a/announce.c b/announce.c
index 22dd080..f5233c4 100644
--- a/announce.c
+++ b/announce.c
@@ -118,6 +118,7 @@ announce_delete(struct cfg *cfg)
fprintf(stderr, "%s called, closing fd %i\n", __func__, cfg->aev->task.fd);
uring_cancel(cfg, &cfg->aev->task);
+ uring_task_put(cfg, &cfg->aev->mcast_task);
cfg->aev = NULL;
}
diff --git a/main.c b/main.c
index 1fc0e5d..398a998 100644
--- a/main.c
+++ b/main.c
@@ -22,6 +22,10 @@
int debuglvl = 0;
+bool exiting = false;
+
+struct cfg *cfghack = NULL;
+
void
debug(unsigned lvl, const char *fmt, ...)
{
@@ -57,8 +61,10 @@ cfg_free(struct uring_task *task)
{
struct cfg *cfg = container_of(task, struct cfg, task);
+ fprintf(stderr, "%s: called\n", __func__);
free(cfg);
- exit(EXIT_SUCCESS);
+ fprintf(stderr, "All resources free, exiting\n");
+ exiting = true;
}
static void
@@ -134,6 +140,7 @@ cfg_init(int argc, char **argv)
struct signalfd_ev {
struct uring_task task;
//struct signalfd_siginfo buf;
+ struct cfg *cfg;
uint64_t buf;
};
@@ -142,7 +149,8 @@ signalfd_free(struct uring_task *task)
{
struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task);
- debug(2, "Freeing signalfd\n");
+ fprintf(stderr, "%s: called\n", __func__);
+ sev->cfg->sev = NULL;
free(sev);
}
@@ -156,7 +164,8 @@ dump_tree(struct cfg *cfg)
fprintf(stderr, "============\n");
uring_task_refdump(&cfg->task);
uring_refdump(cfg->uev);
- uring_task_refdump(&cfg->sev->task);
+ if (cfg->sev)
+ uring_task_refdump(&cfg->sev->task);
announce_refdump(cfg->aev);
if (cfg->iev)
cfgdir_refdump(cfg->iev);
@@ -190,7 +199,9 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res)
cfgdir_delete(cfg);
list_for_each_entry_safe(server, stmp, &cfg->servers, list)
server_delete(cfg, server);
- uring_read(cfg, &sev->task, &sev->buf, sizeof(sev->buf), 0, signalfd_read);
+ fprintf(stderr, "%s: putting sev task 0x%p\n", __func__, &sev->task);
+ uring_task_put(cfg, &sev->task);
+ //uring_read(cfg, &sev->task, &sev->buf, sizeof(sev->buf), 0, signalfd_read);
}
}
@@ -200,11 +211,14 @@ static void
hack_handler(int signum)
{
uint64_t val;
+ static int count = 0;
switch (signum) {
case SIGINT:
fprintf(stderr, "Got a SIGINT\n");
val = 1000;
+ if (count > 3)
+ dump_tree(cfghack);
break;
case SIGHUP:
fprintf(stderr, "Got a SIGHUP\n");
@@ -221,6 +235,7 @@ hack_handler(int signum)
}
write(hack_efd, &val, sizeof(val));
+ count++;
}
static void
@@ -264,6 +279,7 @@ signalfd_init(struct cfg *cfg)
uring_task_init(&sev->task, "sev", uring_parent(cfg), signalfd_free);
uring_task_set_fd(&sev->task, sfd);
cfg->sev = sev;
+ sev->cfg = cfg;
hack_efd = sfd;
uring_read(cfg, &sev->task, &sev->buf, sizeof(sev->buf), 0, signalfd_read);
}
@@ -275,6 +291,8 @@ main(int argc, char **argv)
cfg = cfg_init(argc, argv);
+ cfghack = cfg;
+
cfg_read(cfg);
uring_init(cfg);
@@ -287,7 +305,11 @@ main(int argc, char **argv)
announce_start(cfg->aev);
+ uring_task_put(cfg, &cfg->task);
+
uring_event_loop(cfg);
+ fprintf(stderr, "Event loop exited\n");
+
exit(EXIT_SUCCESS);
}
diff --git a/main.h b/main.h
index d327059..e6ced6f 100644
--- a/main.h
+++ b/main.h
@@ -5,6 +5,8 @@
#include <netinet/ip.h>
#include "utils.h"
+extern bool exiting;
+
extern int debuglvl;
void debug(unsigned lvl, const char *fmt, ...);
diff --git a/uring.c b/uring.c
index 3f089c8..20d8234 100644
--- a/uring.c
+++ b/uring.c
@@ -12,6 +12,7 @@
struct uring_ev {
struct io_uring uring;
struct io_uring_params uring_params;
+ struct cfg *cfg;
struct uring_task task;
};
@@ -271,7 +272,13 @@ uring_cancel(struct cfg *cfg, struct uring_task *task)
static void
uring_free(struct uring_task *task)
{
- fprintf(stderr, "%s called\n", __func__);
+ struct uring_ev *uev = container_of(task, struct uring_ev, task);
+
+ fprintf(stderr, "%s: called\n", __func__);
+
+ io_uring_queue_exit(&uev->uring);
+ uev->cfg->uev = NULL;
+ free(uev);
}
void
@@ -296,9 +303,10 @@ uring_init(struct cfg *cfg)
uring_task_init(&uev->task, "uev", &cfg->task, uring_free);
cfg->uev = uev;
+ uev->cfg = cfg;
}
-int
+void
uring_event_loop(struct cfg *cfg)
{
while (true) {
@@ -320,17 +328,21 @@ uring_event_loop(struct cfg *cfg)
struct uring_task *task = io_uring_cqe_get_data(cqe);
fprintf(stderr, "%s: got CEQ (res: %i, task: 0x%p, cb: 0x%p)\n", __func__, cqe->res, task, task ? task->callback : NULL);
+
if (task && task->callback)
task->callback(cfg, task, cqe->res);
- nr++;
+
if (task)
uring_task_put(cfg, task);
+
+ if (exiting)
+ return;
+
+ nr++;
}
printf("%s: %u CQEs treated\n", __func__, nr);
io_uring_cq_advance(&cfg->uev->uring, nr);
}
-
- return 0;
}
diff --git a/uring.h b/uring.h
index cd13cd4..ff6bfb9 100644
--- a/uring.h
+++ b/uring.h
@@ -44,6 +44,6 @@ void uring_refdump(struct uring_ev *uev);
void uring_init(struct cfg *cfg);
-int uring_event_loop(struct cfg *cfg);
+void uring_event_loop(struct cfg *cfg);
#endif