]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/kmbox1.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / kernel / v2_0 / tests / kmbox1.c
1 /*==========================================================================
2 //
3 //        kmbox1.cxx
4 //
5 //        Kernel Mbox test 1
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 // Copyright (C) 2006 eCosCentric Ltd.
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // 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 along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
40 //
41 // Author:        dsm
42 // Contributors:    dsm
43 // Date:          1998-06-02
44 // Description:   Tests basic mbox functionality.
45 //####DESCRIPTIONEND####
46 */
47
48 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
49
50 #include <cyg/kernel/kapi.h>
51
52 #include <cyg/infra/testcase.h>
53
54 #ifdef CYGFUN_KERNEL_API_C
55
56 #include "testaux.h"
57
58 #define NTHREADS 2
59 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
60
61 static cyg_handle_t thread[NTHREADS];
62
63 static cyg_thread thread_obj[NTHREADS];
64 static char stack[NTHREADS][STACKSIZE];
65
66 static cyg_handle_t m0, m1, m2;
67 static cyg_mbox mbox0, mbox1, mbox2;
68
69 static cyg_atomic q = 0;
70
71 #ifndef CYGMTH_MBOX_PUT_CAN_WAIT
72 #define cyg_mbox_PUT cyg_mbox_tryput
73 #endif
74
75 static void entry0( cyg_addrword_t data )
76 {
77     cyg_count8 u,i,j;
78
79     CYG_TEST_INFO("Testing put() and tryput() without wakeup");
80     CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "mbox not initialized properly");
81     CYG_TEST_CHECK(0==cyg_mbox_peek(m0), "mbox not initialized properly");
82     CYG_TEST_CHECK(NULL==cyg_mbox_peek_item(m0), "mbox not initialized properly");
83     cyg_mbox_PUT(m0, (void *)55);
84     CYG_TEST_CHECK(1==cyg_mbox_peek(m0), "peek() wrong");
85     CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong");
86     for(u=1; cyg_mbox_tryput(m0, (void*)u); u++) {
87         CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong");
88         CYG_TEST_CHECK(u+1==cyg_mbox_peek(m0), "peek() wrong");
89     }
90     CYG_TEST_CHECK(u == CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE, "mbox not configured size");
91
92     // m0 now contains ( 55 1 2 .. u-1 )
93     CYG_TEST_CHECK(u==cyg_mbox_peek(m0), "peek() wrong");
94     CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong");
95
96     CYG_TEST_INFO("Testing get(), tryget()");
97     
98     i = (cyg_count8)cyg_mbox_tryget(m0);
99     CYG_TEST_CHECK( 55 == i, "Got wrong message" );
100     for(j=1; j<u;j++) {
101         CYG_TEST_CHECK( j == (cyg_count8)cyg_mbox_peek_item(m0), "peek_item()" );
102         CYG_TEST_CHECK( cyg_mbox_peek(m0) == u - j, "peek() wrong" );
103         i = (cyg_count8)cyg_mbox_get(m0);
104         CYG_TEST_CHECK( j == i, "Got wrong message" );
105     }
106     
107     CYG_TEST_CHECK( NULL == cyg_mbox_peek_item(m0), "peek_item()" );
108     CYG_TEST_CHECK( 0 == cyg_mbox_peek(m0), "peek()");
109     
110     // m0 now empty
111
112     CYG_TEST_CHECK(!cyg_mbox_waiting_to_put(m0), "waiting_to_put()");
113     CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "waiting_to_get()");
114
115     CYG_TEST_INFO("Testing get(), blocking");
116     
117     CYG_TEST_CHECK(0==q++, "bad synchronization");
118     cyg_mbox_PUT(m1, (void*)99);                  // wakes t1
119     i = (cyg_count8)cyg_mbox_get(m0);          // sent by t1
120     CYG_TEST_CHECK(3==i, "Recieved wrong message");
121     CYG_TEST_CHECK(2==q++, "bad synchronization");
122
123 #ifdef CYGFUN_KERNEL_THREADS_TIMER
124     CYG_TEST_CHECK(NULL==cyg_mbox_timed_get(m0, cyg_current_time()+10),
125                    "unexpectedly found message");
126     CYG_TEST_CHECK(3==q++, "bad synchronization");
127     // Allow t1 to run as this get times out
128     // t1 must not be waiting...
129     CYG_TEST_CHECK(cyg_mbox_waiting_to_get(m0), "waiting_to_get()");
130
131     cyg_mbox_PUT(m0, (void*)7);                   // wake t1 from timed get
132 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT
133     q=10;
134     while(cyg_mbox_tryput(m0, (void*)6))          // fill m0's queue
135         ;
136     // m0 now contains ( 6 ... 6 )
137     CYG_TEST_CHECK(10==q++, "bad synchronization");
138     cyg_mbox_put(m1, (void*)4);                   // wake t1
139     CYG_TEST_CHECK(!cyg_mbox_timed_put(m0, (void*)8, cyg_current_time()+10),
140                    "timed put() unexpectedly worked");
141     CYG_TEST_CHECK(12==q++, "bad synchronization");
142     // m0 still contains ( 6 ... 6 )
143     cyg_mbox_put(m0, (void*)9);
144     CYG_TEST_CHECK(13==q++, "bad synchronization");
145 #endif
146 #endif
147     i=(cyg_count8)cyg_mbox_get(m2);
148     CYG_TEST_FAIL_FINISH("Not reached");
149 }
150
151 static void entry1( cyg_addrword_t data )
152 {
153     cyg_count8 i;
154     i = (cyg_count8)cyg_mbox_get(m1);
155     CYG_TEST_CHECK(1==q++, "bad synchronization");
156     cyg_mbox_PUT(m0, (void *)3);                  // wake t0
157
158 #if defined(CYGFUN_KERNEL_THREADS_TIMER)
159     CYG_TEST_INFO("Testing timed functions");
160     CYG_TEST_CHECK(7==(cyg_count8)cyg_mbox_timed_get(m0,cyg_current_time()+20),
161                    "timed get()");
162     CYG_TEST_CHECK(4==q++, "bad synchronization");
163 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT
164     CYG_TEST_CHECK(4==(cyg_count8)cyg_mbox_get(m1));
165
166     CYG_TEST_CHECK(11==q++, "bad synchronization");
167     thread[0]->delay(20);    // allow t0 to reach put on m1
168     CYG_TEST_CHECK(14==q++, "bad synchronization");
169     CYG_TEST_CHECK(cyg_mbox_waiting_to_put(m0), "waiting_to_put()");
170     do {
171         // after first get m0 contains ( 6 .. 6 9 )
172         i=(cyg_count8)cyg_mbox_tryget(m0);
173     } while(6==i);
174     CYG_TEST_CHECK(9==i,"put gone awry");
175 #endif
176 #endif
177     CYG_TEST_PASS_FINISH("Kernel C API Mbox 1 OK");
178 }
179
180 void kmbox1_main( void )
181 {
182     CYG_TEST_INIT();
183
184     cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmbox1-0",
185         (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
186     cyg_thread_resume(thread[0]);
187
188     cyg_thread_create(5, entry1 , (cyg_addrword_t)1, "kmbox1-1",
189         (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
190     cyg_thread_resume(thread[1]);
191
192     cyg_mbox_create( &m0, &mbox0 );
193     cyg_mbox_create( &m1, &mbox1 );
194     cyg_mbox_create( &m2, &mbox2 );
195
196     cyg_scheduler_start();
197
198     CYG_TEST_FAIL_FINISH("Not reached");
199 }
200
201 externC void
202 cyg_start( void )
203
204     kmbox1_main();
205 }
206 #else /* def CYGFUN_KERNEL_API_C */
207 externC void
208 cyg_start( void )
209 {
210     CYG_TEST_INIT();
211     CYG_TEST_NA("Kernel C API layer disabled");
212 }
213 #endif /* def CYGFUN_KERNEL_API_C */
214
215 /* EOF kmbox1.c */