Skip to content

Commit

Permalink
main: remove fhs cache handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Jan 25, 2023
1 parent b147526 commit 5c6fa78
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ enum {

/** File descriptor handler struct */
struct fhs {
struct le he; /**< Hash entry */
struct le le; /**< Cache/Delete entry */
struct le le; /**< Hash/Delete entry */
bool active; /**< In-use */
re_sock_t fd; /**< File Descriptor */
int flags; /**< Polling flags (Read, Write, etc.) */
Expand All @@ -81,7 +80,6 @@ struct fhs {
struct re {
struct hash *fhl; /**< File descriptor hash list */
struct list fhs_delete; /**< File descriptor delete list */
struct list fhs_cache; /**< File descriptor cache list */
int maxfds; /**< Maximum number of polling fds */
int nfds; /**< Number of active file descriptors */
enum poll_method method; /**< The current polling method */
Expand Down Expand Up @@ -123,7 +121,6 @@ static void re_destructor(void *arg)
hash_flush(re->fhl);
mem_deref(re->fhl);
list_flush(&re->fhs_delete);
list_flush(&re->fhs_cache);
}


Expand Down Expand Up @@ -161,7 +158,6 @@ int re_alloc(struct re **rep)

list_init(&re->tmrl);
list_init(&re->fhs_delete);
list_init(&re->fhs_cache);

re->tid = thrd_current();

Expand Down Expand Up @@ -580,11 +576,11 @@ static int poll_setup(struct re *re)
}


static void fhs_destroy(void *data)
static void fhs_destructor(void *data)
{
struct fhs *fhs = data;

hash_unlink(&fhs->he);
list_unlink(&fhs->le);
}


Expand All @@ -598,14 +594,8 @@ static int fhs_update(struct re *re, struct fhs **fhsp, re_sock_t fd,
if (he) {
fhs = he->data;
}
else if (!list_isempty(&re->fhs_cache)) {
struct le *le = list_head(&re->fhs_cache);

fhs = le->data;
list_unlink(le);
}
else {
fhs = mem_zalloc(sizeof(struct fhs), fhs_destroy);
fhs = mem_zalloc(sizeof(struct fhs), fhs_destructor);
if (!fhs)
return ENOMEM;
}
Expand All @@ -618,7 +608,7 @@ static int fhs_update(struct re *re, struct fhs **fhsp, re_sock_t fd,
if (!he) {
fhs->active = true;
++re->nfds;
hash_append(re->fhl, fhs_hash(re, fd), &fhs->he, fhs);
hash_append(re->fhl, fhs_hash(re, fd), &fhs->le, fhs);
}

*fhsp = fhs;
Expand Down Expand Up @@ -705,11 +695,13 @@ int fd_listen(re_sock_t fd, int flags, fd_h *fh, void *arg)
/* Stop listening */
if (!flags) {
fhs->active = false;
if (re_atomic_rlx(&re->polling))
if (re_atomic_rlx(&re->polling)) {
hash_unlink(&fhs->le);
list_append(&re->fhs_delete, &fhs->le, fhs);
else
list_append(&re->fhs_cache, &fhs->le, fhs);
hash_unlink(&fhs->he);
}
else {
mem_deref(fhs);
}
--re->nfds;
}

Expand Down Expand Up @@ -947,11 +939,13 @@ static int fd_poll(struct re *re)
--n;
}

/* Delayed fhs_cache move to avoid stale fhs pointers and allow fhs
* reuse */
LIST_FOREACH(&re->fhs_delete, le)
/* Delayed fhs destruct to avoid dangling fhs pointers */
le = re->fhs_delete.head;
while (le)
{
list_move(le, &re->fhs_cache);
struct fhs *fhs = le->data;
le = le->next;
mem_deref(fhs);
}

return 0;
Expand Down

0 comments on commit 5c6fa78

Please sign in to comment.