xref: /trueos/lib/libkse/sys/lock.h (revision 420be2d1af153983145ce323e4e2d26cc85e0505)
1 /*
2  * Copyright (c) 2001, 2003 Daniel Eischen <deischen@freebsd.org>.
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 AUTHORS 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  * $FreeBSD$
27  */
28 
29 #ifndef _LOCK_H_
30 #define	_LOCK_H_
31 
32 struct lockreq;
33 struct lockuser;
34 struct lock;
35 
36 enum lock_type {
37 	LCK_DEFAULT	= 0x0000,	/* default is FIFO spin locks */
38 	LCK_PRIORITY	= 0x0001,
39 	LCK_ADAPTIVE 	= 0x0002	/* call user-supplied handlers */
40 };
41 
42 typedef void lock_handler_t(struct lock *, struct lockuser *);
43 
44 struct lock {
45 	struct lockreq	*l_head;
46 	struct lockreq	*l_tail;	/* only used for priority locks */
47 	enum lock_type	l_type;
48 	lock_handler_t	*l_wait;	/* only used for adaptive locks */
49 	lock_handler_t	*l_wakeup;	/* only used for adaptive locks */
50 };
51 
52 /* Try to make this >= CACHELINESIZE */
53 struct lockreq {
54 	struct lockuser	*lr_watcher;	/* only used for priority locks */
55 	struct lockuser	*lr_owner;	/* only used for priority locks */
56 	volatile int	lr_locked;	/* lock granted = 0, busy otherwise */
57 	volatile int	lr_active;	/* non-zero if the lock is last lock for thread */
58 };
59 
60 struct lockuser {
61 	struct lockreq	*lu_myreq;	/* request to give up/trade */
62 	struct lockreq	*lu_watchreq;	/* watch this request */
63 	int		lu_priority;	/* only used for priority locks */
64 	void		*lu_private1;	/* private{1,2} are initialized to */
65 	void		*lu_private2;	/*   NULL and can be used by caller */
66 #define	lu_private	lu_private1
67 };
68 
69 #define	_LCK_INITIALIZER(lck_req)	{ &lck_req, NULL, LCK_DEFAULT, \
70 					  NULL, NULL }
71 #define	_LCK_REQUEST_INITIALIZER	{ 0, NULL, NULL, 0 }
72 
73 #define	_LCK_BUSY(lu)			((lu)->lu_watchreq->lr_locked != 0)
74 #define	_LCK_ACTIVE(lu)			((lu)->lu_watchreq->lr_active != 0)
75 #define	_LCK_GRANTED(lu)		((lu)->lu_watchreq->lr_locked == 3)
76 
77 #define	_LCK_SET_PRIVATE(lu, p)		(lu)->lu_private = (void *)(p)
78 #define	_LCK_GET_PRIVATE(lu)		(lu)->lu_private
79 #define	_LCK_SET_PRIVATE2(lu, p)	(lu)->lu_private2 = (void *)(p)
80 #define	_LCK_GET_PRIVATE2(lu)		(lu)->lu_private2
81 
82 void	_lock_acquire(struct lock *, struct lockuser *, int);
83 void	_lock_destroy(struct lock *);
84 void	_lock_grant(struct lock *, struct lockuser *);
85 int	_lock_init(struct lock *, enum lock_type,
86 	    lock_handler_t *, lock_handler_t *, void *(size_t, size_t));
87 int	_lock_reinit(struct lock *, enum lock_type,
88 	    lock_handler_t *, lock_handler_t *);
89 void	_lock_release(struct lock *, struct lockuser *);
90 int	_lockuser_init(struct lockuser *lu, void *priv);
91 void	_lockuser_destroy(struct lockuser *lu);
92 int	_lockuser_reinit(struct lockuser *lu, void *priv);
93 void	_lockuser_setactive(struct lockuser *lu, int active);
94 
95 #endif
96