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