3 * Copyright © 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
19 * Helper functions to schedule periodic work in Linux kernel mode.
24 #include "periodic_work.h"
26 #define MYDRVNAME "periodic_work"
30 struct PERIODIC_WORK_Tag {
32 struct delayed_work work;
33 void (*workfunc)(void *);
38 struct workqueue_struct *workqueue;
44 static void periodic_work_func(struct work_struct *work)
46 PERIODIC_WORK *periodic_work =
47 container_of(work, struct PERIODIC_WORK_Tag, work.work);
48 (*periodic_work->workfunc)(periodic_work->workfuncarg);
53 PERIODIC_WORK *visor_periodic_work_create(ulong jiffy_interval,
54 struct workqueue_struct *workqueue,
55 void (*workfunc)(void *),
59 PERIODIC_WORK *periodic_work = kzalloc(sizeof(PERIODIC_WORK),
60 GFP_KERNEL | __GFP_NORETRY);
61 if (periodic_work == NULL) {
62 ERRDRV("periodic_work allocation failed ");
65 rwlock_init(&periodic_work->lock);
66 periodic_work->jiffy_interval = jiffy_interval;
67 periodic_work->workqueue = workqueue;
68 periodic_work->workfunc = workfunc;
69 periodic_work->workfuncarg = workfuncarg;
70 periodic_work->devnam = devnam;
73 EXPORT_SYMBOL_GPL(visor_periodic_work_create);
77 void visor_periodic_work_destroy(PERIODIC_WORK *periodic_work)
79 if (periodic_work == NULL)
83 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
87 /** Call this from your periodic work worker function to schedule the next
89 * If this function returns FALSE, there was a failure and the
90 * periodic work is no longer scheduled
92 BOOL visor_periodic_work_nextperiod(PERIODIC_WORK *periodic_work)
95 write_lock(&periodic_work->lock);
96 if (periodic_work->want_to_stop) {
97 periodic_work->is_scheduled = FALSE;
98 periodic_work->want_to_stop = FALSE;
99 rc = TRUE; /* yes, TRUE; see visor_periodic_work_stop() */
101 } else if (queue_delayed_work(periodic_work->workqueue,
102 &periodic_work->work,
103 periodic_work->jiffy_interval) < 0) {
104 ERRDEV(periodic_work->devnam, "queue_delayed_work failed!");
105 periodic_work->is_scheduled = FALSE;
111 write_unlock(&periodic_work->lock);
114 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
118 /** This function returns TRUE iff new periodic work was actually started.
119 * If this function returns FALSE, then no work was started
120 * (either because it was already started, or because of a failure).
122 BOOL visor_periodic_work_start(PERIODIC_WORK *periodic_work)
126 write_lock(&periodic_work->lock);
127 if (periodic_work->is_scheduled) {
131 if (periodic_work->want_to_stop) {
132 ERRDEV(periodic_work->devnam,
133 "dev_start_periodic_work failed!");
137 INIT_DELAYED_WORK(&periodic_work->work, &periodic_work_func);
138 if (queue_delayed_work(periodic_work->workqueue,
139 &periodic_work->work,
140 periodic_work->jiffy_interval) < 0) {
141 ERRDEV(periodic_work->devnam,
142 "%s queue_delayed_work failed!", __func__);
146 periodic_work->is_scheduled = TRUE;
149 write_unlock(&periodic_work->lock);
153 EXPORT_SYMBOL_GPL(visor_periodic_work_start);
158 /** This function returns TRUE iff your call actually stopped the periodic
161 * -- PAY ATTENTION... this is important --
165 * Do NOT call this function from some function that is running on the
166 * same workqueue as the work you are trying to stop might be running
167 * on! If you violate this rule, visor_periodic_work_stop() MIGHT work,
168 * but it also MIGHT get hung up in an infinite loop saying
169 * "waiting for delayed work...". This will happen if the delayed work
170 * you are trying to cancel has been put in the workqueue list, but can't
171 * run yet because we are running that same workqueue thread right now.
173 * Bottom line: If you need to call visor_periodic_work_stop() from a
174 * workitem, be sure the workitem is on a DIFFERENT workqueue than the
175 * workitem that you are trying to cancel.
177 * If I could figure out some way to check for this "no no" condition in
178 * the code, I would. It would have saved me the trouble of writing this
179 * long comment. And also, don't think this is some "theoretical" race
180 * condition. It is REAL, as I have spent the day chasing it.
184 * Take close note of the locks that you own when you call this function.
185 * You must NOT own any locks that are needed by the periodic work
186 * function that is currently installed. If you DO, a deadlock may result,
187 * because stopping the periodic work often involves waiting for the last
188 * iteration of the periodic work function to complete. Again, if you hit
189 * this deadlock, you will get hung up in an infinite loop saying
190 * "waiting for delayed work...".
192 BOOL visor_periodic_work_stop(PERIODIC_WORK *periodic_work)
194 BOOL stopped_something = FALSE;
196 write_lock(&periodic_work->lock);
197 stopped_something = periodic_work->is_scheduled &&
198 (!periodic_work->want_to_stop);
199 while (periodic_work->is_scheduled) {
200 periodic_work->want_to_stop = TRUE;
201 if (cancel_delayed_work(&periodic_work->work)) {
202 /* We get here if the delayed work was pending as
203 * delayed work, but was NOT run.
205 ASSERT(periodic_work->is_scheduled);
206 periodic_work->is_scheduled = FALSE;
208 /* If we get here, either the delayed work:
210 * - is running RIGHT NOW on another processor, OR,
211 * - wasn't even scheduled (there is a miniscule
212 * timing window where this could be the case)
213 * flush_workqueue() would make sure it is finished
214 * executing, but that still isn't very useful, which
215 * explains the loop...
218 if (periodic_work->is_scheduled) {
219 write_unlock(&periodic_work->lock);
220 WARNDEV(periodic_work->devnam,
221 "waiting for delayed work...");
222 /* We rely on the delayed work function running here,
223 * and eventually calling
224 * visor_periodic_work_nextperiod(),
225 * which will see that want_to_stop is set, and
226 * subsequently clear is_scheduled.
229 write_lock(&periodic_work->lock);
231 periodic_work->want_to_stop = FALSE;
233 write_unlock(&periodic_work->lock);
234 return stopped_something;
236 EXPORT_SYMBOL_GPL(visor_periodic_work_stop);