2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Implements simple shared/exclusive locks using LWKT.
36 * $DragonFly: src/sys/kern/Attic/lwkt_rwlock.c,v 1.6 2004/07/16 05:51:10 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/rtprio.h>
44 #include <sys/queue.h>
47 * NOTE! called from low level boot, we cannot do anything fancy.
50 lwkt_rwlock_init(lwkt_rwlock_t lock)
52 lwkt_wait_init(&lock->rw_wait);
53 lock->rw_owner = NULL;
55 lock->rw_requests = 0;
59 lwkt_rwlock_uninit(lwkt_rwlock_t lock)
65 lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg)
70 lwkt_gettoken(&ilock, &lock->rw_token);
71 gen = lock->rw_wait.wa_gen;
72 while (lock->rw_owner != curthread) {
73 if (lock->rw_owner == NULL && lock->rw_count == 0) {
74 lock->rw_owner = curthread;
78 lwkt_block(&lock->rw_wait, wmesg, &gen);
82 lwkt_reltoken(&ilock);
86 lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg)
91 lwkt_gettoken(&ilock, &lock->rw_token);
92 gen = lock->rw_wait.wa_gen;
93 while (lock->rw_owner != NULL) {
95 lwkt_block(&lock->rw_wait, wmesg, &gen);
99 lwkt_reltoken(&ilock);
103 lwkt_exunlock(lwkt_rwlock_t lock)
107 lwkt_gettoken(&ilock, &lock->rw_token);
108 KASSERT(lock->rw_owner != NULL, ("lwkt_exunlock: shared lock"));
109 KASSERT(lock->rw_owner == curthread, ("lwkt_exunlock: not owner"));
110 if (--lock->rw_count == 0) {
111 lock->rw_owner = NULL;
112 if (lock->rw_requests)
113 lwkt_signal(&lock->rw_wait, 1);
115 lwkt_reltoken(&ilock);
119 lwkt_shunlock(lwkt_rwlock_t lock)
123 lwkt_gettoken(&ilock, &lock->rw_token);
124 KASSERT(lock->rw_owner == NULL, ("lwkt_shunlock: exclusive lock"));
125 if (--lock->rw_count == 0) {
126 if (lock->rw_requests)
127 lwkt_signal(&lock->rw_wait, 1);
129 lwkt_reltoken(&ilock);