1236317Skib/*-
2236317Skib * Copyright (c) 2009 Konstantin Belousov <kib@FreeBSD.org>
3236317Skib * All rights reserved.
4236317Skib *
5236317Skib * Redistribution and use in source and binary forms, with or without
6236317Skib * modification, are permitted provided that the following conditions
7236317Skib * are met:
8236317Skib * 1. Redistributions of source code must retain the above copyright
9236317Skib *    notice unmodified, this list of conditions, and the following
10236317Skib *    disclaimer.
11236317Skib * 2. Redistributions in binary form must reproduce the above copyright
12236317Skib *    notice, this list of conditions and the following disclaimer in the
13236317Skib *    documentation and/or other materials provided with the distribution.
14236317Skib *
15236317Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16236317Skib * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17236317Skib * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18236317Skib * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19236317Skib * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20236317Skib * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21236317Skib * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22236317Skib * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23236317Skib * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24236317Skib * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25236317Skib *
26236317Skib * $FreeBSD$
27236317Skib */
28236317Skib
29236317Skib#ifndef	_SYS_RANGELOCK_H
30236317Skib#define	_SYS_RANGELOCK_H
31236317Skib
32236317Skib#include <sys/queue.h>
33236317Skib
34236317Skib#define	RL_LOCK_READ		0x0001
35236317Skib#define	RL_LOCK_WRITE		0x0002
36236317Skib#define	RL_LOCK_TYPE_MASK	0x0003
37236317Skib#define	RL_LOCK_GRANTED		0x0004
38236317Skib
39236317Skibstruct rl_q_entry;
40236317Skib
41236317Skib/*
42236317Skib * The structure representing the range lock.  Caller may request
43236317Skib * read or write access to the range of bytes. Access is granted if
44236317Skib * all existing lock owners are compatible with the request. Two lock
45236317Skib * owners are compatible if their ranges do not overlap, or both
46236317Skib * owners are for read.
47236317Skib *
48236317Skib * Access to the structure itself is synchronized with the externally
49236317Skib * supplied mutex.
50236317Skib *
51254380Scperciva * rl_waiters is the queue containing in order (a) granted write lock
52254380Scperciva * requests, (b) granted read lock requests, and (c) in order of arrival,
53254380Scperciva * lock requests which cannot be granted yet.
54254380Scperciva *
55236317Skib * rl_currdep is the first lock request that cannot be granted now due
56254380Scperciva * to the preceding requests conflicting with it (i.e., it points to
57254380Scperciva * position (c) in the list above).
58236317Skib */
59236317Skibstruct rangelock {
60236317Skib	TAILQ_HEAD(, rl_q_entry) rl_waiters;
61236317Skib	struct rl_q_entry	*rl_currdep;
62236317Skib};
63236317Skib
64236317Skib#ifdef _KERNEL
65236317Skib
66236317Skibstruct mtx;
67236317Skib
68236317Skibvoid	 rangelock_init(struct rangelock *lock);
69236317Skibvoid	 rangelock_destroy(struct rangelock *lock);
70236317Skibvoid	 rangelock_unlock(struct rangelock *lock, void *cookie,
71236317Skib	    struct mtx *ilk);
72236317Skibvoid	*rangelock_unlock_range(struct rangelock *lock, void *cookie,
73236317Skib	    off_t start, off_t end, struct mtx *ilk);
74236317Skibvoid	*rangelock_rlock(struct rangelock *lock, off_t start, off_t end,
75236317Skib	    struct mtx *ilk);
76236317Skibvoid	*rangelock_wlock(struct rangelock *lock, off_t start, off_t end,
77236317Skib	    struct mtx *ilk);
78236317Skibvoid	 rlqentry_free(struct rl_q_entry *rlqe);
79236317Skib
80236317Skib#endif	/* _KERNEL */
81236317Skib
82236317Skib#endif	/* _SYS_RANGELOCK_H */
83