blob: ce6badc98f6dcb3f2b78615cd4aa382d6d956abd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvaldsa08727b2006-12-16 09:53:50 -080011#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct workqueue_struct;
14
David Howells65f27f32006-11-22 14:55:48 +000015struct work_struct;
16typedef void (*work_func_t)(struct work_struct *work);
David Howells6bb49e52006-11-22 14:54:45 +000017
Linus Torvaldsa08727b2006-12-16 09:53:50 -080018/*
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 Torvalds1da177e2005-04-16 15:20:36 -070024struct work_struct {
Linus Torvaldsa08727b2006-12-16 09:53:50 -080025 atomic_long_t data;
David Howells365970a2006-11-22 14:54:49 +000026#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 Torvalds1da177e2005-04-16 15:20:36 -070029 struct list_head entry;
David Howells6bb49e52006-11-22 14:54:45 +000030 work_func_t func;
David Howells52bad642006-11-22 14:54:01 +000031};
32
Oleg Nesterov23b2e592007-05-09 02:34:19 -070033#define WORK_DATA_INIT() ATOMIC_LONG_INIT(0)
Linus Torvaldsa08727b2006-12-16 09:53:50 -080034
David Howells52bad642006-11-22 14:54:01 +000035struct delayed_work {
36 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct timer_list timer;
38};
39
James Bottomley1fa44ec2006-02-23 12:43:43 -060040struct execute_work {
41 struct work_struct work;
42};
43
David Howells65f27f32006-11-22 14:55:48 +000044#define __WORK_INITIALIZER(n, f) { \
Oleg Nesterov23b2e592007-05-09 02:34:19 -070045 .data = WORK_DATA_INIT(), \
46 .entry = { &(n).entry, &(n).entry }, \
David Howells65f27f32006-11-22 14:55:48 +000047 .func = (f), \
48 }
49
50#define __DELAYED_WORK_INITIALIZER(n, f) { \
51 .work = __WORK_INITIALIZER((n).work, (f)), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .timer = TIMER_INITIALIZER(NULL, 0, 0), \
53 }
54
David Howells65f27f32006-11-22 14:55:48 +000055#define DECLARE_WORK(n, f) \
56 struct work_struct n = __WORK_INITIALIZER(n, f)
57
David Howells65f27f32006-11-22 14:55:48 +000058#define DECLARE_DELAYED_WORK(n, f) \
59 struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
David Howells65f27f32006-11-22 14:55:48 +000062 * initialize a work item's function pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
David Howells65f27f32006-11-22 14:55:48 +000064#define PREPARE_WORK(_work, _func) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 do { \
David Howells52bad642006-11-22 14:54:01 +000066 (_work)->func = (_func); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 } while (0)
68
David Howells65f27f32006-11-22 14:55:48 +000069#define PREPARE_DELAYED_WORK(_work, _func) \
70 PREPARE_WORK(&(_work)->work, (_func))
David Howells52bad642006-11-22 14:54:01 +000071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
David Howells52bad642006-11-22 14:54:01 +000073 * initialize all of a work item in one go
Linus Torvaldsa08727b2006-12-16 09:53:50 -080074 *
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 Torvalds1da177e2005-04-16 15:20:36 -070078 */
Oleg Nesterov23b2e592007-05-09 02:34:19 -070079#define INIT_WORK(_work, _func) \
David Howells65f27f32006-11-22 14:55:48 +000080 do { \
Oleg Nesterov23b2e592007-05-09 02:34:19 -070081 (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
David Howells65f27f32006-11-22 14:55:48 +000082 INIT_LIST_HEAD(&(_work)->entry); \
83 PREPARE_WORK((_work), (_func)); \
84 } while (0)
85
86#define INIT_DELAYED_WORK(_work, _func) \
David Howells52bad642006-11-22 14:54:01 +000087 do { \
David Howells65f27f32006-11-22 14:55:48 +000088 INIT_WORK(&(_work)->work, (_func)); \
89 init_timer(&(_work)->timer); \
90 } while (0)
91
Venki Pallipadi28287032007-05-08 00:27:47 -070092#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 Howells365970a2006-11-22 14:54:49 +000098/**
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 Torvaldsa08727b2006-12-16 09:53:50 -0800103 test_bit(WORK_STRUCT_PENDING, work_data_bits(work))
David Howells365970a2006-11-22 14:54:49 +0000104
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 Torvalds02218722006-12-15 14:13:51 -0800110#define delayed_work_pending(w) \
111 work_pending(&(w)->work)
David Howells365970a2006-11-22 14:54:49 +0000112
David Howells65f27f32006-11-22 14:55:48 +0000113/**
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700114 * work_clear_pending - for internal use only, mark a work item as not pending
115 * @work: The work item in question
David Howells65f27f32006-11-22 14:55:48 +0000116 */
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700117#define work_clear_pending(work) \
Linus Torvaldsa08727b2006-12-16 09:53:50 -0800118 clear_bit(WORK_STRUCT_PENDING, work_data_bits(work))
David Howells65f27f32006-11-22 14:55:48 +0000119
David Howells52bad642006-11-22 14:54:01 +0000120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121extern struct workqueue_struct *__create_workqueue(const char *name,
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800122 int singlethread,
123 int freezeable);
124#define create_workqueue(name) __create_workqueue((name), 0, 0)
Oleg Nesterove3dfd292007-05-16 22:11:11 -0700125#define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
Rafael J. Wysocki341a5952006-12-06 20:34:49 -0800126#define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128extern void destroy_workqueue(struct workqueue_struct *wq);
129
130extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work));
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700131extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq,
132 struct delayed_work *work, unsigned long delay));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -0700133extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700134 struct delayed_work *work, unsigned long delay);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700137extern void flush_scheduled_work(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139extern int FASTCALL(schedule_work(struct work_struct *work));
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700140extern int FASTCALL(schedule_delayed_work(struct delayed_work *work,
141 unsigned long delay));
142extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
143 unsigned long delay);
David Howells65f27f32006-11-22 14:55:48 +0000144extern int schedule_on_each_cpu(work_func_t func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145extern int current_is_keventd(void);
146extern int keventd_up(void);
147
148extern void init_workqueues(void);
David Howells65f27f32006-11-22 14:55:48 +0000149int execute_in_process_context(work_func_t fn, struct execute_work *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700151extern int cancel_work_sync(struct work_struct *work);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Kill off a pending schedule_delayed_work(). Note that the work callback
Oleg Nesterov071b6382007-04-26 15:45:32 -0700155 * 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 Nesterov28e53bd2007-05-09 02:34:22 -0700157 * cancel_work_sync() to wait on it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
David Howells52bad642006-11-22 14:54:01 +0000159static inline int cancel_delayed_work(struct delayed_work *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int ret;
162
Oleg Nesterov223a10a2007-05-18 00:36:42 -0700163 ret = del_timer_sync(&work->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ret)
Oleg Nesterov23b2e592007-05-09 02:34:19 -0700165 work_clear_pending(&work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
167}
168
Oleg Nesterov1f1f6422007-07-15 23:41:44 -0700169extern int cancel_delayed_work_sync(struct delayed_work *work);
Oleg Nesterov1634c482007-05-09 02:34:18 -0700170
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700171/* Obsolete. use cancel_delayed_work_sync() */
Oleg Nesterov1634c482007-05-09 02:34:18 -0700172static inline
173void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
174 struct delayed_work *work)
175{
Oleg Nesterovf5a421a2007-07-15 23:41:44 -0700176 cancel_delayed_work_sync(work);
177}
178
179/* Obsolete. use cancel_delayed_work_sync() */
180static inline
181void cancel_rearming_delayed_work(struct delayed_work *work)
182{
183 cancel_delayed_work_sync(work);
Oleg Nesterov1634c482007-05-09 02:34:18 -0700184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif