1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// mbox1.cxx
|
4 |
|
|
//
|
5 |
|
|
// 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, 2006 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY 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
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author: dsm
|
43 |
|
|
// Contributors: dsm
|
44 |
|
|
// Date: 1998-05-19
|
45 |
|
|
// Description: Tests basic mbox functionality.
|
46 |
|
|
//####DESCRIPTIONEND####
|
47 |
|
|
|
48 |
|
|
#include <pkgconf/kernel.h>
|
49 |
|
|
|
50 |
|
|
#include <cyg/kernel/thread.hxx> // Cyg_Thread
|
51 |
|
|
#include <cyg/kernel/thread.inl>
|
52 |
|
|
#include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start()
|
53 |
|
|
|
54 |
|
|
#include <cyg/kernel/mbox.hxx>
|
55 |
|
|
|
56 |
|
|
#include <cyg/infra/testcase.h>
|
57 |
|
|
|
58 |
|
|
#include <cyg/kernel/sched.inl>
|
59 |
|
|
|
60 |
|
|
#include <cyg/kernel/timer.hxx> // Cyg_Timer
|
61 |
|
|
#include <cyg/kernel/clock.inl> // Cyg_Clock
|
62 |
|
|
|
63 |
|
|
#define NTHREADS 2
|
64 |
|
|
#include "testaux.hxx"
|
65 |
|
|
|
66 |
|
|
static Cyg_Mbox m0, m1, m2;
|
67 |
|
|
|
68 |
|
|
static volatile cyg_atomic q = 0;
|
69 |
|
|
|
70 |
|
|
#ifndef CYGMTH_MBOX_PUT_CAN_WAIT
|
71 |
|
|
#define PUT tryput
|
72 |
|
|
#endif
|
73 |
|
|
|
74 |
|
|
static void entry0( CYG_ADDRWORD data )
|
75 |
|
|
{
|
76 |
|
|
cyg_count8 u,i;
|
77 |
|
|
|
78 |
|
|
Cyg_Thread::self()->set_priority(4);
|
79 |
|
|
|
80 |
|
|
CYG_TEST_INFO("Testing put() and tryput() without wakeup");
|
81 |
|
|
CYG_TEST_CHECK(!m0.waiting_to_get(), "mbox not initialized properly");
|
82 |
|
|
CYG_TEST_CHECK(0==m0.peek(), "mbox not initialized properly");
|
83 |
|
|
CYG_TEST_CHECK(NULL==m0.peek_item(), "mbox not initialized properly");
|
84 |
|
|
m0.PUT((void *)55);
|
85 |
|
|
CYG_TEST_CHECK(1==m0.peek(), "peek() wrong");
|
86 |
|
|
CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong");
|
87 |
|
|
for(u=1; m0.tryput((void*)u); u++) {
|
88 |
|
|
CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong");
|
89 |
|
|
CYG_TEST_CHECK(u+1==m0.peek(), "peek() wrong");
|
90 |
|
|
}
|
91 |
|
|
CYG_TEST_CHECK(u == CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE, "mbox not configured size");
|
92 |
|
|
|
93 |
|
|
// m0 now contains ( 55 1 2 .. u-1 )
|
94 |
|
|
CYG_TEST_CHECK(u==m0.peek(), "peek() wrong");
|
95 |
|
|
CYG_TEST_CHECK(55==(cyg_count8)m0.peek_item(), "peek_item() wrong");
|
96 |
|
|
|
97 |
|
|
CYG_TEST_INFO("Testing get(), tryget()");
|
98 |
|
|
|
99 |
|
|
i = (cyg_count8)m0.tryget();
|
100 |
|
|
CYG_TEST_CHECK( 55 == i, "Got wrong message" );
|
101 |
|
|
for(cyg_count8 j=1; j<u;j++) {
|
102 |
|
|
CYG_TEST_CHECK( j == (cyg_count8)m0.peek_item(), "peek_item()" );
|
103 |
|
|
CYG_TEST_CHECK( m0.peek() == u - j, "peek() wrong" );
|
104 |
|
|
i = (cyg_count8)m0.get();
|
105 |
|
|
CYG_TEST_CHECK( j == i, "Got wrong message" );
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
CYG_TEST_CHECK( NULL == m0.peek_item(), "peek_item()" );
|
109 |
|
|
CYG_TEST_CHECK( 0 == m0.peek(), "peek()");
|
110 |
|
|
|
111 |
|
|
// m0 now empty
|
112 |
|
|
|
113 |
|
|
CYG_TEST_CHECK(!m0.waiting_to_put(), "waiting_to_put()");
|
114 |
|
|
CYG_TEST_CHECK(!m0.waiting_to_get(), "waiting_to_get()");
|
115 |
|
|
|
116 |
|
|
CYG_TEST_INFO("Testing get(), blocking");
|
117 |
|
|
|
118 |
|
|
CYG_TEST_CHECK(0==q++, "bad synchronization");
|
119 |
|
|
m1.PUT((void*)99); // wakes t1
|
120 |
|
|
i = (cyg_count8)m0.get(); // sent by t1
|
121 |
|
|
CYG_TEST_CHECK(3==i, "Recieved wrong message");
|
122 |
|
|
CYG_TEST_CHECK(2==q++, "bad synchronization");
|
123 |
|
|
|
124 |
|
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
125 |
|
|
CYG_TEST_CHECK(NULL==m0.get(
|
126 |
|
|
Cyg_Clock::real_time_clock->current_value() + 10),
|
127 |
|
|
"unexpectedly found message");
|
128 |
|
|
CYG_TEST_CHECK(3==q++, "bad synchronization");
|
129 |
|
|
// Allow t1 to run as this get times out
|
130 |
|
|
// t1 must not be waiting...
|
131 |
|
|
CYG_TEST_CHECK(m0.waiting_to_get(), "waiting_to_get()");
|
132 |
|
|
|
133 |
|
|
m0.PUT((void*)7); // wake t1 from timed get
|
134 |
|
|
#ifdef CYGMTH_MBOX_PUT_CAN_WAIT
|
135 |
|
|
q=10;
|
136 |
|
|
while(m0.tryput((void*)6)) // fill m0's queue
|
137 |
|
|
;
|
138 |
|
|
// m0 now contains ( 6 ... 6 )
|
139 |
|
|
CYG_TEST_CHECK(10==q++, "bad synchronization");
|
140 |
|
|
m1.put((void*)4); // wake t1
|
141 |
|
|
CYG_TEST_CHECK(!m0.put((void*)8, 2), "timed put() unexpectedly worked");
|
142 |
|
|
CYG_TEST_CHECK(12==q++, "bad synchronization");
|
143 |
|
|
// m0 still contains ( 6 ... 6 )
|
144 |
|
|
m0.put((void*)9);
|
145 |
|
|
CYG_TEST_CHECK(13==q++, "bad synchronization");
|
146 |
|
|
#endif
|
147 |
|
|
#endif
|
148 |
|
|
i=(cyg_count8)m2.get();
|
149 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
static void entry1( CYG_ADDRWORD data )
|
153 |
|
|
{
|
154 |
|
|
cyg_count8 i;
|
155 |
|
|
|
156 |
|
|
Cyg_Thread::self()->set_priority(5);
|
157 |
|
|
|
158 |
|
|
i = (cyg_count8)m1.get();
|
159 |
|
|
CYG_TEST_CHECK(1==q++, "bad synchronization");
|
160 |
|
|
m0.PUT((void *)3); // wake t0
|
161 |
|
|
|
162 |
|
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
163 |
|
|
CYG_TEST_INFO("Testing timed functions");
|
164 |
|
|
CYG_TEST_CHECK(7==(cyg_count8)m0.get(
|
165 |
|
|
Cyg_Clock::real_time_clock->current_value() + 20), "timed get()");
|
166 |
|
|
CYG_TEST_CHECK(4==q++, "bad synchronization");
|
167 |
|
|
#ifdef CYGMTH_MBOX_PUT_CAN_WAIT
|
168 |
|
|
CYG_TEST_CHECK(4==(cyg_count8)m1.get());
|
169 |
|
|
|
170 |
|
|
CYG_TEST_CHECK(11==q++, "bad synchronization");
|
171 |
|
|
thread[0]->delay(20); // allow t0 to reach put on m1
|
172 |
|
|
CYG_TEST_CHECK(14==q++, "bad synchronization");
|
173 |
|
|
CYG_TEST_CHECK(m0.waiting_to_put(), "waiting_to_put()");
|
174 |
|
|
do {
|
175 |
|
|
// after first get m0 contains ( 6 .. 6 9 )
|
176 |
|
|
i=(cyg_count8)m0.tryget();
|
177 |
|
|
} while(6==i);
|
178 |
|
|
CYG_TEST_CHECK(9==i,"put gone awry");
|
179 |
|
|
#endif
|
180 |
|
|
#endif
|
181 |
|
|
CYG_TEST_PASS_FINISH("Mbox 1 OK");
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
void mbox1_main( void )
|
185 |
|
|
{
|
186 |
|
|
CYG_TEST_INIT();
|
187 |
|
|
|
188 |
|
|
new_thread(entry0, 0);
|
189 |
|
|
new_thread(entry1, 1);
|
190 |
|
|
|
191 |
|
|
Cyg_Scheduler::start();
|
192 |
|
|
|
193 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
externC void
|
197 |
|
|
cyg_start( void )
|
198 |
|
|
{
|
199 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
200 |
|
|
cyg_hal_invoke_constructors();
|
201 |
|
|
#endif
|
202 |
|
|
mbox1_main();
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
// EOF mbox1.cxx
|