Skip to content

Commit

Permalink
change idle table interval and require tid on accessing threadlocal i…
Browse files Browse the repository at this point in the history
…tems
  • Loading branch information
radkesvat committed May 26, 2024
1 parent bee2c65 commit 5d4474d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tunnels/adapters/listener/udp/udp_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
9 changes: 5 additions & 4 deletions ww/idle_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

enum
{
Expand Down Expand Up @@ -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)};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions ww/idle_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

0 comments on commit 5d4474d

Please sign in to comment.