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