1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 2012. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * All Rights Reserved. |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
33 | * SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #include <sys/cdefs.h> |
---|
37 | __FBSDID("$FreeBSD$"); |
---|
38 | |
---|
39 | #include <rtems.h> |
---|
40 | #include <rtems/error.h> |
---|
41 | #include <machine/rtems-bsd-taskqueue.h> |
---|
42 | |
---|
43 | /* |
---|
44 | #define STATIC static |
---|
45 | */ |
---|
46 | #undef DEBUG |
---|
47 | |
---|
48 | #ifdef DEBUG |
---|
49 | #include <stdio.h> |
---|
50 | #ifndef STATIC |
---|
51 | #define STATIC |
---|
52 | #endif |
---|
53 | #else |
---|
54 | #ifndef STATIC |
---|
55 | #define STATIC static |
---|
56 | #endif |
---|
57 | #endif |
---|
58 | |
---|
59 | #define TQ_WAKE_EVENT RTEMS_EVENT_0 |
---|
60 | |
---|
61 | /* This implementation is extremely simple; we assume |
---|
62 | * that all taskqueues (and as a matter of fact there is |
---|
63 | * only a single one) are manipulated with the rtems |
---|
64 | * bsdnet semaphore held. I.e., |
---|
65 | * taskqueue_enqueue() |
---|
66 | * taskqueue_drain() |
---|
67 | * etc. |
---|
68 | * are called from an environment that holds the |
---|
69 | * bsdnet semaphore. |
---|
70 | * Likewise, the thread that works the taskqueue |
---|
71 | * holds the semaphore while doing so. |
---|
72 | * |
---|
73 | */ |
---|
74 | |
---|
75 | /* use single-linked list; 'drain' which would benefit from |
---|
76 | * double-linked list is seldom used and performance doesn't |
---|
77 | * matter much there. OTOH, the frequent case of working |
---|
78 | * the list + enqueueing is more efficient for the single-linked |
---|
79 | * list. |
---|
80 | struct task { |
---|
81 | struct task *ta_next; |
---|
82 | int ta_pending; |
---|
83 | int ta_priority; |
---|
84 | task_fn ta_fn; |
---|
85 | void *ta_fn_arg; |
---|
86 | }; |
---|
87 | */ |
---|
88 | |
---|
89 | struct taskqueue { |
---|
90 | struct task anchor; |
---|
91 | struct task *tail; |
---|
92 | tq_enq_fn enq_fn; |
---|
93 | void *enq_fn_arg; |
---|
94 | rtems_id tid; |
---|
95 | }; |
---|
96 | |
---|
97 | |
---|
98 | STATIC struct taskqueue the_taskqueue = { |
---|
99 | { 0, 0, 0, 0, 0 }, |
---|
100 | &the_taskqueue.anchor, |
---|
101 | taskqueue_thread_enqueue, |
---|
102 | &taskqueue_fast, |
---|
103 | 0 |
---|
104 | }; |
---|
105 | |
---|
106 | struct taskqueue *taskqueue_fast = &the_taskqueue; |
---|
107 | struct taskqueue *taskqueue_swi = NULL; |
---|
108 | |
---|
109 | struct taskqueue * |
---|
110 | taskqueue_create(const char *name, int mflags, tq_enq_fn enq_fn, void *arg) |
---|
111 | { |
---|
112 | if ( enq_fn != taskqueue_thread_enqueue ) |
---|
113 | rtems_panic("rtems_taskqueue: attempt to create non-standard TQ; implementation needs to be modified\n"); |
---|
114 | return &the_taskqueue; |
---|
115 | } |
---|
116 | |
---|
117 | struct taskqueue * |
---|
118 | taskqueue_create_fast(const char *name, int mflags, tq_enq_fn enq_fn, void *arg) |
---|
119 | { |
---|
120 | return taskqueue_create(name, mflags, enq_fn, arg); |
---|
121 | } |
---|
122 | |
---|
123 | /* taskqueue_enqueue must be allowed from an ISR; |
---|
124 | * hence, all critical list manipulation must lock out |
---|
125 | * interrupts... |
---|
126 | */ |
---|
127 | int |
---|
128 | taskqueue_enqueue(struct taskqueue *tq, struct task *ta) |
---|
129 | { |
---|
130 | rtems_interrupt_level l; |
---|
131 | |
---|
132 | rtems_interrupt_disable(l); |
---|
133 | if ( 0 == ta->ta_pending ++ ) { |
---|
134 | /* hook into list */ |
---|
135 | ta->ta_next = 0; |
---|
136 | tq->tail->ta_next = ta; |
---|
137 | tq->tail = ta; |
---|
138 | } |
---|
139 | tq->enq_fn(tq->enq_fn_arg); |
---|
140 | rtems_interrupt_enable(l); |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | int |
---|
145 | taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task) |
---|
146 | { |
---|
147 | return taskqueue_enqueue(queue, task); |
---|
148 | } |
---|
149 | |
---|
150 | void |
---|
151 | taskqueue_thread_enqueue(void *ctxt) |
---|
152 | { |
---|
153 | int dopost; |
---|
154 | /* pointer-to-pointer is what bsd provides; we currently |
---|
155 | * follow the scheme even we don't directly use the argument |
---|
156 | * passed to taskqueue_create... |
---|
157 | */ |
---|
158 | struct taskqueue *tq = *(struct taskqueue **)ctxt; |
---|
159 | /* If this is the first entry on the list then the |
---|
160 | * task needs to be notified... |
---|
161 | */ |
---|
162 | dopost = ( tq->anchor.ta_next == tq->tail && 1 == tq->tail->ta_pending ); |
---|
163 | |
---|
164 | if ( dopost ) |
---|
165 | rtems_event_send(tq->tid, TQ_WAKE_EVENT); |
---|
166 | } |
---|
167 | |
---|
168 | /* Returns 0 on success */ |
---|
169 | int |
---|
170 | taskqueue_start_threads(struct taskqueue **ptq, int count, int prio, const char *fmt, ...) |
---|
171 | { |
---|
172 | if ( count != 1 ) |
---|
173 | rtems_panic("rtems_taskqueue: taskqueue_start_threads cannot currently deal with count != 1\n"); |
---|
174 | |
---|
175 | /* Do (non thread-safe) lazy init as a fallback */ |
---|
176 | if ( ! the_taskqueue.tid ) |
---|
177 | rtems_taskqueue_initialize(); |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | void |
---|
182 | taskqueue_drain(struct taskqueue *tq, struct task *ta) |
---|
183 | { |
---|
184 | rtems_interrupt_level l; |
---|
185 | struct task *p, *q; |
---|
186 | int i; |
---|
187 | |
---|
188 | /* find predecessor; searching the list should be |
---|
189 | * safe; an ISR might append a new record to the tail |
---|
190 | * while we are working but that should be OK. |
---|
191 | */ |
---|
192 | for ( p = &tq->anchor; (q = p->ta_next); p=q ) { |
---|
193 | if ( q == ta ) { |
---|
194 | rtems_interrupt_disable(l); |
---|
195 | /* found; do work */ |
---|
196 | /* remember 'pending' count and extract */ |
---|
197 | i = ta->ta_pending; |
---|
198 | ta->ta_pending = 0; |
---|
199 | p->ta_next = ta->ta_next; |
---|
200 | ta->ta_next = 0; |
---|
201 | /* adjust tail */ |
---|
202 | if ( tq->tail == q ) |
---|
203 | tq->tail = p; |
---|
204 | rtems_interrupt_enable(l); |
---|
205 | for ( ; i>0; i-- ) { |
---|
206 | ta->ta_fn(ta->ta_fn_arg, i); |
---|
207 | } |
---|
208 | return; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | /* work the task queue and return |
---|
214 | * nonzero if the list is not empty |
---|
215 | * (which means that some callback has |
---|
216 | * rescheduled itself) |
---|
217 | */ |
---|
218 | static void * |
---|
219 | taskqueue_work(struct taskqueue *tq) |
---|
220 | { |
---|
221 | rtems_interrupt_level l; |
---|
222 | struct task *p, *q; |
---|
223 | task_fn f; |
---|
224 | void *arg; |
---|
225 | int i; |
---|
226 | |
---|
227 | /* work off a temporary list in case any callback reschedules |
---|
228 | * itself or if new tasks are queued from an ISR. |
---|
229 | */ |
---|
230 | rtems_interrupt_disable(l); |
---|
231 | p = tq->anchor.ta_next; |
---|
232 | |
---|
233 | tq->anchor.ta_next = 0; |
---|
234 | tq->tail = &tq->anchor; |
---|
235 | rtems_interrupt_enable(l); |
---|
236 | |
---|
237 | while ( (q=p) ) { |
---|
238 | rtems_interrupt_disable(l); |
---|
239 | i = q->ta_pending; |
---|
240 | q->ta_pending = 0; |
---|
241 | /* extract */ |
---|
242 | p = q->ta_next; |
---|
243 | q->ta_next = 0; |
---|
244 | f = q->ta_fn; |
---|
245 | arg = q->ta_fn_arg; |
---|
246 | rtems_interrupt_enable(l); |
---|
247 | for ( ; i>0; i-- ) { |
---|
248 | f(arg, i); |
---|
249 | } |
---|
250 | } |
---|
251 | return tq->anchor.ta_next; |
---|
252 | } |
---|
253 | |
---|
254 | void |
---|
255 | taskqueue_free(struct taskqueue *tq) |
---|
256 | { |
---|
257 | taskqueue_work(tq); |
---|
258 | } |
---|
259 | |
---|
260 | static void |
---|
261 | taskqueueDoWork(void *arg) |
---|
262 | { |
---|
263 | struct taskqueue *tq = arg; |
---|
264 | rtems_event_set evs; |
---|
265 | rtems_status_code sc; |
---|
266 | while ( 1 ) { |
---|
267 | sc = rtems_event_receive(TQ_WAKE_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, RTEMS_NO_TIMEOUT, &evs); |
---|
268 | if ( RTEMS_SUCCESSFUL != sc ) { |
---|
269 | rtems_error(sc,"rtems_taskqueue: taskqueueDoWork() unable to receive wakup event\n"); |
---|
270 | rtems_panic("Can't proceed\n"); |
---|
271 | } |
---|
272 | if ( taskqueue_work(tq) ) { |
---|
273 | #if 0 |
---|
274 | /* chance to reschedule */ |
---|
275 | rtems_bsdnet_semaphore_release(); |
---|
276 | rtems_task_wake_after(0); |
---|
277 | rtems_bsdnet_semaphore_obtain(); |
---|
278 | #else |
---|
279 | /* hopefully, releasing the semaphore (as part of bsdnet_event_receive) |
---|
280 | * and obtaining the event (which has been posted already) |
---|
281 | * yields the CPU if necessary... |
---|
282 | */ |
---|
283 | #endif |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | #ifdef DEBUG |
---|
289 | struct task_dbg { |
---|
290 | struct task t; |
---|
291 | char *nm; |
---|
292 | }; |
---|
293 | |
---|
294 | struct task_dbg taskA = { |
---|
295 | {0}, |
---|
296 | "taskA" |
---|
297 | }; |
---|
298 | |
---|
299 | struct task_dbg taskB = { |
---|
300 | {0}, |
---|
301 | "taskB" |
---|
302 | }; |
---|
303 | |
---|
304 | struct task_dbg taskC = { |
---|
305 | {0}, |
---|
306 | "taskC" |
---|
307 | }; |
---|
308 | |
---|
309 | static void the_task_fn(void *arg, int pending) |
---|
310 | { |
---|
311 | struct task_dbg *td = arg; |
---|
312 | printf("%s (pending: %i)\n", td->nm, pending); |
---|
313 | /* Test rescheduling */ |
---|
314 | if ( pending > 3 ) |
---|
315 | taskqueue_enqueue(&the_taskqueue,&td->t); |
---|
316 | } |
---|
317 | |
---|
318 | void taskqueue_dump() |
---|
319 | { |
---|
320 | struct task *p; |
---|
321 | printf("Anchor %p, Tail %p\n", &the_taskqueue.anchor, the_taskqueue.tail); |
---|
322 | for ( p = the_taskqueue.anchor.ta_next; p; p=p->ta_next ) { |
---|
323 | printf("%p: (pending %2i, next %p)\n", |
---|
324 | p, p->ta_pending, p->ta_next); |
---|
325 | } |
---|
326 | } |
---|
327 | #endif |
---|
328 | |
---|
329 | rtems_id |
---|
330 | rtems_taskqueue_initialize() |
---|
331 | { |
---|
332 | #ifdef DEBUG |
---|
333 | TASK_INIT( &taskA.t, 0, the_task_fn, &taskA ); |
---|
334 | TASK_INIT( &taskB.t, 0, the_task_fn, &taskB ); |
---|
335 | TASK_INIT( &taskC.t, 0, the_task_fn, &taskC ); |
---|
336 | #endif |
---|
337 | if ( ! the_taskqueue.tid ) |
---|
338 | the_taskqueue.tid = rtems_bsdnet_newproc("tskq", 10000, taskqueueDoWork, &the_taskqueue); |
---|
339 | return the_taskqueue.tid; |
---|
340 | } |
---|
341 | |
---|
342 | #ifdef DEBUG |
---|
343 | void |
---|
344 | _cexpModuleInitialize(void *u) |
---|
345 | { |
---|
346 | rtems_bsdnet_initialize_network(); |
---|
347 | the_taskqueue.tid = rtems_taskqueue_initialize(); |
---|
348 | } |
---|
349 | #endif |
---|