aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pthread_rwlock.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/pthread_rwlock.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/pthread_rwlock.c')
-rw-r--r--lib/mlibc/tests/posix/pthread_rwlock.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/pthread_rwlock.c b/lib/mlibc/tests/posix/pthread_rwlock.c
new file mode 100644
index 0000000..5316372
--- /dev/null
+++ b/lib/mlibc/tests/posix/pthread_rwlock.c
@@ -0,0 +1,114 @@
+#include <assert.h>
+#include <errno.h>
+#include <pthread.h>
+
+static void test_write_lock_unlock() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_wrlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_read_lock_unlock() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_rdlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_write_trylock_unlock() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_trywrlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_read_trylock_unlock() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_tryrdlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_write_prevents_read() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_wrlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_tryrdlock(&rw);
+ assert(res == EBUSY);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_write_prevents_write() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_wrlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_trywrlock(&rw);
+ assert(res == EBUSY);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_read_prevents_write() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_rdlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_trywrlock(&rw);
+ assert(res == EBUSY);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_read_allows_read() {
+ int res;
+ pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
+ res = pthread_rwlock_rdlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_tryrdlock(&rw);
+ assert(!res);
+ res = pthread_rwlock_unlock(&rw);
+ assert(!res);
+}
+
+static void test_attr() {
+ pthread_rwlockattr_t attr;
+ pthread_rwlockattr_init(&attr);
+
+ int pshared;
+ pthread_rwlockattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_PRIVATE);
+
+ pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+ pthread_rwlockattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_SHARED);
+
+ pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
+ pthread_rwlockattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_PRIVATE);
+
+ pthread_rwlockattr_destroy(&attr);
+}
+
+int main() {
+ test_write_lock_unlock();
+ test_read_lock_unlock();
+ test_write_trylock_unlock();
+ test_read_trylock_unlock();
+ test_write_prevents_read();
+ test_write_prevents_write();
+ test_read_prevents_write();
+ test_read_allows_read();
+ test_attr();
+}