|
0
|
1 /*================================================================= |
|
|
2 // |
|
|
3 // ksem1.c |
|
|
4 // |
|
|
5 // C API semaphore 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 |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): dsm |
|
|
33 // Contributors: dsm |
|
|
34 // Date: 1998-03-20 |
|
|
35 // Description: Tests basic semaphore functionality. |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
2
|
39 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
|
40 |
|
0
|
41 #include <cyg/kernel/kapi.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #ifdef CYGFUN_KERNEL_API_C |
|
|
46 |
|
|
47 #include "testaux.h" |
|
|
48 |
|
|
49 #define NTHREADS 2 |
|
2
|
50 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
0
|
51 |
|
|
52 static cyg_handle_t thread[NTHREADS]; |
|
|
53 |
|
|
54 static cyg_thread thread_obj[NTHREADS]; |
|
|
55 static char stack[NTHREADS][STACKSIZE]; |
|
|
56 |
|
|
57 |
|
|
58 static cyg_sem_t s0, s1, s2; |
|
|
59 |
|
|
60 static volatile cyg_ucount8 q = 0; |
|
|
61 |
|
|
62 static void entry0( cyg_addrword_t data ) |
|
|
63 { |
|
|
64 cyg_ucount32 val; |
|
|
65 |
|
|
66 cyg_semaphore_wait(&s0); |
|
|
67 CHECK( 1 == q++ ); |
|
|
68 cyg_semaphore_post(&s1); |
|
|
69 cyg_semaphore_wait(&s0); |
|
|
70 CHECK( 3 == q++ ); |
|
|
71 cyg_semaphore_peek(&s0, &val); |
|
|
72 CHECK( 0 == val); |
|
|
73 CHECK( ! cyg_semaphore_trywait(&s0) ); |
|
|
74 cyg_semaphore_post(&s0); |
|
|
75 CHECK( 4 == q++ ); |
|
|
76 cyg_semaphore_peek(&s0, &val); |
|
|
77 CHECK( 1 == val); |
|
|
78 cyg_semaphore_post(&s0); |
|
|
79 cyg_semaphore_peek(&s0, &val); |
|
|
80 CHECK( 2 == val); |
|
|
81 cyg_semaphore_post(&s1); |
|
|
82 cyg_semaphore_peek(&s2, &val); |
|
|
83 CHECK( 0 == val); |
|
|
84 cyg_semaphore_wait(&s2); |
|
|
85 CHECK( 6 == q++ ); |
|
|
86 CYG_TEST_PASS_FINISH("Kernel C API Semaphore 1 OK"); |
|
|
87 } |
|
|
88 |
|
|
89 static void entry1( cyg_addrword_t data ) |
|
|
90 { |
|
2
|
91 cyg_count32 val; |
|
0
|
92 |
|
|
93 cyg_semaphore_peek(&s1, &val); |
|
|
94 CHECK( 2 == val); |
|
|
95 cyg_semaphore_wait(&s1); |
|
|
96 cyg_semaphore_peek(&s1, &val); |
|
|
97 CHECK( 1 == val); |
|
|
98 cyg_semaphore_wait(&s1); |
|
|
99 CHECK( 0 == q++ ); |
|
|
100 cyg_semaphore_peek(&s0, &val); |
|
|
101 CHECK( 0 == val); |
|
|
102 cyg_semaphore_post(&s0); |
|
|
103 cyg_semaphore_wait(&s1); |
|
|
104 CHECK( 2 == q++ ); |
|
|
105 cyg_semaphore_post(&s0); |
|
|
106 cyg_semaphore_wait(&s1); |
|
|
107 CHECK( 5 == q++ ); |
|
|
108 cyg_semaphore_peek(&s0, &val); |
|
|
109 CHECK( 2 == val); |
|
|
110 CHECK( cyg_semaphore_trywait(&s0) ); |
|
|
111 cyg_semaphore_peek(&s0, &val); |
|
|
112 CHECK( 1 == val); |
|
|
113 CHECK( cyg_semaphore_trywait(&s0) ); |
|
|
114 cyg_semaphore_peek(&s0, &val); |
|
|
115 CHECK( 0 == val); |
|
|
116 cyg_semaphore_post(&s2); |
|
|
117 cyg_semaphore_wait(&s0); |
|
|
118 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
119 } |
|
|
120 |
|
|
121 void ksem1_main( void ) |
|
|
122 { |
|
|
123 CYG_TEST_INIT(); |
|
|
124 |
|
|
125 cyg_semaphore_init( &s0, 0); |
|
|
126 cyg_semaphore_init( &s1, 2); |
|
|
127 cyg_semaphore_init( &s2, 0); |
|
|
128 |
|
|
129 cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "ksem1-0", |
|
2
|
130 (void *)stack[0], STACKSIZE,&thread[0], &thread_obj[0]); |
|
0
|
131 cyg_thread_resume(thread[0]); |
|
|
132 |
|
|
133 cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "ksem1-1", |
|
2
|
134 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
0
|
135 cyg_thread_resume(thread[1]); |
|
|
136 |
|
|
137 cyg_scheduler_start(); |
|
|
138 |
|
|
139 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
140 } |
|
|
141 |
|
|
142 externC void |
|
|
143 cyg_start( void ) |
|
|
144 { |
|
|
145 ksem1_main(); |
|
|
146 } |
|
|
147 |
|
|
148 |
|
|
149 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
150 externC void |
|
|
151 cyg_start( void ) |
|
|
152 { |
|
|
153 CYG_TEST_INIT(); |
|
2
|
154 CYG_TEST_NA("Kernel C API layer disabled"); |
|
0
|
155 } |
|
|
156 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
157 |
|
|
158 /* EOF ksem1.c */ |