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