posix: add timespec_is_valid() private internal function
Add a common private function timespec_is_valid() that can be used to check if a timespec object is valid, and use that consistently in lib/posix/options. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
This commit is contained in:
parent
bb9ec32cf6
commit
e75f285bd1
8 changed files with 46 additions and 15 deletions
|
@ -175,6 +175,11 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
|
||||||
|
|
||||||
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
|
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
|
||||||
{
|
{
|
||||||
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
LOG_DBG("%s is invalid", "abstime");
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
return cond_wait(cv, mut, abstime);
|
return cond_wait(cv, mut, abstime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -258,6 +258,11 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
|
||||||
{
|
{
|
||||||
mqueue_desc *mqd = (mqueue_desc *)mqdes;
|
mqueue_desc *mqd = (mqueue_desc *)mqdes;
|
||||||
|
|
||||||
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
|
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,6 +293,11 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
|
||||||
{
|
{
|
||||||
mqueue_desc *mqd = (mqueue_desc *)mqdes;
|
mqueue_desc *mqd = (mqueue_desc *)mqdes;
|
||||||
|
|
||||||
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
|
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,6 +211,11 @@ int pthread_mutex_trylock(pthread_mutex_t *m)
|
||||||
int pthread_mutex_timedlock(pthread_mutex_t *m,
|
int pthread_mutex_timedlock(pthread_mutex_t *m,
|
||||||
const struct timespec *abstime)
|
const struct timespec *abstime)
|
||||||
{
|
{
|
||||||
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
LOG_DBG("%s is invalid", "abstime");
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime)));
|
return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,20 @@
|
||||||
#ifndef ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
|
#ifndef ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
|
||||||
#define ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
|
#define ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <zephyr/sys_clock.h>
|
||||||
|
#include <zephyr/sys/__assert.h>
|
||||||
|
|
||||||
|
static inline bool timespec_is_valid(const struct timespec *ts)
|
||||||
|
{
|
||||||
|
__ASSERT_NO_MSG(ts != NULL);
|
||||||
|
return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC);
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime);
|
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime);
|
||||||
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
|
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
|
||||||
|
|
||||||
|
|
|
@ -1149,11 +1149,8 @@ static int pthread_timedjoin_internal(pthread_t pthread, void **status, k_timeou
|
||||||
*/
|
*/
|
||||||
int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec *abstime)
|
int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec *abstime)
|
||||||
{
|
{
|
||||||
if (abstime == NULL) {
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
return EINVAL;
|
LOG_DBG("%s is invalid", "abstime");
|
||||||
}
|
|
||||||
|
|
||||||
if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= NSEC_PER_SEC) {
|
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -201,7 +201,8 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
|
||||||
uint32_t ret = 0U;
|
uint32_t ret = 0U;
|
||||||
struct posix_rwlock *rwl;
|
struct posix_rwlock *rwl;
|
||||||
|
|
||||||
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
LOG_DBG("%s is invalid", "abstime");
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +272,8 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
|
||||||
uint32_t ret = 0U;
|
uint32_t ret = 0U;
|
||||||
struct posix_rwlock *rwl;
|
struct posix_rwlock *rwl;
|
||||||
|
|
||||||
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
LOG_DBG("%s is invalid", "abstime");
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "posix_clock.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/sys/atomic.h>
|
#include <zephyr/sys/atomic.h>
|
||||||
|
@ -163,9 +165,7 @@ int sem_timedwait(sem_t *semaphore, struct timespec *abstime)
|
||||||
struct timespec current;
|
struct timespec current;
|
||||||
int64_t current_ms, abstime_ms;
|
int64_t current_ms, abstime_ms;
|
||||||
|
|
||||||
__ASSERT(abstime, "abstime pointer NULL");
|
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
|
||||||
|
|
||||||
if ((abstime->tv_sec < 0) || (abstime->tv_nsec >= NSEC_PER_SEC)) {
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,12 @@
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#undef _POSIX_C_SOURCE
|
#undef _POSIX_C_SOURCE
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
|
#include "posix_clock.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
|
@ -241,11 +245,8 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
|
||||||
struct timer_obj *timer = (struct timer_obj *) timerid;
|
struct timer_obj *timer = (struct timer_obj *) timerid;
|
||||||
uint32_t duration, current;
|
uint32_t duration, current;
|
||||||
|
|
||||||
if (timer == NULL ||
|
if ((timer == NULL) || !timespec_is_valid(&value->it_interval) ||
|
||||||
value->it_interval.tv_nsec < 0 ||
|
!timespec_is_valid(&value->it_value)) {
|
||||||
value->it_interval.tv_nsec >= NSEC_PER_SEC ||
|
|
||||||
value->it_value.tv_nsec < 0 ||
|
|
||||||
value->it_value.tv_nsec >= NSEC_PER_SEC) {
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue