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 <youvedeep.singh@intel.com>
This commit is contained in:
Youvedeep Singh 2018-01-17 13:09:42 +05:30 committed by Anas Nashif
commit d50b1fe981
6 changed files with 96 additions and 17 deletions

View file

@ -31,6 +31,8 @@
#define pthread_barrier_t zap_pthread_barrier_t #define pthread_barrier_t zap_pthread_barrier_t
#define pthread_barrierattr_t zap_pthread_barrierattr_t #define pthread_barrierattr_t zap_pthread_barrierattr_t
#define pthread_attr_t zap_pthread_attr_t #define pthread_attr_t zap_pthread_attr_t
#define clockid_t zap_clockid_t
#define sched_param zap_sched_param
/* Condition variables */ /* Condition variables */
#define pthread_cond_init(...) zap_pthread_cond_init(__VA_ARGS__) #define pthread_cond_init(...) zap_pthread_cond_init(__VA_ARGS__)
@ -99,6 +101,10 @@
#define sleep(...) zap_sleep(__VA_ARGS__) #define sleep(...) zap_sleep(__VA_ARGS__)
#define usleep(...) zap_usleep(__VA_ARGS__) #define usleep(...) zap_usleep(__VA_ARGS__)
/* Clock */
#define clock_gettime(...) zap_clock_gettime(__VA_ARGS__)
#define clock_settime(...) zap_clock_settime(__VA_ARGS__)
#endif /* CONFIG_ARCH_POSIX */ #endif /* CONFIG_ARCH_POSIX */
#endif #endif

View file

@ -8,18 +8,7 @@
#define __PTHREAD_H__ #define __PTHREAD_H__
#include <kernel.h> #include <kernel.h>
#ifdef CONFIG_NEWLIB_LIBC
#include <time.h> #include <time.h>
#else
/* This should probably live somewhere else but Zephyr doesn't
* currently have a stdc layer to provide it
*/
struct timespec {
s32_t tv_sec;
s32_t tv_nsec;
};
#endif /* CONFIG_NEWLIB_LIBC */
#include "sys/types.h" #include "sys/types.h"
#include "posix_sched.h" #include "posix_sched.h"
#include "unistd.h" #include "unistd.h"
@ -63,11 +52,6 @@ struct posix_thread {
#define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS) #define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS)
#define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS) #define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS)
static inline s32_t _ts_to_ms(const struct timespec *to)
{
return (to->tv_sec * 1000) + (to->tv_nsec / 1000000);
}
/** /**
* @brief Declare a pthread condition variable * @brief Declare a pthread condition variable
* *

View file

@ -7,7 +7,6 @@
#ifndef __POSIX_TYPES_H__ #ifndef __POSIX_TYPES_H__
#define __POSIX_TYPES_H__ #define __POSIX_TYPES_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -15,6 +14,7 @@ extern "C" {
#include_next <sys/types.h> #include_next <sys/types.h>
#ifdef CONFIG_PTHREAD_IPC #ifdef CONFIG_PTHREAD_IPC
#include <kernel.h>
/* Thread attributes */ /* Thread attributes */
typedef struct pthread_attr_t { typedef struct pthread_attr_t {
@ -60,6 +60,9 @@ typedef struct pthread_barrierattr {
} pthread_barrierattr_t; } pthread_barrierattr_t;
/* time related attributes */ /* time related attributes */
#ifndef CONFIG_NEWLIB_LIBC
typedef u32_t clockid_t;
#endif /*CONFIG_NEWLIB_LIBC */
typedef unsigned long useconds_t; typedef unsigned long useconds_t;
#endif /* CONFIG_PTHREAD_IPC */ #endif /* CONFIG_PTHREAD_IPC */

49
include/posix/time.h Normal file
View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __POSIX_TIME_H__
#define __POSIX_TIME_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef CONFIG_NEWLIB_LIBC
#include_next <time.h>
#else
struct timespec {
s32_t tv_sec;
s32_t tv_nsec;
};
#endif /* CONFIG_NEWLIB_LIBC */
#include <kernel.h>
#include <errno.h>
#include "sys/types.h"
#define CLOCK_MONOTONIC 1
static inline s32_t _ts_to_ms(const struct timespec *to)
{
return (to->tv_sec * 1000) + (to->tv_nsec / 1000000);
}
/**
* @brief Set clock time.
*
* See IEEE 1003.1
*/
static inline int clock_settime(clockid_t clock_id, const struct timespec *ts)
{
errno = ENOSYS;
return -1;
}
int clock_gettime(clockid_t clock_id, struct timespec *ts);
#ifdef __cplusplus
}
#endif
#endif /* __POSIX_TIME_H__ */

View file

@ -4,3 +4,4 @@ target_sources(kernel PRIVATE posix/pthread_mutex.c)
target_sources(kernel PRIVATE posix/pthread_barrier.c) target_sources(kernel PRIVATE posix/pthread_barrier.c)
target_sources(kernel PRIVATE posix/pthread.c) target_sources(kernel PRIVATE posix/pthread.c)
target_sources(kernel PRIVATE posix/pthread_sched.c) target_sources(kernel PRIVATE posix/pthread_sched.c)
target_sources(kernel PRIVATE posix/clock.c)

36
kernel/posix/clock.c Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <time.h>
#include <errno.h>
/**
* @brief Get clock time specified by clock_id.
*
* See IEEE 1003.1
*/
int clock_gettime(clockid_t clock_id, struct timespec *ts)
{
s64_t elapsed_msecs, elapsed_secs;
s64_t elapsed_nsec, elapsed_cycles;
if (clock_id != CLOCK_MONOTONIC) {
errno = EINVAL;
return -1;
}
elapsed_msecs = k_uptime_get();
elapsed_secs = elapsed_msecs / MSEC_PER_SEC;
elapsed_cycles = (s64_t) (k_cycle_get_32() %
sys_clock_hw_cycles_per_sec);
elapsed_nsec = (s64_t) ((elapsed_cycles * NSEC_PER_SEC) /
sys_clock_hw_cycles_per_sec);
ts->tv_sec = (s32_t) elapsed_secs;
ts->tv_nsec = (s32_t) elapsed_nsec;
return 0;
}