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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [kernel/] [v2_0/] [tests/] [mqueue1.cxx] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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