1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add pthread_rwlock_{rdlock,wrlock}(3) and pthread_rwlock_unlock(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-09-03 21:47:19 +02:00
parent 993a5736f9
commit 42c051320d
6 changed files with 151 additions and 5 deletions

View file

@ -32,6 +32,9 @@ pthread_mutex_init.o \
pthread_mutex_lock.o \
pthread_mutex_trylock.o \
pthread_mutex_unlock.o \
pthread_rwlock_rdlock.o \
pthread_rwlock_unlock.o \
pthread_rwlock_wrlock.o \
pthread_self.o \
BINS:=libpthread.a

View file

@ -101,7 +101,29 @@ typedef struct
typedef int __pthread_once_t;
typedef int __pthread_rwlock_t;
#if defined(__is_sortix_libpthread)
typedef struct
{
__pthread_cond_t reader_condition;
__pthread_cond_t writer_condition;
__pthread_mutex_t request_mutex;
unsigned long num_readers;
unsigned long num_writers;
unsigned long pending_readers;
unsigned long pending_writers;
} __pthread_rwlock_t;
#else
typedef struct
{
__pthread_cond_t __pthread_reader_condition;
__pthread_cond_t __pthread_writer_condition;
__pthread_mutex_t __pthread_request_mutex;
unsigned long __pthread_num_readers;
unsigned long __pthread_num_writers;
unsigned long __pthread_pending_readers;
unsigned long __pthread_pending_writers;
} __pthread_rwlock_t;
#endif
typedef int __pthread_rwlockattr_t;

View file

@ -152,7 +152,9 @@ struct pthread_cond_elem
#define PTHREAD_COND_INITIALIZER { NULL, NULL, CLOCK_REALTIME }
#define PTHREAD_MUTEX_INITIALIZER { 0, PTHREAD_MUTEX_DEFAULT, 0, 0 }
#define PTHREAD_RWLOCK_INITIALIZER 0
#define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_COND_INITIALIZER, \
PTHREAD_COND_INITIALIZER, \
PTHREAD_MUTEX_INITIALIZER, 0, 0, 0, 0 }
#define PTHREAD_NORMAL_MUTEX_INITIALIZER_NP { 0, PTHREAD_MUTEX_NORMAL, 0, 0 }
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { 0, PTHREAD_MUTEX_RECURSIVE, 0, 0 }
@ -240,13 +242,13 @@ int pthread_mutexattr_settype(pthread_mutexattr_t*, int);
/* TODO: pthread_once */
/* TODO: pthread_rwlock_destroy */
/* TODO: pthread_rwlock_init */
/* TODO: pthread_rwlock_rdlock */
int pthread_rwlock_rdlock(pthread_rwlock_t*);
/* TODO: pthread_rwlock_timedrdlock */
/* TODO: pthread_rwlock_timedwrlock */
/* TODO: pthread_rwlock_tryrdlock */
/* TODO: pthread_rwlock_trywrlock */
/* TODO: pthread_rwlock_unlock */
/* TODO: pthread_rwlock_wrlock */
int pthread_rwlock_unlock(pthread_rwlock_t*);
int pthread_rwlock_wrlock(pthread_rwlock_t*);
/* TODO: pthread_rwlockattr_destroy */
/* TODO: pthread_rwlockattr_getpshared */
/* TODO: pthread_rwlockattr_init */

View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of Sortix libpthread.
Sortix libpthread is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Sortix libpthread is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Sortix libpthread. If not, see <http://www.gnu.org/licenses/>.
pthread_rwlock_rdlock.c++
Acquires read access to a read-write lock.
*******************************************************************************/
#include <pthread.h>
extern "C" int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock)
{
pthread_mutex_lock(&rwlock->request_mutex);
rwlock->pending_readers++;
while ( rwlock->num_writers || rwlock->pending_writers )
pthread_cond_wait(&rwlock->reader_condition, &rwlock->request_mutex);
rwlock->pending_readers--;
rwlock->num_readers++;
pthread_mutex_unlock(&rwlock->request_mutex);
return 0;
}

View file

@ -0,0 +1,45 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of Sortix libpthread.
Sortix libpthread is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Sortix libpthread is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Sortix libpthread. If not, see <http://www.gnu.org/licenses/>.
pthread_rwlock_unlock.c++
Releases hold of a read-write lock.
*******************************************************************************/
#include <pthread.h>
extern "C" int pthread_rwlock_unlock(pthread_rwlock_t* rwlock)
{
pthread_mutex_lock(&rwlock->request_mutex);
if ( rwlock->num_writers )
{
rwlock->num_writers = 0;
if ( rwlock->pending_writers )
pthread_cond_signal(&rwlock->writer_condition);
else
pthread_cond_broadcast(&rwlock->reader_condition);
}
else
{
if ( --rwlock->num_readers == 0 && rwlock->pending_writers )
pthread_cond_signal(&rwlock->writer_condition);
}
pthread_mutex_unlock(&rwlock->request_mutex);
return 0;
}

View file

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of Sortix libpthread.
Sortix libpthread is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Sortix libpthread is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Sortix libpthread. If not, see <http://www.gnu.org/licenses/>.
pthread_rwlock_wrlock.c++
Acquires write access to a read-write lock.
*******************************************************************************/
#include <pthread.h>
extern "C" int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock)
{
pthread_mutex_lock(&rwlock->request_mutex);
rwlock->pending_writers++;
while ( rwlock->num_readers || rwlock->num_writers )
pthread_cond_wait(&rwlock->writer_condition, &rwlock->request_mutex);
rwlock->pending_writers--;
rwlock->num_writers = 1;
pthread_mutex_unlock(&rwlock->request_mutex);
return 0;
}