1 include/linux/dynlocks.h | 33 ++++++++++
3 lib/dynlocks.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++
4 3 files changed, 187 insertions(+), 2 deletions(-)
6 Index: linux-2.4.24/include/linux/dynlocks.h
7 ===================================================================
8 --- linux-2.4.24.orig/include/linux/dynlocks.h 2003-01-30 13:24:37.000000000 +0300
9 +++ linux-2.4.24/include/linux/dynlocks.h 2004-07-18 11:09:33.000000000 +0400
11 +#ifndef _LINUX_DYNLOCKS_H
12 +#define _LINUX_DYNLOCKS_H
14 +#include <linux/list.h>
15 +#include <linux/wait.h>
17 +#define DYNLOCK_MAGIC 0xd19a10c
18 +#define DYNLOCK_MAGIC2 0xd1956ee
22 +struct dynlock_member {
24 + struct list_head dl_list;
25 + unsigned long dl_value; /* lock value */
26 + int dl_refcount; /* number of users */
29 + int dl_pid; /* holder of the lock */
30 + wait_queue_head_t dl_wait;
36 + * - lock to protect this list
39 +#define DYNLOCK_LIST_MAGIC 0x11ee91e6
43 + struct list_head dl_list;
44 + spinlock_t dl_list_lock;
47 +void dynlock_init(struct dynlock *dl);
48 +void *dynlock_lock(struct dynlock *dl, unsigned long value, int rw, int gfp);
49 +void dynlock_unlock(struct dynlock *dl, void *lock);
54 Index: linux-2.4.24/lib/dynlocks.c
55 ===================================================================
56 --- linux-2.4.24.orig/lib/dynlocks.c 2003-01-30 13:24:37.000000000 +0300
57 +++ linux-2.4.24/lib/dynlocks.c 2004-07-18 11:23:01.000000000 +0400
62 + * struct dynlock is lockspace
63 + * one may request lock (exclusive or shared) for some value
68 +#include <linux/dynlocks.h>
69 +#include <linux/module.h>
70 +#include <linux/slab.h>
71 +#include <linux/sched.h>
72 +#include <linux/init.h>
74 +static kmem_cache_t * dynlock_cachep = NULL;
76 +void __init dynlock_cache_init(void)
78 + printk(KERN_INFO "init dynlocks cache\n");
79 + dynlock_cachep = kmem_cache_create("dynlock_cache",
80 + sizeof(struct dynlock_member),
84 + if (dynlock_cachep == NULL)
85 + panic("Can't create dynlock cache");
91 + * initialize lockspace
94 +void dynlock_init(struct dynlock *dl)
96 + spin_lock_init(&dl->dl_list_lock);
97 + INIT_LIST_HEAD(&dl->dl_list);
98 + dl->dl_magic = DYNLOCK_LIST_MAGIC;
104 + * acquires lock (exclusive or shared) in specified lockspace
105 + * each lock in lockspace is allocated separately, so user have
106 + * to specify GFP flags.
107 + * routine returns pointer to lock. this pointer is intended to
108 + * be passed to dynlock_unlock
111 +void *dynlock_lock(struct dynlock *dl, unsigned long value, int rw, int gfp)
113 + struct dynlock_member *nhl = NULL;
114 + struct dynlock_member *hl;
115 + struct list_head *cur;
118 + BUG_ON(dl == NULL);
119 + BUG_ON(dl->dl_magic != DYNLOCK_LIST_MAGIC);
121 + /* find requested lock in lockspace */
122 + spin_lock(&dl->dl_list_lock);
123 + BUG_ON(dl->dl_list.next == NULL);
124 + BUG_ON(dl->dl_list.prev == NULL);
125 + list_for_each(cur, &dl->dl_list) {
126 + BUG_ON(cur->next == NULL);
127 + BUG_ON(cur->prev == NULL);
128 + hl = list_entry(cur, struct dynlock_member, dl_list);
129 + BUG_ON(hl->dl_magic != DYNLOCK_MAGIC);
130 + if (hl->dl_value == value) {
131 + /* lock is found */
133 + /* someone else just allocated
134 + * lock we didn't find and just created
135 + * so, we drop our lock
137 + kmem_cache_free(dynlock_cachep, nhl);
145 + /* lock not found */
147 + /* we already have allocated lock. use it */
150 + list_add(&hl->dl_list, &dl->dl_list);
153 + spin_unlock(&dl->dl_list_lock);
155 + /* lock not found and we haven't allocated lock yet. allocate it */
156 + nhl = kmem_cache_alloc(dynlock_cachep, gfp);
159 + nhl->dl_refcount = 1;
160 + nhl->dl_value = value;
161 + nhl->dl_readers = 0;
162 + nhl->dl_writers = 0;
163 + nhl->dl_magic = DYNLOCK_MAGIC;
164 + init_waitqueue_head(&nhl->dl_wait);
166 + /* while lock is being allocated, someone else may allocate it
167 + * and put onto to list. check this situation
173 + /* exclusive lock: user don't want to share lock at all
174 + * NOTE: one process may take the same lock several times
175 + * this functionaly is useful for rename operations */
176 + while ((hl->dl_writers && hl->dl_pid != current->pid) ||
178 + spin_unlock(&dl->dl_list_lock);
179 + wait_event(hl->dl_wait,
180 + hl->dl_writers == 0 && hl->dl_readers == 0);
181 + spin_lock(&dl->dl_list_lock);
185 + /* shared lock: user do not want to share lock with writer */
186 + while (hl->dl_writers) {
187 + spin_unlock(&dl->dl_list_lock);
188 + wait_event(hl->dl_wait, hl->dl_writers == 0);
189 + spin_lock(&dl->dl_list_lock);
193 + hl->dl_pid = current->pid;
194 + spin_unlock(&dl->dl_list_lock);
203 + * user have to specify lockspace (dl) and pointer to lock structure
204 + * returned by dynlock_lock()
207 +void dynlock_unlock(struct dynlock *dl, void *lock)
209 + struct dynlock_member *hl = lock;
212 + BUG_ON(dl == NULL);
213 + BUG_ON(hl == NULL);
214 + BUG_ON(dl->dl_magic != DYNLOCK_LIST_MAGIC);
215 + BUG_ON(hl->dl_magic != DYNLOCK_MAGIC);
216 + BUG_ON(current->pid != hl->dl_pid);
218 + spin_lock(&dl->dl_list_lock);
219 + if (hl->dl_writers) {
220 + BUG_ON(hl->dl_readers > 0 || hl->dl_readers < 0);
222 + if (hl->dl_writers == 0)
224 + } else if (hl->dl_readers) {
226 + if (hl->dl_readers == 0)
233 + wake_up(&hl->dl_wait);
235 + if (--(hl->dl_refcount) == 0) {
236 + hl->dl_magic = DYNLOCK_MAGIC2;
237 + list_del(&hl->dl_list);
238 + kmem_cache_free(dynlock_cachep, hl);
240 + spin_unlock(&dl->dl_list_lock);
243 +EXPORT_SYMBOL(dynlock_init);
244 +EXPORT_SYMBOL(dynlock_lock);
245 +EXPORT_SYMBOL(dynlock_unlock);
247 Index: linux-2.4.24/lib/Makefile
248 ===================================================================
249 --- linux-2.4.24.orig/lib/Makefile 2004-06-24 09:06:32.000000000 +0400
250 +++ linux-2.4.24/lib/Makefile 2004-07-16 15:54:06.000000000 +0400
254 export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o \
255 - rbtree.o crc32.o firmware_class.o
256 + rbtree.o crc32.o firmware_class.o dynlocks.o
258 obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o \
259 - bust_spinlocks.o rbtree.o dump_stack.o
260 + bust_spinlocks.o rbtree.o dump_stack.o dynlocks.o
262 obj-$(CONFIG_FW_LOADER) += firmware_class.o
263 obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
264 Index: linux-2.4.24/fs/dcache.c
265 ===================================================================
266 --- linux-2.4.24.orig/fs/dcache.c 2004-07-16 12:35:54.000000000 +0400
267 +++ linux-2.4.24/fs/dcache.c 2004-07-16 15:54:06.000000000 +0400
268 @@ -1274,6 +1274,7 @@
269 extern void bdev_cache_init(void);
270 extern void cdev_cache_init(void);
271 extern void iobuf_cache_init(void);
272 +extern void dynlock_cache_init(void);
274 void __init vfs_caches_init(unsigned long mempages)
276 @@ -1310,4 +1311,5 @@
280 + dynlock_cache_init();