2008-05-31 20:02:49 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_initialize.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* 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.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-05-31 20:02:49 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 20:02:49 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-05-31 20:02:49 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2017-03-26 19:22:17 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 20:02:49 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-05-31 20:02:49 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 20:02:49 +02:00
|
|
|
/****************************************************************************
|
2017-05-29 15:05:06 +02:00
|
|
|
* Name: pthread_sem_take, pthread_sem_trytake, and
|
|
|
|
* pthread_sem_give
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Support managed access to the private data sets.
|
|
|
|
*
|
2017-05-29 15:05:06 +02:00
|
|
|
* REVISIT: These functions really do nothing more than match the return
|
|
|
|
* value of the semaphore functions (0 or -1 with errno set) to the
|
|
|
|
* return value of more pthread functions (0 or errno). A better solution
|
|
|
|
* would be to use an internal version of the semaphore functions that
|
|
|
|
* return the error value in the correct form.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-03-26 19:22:17 +02:00
|
|
|
* sem - The semaphore to lock or unlock
|
2021-02-25 13:48:46 +01:00
|
|
|
* intr - false: ignore EINTR errors when locking; true treat EINTR as
|
2017-03-26 19:22:17 +02:00
|
|
|
* other errors by returning the errno value
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-03-26 19:22:17 +02:00
|
|
|
* 0 on success or an errno value on failure.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-05-31 20:02:49 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2019-02-24 21:40:11 +01:00
|
|
|
int pthread_sem_take(FAR sem_t *sem, FAR const struct timespec *abs_timeout,
|
|
|
|
bool intr)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
if (intr)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
if (abs_timeout == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
ret = nxsem_wait(sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nxsem_timedwait(sem, abs_timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (abs_timeout == NULL)
|
|
|
|
{
|
|
|
|
ret = nxsem_wait_uninterruptible(sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nxsem_timedwait_uninterruptible(sem, abs_timeout);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
return -ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2017-05-29 15:05:06 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_UNSAFE
|
|
|
|
int pthread_sem_trytake(sem_t *sem)
|
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
if (sem != NULL)
|
|
|
|
{
|
|
|
|
/* Try to take the semaphore */
|
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
int status = nxsem_trywait(sem);
|
2018-08-06 00:23:07 +02:00
|
|
|
ret = status < 0 ? -status : OK;
|
2017-05-29 15:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int pthread_sem_give(sem_t *sem)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2018-01-30 23:16:41 +01:00
|
|
|
int ret;
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Verify input parameters */
|
|
|
|
|
2017-03-26 19:22:17 +02:00
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
if (sem != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Give the semaphore */
|
|
|
|
|
2018-01-30 23:16:41 +01:00
|
|
|
ret = nxsem_post(sem);
|
|
|
|
if (ret < 0)
|
2008-05-31 20:02:49 +02:00
|
|
|
{
|
2018-01-30 23:16:41 +01:00
|
|
|
return -ret;
|
2008-05-31 20:02:49 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2018-01-30 23:16:41 +01:00
|
|
|
return OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* NULL semaphore pointer! */
|
|
|
|
|
2017-03-26 19:22:17 +02:00
|
|
|
return EINVAL;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|