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