Mercurial > ecos
annotate packages/kernel/current/tests/mbox1.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 3 // mbox1.cxx | |
| 4 // | |
| 5 // Mbox test 1 | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author: dsm | |
| 35 // Contributors: dsm | |
| 36 // Date: 1998-05-19 | |
| 37 // Description: Tests basic mbox functionality. | |
| 38 //####DESCRIPTIONEND#### | |
| 39 | |
| 40 #include <pkgconf/kernel.h> | |
| 41 | |
| 42 #include <cyg/kernel/thread.hxx> // Cyg_Thread | |
| 43 #include <cyg/kernel/thread.inl> | |
| 44 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() | |
| 45 | |
| 46 #include <cyg/kernel/mbox.hxx> | |
| 47 | |
| 48 #include <cyg/infra/testcase.h> | |
| 49 | |
| 50 #include <cyg/kernel/sched.inl> | |
| 51 | |
| 52 #include <cyg/kernel/timer.hxx> // Cyg_Timer | |
| 53 #include <cyg/kernel/clock.inl> // Cyg_Clock | |
| 54 | |
| 55 #define NTHREADS 2 | |
| 56 #include "testaux.hxx" | |
| 57 | |
| 58 static Cyg_Mbox m0, m1, m2; | |
| 59 | |
| 60 static volatile cyg_atomic q = 0; | |
| 61 | |
| 62 #ifndef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 63 #define PUT tryput | |
| 64 #endif | |
| 65 | |
| 66 static void entry0( CYG_ADDRWORD data ) | |
| 67 { | |
| 68 cyg_count8 u,i; | |
| 69 | |
| 70 CYG_TEST_INFO("Testing put() and tryput() without wakeup"); | |
| 71 CYG_TEST_CHECK(!m0.waiting_to_get(), "mbox not initialized properly"); | |
| 72 CYG_TEST_CHECK(0==m0.peek(), "mbox not initialized properly"); | |
| 73 CYG_TEST_CHECK(NULL==m0.peek_item(), "mbox not initialized properly"); | |
| 74 m0.PUT((void *)55); | |
| 75 CYG_TEST_CHECK(1==m0.peek(), "peek() wrong"); | |
| 76 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 77 for(u=1; m0.tryput((void*)u); u++) { | |
| 78 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 79 CYG_TEST_CHECK(u+1==m0.peek(), "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==m0.peek(), "peek() wrong"); | |
| 85 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 86 | |
| 87 CYG_TEST_INFO("Testing get(), tryget()"); | |
| 88 | |
| 89 i = (cyg_count8)m0.tryget(); | |
| 90 CYG_TEST_CHECK( 55 == i, "Got wrong message" ); | |
| 91 for(cyg_count8 j=1; j<u;j++) { | |
| 92 CYG_TEST_CHECK( j == (cyg_count8)m0.peek_item(), "peek_item()" ); | |
| 93 CYG_TEST_CHECK( m0.peek() == u - j, "peek() wrong" ); | |
| 94 i = (cyg_count8)m0.get(); | |
| 95 CYG_TEST_CHECK( j == i, "Got wrong message" ); | |
| 96 } | |
| 97 | |
| 98 CYG_TEST_CHECK( NULL == m0.peek_item(), "peek_item()" ); | |
| 99 CYG_TEST_CHECK( 0 == m0.peek(), "peek()"); | |
| 100 | |
| 101 // m0 now empty | |
| 102 | |
| 103 CYG_TEST_CHECK(!m0.waiting_to_put(), "waiting_to_put()"); | |
| 104 CYG_TEST_CHECK(!m0.waiting_to_get(), "waiting_to_get()"); | |
| 105 | |
| 106 CYG_TEST_INFO("Testing get(), blocking"); | |
| 107 | |
| 108 CYG_TEST_CHECK(0==q++, "bad synchronization"); | |
| 109 m1.PUT((void*)99); // wakes t1 | |
| 110 i = (cyg_count8)m0.get(); // 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 | |
| 115 CYG_TEST_CHECK(NULL==m0.get( | |
| 2 | 116 Cyg_Clock::real_time_clock->current_value() + 10), |
| 0 | 117 "unexpectedly found message"); |
| 118 CYG_TEST_CHECK(3==q++, "bad synchronization"); | |
| 119 // Allow t1 to run as this get times out | |
| 120 // t1 must not be waiting... | |
| 121 CYG_TEST_CHECK(m0.waiting_to_get(), "waiting_to_get()"); | |
| 122 | |
| 123 m0.PUT((void*)7); // wake t1 from timed get | |
| 124 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 125 q=10; | |
| 126 while(m0.tryput((void*)6)) // fill m0's queue | |
| 127 ; | |
| 128 // m0 now contains ( 6 ... 6 ) | |
| 129 CYG_TEST_CHECK(10==q++, "bad synchronization"); | |
| 130 m1.put((void*)4); // wake t1 | |
| 131 CYG_TEST_CHECK(!m0.put((void*)8, 2), "timed put() unexpectedly worked"); | |
| 132 CYG_TEST_CHECK(12==q++, "bad synchronization"); | |
| 133 // m0 still contains ( 6 ... 6 ) | |
| 134 m0.put((void*)9); | |
| 135 CYG_TEST_CHECK(13==q++, "bad synchronization"); | |
| 136 #endif | |
| 137 #endif | |
| 138 i=(cyg_count8)m2.get(); | |
| 139 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 140 } | |
| 141 | |
| 142 static void entry1( CYG_ADDRWORD data ) | |
| 143 { | |
| 144 cyg_count8 i; | |
| 145 i = (cyg_count8)m1.get(); | |
| 146 CYG_TEST_CHECK(1==q++, "bad synchronization"); | |
| 147 m0.PUT((void *)3); // wake t0 | |
| 148 | |
| 149 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 150 CYG_TEST_INFO("Testing timed functions"); | |
| 151 CYG_TEST_CHECK(7==(cyg_count8)m0.get( | |
| 2 | 152 Cyg_Clock::real_time_clock->current_value() + 20), "timed get()"); |
| 0 | 153 CYG_TEST_CHECK(4==q++, "bad synchronization"); |
| 154 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 155 CYG_TEST_CHECK(4==(cyg_count8)m1.get()); | |
| 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(m0.waiting_to_put(), "waiting_to_put()"); | |
| 161 do { | |
| 162 // after first get m0 contains ( 6 .. 6 9 ) | |
| 163 i=(cyg_count8)m0.tryget(); | |
| 164 } while(6==i); | |
| 165 CYG_TEST_CHECK(9==i,"put gone awry"); | |
| 166 #endif | |
| 167 #endif | |
| 168 CYG_TEST_PASS_FINISH("Mbox 1 OK"); | |
| 169 } | |
| 170 | |
| 171 void mbox1_main( void ) | |
| 172 { | |
| 173 CYG_TEST_INIT(); | |
| 174 | |
| 175 new_thread(entry0, 0); | |
| 176 new_thread(entry1, 1); | |
| 177 | |
| 178 Cyg_Scheduler::start(); | |
| 179 | |
| 180 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 181 } | |
| 182 | |
| 183 externC void | |
| 184 cyg_start( void ) | |
| 185 { | |
| 186 mbox1_main(); | |
| 187 } | |
| 188 | |
| 189 // EOF mbox1.cxx |
