wait-simple: Rework for use with completions

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
This commit is contained in:
Thomas Gleixner 2013-01-10 11:47:35 +01:00 committed by Alibek Omarov
parent 99076b3731
commit 65870d64d3
2 changed files with 76 additions and 57 deletions

View File

@ -22,12 +22,14 @@ struct swait_head {
struct list_head list; struct list_head list;
}; };
#define DEFINE_SWAIT_HEAD(name) \ #define SWAIT_HEAD_INITIALIZER(name) { \
struct swait_head name = { \
.lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
.list = LIST_HEAD_INIT((name).list), \ .list = LIST_HEAD_INIT((name).list), \
} }
#define DEFINE_SWAIT_HEAD(name) \
struct swait_head name = SWAIT_HEAD_INITIALIZER(name)
extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key); extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
#define init_swait_head(swh) \ #define init_swait_head(swh) \
@ -40,63 +42,25 @@ extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
/* /*
* Waiter functions * Waiter functions
*/ */
static inline bool swaiter_enqueued(struct swaiter *w) extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w);
{
return w->task != NULL;
}
extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state); extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state);
extern void swait_finish_locked(struct swait_head *head, struct swaiter *w);
extern void swait_finish(struct swait_head *head, struct swaiter *w); extern void swait_finish(struct swait_head *head, struct swaiter *w);
/*
* Adds w to head->list. Must be called with head->lock locked.
*/
static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
{
list_add(&w->node, &head->list);
/* We can't let the condition leak before the setting of head */
smp_mb();
}
/*
* Removes w from head->list. Must be called with head->lock locked.
*/
static inline void __swait_dequeue(struct swaiter *w)
{
list_del_init(&w->node);
}
/*
* Check whether a head has waiters enqueued
*/
static inline bool swait_head_has_waiters(struct swait_head *h)
{
/* Make sure the condition is visible before checking list_empty() */
smp_mb();
return !list_empty(&h->list);
}
/* /*
* Wakeup functions * Wakeup functions
*/ */
extern int __swait_wake(struct swait_head *head, unsigned int state); extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num);
extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num);
static inline int swait_wake(struct swait_head *head) #define swait_wake(head) __swait_wake(head, TASK_NORMAL, 1)
{ #define swait_wake_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 1)
return swait_head_has_waiters(head) ? #define swait_wake_all(head) __swait_wake(head, TASK_NORMAL, 0)
__swait_wake(head, TASK_NORMAL) : 0; #define swait_wake_all_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 0)
}
static inline int swait_wake_interruptible(struct swait_head *head)
{
return swait_head_has_waiters(head) ?
__swait_wake(head, TASK_INTERRUPTIBLE) : 0;
}
/* /*
* Event API * Event API
*/ */
#define __swait_event(wq, condition) \ #define __swait_event(wq, condition) \
do { \ do { \
DEFINE_SWAITER(__wait); \ DEFINE_SWAITER(__wait); \

View File

@ -12,6 +12,28 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/wait-simple.h> #include <linux/wait-simple.h>
/* Adds w to head->list. Must be called with head->lock locked. */
static inline void __swait_enqueue(struct swait_head *head, struct swaiter *w)
{
list_add(&w->node, &head->list);
/* We can't let the condition leak before the setting of head */
smp_mb();
}
/* Removes w from head->list. Must be called with head->lock locked. */
static inline void __swait_dequeue(struct swaiter *w)
{
list_del_init(&w->node);
}
/* Check whether a head has waiters enqueued */
static inline bool swait_head_has_waiters(struct swait_head *h)
{
/* Make sure the condition is visible before checking list_empty() */
smp_mb();
return !list_empty(&h->list);
}
void __init_swait_head(struct swait_head *head, struct lock_class_key *key) void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
{ {
raw_spin_lock_init(&head->lock); raw_spin_lock_init(&head->lock);
@ -20,19 +42,31 @@ void __init_swait_head(struct swait_head *head, struct lock_class_key *key)
} }
EXPORT_SYMBOL(__init_swait_head); EXPORT_SYMBOL(__init_swait_head);
void swait_prepare_locked(struct swait_head *head, struct swaiter *w)
{
w->task = current;
if (list_empty(&w->node))
__swait_enqueue(head, w);
}
void swait_prepare(struct swait_head *head, struct swaiter *w, int state) void swait_prepare(struct swait_head *head, struct swaiter *w, int state)
{ {
unsigned long flags; unsigned long flags;
raw_spin_lock_irqsave(&head->lock, flags); raw_spin_lock_irqsave(&head->lock, flags);
w->task = current; swait_prepare_locked(head, w);
if (list_empty(&w->node)) __set_current_state(state);
__swait_enqueue(head, w);
set_current_state(state);
raw_spin_unlock_irqrestore(&head->lock, flags); raw_spin_unlock_irqrestore(&head->lock, flags);
} }
EXPORT_SYMBOL(swait_prepare); EXPORT_SYMBOL(swait_prepare);
void swait_finish_locked(struct swait_head *head, struct swaiter *w)
{
__set_current_state(TASK_RUNNING);
if (w->task)
__swait_dequeue(w);
}
void swait_finish(struct swait_head *head, struct swaiter *w) void swait_finish(struct swait_head *head, struct swaiter *w)
{ {
unsigned long flags; unsigned long flags;
@ -46,22 +80,43 @@ void swait_finish(struct swait_head *head, struct swaiter *w)
} }
EXPORT_SYMBOL(swait_finish); EXPORT_SYMBOL(swait_finish);
int __swait_wake(struct swait_head *head, unsigned int state) unsigned int
__swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num)
{ {
struct swaiter *curr, *next; struct swaiter *curr, *next;
unsigned long flags;
int woken = 0; int woken = 0;
raw_spin_lock_irqsave(&head->lock, flags);
list_for_each_entry_safe(curr, next, &head->list, node) { list_for_each_entry_safe(curr, next, &head->list, node) {
if (wake_up_state(curr->task, state)) { if (wake_up_state(curr->task, state)) {
__swait_dequeue(curr); __swait_dequeue(curr);
/*
* The waiting task can free the waiter as
* soon as curr->task = NULL is written,
* without taking any locks. A memory barrier
* is required here to prevent the following
* store to curr->task from getting ahead of
* the dequeue operation.
*/
smp_wmb();
curr->task = NULL; curr->task = NULL;
woken++; if (++woken == num)
break;
} }
} }
return woken;
}
unsigned int
__swait_wake(struct swait_head *head, unsigned int state, unsigned int num)
{
unsigned long flags;
int woken;
if (!swait_head_has_waiters(head))
return 0;
raw_spin_lock_irqsave(&head->lock, flags);
woken = __swait_wake_locked(head, state, num);
raw_spin_unlock_irqrestore(&head->lock, flags); raw_spin_unlock_irqrestore(&head->lock, flags);
return woken; return woken;
} }