comparison packages/kernel/current/tests/sync3.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 // sync3.cxx
4 //
5 // Sync test 3 -- tests priorities and priority inheritance
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 mutexes and threads to set up starvation condition.
37 // Checks simple priority inheritance cures this.
38 //
39 // The starvation condition is caused by the highest priority
40 // thread, t0 waiting on a mutex which is never released because
41 // it is held by t2. t2 never releases it because t1 will be
42 // running at a priority level higher than t2 (but lower than t0).
43 //
44 // With priority inheritance enabled, t2 will inherit its priority
45 // from t0 when t0 tries to grab the mutex.
46 //
47 // Options:
48 // CYGIMP_THREAD_PRIORITY
49 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE
50 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE
51 //####DESCRIPTIONEND####
52
53 #include <pkgconf/kernel.h>
54
55 #include <cyg/kernel/thread.hxx>
56 #include <cyg/kernel/thread.inl>
57 #include <cyg/kernel/sched.hxx>
58 #include <cyg/kernel/mutex.hxx>
59 #include <cyg/kernel/sema.hxx>
60
61 #include <cyg/infra/testcase.h>
62
63 #include <cyg/kernel/sched.inl>
64
65 #ifndef CYGIMP_THREAD_PRIORITY
66 #error "Thread priorities disabled"
67 #endif
68
69 #define NTHREADS 3
70
71 #include "testaux.hxx"
72
73 static Cyg_Mutex m0;
74 static Cyg_Binary_Semaphore s0, s1, s2;
75
76 static cyg_ucount8 m0d = 9;
77
78 static void check_priorities_normal()
79 {
80 CHECK( 5 == thread[0]->get_priority());
81 CHECK( 6 == thread[1]->get_priority());
82 CHECK( 7 == thread[2]->get_priority());
83 }
84
85 static void check_priorities_inherited()
86 {
87 CHECK( 5 == thread[0]->get_priority());
88 CHECK( 6 == thread[1]->get_priority());
89 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE
90 CHECK( 5 == thread[2]->get_current_priority());
91 #endif
92 CHECK( 7 == thread[2]->get_priority());
93
94 }
95
96 static void entry0( CYG_ADDRWORD data )
97 {
98 s0.wait(); // wait until t2 has gained m0.lock
99 check_priorities_normal();
100 m0.lock(); {
101 check_priorities_normal();
102 CHECK( 2 == m0d );
103 m0d = 0;
104 } m0.unlock();
105 check_priorities_normal();
106 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE
107 CYG_TEST_PASS_FINISH("Sync 3 OK -- priority inheritance worked");
108 #else
109 CYG_TEST_FAIL_FINISH("Sync 3: thread not starved");
110 #endif
111 // NOT REACHED
112 }
113
114 static void entry1( CYG_ADDRWORD data )
115 {
116 s1.wait();
117 // The delay below will allow testing of the priority inheritance
118 // mechanism when scheduler does not guarantee to schedule threads
119 // in strict priority order.
120 for ( volatile cyg_ucount32 i=0; i < 100000; i++ )
121 ; // math is hard
122
123 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE
124 // thread0 should have stopped by this point
125 CYG_TEST_FAIL_FINISH("Sync 3: priority inheritance mechanism failed");
126 #else
127 // With strict priority scheduling and no priority inheritance
128 // this is expected to happen.
129 CYG_TEST_PASS_FINISH("Sync 3 OK");
130 #endif
131 CYG_TEST_FAIL_FINISH("Not reached");
132 }
133
134 void entry2( CYG_ADDRWORD data )
135 {
136 m0.lock(); {
137 CHECK( 9 == m0d );
138 check_priorities_normal();
139 s0.post(); // Now I have lock on m0, wake t0 then t1
140 check_priorities_inherited();
141 s1.post();
142 check_priorities_inherited();
143 m0d = 2;
144 } m0.unlock();
145 check_priorities_normal();
146 m0.lock(); {
147 check_priorities_normal();
148 CHECK( 0 == m0d );
149 m0d = 21;
150 s2.wait(); // never posted
151 } m0.unlock();
152 }
153
154
155
156 void sync3_main(void)
157 {
158 CYG_TEST_INIT();
159
160 new_thread( entry0, 0);
161 new_thread( entry1, 1);
162 new_thread( entry2, 2);
163
164 thread[0]->set_priority(5);
165 thread[1]->set_priority(6);
166 thread[2]->set_priority(7);
167
168 Cyg_Scheduler::start();
169
170 CYG_TEST_FAIL_FINISH("Not reached");
171 }
172
173 externC void
174 cyg_start( void )
175 {
176 sync3_main();
177 }
178
179 // EOF sync3.cxx