summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/src/rtems-bsd-taskqueue.c
blob: 36bbf235e0e32063b3380be6c21f007740e07d8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * COPYRIGHT (c) 2012.
 * On-Line Applications Research Corporation (OAR).
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <rtems.h>
#include <rtems/error.h>
#include <freebsd/machine/rtems-bsd-taskqueue.h>

/*
#define STATIC static
*/
#undef  DEBUG

#ifdef DEBUG
#include <stdio.h>
#ifndef STATIC
#define STATIC
#endif
#else
#ifndef STATIC
#define STATIC static
#endif
#endif

#define TQ_WAKE_EVENT RTEMS_EVENT_0

/* This implementation is extremely simple; we assume
 * that all taskqueues (and as a matter of fact there is
 * only a single one) are manipulated with the rtems
 * bsdnet semaphore held. I.e.,
 *   taskqueue_enqueue()
 *   taskqueue_drain()
 *   etc.
 * are called from an environment that holds the
 * bsdnet semaphore.
 * Likewise, the thread that works the taskqueue
 * holds the semaphore while doing so.
 *
 */

/* use single-linked list; 'drain' which would benefit from
 * double-linked list is seldom used and performance doesn't
 * matter much there. OTOH, the frequent case of working
 * the list + enqueueing is more efficient for the single-linked
 * list.
struct task {
	struct task *ta_next;
	int		     ta_pending;
	int		     ta_priority;
	task_fn      ta_fn;
	void        *ta_fn_arg;
};
 */

struct taskqueue {
	struct task anchor;
	struct task *tail;
	tq_enq_fn   enq_fn;
	void       *enq_fn_arg;
	rtems_id    tid;
};


STATIC struct taskqueue the_taskqueue = {
	{ 0, 0, 0, 0, 0 },
	&the_taskqueue.anchor,
	taskqueue_thread_enqueue,
	&taskqueue_fast,
	0
};

struct taskqueue *taskqueue_fast = &the_taskqueue;
struct taskqueue *taskqueue_swi = NULL;

struct taskqueue *
taskqueue_create(const char *name, int mflags, tq_enq_fn enq_fn, void *arg)
{
	if ( enq_fn != taskqueue_thread_enqueue )
		rtems_panic("rtems_taskqueue: attempt to create non-standard TQ; implementation needs to be modified\n");
	return &the_taskqueue;
}

struct taskqueue *
taskqueue_create_fast(const char *name, int mflags, tq_enq_fn enq_fn, void *arg)
{
	return taskqueue_create(name, mflags, enq_fn, arg);
}

/* taskqueue_enqueue must be allowed from an ISR;
 * hence, all critical list manipulation must lock out
 * interrupts...
 */
int
taskqueue_enqueue(struct taskqueue *tq, struct task *ta)
{
rtems_interrupt_level l;

rtems_interrupt_disable(l);
	if ( 0 == ta->ta_pending ++ ) {
		/* hook into list */
		ta->ta_next       = 0;
		tq->tail->ta_next = ta;
		tq->tail          = ta;
	}
	tq->enq_fn(tq->enq_fn_arg);
rtems_interrupt_enable(l);
	return 0;
}

int
taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
{
  return taskqueue_enqueue(queue, task);
}

void
taskqueue_thread_enqueue(void *ctxt)
{
int                   dopost;
/* pointer-to-pointer is what bsd provides; we currently
 * follow the scheme even we don't directly use the argument
 * passed to taskqueue_create...
 */
struct taskqueue *tq = *(struct taskqueue **)ctxt;
	/* If this is the first entry on the list then the
	 * task needs to be notified...
	 */
	dopost = ( tq->anchor.ta_next == tq->tail && 1 == tq->tail->ta_pending );

	if ( dopost )
		rtems_event_send(tq->tid, TQ_WAKE_EVENT);
}

/* Returns 0 on success */
int
taskqueue_start_threads(struct taskqueue **ptq, int count, int prio, const char *fmt, ...)
{
	if ( count != 1 )
		rtems_panic("rtems_taskqueue: taskqueue_start_threads cannot currently deal with count != 1\n");
	
	/* Do (non thread-safe) lazy init as a fallback */
	if ( ! the_taskqueue.tid )
		rtems_taskqueue_initialize();
	return 0;
}

void
taskqueue_drain(struct taskqueue *tq, struct task *ta)
{
rtems_interrupt_level l;
struct task *p, *q;
int    i;

	/* find predecessor; searching the list should be
	 * safe; an ISR might append a new record to the tail
	 * while we are working but that should be OK.
	 */
	for ( p = &tq->anchor; (q = p->ta_next); p=q ) {
		if ( q == ta ) {
		rtems_interrupt_disable(l);
			/* found; do work */
			/* remember 'pending' count and extract */
			i              = ta->ta_pending;
			ta->ta_pending = 0;
			p->ta_next     = ta->ta_next;
			ta->ta_next    = 0;
			/* adjust tail */
			if ( tq->tail == q )
				tq->tail = p;
		rtems_interrupt_enable(l);
			for ( ; i>0; i-- ) {
				ta->ta_fn(ta->ta_fn_arg, i);
			}
			return;
		}
	}
}

/* work the task queue and return
 * nonzero if the list is not empty
 * (which means that some callback has
 * rescheduled itself)
 */
static void *
taskqueue_work(struct taskqueue *tq)
{
rtems_interrupt_level l;
struct task   *p, *q;
task_fn        f;
void        *arg;
int            i;

/* work off a temporary list in case any callback reschedules
 * itself or if new tasks are queued from an ISR.
 */
rtems_interrupt_disable(l);
	p = tq->anchor.ta_next;

	tq->anchor.ta_next = 0;
	tq->tail           = &tq->anchor;
rtems_interrupt_enable(l);

	while ( (q=p) ) {
	rtems_interrupt_disable(l);
		i = q->ta_pending;
		q->ta_pending = 0;
		/* extract */
		p          = q->ta_next;
		q->ta_next = 0;
		f          = q->ta_fn;
		arg        = q->ta_fn_arg;
	rtems_interrupt_enable(l);
		for ( ; i>0; i-- ) {
			f(arg, i);
		}
	}
	return tq->anchor.ta_next;
}

void
taskqueue_free(struct taskqueue *tq)
{
	taskqueue_work(tq);
}

static void
taskqueueDoWork(void *arg)
{
struct taskqueue *tq = arg;
rtems_event_set  evs;
rtems_status_code sc;
	while ( 1 ) {
		sc = rtems_event_receive(TQ_WAKE_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, RTEMS_NO_TIMEOUT, &evs);
		if ( RTEMS_SUCCESSFUL != sc ) {
			rtems_error(sc,"rtems_taskqueue: taskqueueDoWork() unable to receive wakup event\n");
			rtems_panic("Can't proceed\n");
		}
		if ( taskqueue_work(tq) ) {
#if 0
			/* chance to reschedule */
			rtems_bsdnet_semaphore_release();
			rtems_task_wake_after(0);
			rtems_bsdnet_semaphore_obtain();
#else
			/* hopefully, releasing the semaphore (as part of bsdnet_event_receive)
			 * and obtaining the event (which has been posted already)
			 * yields the CPU if necessary...
			 */
#endif
		}
	}
}

#ifdef DEBUG
struct task_dbg {
	struct task t;
	char        *nm;
};

struct task_dbg taskA = {
	{0},
	"taskA"
};

struct task_dbg taskB = {
	{0},
	"taskB"
};

struct task_dbg taskC = {
	{0},
	"taskC"
};

static void the_task_fn(void *arg, int pending)
{
struct task_dbg *td = arg;
	printf("%s (pending: %i)\n", td->nm, pending);
	/* Test rescheduling */
	if ( pending > 3 )
		taskqueue_enqueue(&the_taskqueue,&td->t);
}

void taskqueue_dump()
{
struct task *p;
	printf("Anchor %p, Tail %p\n", &the_taskqueue.anchor, the_taskqueue.tail);
	for ( p = the_taskqueue.anchor.ta_next; p; p=p->ta_next ) {
		printf("%p: (pending %2i, next %p)\n",
			p, p->ta_pending, p->ta_next);
	}
}
#endif

rtems_id
rtems_taskqueue_initialize()
{
#ifdef DEBUG
	TASK_INIT( &taskA.t, 0, the_task_fn, &taskA );
	TASK_INIT( &taskB.t, 0, the_task_fn, &taskB );
	TASK_INIT( &taskC.t, 0, the_task_fn, &taskC );
#endif
	if ( ! the_taskqueue.tid )
		the_taskqueue.tid = rtems_bsdnet_newproc("tskq", 10000, taskqueueDoWork, &the_taskqueue);
	return the_taskqueue.tid;
}

#ifdef DEBUG
void
_cexpModuleInitialize(void *u)
{
	rtems_bsdnet_initialize_network();
	the_taskqueue.tid = rtems_taskqueue_initialize();
}
#endif