2016-11-04 01:51:38 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/semaphore/sem_setprotocol.c
|
2016-11-04 01:51:38 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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-04 01:51:38 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-17 15:56:21 +02:00
|
|
|
* Name: nxsem_set_protocol
|
2016-11-04 01:51:38 +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-04 01:51:38 +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);
|
2021-05-06 04:18:49 +02:00
|
|
|
* nxsem_post(sem);
|
2016-11-04 01:51:38 +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-04 01:51:38 +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-04 01:51:38 +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-04 01:51:38 +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 20:51:15 +02:00
|
|
|
* This function is exposed as a non-standard application interface. It
|
2022-01-06 11:02:13 +01:00
|
|
|
* returns zero (OK) if successful. Otherwise, -EINVAL is returned if the
|
|
|
|
* value specified by protocol is invalid or -ENOTSUP if the value
|
|
|
|
* specified by protocol is an unsupported value.
|
2016-11-04 01:51:38 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
int nxsem_set_protocol(FAR sem_t *sem, int protocol)
|
2016-11-04 01:51:38 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
|
2023-06-19 09:49:45 +02:00
|
|
|
switch (protocol & SEM_PRIO_MASK)
|
2016-11-04 01:51:38 +01:00
|
|
|
{
|
|
|
|
case SEM_PRIO_NONE:
|
2023-06-19 09:49:45 +02:00
|
|
|
break;
|
2016-11-04 01:51:38 +01:00
|
|
|
|
|
|
|
case SEM_PRIO_INHERIT:
|
|
|
|
case SEM_PRIO_PROTECT:
|
2022-01-06 11:02:13 +01:00
|
|
|
return -ENOTSUP;
|
2016-11-04 01:51:38 +01:00
|
|
|
|
|
|
|
default:
|
2023-06-19 09:49:45 +02:00
|
|
|
return -EINVAL;
|
2016-11-04 01:51:38 +01:00
|
|
|
}
|
|
|
|
|
2023-06-19 09:49:45 +02:00
|
|
|
sem->flags = protocol;
|
|
|
|
return OK;
|
2017-10-03 23:35:24 +02:00
|
|
|
}
|
|
|
|
|
2023-11-16 15:27:06 +01:00
|
|
|
#endif /* !CONFIG_PRIORITY_INHERITANCE */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sem_setprotocol
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set semaphore protocol attribute.
|
|
|
|
*
|
2019-09-08 23:59:14 +02:00
|
|
|
* One particularly important use of this function is when a semaphore
|
2017-10-03 23:35:24 +02:00
|
|
|
* is used for inter-task communication like:
|
|
|
|
*
|
|
|
|
* TASK A TASK B
|
|
|
|
* sem_init(sem, 0, 0);
|
|
|
|
* sem_wait(sem);
|
|
|
|
* sem_post(sem);
|
|
|
|
* 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
|
|
|
|
* holder of the semaphore. However, it never calls sem_post(sem) so it
|
|
|
|
* becomes *permanently* a holder of the semaphore and may have its
|
|
|
|
* priority boosted when any other task tries to acquire the semaphore.
|
|
|
|
*
|
|
|
|
* The fix is to call sem_setprotocol(SEM_PRIO_NONE) immediately after
|
|
|
|
* 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:
|
2017-10-03 23:35:24 +02: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 function is exposed as a non-standard application interface. It
|
|
|
|
* returns zero (OK) if successful. Otherwise, -1 (ERROR) is returned and
|
|
|
|
* the errno value is set appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sem_setprotocol(FAR sem_t *sem, int protocol)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
ret = nxsem_set_protocol(sem, protocol);
|
2017-10-03 23:35:24 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-11-04 01:51:38 +01:00
|
|
|
}
|