diff options
author | David Howells <dhowells@redhat.com> | 2006-12-07 06:33:26 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.osdl.org> | 2006-12-09 15:25:08 -0500 |
commit | 4594bf159f1962cec3b727954b7c598b07e2e737 (patch) | |
tree | bf2b58509973e8961d8d3a37c863ccec70547d3b /kernel/workqueue.c | |
parent | d8a53b358257625778387925165eaa97839b2f52 (diff) |
[PATCH] WorkStruct: Use direct assignment rather than cmpxchg()
Use direct assignment rather than cmpxchg() as the latter is unavailable
and unimplementable on some platforms and is actually unnecessary.
The use of cmpxchg() was to guard against two possibilities, neither of
which can actually occur:
(1) The pending flag may have been unset or may be cleared. However, given
where it's called, the pending flag is _always_ set. I don't think it
can be unset whilst we're in set_wq_data().
Once the work is enqueued to be actually run, the only way off the queue
is for it to be actually run.
If it's a delayed work item, then the bit can't be cleared by the timer
because we haven't started the timer yet. Also, the pending bit can't be
cleared by cancelling the delayed work _until_ the work item has had its
timer started.
(2) The workqueue pointer might change. This can only happen in two cases:
(a) The work item has just been queued to actually run, and so we're
protected by the appropriate workqueue spinlock.
(b) A delayed work item is being queued, and so the timer hasn't been
started yet, and so no one else knows about the work item or can
access it (the pending bit protects us).
Besides, set_wq_data() _sets_ the workqueue pointer unconditionally, so
it can be assigned instead.
So, replacing the set_wq_data() with a straight assignment would be okay
in most cases.
The problem is where we end up tangling with test_and_set_bit() emulated
using spinlocks, and even then it's not a problem _provided_
test_and_set_bit() doesn't attempt to modify the word if the bit was
set.
If that's a problem, then a bitops-proofed assignment will be required -
equivalent to atomic_set() vs other atomic_xxx() ops.
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r-- | kernel/workqueue.c | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 6b186750e9be..db49886bfae1 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c | |||
@@ -85,22 +85,19 @@ static inline int is_single_threaded(struct workqueue_struct *wq) | |||
85 | return list_empty(&wq->list); | 85 | return list_empty(&wq->list); |
86 | } | 86 | } |
87 | 87 | ||
88 | /* | ||
89 | * Set the workqueue on which a work item is to be run | ||
90 | * - Must *only* be called if the pending flag is set | ||
91 | */ | ||
88 | static inline void set_wq_data(struct work_struct *work, void *wq) | 92 | static inline void set_wq_data(struct work_struct *work, void *wq) |
89 | { | 93 | { |
90 | unsigned long new, old, res; | 94 | unsigned long new; |
95 | |||
96 | BUG_ON(!work_pending(work)); | ||
91 | 97 | ||
92 | /* assume the pending flag is already set and that the task has already | ||
93 | * been queued on this workqueue */ | ||
94 | new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING); | 98 | new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING); |
95 | res = work->management; | 99 | new |= work->management & WORK_STRUCT_FLAG_MASK; |
96 | if (res != new) { | 100 | work->management = new; |
97 | do { | ||
98 | old = res; | ||
99 | new = (unsigned long) wq; | ||
100 | new |= (old & WORK_STRUCT_FLAG_MASK); | ||
101 | res = cmpxchg(&work->management, old, new); | ||
102 | } while (res != old); | ||
103 | } | ||
104 | } | 101 | } |
105 | 102 | ||
106 | static inline void *get_wq_data(struct work_struct *work) | 103 | static inline void *get_wq_data(struct work_struct *work) |