OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [include/] [mqueue.inl] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_KERNEL_MQUEUE_INL
2
#define CYGONCE_KERNEL_MQUEUE_INL
3
/*========================================================================
4
//
5
//      mqueue.inl
6
//
7
//      Message queues implementation
8
//
9
//========================================================================
10
// ####ECOSGPLCOPYRIGHTBEGIN####
11
// -------------------------------------------
12
// This file is part of eCos, the Embedded Configurable Operating System.
13
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
14
//
15
// eCos is free software; you can redistribute it and/or modify it under
16
// the terms of the GNU General Public License as published by the Free
17
// Software Foundation; either version 2 or (at your option) any later
18
// version.
19
//
20
// eCos is distributed in the hope that it will be useful, but WITHOUT
21
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23
// for more details.
24
//
25
// You should have received a copy of the GNU General Public License
26
// along with eCos; if not, write to the Free Software Foundation, Inc.,
27
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
28
//
29
// As a special exception, if other files instantiate templates or use
30
// macros or inline functions from this file, or you compile this file
31
// and link it with other works to produce a work based on this file,
32
// this file does not by itself cause the resulting work to be covered by
33
// the GNU General Public License. However the source code for this file
34
// must still be made available in accordance with section (3) of the GNU
35
// General Public License v2.
36
//
37
// This exception does not invalidate any other reasons why a work based
38
// on this file might be covered by the GNU General Public License.
39
// -------------------------------------------
40
// ####ECOSGPLCOPYRIGHTEND####
41
//========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):     jlarmour
45
// Contributors:
46
// Date:          2000-05-09
47
// Purpose:       This file provides the implementation for eCos message
48
//                queues
49
// Description:   This differs from the message boxes also supported
50
//                by eCos primarily because the requirements of message
51
//                queues are driven by POSIX semantics. POSIX semantics are
52
//                more dynamic and therefore heavyweight than Mboxes,
53
//                including prioritization, and variable sized queues and
54
//                message lengths
55
// Usage:         Do not include this file directly - instead
56
//                #include 
57
//
58
//####DESCRIPTIONEND####
59
//
60
//======================================================================
61
*/
62
 
63
/* CONFIGURATION */
64
 
65
#include 
66
#include           // Configuration header
67
 
68
/* INCLUDES */
69
 
70
#include                   // size_t, NULL
71
#include       // Types
72
#include      // Header for this file, just in case
73
#include        // Assertion support
74
#include       // Tracing support
75
#include       // scheduler
76
#include       // scheduler inlines
77
#include        // Cyg_Counting_Semaphore
78
 
79
#ifdef CYGPKG_ISOINFRA
80
# include                  // memcpy
81
#else
82
externC void * memcpy( void *, const void *, size_t );
83
#endif
84
 
85
// NOTE:
86
// An alternative implementation based on mutexes and condition variables
87
// rather than semaphores/scheduler locking was considered. But it was
88
// not thought quite as good because it isn't driver safe. You would
89
// also have to manage explicitly what counting semaphores do for you
90
// intrinsically. Also with the mutex approach, the message queue would
91
// be locked the whole time a new entry was being filled in, or copied out
92
//
93
// It also makes the non-blocking case properly non-blocking rather than
94
// still being able to block while waiting for a mutex protecting
95
// the message queue internal structures
96
 
97
/* INLINE FUNCTIONS */
98
 
99
#ifndef CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE
100
# define CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE inline
101
#endif
102
 
103
//------------------------------------------------------------------------
104
 
105
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE cyg_bool
106
Cyg_Mqueue::check_this( cyg_assert_class_zeal zeal ) const
107
{
108
    if (zeal != cyg_none) {
109
        CYG_CHECK_DATA_PTRC(this);  // extreme paranoia
110
 
111
#ifdef CYGDBG_USE_ASSERTS
112
        if ( qlen <= 0 || msgsize <= 0 )
113
            return false;
114
#endif
115
 
116
        if ( queuespacesize < sizeof(struct qentry)+1 )
117
            return false;
118
 
119
        CYG_CHECK_DATA_PTRC(queuespace);
120
        CYG_CHECK_FUNC_PTRC(free_fn);
121
 
122
        // prevent pre-emption through this. Not so bad since
123
        // this is only a diagnostic function
124
        Cyg_Scheduler::lock();
125
 
126
        if (NULL != q)
127
            CYG_CHECK_DATA_PTRC(q);
128
        if (NULL != freelist)
129
            CYG_CHECK_DATA_PTRC(freelist);
130
        if (NULL != callback)
131
            CYG_CHECK_FUNC_PTRC(callback);
132
 
133
        // check each queue entry
134
        long msgs=0, busymsgs=0;
135
        unsigned int oldprio=0;
136
        struct qentry *qtmp;
137
 
138
        if ( NULL != q )
139
            oldprio = q->priority;
140
        for ( qtmp=q; NULL != qtmp; qtmp=qtmp->next ) {
141
            if ( NULL != qtmp->next )
142
                CYG_CHECK_DATA_PTRC( qtmp->next );
143
 
144
            // queue should be priority ordered
145
            if ( qtmp->priority > oldprio )
146
                goto fail;
147
            oldprio = qtmp->priority;
148
 
149
#ifdef CYGDBG_USE_ASSERTS
150
            // valid length
151
            if ( !qtmp->busy )
152
                if ( qtmp->buflen > msgsize )
153
                    goto fail;
154
#endif
155
            if ( qtmp->busy )
156
                busymsgs++;
157
            else
158
                msgs++;
159
        } // for
160
 
161
        long freemsgs=0;
162
 
163
        // check that number of used and unused messages == q length
164
        for ( qtmp=freelist; NULL != qtmp; qtmp=qtmp->next ) {
165
            if ( NULL != qtmp->next )
166
                CYG_CHECK_DATA_PTRC( qtmp->next );
167
            if ( qtmp->busy )
168
                busymsgs++;
169
            else
170
                freemsgs++;
171
        }
172
 
173
#ifdef CYGDBG_USE_ASSERTS
174
        // and sum of all messages should be the total q length
175
        if ( qlen != (msgs+freemsgs+busymsgs) )
176
            goto fail;
177
#endif
178
 
179
        Cyg_Scheduler::unlock();
180
 
181
    }
182
    return true; // object OK
183
 fail:
184
    Cyg_Scheduler::unlock();
185
    return false; // object fubar'd
186
}
187
 
188
//------------------------------------------------------------------------
189
 
190
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE
191
Cyg_Mqueue::Cyg_Mqueue( long maxmsgs, long maxmsgsize,
192
                        qalloc_fn_t qalloc, qfree_fn_t qfree, qerr_t *err )
193
    : putsem(maxmsgs), getsem(0)
194
{
195
    CYG_REPORT_FUNCTION();
196
    CYG_REPORT_FUNCARG5( "maxmsgs=%ld, maxmsgsize=%ld, qalloc=%08x, "
197
                         "qfree=%08x, &err=%08x", maxmsgs, maxmsgsize,
198
                         qalloc, qfree, err);
199
    CYG_PRECONDITIONC( (maxmsgs > 0) && (maxmsgsize > 0) );
200
    CYG_CHECK_DATA_PTRC( err );
201
    CYG_CHECK_FUNC_PTRC( qalloc );
202
    CYG_CHECK_FUNC_PTRC( qfree );
203
 
204
    // mem to allocate for entire queue size. Also wants to be rounded
205
    // up so that the structs are aligned.
206
    const long addralign = sizeof(void *) - 1;
207
    long entrysize = (sizeof(struct qentry) + maxmsgsize + addralign)
208
       & ~addralign;
209
 
210
    queuespacesize = entrysize * maxmsgs;
211
    queuespace = qalloc( queuespacesize );
212
 
213
    if (NULL == queuespace) {
214
        *err=NOMEM;
215
        CYG_REPORT_RETURN();
216
        return;
217
    }
218
 
219
    // link up freelist
220
    long i;
221
    struct qentry *qtmp;
222
    for ( i=0, qtmp=(struct qentry *)queuespace;
223
          i
224
          i++, qtmp=qtmp->next ) {
225
        qtmp->busy = false;
226
        qtmp->next = (struct qentry *)((char *)qtmp + entrysize);
227
    } // for
228
 
229
    freelist   = (struct qentry *)queuespace;
230
 
231
    // set the last entry in the chain to the start to make the list circular
232
    qtmp->next = NULL;
233
    qtmp->busy = false;
234
    callback   = NULL;
235
    q          = NULL;
236
    free_fn    = qfree;
237
#ifdef CYGDBG_USE_ASSERTS
238
    qlen       = maxmsgs;
239
    msgsize    = maxmsgsize;
240
#endif
241
 
242
    *err = OK;
243
 
244
    // object should be valid now
245
    CYG_ASSERT_THISC();
246
 
247
    CYG_REPORT_RETURN();
248
}
249
 
250
//------------------------------------------------------------------------
251
 
252
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE
253
Cyg_Mqueue::~Cyg_Mqueue()
254
{
255
    CYG_REPORT_FUNCTION();
256
 
257
    if ( NULL != queuespace ) {
258
        // object should be valid if queuespace was successfully allocated
259
        CYG_ASSERT_THISC();
260
        free_fn( queuespace, queuespacesize );
261
    }
262
 
263
#ifdef CYGDBG_USE_ASSERTS
264
    qlen = msgsize = 0; // deliberately make it fail check_this() if used
265
#endif
266
 
267
    CYG_REPORT_RETURN();
268
}
269
 
270
//------------------------------------------------------------------------
271
 
272
// put() copies len bytes of *buf into the queue at priority prio
273
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE Cyg_Mqueue::qerr_t
274
Cyg_Mqueue::put( const char *buf, size_t len, unsigned int prio, bool block
275
#ifdef CYGFUN_KERNEL_THREADS_TIMER
276
                 , cyg_tick_count timeout
277
#endif
278
               )
279
{
280
    CYG_REPORT_FUNCTYPE( "err=%d");
281
    CYG_REPORT_FUNCARG4( "buf=%08x, len=%ld, prio=%ud, block=%d",
282
                         buf, len, prio, block==true );
283
    CYG_CHECK_DATA_PTRC( buf );
284
    CYG_ASSERT_THISC();
285
    CYG_PRECONDITIONC( len <= (size_t)msgsize );
286
 
287
    qerr_t err;
288
    struct qentry *qtmp, *qent;
289
 
290
    // wait till a freelist entry is available
291
    if ( true == block ) {
292
#ifdef CYGFUN_KERNEL_THREADS_TIMER
293
        if ( timeout != 0) {
294
            if ( false == putsem.wait(timeout) ) {
295
                err = TIMEOUT;
296
                goto exit;
297
            }
298
        }
299
        else
300
#endif
301
        if ( false == putsem.wait() ) {
302
            err = INTR;
303
            goto exit;
304
        }
305
    } else {
306
        if ( false == putsem.trywait() ) {
307
            err = WOULDBLOCK;
308
            goto exit;
309
        }
310
    }
311
 
312
    // prevent preemption when fiddling with important members
313
    Cyg_Scheduler::lock();
314
 
315
    CYG_ASSERT_THISC();
316
 
317
    // get a queue entry from the freelist
318
    // don't need to check the freelist - the semaphore tells us there's
319
    // definitely a usable non-busy one there. It's just a question of
320
    // locating it.
321
 
322
    if (!freelist->busy) { // fast-track common case
323
        qent     = freelist;
324
        freelist = freelist->next;
325
    } else {
326
        for ( qtmp=freelist; qtmp->next->busy; qtmp=qtmp->next )
327
            CYG_EMPTY_STATEMENT; // skip through
328
        qent       = qtmp->next;
329
        qtmp->next = qent->next;
330
    }
331
 
332
    // now put it in place in q
333
 
334
    if ( NULL == q ) {
335
        q = qent;
336
        q->next = NULL;
337
    } else {
338
        struct qentry **qentp;
339
 
340
        // insert into queue according to prio
341
        for ( qentp=&q; NULL != *qentp; qentp = &((*qentp)->next) ) {
342
            if ((*qentp)->priority < prio)
343
                break;
344
        } // for
345
 
346
        qent->next = *qentp;
347
        *qentp = qent;
348
    } // else
349
 
350
    qent->priority = prio; // have to set this now so when the sched is
351
                           // unlocked, other qent's can be added in the
352
                           // right place
353
    qent->busy = true; // let things know this entry should be ignored until
354
                       // it's finished having its data copied
355
 
356
    // unlock the scheduler, and potentially switch threads, but
357
    // that's okay now. We don't want it locked for the expensive memcpy
358
    Cyg_Scheduler::unlock();
359
 
360
    qent->buflen   = len;
361
    memcpy( qent->buf(), buf, len );
362
 
363
    // make available now - setting non-atomically is alright if you think
364
    // about it - the only thing that matters is that it's completed before
365
    // the post()
366
    qent->busy = false;
367
 
368
    // if we have to notify someone, we only do it if no-one's already
369
    // sitting waiting for a message to appear, AND if it's a transition
370
    // from empty to non-empty
371
 
372
    if ( callback != NULL && !getsem.waiting() && (0 == getsem.peek()) ) {
373
        getsem.post();
374
        callback( *this, callback_data );
375
    } else
376
        getsem.post();
377
 
378
    err = OK;
379
 
380
 exit:
381
    CYG_ASSERT_THISC();
382
    CYG_REPORT_RETVAL(err);
383
    return err;
384
} // Cyg_Mqueue::put()
385
 
386
//------------------------------------------------------------------------
387
 
388
 
389
// get() returns the oldest highest priority message in the queue in *buf
390
// and sets *prio to the priority (if prio is non-NULL) and *len to the
391
// actual message size
392
 
393
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE Cyg_Mqueue::qerr_t
394
Cyg_Mqueue::get( char *buf, size_t *len, unsigned int *prio, bool block
395
#ifdef CYGFUN_KERNEL_THREADS_TIMER
396
                 , cyg_tick_count timeout
397
#endif
398
               )
399
{
400
    CYG_REPORT_FUNCTYPE( "err=%d");
401
    CYG_REPORT_FUNCARG4( "buf=%08x, len=%08x, prio=%08x, block=%d",
402
                         buf, len, prio, block==true );
403
    CYG_CHECK_DATA_PTRC( buf );
404
    CYG_CHECK_DATA_PTRC( len );
405
    if ( NULL != prio )
406
        CYG_CHECK_DATA_PTRC( prio );
407
    CYG_ASSERT_THISC();
408
 
409
    qerr_t err;
410
    struct qentry *qent;
411
 
412
    // wait till a q entry is available
413
    if ( true == block ) {
414
#ifdef CYGFUN_KERNEL_THREADS_TIMER
415
        if ( timeout != 0) {
416
            if ( false == getsem.wait(timeout) ) {
417
                err = TIMEOUT;
418
                goto exit;
419
            }
420
        }
421
        else
422
#endif
423
        if ( false == getsem.wait() ) {
424
            err = INTR;
425
            goto exit;
426
        }
427
    } else {
428
        if ( false == getsem.trywait() ) {
429
            err = WOULDBLOCK;
430
            goto exit;
431
        }
432
    }
433
 
434
    // prevent preemption when fiddling with important members
435
 
436
    Cyg_Scheduler::lock();
437
 
438
    // don't need to check the q - the semaphore tells us there's
439
    // definitely a usable non-busy one there. It's just a question of
440
    // locating it.
441
 
442
    if ( !q->busy ) {   // fast-track the common case
443
        qent       = q;
444
        q          = qent->next;
445
    } else {
446
        struct qentry *qtmp;
447
 
448
        for ( qtmp=q; qtmp->next->busy; qtmp=qtmp->next )
449
            CYG_EMPTY_STATEMENT; // skip through
450
 
451
        qent = qtmp->next;
452
        qtmp->next = qent->next;
453
    } // else
454
 
455
    // now stick at front of freelist, but marked busy
456
    qent->next = freelist;
457
    freelist   = qent;
458
 
459
    qent->busy = true; // don't let it truly be part of the freelist just yet
460
                       // till the data is copied out
461
 
462
    // unlock the scheduler, and potentially switch threads, but
463
    // that's okay now. We don't want it locked for the expensive memcpy
464
    Cyg_Scheduler::unlock();
465
 
466
    *len  = qent->buflen;
467
    if ( NULL != prio )
468
        *prio = qent->priority;
469
    memcpy( buf, qent->buf(), *len );
470
 
471
    // make available now - setting non-atomically is alright if you think
472
    // about it - the only thing that matters is that it's completed before
473
    // the post()
474
    qent->busy = false;
475
 
476
    putsem.post();
477
 
478
    err = OK;
479
 
480
 exit:
481
    CYG_ASSERT_THISC();
482
    CYG_REPORT_RETVAL(err);
483
    return err;
484
 
485
} // Cyg_Mqueue::get()
486
 
487
//------------------------------------------------------------------------
488
 
489
// count() returns the number of messages in the queue
490
inline long
491
Cyg_Mqueue::count()
492
{
493
    CYG_REPORT_FUNCTYPE("curmsgs=%d");
494
 
495
    long curmsgs = (long)getsem.peek();
496
 
497
    CYG_REPORT_RETVAL(curmsgs);
498
    return curmsgs;
499
} // Cyg_Mqueue::count()
500
 
501
//------------------------------------------------------------------------
502
 
503
 
504
// Supply a callback function to call (with the supplied data argument)
505
// when the queue goes from empty to non-empty (unless someone's already
506
// doing a get()). This returns the old callback_fn, and if olddata is
507
// non-NULL sets it to the old data (yes, really!)
508
CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE Cyg_Mqueue::callback_fn_t
509
Cyg_Mqueue::setnotify( callback_fn_t callback_fn, CYG_ADDRWORD data,
510
                       CYG_ADDRWORD *olddata)
511
{
512
    CYG_REPORT_FUNCTYPE("old callback=%08x");
513
    CYG_REPORT_FUNCARG3XV( callback_fn, data, olddata );
514
    if ( NULL != callback_fn )
515
        CYG_CHECK_FUNC_PTRC( callback_fn );
516
    if (NULL != olddata)
517
        CYG_CHECK_DATA_PTRC( olddata );
518
 
519
    callback_fn_t oldfn;
520
 
521
    // Need to prevent preemption for accessing common structures
522
    // Just locking the scheduler has the least overhead
523
    Cyg_Scheduler::lock();
524
 
525
    oldfn = callback;
526
    if (NULL != olddata)
527
        *olddata = callback_data;
528
 
529
    callback_data = data;
530
    callback      = callback_fn;
531
 
532
    Cyg_Scheduler::unlock();
533
 
534
    CYG_REPORT_RETVAL(oldfn);
535
    return oldfn;
536
}
537
 
538
//------------------------------------------------------------------------
539
 
540
#endif /* CYGONCE_KERNEL_MQUEUE_INL multiple inclusion protection */
541
 
542
/* EOF mqueue.inl */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.