staging: unisys: remove BOOL,TRUE,FALSE definitions
[deliverable/linux.git] / drivers / staging / unisys / visorutil / periodic_work.c
1 /* periodic_work.c
2 *
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
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.
10 *
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
15 * details.
16 */
17
18 /*
19 * Helper functions to schedule periodic work in Linux kernel mode.
20 */
21
22 #include "timskmod.h"
23 #include "periodic_work.h"
24
25 #define MYDRVNAME "periodic_work"
26
27 struct periodic_work {
28 rwlock_t lock;
29 struct delayed_work work;
30 void (*workfunc)(void *);
31 void *workfuncarg;
32 bool is_scheduled;
33 bool want_to_stop;
34 ulong jiffy_interval;
35 struct workqueue_struct *workqueue;
36 const char *devnam;
37 };
38
39 static void periodic_work_func(struct work_struct *work)
40 {
41 struct periodic_work *pw;
42
43 pw = container_of(work, struct periodic_work, work.work);
44 (*pw->workfunc)(pw->workfuncarg);
45 }
46
47 struct periodic_work *visor_periodic_work_create(ulong jiffy_interval,
48 struct workqueue_struct *workqueue,
49 void (*workfunc)(void *),
50 void *workfuncarg,
51 const char *devnam)
52 {
53 struct periodic_work *pw;
54
55 pw = kzalloc(sizeof(*pw), GFP_KERNEL | __GFP_NORETRY);
56 if (!pw)
57 return NULL;
58
59 rwlock_init(&pw->lock);
60 pw->jiffy_interval = jiffy_interval;
61 pw->workqueue = workqueue;
62 pw->workfunc = workfunc;
63 pw->workfuncarg = workfuncarg;
64 pw->devnam = devnam;
65 return pw;
66 }
67 EXPORT_SYMBOL_GPL(visor_periodic_work_create);
68
69 void visor_periodic_work_destroy(struct periodic_work *pw)
70 {
71 kfree(pw);
72 }
73 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
74
75 /** Call this from your periodic work worker function to schedule the next
76 * call.
77 * If this function returns false, there was a failure and the
78 * periodic work is no longer scheduled
79 */
80 bool visor_periodic_work_nextperiod(struct periodic_work *pw)
81 {
82 bool rc = false;
83
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() */
89 goto unlock;
90 } else if (queue_delayed_work(pw->workqueue, &pw->work,
91 pw->jiffy_interval) < 0) {
92 pw->is_scheduled = false;
93 rc = false;
94 goto unlock;
95 }
96 rc = true;
97 unlock:
98 write_unlock(&pw->lock);
99 return rc;
100 }
101 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
102
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).
106 */
107 bool visor_periodic_work_start(struct periodic_work *pw)
108 {
109 bool rc = false;
110
111 write_lock(&pw->lock);
112 if (pw->is_scheduled) {
113 rc = false;
114 goto unlock;
115 }
116 if (pw->want_to_stop) {
117 rc = false;
118 goto unlock;
119 }
120 INIT_DELAYED_WORK(&pw->work, &periodic_work_func);
121 if (queue_delayed_work(pw->workqueue, &pw->work,
122 pw->jiffy_interval) < 0) {
123 rc = false;
124 goto unlock;
125 }
126 pw->is_scheduled = true;
127 rc = true;
128 unlock:
129 write_unlock(&pw->lock);
130 return rc;
131 }
132 EXPORT_SYMBOL_GPL(visor_periodic_work_start);
133
134 /** This function returns true iff your call actually stopped the periodic
135 * work.
136 *
137 * -- PAY ATTENTION... this is important --
138 *
139 * NO NO #1
140 *
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.
148 *
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.
152 *
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.
157 *
158 * NO NO #2
159 *
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...".
167 */
168 bool visor_periodic_work_stop(struct periodic_work *pw)
169 {
170 bool stopped_something = false;
171
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.
179 */
180 WARN_ON(!pw->is_scheduled);
181 pw->is_scheduled = false;
182 } else {
183 /* If we get here, either the delayed work:
184 * - was run, OR,
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...
191 */
192 }
193 if (pw->is_scheduled) {
194 write_unlock(&pw->lock);
195 SLEEPJIFFIES(10);
196 write_lock(&pw->lock);
197 } else {
198 pw->want_to_stop = false;
199 }
200 }
201 write_unlock(&pw->lock);
202 return stopped_something;
203 }
204 EXPORT_SYMBOL_GPL(visor_periodic_work_stop);
This page took 0.035868 seconds and 5 git commands to generate.