Skip to content

Commit d50b1fe

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
kernel: POSIX: Compatibility layer for POSIX clock APIs.
This patch provides POSIX clock APIs for POSIX 1003.1 PSE52 standard. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent 1f2e126 commit d50b1fe

File tree

6 files changed

+96
-17
lines changed

6 files changed

+96
-17
lines changed

arch/posix/include/posix_cheats.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#define pthread_barrier_t zap_pthread_barrier_t
3232
#define pthread_barrierattr_t zap_pthread_barrierattr_t
3333
#define pthread_attr_t zap_pthread_attr_t
34+
#define clockid_t zap_clockid_t
35+
#define sched_param zap_sched_param
3436

3537
/* Condition variables */
3638
#define pthread_cond_init(...) zap_pthread_cond_init(__VA_ARGS__)
@@ -99,6 +101,10 @@
99101
#define sleep(...) zap_sleep(__VA_ARGS__)
100102
#define usleep(...) zap_usleep(__VA_ARGS__)
101103

104+
/* Clock */
105+
#define clock_gettime(...) zap_clock_gettime(__VA_ARGS__)
106+
#define clock_settime(...) zap_clock_settime(__VA_ARGS__)
107+
102108
#endif /* CONFIG_ARCH_POSIX */
103109

104110
#endif

include/posix/pthread.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88
#define __PTHREAD_H__
99

1010
#include <kernel.h>
11-
#ifdef CONFIG_NEWLIB_LIBC
1211
#include <time.h>
13-
#else
14-
/* This should probably live somewhere else but Zephyr doesn't
15-
* currently have a stdc layer to provide it
16-
*/
17-
struct timespec {
18-
s32_t tv_sec;
19-
s32_t tv_nsec;
20-
};
21-
#endif /* CONFIG_NEWLIB_LIBC */
22-
2312
#include "sys/types.h"
2413
#include "posix_sched.h"
2514
#include "unistd.h"
@@ -63,11 +52,6 @@ struct posix_thread {
6352
#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
6453
#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
6554

66-
static inline s32_t _ts_to_ms(const struct timespec *to)
67-
{
68-
return (to->tv_sec * 1000) + (to->tv_nsec / 1000000);
69-
}
70-
7155
/**
7256
* @brief Declare a pthread condition variable
7357
*

include/posix/sys/types.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#ifndef __POSIX_TYPES_H__
88
#define __POSIX_TYPES_H__
99

10-
1110
#ifdef __cplusplus
1211
extern "C" {
1312
#endif
1413

1514
#include_next <sys/types.h>
1615

1716
#ifdef CONFIG_PTHREAD_IPC
17+
#include <kernel.h>
1818

1919
/* Thread attributes */
2020
typedef struct pthread_attr_t {
@@ -60,6 +60,9 @@ typedef struct pthread_barrierattr {
6060
} pthread_barrierattr_t;
6161

6262
/* time related attributes */
63+
#ifndef CONFIG_NEWLIB_LIBC
64+
typedef u32_t clockid_t;
65+
#endif /*CONFIG_NEWLIB_LIBC */
6366
typedef unsigned long useconds_t;
6467

6568
#endif /* CONFIG_PTHREAD_IPC */

include/posix/time.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef __POSIX_TIME_H__
7+
#define __POSIX_TIME_H__
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#ifdef CONFIG_NEWLIB_LIBC
14+
#include_next <time.h>
15+
#else
16+
struct timespec {
17+
s32_t tv_sec;
18+
s32_t tv_nsec;
19+
};
20+
#endif /* CONFIG_NEWLIB_LIBC */
21+
#include <kernel.h>
22+
#include <errno.h>
23+
#include "sys/types.h"
24+
25+
#define CLOCK_MONOTONIC 1
26+
27+
static inline s32_t _ts_to_ms(const struct timespec *to)
28+
{
29+
return (to->tv_sec * 1000) + (to->tv_nsec / 1000000);
30+
}
31+
32+
/**
33+
* @brief Set clock time.
34+
*
35+
* See IEEE 1003.1
36+
*/
37+
static inline int clock_settime(clockid_t clock_id, const struct timespec *ts)
38+
{
39+
errno = ENOSYS;
40+
return -1;
41+
}
42+
43+
int clock_gettime(clockid_t clock_id, struct timespec *ts);
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
49+
#endif /* __POSIX_TIME_H__ */

kernel/posix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ target_sources(kernel PRIVATE posix/pthread_mutex.c)
44
target_sources(kernel PRIVATE posix/pthread_barrier.c)
55
target_sources(kernel PRIVATE posix/pthread.c)
66
target_sources(kernel PRIVATE posix/pthread_sched.c)
7+
target_sources(kernel PRIVATE posix/clock.c)

kernel/posix/clock.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <kernel.h>
7+
#include <time.h>
8+
#include <errno.h>
9+
10+
/**
11+
* @brief Get clock time specified by clock_id.
12+
*
13+
* See IEEE 1003.1
14+
*/
15+
int clock_gettime(clockid_t clock_id, struct timespec *ts)
16+
{
17+
s64_t elapsed_msecs, elapsed_secs;
18+
s64_t elapsed_nsec, elapsed_cycles;
19+
20+
if (clock_id != CLOCK_MONOTONIC) {
21+
errno = EINVAL;
22+
return -1;
23+
}
24+
25+
elapsed_msecs = k_uptime_get();
26+
elapsed_secs = elapsed_msecs / MSEC_PER_SEC;
27+
28+
elapsed_cycles = (s64_t) (k_cycle_get_32() %
29+
sys_clock_hw_cycles_per_sec);
30+
elapsed_nsec = (s64_t) ((elapsed_cycles * NSEC_PER_SEC) /
31+
sys_clock_hw_cycles_per_sec);
32+
33+
ts->tv_sec = (s32_t) elapsed_secs;
34+
ts->tv_nsec = (s32_t) elapsed_nsec;
35+
return 0;
36+
}

0 commit comments

Comments
 (0)