source: rtems-libbsd/rtemsbsd/src/rtems-bsd-taskqueue.c @ 8f6d129

4.115-freebsd-12freebsd-9.3
Last change on this file since 8f6d129 was 8f6d129, checked in by Jennifer Averett <jennifer.averett@…>, on Apr 16, 2012 at 5:27:49 PM

moved taskqueue_enqueue_fast from a define to a method to resolve linker errors.

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