source: libbsdport/bsd_eth_drivers/libbsdport/rtems_taskqueue.c @ 28a6f92

Last change on this file since 28a6f92 was 28a6f92, checked in by Sebastian Huber <sebastian.huber@…>, on Feb 13, 2013 at 8:46:25 AM

Avoid rtems_bsdnet_event_receive()

RTEMS 4.11 will use system events inside the network stack. Use normal
events for CALLOUT(9) and TASKQUEUE(9) implementations.

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