2023-11-07 18:27:31 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/semaphore/sem_rw.c
|
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2023-11-07 18:27:31 +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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-05-17 17:52:58 +08:00
|
|
|
#include <nuttx/rwsem.h>
|
|
|
|
#include <nuttx/sched.h>
|
2024-02-26 09:45:21 +08:00
|
|
|
#include <assert.h>
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void up_wait(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < rwsem->waiter; i++)
|
|
|
|
{
|
|
|
|
/* If there are some waiter for unlock, then post the lock wait queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&rwsem->waiting);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: down_read_trylock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Acquire a read lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Return 1 if successful, 0 if failed
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int down_read_trylock(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
/* if the write lock is already held by oneself and since the write lock
|
|
|
|
* can be recursively held, so, this operation can be converted to a write
|
|
|
|
* lock to avoid deadlock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rwsem->holder == _SCHED_GETTID())
|
|
|
|
{
|
|
|
|
rwsem->writer++;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-11-07 18:27:31 +08:00
|
|
|
if (rwsem->writer > 0)
|
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In a scenario where there is no write lock, we just need to make the
|
|
|
|
* read base +1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rwsem->reader++;
|
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
out:
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: down_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Acquire a read lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void down_read(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
|
|
|
/* we have to check if there is a write-lock scenario, if there is then we
|
|
|
|
* block and wait for the write-lock to be unlocked.
|
|
|
|
*/
|
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
/* if the write lock is already held by oneself and since the write lock
|
|
|
|
* can be recursively held, so, this operation can be converted to a write
|
|
|
|
* lock to avoid deadlock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rwsem->holder == _SCHED_GETTID())
|
|
|
|
{
|
|
|
|
rwsem->writer++;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-11-07 18:27:31 +08:00
|
|
|
while (rwsem->writer > 0)
|
|
|
|
{
|
|
|
|
rwsem->waiter++;
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
nxsem_wait(&rwsem->waiting);
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
rwsem->waiter--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In a scenario where there is no write lock, we just need to make the
|
|
|
|
* read base +1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rwsem->reader++;
|
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
out:
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock a read lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void up_read(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
/* when releasing a read lock and holder is oneself, the read lock is a
|
|
|
|
* write lock that has been converted, so it should be released according
|
|
|
|
* to the procedures for releasing a write lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rwsem->holder == _SCHED_GETTID())
|
|
|
|
{
|
|
|
|
if (--rwsem->writer <= 0)
|
|
|
|
{
|
|
|
|
rwsem->holder = RWSEM_NO_HOLDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-11-07 18:27:31 +08:00
|
|
|
DEBUGASSERT(rwsem->reader > 0);
|
|
|
|
|
|
|
|
rwsem->reader--;
|
|
|
|
|
|
|
|
if (rwsem->waiter > 0)
|
|
|
|
{
|
|
|
|
up_wait(rwsem);
|
|
|
|
}
|
|
|
|
|
2024-10-02 23:29:22 +08:00
|
|
|
out:
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: down_write_trylock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Acquire a write lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Return 1 if successful, 0 if failed
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int down_write_trylock(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
2024-05-17 17:52:58 +08:00
|
|
|
pid_t tid = _SCHED_GETTID();
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
|
|
|
|
2024-05-17 17:52:58 +08:00
|
|
|
if (rwsem->reader > 0 || (rwsem->writer > 0 && tid != rwsem->holder))
|
2023-11-07 18:27:31 +08:00
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The check passes, then we just need the writer reference + 1 */
|
|
|
|
|
|
|
|
rwsem->writer++;
|
2024-05-17 17:52:58 +08:00
|
|
|
rwsem->holder = tid;
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: down_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Acquire a write lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void down_write(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
2024-05-17 17:52:58 +08:00
|
|
|
pid_t tid = _SCHED_GETTID();
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
|
|
|
|
2024-05-17 17:52:58 +08:00
|
|
|
while (rwsem->reader > 0 || (rwsem->writer > 0 && rwsem->holder != tid))
|
2023-11-07 18:27:31 +08:00
|
|
|
{
|
|
|
|
rwsem->waiter++;
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
nxsem_wait(&rwsem->waiting);
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
rwsem->waiter--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The check passes, then we just need the writer reference + 1 */
|
|
|
|
|
|
|
|
rwsem->writer++;
|
2024-05-17 17:52:58 +08:00
|
|
|
rwsem->holder = tid;
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock a write lock on a read-write-lock object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void up_write(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_lock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(rwsem->writer > 0);
|
2024-05-17 17:52:58 +08:00
|
|
|
DEBUGASSERT(rwsem->holder == _SCHED_GETTID());
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-05-17 17:52:58 +08:00
|
|
|
if (--rwsem->writer <= 0)
|
|
|
|
{
|
|
|
|
rwsem->holder = RWSEM_NO_HOLDER;
|
|
|
|
}
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
up_wait(rwsem);
|
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_unlock(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: init_rwsem
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a read-write-lock object, setting its initial state.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int init_rwsem(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize structure information */
|
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
ret = nxmutex_init(&rwsem->protected);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
ret = nxsem_init(&rwsem->waiting, 0, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_destroy(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rwsem->reader = 0;
|
|
|
|
rwsem->writer = 0;
|
|
|
|
rwsem->waiter = 0;
|
2024-05-17 17:52:58 +08:00
|
|
|
rwsem->holder = RWSEM_NO_HOLDER;
|
2023-11-07 18:27:31 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: destroy_rwsem
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Destroy a read-write-lock object, freeing any resources associated with
|
|
|
|
* it.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rwsem - Pointer to the read-write-lock descriptor.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void destroy_rwsem(FAR rw_semaphore_t *rwsem)
|
|
|
|
{
|
|
|
|
/* Need to check if there is still an unlocked or waiting state */
|
|
|
|
|
|
|
|
DEBUGASSERT(rwsem->waiter == 0 && rwsem->reader == 0 &&
|
2024-05-17 17:52:58 +08:00
|
|
|
rwsem->writer == 0 && rwsem->holder == RWSEM_NO_HOLDER);
|
2023-11-07 18:27:31 +08:00
|
|
|
|
2024-10-24 18:26:00 +08:00
|
|
|
nxmutex_destroy(&rwsem->protected);
|
2023-11-07 18:27:31 +08:00
|
|
|
nxsem_destroy(&rwsem->waiting);
|
|
|
|
}
|