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