workqueue.h revision 293151
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29#ifndef _LINUX_WORKQUEUE_H_ 30#define _LINUX_WORKQUEUE_H_ 31 32#include <linux/types.h> 33#include <linux/kernel.h> 34#include <linux/timer.h> 35#include <linux/slab.h> 36 37#include <asm/atomic.h> 38 39#include <sys/taskqueue.h> 40 41struct workqueue_struct { 42 struct taskqueue *taskqueue; 43 atomic_t draining; 44}; 45 46struct work_struct { 47 struct task work_task; 48 struct taskqueue *taskqueue; 49 void (*fn)(struct work_struct *); 50}; 51 52typedef __typeof(((struct work_struct *)0)->fn) work_func_t; 53 54struct delayed_work { 55 struct work_struct work; 56 struct callout timer; 57}; 58 59extern void linux_work_fn(void *, int); 60extern void linux_flush_fn(void *, int); 61extern void linux_delayed_work_fn(void *); 62extern struct workqueue_struct *linux_create_workqueue_common(const char *, int); 63extern void destroy_workqueue(struct workqueue_struct *); 64 65static inline struct delayed_work * 66to_delayed_work(struct work_struct *work) 67{ 68 69 return container_of(work, struct delayed_work, work); 70} 71 72#define INIT_WORK(work, func) \ 73do { \ 74 (work)->fn = (func); \ 75 (work)->taskqueue = NULL; \ 76 TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work)); \ 77} while (0) 78 79#define INIT_DELAYED_WORK(_work, func) \ 80do { \ 81 INIT_WORK(&(_work)->work, func); \ 82 callout_init(&(_work)->timer, CALLOUT_MPSAFE); \ 83} while (0) 84 85#define INIT_DEFERRABLE_WORK(...) INIT_DELAYED_WORK(__VA_ARGS__) 86 87#define schedule_work(work) \ 88do { \ 89 (work)->taskqueue = taskqueue_thread; \ 90 taskqueue_enqueue(taskqueue_thread, &(work)->work_task); \ 91} while (0) 92 93#define flush_scheduled_work() flush_taskqueue(taskqueue_thread) 94 95static inline int 96queue_work(struct workqueue_struct *wq, struct work_struct *work) 97{ 98 work->taskqueue = wq->taskqueue; 99 /* Check for draining */ 100 if (atomic_read(&wq->draining) != 0) 101 return (!work->work_task.ta_pending); 102 /* Return opposite value to align with Linux logic */ 103 return (!taskqueue_enqueue(wq->taskqueue, &work->work_task)); 104} 105 106static inline int 107queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, 108 unsigned long delay) 109{ 110 int pending; 111 112 work->work.taskqueue = wq->taskqueue; 113 if (atomic_read(&wq->draining) != 0) { 114 pending = work->work.work_task.ta_pending; 115 } else if (delay != 0) { 116 pending = work->work.work_task.ta_pending; 117 callout_reset(&work->timer, delay, linux_delayed_work_fn, work); 118 } else { 119 callout_stop(&work->timer); 120 pending = taskqueue_enqueue(work->work.taskqueue, 121 &work->work.work_task); 122 } 123 return (!pending); 124} 125 126static inline bool 127schedule_delayed_work(struct delayed_work *dwork, 128 unsigned long delay) 129{ 130 struct workqueue_struct wq; 131 132 wq.taskqueue = taskqueue_thread; 133 atomic_set(&wq.draining, 0); 134 return (queue_delayed_work(&wq, dwork, delay)); 135} 136 137#define create_singlethread_workqueue(name) \ 138 linux_create_workqueue_common(name, 1) 139 140#define create_workqueue(name) \ 141 linux_create_workqueue_common(name, MAXCPU) 142 143#define alloc_ordered_workqueue(name, flags) \ 144 linux_create_workqueue_common(name, 1) 145 146#define alloc_workqueue(name, flags, max_active) \ 147 linux_create_workqueue_common(name, max_active) 148 149#define flush_workqueue(wq) flush_taskqueue((wq)->taskqueue) 150 151static inline void 152flush_taskqueue(struct taskqueue *tq) 153{ 154 struct task flushtask; 155 156 PHOLD(curproc); 157 TASK_INIT(&flushtask, 0, linux_flush_fn, NULL); 158 taskqueue_enqueue(tq, &flushtask); 159 taskqueue_drain(tq, &flushtask); 160 PRELE(curproc); 161} 162 163static inline void 164drain_workqueue(struct workqueue_struct *wq) 165{ 166 atomic_inc(&wq->draining); 167 flush_taskqueue(wq->taskqueue); 168 atomic_dec(&wq->draining); 169} 170 171static inline int 172cancel_work_sync(struct work_struct *work) 173{ 174 if (work->taskqueue && 175 taskqueue_cancel(work->taskqueue, &work->work_task, NULL)) 176 taskqueue_drain(work->taskqueue, &work->work_task); 177 return 0; 178} 179 180/* 181 * This may leave work running on another CPU as it does on Linux. 182 */ 183static inline int 184cancel_delayed_work(struct delayed_work *work) 185{ 186 187 callout_stop(&work->timer); 188 if (work->work.taskqueue) 189 return (taskqueue_cancel(work->work.taskqueue, 190 &work->work.work_task, NULL) == 0); 191 return 0; 192} 193 194static inline int 195cancel_delayed_work_sync(struct delayed_work *work) 196{ 197 198 callout_drain(&work->timer); 199 if (work->work.taskqueue && 200 taskqueue_cancel(work->work.taskqueue, &work->work.work_task, NULL)) 201 taskqueue_drain(work->work.taskqueue, &work->work.work_task); 202 return 0; 203} 204 205static inline bool 206mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, 207 unsigned long delay) 208{ 209 cancel_delayed_work(dwork); 210 queue_delayed_work(wq, dwork, delay); 211 return false; 212} 213 214#endif /* _LINUX_WORKQUEUE_H_ */ 215