Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tmr: prevent race condition on cancel #1048

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/re_tmr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


#include "re_thread.h"
#include "re_atomic.h"

/**
* Defines the timeout handler
Expand All @@ -19,7 +20,8 @@ struct tmrl;
/** Defines a timer */
struct tmr {
struct le le; /**< Linked list element */
mtx_t *lock; /**< Mutex lock */
RE_ATOMIC bool active; /**< Timer is active */
mtx_t *llock; /**< List Mutex lock */
tmr_h *th; /**< Timeout handler */
void *arg; /**< Handler argument */
uint64_t jfs; /**< Jiffies for timeout */
Expand Down
18 changes: 13 additions & 5 deletions src/tmr/tmr.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,16 @@ static void tmr_startcont_dbg(struct tmr *tmr, uint64_t delay, bool syncnow,
if (!tmr || !tmrl)
return;

if (!tmr->lock || !tmr->le.list)
lock = tmrl->lock;
/* Prevent multiple cancel race conditions */
if (!re_atomic_acq(&tmr->active) && !th)
return;

re_atomic_rls_set(&tmr->active, false);

if (!tmr->llock || !tmr->le.list)
lock = tmrl->lock; /* use current list lock */
else
lock = tmr->lock; /* use old lock for unlinking */
lock = tmr->llock; /* use old list lock for unlinking */

mtx_lock(lock);

Expand All @@ -413,10 +419,10 @@ static void tmr_startcont_dbg(struct tmr *tmr, uint64_t delay, bool syncnow,
tmr->arg = arg;
tmr->file = file;
tmr->line = line;
tmr->lock = tmrl->lock;
tmr->llock = tmrl->lock;

if (!th) {
tmr->lock = NULL;
tmr->llock = NULL;
mtx_unlock(lock);
return;
}
Expand Down Expand Up @@ -445,6 +451,8 @@ static void tmr_startcont_dbg(struct tmr *tmr, uint64_t delay, bool syncnow,
}
}

re_atomic_rls_set(&tmr->active, true);

mtx_unlock(lock);
}

Expand Down
Loading