summaryrefslogtreecommitdiff
path: root/components/libc
diff options
context:
space:
mode:
authorzhkag <[email protected]>2023-06-29 00:01:51 +0800
committerGitHub <[email protected]>2023-06-29 00:01:51 +0800
commit077e984175e546a495fee1ace6f2f54ff161fc77 (patch)
tree8da9ec027d496a7ff2adcccb9eb199fa91d499ac /components/libc
parent0b79bea7cde71dc8db1d8ba33acd2705de64ee76 (diff)
[libc]更新 libc timer id 分配方式 (#7744)
Diffstat (limited to 'components/libc')
-rw-r--r--components/libc/compilers/common/ctime.c84
-rw-r--r--components/libc/posix/Kconfig1
-rw-r--r--components/libc/posix/ipc/Kconfig1
3 files changed, 11 insertions, 75 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 <resource_id.h>
+
#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);
-
-void timer_id_init(void)
-{
- for (int i = 0; i < TIMER_ID_MAX; i++)
- {
- _g_timerid[i] = NULL;
- }
- timerid_idx = 0;
-}
+static void *timer_id[TIMER_ID_MAX];
+static resource_id_t id_timer = RESOURCE_ID_INIT(TIMER_ID_MAX, timer_id);
-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