2016-11-02 16:05:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/semaphore/sem_setprotocol.c
|
|
|
|
*
|
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
|
2016-11-02 16:05:18 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-11-02 16:05:18 +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.
|
2016-11-02 16:05:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "semaphore/semaphore.h"
|
|
|
|
|
2016-11-04 01:51:38 +01:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
|
2016-11-02 16:05:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-17 15:56:21 +02:00
|
|
|
* Name: nxsem_set_protocol
|
2016-11-02 16:05:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set semaphore protocol attribute.
|
|
|
|
*
|
2019-09-08 23:59:14 +02:00
|
|
|
* One particularly important use of this function is when a semaphore
|
2016-11-03 01:21:46 +01:00
|
|
|
* is used for inter-task communication like:
|
|
|
|
*
|
|
|
|
* TASK A TASK B
|
2017-10-03 23:35:24 +02:00
|
|
|
* nxsem_init(sem, 0, 0);
|
|
|
|
* nxsem_wait(sem);
|
|
|
|
* nxsem_post(sem);
|
2016-11-03 01:21:46 +01:00
|
|
|
* Awakens as holder
|
|
|
|
*
|
|
|
|
* In this case priority inheritance can interfere with the operation of
|
|
|
|
* the semaphore. The problem is that when TASK A is restarted it is a
|
2017-10-03 23:35:24 +02:00
|
|
|
* holder of the semaphore. However, it never calls nxsem_post(sem) so it
|
2016-11-03 01:21:46 +01:00
|
|
|
* becomes *permanently* a holder of the semaphore and may have its
|
|
|
|
* priority boosted when any other task tries to acquire the semaphore.
|
|
|
|
*
|
2020-05-17 15:56:21 +02:00
|
|
|
* The fix is to call nxsem_set_protocol(SEM_PRIO_NONE) immediately after
|
2016-11-03 01:21:46 +01:00
|
|
|
* the sem_init() call so that there will be no priority inheritance
|
|
|
|
* operations on this semaphore.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-11-02 16:05:18 +01:00
|
|
|
* sem - A pointer to the semaphore whose attributes are to be
|
|
|
|
* modified
|
|
|
|
* protocol - The new protocol to use
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-10-03 23:35:24 +02:00
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
2016-11-02 16:05:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
int nxsem_set_protocol(FAR sem_t *sem, int protocol)
|
2016-11-02 16:05:18 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
|
2023-01-10 17:10:48 +01:00
|
|
|
switch (protocol & SEM_PRIO_MASK)
|
2016-11-02 16:05:18 +01:00
|
|
|
{
|
|
|
|
case SEM_PRIO_NONE:
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2016-11-02 16:05:18 +01:00
|
|
|
/* Remove any current holders */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_destroyholder(sem);
|
2023-01-10 17:10:48 +01:00
|
|
|
break;
|
2016-11-02 16:05:18 +01:00
|
|
|
|
|
|
|
case SEM_PRIO_INHERIT:
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2023-01-10 17:10:48 +01:00
|
|
|
break;
|
2016-11-02 16:05:18 +01:00
|
|
|
|
|
|
|
case SEM_PRIO_PROTECT:
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2016-11-02 16:05:18 +01:00
|
|
|
/* Not yet supported */
|
|
|
|
|
2022-01-06 11:02:13 +01:00
|
|
|
return -ENOTSUP;
|
2016-11-02 16:05:18 +01:00
|
|
|
|
|
|
|
default:
|
2023-01-10 17:10:48 +01:00
|
|
|
return -EINVAL;
|
2016-11-02 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 17:10:48 +01:00
|
|
|
sem->flags = protocol;
|
|
|
|
return OK;
|
2017-10-03 23:35:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 04:27:52 +01:00
|
|
|
#endif /* CONFIG_PRIORITY_INHERITANCE */
|