Skip to content

Commit 5e3e5ae

Browse files
committed
tlist: Add more locking
Signed-off-by: Jan Friesse <[email protected]>
1 parent 21bd7bf commit 5e3e5ae

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

include/tlist.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,22 @@ static inline int32_t timerlist_add_duration(struct timerlist *timerlist,
347347
return (0);
348348
}
349349

350-
static inline void timerlist_del(struct timerlist *timerlist,
350+
static inline int32_t timerlist_del(struct timerlist *timerlist,
351351
timer_handle _timer_handle)
352352
{
353353
struct timerlist_timer *timer = (struct timerlist_timer *)_timer_handle;
354354

355+
if (pthread_mutex_lock(&timerlist->list_mutex)) {
356+
return -errno;
357+
}
358+
355359
memset(timer->handle_addr, 0, sizeof(struct timerlist_timer *));
356360

357361
timerlist_heap_delete(timerlist, timer);
358362
free(timer);
363+
364+
pthread_mutex_unlock(&timerlist->list_mutex);
365+
return 0;
359366
}
360367

361368
static inline uint64_t timerlist_expire_time(struct timerlist
@@ -395,15 +402,29 @@ static inline uint64_t timerlist_msec_duration_to_expire(struct timerlist *timer
395402
volatile uint64_t current_time;
396403
volatile uint64_t msec_duration_to_expire;
397404

405+
/*
406+
* There is really no reasonable value to return when mutex lock fails
407+
*/
408+
if (pthread_mutex_lock(&timerlist->list_mutex)) {
409+
return (-1);
410+
}
411+
398412
/*
399413
* empty list, no expire
400414
*/
401415
if (timerlist->size == 0) {
416+
pthread_mutex_unlock(&timerlist->list_mutex);
417+
402418
return (-1);
403419
}
404420

405421
timer_from_list = timerlist_heap_entry_get(timerlist, 0);
406422

423+
/*
424+
* Mutex is no longer needed
425+
*/
426+
pthread_mutex_unlock(&timerlist->list_mutex);
427+
407428
if (timer_from_list->is_absolute_timer) {
408429
current_time = qb_util_nano_from_epoch_get();
409430
} else {
@@ -426,7 +447,7 @@ static inline uint64_t timerlist_msec_duration_to_expire(struct timerlist *timer
426447
/*
427448
* Expires any timers that should be expired
428449
*/
429-
static inline void timerlist_expire(struct timerlist *timerlist)
450+
static inline int32_t timerlist_expire(struct timerlist *timerlist)
430451
{
431452
struct timerlist_timer *timer;
432453
uint64_t current_time_from_epoch;
@@ -436,6 +457,10 @@ static inline void timerlist_expire(struct timerlist *timerlist)
436457
current_monotonic_time = qb_util_nano_current_get();
437458
current_time_from_epoch = qb_util_nano_from_epoch_get();
438459

460+
if (pthread_mutex_lock(&timerlist->list_mutex)) {
461+
return -errno;
462+
}
463+
439464
while (timerlist->size > 0) {
440465
timer = timerlist_heap_entry_get(timerlist, 0);
441466

@@ -455,5 +480,9 @@ static inline void timerlist_expire(struct timerlist *timerlist)
455480
break; /* for timer iteration */
456481
}
457482
}
483+
484+
pthread_mutex_unlock(&timerlist->list_mutex);
485+
486+
return (0);
458487
}
459488
#endif /* QB_TLIST_H_DEFINED */

lib/loop_timerlist.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ expire_the_timers(struct qb_loop_source *s, int32_t ms_timeout)
7676
{
7777
struct qb_timer_source *ts = (struct qb_timer_source *)s;
7878
expired_timers = 0;
79-
timerlist_expire(&ts->timerlist);
79+
if (timerlist_expire(&ts->timerlist) != 0) {
80+
qb_util_log(LOG_ERR, "timerlist_expire failed");
81+
}
8082
return expired_timers;
8183
}
8284

@@ -265,7 +267,9 @@ qb_loop_timer_del(struct qb_loop * lp, qb_loop_timer_handle th)
265267
}
266268

267269
if (t->timerlist_handle) {
268-
timerlist_del(&s->timerlist, t->timerlist_handle);
270+
if (timerlist_del(&s->timerlist, t->timerlist_handle) != 0) {
271+
qb_util_log(LOG_ERR, "Could not delete timer from timerlist");
272+
}
269273
}
270274
t->state = QB_POLL_ENTRY_EMPTY;
271275
return 0;

0 commit comments

Comments
 (0)