s/ich_arg/ich_desc/
[dragonfly.git] / sys / kern / lwkt_rwlock.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * Implements simple shared/exclusive locks using LWKT. 
35  *
36  * $DragonFly: src/sys/kern/Attic/lwkt_rwlock.c,v 1.6 2004/07/16 05:51:10 dillon Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/rtprio.h>
44 #include <sys/queue.h>
45
46 /*
47  * NOTE! called from low level boot, we cannot do anything fancy.
48  */
49 void
50 lwkt_rwlock_init(lwkt_rwlock_t lock)
51 {
52     lwkt_wait_init(&lock->rw_wait);
53     lock->rw_owner = NULL;
54     lock->rw_count = 0;
55     lock->rw_requests = 0;
56 }
57
58 void
59 lwkt_rwlock_uninit(lwkt_rwlock_t lock)
60 {
61     /* empty */
62 }
63
64 void
65 lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg)
66 {
67     lwkt_tokref ilock;
68     int gen;
69
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;
75             break;
76         }
77         ++lock->rw_requests;
78         lwkt_block(&lock->rw_wait, wmesg, &gen);
79         --lock->rw_requests;
80     }
81     ++lock->rw_count;
82     lwkt_reltoken(&ilock);
83 }
84
85 void
86 lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg)
87 {
88     lwkt_tokref ilock;
89     int gen;
90
91     lwkt_gettoken(&ilock, &lock->rw_token);
92     gen = lock->rw_wait.wa_gen;
93     while (lock->rw_owner != NULL) {
94         ++lock->rw_requests;
95         lwkt_block(&lock->rw_wait, wmesg, &gen);
96         --lock->rw_requests;
97     }
98     ++lock->rw_count;
99     lwkt_reltoken(&ilock);
100 }
101
102 void
103 lwkt_exunlock(lwkt_rwlock_t lock)
104 {
105     lwkt_tokref ilock;
106
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);
114     }
115     lwkt_reltoken(&ilock);
116 }
117
118 void
119 lwkt_shunlock(lwkt_rwlock_t lock)
120 {
121     lwkt_tokref ilock;
122
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);
128     }
129     lwkt_reltoken(&ilock);
130 }
131