Skip to content

Commit

Permalink
[ctime] fixup of data racing
Browse files Browse the repository at this point in the history
Signed-off-by: Shell <[email protected]>
  • Loading branch information
polarvid committed Oct 25, 2023
1 parent 3283f54 commit 121d825
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
23 changes: 20 additions & 3 deletions components/libc/compilers/common/ctime.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* adapt to new api and do the signal handling in thread context
* 2023-08-12 Meco Man re-implement RT-Thread lightweight timezone API
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2023-10-23 Shell add lock for _g_timerid
*/

#include "sys/time.h"
Expand Down Expand Up @@ -880,6 +881,7 @@ static void rtthread_timer_wrapper(void *timerobj)
}

#define TIMER_ID_MAX 50
static struct rt_spinlock _timer_id_lock = RT_HW_SPIN_LOCK_INIT;
static struct timer_obj *_g_timerid[TIMER_ID_MAX];
static void *timer_id[TIMER_ID_MAX];
static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id);
Expand Down Expand Up @@ -972,11 +974,20 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
rt_ktime_hrtimer_init(&timer->hrtimer, timername, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
rtthread_timer_wrapper, timer);

RT_DEBUG_NOT_IN_INTERRUPT;
rt_spin_lock(&_timer_id_lock);
_timerid = resource_id_get(&id_timer);
rt_spin_unlock(&_timer_id_lock);

if (_timerid < 0)
{
LOG_E("_timerid overflow!");
return -1; /* todo:memory leak */
#ifdef RT_USING_SMART
rt_free(param);
#endif /* RT_USING_SMART */

rt_ktime_hrtimer_detach(&timer->hrtimer);
rt_free(timer);
return -ENOSPC;
}
_g_timerid[_timerid] = timer;

Expand Down Expand Up @@ -1008,11 +1019,17 @@ int timer_delete(timer_t timerid)
if (_g_timerid[ktimerid] == NULL)
{
rt_set_errno(EINVAL);
LOG_E("can not find timer!");
LOG_D("can not find timer %ld", ktimerid);
return -1;
}

RT_DEBUG_NOT_IN_INTERRUPT;
rt_spin_lock(&_timer_id_lock);
timer = _g_timerid[ktimerid];
_g_timerid[ktimerid] = RT_NULL;
resource_id_put(&id_timer, ktimerid);
rt_spin_unlock(&_timer_id_lock);

if (timer == RT_NULL)
{
rt_set_errno(EINVAL);
Expand Down
5 changes: 5 additions & 0 deletions include/rthw.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ int rt_hw_cpu_id(void);
void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
#endif

/* cpuport.h not defined, than use {0} as default */
#ifndef RT_HW_SPIN_LOCK_INIT
#define RT_HW_SPIN_LOCK_INIT {0}
#endif

#ifdef RT_USING_SMP

void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
Expand Down
12 changes: 6 additions & 6 deletions include/rtthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,20 +544,20 @@ rt_thread_t rt_thread_defunct_dequeue(void);
/*
* spinlock
*/
#ifdef RT_USING_SMP
struct rt_spinlock;
#ifdef RT_USING_SMP

void rt_spin_lock_init(struct rt_spinlock *lock);
void rt_spin_lock(struct rt_spinlock *lock);
void rt_spin_unlock(struct rt_spinlock *lock);
rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock);
void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level);
#else
#define rt_spin_lock_init(lock) { RT_UNUSED(lock); }
#define rt_spin_lock(lock) { RT_UNUSED(lock); }
#define rt_spin_unlock(lock) { RT_UNUSED(lock); }
#define rt_spin_lock_irqsave(lock) ({ RT_UNUSED(lock); rt_hw_interrupt_disable(); })
#define rt_spin_unlock_irqrestore(lock, level) { RT_UNUSED(lock); rt_hw_interrupt_enable(level); }
rt_inline void rt_spin_lock_init(struct rt_spinlock *lock) {RT_UNUSED(lock);}
rt_inline void rt_spin_lock(struct rt_spinlock *lock) {RT_UNUSED(lock);rt_enter_critical();}
rt_inline void rt_spin_unlock(struct rt_spinlock *lock) {RT_UNUSED(lock);rt_exit_critical();}
rt_inline rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock) {RT_UNUSED(lock);return rt_hw_interrupt_disable();}
rt_inline void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level) {RT_UNUSED(lock);rt_hw_interrupt_enable(level);}
#endif /* RT_USING_SMP */

/**@}*/
Expand Down

0 comments on commit 121d825

Please sign in to comment.