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
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2007-2010, 2013, 2017 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-05-31 20:02:49 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
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>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <semaphore.h>
|
2017-03-26 19:22:17 +02:00
|
|
|
#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
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_initialize
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-02-03 17:43:58 +01:00
|
|
|
* This is an internal OS function called only at power-up boot time. It
|
|
|
|
* no longer does anything since all of the pthread data structures have
|
|
|
|
* been moved into the "task group"
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-02-18 00:21:28 +01:00
|
|
|
* None
|
|
|
|
*
|
2008-05-31 20:02:49 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
void pthread_initialize(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* intr - false: ignore EINTR errors when locking; true tread EINTR as
|
|
|
|
* 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
|
|
|
|
2017-05-29 15:05:06 +02:00
|
|
|
int pthread_sem_take(sem_t *sem, bool intr)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02: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
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
do
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(sem);
|
|
|
|
if (ret < 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-10-10 16:43:10 +02:00
|
|
|
/* The only cases that an error should occur here is if the wait
|
|
|
|
* was awakened by a signal or if the thread was canceled during
|
|
|
|
* the wait.
|
2017-10-04 23:22:27 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
/* When the error occurs in this case, should we errout? Or
|
|
|
|
* should we just continue waiting until we have the
|
|
|
|
* semaphore?
|
2017-10-04 23:22:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (intr)
|
|
|
|
{
|
|
|
|
return -ret;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
|
|
|
|
|
|
|
/* We have the semaphore (or some awful, unexpected error has
|
|
|
|
* occurred).
|
|
|
|
*/
|
2017-03-26 19:22:17 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
return EINVAL;
|
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);
|
|
|
|
ret = ret < 0 ? -ret : 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
|
|
|
}
|
|
|
|
}
|