]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/sync3.cxx
Initial revision
[karo-tx-redboot.git] / packages / kernel / v2_0 / tests / sync3.cxx
1 //==========================================================================
2 //
3 //        sync3.cxx
4 //
5 //        Sync test 3 -- tests priorities and priority inheritance
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 Red Hat, 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 version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     dsm
44 // Contributors:    dsm
45 // Date:          1998-02-18
46 // Description: 
47 //     Creates mutexes and threads to set up starvation condition.
48 //     Checks simple priority inheritance cures this.
49 //     
50 //     The starvation condition is caused by the highest priority
51 //     thread, t0 waiting on a mutex which is never released because
52 //     it is held by t2.  t2 never releases it because t1 will be
53 //     running at a priority level higher than t2 (but lower than t0).
54 //     
55 //     With priority inheritance enabled, t2 will inherit its priority
56 //     from t0 when t0 tries to grab the mutex.
57 //     
58 // Options:
59 //     CYGIMP_THREAD_PRIORITY
60 //     CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
61 //     CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE
62 //####DESCRIPTIONEND####
63
64 #include <pkgconf/kernel.h>
65
66 #include <cyg/kernel/thread.hxx>
67 #include <cyg/kernel/thread.inl>
68 #include <cyg/kernel/sched.hxx>
69 #include <cyg/kernel/mutex.hxx>
70 #include <cyg/kernel/sema.hxx>
71
72 #include <cyg/infra/testcase.h>
73
74 #include <cyg/kernel/sched.inl>
75
76 #if defined(CYGIMP_THREAD_PRIORITY) && \
77     !defined(CYGPKG_KERNEL_SMP_SUPPORT)
78
79 // ------------------------------------------------------------------------
80 // Manufacture a simpler feature test macro for priority inheritance than
81 // the configuration gives us. We have priority inheritance if it is configured
82 // as the only protocol, or if it is the default protocol for dynamic protocol
83 // choice.
84 // FIXME: If we have dynamic protocol choice, we can also set priority inheritance
85 // as the protocol to be used on the mutexes we are interested in. At present we
86 // do not do this.
87
88 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
89 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
90 #  ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT
91 #   define PRIORITY_INHERITANCE
92 #  else
93 #   undef PRIORITY_INHERITANCE
94 #  endif
95 # else
96 #  define PRIORITY_INHERITANCE
97 # endif
98 #else
99 # undef PRIORITY_INHERITANCE
100 #endif
101
102 // ------------------------------------------------------------------------
103
104 #define NTHREADS 3
105
106 #include "testaux.hxx"
107
108 static Cyg_Mutex m0;
109 static Cyg_Binary_Semaphore s0, s1, s2;
110
111 static cyg_ucount8 m0d = 9;
112
113 static void check_priorities_normal()
114 {
115     CHECK( 5 == thread[0]->get_priority());
116     CHECK( 6 == thread[1]->get_priority());
117     CHECK( 7 == thread[2]->get_priority());
118 }
119
120 static void check_priorities_inherited()
121 {
122     CHECK( 5 == thread[0]->get_priority());
123     CHECK( 6 == thread[1]->get_priority());
124 #ifdef PRIORITY_INHERITANCE
125     CHECK( 5 == thread[2]->get_current_priority());
126 #endif
127     CHECK( 7 == thread[2]->get_priority());
128
129 }
130
131 static void entry0( CYG_ADDRWORD data )
132 {
133     s0.wait();                  // wait until t2 has gained m0.lock
134     check_priorities_normal();
135     m0.lock(); {
136         check_priorities_normal();
137         CHECK( 2 == m0d );
138         m0d = 0;
139     } m0.unlock();
140     check_priorities_normal();
141 #ifdef PRIORITY_INHERITANCE
142     CYG_TEST_PASS_FINISH("Sync 3 OK -- priority inheritance worked");
143 #else
144     CYG_TEST_FAIL_FINISH("Sync 3: thread not starved");
145 #endif
146     // NOT REACHED
147 }
148
149 static void entry1( CYG_ADDRWORD data )
150 {
151     s1.wait();
152     // The delay below will allow testing of the priority inheritance
153     // mechanism when scheduler does not guarantee to schedule threads
154     // in strict priority order.
155     for ( volatile cyg_ucount32 i=0; i < 100000; i++ )
156         ; // math is hard
157
158 #ifdef PRIORITY_INHERITANCE
159     // thread0 should have stopped by this point
160     CYG_TEST_FAIL_FINISH("Sync 3: priority inheritance mechanism failed");
161 #else
162     // With strict priority scheduling and no priority inheritance
163     // this is expected to happen.
164     CYG_TEST_PASS_FINISH("Sync 3 OK");
165 #endif
166     CYG_TEST_FAIL_FINISH("Not reached");
167 }
168
169 void entry2( CYG_ADDRWORD data )
170 {
171     m0.lock(); {
172         CHECK( 9 == m0d );
173         check_priorities_normal();
174         s0.post();              // Now I have lock on m0, wake t0 then t1 
175         check_priorities_inherited();
176         s1.post();
177         check_priorities_inherited();
178         m0d = 2;
179     } m0.unlock();
180     check_priorities_normal();
181     m0.lock(); {
182         check_priorities_normal();
183         CHECK( 0 == m0d );
184         m0d = 21;
185         s2.wait();              // never posted
186     } m0.unlock();
187 }
188
189
190
191 void sync3_main(void)
192 {
193     CYG_TEST_INIT();
194
195     new_thread( entry0, 0);
196     new_thread( entry1, 1);
197     new_thread( entry2, 2);
198
199     thread[0]->set_priority(5);
200     thread[1]->set_priority(6);
201     thread[2]->set_priority(7);
202
203     Cyg_Scheduler::start();
204
205     CYG_TEST_FAIL_FINISH("Not reached");
206 }
207
208 externC void
209 cyg_start( void )
210 {
211 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
212     cyg_hal_invoke_constructors();
213 #endif
214     sync3_main();
215 }
216
217 #else // defined(CYGIMP_THREAD_PRIORITY) etc
218
219 externC void
220 cyg_start( void )
221
222     CYG_TEST_INIT();
223     CYG_TEST_INFO("Sync3 test requires:\n"
224                          "defined(CYGIMP_THREAD_PRIORITY) &&\n"
225                          "!defined(CYGPKG_KERNEL_SMP_SUPPORT)\n");
226     CYG_TEST_NA("Sync3 test requirements");
227
228 }
229
230 #endif // defined(CYGIMP_THREAD_PRIORITY) etc
231
232 // EOF sync3.cxx