source: rtems-libbsd/rtemsbsd/src/rtems-bsd-taskqueue.c @ 8420b94

4.115-freebsd-12freebsd-9.3
Last change on this file since 8420b94 was 8420b94, checked in by Jennifer Averett <jennifer.averett@…>, on May 8, 2012 at 2:14:42 PM

Modified copyright on rtems-bsd-xxx files to be consistant with FreeBSD copyright.

  • Property mode set to 100644
File size: 8.3 KB
Line 
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 <freebsd/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.
80struct 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
89struct 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
98STATIC 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
106struct taskqueue *taskqueue_fast = &the_taskqueue;
107struct taskqueue *taskqueue_swi = NULL;
108
109struct taskqueue *
110taskqueue_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
117struct taskqueue *
118taskqueue_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 */
127int
128taskqueue_enqueue(struct taskqueue *tq, struct task *ta)
129{
130rtems_interrupt_level l;
131
132rtems_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);
140rtems_interrupt_enable(l);
141        return 0;
142}
143
144int
145taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
146{
147  return taskqueue_enqueue(queue, task);
148}
149
150void
151taskqueue_thread_enqueue(void *ctxt)
152{
153int                   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 */
158struct 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 */
169int
170taskqueue_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
181void
182taskqueue_drain(struct taskqueue *tq, struct task *ta)
183{
184rtems_interrupt_level l;
185struct task *p, *q;
186int    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 */
218static void *
219taskqueue_work(struct taskqueue *tq)
220{
221rtems_interrupt_level l;
222struct task   *p, *q;
223task_fn        f;
224void        *arg;
225int            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 */
230rtems_interrupt_disable(l);
231        p = tq->anchor.ta_next;
232
233        tq->anchor.ta_next = 0;
234        tq->tail           = &tq->anchor;
235rtems_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
254void
255taskqueue_free(struct taskqueue *tq)
256{
257        taskqueue_work(tq);
258}
259
260static void
261taskqueueDoWork(void *arg)
262{
263struct taskqueue *tq = arg;
264rtems_event_set  evs;
265rtems_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
289struct task_dbg {
290        struct task t;
291        char        *nm;
292};
293
294struct task_dbg taskA = {
295        {0},
296        "taskA"
297};
298
299struct task_dbg taskB = {
300        {0},
301        "taskB"
302};
303
304struct task_dbg taskC = {
305        {0},
306        "taskC"
307};
308
309static void the_task_fn(void *arg, int pending)
310{
311struct 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
318void taskqueue_dump()
319{
320struct 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
329rtems_id
330rtems_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
343void
344_cexpModuleInitialize(void *u)
345{
346        rtems_bsdnet_initialize_network();
347        the_taskqueue.tid = rtems_taskqueue_initialize();
348}
349#endif
Note: See TracBrowser for help on using the repository browser.