From 7ce2b3fa74ebe910ba179fcffcef03bd657a3158 Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Wed, 12 Aug 2020 17:45:35 +0800 Subject: [PATCH] libc/pthread: Implement pthread_condattr_[g|s]etclock Reference: https: //pubs.opengroup.org/onlinepubs/009695399/functions/pthread_condattr_setclock.html Change-Id: I19c15d5f219fcf28dbfeb2e5a1e3fc7b38ba2259 Signed-off-by: chao.an --- include/pthread.h | 16 ++++- libs/libc/pthread/Make.defs | 1 + libs/libc/pthread/pthread_condattr_getclock.c | 62 +++++++++++++++++ libs/libc/pthread/pthread_condattr_init.c | 2 +- libs/libc/pthread/pthread_condattr_setclock.c | 66 +++++++++++++++++++ libs/libc/pthread/pthread_condinit.c | 5 +- libs/libc/pthread/pthread_condtimedwait.c | 2 +- 7 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 libs/libc/pthread/pthread_condattr_getclock.c create mode 100644 libs/libc/pthread/pthread_condattr_setclock.c diff --git a/include/pthread.h b/include/pthread.h index ccc6ade1d64..4495c77a210 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -270,14 +270,20 @@ typedef pid_t pthread_t; #define __PTHREAD_T_DEFINED 1 #endif +struct pthread_condattr_s +{ + clockid_t clockid; +}; + #ifndef __PTHREAD_CONDATTR_T_DEFINED -typedef int pthread_condattr_t; +typedef struct pthread_condattr_s pthread_condattr_t; #define __PTHREAD_CONDATTR_T_DEFINED 1 #endif struct pthread_cond_s { sem_t sem; + clockid_t clockid; }; #ifndef __PTHREAD_COND_T_DEFINED @@ -285,7 +291,7 @@ typedef struct pthread_cond_s pthread_cond_t; #define __PTHREAD_COND_T_DEFINED 1 #endif -#define PTHREAD_COND_INITIALIZER {SEM_INITIALIZER(0)} +#define PTHREAD_COND_INITIALIZER {SEM_INITIALIZER(0), CLOCK_REALTIME } struct pthread_mutexattr_s { @@ -607,6 +613,10 @@ int pthread_mutex_consistent(FAR pthread_mutex_t *mutex); int pthread_condattr_init(FAR pthread_condattr_t *attr); int pthread_condattr_destroy(FAR pthread_condattr_t *attr); +int pthread_condattr_getclock(FAR const pthread_condattr_t *attr, + clockid_t *clock_id); +int pthread_condattr_setclock(FAR pthread_condattr_t *attr, + clockid_t clock_id); /* A thread can create and delete condition variables. */ @@ -739,7 +749,7 @@ typedef pid_t pthread_t; #endif #ifndef __PTHREAD_CONDATTR_T_DEFINED -typedef int pthread_condattr_t; +typedef struct pthread_condattr_s pthread_condattr_t; # define __PTHREAD_CONDATTR_T_DEFINED 1 #endif diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs index 63a5b2d25ef..53afa0a008a 100644 --- a/libs/libc/pthread/Make.defs +++ b/libs/libc/pthread/Make.defs @@ -49,6 +49,7 @@ CSRCS += pthread_testcancel.c CSRCS += pthread_rwlock.c pthread_rwlock_rdlock.c pthread_rwlock_wrlock.c CSRCS += pthread_once.c pthread_yield.c CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c +CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c ifeq ($(CONFIG_SMP),y) CSRCS += pthread_attr_getaffinity.c pthread_attr_setaffinity.c diff --git a/libs/libc/pthread/pthread_condattr_getclock.c b/libs/libc/pthread/pthread_condattr_getclock.c new file mode 100644 index 00000000000..e8d37edd80a --- /dev/null +++ b/libs/libc/pthread/pthread_condattr_getclock.c @@ -0,0 +1,62 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_condattr_getclock.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_condattr_getclock + * + * Description: + * set the clock selection condition variable attribute + * + * Input Parameters: + * None + * + * Returned Value: + * If successful, the pthread_condattr_getclock() function shall return + * zero and store the value of the clock attribute of attr into the object + * referenced by the clock_id argument. Otherwise, an error number shall + * be returned to indicate the error. + * + ****************************************************************************/ + +int pthread_condattr_getclock(FAR const pthread_condattr_t *attr, + FAR clockid_t *clock_id) +{ + if (!attr) + { + return EINVAL; + } + + *clock_id = attr->clockid; + + return OK; +} diff --git a/libs/libc/pthread/pthread_condattr_init.c b/libs/libc/pthread/pthread_condattr_init.c index 95e68518826..69c5e02698b 100644 --- a/libs/libc/pthread/pthread_condattr_init.c +++ b/libs/libc/pthread/pthread_condattr_init.c @@ -75,7 +75,7 @@ int pthread_condattr_init(FAR pthread_condattr_t *attr) } else { - *attr = 0; + attr->clockid = CLOCK_REALTIME; } linfo("Returning %d\n", ret); diff --git a/libs/libc/pthread/pthread_condattr_setclock.c b/libs/libc/pthread/pthread_condattr_setclock.c new file mode 100644 index 00000000000..f3a0c2f6f57 --- /dev/null +++ b/libs/libc/pthread/pthread_condattr_setclock.c @@ -0,0 +1,66 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_condattr_setclock.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_condattr_setclock + * + * Description: + * set the clock selection condition variable attribute + * + * Input Parameters: + * None + * + * Returned Value: + * If successful, the pthread_condattr_setclock() function shall + * return zero; otherwise, an error number shall be returned to + * indicate the error. + * + ****************************************************************************/ + +int pthread_condattr_setclock(FAR pthread_condattr_t *attr, + clockid_t clock_id) +{ + if (!attr || + ( +#ifdef CONFIG_CLOCK_MONOTONIC + clock_id != CLOCK_MONOTONIC && +#endif + clock_id != CLOCK_REALTIME)) + { + return EINVAL; + } + + attr->clockid = clock_id; + + return OK; +} diff --git a/libs/libc/pthread/pthread_condinit.c b/libs/libc/pthread/pthread_condinit.c index b01e7829775..bce0e3584b0 100644 --- a/libs/libc/pthread/pthread_condinit.c +++ b/libs/libc/pthread/pthread_condinit.c @@ -64,7 +64,8 @@ * ****************************************************************************/ -int pthread_cond_init(FAR pthread_cond_t *cond, FAR const pthread_condattr_t *attr) +int pthread_cond_init(FAR pthread_cond_t *cond, + FAR const pthread_condattr_t *attr) { int ret = OK; @@ -90,6 +91,8 @@ int pthread_cond_init(FAR pthread_cond_t *cond, FAR const pthread_condattr_t *at */ sem_setprotocol(&cond->sem, SEM_PRIO_NONE); + + cond->clockid = attr ? attr->clockid : CLOCK_REALTIME; } sinfo("Returning %d\n", ret); diff --git a/libs/libc/pthread/pthread_condtimedwait.c b/libs/libc/pthread/pthread_condtimedwait.c index 9a7dffa78c9..1f7437cbb15 100644 --- a/libs/libc/pthread/pthread_condtimedwait.c +++ b/libs/libc/pthread/pthread_condtimedwait.c @@ -53,5 +53,5 @@ int pthread_cond_timedwait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex, FAR const struct timespec *abstime) { - return pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, abstime); + return pthread_cond_clockwait(cond, mutex, cond->clockid, abstime); }