Mercurial > flash_v2
annotate packages/kernel/current/tests/sync3.cxx @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children | 6ed91473a1cd |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 3 // sync3.cxx | |
| 4 // | |
| 5 // Sync test 3 -- tests priorities and priority inheritance | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): dsm | |
| 35 // Contributors: dsm | |
| 36 // Date: 1998-02-18 | |
| 37 // Description: | |
| 38 // Creates mutexes and threads to set up starvation condition. | |
| 39 // Checks simple priority inheritance cures this. | |
| 40 // | |
| 41 // The starvation condition is caused by the highest priority | |
| 42 // thread, t0 waiting on a mutex which is never released because | |
| 43 // it is held by t2. t2 never releases it because t1 will be | |
| 44 // running at a priority level higher than t2 (but lower than t0). | |
| 45 // | |
| 46 // With priority inheritance enabled, t2 will inherit its priority | |
| 47 // from t0 when t0 tries to grab the mutex. | |
| 48 // | |
| 49 // Options: | |
| 50 // CYGIMP_THREAD_PRIORITY | |
| 51 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 52 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE | |
| 53 //####DESCRIPTIONEND#### | |
| 54 | |
| 55 #include <pkgconf/kernel.h> | |
| 56 | |
| 57 #include <cyg/kernel/thread.hxx> | |
| 58 #include <cyg/kernel/thread.inl> | |
| 59 #include <cyg/kernel/sched.hxx> | |
| 60 #include <cyg/kernel/mutex.hxx> | |
| 61 #include <cyg/kernel/sema.hxx> | |
| 62 | |
| 63 #include <cyg/infra/testcase.h> | |
| 64 | |
| 65 #include <cyg/kernel/sched.inl> | |
| 66 | |
| 67 #ifndef CYGIMP_THREAD_PRIORITY | |
| 68 #error "Thread priorities disabled" | |
| 69 #endif | |
| 70 | |
| 71 #define NTHREADS 3 | |
| 72 | |
| 73 #include "testaux.hxx" | |
| 74 | |
| 75 static Cyg_Mutex m0; | |
| 76 static Cyg_Binary_Semaphore s0, s1, s2; | |
| 77 | |
| 78 static cyg_ucount8 m0d = 9; | |
| 79 | |
| 80 static void check_priorities_normal() | |
| 81 { | |
| 82 CHECK( 5 == thread[0]->get_priority()); | |
| 83 CHECK( 6 == thread[1]->get_priority()); | |
| 84 CHECK( 7 == thread[2]->get_priority()); | |
| 85 } | |
| 86 | |
| 87 static void check_priorities_inherited() | |
| 88 { | |
| 89 CHECK( 5 == thread[0]->get_priority()); | |
| 90 CHECK( 6 == thread[1]->get_priority()); | |
| 91 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 92 CHECK( 5 == thread[2]->get_current_priority()); | |
| 93 #endif | |
| 94 CHECK( 7 == thread[2]->get_priority()); | |
| 95 | |
| 96 } | |
| 97 | |
| 98 static void entry0( CYG_ADDRWORD data ) | |
| 99 { | |
| 2 | 100 s0.wait(); // wait until t2 has gained m0.lock |
| 0 | 101 check_priorities_normal(); |
| 102 m0.lock(); { | |
| 103 check_priorities_normal(); | |
| 2 | 104 CHECK( 2 == m0d ); |
| 105 m0d = 0; | |
| 0 | 106 } m0.unlock(); |
| 107 check_priorities_normal(); | |
| 108 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 109 CYG_TEST_PASS_FINISH("Sync 3 OK -- priority inheritance worked"); | |
| 110 #else | |
| 111 CYG_TEST_FAIL_FINISH("Sync 3: thread not starved"); | |
| 112 #endif | |
| 113 // NOT REACHED | |
| 114 } | |
| 115 | |
| 116 static void entry1( CYG_ADDRWORD data ) | |
| 117 { | |
| 118 s1.wait(); | |
| 119 // The delay below will allow testing of the priority inheritance | |
| 120 // mechanism when scheduler does not guarantee to schedule threads | |
| 121 // in strict priority order. | |
| 122 for ( volatile cyg_ucount32 i=0; i < 100000; i++ ) | |
| 2 | 123 ; // math is hard |
| 0 | 124 |
| 125 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE | |
| 126 // thread0 should have stopped by this point | |
| 127 CYG_TEST_FAIL_FINISH("Sync 3: priority inheritance mechanism failed"); | |
| 128 #else | |
| 129 // With strict priority scheduling and no priority inheritance | |
| 130 // this is expected to happen. | |
| 131 CYG_TEST_PASS_FINISH("Sync 3 OK"); | |
| 132 #endif | |
| 133 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 134 } | |
| 135 | |
| 136 void entry2( CYG_ADDRWORD data ) | |
| 137 { | |
| 138 m0.lock(); { | |
| 2 | 139 CHECK( 9 == m0d ); |
| 0 | 140 check_priorities_normal(); |
| 2 | 141 s0.post(); // Now I have lock on m0, wake t0 then t1 |
| 0 | 142 check_priorities_inherited(); |
| 2 | 143 s1.post(); |
| 0 | 144 check_priorities_inherited(); |
| 2 | 145 m0d = 2; |
| 0 | 146 } m0.unlock(); |
| 147 check_priorities_normal(); | |
| 148 m0.lock(); { | |
| 149 check_priorities_normal(); | |
| 2 | 150 CHECK( 0 == m0d ); |
| 151 m0d = 21; | |
| 152 s2.wait(); // never posted | |
| 0 | 153 } m0.unlock(); |
| 154 } | |
| 155 | |
| 156 | |
| 157 | |
| 158 void sync3_main(void) | |
| 159 { | |
| 160 CYG_TEST_INIT(); | |
| 161 | |
| 162 new_thread( entry0, 0); | |
| 163 new_thread( entry1, 1); | |
| 164 new_thread( entry2, 2); | |
| 165 | |
| 166 thread[0]->set_priority(5); | |
| 167 thread[1]->set_priority(6); | |
| 168 thread[2]->set_priority(7); | |
| 169 | |
| 170 Cyg_Scheduler::start(); | |
| 171 | |
| 172 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 173 } | |
| 174 | |
| 175 externC void | |
| 176 cyg_start( void ) | |
| 177 { | |
| 178 sync3_main(); | |
| 179 } | |
| 180 | |
| 181 // EOF sync3.cxx |
