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
|
|
|
*
|
2013-02-03 17:43:58 +01:00
|
|
|
* Copyright (C) 2007-2010, 2013 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
|
|
|
|
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
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* 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
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_takesemaphore and pthread_givesemaphore
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Support managed access to the private data sets.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Return 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-03-26 19:22:17 +02:00
|
|
|
int pthread_takesemaphore(sem_t *sem, bool intr)
|
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
|
|
|
{
|
|
|
|
/* Take the semaphore */
|
|
|
|
|
|
|
|
while (sem_wait(sem) != OK)
|
|
|
|
{
|
2017-03-26 19:22:17 +02:00
|
|
|
int errcode = get_errno();
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Handle the special case where the semaphore wait was
|
|
|
|
* awakened by the receipt of a signal.
|
|
|
|
*/
|
|
|
|
|
2017-03-26 19:22:17 +02:00
|
|
|
if (intr || errcode != EINTR)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-03-26 19:22:17 +02:00
|
|
|
return errcode;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 19:22:17 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* NULL semaphore pointer! */
|
|
|
|
|
2017-03-26 19:22:17 +02:00
|
|
|
return EINVAL;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_givesemaphore(sem_t *sem)
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
if (sem_post(sem) == OK)
|
2008-05-31 20:02:49 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* sem_post() reported an error */
|
|
|
|
|
2017-03-26 19:22:17 +02:00
|
|
|
return get_errno();
|
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
|
|
|
}
|
|
|
|
}
|
2007-02-27 22:17:21 +01:00
|
|
|
|