Skip to content

Commit 3ec7280

Browse files
committed
clock: refactor clock_systime_timespec() & clock_systime_ticks()
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 0ae82b7 commit 3ec7280

File tree

2 files changed

+58
-138
lines changed

2 files changed

+58
-138
lines changed

sched/clock/clock_systime_ticks.c

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

151-
return sample;
84+
clock_systime_timespec(&ts);
85+
return clock_time2ticks(&ts);
86+
#elif defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
87+
clock_t ticks = 0;
15288

153-
#else /* CONFIG_SYSTEM_TIME64 */
154-
155-
/* Return the current system time */
156-
157-
return g_system_ticks;
89+
up_timer_gettick(&ticks);
90+
return ticks;
91+
#elif defined(CONFIG_SCHED_TICKLESS)
92+
struct timespec ts =
93+
{
94+
0
95+
};
15896

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

sched/clock/clock_systime_timespec.c

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,76 +59,31 @@
5959
int clock_systime_timespec(FAR struct timespec *ts)
6060
{
6161
#ifdef CONFIG_RTC_HIRES
62-
/* Do we have a high-resolution RTC that can provide us with the time? */
63-
6462
if (g_rtc_enabled)
6563
{
66-
int ret;
67-
68-
/* Get the hi-resolution time from the RTC. This will return the
69-
* current time, not the time since power up.
70-
*/
71-
72-
ret = up_rtc_gettime(ts);
73-
if (ret < 0)
74-
{
75-
return ret;
76-
}
77-
78-
/* Subtract the base time to this in order to convert this to the
79-
* time since power up.
80-
*/
81-
82-
DEBUGASSERT(ts->tv_sec >= g_basetime.tv_sec);
83-
if (ts->tv_sec < g_basetime.tv_sec)
84-
{
85-
/* Negative times are not supported */
86-
87-
return -ENOSYS;
88-
}
89-
90-
ts->tv_sec -= g_basetime.tv_sec;
91-
if (ts->tv_nsec < g_basetime.tv_nsec)
92-
{
93-
/* Borrow */
94-
95-
if (ts->tv_sec < 1)
96-
{
97-
/* Negative times are not supported */
64+
irqstate_t flags;
9865

99-
return -ENOSYS;
100-
}
66+
up_rtc_gettime(ts);
10167

102-
ts->tv_sec--;
103-
ts->tv_nsec += NSEC_PER_SEC;
104-
}
105-
106-
ts->tv_nsec -= g_basetime.tv_nsec;
107-
return OK;
68+
flags = spin_lock_irqsave(NULL);
69+
clock_timespec_subtract(ts, &g_basetime, ts);
70+
spin_unlock_irqrestore(NULL, flags);
10871
}
10972
else
110-
#endif
11173
{
112-
/* In tickless mode, all timing is controlled by platform-specific
113-
* code. Let the platform timer do the work.
114-
*/
115-
116-
#if defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
117-
clock_t ticks;
118-
int ret;
74+
ts->tv_sec = 0;
75+
ts->tv_nsec = 0;
76+
}
77+
#elif defined(CONFIG_SCHED_TICKLESS_TICK_ARGUMENT)
78+
clock_t ticks = 0;
11979

120-
ret = up_timer_gettick(&ticks);
121-
clock_ticks2time(ts, ticks);
122-
return ret;
80+
up_timer_gettick(&ticks);
81+
clock_ticks2time(ts, ticks);
12382
#elif defined(CONFIG_SCHED_TICKLESS)
124-
return up_timer_gettime(ts);
83+
up_timer_gettime(ts);
12584
#else
126-
/* 64-bit microsecond calculations should improve our accuracy
127-
* when the clock period is in units of microseconds.
128-
*/
129-
130-
clock_ticks2time(ts, clock_systime_ticks());
131-
return OK;
85+
clock_ticks2time(ts, g_system_ticks);
13286
#endif
133-
}
87+
return 0;
13488
}
89+

0 commit comments

Comments
 (0)