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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [mqueue1.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
/*========================================================================
2
//
3
//      mqueue1.cxx
4
//
5
//      Message queues tests
6
//
7
//========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later
16
// version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
//
27
// As a special exception, if other files instantiate templates or use
28
// macros or inline functions from this file, or you compile this file
29
// and link it with other works to produce a work based on this file,
30
// this file does not by itself cause the resulting work to be covered by
31
// the GNU General Public License. However the source code for this file
32
// must still be made available in accordance with section (3) of the GNU
33
// General Public License v2.
34
//
35
// This exception does not invalidate any other reasons why a work based
36
// on this file might be covered by the GNU General Public License.
37
// -------------------------------------------
38
// ####ECOSGPLCOPYRIGHTEND####
39
//========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     jlarmour
43
// Contributors:
44
// Date:          2000-05-12
45
// Purpose:       This file provides tests for eCos mqueues
46
// Description:
47
// Usage:
48
//
49
//####DESCRIPTIONEND####
50
//
51
//======================================================================
52
*/
53
 
54
/* CONFIGURATION */
55
 
56
#include <pkgconf/kernel.h>
57
 
58
/* INCLUDES */
59
 
60
#include <cyg/infra/cyg_type.h>      // common types and externC
61
#include <cyg/kernel/thread.hxx>     // Cyg_Thread
62
#include <cyg/kernel/thread.inl>
63
// Specially avoid inlining here due to the way we abuse the mqueue
64
// implementation by making lots and lots of calls.
65
#define CYGPRI_KERNEL_SYNCH_MQUEUE_INLINE
66
#include <cyg/kernel/mqueue.hxx>     // Mqueue Header
67
#include <cyg/kernel/sema.hxx>       // semaphores
68
#include <cyg/infra/testcase.h>      // test API
69
 
70
// use the common kernel test magic to define 2 threads
71
#define NTHREADS 2
72
#include "testaux.hxx"
73
 
74
/* GLOBALS */
75
 
76
static char mempool[500];
77
static size_t storedmempoollen;
78
Cyg_Mqueue *mq;
79
static Cyg_Binary_Semaphore t0sem, t1sem;
80
static int calledback;
81
 
82
 
83
/* FUNCTIONS */
84
 
85
static int
86
my_memcmp(const void *m1, const void *m2, size_t n)
87
{
88
    char *s1 = (char *)m1;
89
    char *s2 = (char *)m2;
90
 
91
    while (n--) {
92
        if (*s1 != *s2)
93
            return *s1 - *s2;
94
        s1++;
95
        s2++;
96
    }
97
    return 0;
98
} // my_memcmp()
99
 
100
static void *
101
my_alloc( size_t len )
102
{
103
    if ( len > sizeof(mempool) )
104
        return NULL;
105
 
106
    storedmempoollen = len;
107
    return &mempool[0];
108
}
109
 
110
static void
111
my_free( void *ptr, size_t len )
112
{
113
    CYG_TEST_PASS_FAIL( (ptr == &mempool[0]) && (len == storedmempoollen),
114
                        "Freed pool correctly");
115
    mq = NULL; // invalidate
116
}
117
 
118
static void
119
callback(Cyg_Mqueue &mq, CYG_ADDRWORD data)
120
{
121
    calledback += (int)data;
122
}
123
 
124
//************************************************************************
125
//************************************************************************
126
 
127
static void
128
t0( CYG_ADDRWORD data )
129
{
130
    Cyg_Mqueue::qerr_t err;
131
    char buf[35];
132
    size_t len;
133
    unsigned int prio;
134
    bool b;
135
 
136
    Cyg_Mqueue the_mq(4, 32, &my_alloc, &my_free, &err );
137
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
138
                        "Create queue" );
139
    mq = &the_mq;
140
 
141
//------------------------------------------------------------------------
142
 
143
    err = mq->put( "Peter piper picked", sizeof("Peter piper picked"),
144
                   5, true );
145
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, "Simple (put)");
146
 
147
    t1sem.post();
148
 
149
//------------------------------------------------------------------------
150
 
151
    t0sem.wait();
152
 
153
    err = mq->get( buf, &len, &prio, true );
154
    b = (err == Cyg_Mqueue::OK);
155
    if (b)
156
        b = (len == sizeof("a peck of"));
157
    if (b)
158
        b = (prio == 100);
159
    if (b)
160
        b = (0 ==
161
             my_memcmp(buf, "a peck of", sizeof("a peck of"))
162
            );
163
    CYG_TEST_PASS_FAIL( b, "Blocking get");
164
 
165
//------------------------------------------------------------------------
166
 
167
    t0sem.wait();
168
 
169
    CYG_TEST_PASS_FAIL( 4 == mq->count(), "mq count" );
170
    err = mq->get( buf, &len, &prio, false );
171
    b = (err == Cyg_Mqueue::OK);
172
    if (b)
173
        b = (len == sizeof("pickled peppers"));
174
    if (b)
175
        b = (prio == 300);
176
    if (b)
177
        b = (0 ==
178
             my_memcmp(buf, "pickled peppers", sizeof("pickled peppers"))
179
            );
180
    if (b)
181
        b = (3 == mq->count());
182
    CYG_TEST_PASS_FAIL( b, "Prioritized (get 1)");
183
 
184
    err = mq->get( buf, &len, &prio, false );
185
    b = (err == Cyg_Mqueue::OK);
186
    if (b)
187
        b = (len == sizeof("."));
188
    if (b)
189
        b = (prio == 250);
190
    if (b)
191
        b = (0 ==
192
             my_memcmp(buf, ".", sizeof("."))
193
            );
194
    if (b)
195
        b = (2 == mq->count());
196
    CYG_TEST_PASS_FAIL( b, "Prioritized (get 2)");
197
 
198
    err = mq->get( buf, &len, &prio, false );
199
    b = (err == Cyg_Mqueue::OK);
200
    if (b)
201
        b = (len == 1);
202
    if (b)
203
        b = (prio == 225);
204
    if (b)
205
        b = (0 ==
206
             my_memcmp(buf, "", 1)
207
            );
208
    if (b)
209
        b = (1 == mq->count());
210
    CYG_TEST_PASS_FAIL( b, "Prioritized (get 3)");
211
 
212
    err = mq->get( buf, &len, &prio, false );
213
    b = (err == Cyg_Mqueue::OK);
214
    if (b)
215
        b = (len == sizeof("If Peter"));
216
    if (b)
217
        b = (prio == 200);
218
    if (b)
219
        b = (0 ==
220
             my_memcmp(buf, "If Peter", sizeof("If Peter"))
221
            );
222
    if (b)
223
        b = (0 == mq->count());
224
    CYG_TEST_PASS_FAIL( b, "Prioritized (get 4)");
225
 
226
//------------------------------------------------------------------------
227
 
228
    err = mq->get( buf, &len, &prio, false );
229
 
230
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::WOULDBLOCK == err,
231
                        "Non-blocking get of empty queue" );
232
 
233
//------------------------------------------------------------------------
234
 
235
    Cyg_Mqueue::callback_fn_t oldcallback;
236
 
237
    oldcallback = mq->setnotify( &callback, (CYG_ADDRWORD) 42 );
238
 
239
    err = mq->put( "If Peter", sizeof("If Peter"),
240
                   200, false );
241
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
242
                        "Prioritized (put in empty queue)");
243
    CYG_TEST_PASS_FAIL( 42 == calledback, "callback" );
244
    CYG_TEST_PASS_FAIL( NULL == oldcallback, "oldcallback" );
245
 
246
    err = mq->get( buf, &len, &prio, false );
247
    b = (err == Cyg_Mqueue::OK);
248
    if (b)
249
        b = (len == sizeof("If Peter"));
250
    if (b)
251
        b = (prio == 200);
252
    if (b)
253
        b = (0 ==
254
             my_memcmp(buf, "If Peter", sizeof("If Peter"))
255
            );
256
    CYG_TEST_PASS_FAIL( b, "Prioritized (get 2)");
257
 
258
    t1sem.post();
259
 
260
    err = mq->get( buf, &len, &prio, true );
261
    b = (err == Cyg_Mqueue::OK);
262
    if (b)
263
        b = (len == 32);
264
    if (b)
265
        b = (42 == calledback);
266
    if (b)
267
        b = (prio == 250);
268
    if (b)
269
        b = (0 ==
270
             my_memcmp(buf, "12345678901234567890123456789012", 32)
271
            );
272
    CYG_TEST_PASS_FAIL( b, "callback (blocked wait)");
273
 
274
//------------------------------------------------------------------------
275
 
276
    t1sem.post();
277
    t0sem.wait();
278
 
279
} // t0()
280
 
281
 
282
//************************************************************************
283
//************************************************************************
284
 
285
 
286
static void
287
t1( CYG_ADDRWORD data )
288
{
289
    Cyg_Mqueue::qerr_t err;
290
    char buf[35];
291
    size_t len;
292
    unsigned int prio;
293
    bool b;
294
 
295
//------------------------------------------------------------------------
296
 
297
    // wait till t0 says we can go
298
    t1sem.wait();
299
 
300
    err = mq->get( buf, &len, &prio, true );
301
    b = (err == Cyg_Mqueue::OK);
302
    if (b)
303
        b = (len == sizeof("Peter piper picked"));
304
    if (b)
305
        b = (prio == 5);
306
    if (b)
307
        b = (0 ==
308
             my_memcmp(buf, "Peter piper picked", sizeof("Peter piper picked"))
309
            );
310
 
311
    CYG_TEST_PASS_FAIL( b, "Simple");
312
 
313
//------------------------------------------------------------------------
314
 
315
    t0sem.post();         // t0 should run straight away
316
    Cyg_Thread::yield();  // but just in case we have a funny sched
317
 
318
    // by now t0 is blocked in mq->get
319
 
320
    err = mq->put( "a peck of", sizeof("a peck of"),
321
                   100, false );
322
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err, "Block (put in empty queue)");
323
 
324
//------------------------------------------------------------------------
325
 
326
    err = mq->put( "If Peter", sizeof("If Peter"),
327
                   200, false );
328
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
329
                        "Prioritized (put in empty queue)");
330
 
331
    err = mq->put( "pickled peppers", sizeof("pickled peppers"),
332
                   300, false );
333
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
334
                        "Prioritized (put in queue w/1)");
335
 
336
    err = mq->put( ".", sizeof("."), 250, false );
337
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
338
                        "Prioritized (put in queue w/2)");
339
 
340
    err = mq->put( "", 1, 225, false );
341
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
342
                        "Prioritized (put in queue w/3)");
343
 
344
    err = mq->put( "foobar", 6, 1, false );
345
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::WOULDBLOCK == err,
346
                        "Prioritized (full queue)");
347
 
348
    t0sem.post();
349
 
350
//------------------------------------------------------------------------
351
 
352
    t1sem.wait();
353
    Cyg_Thread::yield();  // but just in case we have a funny sched
354
 
355
    err = mq->put( "12345678901234567890123456789012xxxx", 32,
356
                   250, false );
357
    CYG_TEST_PASS_FAIL( Cyg_Mqueue::OK == err,
358
                        "callback (put in queue)");
359
 
360
//------------------------------------------------------------------------
361
 
362
    t1sem.wait();
363
 
364
    // Create an oversized queue
365
    {
366
        Cyg_Mqueue huge_mq(99999, 99999, &my_alloc, &my_free, &err );
367
        CYG_TEST_PASS_FAIL( Cyg_Mqueue::NOMEM == err,
368
                            "Oversized queue rejected" );
369
        // and it now gets destructed - but that shouldn't call free
370
        // to be called
371
    }
372
 
373
//------------------------------------------------------------------------
374
 
375
    t0sem.post();         // t0 should run straight away
376
    Cyg_Thread::yield();  // but just in case we have a funny sched
377
 
378
    // check that mq was destroyed when t0 dropped off the end
379
    CYG_TEST_PASS_FAIL( NULL == mq, "queue destroyed correctly" );
380
 
381
    CYG_TEST_EXIT("kernel mqueue test 1");
382
 
383
} // t1()
384
 
385
 
386
//************************************************************************
387
//************************************************************************
388
 
389
externC void
390
cyg_user_start(void)
391
{
392
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
393
    cyg_hal_invoke_constructors();
394
#endif
395
    CYG_TEST_INIT();
396
 
397
    CYG_TEST_INFO( "Starting kernel mqueue test 1" );
398
    new_thread( t0, 0);
399
    new_thread( t1, 1);
400
 
401
#ifdef CYGIMP_THREAD_PRIORITY
402
    thread[0]->set_priority( 4 );
403
    thread[1]->set_priority( 5 ); // make sure the threads execute as intended
404
#endif
405
} // cyg_user_start()
406
 
407
//------------------------------------------------------------------------
408
 
409
 
410
/* EOF mqueue1.cxx */

powered by: WebSVN 2.1.0

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