2017-04-07 15:03:00 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/pthread/pthread_rwlock_rdlock.c
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
2021-03-31 14:31:29 +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
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
2021-03-31 14:31:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
2021-03-31 14:31:29 +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.
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-06-13 08:49:56 +02:00
|
|
|
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
|
2017-04-10 15:18:16 +02:00
|
|
|
static void rdlock_cleanup(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg;
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_mutex_unlock(&rw_lock->lock);
|
2017-04-10 15:18:16 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-07 15:03:00 +02:00
|
|
|
static int tryrdlock(FAR pthread_rwlock_t *rw_lock)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2017-04-07 23:45:24 +02:00
|
|
|
if (rw_lock->num_writers > 0 || rw_lock->write_in_progress)
|
2017-04-07 15:03:00 +02:00
|
|
|
{
|
|
|
|
err = EBUSY;
|
|
|
|
}
|
|
|
|
else if (rw_lock->num_readers == UINT_MAX)
|
|
|
|
{
|
|
|
|
err = EAGAIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rw_lock->num_readers++;
|
|
|
|
err = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_rwlock_rdlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Locks a read/write lock for reading
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-04-07 15:03:00 +02:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-04-07 15:03:00 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pthread_rwlock_tryrdlock(FAR pthread_rwlock_t *rw_lock)
|
|
|
|
{
|
|
|
|
int err = pthread_mutex_trylock(&rw_lock->lock);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tryrdlock(rw_lock);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&rw_lock->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-07-27 07:46:03 +02:00
|
|
|
int pthread_rwlock_clockrdlock(FAR pthread_rwlock_t *rw_lock,
|
|
|
|
clockid_t clockid,
|
2017-04-07 15:03:00 +02:00
|
|
|
FAR const struct timespec *ts)
|
|
|
|
{
|
|
|
|
int err = pthread_mutex_lock(&rw_lock->lock);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-06-13 08:49:56 +02:00
|
|
|
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
|
2017-04-10 15:18:16 +02:00
|
|
|
pthread_cleanup_push(&rdlock_cleanup, rw_lock);
|
|
|
|
#endif
|
2017-04-07 15:03:00 +02:00
|
|
|
while ((err = tryrdlock(rw_lock)) == EBUSY)
|
|
|
|
{
|
|
|
|
if (ts != NULL)
|
|
|
|
{
|
2020-07-27 07:46:03 +02:00
|
|
|
err = pthread_cond_clockwait(&rw_lock->cv, &rw_lock->lock,
|
|
|
|
clockid, ts);
|
2017-04-07 15:03:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = pthread_cond_wait(&rw_lock->cv, &rw_lock->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 07:46:03 +02:00
|
|
|
|
2023-06-13 08:49:56 +02:00
|
|
|
#if defined(CONFIG_PTHREAD_CLEANUP_STACKSIZE) && CONFIG_PTHREAD_CLEANUP_STACKSIZE > 0
|
2017-04-10 15:18:16 +02:00
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
#endif
|
2017-04-07 15:03:00 +02:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&rw_lock->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-07-27 07:46:03 +02:00
|
|
|
int pthread_rwlock_timedrdlock(FAR pthread_rwlock_t *rw_lock,
|
|
|
|
FAR const struct timespec *ts)
|
|
|
|
{
|
|
|
|
return pthread_rwlock_clockrdlock(rw_lock, CLOCK_REALTIME, ts);
|
|
|
|
}
|
|
|
|
|
2017-04-07 15:03:00 +02:00
|
|
|
int pthread_rwlock_rdlock(FAR pthread_rwlock_t * rw_lock)
|
|
|
|
{
|
|
|
|
return pthread_rwlock_timedrdlock(rw_lock, NULL);
|
|
|
|
}
|