123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include <linux/spinlock.h>
- #include <linux/task_work.h>
- #include <linux/tracehook.h>
- static struct callback_head work_exited;
- int
- task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
- {
- struct callback_head *head;
- do {
- head = READ_ONCE(task->task_works);
- if (unlikely(head == &work_exited))
- return -ESRCH;
- work->next = head;
- } while (cmpxchg(&task->task_works, head, work) != head);
- if (notify)
- set_notify_resume(task);
- return 0;
- }
- struct callback_head *
- task_work_cancel(struct task_struct *task, task_work_func_t func)
- {
- struct callback_head **pprev = &task->task_works;
- struct callback_head *work;
- unsigned long flags;
- if (likely(!task->task_works))
- return NULL;
-
- raw_spin_lock_irqsave(&task->pi_lock, flags);
- while ((work = lockless_dereference(*pprev))) {
- if (work->func != func)
- pprev = &work->next;
- else if (cmpxchg(pprev, work, work->next) == work)
- break;
- }
- raw_spin_unlock_irqrestore(&task->pi_lock, flags);
- return work;
- }
- void task_work_run(void)
- {
- struct task_struct *task = current;
- struct callback_head *work, *head, *next;
- for (;;) {
-
- do {
- work = READ_ONCE(task->task_works);
- head = !work && (task->flags & PF_EXITING) ?
- &work_exited : NULL;
- } while (cmpxchg(&task->task_works, work, head) != work);
- if (!work)
- break;
-
- raw_spin_unlock_wait(&task->pi_lock);
- do {
- next = work->next;
- work->func(work);
- work = next;
- cond_resched();
- } while (work);
- }
- }
|