[ctime] fixup of data racing (#8187)

Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
Shell 2023-10-26 13:56:27 +08:00 committed by GitHub
parent 01672f8627
commit eafb04e011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View File

@ -24,6 +24,7 @@
* adapt to new api and do the signal handling in thread context * 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-08-12 Meco Man re-implement RT-Thread lightweight timezone API
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2023-10-23 Shell add lock for _g_timerid
*/ */
#include "sys/time.h" #include "sys/time.h"
@ -880,6 +881,7 @@ static void rtthread_timer_wrapper(void *timerobj)
} }
#define TIMER_ID_MAX 50 #define TIMER_ID_MAX 50
static struct rt_spinlock _timer_id_lock = RT_SPINLOCK_INIT;
static struct timer_obj *_g_timerid[TIMER_ID_MAX]; static struct timer_obj *_g_timerid[TIMER_ID_MAX];
static void *timer_id[TIMER_ID_MAX]; static void *timer_id[TIMER_ID_MAX];
static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id); static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id);
@ -975,8 +977,14 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
_timerid = resource_id_get(&id_timer); _timerid = resource_id_get(&id_timer);
if (_timerid < 0) if (_timerid < 0)
{ {
LOG_E("_timerid overflow!"); #ifdef RT_USING_SMART
return -1; /* todo:memory leak */ rt_free(param);
#endif /* RT_USING_SMART */
rt_ktime_hrtimer_detach(&timer->hrtimer);
rt_free(timer);
rt_set_errno(ENOMEM);
return -1;
} }
_g_timerid[_timerid] = timer; _g_timerid[_timerid] = timer;
@ -1005,17 +1013,20 @@ int timer_delete(timer_t timerid)
return -1; return -1;
} }
if (_g_timerid[ktimerid] == NULL) RT_DEBUG_NOT_IN_INTERRUPT;
{ rt_spin_lock(&_timer_id_lock);
rt_set_errno(EINVAL);
LOG_E("can not find timer!");
return -1;
}
timer = _g_timerid[ktimerid]; timer = _g_timerid[ktimerid];
resource_id_put(&id_timer, ktimerid); if (timer != NULL)
{
_g_timerid[ktimerid] = RT_NULL;
resource_id_put(&id_timer, ktimerid);
}
rt_spin_unlock(&_timer_id_lock);
if (timer == RT_NULL) if (timer == RT_NULL)
{ {
rt_set_errno(EINVAL); rt_set_errno(EINVAL);
LOG_D("can not find timer %ld", ktimerid);
return -1; return -1;
} }

View File

@ -55,7 +55,7 @@ static rt_ipc_msg_t _ipc_msg_free_list = (rt_ipc_msg_t)RT_NULL; /* released chai
static int rt_ipc_msg_used = 0; /* first unallocated entry */ static int rt_ipc_msg_used = 0; /* first unallocated entry */
static struct rt_ipc_msg ipc_msg_pool[RT_CH_MSG_MAX_NR]; /* initial message array */ static struct rt_ipc_msg ipc_msg_pool[RT_CH_MSG_MAX_NR]; /* initial message array */
static rt_spinlock_t ipc_big_lock; static struct rt_spinlock ipc_big_lock;
#define ipc_list_lock ipc_big_lock #define ipc_list_lock ipc_big_lock
#define ipc_ch_lock ipc_big_lock #define ipc_ch_lock ipc_big_lock

View File

@ -544,8 +544,8 @@ rt_thread_t rt_thread_defunct_dequeue(void);
/* /*
* spinlock * spinlock
*/ */
#ifdef RT_USING_SMP
struct rt_spinlock; struct rt_spinlock;
#ifdef RT_USING_SMP
void rt_spin_lock_init(struct rt_spinlock *lock); void rt_spin_lock_init(struct rt_spinlock *lock);
void rt_spin_lock(struct rt_spinlock *lock); void rt_spin_lock(struct rt_spinlock *lock);
@ -553,11 +553,11 @@ void rt_spin_unlock(struct rt_spinlock *lock);
rt_base_t rt_spin_lock_irqsave(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); void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level);
#else #else
#define rt_spin_lock_init(lock) { RT_UNUSED(lock); } #define rt_spin_lock_init(lock) do {RT_UNUSED(lock);} while (0)
#define rt_spin_lock(lock) { RT_UNUSED(lock); } #define rt_spin_lock(lock) do {RT_UNUSED(lock);} while (0)
#define rt_spin_unlock(lock) { RT_UNUSED(lock); } #define rt_spin_unlock(lock) do {RT_UNUSED(lock);} while (0)
#define rt_spin_lock_irqsave(lock) ({ RT_UNUSED(lock); rt_hw_interrupt_disable(); }) rt_inline rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock) {RT_UNUSED(lock);return rt_hw_interrupt_disable();}
#define rt_spin_unlock_irqrestore(lock, level) { RT_UNUSED(lock); rt_hw_interrupt_enable(level); } #define rt_spin_unlock_irqrestore(lock, level) do {RT_UNUSED(lock); rt_hw_interrupt_enable(level);} while (0)
#endif /* RT_USING_SMP */ #endif /* RT_USING_SMP */
/**@}*/ /**@}*/