comparison packages/kernel/current/tests/sync2.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 // sync2.cxx
4 //
5 // Sync test 2 -- test of different locking mechanisms
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(s): dsm
33 // Contributors: dsm
34 // Date: 1998-02-18
35 // Description:
36 // Creates some threads and tests the various synchronization
37 // mechanisms. Four threads are created t0..t3. t0 and t3 grab a
38 // mutex and check they have exclusive access to shared variable.
39 // t0,t1,t2 post each other in a loop with a semaphore so that
40 // only one is running at any time. t1,t2,t3 do a similar thing
41 // with counting semaphores, except that there are two active
42 // threads.
43 // Omissions:
44 // Doesn't test condition variables
45 //
46 //####DESCRIPTIONEND####
47
48 #include <pkgconf/kernel.h>
49
50 #include <cyg/kernel/thread.hxx>
51 #include <cyg/kernel/thread.inl>
52 #include <cyg/kernel/sched.hxx>
53 #include <cyg/kernel/mutex.hxx>
54 #include <cyg/kernel/sema.hxx>
55
56 #include <cyg/infra/testcase.h>
57
58 #include <cyg/kernel/sched.inl>
59
60 #define NTHREADS 4
61
62 #include "testaux.hxx"
63
64 static Cyg_Mutex m0;
65 static Cyg_Binary_Semaphore s0, s1, s2(1);
66 static Cyg_Counting_Semaphore cs0, cs1, cs2, cs3;
67
68 static const cyg_ucount16 n = 1000;
69 static cyg_ucount8 m0d=99, sd=2, cd0=99, cd1=99;
70
71 static void entry0( CYG_ADDRWORD data )
72 {
73 for(cyg_ucount16 i=0; i<n; i++) {
74 s2.wait();
75 CHECK( 2 == sd );
76 sd = 0;
77 m0.lock(); {
78 m0d = 0;
79 s0.post();
80 CHECK( 0 == m0d );
81 } m0.unlock();
82 }
83 // wait for 3 explicit posts to indicate threads have stopped.
84 for(cyg_ucount8 i=0; i<3; i++)
85 cs3.wait();
86
87 CHECK( ! s0.posted() );
88 CHECK( ! s1.posted() );
89 CHECK( s2.posted() );
90
91 CHECK( 0 == cs0.peek() );
92 CHECK( 0 == cs1.peek() );
93 CHECK( 0 == cs2.peek() );
94 CHECK( 0 == cs3.peek() );
95
96 CHECK( 0 == cd0 );
97 CHECK( 0 == cd1 );
98 CYG_TEST_PASS_FINISH("Sync 2 OK");
99 CYG_TEST_FAIL_FINISH("Not reached");
100 }
101
102 static void entry1( CYG_ADDRWORD data )
103 {
104 for(cyg_ucount16 i=0; i<n; i++) {
105 s0.wait();
106 CHECK( 0 == sd );
107 sd = 1;
108 cd0 = 1;
109 cs1.post();
110 cd1 = 1;
111 cs1.post();
112 s1.post();
113 cs0.wait();
114 CHECK( 0 == cd0 );
115 cs0.wait();
116 CHECK( 0 == cd1 );
117 }
118 cs3.post();
119 s0.wait();
120 CYG_TEST_FAIL_FINISH("Not reached");
121 }
122
123 static void entry2( CYG_ADDRWORD data )
124 {
125 for(cyg_ucount16 i=0; i<n; i++) {
126 s1.wait();
127 CHECK( 1 == sd );
128 sd = 2;
129 cs1.wait();
130 CHECK( 1 == cd0 );
131 cd0 = 2;
132 cs2.post();
133 s2.post();
134 cs1.wait();
135 CHECK( 1 == cd1 );
136 cd1 = 2;
137 cs2.post();
138 }
139 cs3.post();
140 s1.wait();
141 CYG_TEST_FAIL_FINISH("Not reached");
142 }
143
144 static void entry3( CYG_ADDRWORD data )
145 {
146 for(cyg_ucount16 i=0; i < n*2; i++) {
147 cs2.wait();
148 CHECK( 2 == cd0 || 2 == cd1 );
149 m0.lock(); {
150 m0d = 3;
151 if( 2 == cd0 )
152 cd0 = 0;
153 else {
154 CHECK( 2 == cd1 );
155 cd1 = 0;
156 }
157 cs0.post();
158 CHECK( 3 == m0d );
159 } m0.unlock();
160 }
161 cs3.post();
162 cs1.wait();
163 CYG_TEST_FAIL_FINISH("Not reached");
164 }
165
166
167 void sync2_main(void)
168 {
169 CYG_TEST_INIT();
170
171 new_thread(entry0, 0);
172 new_thread(entry1, 1);
173 new_thread(entry2, 2);
174 new_thread(entry3, 3);
175
176 Cyg_Scheduler::start();
177
178 CYG_TEST_PASS_FINISH("Not reached");
179 }
180
181 externC void
182 cyg_start( void )
183 {
184 sync2_main();
185 }
186
187 // EOF sync2.cxx