2016-11-02 23:05:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/semaphore/sem_setprotocol.c
|
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 23:33:58 +08: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 23:05:18 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-11-02 23:05:18 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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 23:05:18 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "semaphore/semaphore.h"
|
|
|
|
|
2016-11-04 08:51:38 +08:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
|
2016-11-02 23:05:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-17 21:56:21 +08:00
|
|
|
* Name: nxsem_set_protocol
|
2016-11-02 23:05:18 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set semaphore protocol attribute.
|
|
|
|
*
|
2019-09-09 05:59:14 +08:00
|
|
|
* One particularly important use of this function is when a semaphore
|
2016-11-03 08:21:46 +08:00
|
|
|
* is used for inter-task communication like:
|
|
|
|
*
|
|
|
|
* TASK A TASK B
|
2017-10-04 05:35:24 +08:00
|
|
|
* nxsem_init(sem, 0, 0);
|
|
|
|
* nxsem_wait(sem);
|
|
|
|
* nxsem_post(sem);
|
2016-11-03 08:21:46 +08: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-04 05:35:24 +08:00
|
|
|
* holder of the semaphore. However, it never calls nxsem_post(sem) so it
|
2016-11-03 08:21:46 +08: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 21:56:21 +08:00
|
|
|
* The fix is to call nxsem_set_protocol(SEM_PRIO_NONE) immediately after
|
2016-11-03 08:21:46 +08:00
|
|
|
* the sem_init() call so that there will be no priority inheritance
|
|
|
|
* operations on this semaphore.
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2016-11-02 23:05:18 +08:00
|
|
|
* sem - A pointer to the semaphore whose attributes are to be
|
|
|
|
* modified
|
|
|
|
* protocol - The new protocol to use
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2017-10-04 05:35:24 +08: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 23:05:18 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-17 21:56:21 +08:00
|
|
|
int nxsem_set_protocol(FAR sem_t *sem, int protocol)
|
2016-11-02 23:05:18 +08:00
|
|
|
{
|
|
|
|
DEBUGASSERT(sem != NULL);
|
|
|
|
|
2023-01-11 00:10:48 +08:00
|
|
|
switch (protocol & SEM_PRIO_MASK)
|
2016-11-02 23:05:18 +08:00
|
|
|
{
|
|
|
|
case SEM_PRIO_NONE:
|
2019-10-25 00:49:28 +08:00
|
|
|
|
2016-11-02 23:05:18 +08:00
|
|
|
/* Remove any current holders */
|
|
|
|
|
2017-10-04 02:51:15 +08:00
|
|
|
nxsem_destroyholder(sem);
|
2023-01-11 00:10:48 +08:00
|
|
|
break;
|
2016-11-02 23:05:18 +08:00
|
|
|
|
|
|
|
case SEM_PRIO_INHERIT:
|
2019-10-25 00:49:28 +08:00
|
|
|
|
2023-01-11 00:10:48 +08:00
|
|
|
break;
|
2016-11-02 23:05:18 +08:00
|
|
|
|
|
|
|
case SEM_PRIO_PROTECT:
|
2024-08-05 15:10:44 +08:00
|
|
|
#ifdef CONFIG_PRIORITY_PROTECT
|
|
|
|
break;
|
|
|
|
#else
|
2016-11-02 23:05:18 +08:00
|
|
|
/* Not yet supported */
|
|
|
|
|
2022-01-06 18:02:13 +08:00
|
|
|
return -ENOTSUP;
|
2024-08-05 15:10:44 +08:00
|
|
|
#endif
|
2016-11-02 23:05:18 +08:00
|
|
|
|
|
|
|
default:
|
2023-01-11 00:10:48 +08:00
|
|
|
return -EINVAL;
|
2016-11-02 23:05:18 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 00:10:48 +08:00
|
|
|
sem->flags = protocol;
|
|
|
|
return OK;
|
2017-10-04 05:35:24 +08:00
|
|
|
}
|
|
|
|
|
2016-11-04 11:27:52 +08:00
|
|
|
#endif /* CONFIG_PRIORITY_INHERITANCE */
|