Mercurial > ecos
comparison packages/kernel/current/tests/mbox1.cxx @ 0:3111d98ba7b3 ecos-v1_1-release
Initial commit of eCos version 1.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 11:16:07 +0000 |
| parents | |
| children | 443894e2e912 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3111d98ba7b3 |
|---|---|
| 1 //========================================================================== | |
| 2 // | |
| 3 // mbox1.cxx | |
| 4 // | |
| 5 // 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 | |
| 25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. | |
| 26 // ------------------------------------------- | |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //========================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 32 // Author: dsm | |
| 33 // Contributors: dsm | |
| 34 // Date: 1998-05-19 | |
| 35 // Description: Tests basic mbox functionality. | |
| 36 //####DESCRIPTIONEND#### | |
| 37 | |
| 38 #include <pkgconf/kernel.h> | |
| 39 | |
| 40 #include <cyg/kernel/thread.hxx> // Cyg_Thread | |
| 41 #include <cyg/kernel/thread.inl> | |
| 42 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() | |
| 43 | |
| 44 #include <cyg/kernel/mbox.hxx> | |
| 45 | |
| 46 #include <cyg/infra/testcase.h> | |
| 47 | |
| 48 #include <cyg/kernel/sched.inl> | |
| 49 | |
| 50 #include <cyg/kernel/timer.hxx> // Cyg_Timer | |
| 51 #include <cyg/kernel/clock.inl> // Cyg_Clock | |
| 52 | |
| 53 #define NTHREADS 2 | |
| 54 #include "testaux.hxx" | |
| 55 | |
| 56 static Cyg_Mbox m0, m1, m2; | |
| 57 | |
| 58 static volatile cyg_atomic q = 0; | |
| 59 | |
| 60 #ifndef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 61 #define PUT tryput | |
| 62 #endif | |
| 63 | |
| 64 static void entry0( CYG_ADDRWORD data ) | |
| 65 { | |
| 66 cyg_count8 u,i; | |
| 67 | |
| 68 CYG_TEST_INFO("Testing put() and tryput() without wakeup"); | |
| 69 CYG_TEST_CHECK(!m0.waiting_to_get(), "mbox not initialized properly"); | |
| 70 CYG_TEST_CHECK(0==m0.peek(), "mbox not initialized properly"); | |
| 71 CYG_TEST_CHECK(NULL==m0.peek_item(), "mbox not initialized properly"); | |
| 72 m0.PUT((void *)55); | |
| 73 CYG_TEST_CHECK(1==m0.peek(), "peek() wrong"); | |
| 74 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 75 for(u=1; m0.tryput((void*)u); u++) { | |
| 76 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 77 CYG_TEST_CHECK(u+1==m0.peek(), "peek() wrong"); | |
| 78 } | |
| 79 CYG_TEST_CHECK(u == CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE, "mbox not configured size"); | |
| 80 | |
| 81 // m0 now contains ( 55 1 2 .. u-1 ) | |
| 82 CYG_TEST_CHECK(u==m0.peek(), "peek() wrong"); | |
| 83 CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong"); | |
| 84 | |
| 85 CYG_TEST_INFO("Testing get(), tryget()"); | |
| 86 | |
| 87 i = (cyg_count8)m0.tryget(); | |
| 88 CYG_TEST_CHECK( 55 == i, "Got wrong message" ); | |
| 89 for(cyg_count8 j=1; j<u;j++) { | |
| 90 CYG_TEST_CHECK( j == (cyg_count8)m0.peek_item(), "peek_item()" ); | |
| 91 CYG_TEST_CHECK( m0.peek() == u - j, "peek() wrong" ); | |
| 92 i = (cyg_count8)m0.get(); | |
| 93 CYG_TEST_CHECK( j == i, "Got wrong message" ); | |
| 94 } | |
| 95 | |
| 96 CYG_TEST_CHECK( NULL == m0.peek_item(), "peek_item()" ); | |
| 97 CYG_TEST_CHECK( 0 == m0.peek(), "peek()"); | |
| 98 | |
| 99 // m0 now empty | |
| 100 | |
| 101 CYG_TEST_CHECK(!m0.waiting_to_put(), "waiting_to_put()"); | |
| 102 CYG_TEST_CHECK(!m0.waiting_to_get(), "waiting_to_get()"); | |
| 103 | |
| 104 CYG_TEST_INFO("Testing get(), blocking"); | |
| 105 | |
| 106 CYG_TEST_CHECK(0==q++, "bad synchronization"); | |
| 107 m1.PUT((void*)99); // wakes t1 | |
| 108 i = (cyg_count8)m0.get(); // sent by t1 | |
| 109 CYG_TEST_CHECK(3==i, "Recieved wrong message"); | |
| 110 CYG_TEST_CHECK(2==q++, "bad synchronization"); | |
| 111 | |
| 112 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 113 CYG_TEST_CHECK(NULL==m0.get( | |
| 114 Cyg_Clock::real_time_clock->current_value() + 2), | |
| 115 "unexpectedly found message"); | |
| 116 CYG_TEST_CHECK(3==q++, "bad synchronization"); | |
| 117 // Allow t1 to run as this get times out | |
| 118 // t1 must not be waiting... | |
| 119 CYG_TEST_CHECK(m0.waiting_to_get(), "waiting_to_get()"); | |
| 120 | |
| 121 m0.PUT((void*)7); // wake t1 from timed get | |
| 122 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 123 q=10; | |
| 124 while(m0.tryput((void*)6)) // fill m0's queue | |
| 125 ; | |
| 126 // m0 now contains ( 6 ... 6 ) | |
| 127 CYG_TEST_CHECK(10==q++, "bad synchronization"); | |
| 128 m1.put((void*)4); // wake t1 | |
| 129 CYG_TEST_CHECK(!m0.put((void*)8, 2), "timed put() unexpectedly worked"); | |
| 130 CYG_TEST_CHECK(12==q++, "bad synchronization"); | |
| 131 // m0 still contains ( 6 ... 6 ) | |
| 132 m0.put((void*)9); | |
| 133 CYG_TEST_CHECK(13==q++, "bad synchronization"); | |
| 134 #endif | |
| 135 #endif | |
| 136 i=(cyg_count8)m2.get(); | |
| 137 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 138 } | |
| 139 | |
| 140 static void entry1( CYG_ADDRWORD data ) | |
| 141 { | |
| 142 cyg_count8 i; | |
| 143 i = (cyg_count8)m1.get(); | |
| 144 CYG_TEST_CHECK(1==q++, "bad synchronization"); | |
| 145 m0.PUT((void *)3); // wake t0 | |
| 146 | |
| 147 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 148 CYG_TEST_INFO("Testing timed functions"); | |
| 149 CYG_TEST_CHECK(7==(cyg_count8)m0.get( | |
| 150 Cyg_Clock::real_time_clock->current_value() + 4), "timed get()"); | |
| 151 CYG_TEST_CHECK(4==q++, "bad synchronization"); | |
| 152 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 153 CYG_TEST_CHECK(4==(cyg_count8)m1.get()); | |
| 154 | |
| 155 CYG_TEST_CHECK(11==q++, "bad synchronization"); | |
| 156 thread[0]->delay(4); // allow t0 to reach put on m1 | |
| 157 CYG_TEST_CHECK(14==q++, "bad synchronization"); | |
| 158 CYG_TEST_CHECK(m0.waiting_to_put(), "waiting_to_put()"); | |
| 159 do { | |
| 160 // after first get m0 contains ( 6 .. 6 9 ) | |
| 161 i=(cyg_count8)m0.tryget(); | |
| 162 } while(6==i); | |
| 163 CYG_TEST_CHECK(9==i,"put gone awry"); | |
| 164 #endif | |
| 165 #endif | |
| 166 CYG_TEST_PASS_FINISH("Mbox 1 OK"); | |
| 167 } | |
| 168 | |
| 169 void mbox1_main( void ) | |
| 170 { | |
| 171 CYG_TEST_INIT(); | |
| 172 | |
| 173 new_thread(entry0, 0); | |
| 174 new_thread(entry1, 1); | |
| 175 | |
| 176 Cyg_Scheduler::start(); | |
| 177 | |
| 178 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 179 } | |
| 180 | |
| 181 externC void | |
| 182 cyg_start( void ) | |
| 183 { | |
| 184 mbox1_main(); | |
| 185 } | |
| 186 | |
| 187 // EOF mbox1.cxx |
