Skip to content

Commit d3da3bd

Browse files
committed
[libc] move clock_time to time.
1 parent 1383a97 commit d3da3bd

File tree

8 files changed

+306
-240
lines changed

8 files changed

+306
-240
lines changed

components/libc/compilers/newlib/syscalls.c

Lines changed: 183 additions & 189 deletions
Large diffs are not rendered by default.

components/libc/pthreads/pthread.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
int pthread_system_init(void)
2929
{
30-
/* initialize clock and time */
31-
clock_time_system_init();
32-
3330
/* initialize key area */
3431
pthread_key_system_init();
3532
/* initialize posix mqueue */

components/libc/pthreads/pthread.h

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -276,35 +276,4 @@ int pthread_barrier_init(pthread_barrier_t *barrier,
276276

277277
int pthread_barrier_wait(pthread_barrier_t *barrier);
278278

279-
/* posix clock and timer */
280-
#define MILLISECOND_PER_SECOND 1000UL
281-
#define MICROSECOND_PER_SECOND 1000000UL
282-
#define NANOSECOND_PER_SECOND 1000000000UL
283-
284-
#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
285-
#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
286-
#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
287-
288-
#ifndef CLOCK_REALTIME
289-
#define CLOCK_REALTIME 1
290-
#endif
291-
292-
#define CLOCK_CPUTIME_ID 2
293-
294-
#ifndef CLOCK_PROCESS_CPUTIME_ID
295-
#define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID
296-
#endif
297-
#ifndef CLOCK_THREAD_CPUTIME_ID
298-
#define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID
299279
#endif
300-
301-
#ifndef CLOCK_MONOTONIC
302-
#define CLOCK_MONOTONIC 4
303-
#endif
304-
305-
int clock_getres (clockid_t clockid, struct timespec *res);
306-
int clock_gettime (clockid_t clockid, struct timespec *tp);
307-
int clock_settime (clockid_t clockid, const struct timespec *tp);
308-
309-
#endif
310-

components/libc/pthreads/pthread_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ rt_inline _pthread_data_t *_pthread_get_data(pthread_t thread)
8383
}
8484

8585
int clock_time_to_tick(const struct timespec *time);
86-
void clock_time_system_init(void);
86+
8787
void posix_mq_system_init(void);
8888
void posix_sem_system_init(void);
8989
void pthread_key_system_init(void);

components/libc/timer/SConscript renamed to components/libc/time/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
66
src = Glob('*.c') + Glob('*.cpp')
77
CPPPATH = [cwd]
88

9-
group = DefineGroup('libc', src,
10-
depend = ['RT_USING_LIBC', 'RT_USING_POSIX_TIMER'], CPPPATH = CPPPATH)
9+
group = DefineGroup('pthreads', src,
10+
depend = ['RT_USING_PTHREADS'], CPPPATH = CPPPATH)
1111

1212
Return('group')

components/libc/pthreads/clock_time.c renamed to components/libc/time/clock_time.c

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
*/
2525

2626
#include <rtthread.h>
27+
#include <rtdevice.h>
2728
#include <pthread.h>
2829

30+
#include "clock_time.h"
31+
2932
struct timeval _timevalue;
30-
void clock_time_system_init()
33+
int clock_time_system_init()
3134
{
3235
time_t time;
3336
rt_tick_t tick;
@@ -46,7 +49,10 @@ void clock_time_system_init()
4649

4750
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
4851
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
52+
53+
return 0;
4954
}
55+
INIT_COMPONENT_EXPORT(clock_time_system_init);
5056

5157
int clock_time_to_tick(const struct timespec *time)
5258
{
@@ -83,38 +89,80 @@ RTM_EXPORT(clock_time_to_tick);
8389

8490
int clock_getres(clockid_t clockid, struct timespec *res)
8591
{
86-
if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
92+
int ret = 0;
93+
94+
if (res == RT_NULL)
8795
{
8896
rt_set_errno(EINVAL);
89-
9097
return -1;
9198
}
9299

93-
res->tv_sec = 0;
94-
res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
100+
switch (clockid)
101+
{
102+
case CLOCK_REALTIME:
103+
res->tv_sec = 0;
104+
res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
105+
break;
106+
107+
#ifdef RT_USING_CPUTIME
108+
case CLOCK_CPUTIME_ID:
109+
res->tv_sec = 0;
110+
res->tv_nsec = clock_cpu_getres();
111+
break;
112+
#endif
113+
114+
default:
115+
ret = -1;
116+
rt_set_errno(EINVAL);
117+
break;
118+
}
95119

96-
return 0;
120+
return ret;
97121
}
98122
RTM_EXPORT(clock_getres);
99123

100124
int clock_gettime(clockid_t clockid, struct timespec *tp)
101125
{
102-
rt_tick_t tick;
126+
int ret = 0;
103127

104-
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
128+
if (tp == RT_NULL)
105129
{
106130
rt_set_errno(EINVAL);
107-
108131
return -1;
109132
}
110133

111-
/* get tick */
112-
tick = rt_tick_get();
134+
switch (clockid)
135+
{
136+
case CLOCK_REALTIME:
137+
{
138+
/* get tick */
139+
int tick = rt_tick_get();
113140

114-
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
115-
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
141+
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
142+
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
143+
}
144+
break;
116145

117-
return 0;
146+
#ifdef RT_USING_CPUTIME
147+
case CLOCK_CPUTIME_ID:
148+
{
149+
float unit = 0;
150+
long long cpu_tick;
151+
152+
unit = clock_cpu_getres();
153+
cpu_tick = clock_cpu_gettime();
154+
155+
tp->tv_sec = ((int)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
156+
tp->tv_nsec = ((int)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
157+
}
158+
break;
159+
#endif
160+
default:
161+
rt_set_errno(EINVAL);
162+
ret = -1;
163+
}
164+
165+
return ret;
118166
}
119167
RTM_EXPORT(clock_gettime);
120168

components/libc/time/clock_time.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* File : clock_time.h
3+
* This file is part of RT-Thread RTOS
4+
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Change Logs:
21+
* Date Author Notes
22+
* 2017-12-31 Bernard the first version
23+
*/
24+
25+
#ifndef CLOCK_TIME_H__
26+
#define CLOCK_TIME_H__
27+
28+
/* posix clock and timer */
29+
#define MILLISECOND_PER_SECOND 1000UL
30+
#define MICROSECOND_PER_SECOND 1000000UL
31+
#define NANOSECOND_PER_SECOND 1000000000UL
32+
33+
#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
34+
#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
35+
#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
36+
37+
#ifndef CLOCK_REALTIME
38+
#define CLOCK_REALTIME 1
39+
#endif
40+
41+
#define CLOCK_CPUTIME_ID 2
42+
43+
#ifndef CLOCK_PROCESS_CPUTIME_ID
44+
#define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID
45+
#endif
46+
#ifndef CLOCK_THREAD_CPUTIME_ID
47+
#define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID
48+
#endif
49+
50+
#ifndef CLOCK_MONOTONIC
51+
#define CLOCK_MONOTONIC 4
52+
#endif
53+
54+
int clock_getres (clockid_t clockid, struct timespec *res);
55+
int clock_gettime (clockid_t clockid, struct timespec *tp);
56+
int clock_settime (clockid_t clockid, const struct timespec *tp);
57+
58+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)