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> |
| 11 | |
| 12 | struct workqueue_struct; |
| 13 | |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 14 | typedef void (*work_func_t)(void *data); |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | struct work_struct { |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame^] | 17 | /* the first word is the work queue pointer and the pending flag |
| 18 | * rolled into one */ |
| 19 | unsigned long management; |
| 20 | #define WORK_STRUCT_PENDING 0 /* T if work item pending execution */ |
| 21 | #define WORK_STRUCT_FLAG_MASK (3UL) |
| 22 | #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct list_head entry; |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 24 | work_func_t func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | void *data; |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | struct delayed_work { |
| 29 | struct work_struct work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | struct timer_list timer; |
| 31 | }; |
| 32 | |
James Bottomley | 1fa44ec | 2006-02-23 12:43:43 -0600 | [diff] [blame] | 33 | struct execute_work { |
| 34 | struct work_struct work; |
| 35 | }; |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define __WORK_INITIALIZER(n, f, d) { \ |
| 38 | .entry = { &(n).entry, &(n).entry }, \ |
| 39 | .func = (f), \ |
| 40 | .data = (d), \ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | #define __DELAYED_WORK_INITIALIZER(n, f, d) { \ |
| 44 | .work = __WORK_INITIALIZER((n).work, (f), (d)), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | .timer = TIMER_INITIALIZER(NULL, 0, 0), \ |
| 46 | } |
| 47 | |
| 48 | #define DECLARE_WORK(n, f, d) \ |
| 49 | struct work_struct n = __WORK_INITIALIZER(n, f, d) |
| 50 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 51 | #define DECLARE_DELAYED_WORK(n, f, d) \ |
| 52 | struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, d) |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 55 | * initialize a work item's function and data pointers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
| 57 | #define PREPARE_WORK(_work, _func, _data) \ |
| 58 | do { \ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 59 | (_work)->func = (_func); \ |
| 60 | (_work)->data = (_data); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } while (0) |
| 62 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 63 | #define PREPARE_DELAYED_WORK(_work, _func, _data) \ |
| 64 | PREPARE_WORK(&(_work)->work, (_func), (_data)) |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 67 | * initialize all of a work item in one go |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
| 69 | #define INIT_WORK(_work, _func, _data) \ |
| 70 | do { \ |
| 71 | INIT_LIST_HEAD(&(_work)->entry); \ |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame^] | 72 | (_work)->management = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | PREPARE_WORK((_work), (_func), (_data)); \ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 74 | } while (0) |
| 75 | |
| 76 | #define INIT_DELAYED_WORK(_work, _func, _data) \ |
| 77 | do { \ |
| 78 | INIT_WORK(&(_work)->work, (_func), (_data)); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | init_timer(&(_work)->timer); \ |
| 80 | } while (0) |
| 81 | |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame^] | 82 | /** |
| 83 | * work_pending - Find out whether a work item is currently pending |
| 84 | * @work: The work item in question |
| 85 | */ |
| 86 | #define work_pending(work) \ |
| 87 | test_bit(WORK_STRUCT_PENDING, &(work)->management) |
| 88 | |
| 89 | /** |
| 90 | * delayed_work_pending - Find out whether a delayable work item is currently |
| 91 | * pending |
| 92 | * @work: The work item in question |
| 93 | */ |
| 94 | #define delayed_work_pending(work) \ |
| 95 | test_bit(WORK_STRUCT_PENDING, &(work)->work.management) |
| 96 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | extern struct workqueue_struct *__create_workqueue(const char *name, |
| 99 | int singlethread); |
| 100 | #define create_workqueue(name) __create_workqueue((name), 0) |
| 101 | #define create_singlethread_workqueue(name) __create_workqueue((name), 1) |
| 102 | |
| 103 | extern void destroy_workqueue(struct workqueue_struct *wq); |
| 104 | |
| 105 | extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work)); |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 106 | extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay)); |
Venkatesh Pallipadi | 7a6bc1c | 2006-06-28 13:50:33 -0700 | [diff] [blame] | 107 | extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 108 | struct delayed_work *work, unsigned long delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq)); |
| 110 | |
| 111 | extern int FASTCALL(schedule_work(struct work_struct *work)); |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 112 | extern int FASTCALL(schedule_delayed_work(struct delayed_work *work, unsigned long delay)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 114 | extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, unsigned long delay); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 115 | extern int schedule_on_each_cpu(work_func_t func, void *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | extern void flush_scheduled_work(void); |
| 117 | extern int current_is_keventd(void); |
| 118 | extern int keventd_up(void); |
| 119 | |
| 120 | extern void init_workqueues(void); |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 121 | void cancel_rearming_delayed_work(struct delayed_work *work); |
James Bottomley | 81ddef7 | 2005-04-16 15:23:59 -0700 | [diff] [blame] | 122 | void cancel_rearming_delayed_workqueue(struct workqueue_struct *, |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 123 | struct delayed_work *); |
David Howells | 6bb49e5 | 2006-11-22 14:54:45 +0000 | [diff] [blame] | 124 | int execute_in_process_context(work_func_t fn, void *, struct execute_work *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Kill off a pending schedule_delayed_work(). Note that the work callback |
| 128 | * function may still be running on return from cancel_delayed_work(). Run |
| 129 | * flush_scheduled_work() to wait on it. |
| 130 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 131 | static inline int cancel_delayed_work(struct delayed_work *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | int ret; |
| 134 | |
| 135 | ret = del_timer_sync(&work->timer); |
| 136 | if (ret) |
David Howells | 365970a | 2006-11-22 14:54:49 +0000 | [diff] [blame^] | 137 | clear_bit(WORK_STRUCT_PENDING, &work->work.management); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | #endif |