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