From 5d4474d587ae2faa5b43d4058419b2232f8e9d96 Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Sun, 26 May 2024 14:58:20 +0000 Subject: [PATCH] change idle table interval and require tid on accessing threadlocal items --- tunnels/adapters/listener/udp/udp_listener.c | 2 +- ww/idle_table.c | 9 +++++---- ww/idle_table.h | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tunnels/adapters/listener/udp/udp_listener.c b/tunnels/adapters/listener/udp/udp_listener.c index 364c96af..5bfded38 100644 --- a/tunnels/adapters/listener/udp/udp_listener.c +++ b/tunnels/adapters/listener/udp/udp_listener.c @@ -198,7 +198,7 @@ static void onFilteredRecv(hevent_t *ev) udp_payload_t *data = (udp_payload_t *) hevent_userdata(ev); hash_t peeraddr_hash = sockAddrCalcHash((sockaddr_u *) hio_peeraddr(data->sock->io)); - idle_item_t *idle = getIdleItemByHash(data->sock->table, peeraddr_hash); + idle_item_t *idle = getIdleItemByHash(data->tid,data->sock->table, peeraddr_hash); if (idle == NULL) { udp_listener_con_state_t *con = diff --git a/ww/idle_table.c b/ww/idle_table.c index a3630149..908959e8 100644 --- a/ww/idle_table.c +++ b/ww/idle_table.c @@ -9,6 +9,7 @@ #include #include #include +#include enum { @@ -57,7 +58,7 @@ idle_table_t *newIdleTable(hloop_t *loop) { idle_table_t *newtable = malloc(sizeof(idle_table_t)); *newtable = (idle_table_t){.loop = loop, - .idle_handle = htimer_add_period(loop, idleCallBack, 1, 0, 0, 0, 0, INFINITE), + .idle_handle = htimer_add(loop, idleCallBack, 1000, INFINITE), .hqueue = heapq_idles_t_with_capacity(kVecCap), .hmap = hmap_idles_t_with_capacity(kVecCap), .last_update_ms = hloop_now_ms(loop)}; @@ -90,12 +91,12 @@ void keepIdleItemForAtleast(idle_table_t *self, idle_item_t *item, uint64_t age_ heapq_idles_t_make_heap(&self->hqueue); hhybridmutex_unlock(&(self->mutex)); } -idle_item_t *getIdleItemByHash(idle_table_t *self, hash_t key) +idle_item_t *getIdleItemByHash(uint8_t tid, idle_table_t *self, hash_t key) { hhybridmutex_lock(&(self->mutex)); hmap_idles_t_iter find_result = hmap_idles_t_find(&(self->hmap), key); - if (find_result.ref == hmap_idles_t_end(&(self->hmap)).ref) + if (find_result.ref == hmap_idles_t_end(&(self->hmap)).ref || find_result.ref->second->tid != tid) { hhybridmutex_unlock(&(self->mutex)); return NULL; @@ -150,7 +151,7 @@ bool removeIdleItemByHash(idle_table_t *self, hash_t key) static void beforeCloseCallBack(hevent_t *ev) { - idle_item_t *item = hevent_userdata(ev); + idle_item_t *item = hevent_userdata(ev); item->cb(item); free(item); } diff --git a/ww/idle_table.h b/ww/idle_table.h index c335b9a9..2c070d21 100644 --- a/ww/idle_table.h +++ b/ww/idle_table.h @@ -14,7 +14,7 @@ you also can keep updating the item timeout The time checking has no cost and won't syscall at all, and the checking is synced by the - eventloop which by default wakes up every 100 ms. (debug note: current interval is set to 60s) + eventloop which by default wakes up every 100 ms. (debug note: current interval is set to 1s) idle item is a threadlocal item, it belongs to the thread that created it and other threads must not change , remove or do anything to it @@ -43,6 +43,6 @@ idle_item_t *newIdleItem(idle_table_t *self, hash_t key, void *userdata, Expire uint64_t age_ms); // [Notice] only the owner thread of an idle item can use these functios on it -idle_item_t *getIdleItemByHash(idle_table_t *self, hash_t key); +idle_item_t *getIdleItemByHash(uint8_t tid, idle_table_t *self, hash_t key); void keepIdleItemForAtleast(idle_table_t *self, idle_item_t *item, uint64_t age_ms); bool removeIdleItemByHash(idle_table_t *self, hash_t key);