|
0
|
1 /*========================================================================== |
|
|
2 // |
|
|
3 // kmbox1.cxx |
|
|
4 // |
|
|
5 // Kernel Mbox test 1 |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author: dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-06-02 |
|
|
35 // Description: Tests basic mbox functionality. |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
2
|
39 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
|
40 |
|
0
|
41 #include <cyg/kernel/kapi.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #ifdef CYGFUN_KERNEL_API_C |
|
|
46 |
|
|
47 #include "testaux.h" |
|
|
48 |
|
|
49 #define NTHREADS 2 |
|
2
|
50 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
0
|
51 |
|
|
52 static cyg_handle_t thread[NTHREADS]; |
|
|
53 |
|
|
54 static cyg_thread thread_obj[NTHREADS]; |
|
|
55 static char stack[NTHREADS][STACKSIZE]; |
|
|
56 |
|
|
57 static cyg_handle_t m0, m1, m2; |
|
|
58 static cyg_mbox mbox0, mbox1, mbox2; |
|
|
59 |
|
|
60 static cyg_atomic q = 0; |
|
|
61 |
|
|
62 #ifndef CYGMTH_MBOX_PUT_CAN_WAIT |
|
|
63 #define cyg_mbox_PUT cyg_mbox_tryput |
|
|
64 #endif |
|
|
65 |
|
|
66 static void entry0( cyg_addrword_t data ) |
|
|
67 { |
|
|
68 cyg_count8 u,i,j; |
|
|
69 |
|
|
70 CYG_TEST_INFO("Testing put() and tryput() without wakeup"); |
|
|
71 CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "mbox not initialized properly"); |
|
|
72 CYG_TEST_CHECK(0==cyg_mbox_peek(m0), "mbox not initialized properly"); |
|
|
73 CYG_TEST_CHECK(NULL==cyg_mbox_peek_item(m0), "mbox not initialized properly"); |
|
|
74 cyg_mbox_PUT(m0, (void *)55); |
|
|
75 CYG_TEST_CHECK(1==cyg_mbox_peek(m0), "peek() wrong"); |
|
|
76 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); |
|
|
77 for(u=1; cyg_mbox_tryput(m0, (void*)u); u++) { |
|
|
78 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); |
|
|
79 CYG_TEST_CHECK(u+1==cyg_mbox_peek(m0), "peek() wrong"); |
|
|
80 } |
|
|
81 CYG_TEST_CHECK(u == CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE, "mbox not configured size"); |
|
|
82 |
|
|
83 // m0 now contains ( 55 1 2 .. u-1 ) |
|
|
84 CYG_TEST_CHECK(u==cyg_mbox_peek(m0), "peek() wrong"); |
|
|
85 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); |
|
|
86 |
|
|
87 CYG_TEST_INFO("Testing get(), tryget()"); |
|
|
88 |
|
|
89 i = (cyg_count8)cyg_mbox_tryget(m0); |
|
|
90 CYG_TEST_CHECK( 55 == i, "Got wrong message" ); |
|
|
91 for(j=1; j<u;j++) { |
|
|
92 CYG_TEST_CHECK( j == (cyg_count8)cyg_mbox_peek_item(m0), "peek_item()" ); |
|
|
93 CYG_TEST_CHECK( cyg_mbox_peek(m0) == u - j, "peek() wrong" ); |
|
|
94 i = (cyg_count8)cyg_mbox_get(m0); |
|
|
95 CYG_TEST_CHECK( j == i, "Got wrong message" ); |
|
|
96 } |
|
|
97 |
|
|
98 CYG_TEST_CHECK( NULL == cyg_mbox_peek_item(m0), "peek_item()" ); |
|
|
99 CYG_TEST_CHECK( 0 == cyg_mbox_peek(m0), "peek()"); |
|
|
100 |
|
|
101 // m0 now empty |
|
|
102 |
|
|
103 CYG_TEST_CHECK(!cyg_mbox_waiting_to_put(m0), "waiting_to_put()"); |
|
|
104 CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "waiting_to_get()"); |
|
|
105 |
|
|
106 CYG_TEST_INFO("Testing get(), blocking"); |
|
|
107 |
|
|
108 CYG_TEST_CHECK(0==q++, "bad synchronization"); |
|
|
109 cyg_mbox_PUT(m1, (void*)99); // wakes t1 |
|
|
110 i = (cyg_count8)cyg_mbox_get(m0); // sent by t1 |
|
|
111 CYG_TEST_CHECK(3==i, "Recieved wrong message"); |
|
|
112 CYG_TEST_CHECK(2==q++, "bad synchronization"); |
|
|
113 |
|
|
114 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
2
|
115 CYG_TEST_CHECK(NULL==cyg_mbox_timed_get(m0, cyg_current_time()+10), |
|
0
|
116 "unexpectedly found message"); |
|
|
117 CYG_TEST_CHECK(3==q++, "bad synchronization"); |
|
|
118 // Allow t1 to run as this get times out |
|
|
119 // t1 must not be waiting... |
|
|
120 CYG_TEST_CHECK(cyg_mbox_waiting_to_get(m0), "waiting_to_get()"); |
|
|
121 |
|
|
122 cyg_mbox_PUT(m0, (void*)7); // wake t1 from timed get |
|
|
123 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT |
|
|
124 q=10; |
|
|
125 while(cyg_mbox_tryput(m0, (void*)6)) // fill m0's queue |
|
|
126 ; |
|
|
127 // m0 now contains ( 6 ... 6 ) |
|
|
128 CYG_TEST_CHECK(10==q++, "bad synchronization"); |
|
|
129 cyg_mbox_put(m1, (void*)4); // wake t1 |
|
2
|
130 CYG_TEST_CHECK(!cyg_mbox_timed_put(m0, (void*)8, cyg_current_time()+10), |
|
0
|
131 "timed put() unexpectedly worked"); |
|
|
132 CYG_TEST_CHECK(12==q++, "bad synchronization"); |
|
|
133 // m0 still contains ( 6 ... 6 ) |
|
|
134 cyg_mbox_put(m0, (void*)9); |
|
|
135 CYG_TEST_CHECK(13==q++, "bad synchronization"); |
|
|
136 #endif |
|
|
137 #endif |
|
|
138 i=(cyg_count8)cyg_mbox_get(m2); |
|
|
139 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
140 } |
|
|
141 |
|
|
142 static void entry1( cyg_addrword_t data ) |
|
|
143 { |
|
|
144 cyg_count8 i; |
|
|
145 i = (cyg_count8)cyg_mbox_get(m1); |
|
|
146 CYG_TEST_CHECK(1==q++, "bad synchronization"); |
|
|
147 cyg_mbox_PUT(m0, (void *)3); // wake t0 |
|
|
148 |
|
2
|
149 #if defined(CYGFUN_KERNEL_THREADS_TIMER) |
|
0
|
150 CYG_TEST_INFO("Testing timed functions"); |
|
2
|
151 CYG_TEST_CHECK(7==(cyg_count8)cyg_mbox_timed_get(m0,cyg_current_time()+20), |
|
0
|
152 "timed get()"); |
|
|
153 CYG_TEST_CHECK(4==q++, "bad synchronization"); |
|
|
154 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT |
|
|
155 CYG_TEST_CHECK(4==(cyg_count8)cyg_mbox_get(m1)); |
|
|
156 |
|
|
157 CYG_TEST_CHECK(11==q++, "bad synchronization"); |
|
2
|
158 thread[0]->delay(20); // allow t0 to reach put on m1 |
|
0
|
159 CYG_TEST_CHECK(14==q++, "bad synchronization"); |
|
|
160 CYG_TEST_CHECK(cyg_mbox_waiting_to_put(m0), "waiting_to_put()"); |
|
|
161 do { |
|
|
162 // after first get m0 contains ( 6 .. 6 9 ) |
|
|
163 i=(cyg_count8)cyg_mbox_tryget(m0); |
|
|
164 } while(6==i); |
|
|
165 CYG_TEST_CHECK(9==i,"put gone awry"); |
|
|
166 #endif |
|
|
167 #endif |
|
|
168 CYG_TEST_PASS_FINISH("Kernel C API Mbox 1 OK"); |
|
|
169 } |
|
|
170 |
|
|
171 void kmbox1_main( void ) |
|
|
172 { |
|
|
173 CYG_TEST_INIT(); |
|
|
174 |
|
|
175 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmbox1-0", |
|
2
|
176 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
0
|
177 cyg_thread_resume(thread[0]); |
|
|
178 |
|
|
179 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmbox1-1", |
|
2
|
180 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
0
|
181 cyg_thread_resume(thread[1]); |
|
|
182 |
|
|
183 cyg_mbox_create( &m0, &mbox0 ); |
|
|
184 cyg_mbox_create( &m1, &mbox1 ); |
|
|
185 cyg_mbox_create( &m2, &mbox2 ); |
|
|
186 |
|
|
187 cyg_scheduler_start(); |
|
|
188 |
|
|
189 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
190 } |
|
|
191 |
|
|
192 externC void |
|
|
193 cyg_start( void ) |
|
|
194 { |
|
|
195 kmbox1_main(); |
|
|
196 } |
|
|
197 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
198 externC void |
|
|
199 cyg_start( void ) |
|
|
200 { |
|
|
201 CYG_TEST_INIT(); |
|
2
|
202 CYG_TEST_NA("Kernel C API layer disabled"); |
|
0
|
203 } |
|
|
204 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
205 |
|
|
206 /* EOF kmbox1.c */ |