Skip to content

Commit

Permalink
Merge pull request #1395 from Barenboim/master
Browse files Browse the repository at this point in the history
Fix timer bug of accessing SleepSession after added to poller.
  • Loading branch information
Barenboim authored Oct 16, 2023
2 parents f94bbb6 + c4049c0 commit 5b9be7a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
10 changes: 2 additions & 8 deletions src/kernel/Communicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1846,18 +1846,12 @@ int Communicator::shutdown(CommSession *session)
int Communicator::sleep(SleepSession *session)
{
struct timespec value;
void *timer;
int index;

if (session->duration(&value) >= 0)
{
timer = mpoller_add_timer(&value, session, &index, this->mpoller);
if (timer)
{
session->timer = timer;
session->index = index;
if (mpoller_add_timer(&value, session, &session->timer, &session->index,
this->mpoller) >= 0)
return 0;
}
}

return -1;
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/mpoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ static inline int mpoller_set_timeout(int fd, int timeout, mpoller_t *mpoller)
return poller_set_timeout(fd, timeout, mpoller->poller[index]);
}

static inline void *mpoller_add_timer(const struct timespec *value,
void *context, int *index,
mpoller_t *mpoller)
static inline int mpoller_add_timer(const struct timespec *value, void *context,
void **timer, int *index,
mpoller_t *mpoller)
{
static unsigned int n = 0;
*index = n++ % mpoller->nthreads;
return poller_add_timer(value, context, mpoller->poller[*index]);
return poller_add_timer(value, context, timer, mpoller->poller[*index]);
}

static inline int mpoller_del_timer(void *timer, int index, mpoller_t *mpoller)
Expand Down
10 changes: 6 additions & 4 deletions src/kernel/poller.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,8 +1527,8 @@ int poller_set_timeout(int fd, int timeout, poller_t *poller)
return -!node;
}

void *poller_add_timer(const struct timespec *value, void *context,
poller_t *poller)
int poller_add_timer(const struct timespec *value, void *context, void **timer,
poller_t *poller)
{
struct __poller_node *node;

Expand All @@ -1552,12 +1552,14 @@ void *poller_add_timer(const struct timespec *value, void *context,
node->timeout.tv_sec++;
}

*timer = node;
pthread_mutex_lock(&poller->mutex);
__poller_insert_node(node, poller);
pthread_mutex_unlock(&poller->mutex);
return 0;
}

return node;
return -1;
}

int poller_del_timer(void *timer, poller_t *poller)
Expand All @@ -1578,7 +1580,7 @@ int poller_del_timer(void *timer, poller_t *poller)
node->error = 0;
node->state = PR_ST_DELETED;
stopped = poller->stopped;
if (!poller->stopped)
if (!stopped)
write(poller->pipe_wr, &node, sizeof (void *));
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int poller_add(const struct poller_data *data, int timeout, poller_t *poller);
int poller_del(int fd, poller_t *poller);
int poller_mod(const struct poller_data *data, int timeout, poller_t *poller);
int poller_set_timeout(int fd, int timeout, poller_t *poller);
void *poller_add_timer(const struct timespec *value, void *context,
poller_t *poller);
int poller_add_timer(const struct timespec *value, void *context, void **timer,
poller_t *poller);
int poller_del_timer(void *timer, poller_t *poller);
void poller_stop(poller_t *poller);
void poller_destroy(poller_t *poller);
Expand Down

0 comments on commit 5b9be7a

Please sign in to comment.