|
0
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // thread2.cxx |
|
|
4 // |
|
|
5 // Thread test 2 |
|
|
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-19 |
|
|
35 // Description: |
|
|
36 // tests scheduler & threads & priorities |
|
|
37 // + create multiple threads with various priorities |
|
|
38 // + check highest priority running thread is always running |
|
|
39 // + check next highest runs when highest suspended |
|
|
40 // + check several threads of equal priority share time |
|
|
41 // (only !CYG_SCHED_UNIQUE_PRIORITIES) |
|
|
42 // + check other threads are starved |
|
|
43 // + check setting priority dynamically causes a thread to |
|
|
44 // become/stay current/non-current |
|
|
45 // Omissions: |
|
|
46 // check yield |
|
|
47 // check can set threads with min and max priority |
|
|
48 // Options: |
|
|
49 // CYG_SCHED_UNIQUE_PRIORITIES |
|
|
50 // CYGIMP_THREAD_PRIORITY |
|
|
51 // CYGNUM_KERNEL_SCHED_PRIORITIES |
|
|
52 // CYGSEM_KERNEL_SCHED_BITMAP |
|
|
53 // CYGSEM_KERNEL_SCHED_MLQUEUE |
|
|
54 //####DESCRIPTIONEND#### |
|
|
55 |
|
|
56 #include <pkgconf/kernel.h> |
|
|
57 |
|
|
58 #include <cyg/kernel/thread.hxx> |
|
|
59 #include <cyg/kernel/thread.inl> |
|
|
60 #include <cyg/kernel/sched.hxx> |
|
|
61 #include <cyg/kernel/mutex.hxx> |
|
|
62 #include <cyg/kernel/sema.hxx> |
|
|
63 |
|
|
64 #include <cyg/infra/testcase.h> |
|
|
65 |
|
|
66 #include <cyg/kernel/sched.inl> |
|
|
67 |
|
|
68 #include "testaux.hxx" |
|
|
69 |
|
|
70 #ifndef CYGIMP_THREAD_PRIORITY |
|
|
71 #error "Test requires thread priorities" |
|
|
72 #endif |
|
|
73 |
|
|
74 #define STACKSIZE 4096 |
|
|
75 |
|
|
76 static char stack[3][STACKSIZE]; |
|
|
77 |
|
|
78 static Cyg_Counting_Semaphore s0, s1, s2; |
|
|
79 |
|
|
80 static volatile cyg_ucount8 q = 0; |
|
|
81 |
|
|
82 static Cyg_Thread *thread0, *thread1, *thread2; |
|
|
83 |
|
|
84 static char thread[3][sizeof(Cyg_Thread)]; |
|
|
85 |
|
|
86 inline void *operator new(size_t size, void *ptr) { return ptr; }; |
|
|
87 |
|
|
88 |
|
|
89 static void entry0( CYG_ADDRWORD data ) |
|
|
90 { |
|
|
91 CHECK( 0 == q++ ); |
|
|
92 s0.wait(); |
|
|
93 CHECK( 3 == q++ ); |
|
|
94 s1.post(); |
|
|
95 CHECK( 4 == q++ ); |
|
|
96 s0.wait(); |
|
|
97 s0.wait(); |
|
|
98 CYG_TEST_PASS_FINISH("Thread 2 OK"); |
|
|
99 } |
|
|
100 |
|
|
101 static void entry1( CYG_ADDRWORD data ) |
|
|
102 { |
|
|
103 CHECK( 1 == q++ ); |
|
|
104 s1.wait(); |
|
|
105 CHECK( 5 == q++ ); |
|
|
106 thread0->set_priority(9); |
|
|
107 s0.post(); |
|
|
108 CHECK( 6 == q++ ); |
|
|
109 thread2->set_priority(3); |
|
|
110 CHECK( 8 == q++ ); |
|
|
111 s2.post(); |
|
|
112 CHECK( 12 == q++ ); |
|
|
113 CHECK( 9 == thread0->get_priority() ); |
|
|
114 CHECK( 6 == thread1->get_priority() ); |
|
|
115 CHECK( 7 == thread2->get_priority() ); |
|
|
116 q = 100; |
|
|
117 #if !(CYG_SCHED_UNIQUE_PRIORITIES) && defined(CYGSEM_KERNEL_SCHED_TIMESLICE) |
|
|
118 thread2->set_priority(6); |
|
|
119 CHECK( 6 == thread1->get_priority() ); |
|
|
120 CHECK( 6 == thread2->get_priority() ); |
|
|
121 |
|
|
122 while ( 100 == q ) |
|
|
123 ; |
|
|
124 CHECK( 101 == q++ ); |
|
|
125 s1.wait(); |
|
|
126 CHECK( 103 == q++ ); |
|
|
127 #endif |
|
|
128 s0.post(); |
|
|
129 s1.wait(); |
|
|
130 } |
|
|
131 |
|
|
132 static void entry2( CYG_ADDRWORD data ) |
|
|
133 { |
|
|
134 CHECK( 2 == q++ ); |
|
|
135 s0.post(); |
|
|
136 CHECK( 7 == q++ ); |
|
|
137 s2.wait(); |
|
|
138 CHECK( 9 == q++ ); |
|
|
139 thread1->set_priority(6); |
|
|
140 CHECK( 10 == q++ ); |
|
|
141 thread2->set_priority(2); |
|
|
142 CHECK( 11 == q++ ); |
|
|
143 thread2->set_priority(7); |
|
|
144 |
|
|
145 #if !(CYG_SCHED_UNIQUE_PRIORITIES) && defined(CYGSEM_KERNEL_SCHED_TIMESLICE) |
|
|
146 CHECK( 6 == thread1->get_priority() ); |
|
|
147 CHECK( 6 == thread2->get_priority() ); |
|
|
148 |
|
|
149 CHECK( 100 == q++ ); |
|
|
150 while ( 101 == q ) |
|
|
151 ; |
|
|
152 CHECK( 102 == q++ ); |
|
|
153 s1.post(); |
|
|
154 #endif |
|
|
155 s0.post(); |
|
|
156 s2.wait(); |
|
|
157 } |
|
|
158 |
|
|
159 |
|
|
160 void thread2_main( void ) |
|
|
161 { |
|
|
162 CYG_TEST_INIT(); |
|
|
163 |
|
|
164 thread0 = new((void *)&thread[0]) |
|
|
165 Cyg_Thread(entry0, 0, STACKSIZE, (CYG_ADDRESS)stack[0] ); |
|
|
166 thread1 = new((void *)&thread[1]) |
|
|
167 Cyg_Thread(entry1, 1, STACKSIZE, (CYG_ADDRESS)stack[1] ); |
|
|
168 thread2 = new((void *)&thread[2]) |
|
|
169 Cyg_Thread(entry2, 2, STACKSIZE, (CYG_ADDRESS)stack[2] ); |
|
|
170 |
|
|
171 thread0->resume(); |
|
|
172 thread1->resume(); |
|
|
173 thread2->resume(); |
|
|
174 |
|
|
175 thread0->set_priority(5); |
|
|
176 thread1->set_priority(6); |
|
|
177 thread2->set_priority(7); |
|
|
178 |
|
|
179 if( 9 >= CYG_THREAD_MIN_PRIORITY ) |
|
|
180 CYG_TEST_FAIL_FINISH("Test requires priorities up to 9"); |
|
|
181 |
|
|
182 if( 2 <= CYG_THREAD_MAX_PRIORITY ) |
|
|
183 CYG_TEST_FAIL_FINISH("Test requires priorities as low as 2"); |
|
|
184 |
|
|
185 Cyg_Scheduler::start(); |
|
|
186 |
|
|
187 CYG_TEST_FAIL_FINISH("Unresolved"); |
|
|
188 } |
|
|
189 |
|
|
190 externC void |
|
|
191 cyg_start( void ) |
|
|
192 { |
|
|
193 thread2_main(); |
|
|
194 } |
|
|
195 |
|
|
196 // EOF thread2.cxx |