1 #include <minix/mthread.h>
4 /*===========================================================================*
5 * mthread_rwlock_init *
6 *===========================================================================*/
7 int mthread_rwlock_init(rwlock
)
8 mthread_rwlock_t
*rwlock
; /* The rwlock to be initialized */
10 /* Initialize a readers/writer lock. */
16 rwlock
->writer
= NO_THREAD
;
19 r
= mthread_mutex_init(&rwlock
->queue
, NULL
);
23 r
= mthread_event_init(&rwlock
->drain
);
25 mthread_mutex_destroy(&rwlock
->queue
);
30 /*===========================================================================*
31 * mthread_rwlock_destroy *
32 *===========================================================================*/
33 int mthread_rwlock_destroy(rwlock
)
34 mthread_rwlock_t
*rwlock
; /* The rwlock to be destroyed */
36 /* Destroy a readers/writer lock. */
42 assert(rwlock
->writer
== NO_THREAD
);
43 assert(rwlock
->readers
== 0);
45 r
= mthread_event_destroy(&rwlock
->drain
);
49 return mthread_mutex_destroy(&rwlock
->queue
);
52 /*===========================================================================*
53 * mthread_rwlock_rdlock *
54 *===========================================================================*/
55 int mthread_rwlock_rdlock(rwlock
)
56 mthread_rwlock_t
*rwlock
; /* The rwlock to be read locked */
58 /* Acquire a reader lock. */
64 r
= mthread_mutex_lock(&rwlock
->queue
);
68 r
= mthread_mutex_unlock(&rwlock
->queue
);
77 /*===========================================================================*
78 * mthread_rwlock_wrlock *
79 *===========================================================================*/
80 int mthread_rwlock_wrlock(rwlock
)
81 mthread_rwlock_t
*rwlock
; /* The rwlock to be write locked */
83 /* Acquire a writer lock. */
89 r
= mthread_mutex_lock(&rwlock
->queue
);
93 rwlock
->writer
= current_thread
;
95 if (rwlock
->readers
> 0)
96 r
= mthread_event_wait(&rwlock
->drain
);
99 assert(rwlock
->readers
== 0);
104 /*===========================================================================*
105 * mthread_rwlock_unlock *
106 *===========================================================================*/
107 int mthread_rwlock_unlock(rwlock
)
108 mthread_rwlock_t
*rwlock
; /* The rwlock to be unlocked */
110 /* Release a lock. */
117 if (rwlock
->writer
== current_thread
) {
118 rwlock
->writer
= NO_THREAD
;
119 r
= mthread_mutex_unlock(&rwlock
->queue
);
121 assert(rwlock
->readers
> 0);
125 if (rwlock
->readers
== 0 && rwlock
->writer
!= NO_THREAD
)
126 r
= mthread_event_fire(&rwlock
->drain
);