2017-04-07 15:03:00 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/pthread/pthread_rwlock_wrlock.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>
|
|
|
|
|
2017-04-10 15:18:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
static void wrlock_cleanup(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg;
|
|
|
|
|
|
|
|
rw_lock->num_writers--;
|
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
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-10 15:18:16 +02:00
|
|
|
* Name: pthread_rwlock_wrlock
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-04-10 15:18:16 +02:00
|
|
|
* Locks a read/write lock for writing
|
2017-04-07 15:03:00 +02:00
|
|
|
*
|
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_trywrlock(FAR pthread_rwlock_t *rw_lock)
|
|
|
|
{
|
|
|
|
int err = pthread_mutex_trylock(&rw_lock->lock);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-07 23:45:24 +02:00
|
|
|
if (rw_lock->num_readers > 0 || rw_lock->write_in_progress)
|
2017-04-07 15:03:00 +02:00
|
|
|
{
|
|
|
|
err = EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-07 23:45:24 +02:00
|
|
|
rw_lock->write_in_progress = true;
|
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_clockwrlock(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;
|
|
|
|
}
|
|
|
|
|
2017-04-07 23:45:24 +02:00
|
|
|
if (rw_lock->num_writers == UINT_MAX)
|
2017-04-07 15:03:00 +02:00
|
|
|
{
|
2017-04-07 23:45:24 +02:00
|
|
|
err = EAGAIN;
|
2017-04-07 15:03:00 +02:00
|
|
|
goto exit_with_mutex;
|
|
|
|
}
|
|
|
|
|
2017-04-07 23:45:24 +02:00
|
|
|
rw_lock->num_writers++;
|
|
|
|
|
2017-04-10 15:18:16 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
pthread_cleanup_push(&wrlock_cleanup, rw_lock);
|
|
|
|
#endif
|
2017-04-07 23:45:24 +02:00
|
|
|
while (rw_lock->write_in_progress || rw_lock->num_readers > 0)
|
2017-04-07 15:03:00 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-10 15:18:16 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
#endif
|
2017-04-07 15:03:00 +02:00
|
|
|
|
2017-04-07 23:45:24 +02:00
|
|
|
if (err == 0)
|
|
|
|
{
|
|
|
|
rw_lock->write_in_progress = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In case of error, notify any blocked readers. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_cond_broadcast(&rw_lock->cv);
|
2017-04-07 23:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rw_lock->num_writers--;
|
|
|
|
|
2017-04-07 15:03:00 +02:00
|
|
|
exit_with_mutex:
|
|
|
|
pthread_mutex_unlock(&rw_lock->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-07-27 07:46:03 +02:00
|
|
|
int pthread_rwlock_timedwrlock(FAR pthread_rwlock_t *rw_lock,
|
|
|
|
FAR const struct timespec *ts)
|
|
|
|
{
|
|
|
|
return pthread_rwlock_clockwrlock(rw_lock, CLOCK_REALTIME, ts);
|
|
|
|
}
|
|
|
|
|
2017-04-07 15:03:00 +02:00
|
|
|
int pthread_rwlock_wrlock(FAR pthread_rwlock_t *rw_lock)
|
|
|
|
{
|
|
|
|
return pthread_rwlock_timedwrlock(rw_lock, NULL);
|
|
|
|
}
|