Mercurial > ecos
annotate packages/kernel/current/tests/kmutex1.c @ 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 | e0c0827131d1 |
| rev | line source |
|---|---|
| 0 | 1 /*================================================================= |
| 2 // | |
| 3 // kmutex1.c | |
| 4 // | |
| 5 // Kernel C API Mutex test 1 | |
| 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-03-23 | |
| 37 // Description: Tests basic mutex functionality. | |
| 38 // Omissions: Timed wait. | |
| 39 //####DESCRIPTIONEND#### | |
| 40 */ | |
| 41 | |
| 2 | 42 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
| 43 | |
| 0 | 44 #include <cyg/kernel/kapi.h> |
| 45 | |
| 46 #include <cyg/infra/testcase.h> | |
| 47 | |
| 48 #ifdef CYGFUN_KERNEL_API_C | |
| 49 | |
| 50 #include "testaux.h" | |
| 51 | |
| 52 #define NTHREADS 3 | |
| 2 | 53 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
| 0 | 54 |
| 55 static cyg_handle_t thread[NTHREADS]; | |
| 56 | |
| 57 static cyg_thread thread_obj[NTHREADS]; | |
| 58 static char stack[NTHREADS][STACKSIZE]; | |
| 59 | |
| 60 | |
| 61 static cyg_mutex_t m0, m1; | |
| 62 static cyg_cond_t cvar0, cvar1, cvar2; | |
| 63 | |
| 64 static cyg_ucount8 m0d=0, m1d=0; | |
| 65 | |
| 66 static void finish( cyg_ucount8 t ) | |
| 67 { | |
| 68 cyg_mutex_lock( &m1 ); { | |
| 69 m1d |= 1<<t; | |
| 70 if( 0x7 == m1d ) | |
| 71 CYG_TEST_PASS_FINISH("Kernel C API Mutex 1 OK"); | |
| 72 cyg_cond_wait( &cvar2 ); | |
| 73 } /* cyg_mutex_unlock( &m1 ); */ | |
| 74 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 75 } | |
| 76 | |
| 77 static void entry0( cyg_addrword_t data ) | |
| 78 { | |
| 79 cyg_mutex_lock( &m0 ); { | |
| 80 CHECK( ! cyg_mutex_trylock( &m0 ) ); | |
| 81 cyg_mutex_lock( &m1 ); { | |
| 82 CHECK( ! cyg_mutex_trylock( &m0 ) ); | |
| 83 } cyg_mutex_unlock( &m1 ); | |
| 84 } cyg_mutex_unlock( &m0 ); | |
| 85 | |
| 86 cyg_mutex_lock( &m0 ); { | |
| 87 while ( 0 == m0d ) | |
| 88 cyg_cond_wait( &cvar0 ); | |
| 89 CHECK( 1 == m0d++ ); | |
| 90 cyg_cond_signal( &cvar0 ); | |
| 91 while ( 4 != m0d ) | |
| 92 cyg_cond_wait( &cvar1 ); | |
| 93 CHECK( 4 == m0d ); | |
| 94 } cyg_mutex_unlock( &m0 ); | |
| 95 | |
| 96 finish( (cyg_ucount8)data ); | |
| 97 } | |
| 98 | |
| 99 static void entry1( cyg_addrword_t data ) | |
| 100 { | |
| 101 cyg_mutex_lock( &m0 ); { | |
| 102 CHECK( cyg_mutex_trylock( &m1 ) ); { | |
| 103 } cyg_mutex_unlock( &m1 ); | |
| 104 } cyg_mutex_unlock( &m0 ); | |
| 105 | |
| 106 cyg_mutex_lock( &m0 ); { | |
| 107 CHECK( 0 == m0d++ ); | |
| 108 cyg_cond_broadcast( &cvar0 ); | |
| 109 } cyg_mutex_unlock( &m0 ); | |
| 110 | |
| 111 cyg_mutex_lock( &m0 ); { | |
| 112 while( 1 == m0d ) | |
| 113 cyg_cond_wait( &cvar0 ); | |
| 114 CHECK( 2 == m0d++ ); | |
| 115 cyg_cond_signal( &cvar0 ); | |
| 116 while( 3 == m0d ) | |
| 117 cyg_cond_wait( &cvar1 ); | |
| 118 } cyg_mutex_unlock( &m0 ); | |
| 119 | |
| 120 finish( (cyg_ucount8)data ); | |
| 121 } | |
| 122 | |
| 123 static void entry2( cyg_addrword_t data ) | |
| 124 { | |
| 125 cyg_mutex_lock( &m0 ); { | |
| 126 while( 3 != m0d ) { | |
| 127 cyg_cond_wait( &cvar0 ); | |
| 128 } | |
| 129 CHECK( 3 == m0d++ ); | |
| 130 cyg_cond_broadcast( &cvar1 ); | |
| 131 } cyg_mutex_unlock( &m0 ); | |
| 132 | |
| 133 finish( (cyg_ucount8)data ); | |
| 134 } | |
| 135 | |
| 136 void kmutex1_main( void ) | |
| 137 { | |
| 138 CYG_TEST_INIT(); | |
| 139 | |
| 140 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmutex1-0", | |
| 2 | 141 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
| 0 | 142 cyg_thread_resume(thread[0]); |
| 143 | |
| 144 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmutex1-1", | |
| 2 | 145 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
| 0 | 146 cyg_thread_resume(thread[1]); |
| 147 | |
| 148 cyg_thread_create(4, entry2 , (cyg_addrword_t)2, "kmutex1-2", | |
| 2 | 149 (void *)stack[2], STACKSIZE, &thread[2], &thread_obj[2]); |
| 0 | 150 cyg_thread_resume(thread[2]); |
| 151 | |
| 152 cyg_mutex_init( &m0 ); | |
| 153 cyg_mutex_init( &m1 ); | |
| 154 | |
| 155 cyg_cond_init( &cvar0, &m0 ); | |
| 156 cyg_cond_init( &cvar1, &m0 ); | |
| 157 cyg_cond_init( &cvar2, &m1 ); | |
| 158 | |
| 159 cyg_scheduler_start(); | |
| 160 | |
| 161 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 162 } | |
| 163 | |
| 164 externC void | |
| 165 cyg_start( void ) | |
| 166 { | |
| 167 kmutex1_main(); | |
| 168 } | |
| 169 | |
| 170 #else /* def CYGFUN_KERNEL_API_C */ | |
| 171 externC void | |
| 172 cyg_start( void ) | |
| 173 { | |
| 174 CYG_TEST_INIT(); | |
| 2 | 175 CYG_TEST_NA("Kernel C API layer disabled"); |
| 0 | 176 } |
| 177 #endif /* def CYGFUN_KERNEL_API_C */ | |
| 178 | |
| 179 /* EOF kmutex1.c */ |
