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