1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef _SPL_CONDVAR_H
25 #define	_SPL_CONDVAR_H
26 
27 #include <linux/module.h>
28 #include <sys/kmem.h>
29 #include <sys/mutex.h>
30 #include <sys/callo.h>
31 #include <sys/wait.h>
32 #include <sys/time.h>
33 
34 /*
35  * cv_timedwait() is similar to cv_wait() except that it additionally expects
36  * a timeout value specified in ticks.  When woken by cv_signal() or
37  * cv_broadcast() it returns 1, otherwise when the timeout is reached -1 is
38  * returned.
39  *
40  * cv_timedwait_sig() behaves the same as cv_timedwait() but blocks
41  * interruptibly and can be woken by a signal (EINTR, ERESTART).  When
42  * this occurs 0 is returned.
43  *
44  * cv_timedwait_io() and cv_timedwait_sig_io() are variants of cv_timedwait()
45  * and cv_timedwait_sig() which should be used when waiting for outstanding
46  * IO to complete.  They are responsible for updating the iowait accounting
47  * when this is supported by the platform.
48  *
49  * cv_timedwait_hires() and cv_timedwait_sig_hires() are high resolution
50  * versions of cv_timedwait() and cv_timedwait_sig().  They expect the timeout
51  * to be specified as a hrtime_t allowing for timeouts of less than a tick.
52  *
53  * N.B. The return values differ slightly from the illumos implementation
54  * which returns the time remaining, instead of 1, when woken.  They both
55  * return -1 on timeout. Consumers which need to know the time remaining
56  * are responsible for tracking it themselves.
57  */
58 
59 
60 /*
61  * The kcondvar_t struct is protected by mutex taken externally before
62  * calling any of the wait/signal funs, and passed into the wait funs.
63  */
64 #define	CV_MAGIC			0x346545f4
65 #define	CV_DESTROY			0x346545f5
66 
67 typedef struct {
68 	int cv_magic;
69 	spl_wait_queue_head_t cv_event;
70 	spl_wait_queue_head_t cv_destroy;
71 	atomic_t cv_refs;
72 	atomic_t cv_waiters;
73 	kmutex_t *cv_mutex;
74 } kcondvar_t;
75 
76 typedef enum { CV_DEFAULT = 0, CV_DRIVER } kcv_type_t;
77 
78 extern void __cv_init(kcondvar_t *, char *, kcv_type_t, void *);
79 extern void __cv_destroy(kcondvar_t *);
80 extern void __cv_wait(kcondvar_t *, kmutex_t *);
81 extern void __cv_wait_io(kcondvar_t *, kmutex_t *);
82 extern void __cv_wait_idle(kcondvar_t *, kmutex_t *);
83 extern int __cv_wait_io_sig(kcondvar_t *, kmutex_t *);
84 extern int __cv_wait_sig(kcondvar_t *, kmutex_t *);
85 extern int __cv_timedwait(kcondvar_t *, kmutex_t *, clock_t);
86 extern int __cv_timedwait_io(kcondvar_t *, kmutex_t *, clock_t);
87 extern int __cv_timedwait_sig(kcondvar_t *, kmutex_t *, clock_t);
88 extern int __cv_timedwait_idle(kcondvar_t *, kmutex_t *, clock_t);
89 extern int cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t,
90     hrtime_t res, int flag);
91 extern int cv_timedwait_sig_hires(kcondvar_t *, kmutex_t *, hrtime_t,
92     hrtime_t res, int flag);
93 extern int cv_timedwait_idle_hires(kcondvar_t *, kmutex_t *, hrtime_t,
94     hrtime_t res, int flag);
95 extern void __cv_signal(kcondvar_t *);
96 extern void __cv_broadcast(kcondvar_t *c);
97 
98 #define	cv_init(cvp, name, type, arg)		__cv_init(cvp, name, type, arg)
99 #define	cv_destroy(cvp)				__cv_destroy(cvp)
100 #define	cv_wait(cvp, mp)			__cv_wait(cvp, mp)
101 #define	cv_wait_io(cvp, mp)			__cv_wait_io(cvp, mp)
102 #define	cv_wait_idle(cvp, mp)			__cv_wait_idle(cvp, mp)
103 #define	cv_wait_io_sig(cvp, mp)			__cv_wait_io_sig(cvp, mp)
104 #define	cv_wait_sig(cvp, mp)			__cv_wait_sig(cvp, mp)
105 #define	cv_signal(cvp)				__cv_signal(cvp)
106 #define	cv_broadcast(cvp)			__cv_broadcast(cvp)
107 
108 /*
109  * NB: There is no way to reliably distinguish between having been signalled
110  * and having timed out on Linux. If the client code needs to reliably
111  * distinguish between the two it should use the hires variant.
112  */
113 #define	cv_timedwait(cvp, mp, t)		__cv_timedwait(cvp, mp, t)
114 #define	cv_timedwait_io(cvp, mp, t)		__cv_timedwait_io(cvp, mp, t)
115 #define	cv_timedwait_sig(cvp, mp, t)		__cv_timedwait_sig(cvp, mp, t)
116 #define	cv_timedwait_idle(cvp, mp, t)		__cv_timedwait_idle(cvp, mp, t)
117 
118 
119 #endif /* _SPL_CONDVAR_H */
120