008e7f145f533415d6e378155dfa5c2a86bfd90b
[dragonfly.git] / sys / kern / lwkt_rwlock.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * Implements simple shared/exclusive locks using LWKT. 
27  *
28  * $DragonFly: src/sys/kern/Attic/lwkt_rwlock.c,v 1.5 2004/03/01 06:33:17 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/proc.h>
35 #include <sys/rtprio.h>
36 #include <sys/queue.h>
37
38 /*
39  * NOTE! called from low level boot, we cannot do anything fancy.
40  */
41 void
42 lwkt_rwlock_init(lwkt_rwlock_t lock)
43 {
44     lwkt_wait_init(&lock->rw_wait);
45     lock->rw_owner = NULL;
46     lock->rw_count = 0;
47     lock->rw_requests = 0;
48 }
49
50 void
51 lwkt_rwlock_uninit(lwkt_rwlock_t lock)
52 {
53     /* empty */
54 }
55
56 void
57 lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg)
58 {
59     lwkt_tokref ilock;
60     int gen;
61
62     lwkt_gettoken(&ilock, &lock->rw_token);
63     gen = lock->rw_wait.wa_gen;
64     while (lock->rw_owner != curthread) {
65         if (lock->rw_owner == NULL && lock->rw_count == 0) {
66             lock->rw_owner = curthread;
67             break;
68         }
69         ++lock->rw_requests;
70         lwkt_block(&lock->rw_wait, wmesg, &gen);
71         --lock->rw_requests;
72     }
73     ++lock->rw_count;
74     lwkt_reltoken(&ilock);
75 }
76
77 void
78 lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg)
79 {
80     lwkt_tokref ilock;
81     int gen;
82
83     lwkt_gettoken(&ilock, &lock->rw_token);
84     gen = lock->rw_wait.wa_gen;
85     while (lock->rw_owner != NULL) {
86         ++lock->rw_requests;
87         lwkt_block(&lock->rw_wait, wmesg, &gen);
88         --lock->rw_requests;
89     }
90     ++lock->rw_count;
91     lwkt_reltoken(&ilock);
92 }
93
94 void
95 lwkt_exunlock(lwkt_rwlock_t lock)
96 {
97     lwkt_tokref ilock;
98
99     lwkt_gettoken(&ilock, &lock->rw_token);
100     KASSERT(lock->rw_owner != NULL, ("lwkt_exunlock: shared lock"));
101     KASSERT(lock->rw_owner == curthread, ("lwkt_exunlock: not owner"));
102     if (--lock->rw_count == 0) {
103         lock->rw_owner = NULL;
104         if (lock->rw_requests)
105             lwkt_signal(&lock->rw_wait, 1);
106     }
107     lwkt_reltoken(&ilock);
108 }
109
110 void
111 lwkt_shunlock(lwkt_rwlock_t lock)
112 {
113     lwkt_tokref ilock;
114
115     lwkt_gettoken(&ilock, &lock->rw_token);
116     KASSERT(lock->rw_owner == NULL, ("lwkt_shunlock: exclusive lock"));
117     if (--lock->rw_count == 0) {
118         if (lock->rw_requests)
119             lwkt_signal(&lock->rw_wait, 1);
120     }
121     lwkt_reltoken(&ilock);
122 }
123