Home | History | Annotate | Line # | Download | only in linux
completion.h revision 1.3.2.2
      1  1.3.2.2  rmind /*	$NetBSD: completion.h,v 1.3.2.2 2014/05/18 17:45:53 rmind Exp $	*/
      2  1.3.2.2  rmind 
      3  1.3.2.2  rmind /*-
      4  1.3.2.2  rmind  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.3.2.2  rmind  * All rights reserved.
      6  1.3.2.2  rmind  *
      7  1.3.2.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.2.2  rmind  * by Taylor R. Campbell.
      9  1.3.2.2  rmind  *
     10  1.3.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     11  1.3.2.2  rmind  * modification, are permitted provided that the following conditions
     12  1.3.2.2  rmind  * are met:
     13  1.3.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.3.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.3.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     18  1.3.2.2  rmind  *
     19  1.3.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.2.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.2.2  rmind  */
     31  1.3.2.2  rmind 
     32  1.3.2.2  rmind #ifndef _LINUX_COMPLETION_H_
     33  1.3.2.2  rmind #define _LINUX_COMPLETION_H_
     34  1.3.2.2  rmind 
     35  1.3.2.2  rmind #include <sys/types.h>
     36  1.3.2.2  rmind #include <sys/condvar.h>
     37  1.3.2.2  rmind #include <sys/mutex.h>
     38  1.3.2.2  rmind 
     39  1.3.2.2  rmind #include <machine/limits.h>
     40  1.3.2.2  rmind 
     41  1.3.2.2  rmind #include <linux/errno.h>
     42  1.3.2.2  rmind 
     43  1.3.2.2  rmind struct completion {
     44  1.3.2.2  rmind 	kmutex_t	c_lock;
     45  1.3.2.2  rmind 	kcondvar_t	c_cv;
     46  1.3.2.2  rmind 
     47  1.3.2.2  rmind 	/*
     48  1.3.2.2  rmind 	 * c_done is either
     49  1.3.2.2  rmind 	 *
     50  1.3.2.2  rmind 	 *   . -1, meaning it's open season and we're done for good and
     51  1.3.2.2  rmind 	 *     nobody need wait any more;
     52  1.3.2.2  rmind 	 *
     53  1.3.2.2  rmind 	 *   . 0, meaning nothing is done, so waiters must block; or
     54  1.3.2.2  rmind 	 *
     55  1.3.2.2  rmind 	 *   . a positive integer, meaning that many waiters can
     56  1.3.2.2  rmind 	 *     proceed before further waiters must block.
     57  1.3.2.2  rmind 	 *
     58  1.3.2.2  rmind 	 * Negative values other than -1 are not allowed.
     59  1.3.2.2  rmind 	 */
     60  1.3.2.2  rmind 	int		c_done;
     61  1.3.2.2  rmind };
     62  1.3.2.2  rmind 
     63  1.3.2.2  rmind /*
     64  1.3.2.2  rmind  * Initialize a new completion object.
     65  1.3.2.2  rmind  */
     66  1.3.2.2  rmind static inline void
     67  1.3.2.2  rmind init_completion(struct completion *completion)
     68  1.3.2.2  rmind {
     69  1.3.2.2  rmind 
     70  1.3.2.2  rmind 	mutex_init(&completion->c_lock, MUTEX_DEFAULT, IPL_VM);
     71  1.3.2.2  rmind 	cv_init(&completion->c_cv, "lnxcmplt");
     72  1.3.2.2  rmind 	completion->c_done = 0;
     73  1.3.2.2  rmind }
     74  1.3.2.2  rmind 
     75  1.3.2.2  rmind /*
     76  1.3.2.2  rmind  * Destroy a completion object.
     77  1.3.2.2  rmind  */
     78  1.3.2.2  rmind static inline void
     79  1.3.2.2  rmind destroy_completion(struct completion *completion)
     80  1.3.2.2  rmind {
     81  1.3.2.2  rmind 	KASSERT(!cv_has_waiters(&completion->c_cv));
     82  1.3.2.2  rmind 	cv_destroy(&completion->c_cv);
     83  1.3.2.2  rmind 	mutex_destroy(&completion->c_lock);
     84  1.3.2.2  rmind }
     85  1.3.2.2  rmind 
     86  1.3.2.2  rmind /*
     87  1.3.2.2  rmind  * Notify one waiter of completion, but not any future ones.
     88  1.3.2.2  rmind  */
     89  1.3.2.2  rmind static inline void
     90  1.3.2.2  rmind complete(struct completion *completion)
     91  1.3.2.2  rmind {
     92  1.3.2.2  rmind 
     93  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
     94  1.3.2.2  rmind 
     95  1.3.2.2  rmind 	/* If it's not open season, wake one waiter.  */
     96  1.3.2.2  rmind 	if (completion->c_done >= 0) {
     97  1.3.2.2  rmind 		KASSERT(completion->c_done < INT_MAX); /* XXX check */
     98  1.3.2.2  rmind 		completion->c_done++;
     99  1.3.2.2  rmind 		cv_signal(&completion->c_cv);
    100  1.3.2.2  rmind 	} else {
    101  1.3.2.2  rmind 		KASSERT(completion->c_done == -1);
    102  1.3.2.2  rmind 	}
    103  1.3.2.2  rmind 
    104  1.3.2.2  rmind 	mutex_exit(&completion->c_lock);
    105  1.3.2.2  rmind }
    106  1.3.2.2  rmind 
    107  1.3.2.2  rmind /*
    108  1.3.2.2  rmind  * Notify all waiters, present and future (until INIT_COMPLETION), of
    109  1.3.2.2  rmind  * completion.
    110  1.3.2.2  rmind  */
    111  1.3.2.2  rmind static inline void
    112  1.3.2.2  rmind complete_all(struct completion *completion)
    113  1.3.2.2  rmind {
    114  1.3.2.2  rmind 
    115  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
    116  1.3.2.2  rmind 
    117  1.3.2.2  rmind 	/* If it's not open season, make it open season and wake everyone.  */
    118  1.3.2.2  rmind 	if (completion->c_done >= 0) {
    119  1.3.2.2  rmind 		completion->c_done = -1;
    120  1.3.2.2  rmind 		cv_broadcast(&completion->c_cv);
    121  1.3.2.2  rmind 	} else {
    122  1.3.2.2  rmind 		KASSERT(completion->c_done == -1);
    123  1.3.2.2  rmind 	}
    124  1.3.2.2  rmind 
    125  1.3.2.2  rmind 	mutex_exit(&completion->c_lock);
    126  1.3.2.2  rmind }
    127  1.3.2.2  rmind 
    128  1.3.2.2  rmind /*
    129  1.3.2.2  rmind  * Reverse the effect of complete_all so that subsequent waiters block
    130  1.3.2.2  rmind  * until someone calls complete or complete_all.
    131  1.3.2.2  rmind  *
    132  1.3.2.2  rmind  * This operation is very different from its lowercase counterpart.
    133  1.3.2.2  rmind  *
    134  1.3.2.2  rmind  * For some reason this works on the completion object itself, not on a
    135  1.3.2.2  rmind  * pointer thereto, so it must be a macro.
    136  1.3.2.2  rmind  */
    137  1.3.2.2  rmind #define	INIT_COMPLETION(COMPLETION)	INIT_COMPLETION_blorp(&(COMPLETION))
    138  1.3.2.2  rmind 
    139  1.3.2.2  rmind static inline void
    140  1.3.2.2  rmind INIT_COMPLETION_blorp(struct completion *completion)
    141  1.3.2.2  rmind {
    142  1.3.2.2  rmind 
    143  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
    144  1.3.2.2  rmind 	completion->c_done = 0;
    145  1.3.2.2  rmind 	/* No notify -- waiters are interested only in nonzero values.  */
    146  1.3.2.2  rmind 	mutex_exit(&completion->c_lock);
    147  1.3.2.2  rmind }
    148  1.3.2.2  rmind 
    149  1.3.2.2  rmind static inline void
    150  1.3.2.2  rmind _completion_claim(struct completion *completion)
    151  1.3.2.2  rmind {
    152  1.3.2.2  rmind 
    153  1.3.2.2  rmind 	KASSERT(mutex_owned(&completion->c_lock));
    154  1.3.2.2  rmind 	KASSERT(completion->c_done != 0);
    155  1.3.2.2  rmind 	if (completion->c_done > 0)
    156  1.3.2.2  rmind 		completion->c_done--;
    157  1.3.2.2  rmind 	else
    158  1.3.2.2  rmind 		KASSERT(completion->c_done == -1);
    159  1.3.2.2  rmind }
    160  1.3.2.2  rmind 
    161  1.3.2.2  rmind /*
    162  1.3.2.2  rmind  * Wait interruptibly with a timeout for someone to call complete or
    163  1.3.2.2  rmind  * complete_all.
    164  1.3.2.2  rmind  */
    165  1.3.2.2  rmind static inline int
    166  1.3.2.2  rmind wait_for_completion_interruptible_timeout(struct completion *completion,
    167  1.3.2.2  rmind     unsigned long ticks)
    168  1.3.2.2  rmind {
    169  1.3.2.2  rmind 	/* XXX Arithmetic overflow...?  */
    170  1.3.2.2  rmind 	unsigned int start = hardclock_ticks, now;
    171  1.3.2.2  rmind 	int error;
    172  1.3.2.2  rmind 
    173  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
    174  1.3.2.2  rmind 
    175  1.3.2.2  rmind 	/* Wait until c_done is nonzero.  */
    176  1.3.2.2  rmind 	while (completion->c_done == 0) {
    177  1.3.2.2  rmind 		error = cv_timedwait_sig(&completion->c_cv,
    178  1.3.2.2  rmind 		    &completion->c_lock, ticks);
    179  1.3.2.2  rmind 		if (error)
    180  1.3.2.2  rmind 			goto out;
    181  1.3.2.2  rmind 		now = hardclock_ticks;
    182  1.3.2.2  rmind 		if (ticks < (now - start)) {
    183  1.3.2.2  rmind 			error = EWOULDBLOCK;
    184  1.3.2.2  rmind 			goto out;
    185  1.3.2.2  rmind 		}
    186  1.3.2.2  rmind 		ticks -= (now - start);
    187  1.3.2.2  rmind 		start = now;
    188  1.3.2.2  rmind 	}
    189  1.3.2.2  rmind 
    190  1.3.2.2  rmind 	/* Success!  */
    191  1.3.2.2  rmind 	_completion_claim(completion);
    192  1.3.2.2  rmind 	error = 0;
    193  1.3.2.2  rmind 
    194  1.3.2.2  rmind out:	mutex_exit(&completion->c_lock);
    195  1.3.2.2  rmind 	if (error == EWOULDBLOCK) {
    196  1.3.2.2  rmind 		return 0;
    197  1.3.2.2  rmind 	} else if ((error == EINTR) || (error == ERESTART)) {
    198  1.3.2.2  rmind 		return -ERESTARTSYS;
    199  1.3.2.2  rmind 	} else {
    200  1.3.2.2  rmind 		KASSERTMSG((error == 0), "error = %d", error);
    201  1.3.2.2  rmind 		return ticks;
    202  1.3.2.2  rmind 	}
    203  1.3.2.2  rmind }
    204  1.3.2.2  rmind 
    205  1.3.2.2  rmind /*
    206  1.3.2.2  rmind  * Wait interruptibly for someone to call complete or complete_all.
    207  1.3.2.2  rmind  */
    208  1.3.2.2  rmind static inline int
    209  1.3.2.2  rmind wait_for_completion_interruptible(struct completion *completion)
    210  1.3.2.2  rmind {
    211  1.3.2.2  rmind 	int error;
    212  1.3.2.2  rmind 
    213  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
    214  1.3.2.2  rmind 
    215  1.3.2.2  rmind 	/* Wait until c_done is nonzero.  */
    216  1.3.2.2  rmind 	while (completion->c_done == 0) {
    217  1.3.2.2  rmind 		error = cv_wait_sig(&completion->c_cv, &completion->c_lock);
    218  1.3.2.2  rmind 		if (error)
    219  1.3.2.2  rmind 			goto out;
    220  1.3.2.2  rmind 	}
    221  1.3.2.2  rmind 
    222  1.3.2.2  rmind 	/* Success!  */
    223  1.3.2.2  rmind 	_completion_claim(completion);
    224  1.3.2.2  rmind 	error = 0;
    225  1.3.2.2  rmind 
    226  1.3.2.2  rmind out:	mutex_exit(&completion->c_lock);
    227  1.3.2.2  rmind 	if ((error == EINTR) || (error == ERESTART))
    228  1.3.2.2  rmind 		error = -ERESTARTSYS;
    229  1.3.2.2  rmind 	return error;
    230  1.3.2.2  rmind }
    231  1.3.2.2  rmind 
    232  1.3.2.2  rmind /*
    233  1.3.2.2  rmind  * Wait uninterruptibly, except by SIGKILL, for someone to call
    234  1.3.2.2  rmind  * complete or complete_all.
    235  1.3.2.2  rmind  *
    236  1.3.2.2  rmind  * XXX In this implementation, any signal will actually wake us, not
    237  1.3.2.2  rmind  * just SIGKILL.
    238  1.3.2.2  rmind  */
    239  1.3.2.2  rmind static inline int
    240  1.3.2.2  rmind wait_for_completion_killable(struct completion *completion)
    241  1.3.2.2  rmind {
    242  1.3.2.2  rmind 
    243  1.3.2.2  rmind 	return wait_for_completion_interruptible(completion);
    244  1.3.2.2  rmind }
    245  1.3.2.2  rmind 
    246  1.3.2.2  rmind /*
    247  1.3.2.2  rmind  * Try to claim a completion immediately.  Return true on success, false
    248  1.3.2.2  rmind  * if it would block.
    249  1.3.2.2  rmind  */
    250  1.3.2.2  rmind static inline bool
    251  1.3.2.2  rmind try_wait_for_completion(struct completion *completion)
    252  1.3.2.2  rmind {
    253  1.3.2.2  rmind 	bool ok;
    254  1.3.2.2  rmind 
    255  1.3.2.2  rmind 	mutex_enter(&completion->c_lock);
    256  1.3.2.2  rmind 	if (completion->c_done == 0) {
    257  1.3.2.2  rmind 		ok = false;
    258  1.3.2.2  rmind 	} else {
    259  1.3.2.2  rmind 		_completion_claim(completion);
    260  1.3.2.2  rmind 		ok = true;
    261  1.3.2.2  rmind 	}
    262  1.3.2.2  rmind 	mutex_exit(&completion->c_lock);
    263  1.3.2.2  rmind 
    264  1.3.2.2  rmind 	return ok;
    265  1.3.2.2  rmind }
    266  1.3.2.2  rmind 
    267  1.3.2.2  rmind #endif	/* _LINUX_COMPLETION_H_ */
    268