1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef __LINUX_COMPLETION_H 31da177e4SLinus Torvalds #define __LINUX_COMPLETION_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds /* 61da177e4SLinus Torvalds * (C) Copyright 2001 Linus Torvalds 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Atomic wait-for-completion handler data structures. 9b8a21626SPeter Zijlstra * See kernel/sched/completion.c for details. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 12a5c6234eSThomas Gleixner #include <linux/swait.h> 131da177e4SLinus Torvalds 14ee2f154aSRandy Dunlap /* 1565eb3dc6SKevin Diggs * struct completion - structure used to maintain state for a "completion" 1665eb3dc6SKevin Diggs * 1765eb3dc6SKevin Diggs * This is the opaque structure used to maintain the state for a "completion". 1865eb3dc6SKevin Diggs * Completions currently use a FIFO to queue threads that have to wait for 1965eb3dc6SKevin Diggs * the "completion" event. 2065eb3dc6SKevin Diggs * 2165eb3dc6SKevin Diggs * See also: complete(), wait_for_completion() (and friends _timeout, 2265eb3dc6SKevin Diggs * _interruptible, _interruptible_timeout, and _killable), init_completion(), 23c32f74abSWolfram Sang * reinit_completion(), and macros DECLARE_COMPLETION(), 24c32f74abSWolfram Sang * DECLARE_COMPLETION_ONSTACK(). 2565eb3dc6SKevin Diggs */ 261da177e4SLinus Torvalds struct completion { 271da177e4SLinus Torvalds unsigned int done; 28a5c6234eSThomas Gleixner struct swait_queue_head wait; 291da177e4SLinus Torvalds }; 301da177e4SLinus Torvalds 31a7967bc3SByungchul Park #define init_completion_map(x, m) __init_completion(x) 32cd8084f9SByungchul Park #define init_completion(x) __init_completion(x) 33cd8084f9SByungchul Park static inline void complete_acquire(struct completion *x) {} 34cd8084f9SByungchul Park static inline void complete_release(struct completion *x) {} 35cd8084f9SByungchul Park 361da177e4SLinus Torvalds #define COMPLETION_INITIALIZER(work) \ 37a5c6234eSThomas Gleixner { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) } 381da177e4SLinus Torvalds 39a7967bc3SByungchul Park #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \ 40a7967bc3SByungchul Park (*({ init_completion_map(&(work), &(map)); &(work); })) 41a7967bc3SByungchul Park 42f86bf9b7SIngo Molnar #define COMPLETION_INITIALIZER_ONSTACK(work) \ 43ec81048cSBoqun Feng (*({ init_completion(&work); &work; })) 44f86bf9b7SIngo Molnar 4565eb3dc6SKevin Diggs /** 46ee2f154aSRandy Dunlap * DECLARE_COMPLETION - declare and initialize a completion structure 4765eb3dc6SKevin Diggs * @work: identifier for the completion structure 4865eb3dc6SKevin Diggs * 4965eb3dc6SKevin Diggs * This macro declares and initializes a completion structure. Generally used 5065eb3dc6SKevin Diggs * for static declarations. You should use the _ONSTACK variant for automatic 5165eb3dc6SKevin Diggs * variables. 5265eb3dc6SKevin Diggs */ 531da177e4SLinus Torvalds #define DECLARE_COMPLETION(work) \ 541da177e4SLinus Torvalds struct completion work = COMPLETION_INITIALIZER(work) 551da177e4SLinus Torvalds 568b3db9c5SIngo Molnar /* 578b3db9c5SIngo Molnar * Lockdep needs to run a non-constant initializer for on-stack 588b3db9c5SIngo Molnar * completions - so we use the _ONSTACK() variant for those that 598b3db9c5SIngo Molnar * are on the kernel stack: 608b3db9c5SIngo Molnar */ 6165eb3dc6SKevin Diggs /** 62ee2f154aSRandy Dunlap * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure 6365eb3dc6SKevin Diggs * @work: identifier for the completion structure 6465eb3dc6SKevin Diggs * 6565eb3dc6SKevin Diggs * This macro declares and initializes a completion structure on the kernel 6665eb3dc6SKevin Diggs * stack. 6765eb3dc6SKevin Diggs */ 688b3db9c5SIngo Molnar #ifdef CONFIG_LOCKDEP 698b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) \ 70f86bf9b7SIngo Molnar struct completion work = COMPLETION_INITIALIZER_ONSTACK(work) 71a7967bc3SByungchul Park # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \ 72a7967bc3SByungchul Park struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) 738b3db9c5SIngo Molnar #else 748b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work) 75a7967bc3SByungchul Park # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work) 768b3db9c5SIngo Molnar #endif 778b3db9c5SIngo Molnar 7865eb3dc6SKevin Diggs /** 79ee2f154aSRandy Dunlap * init_completion - Initialize a dynamically allocated completion 80c32f74abSWolfram Sang * @x: pointer to completion structure that is to be initialized 8165eb3dc6SKevin Diggs * 8265eb3dc6SKevin Diggs * This inline function will initialize a dynamically created completion 8365eb3dc6SKevin Diggs * structure. 8465eb3dc6SKevin Diggs */ 85cd8084f9SByungchul Park static inline void __init_completion(struct completion *x) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds x->done = 0; 88a5c6234eSThomas Gleixner init_swait_queue_head(&x->wait); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 91c32f74abSWolfram Sang /** 92c32f74abSWolfram Sang * reinit_completion - reinitialize a completion structure 93c32f74abSWolfram Sang * @x: pointer to completion structure that is to be reinitialized 94c32f74abSWolfram Sang * 95c32f74abSWolfram Sang * This inline function should be used to reinitialize a completion structure so it can 96c32f74abSWolfram Sang * be reused. This is especially important after complete_all() is used. 97c32f74abSWolfram Sang */ 98c32f74abSWolfram Sang static inline void reinit_completion(struct completion *x) 99c32f74abSWolfram Sang { 100c32f74abSWolfram Sang x->done = 0; 101c32f74abSWolfram Sang } 102c32f74abSWolfram Sang 103b15136e9SIngo Molnar extern void wait_for_completion(struct completion *); 104686855f5SVladimir Davydov extern void wait_for_completion_io(struct completion *); 105b15136e9SIngo Molnar extern int wait_for_completion_interruptible(struct completion *x); 106009e577eSMatthew Wilcox extern int wait_for_completion_killable(struct completion *x); 107b15136e9SIngo Molnar extern unsigned long wait_for_completion_timeout(struct completion *x, 108b15136e9SIngo Molnar unsigned long timeout); 109686855f5SVladimir Davydov extern unsigned long wait_for_completion_io_timeout(struct completion *x, 110686855f5SVladimir Davydov unsigned long timeout); 1116bf41237SNeilBrown extern long wait_for_completion_interruptible_timeout( 112b15136e9SIngo Molnar struct completion *x, unsigned long timeout); 1136bf41237SNeilBrown extern long wait_for_completion_killable_timeout( 1140aa12fb4SSage Weil struct completion *x, unsigned long timeout); 115be4de352SDave Chinner extern bool try_wait_for_completion(struct completion *x); 116be4de352SDave Chinner extern bool completion_done(struct completion *x); 1171da177e4SLinus Torvalds 118b15136e9SIngo Molnar extern void complete(struct completion *); 119b15136e9SIngo Molnar extern void complete_all(struct completion *); 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds #endif 122