Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * workqueue.h --- work queue handling for Linux. |
| 3 | */ |
| 4 | |
| 5 | #ifndef _LINUX_WORKQUEUE_H |
| 6 | #define _LINUX_WORKQUEUE_H |
| 7 | |
| 8 | #include <linux/timer.h> |
| 9 | #include <linux/linkage.h> |
| 10 | #include <linux/bitops.h> |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 11 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct workqueue_struct; |
| 14 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 15 | struct work_struct; |
| 16 | typedef void (*work_func_t)(struct work_struct *work); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 17 | |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 18 | /* |
| 19 | * The first word is the work queue pointer and the flags rolled into |
| 20 | * one |
| 21 | */ |
| 22 | #define work_data_bits(work) ((unsigned long *)(&(work)->data)) |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | struct work_struct { |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 25 | atomic_long_t data; |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 26 | #define WORK_STRUCT_PENDING 0 /* T if work item pending execution */ |
| 27 | #define WORK_STRUCT_FLAG_MASK (3UL) |
| 28 | #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct list_head entry; |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 30 | work_func_t func; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 33 | #define WORK_DATA_INIT() ATOMIC_LONG_INIT(0) |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 34 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 35 | struct delayed_work { |
| 36 | struct work_struct work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct timer_list timer; |
| 38 | }; |
| 39 | |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 40 | struct execute_work { |
| 41 | struct work_struct work; |
| 42 | }; |
| 43 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 44 | #define __WORK_INITIALIZER(n, f) { \ |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 45 | .data = WORK_DATA_INIT(), \ |
| 46 | .entry = { &(n).entry, &(n).entry }, \ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 47 | .func = (f), \ |
| 48 | } |
| 49 | |
| 50 | #define __DELAYED_WORK_INITIALIZER(n, f) { \ |
| 51 | .work = __WORK_INITIALIZER((n).work, (f)), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .timer = TIMER_INITIALIZER(NULL, 0, 0), \ |
| 53 | } |
| 54 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 55 | #define DECLARE_WORK(n, f) \ |
| 56 | struct work_struct n = __WORK_INITIALIZER(n, f) |
| 57 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 58 | #define DECLARE_DELAYED_WORK(n, f) \ |
| 59 | struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f) |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 62 | * initialize a work item's function pointer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 64 | #define PREPARE_WORK(_work, _func) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | do { \ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 66 | (_work)->func = (_func); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } while (0) |
| 68 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 69 | #define PREPARE_DELAYED_WORK(_work, _func) \ |
| 70 | PREPARE_WORK(&(_work)->work, (_func)) |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 73 | * initialize all of a work item in one go |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 74 | * |
| 75 | * NOTE! No point in using "atomic_long_set()": useing a direct |
| 76 | * assignment of the work data initializer allows the compiler |
| 77 | * to generate better code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | */ |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 79 | #define INIT_WORK(_work, _func) \ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 80 | do { \ |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 81 | (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 82 | INIT_LIST_HEAD(&(_work)->entry); \ |
| 83 | PREPARE_WORK((_work), (_func)); \ |
| 84 | } while (0) |
| 85 | |
| 86 | #define INIT_DELAYED_WORK(_work, _func) \ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 87 | do { \ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 88 | INIT_WORK(&(_work)->work, (_func)); \ |
| 89 | init_timer(&(_work)->timer); \ |
| 90 | } while (0) |
| 91 | |
Venki Pallipadi | 2828703 | 2007-05-08 00:27:47 -0700 | [diff] [blame] | 92 | #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \ |
| 93 | do { \ |
| 94 | INIT_WORK(&(_work)->work, (_func)); \ |
| 95 | init_timer_deferrable(&(_work)->timer); \ |
| 96 | } while (0) |
| 97 | |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 98 | /** |
| 99 | * work_pending - Find out whether a work item is currently pending |
| 100 | * @work: The work item in question |
| 101 | */ |
| 102 | #define work_pending(work) \ |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 103 | test_bit(WORK_STRUCT_PENDING, work_data_bits(work)) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * delayed_work_pending - Find out whether a delayable work item is currently |
| 107 | * pending |
| 108 | * @work: The work item in question |
| 109 | */ |
Linus Torvalds | 0221872 | 2006-12-15 14:13:51 -0800 | [diff] [blame] | 110 | #define delayed_work_pending(w) \ |
| 111 | work_pending(&(w)->work) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame] | 112 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 113 | /** |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 114 | * work_clear_pending - for internal use only, mark a work item as not pending |
| 115 | * @work: The work item in question |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 116 | */ |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 117 | #define work_clear_pending(work) \ |
Linus Torvalds | a08727b | 2006-12-16 09:53:50 -0800 | [diff] [blame] | 118 | clear_bit(WORK_STRUCT_PENDING, work_data_bits(work)) |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 119 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | extern struct workqueue_struct *__create_workqueue(const char *name, |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 122 | int singlethread, |
| 123 | int freezeable); |
| 124 | #define create_workqueue(name) __create_workqueue((name), 0, 0) |
Oleg Nesterov | e3dfd29 | 2007-05-16 22:11:11 -0700 | [diff] [blame] | 125 | #define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1) |
Rafael J. Wysocki | 341a595 | 2006-12-06 20:34:49 -0800 | [diff] [blame] | 126 | #define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | extern void destroy_workqueue(struct workqueue_struct *wq); |
| 129 | |
| 130 | extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work)); |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 131 | extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, |
| 132 | struct delayed_work *work, unsigned long delay)); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 133 | extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 134 | struct delayed_work *work, unsigned long delay); |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq)); |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 137 | extern void flush_scheduled_work(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | extern int FASTCALL(schedule_work(struct work_struct *work)); |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 140 | extern int FASTCALL(schedule_delayed_work(struct delayed_work *work, |
| 141 | unsigned long delay)); |
| 142 | extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, |
| 143 | unsigned long delay); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 144 | extern int schedule_on_each_cpu(work_func_t func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | extern int current_is_keventd(void); |
| 146 | extern int keventd_up(void); |
| 147 | |
| 148 | extern void init_workqueues(void); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 149 | int execute_in_process_context(work_func_t fn, struct execute_work *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 151 | extern void cancel_work_sync(struct work_struct *work); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* |
| 154 | * Kill off a pending schedule_delayed_work(). Note that the work callback |
Oleg Nesterov | 071b638 | 2007-04-26 15:45:32 -0700 | [diff] [blame] | 155 | * function may still be running on return from cancel_delayed_work(), unless |
| 156 | * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 157 | * cancel_work_sync() to wait on it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 159 | static inline int cancel_delayed_work(struct delayed_work *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
| 161 | int ret; |
| 162 | |
Oleg Nesterov | 223a10a | 2007-05-18 00:36:42 -0700 | [diff] [blame] | 163 | ret = del_timer_sync(&work->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (ret) |
Oleg Nesterov | 23b2e59 | 2007-05-09 02:34:19 -0700 | [diff] [blame] | 165 | work_clear_pending(&work->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Oleg Nesterov | 1634c48 | 2007-05-09 02:34:18 -0700 | [diff] [blame] | 169 | extern void cancel_rearming_delayed_work(struct delayed_work *work); |
| 170 | |
| 171 | /* Obsolete. use cancel_rearming_delayed_work() */ |
| 172 | static inline |
| 173 | void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq, |
| 174 | struct delayed_work *work) |
| 175 | { |
| 176 | cancel_rearming_delayed_work(work); |
| 177 | } |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #endif |