Hash :
b3bc87e4
Author :
Thomas de Grivel
Date :
2024-12-04T16:20:49
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <errno.h>
#include <pthread.h>
#include "assert.h"
#include "io.h"
#include "rwlock.h"
#if HAVE_PTHREAD
void rwlock_clean (s_rwlock *rwlock)
{
assert(rwlock);
if (pthread_rwlock_destroy(&rwlock->rwlock)) {
err_puts("rwlock_clean: pthread_rwlock_destroy");
assert(! "rwlock_clean: pthread_rwlock_destroy");
abort();
}
}
s_rwlock * rwlock_init (s_rwlock *rwlock)
{
uw i;
assert(rwlock);
if (false) {
i = (uw) rwlock->rwlock;
err_write_1("rwlock_init: ");
err_inspect_uw_hexadecimal(&i);
err_write_1("\n");
}
if (pthread_rwlock_init(&rwlock->rwlock, NULL)) {
err_puts("rwlock_init: pthread_rwlock_init");
assert(! "rwlock_init: pthread_rwlock_init");
return NULL;
}
rwlock->count = 0;
rwlock->thread = 0;
return rwlock;
}
s_rwlock * rwlock_r (s_rwlock *rwlock)
{
sw e;
uw i;
pthread_t thread;
assert(rwlock);
thread = pthread_self();
if (rwlock->thread != thread) {
if (false) {
i = (uw) rwlock;
err_write_1("rwlock_r: ");
err_inspect_uw_hexadecimal(&i);
err_write_1("\n");
}
if ((e = pthread_rwlock_rdlock(&rwlock->rwlock))) {
err_write_1("rwlock_r: pthread_rwlock_rdlock: ");
switch (e) {
case EAGAIN:
err_puts("the maximum number of read locks on this lock has"
" been exceeded");
break;
case EDEADLK:
err_puts("the current thread already owns this lock for"
" writing");
break;
case EINVAL:
err_puts("invalid lock");
break;
case ENOMEM:
err_puts("not enough memory to initialize the lock");
break;
default:
err_puts("unknown error");
break;
}
assert(! "rwlock_r: pthread_rwlock_rdlock");
abort();
return NULL;
}
}
return rwlock;
}
s_rwlock * rwlock_unlock_r (s_rwlock *rwlock)
{
pthread_t thread;
assert(rwlock);
thread = pthread_self();
if (rwlock->thread != thread &&
pthread_rwlock_unlock(&rwlock->rwlock)) {
err_puts("rwlock_unlock_r: pthread_rwlock_unlock");
assert(! "rwlock_unlock_r: pthread_rwlock_unlock");
abort();
return NULL;
}
return rwlock;
}
s_rwlock * rwlock_unlock_w (s_rwlock *rwlock)
{
assert(rwlock);
assert(rwlock->count);
rwlock->count--;
if (! rwlock->count) {
rwlock->thread = 0;
if (pthread_rwlock_unlock(&rwlock->rwlock)) {
err_puts("rwlock_unlock_w: pthread_rwlock_unlock");
assert(! "rwlock_unlock_w: pthread_rwlock_unlock");
return NULL;
}
}
return rwlock;
}
s_rwlock * rwlock_w (s_rwlock *rwlock)
{
pthread_t thread;
assert(rwlock);
thread = pthread_self();
if (rwlock->thread != thread) {
if (pthread_rwlock_wrlock(&rwlock->rwlock)) {
err_puts("rwlock_w: pthread_rwlock_wrlock");
assert(! "rwlock_w: pthread_rwlock_wrlock");
return NULL;
}
rwlock->thread = thread;
rwlock->count = 0;
}
rwlock->count++;
return rwlock;
}
#endif /* HAVE_PTHREAD */