3 * Copyright (C) 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.
23 #include "periodic_work.h"
25 #define MYDRVNAME "periodic_work"
27 struct periodic_work
{
29 struct delayed_work work
;
30 void (*workfunc
)(void *);
35 struct workqueue_struct
*workqueue
;
39 static void periodic_work_func(struct work_struct
*work
)
41 struct periodic_work
*pw
;
43 pw
= container_of(work
, struct periodic_work
, work
.work
);
44 (*pw
->workfunc
)(pw
->workfuncarg
);
47 struct periodic_work
*visor_periodic_work_create(ulong jiffy_interval
,
48 struct workqueue_struct
*workqueue
,
49 void (*workfunc
)(void *),
53 struct periodic_work
*pw
;
55 pw
= kzalloc(sizeof(*pw
), GFP_KERNEL
| __GFP_NORETRY
);
59 rwlock_init(&pw
->lock
);
60 pw
->jiffy_interval
= jiffy_interval
;
61 pw
->workqueue
= workqueue
;
62 pw
->workfunc
= workfunc
;
63 pw
->workfuncarg
= workfuncarg
;
67 EXPORT_SYMBOL_GPL(visor_periodic_work_create
);
69 void visor_periodic_work_destroy(struct periodic_work
*pw
)
73 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy
);
75 /** Call this from your periodic work worker function to schedule the next
77 * If this function returns false, there was a failure and the
78 * periodic work is no longer scheduled
80 bool visor_periodic_work_nextperiod(struct periodic_work
*pw
)
84 write_lock(&pw
->lock
);
85 if (pw
->want_to_stop
) {
86 pw
->is_scheduled
= false;
87 pw
->want_to_stop
= false;
88 rc
= true; /* yes, true; see visor_periodic_work_stop() */
90 } else if (queue_delayed_work(pw
->workqueue
, &pw
->work
,
91 pw
->jiffy_interval
) < 0) {
92 pw
->is_scheduled
= false;
98 write_unlock(&pw
->lock
);
101 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod
);
103 /** This function returns true iff new periodic work was actually started.
104 * If this function returns false, then no work was started
105 * (either because it was already started, or because of a failure).
107 bool visor_periodic_work_start(struct periodic_work
*pw
)
111 write_lock(&pw
->lock
);
112 if (pw
->is_scheduled
) {
116 if (pw
->want_to_stop
) {
120 INIT_DELAYED_WORK(&pw
->work
, &periodic_work_func
);
121 if (queue_delayed_work(pw
->workqueue
, &pw
->work
,
122 pw
->jiffy_interval
) < 0) {
126 pw
->is_scheduled
= true;
129 write_unlock(&pw
->lock
);
132 EXPORT_SYMBOL_GPL(visor_periodic_work_start
);
134 /** This function returns true iff your call actually stopped the periodic
137 * -- PAY ATTENTION... this is important --
141 * Do NOT call this function from some function that is running on the
142 * same workqueue as the work you are trying to stop might be running
143 * on! If you violate this rule, visor_periodic_work_stop() MIGHT work,
144 * but it also MIGHT get hung up in an infinite loop saying
145 * "waiting for delayed work...". This will happen if the delayed work
146 * you are trying to cancel has been put in the workqueue list, but can't
147 * run yet because we are running that same workqueue thread right now.
149 * Bottom line: If you need to call visor_periodic_work_stop() from a
150 * workitem, be sure the workitem is on a DIFFERENT workqueue than the
151 * workitem that you are trying to cancel.
153 * If I could figure out some way to check for this "no no" condition in
154 * the code, I would. It would have saved me the trouble of writing this
155 * long comment. And also, don't think this is some "theoretical" race
156 * condition. It is REAL, as I have spent the day chasing it.
160 * Take close note of the locks that you own when you call this function.
161 * You must NOT own any locks that are needed by the periodic work
162 * function that is currently installed. If you DO, a deadlock may result,
163 * because stopping the periodic work often involves waiting for the last
164 * iteration of the periodic work function to complete. Again, if you hit
165 * this deadlock, you will get hung up in an infinite loop saying
166 * "waiting for delayed work...".
168 bool visor_periodic_work_stop(struct periodic_work
*pw
)
170 bool stopped_something
= false;
172 write_lock(&pw
->lock
);
173 stopped_something
= pw
->is_scheduled
&& (!pw
->want_to_stop
);
174 while (pw
->is_scheduled
) {
175 pw
->want_to_stop
= true;
176 if (cancel_delayed_work(&pw
->work
)) {
177 /* We get here if the delayed work was pending as
178 * delayed work, but was NOT run.
180 WARN_ON(!pw
->is_scheduled
);
181 pw
->is_scheduled
= false;
183 /* If we get here, either the delayed work:
185 * - is running RIGHT NOW on another processor, OR,
186 * - wasn't even scheduled (there is a miniscule
187 * timing window where this could be the case)
188 * flush_workqueue() would make sure it is finished
189 * executing, but that still isn't very useful, which
190 * explains the loop...
193 if (pw
->is_scheduled
) {
194 write_unlock(&pw
->lock
);
196 write_lock(&pw
->lock
);
198 pw
->want_to_stop
= false;
201 write_unlock(&pw
->lock
);
202 return stopped_something
;
204 EXPORT_SYMBOL_GPL(visor_periodic_work_stop
);