2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Implements simple shared/exclusive locks using LWKT.
28 * $DragonFly: src/sys/kern/Attic/lwkt_rwlock.c,v 1.4 2003/07/20 01:37:22 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/rtprio.h>
36 #include <sys/queue.h>
39 * NOTE! called from low level boot, we cannot do anything fancy.
42 lwkt_rwlock_init(lwkt_rwlock_t lock)
44 lwkt_init_wait(&lock->rw_wait);
50 lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg)
54 lwkt_gettoken(&lock->rw_token);
55 gen = lock->rw_wait.wa_gen;
56 while (lock->rw_owner != curthread) {
57 if (lock->rw_owner == NULL && lock->rw_count == 0) {
58 lock->rw_owner = curthread;
62 lwkt_block(&lock->rw_wait, wmesg, &gen);
63 lwkt_regettoken(&lock->rw_token);
67 lwkt_reltoken(&lock->rw_token);
71 lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg)
75 lwkt_gettoken(&lock->rw_token);
76 gen = lock->rw_wait.wa_gen;
77 while (lock->rw_owner != NULL) {
79 lwkt_block(&lock->rw_wait, wmesg, &gen);
80 lwkt_regettoken(&lock->rw_token);
84 lwkt_reltoken(&lock->rw_token);
88 lwkt_exunlock(lwkt_rwlock_t lock)
90 lwkt_gettoken(&lock->rw_token);
91 KASSERT(lock->rw_owner != NULL, ("lwkt_exunlock: shared lock"));
92 KASSERT(lock->rw_owner == curthread, ("lwkt_exunlock: not owner"));
93 if (--lock->rw_count == 0) {
94 lock->rw_owner = NULL;
95 if (lock->rw_requests)
96 lwkt_signal(&lock->rw_wait, 1);
98 lwkt_reltoken(&lock->rw_token);
102 lwkt_shunlock(lwkt_rwlock_t lock)
104 lwkt_gettoken(&lock->rw_token);
105 KASSERT(lock->rw_owner == NULL, ("lwkt_shunlock: exclusive lock"));
106 if (--lock->rw_count == 0) {
107 if (lock->rw_requests)
108 lwkt_signal(&lock->rw_wait, 1);
110 lwkt_reltoken(&lock->rw_token);