2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_condwait.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-04-05 22:52:23 +02: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
|
|
|
*
|
2020-04-05 22:52:23 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-04-05 22:52:23 +02: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-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: int pthread_cond_wait
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* A thread can wait for a condition variable to be signalled or broadcast.
|
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
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-03-26 19:22:17 +02:00
|
|
|
int status;
|
2007-02-18 00:21:28 +01:00
|
|
|
int ret;
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("cond=0x%p mutex=0x%p\n", cond, mutex);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
/* pthread_cond_wait() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Make sure that non-NULL references were provided. */
|
|
|
|
|
2016-12-10 00:20:57 +01:00
|
|
|
if (cond == NULL || mutex == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the caller holds the mutex */
|
|
|
|
|
|
|
|
else if (mutex->pid != (int)getpid())
|
|
|
|
{
|
|
|
|
ret = EPERM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-06 00:33:44 +02:00
|
|
|
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
|
|
|
|
uint8_t mflags;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
|
|
|
|
uint8_t type;
|
|
|
|
int16_t nlocks;
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Give up the mutex */
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Give up mutex / take cond\n");
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_lock();
|
2015-06-16 16:27:38 +02:00
|
|
|
mutex->pid = -1;
|
2020-04-06 00:33:44 +02:00
|
|
|
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
|
|
|
|
mflags = mutex->flags;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
|
|
|
|
type = mutex->type;
|
|
|
|
nlocks = mutex->nlocks;
|
|
|
|
#endif
|
2020-04-05 22:52:23 +02:00
|
|
|
ret = pthread_mutex_give(mutex);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-04-05 22:52:23 +02:00
|
|
|
/* Take the semaphore. This may be awakened only be a signal (EINTR)
|
|
|
|
* or if the thread is canceled (ECANCELED)
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2019-02-24 21:04:24 +01:00
|
|
|
status = pthread_sem_take((FAR sem_t *)&cond->sem, NULL, false);
|
2017-03-26 19:22:17 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Report the first failure that occurs */
|
|
|
|
|
|
|
|
ret = status;
|
|
|
|
}
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
sched_unlock();
|
|
|
|
|
2016-12-10 01:39:40 +01:00
|
|
|
/* Reacquire the mutex.
|
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* When cancellation points are enabled, we need to hold the mutex
|
|
|
|
* when the pthread is canceled and cleanup handlers, if any, are
|
|
|
|
* entered.
|
2016-12-10 01:39:40 +01:00
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Reacquire mutex...\n");
|
2017-04-11 19:03:25 +02:00
|
|
|
|
2019-02-24 21:04:24 +01:00
|
|
|
status = pthread_mutex_take(mutex, NULL, false);
|
2017-03-26 19:22:17 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Report the first failure that occurs */
|
|
|
|
|
|
|
|
ret = status;
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:52:23 +02:00
|
|
|
/* Did we get the mutex? */
|
2017-03-26 19:22:17 +02:00
|
|
|
|
2020-04-05 22:52:23 +02:00
|
|
|
if (status == OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-04-05 22:52:23 +02:00
|
|
|
/* Yes.. Then initialize it properly */
|
|
|
|
|
2020-04-06 00:33:44 +02:00
|
|
|
mutex->pid = getpid();
|
|
|
|
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
|
|
|
|
mutex->flags = mflags;
|
|
|
|
#endif
|
2020-04-05 22:52:23 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
|
2020-04-06 00:33:44 +02:00
|
|
|
mutex->type = type;
|
|
|
|
mutex->nlocks = nlocks;
|
2020-04-05 22:52:23 +02:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|