From 077e984175e546a495fee1ace6f2f54ff161fc77 Mon Sep 17 00:00:00 2001 From: zhkag Date: Thu, 29 Jun 2023 00:01:51 +0800 Subject: [PATCH] =?UTF-8?q?[libc]=E6=9B=B4=E6=96=B0=20libc=20timer=20id=20?= =?UTF-8?q?=E5=88=86=E9=85=8D=E6=96=B9=E5=BC=8F=20(#7744)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/ctime.c | 84 +++--------------------- components/libc/posix/Kconfig | 1 + components/libc/posix/ipc/Kconfig | 1 + components/utilities/Kconfig | 4 ++ components/utilities/resource/SConscript | 2 +- 5 files changed, 16 insertions(+), 76 deletions(-) diff --git a/components/libc/compilers/common/ctime.c b/components/libc/compilers/common/ctime.c index f3e7de2229..0e148ef56f 100644 --- a/components/libc/compilers/common/ctime.c +++ b/components/libc/compilers/common/ctime.c @@ -877,6 +877,8 @@ RTM_EXPORT(rt_timespec_to_tick); #ifdef RT_USING_POSIX_TIMER +#include + #define ACTIVE 1 #define NOT_ACTIVE 0 @@ -940,72 +942,9 @@ static void rtthread_timer_wrapper(void *timerobj) #define TIMER_ID_MAX 50 static struct timer_obj *_g_timerid[TIMER_ID_MAX]; -static int timerid_idx = 0; -RT_DEFINE_SPINLOCK(_timer_id_lock); +static void *timer_id[TIMER_ID_MAX]; +static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id); -void timer_id_init(void) -{ - for (int i = 0; i < TIMER_ID_MAX; i++) - { - _g_timerid[i] = NULL; - } - timerid_idx = 0; -} - -int timer_id_alloc(void) -{ - for (int i = 0; i < timerid_idx; i++) - { - if (_g_timerid[i] == NULL) - return i; - } - if (timerid_idx < TIMER_ID_MAX) - { - timerid_idx++; - return timerid_idx; /* todo */ - } - - return -1; -} - -void timer_id_lock() -{ - rt_hw_spin_lock(&_timer_id_lock); -} - -void timer_id_unlock() -{ - rt_hw_spin_unlock(&_timer_id_lock); -} - -struct timer_obj *timer_id_get(rt_ubase_t timerid) -{ - struct timer_obj *timer; - - if (timerid < 0 || timerid >= TIMER_ID_MAX) - { - return NULL; - } - - timer_id_lock(); - if (_g_timerid[timerid] == NULL) - { - timer_id_unlock(); - LOG_E("can not find timer!"); - return NULL; - } - timer = _g_timerid[timerid]; - timer_id_unlock(); - return timer; -} - -int timer_id_put(int id) -{ - if (_g_timerid[id] == NULL) - return -1; - _g_timerid[id] = NULL; - return 0; -} /** * @brief Create a per-process timer. * @@ -1064,17 +1003,14 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid) rt_timer_init(&timer->timer, timername, rtthread_timer_wrapper, timer, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); } - timer_id_lock(); - _timerid = timer_id_alloc(); + _timerid = resource_id_get(&id_timer); if (_timerid < 0) { - timer_id_unlock(); LOG_E("_timerid overflow!"); return -1; /* todo:memory leak */ } _g_timerid[_timerid] = timer; *timerid = (timer_t)(rt_ubase_t)_timerid; - timer_id_unlock(); return 0; } @@ -1098,17 +1034,14 @@ int timer_delete(timer_t timerid) return -1; } - timer_id_lock(); if (_g_timerid[ktimerid] == NULL) { - timer_id_unlock(); rt_set_errno(EINVAL); LOG_E("can not find timer!"); return -1; } timer = _g_timerid[ktimerid]; - timer_id_put(ktimerid); - timer_id_unlock(); + resource_id_put(&id_timer, ktimerid); if (timer == RT_NULL) { rt_set_errno(EINVAL); @@ -1162,7 +1095,7 @@ int timer_gettime(timer_t timerid, struct itimerspec *its) struct timer_obj *timer; rt_uint32_t seconds, nanoseconds; - timer = timer_id_get((rt_ubase_t)timerid); + timer = _g_timerid[(rt_ubase_t)timerid]; if (timer == NULL) { @@ -1241,7 +1174,8 @@ RTM_EXPORT(timer_gettime); int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue) { - struct timer_obj *timer = timer_id_get((rt_ubase_t)timerid); + struct timer_obj *timer; + timer = _g_timerid[(rt_ubase_t)timerid]; if (timer == NULL || value->it_interval.tv_nsec < 0 || value->it_interval.tv_nsec >= NANOSECOND_PER_SECOND || diff --git a/components/libc/posix/Kconfig b/components/libc/posix/Kconfig index ee2fd81a73..148014407a 100644 --- a/components/libc/posix/Kconfig +++ b/components/libc/posix/Kconfig @@ -57,6 +57,7 @@ config RT_USING_POSIX_CLOCK config RT_USING_POSIX_TIMER select RT_USING_TIMER_SOFT + select RT_USING_RESOURCE_ID bool "Enable timer APIs, timer_create()/timer_gettime() etc" default n diff --git a/components/libc/posix/ipc/Kconfig b/components/libc/posix/ipc/Kconfig index cf3135e53a..deaab1b3e3 100644 --- a/components/libc/posix/ipc/Kconfig +++ b/components/libc/posix/ipc/Kconfig @@ -5,6 +5,7 @@ config RT_USING_POSIX_PIPE select RT_USING_POSIX_FS select RT_USING_POSIX_DEVIO select RT_USING_POSIX_POLL + select RT_USING_RESOURCE_ID default n config RT_USING_POSIX_PIPE_SIZE diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index 4fb29b1d62..93afcc01b0 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -221,6 +221,10 @@ config RT_USING_ADT default y if ARCH_MM_MMU default n +config RT_USING_RESOURCE_ID + bool "Enable resource id" + default n + source "$RTT_DIR/components/utilities/rt-link/Kconfig" endmenu diff --git a/components/utilities/resource/SConscript b/components/utilities/resource/SConscript index 9216d16a23..6d572130a8 100644 --- a/components/utilities/resource/SConscript +++ b/components/utilities/resource/SConscript @@ -3,6 +3,6 @@ from building import * cwd = GetCurrentDir() src = Glob('*.c') CPPPATH = [cwd] -group = DefineGroup('Utilities', src, depend = ['RT_USING_POSIX_PIPE'], CPPPATH = CPPPATH) +group = DefineGroup('Utilities', src, depend = ['RT_USING_RESOURCE_ID'], CPPPATH = CPPPATH) Return('group')