blob: 95e255c4f264f60d79fa52cd7ebdb34d5916bc68 (
plain)
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
|
#include <minix/mthread.h>
#include "global.h"
/*===========================================================================*
* mthread_rwlock_init *
*===========================================================================*/
int mthread_rwlock_init(rwlock)
mthread_rwlock_t *rwlock; /* The rwlock to be initialized */
{
/* Initialize a readers/writer lock. */
int r;
if (!rwlock)
return EINVAL;
rwlock->writer = NO_THREAD;
rwlock->readers = 0;
r = mthread_mutex_init(&rwlock->queue, NULL);
if (r != 0)
return r;
r = mthread_event_init(&rwlock->drain);
if (r != 0)
mthread_mutex_destroy(&rwlock->queue);
return r;
}
/*===========================================================================*
* mthread_rwlock_destroy *
*===========================================================================*/
int mthread_rwlock_destroy(rwlock)
mthread_rwlock_t *rwlock; /* The rwlock to be destroyed */
{
/* Destroy a readers/writer lock. */
int r;
if (!rwlock)
return EINVAL;
assert(rwlock->writer == NO_THREAD);
assert(rwlock->readers == 0);
r = mthread_event_destroy(&rwlock->drain);
if (r != 0)
return r;
return mthread_mutex_destroy(&rwlock->queue);
}
/*===========================================================================*
* mthread_rwlock_rdlock *
*===========================================================================*/
int mthread_rwlock_rdlock(rwlock)
mthread_rwlock_t *rwlock; /* The rwlock to be read locked */
{
/* Acquire a reader lock. */
int r;
if (!rwlock)
return EINVAL;
r = mthread_mutex_lock(&rwlock->queue);
if (r != 0)
return r;
r = mthread_mutex_unlock(&rwlock->queue);
if (r != 0)
return r;
rwlock->readers++;
return 0;
}
/*===========================================================================*
* mthread_rwlock_wrlock *
*===========================================================================*/
int mthread_rwlock_wrlock(rwlock)
mthread_rwlock_t *rwlock; /* The rwlock to be write locked */
{
/* Acquire a writer lock. */
int r;
if (!rwlock)
return EINVAL;
r = mthread_mutex_lock(&rwlock->queue);
if (r != 0)
return r;
rwlock->writer = current_thread;
if (rwlock->readers > 0)
r = mthread_event_wait(&rwlock->drain);
if (r == 0)
assert(rwlock->readers == 0);
return r;
}
/*===========================================================================*
* mthread_rwlock_unlock *
*===========================================================================*/
int mthread_rwlock_unlock(rwlock)
mthread_rwlock_t *rwlock; /* The rwlock to be unlocked */
{
/* Release a lock. */
int r;
r = 0;
if (!rwlock)
return EINVAL;
if (rwlock->writer == current_thread) {
rwlock->writer = NO_THREAD;
r = mthread_mutex_unlock(&rwlock->queue);
} else {
assert(rwlock->readers > 0);
rwlock->readers--;
if (rwlock->readers == 0 && rwlock->writer != NO_THREAD)
r = mthread_event_fire(&rwlock->drain);
}
return r;
}
/* pthread compatibility layer. */
__weak_alias(pthread_rwlock_destroy, mthread_rwlock_destroy)
__weak_alias(pthread_rwlock_rdlock, mthread_rwlock_rdlock)
__weak_alias(pthread_rwlock_wrlock, mthread_rwlock_wrlock)
__weak_alias(pthread_rwlock_unlock, mthread_rwlock_unlock)
|