Skip to content

Commit fae6a00

Browse files
GUIDINGLIxiaoxiang781216
authored andcommitted
clock: refactor clock_systime_timespec() & clock_systime_ticks()
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 76e8075 commit fae6a00

File tree

2 files changed

+59
-138
lines changed

2 files changed

+59
-138
lines changed

sched/clock/clock_systime_ticks.c

Lines changed: 42 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -78,86 +78,51 @@
7878
clock_t clock_systime_ticks(void)
7979
{
8080
#ifdef CONFIG_RTC_HIRES
81-
/* Do we have a high-resolution RTC that can provide us with the time? */
82-
83-
if (g_rtc_enabled)
81+
struct timespec ts =
8482
{
85-
struct timespec ts;
86-
87-
/* Get the time from the platform specific hardware */
88-
89-
if (clock_systime_timespec(&ts) == OK)
90-
{
91-
/* Convert to a 64-bit value in microseconds,
92-
* then in clock tick units.
93-
*/
94-
95-
return clock_time2ticks(&ts);
96-
}
97-
else
98-
{
99-
return 0;
100-
}
101-
}
102-
else
103-
#endif
104-
{
105-
/* In tickless mode, all timing is controlled by platform-specific
106-
* code. Let the platform timer do the work.
107-
*/
108-
109-
#if defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
110-
clock_t ticks;
111-
if (up_timer_gettick(&ticks) == OK)
112-
{
113-
return ticks;
114-
}
115-
else
116-
{
117-
return 0;
118-
}
119-
#elif defined(CONFIG_SCHED_TICKLESS)
120-
struct timespec ts;
121-
if (up_timer_gettime(&ts) == OK)
122-
{
123-
return clock_time2ticks(&ts);
124-
}
125-
else
126-
{
127-
return 0;
128-
}
129-
#elif defined(CONFIG_SYSTEM_TIME64)
130-
131-
clock_t sample;
132-
clock_t verify;
133-
134-
/* 64-bit accesses are not atomic on most architectures. The following
135-
* loop samples the 64-bit timer twice and loops in the rare event that
136-
* there was 32-bit rollover between samples.
137-
*
138-
* If there is no 32-bit rollover, then:
139-
*
140-
* - The MS 32-bits of each sample will be the same, and
141-
* - The LS 32-bits of the second sample will be greater than or equal
142-
* to the LS 32-bits for the first sample.
143-
*/
144-
145-
do
146-
{
147-
verify = g_system_ticks;
148-
sample = g_system_ticks;
149-
}
150-
while ((sample & TIMER_MASK32) < (verify & TIMER_MASK32) ||
151-
(sample & ~TIMER_MASK32) != (verify & ~TIMER_MASK32));
83+
0
84+
};
15285

153-
return sample;
86+
clock_systime_timespec(&ts);
87+
return clock_time2ticks(&ts);
88+
#elif defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
89+
clock_t ticks = 0;
15490

155-
#else /* CONFIG_SYSTEM_TIME64 */
156-
157-
/* Return the current system time */
158-
159-
return g_system_ticks;
91+
up_timer_gettick(&ticks);
92+
return ticks;
93+
#elif defined(CONFIG_SCHED_TICKLESS)
94+
struct timespec ts =
95+
{
96+
0
97+
};
16098

161-
#endif /* CONFIG_SYSTEM_TIME64 */
99+
up_timer_gettime(&ts);
100+
return clock_time2ticks(&ts);
101+
#elif defined(CONFIG_SYSTEM_TIME64)
102+
clock_t sample;
103+
clock_t verify;
104+
105+
/* 64-bit accesses are not atomic on most architectures. The following
106+
* loop samples the 64-bit timer twice and loops in the rare event that
107+
* there was 32-bit rollover between samples.
108+
*
109+
* If there is no 32-bit rollover, then:
110+
*
111+
* - The MS 32-bits of each sample will be the same, and
112+
* - The LS 32-bits of the second sample will be greater than or equal
113+
* to the LS 32-bits for the first sample.
114+
*/
115+
116+
do
117+
{
118+
verify = g_system_ticks;
119+
sample = g_system_ticks;
162120
}
121+
while ((sample & TIMER_MASK32) < (verify & TIMER_MASK32) ||
122+
(sample & ~TIMER_MASK32) != (verify & ~TIMER_MASK32));
123+
124+
return sample;
125+
#else
126+
return g_system_ticks;
127+
#endif
163128
}

sched/clock/clock_systime_timespec.c

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <nuttx/arch.h>
3535
#include <nuttx/clock.h>
36+
#include <nuttx/spinlock.h>
3637

3738
#include "clock/clock.h"
3839

@@ -61,76 +62,31 @@
6162
int clock_systime_timespec(FAR struct timespec *ts)
6263
{
6364
#ifdef CONFIG_RTC_HIRES
64-
/* Do we have a high-resolution RTC that can provide us with the time? */
65-
6665
if (g_rtc_enabled)
6766
{
68-
int ret;
69-
70-
/* Get the hi-resolution time from the RTC. This will return the
71-
* current time, not the time since power up.
72-
*/
73-
74-
ret = up_rtc_gettime(ts);
75-
if (ret < 0)
76-
{
77-
return ret;
78-
}
79-
80-
/* Subtract the base time to this in order to convert this to the
81-
* time since power up.
82-
*/
83-
84-
DEBUGASSERT(ts->tv_sec >= g_basetime.tv_sec);
85-
if (ts->tv_sec < g_basetime.tv_sec)
86-
{
87-
/* Negative times are not supported */
88-
89-
return -ENOSYS;
90-
}
91-
92-
ts->tv_sec -= g_basetime.tv_sec;
93-
if (ts->tv_nsec < g_basetime.tv_nsec)
94-
{
95-
/* Borrow */
96-
97-
if (ts->tv_sec < 1)
98-
{
99-
/* Negative times are not supported */
67+
irqstate_t flags;
10068

101-
return -ENOSYS;
102-
}
69+
up_rtc_gettime(ts);
10370

104-
ts->tv_sec--;
105-
ts->tv_nsec += NSEC_PER_SEC;
106-
}
107-
108-
ts->tv_nsec -= g_basetime.tv_nsec;
109-
return OK;
71+
flags = spin_lock_irqsave(NULL);
72+
clock_timespec_subtract(ts, &g_basetime, ts);
73+
spin_unlock_irqrestore(NULL, flags);
11074
}
11175
else
112-
#endif
11376
{
114-
/* In tickless mode, all timing is controlled by platform-specific
115-
* code. Let the platform timer do the work.
116-
*/
117-
118-
#if defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
119-
clock_t ticks;
120-
int ret;
77+
ts->tv_sec = 0;
78+
ts->tv_nsec = 0;
79+
}
80+
#elif defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
81+
clock_t ticks = 0;
12182

122-
ret = up_timer_gettick(&ticks);
123-
clock_ticks2time(ts, ticks);
124-
return ret;
83+
up_timer_gettick(&ticks);
84+
clock_ticks2time(ts, ticks);
12585
#elif defined(CONFIG_SCHED_TICKLESS)
126-
return up_timer_gettime(ts);
86+
up_timer_gettime(ts);
12787
#else
128-
/* 64-bit microsecond calculations should improve our accuracy
129-
* when the clock period is in units of microseconds.
130-
*/
131-
132-
clock_ticks2time(ts, clock_systime_ticks());
133-
return OK;
88+
clock_ticks2time(ts, g_system_ticks);
13489
#endif
135-
}
90+
return 0;
13691
}
92+

0 commit comments

Comments
 (0)