Mercurial > ecos
comparison packages/kernel/current/tests/kmbox1.c @ 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 // 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 | |
| 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-06-02 | |
| 35 // Description: Tests basic mbox functionality. | |
| 36 //####DESCRIPTIONEND#### | |
| 37 */ | |
| 38 | |
| 39 #include <cyg/kernel/kapi.h> | |
| 40 | |
| 41 #include <cyg/infra/testcase.h> | |
| 42 | |
| 43 #ifdef CYGFUN_KERNEL_API_C | |
| 44 | |
| 45 #include "testaux.h" | |
| 46 | |
| 47 #define NTHREADS 2 | |
| 48 #define STACKSIZE 4096 | |
| 49 | |
| 50 static cyg_handle_t thread[NTHREADS]; | |
| 51 | |
| 52 static cyg_thread thread_obj[NTHREADS]; | |
| 53 static char stack[NTHREADS][STACKSIZE]; | |
| 54 | |
| 55 static cyg_handle_t m0, m1, m2; | |
| 56 static cyg_mbox mbox0, mbox1, mbox2; | |
| 57 | |
| 58 static cyg_atomic q = 0; | |
| 59 | |
| 60 #ifndef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 61 #define cyg_mbox_PUT cyg_mbox_tryput | |
| 62 #endif | |
| 63 | |
| 64 static void entry0( cyg_addrword_t data ) | |
| 65 { | |
| 66 cyg_count8 u,i,j; | |
| 67 | |
| 68 CYG_TEST_INFO("Testing put() and tryput() without wakeup"); | |
| 69 CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "mbox not initialized properly"); | |
| 70 CYG_TEST_CHECK(0==cyg_mbox_peek(m0), "mbox not initialized properly"); | |
| 71 CYG_TEST_CHECK(NULL==cyg_mbox_peek_item(m0), "mbox not initialized properly"); | |
| 72 cyg_mbox_PUT(m0, (void *)55); | |
| 73 CYG_TEST_CHECK(1==cyg_mbox_peek(m0), "peek() wrong"); | |
| 74 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); | |
| 75 for(u=1; cyg_mbox_tryput(m0, (void*)u); u++) { | |
| 76 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); | |
| 77 CYG_TEST_CHECK(u+1==cyg_mbox_peek(m0), "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==cyg_mbox_peek(m0), "peek() wrong"); | |
| 83 CYG_TEST_CHECK(55==(cyg_count8)cyg_mbox_peek_item(m0), "peek_item() wrong"); | |
| 84 | |
| 85 CYG_TEST_INFO("Testing get(), tryget()"); | |
| 86 | |
| 87 i = (cyg_count8)cyg_mbox_tryget(m0); | |
| 88 CYG_TEST_CHECK( 55 == i, "Got wrong message" ); | |
| 89 for(j=1; j<u;j++) { | |
| 90 CYG_TEST_CHECK( j == (cyg_count8)cyg_mbox_peek_item(m0), "peek_item()" ); | |
| 91 CYG_TEST_CHECK( cyg_mbox_peek(m0) == u - j, "peek() wrong" ); | |
| 92 i = (cyg_count8)cyg_mbox_get(m0); | |
| 93 CYG_TEST_CHECK( j == i, "Got wrong message" ); | |
| 94 } | |
| 95 | |
| 96 CYG_TEST_CHECK( NULL == cyg_mbox_peek_item(m0), "peek_item()" ); | |
| 97 CYG_TEST_CHECK( 0 == cyg_mbox_peek(m0), "peek()"); | |
| 98 | |
| 99 // m0 now empty | |
| 100 | |
| 101 CYG_TEST_CHECK(!cyg_mbox_waiting_to_put(m0), "waiting_to_put()"); | |
| 102 CYG_TEST_CHECK(!cyg_mbox_waiting_to_get(m0), "waiting_to_get()"); | |
| 103 | |
| 104 CYG_TEST_INFO("Testing get(), blocking"); | |
| 105 | |
| 106 CYG_TEST_CHECK(0==q++, "bad synchronization"); | |
| 107 cyg_mbox_PUT(m1, (void*)99); // wakes t1 | |
| 108 i = (cyg_count8)cyg_mbox_get(m0); // 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==cyg_mbox_timed_get(m0, cyg_current_time()+2), | |
| 114 "unexpectedly found message"); | |
| 115 CYG_TEST_CHECK(3==q++, "bad synchronization"); | |
| 116 // Allow t1 to run as this get times out | |
| 117 // t1 must not be waiting... | |
| 118 CYG_TEST_CHECK(cyg_mbox_waiting_to_get(m0), "waiting_to_get()"); | |
| 119 | |
| 120 cyg_mbox_PUT(m0, (void*)7); // wake t1 from timed get | |
| 121 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 122 q=10; | |
| 123 while(cyg_mbox_tryput(m0, (void*)6)) // fill m0's queue | |
| 124 ; | |
| 125 // m0 now contains ( 6 ... 6 ) | |
| 126 CYG_TEST_CHECK(10==q++, "bad synchronization"); | |
| 127 cyg_mbox_put(m1, (void*)4); // wake t1 | |
| 128 CYG_TEST_CHECK(!cyg_mbox_timed_put(m0, (void*)8, cyg_current_time()+2), | |
| 129 "timed put() unexpectedly worked"); | |
| 130 CYG_TEST_CHECK(12==q++, "bad synchronization"); | |
| 131 // m0 still contains ( 6 ... 6 ) | |
| 132 cyg_mbox_put(m0, (void*)9); | |
| 133 CYG_TEST_CHECK(13==q++, "bad synchronization"); | |
| 134 #endif | |
| 135 #endif | |
| 136 i=(cyg_count8)cyg_mbox_get(m2); | |
| 137 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 138 } | |
| 139 | |
| 140 static void entry1( cyg_addrword_t data ) | |
| 141 { | |
| 142 cyg_count8 i; | |
| 143 i = (cyg_count8)cyg_mbox_get(m1); | |
| 144 CYG_TEST_CHECK(1==q++, "bad synchronization"); | |
| 145 cyg_mbox_PUT(m0, (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)cyg_mbox_timed_get(m0,cyg_current_time()+4), | |
| 150 "timed get()"); | |
| 151 CYG_TEST_CHECK(4==q++, "bad synchronization"); | |
| 152 #ifdef CYGMTH_MBOX_PUT_CAN_WAIT | |
| 153 CYG_TEST_CHECK(4==(cyg_count8)cyg_mbox_get(m1)); | |
| 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(cyg_mbox_waiting_to_put(m0), "waiting_to_put()"); | |
| 159 do { | |
| 160 // after first get m0 contains ( 6 .. 6 9 ) | |
| 161 i=(cyg_count8)cyg_mbox_tryget(m0); | |
| 162 } while(6==i); | |
| 163 CYG_TEST_CHECK(9==i,"put gone awry"); | |
| 164 #endif | |
| 165 #endif | |
| 166 CYG_TEST_PASS_FINISH("Kernel C API Mbox 1 OK"); | |
| 167 } | |
| 168 | |
| 169 void kmbox1_main( void ) | |
| 170 { | |
| 171 CYG_TEST_INIT(); | |
| 172 | |
| 173 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmbox1-0", | |
| 174 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); | |
| 175 cyg_thread_resume(thread[0]); | |
| 176 | |
| 177 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmbox1-1", | |
| 178 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); | |
| 179 cyg_thread_resume(thread[1]); | |
| 180 | |
| 181 cyg_mbox_create( &m0, &mbox0 ); | |
| 182 cyg_mbox_create( &m1, &mbox1 ); | |
| 183 cyg_mbox_create( &m2, &mbox2 ); | |
| 184 | |
| 185 cyg_scheduler_start(); | |
| 186 | |
| 187 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 188 } | |
| 189 | |
| 190 externC void | |
| 191 cyg_start( void ) | |
| 192 { | |
| 193 kmbox1_main(); | |
| 194 } | |
| 195 #else /* def CYGFUN_KERNEL_API_C */ | |
| 196 externC void | |
| 197 cyg_start( void ) | |
| 198 { | |
| 199 CYG_TEST_INIT(); | |
| 200 CYG_TEST_PASS_FINISH("Kernel C API layer disabled"); | |
| 201 } | |
| 202 #endif /* def CYGFUN_KERNEL_API_C */ | |
| 203 | |
| 204 /* EOF kmbox1.c */ |
