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 <anchao@xiaomi.com>
This commit is contained in:
chao.an 2020-08-12 17:45:35 +08:00 committed by Abdelatif Guettouche
parent 79d476e423
commit 7ce2b3fa74
7 changed files with 148 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <nuttx/config.h>
#include <pthread.h>
#include <errno.h>
/****************************************************************************
* 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;
}

View File

@ -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);

View File

@ -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 <nuttx/config.h>
#include <pthread.h>
#include <errno.h>
/****************************************************************************
* 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;
}

View File

@ -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);

View File

@ -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);
}