Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_COMPLETION_H
0003 #define __LINUX_COMPLETION_H
0004 
0005 /*
0006  * (C) Copyright 2001 Linus Torvalds
0007  *
0008  * Atomic wait-for-completion handler data structures.
0009  * See kernel/sched/completion.c for details.
0010  */
0011 
0012 #include <linux/swait.h>
0013 
0014 /*
0015  * struct completion - structure used to maintain state for a "completion"
0016  *
0017  * This is the opaque structure used to maintain the state for a "completion".
0018  * Completions currently use a FIFO to queue threads that have to wait for
0019  * the "completion" event.
0020  *
0021  * See also:  complete(), wait_for_completion() (and friends _timeout,
0022  * _interruptible, _interruptible_timeout, and _killable), init_completion(),
0023  * reinit_completion(), and macros DECLARE_COMPLETION(),
0024  * DECLARE_COMPLETION_ONSTACK().
0025  */
0026 struct completion {
0027     unsigned int done;
0028     struct swait_queue_head wait;
0029 };
0030 
0031 #define init_completion_map(x, m) init_completion(x)
0032 static inline void complete_acquire(struct completion *x) {}
0033 static inline void complete_release(struct completion *x) {}
0034 
0035 #define COMPLETION_INITIALIZER(work) \
0036     { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
0037 
0038 #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
0039     (*({ init_completion_map(&(work), &(map)); &(work); }))
0040 
0041 #define COMPLETION_INITIALIZER_ONSTACK(work) \
0042     (*({ init_completion(&work); &work; }))
0043 
0044 /**
0045  * DECLARE_COMPLETION - declare and initialize a completion structure
0046  * @work:  identifier for the completion structure
0047  *
0048  * This macro declares and initializes a completion structure. Generally used
0049  * for static declarations. You should use the _ONSTACK variant for automatic
0050  * variables.
0051  */
0052 #define DECLARE_COMPLETION(work) \
0053     struct completion work = COMPLETION_INITIALIZER(work)
0054 
0055 /*
0056  * Lockdep needs to run a non-constant initializer for on-stack
0057  * completions - so we use the _ONSTACK() variant for those that
0058  * are on the kernel stack:
0059  */
0060 /**
0061  * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
0062  * @work:  identifier for the completion structure
0063  *
0064  * This macro declares and initializes a completion structure on the kernel
0065  * stack.
0066  */
0067 #ifdef CONFIG_LOCKDEP
0068 # define DECLARE_COMPLETION_ONSTACK(work) \
0069     struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
0070 # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
0071     struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
0072 #else
0073 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
0074 # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
0075 #endif
0076 
0077 /**
0078  * init_completion - Initialize a dynamically allocated completion
0079  * @x:  pointer to completion structure that is to be initialized
0080  *
0081  * This inline function will initialize a dynamically created completion
0082  * structure.
0083  */
0084 static inline void init_completion(struct completion *x)
0085 {
0086     x->done = 0;
0087     init_swait_queue_head(&x->wait);
0088 }
0089 
0090 /**
0091  * reinit_completion - reinitialize a completion structure
0092  * @x:  pointer to completion structure that is to be reinitialized
0093  *
0094  * This inline function should be used to reinitialize a completion structure so it can
0095  * be reused. This is especially important after complete_all() is used.
0096  */
0097 static inline void reinit_completion(struct completion *x)
0098 {
0099     x->done = 0;
0100 }
0101 
0102 extern void wait_for_completion(struct completion *);
0103 extern void wait_for_completion_io(struct completion *);
0104 extern int wait_for_completion_interruptible(struct completion *x);
0105 extern int wait_for_completion_killable(struct completion *x);
0106 extern unsigned long wait_for_completion_timeout(struct completion *x,
0107                            unsigned long timeout);
0108 extern unsigned long wait_for_completion_io_timeout(struct completion *x,
0109                             unsigned long timeout);
0110 extern long wait_for_completion_interruptible_timeout(
0111     struct completion *x, unsigned long timeout);
0112 extern long wait_for_completion_killable_timeout(
0113     struct completion *x, unsigned long timeout);
0114 extern bool try_wait_for_completion(struct completion *x);
0115 extern bool completion_done(struct completion *x);
0116 
0117 extern void complete(struct completion *);
0118 extern void complete_all(struct completion *);
0119 
0120 #endif