1 | /* |
---|
2 | * This test exercises the POSIX Spinlock manager. |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2009. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <stdlib.h> |
---|
17 | |
---|
18 | #include <pthread.h> |
---|
19 | |
---|
20 | #include <rtems.h> /* for task creation */ |
---|
21 | |
---|
22 | pthread_spinlock_t Spinlock; |
---|
23 | |
---|
24 | volatile int mainThreadSpinning; |
---|
25 | |
---|
26 | rtems_task SpinlockThread(rtems_task_argument arg) |
---|
27 | { |
---|
28 | int status; |
---|
29 | |
---|
30 | if ( mainThreadSpinning ) { |
---|
31 | puts( "main thread is not supposed to be spinning yet" ); |
---|
32 | exit(0); |
---|
33 | } |
---|
34 | puts( "pthread_spin_lock( &Spinlock ) from Thread -- OK" ); |
---|
35 | status = pthread_spin_lock( &Spinlock ); |
---|
36 | rtems_test_assert( status == 0 ); |
---|
37 | |
---|
38 | puts( "sleep to allow main thread to run" ); |
---|
39 | sleep( 1 ); |
---|
40 | |
---|
41 | if ( !mainThreadSpinning ) { |
---|
42 | puts( "main thread is not spinning on lock" ); |
---|
43 | exit(0); |
---|
44 | } |
---|
45 | |
---|
46 | puts( "pthread_spin_unlock( &Spinlock ) from Thread -- OK" ); |
---|
47 | status = pthread_spin_unlock( &Spinlock ); |
---|
48 | rtems_test_assert( status == 0 ); |
---|
49 | |
---|
50 | rtems_task_delete( RTEMS_SELF ); |
---|
51 | } |
---|
52 | |
---|
53 | /* |
---|
54 | * main entry point to the test |
---|
55 | */ |
---|
56 | |
---|
57 | #if defined(__rtems__) |
---|
58 | int test_main(void) |
---|
59 | #else |
---|
60 | int main( |
---|
61 | int argc, |
---|
62 | char **argv |
---|
63 | ) |
---|
64 | #endif |
---|
65 | { |
---|
66 | pthread_spinlock_t spinlock; |
---|
67 | int status; |
---|
68 | rtems_status_code rstatus; |
---|
69 | rtems_id taskid; |
---|
70 | |
---|
71 | puts( "\n\n*** POSIX SPINLOCK TEST 01 ***" ); |
---|
72 | |
---|
73 | puts( "pthread_spin_init( NULL, PTHREAD_PROCESS_PRIVATE ) -- EINVAL" ); |
---|
74 | status = pthread_spin_init( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
75 | rtems_test_assert( status == EINVAL ); |
---|
76 | |
---|
77 | puts( "pthread_spin_init( NULL, PTHREAD_PROCESS_SHARED ) -- EINVAL" ); |
---|
78 | status = pthread_spin_init( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
79 | rtems_test_assert( status == EINVAL ); |
---|
80 | |
---|
81 | puts( "pthread_spin_init( &spinlock, 0x1234 ) -- EINVAL" ); |
---|
82 | status = pthread_spin_init( &spinlock, 0x1234 ); |
---|
83 | rtems_test_assert( status == EINVAL ); |
---|
84 | |
---|
85 | puts( "pthread_spin_init( &spinlock, PTHREAD_PROCESS_SHARED ) -- EINVAL" ); |
---|
86 | status = pthread_spin_init( &spinlock, PTHREAD_PROCESS_SHARED ); |
---|
87 | rtems_test_assert( status == EINVAL ); |
---|
88 | |
---|
89 | /* This successfully creates one */ |
---|
90 | puts( "pthread_spin_init( &Spinlock, PTHREAD_PROCESS_PRIVATE ) -- OK" ); |
---|
91 | status = pthread_spin_init( &Spinlock, PTHREAD_PROCESS_PRIVATE ); |
---|
92 | rtems_test_assert( status == 0 ); |
---|
93 | |
---|
94 | puts( "pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE ) -- EAGAIN" ); |
---|
95 | status = pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE ); |
---|
96 | rtems_test_assert( status == EAGAIN ); |
---|
97 | |
---|
98 | puts( "pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE ) -- EAGAIN" ); |
---|
99 | status = pthread_spin_init( &spinlock, PTHREAD_PROCESS_PRIVATE ); |
---|
100 | rtems_test_assert( status == EAGAIN ); |
---|
101 | |
---|
102 | puts( "pthread_spin_lock( NULL ) -- EINVAL" ); |
---|
103 | status = pthread_spin_lock( NULL ); |
---|
104 | rtems_test_assert( status == EINVAL ); |
---|
105 | |
---|
106 | puts( "pthread_spin_trylock( NULL ) -- EINVAL" ); |
---|
107 | status = pthread_spin_trylock( NULL ); |
---|
108 | rtems_test_assert( status == EINVAL ); |
---|
109 | |
---|
110 | puts( "pthread_spin_unlock( NULL ) -- EINVAL" ); |
---|
111 | status = pthread_spin_unlock( NULL ); |
---|
112 | rtems_test_assert( status == EINVAL ); |
---|
113 | |
---|
114 | puts( "pthread_spin_destroy( NULL ) -- EINVAL" ); |
---|
115 | status = pthread_spin_destroy( NULL ); |
---|
116 | rtems_test_assert( status == EINVAL ); |
---|
117 | |
---|
118 | spinlock = 0; |
---|
119 | |
---|
120 | puts( "pthread_spin_lock( &spinlock ) -- EINVAL" ); |
---|
121 | status = pthread_spin_lock( &spinlock ); |
---|
122 | rtems_test_assert( status == EINVAL ); |
---|
123 | |
---|
124 | puts( "pthread_spin_trylock( &spinlock ) -- EINVAL" ); |
---|
125 | status = pthread_spin_trylock( &spinlock ); |
---|
126 | rtems_test_assert( status == EINVAL ); |
---|
127 | |
---|
128 | puts( "pthread_spin_unlock( &spinlock ) -- EINVAL" ); |
---|
129 | status = pthread_spin_unlock( &spinlock ); |
---|
130 | rtems_test_assert( status == EINVAL ); |
---|
131 | |
---|
132 | puts( "pthread_spin_destroy( &spinlock ) -- EINVAL" ); |
---|
133 | status = pthread_spin_destroy( &spinlock ); |
---|
134 | rtems_test_assert( status == EINVAL ); |
---|
135 | |
---|
136 | puts( "pthread_spin_unlock( &Spinlock ) -- already unlocked OK" ); |
---|
137 | status = pthread_spin_unlock( &Spinlock ); |
---|
138 | rtems_test_assert( status == 0 ); |
---|
139 | |
---|
140 | /* Now some basic locking and unlocking with a deadlock verification */ |
---|
141 | puts( "pthread_spin_lock( &Spinlock ) -- OK" ); |
---|
142 | status = pthread_spin_lock( &Spinlock ); |
---|
143 | rtems_test_assert( status == 0 ); |
---|
144 | |
---|
145 | puts( "pthread_spin_lock( &Spinlock ) -- EDEADLK" ); |
---|
146 | status = pthread_spin_lock( &Spinlock ); |
---|
147 | rtems_test_assert( status == EDEADLK ); |
---|
148 | |
---|
149 | puts( "pthread_spin_trylock( &Spinlock ) -- EDEADLK" ); |
---|
150 | status = pthread_spin_trylock( &Spinlock ); |
---|
151 | rtems_test_assert( status == EDEADLK ); |
---|
152 | |
---|
153 | puts( "pthread_spin_unlock( &Spinlock ) -- OK" ); |
---|
154 | status = pthread_spin_unlock( &Spinlock ); |
---|
155 | rtems_test_assert( status == 0 ); |
---|
156 | |
---|
157 | /* Try lock/unlock pair */ |
---|
158 | puts( "pthread_spin_trylock( &Spinlock ) -- OK" ); |
---|
159 | status = pthread_spin_trylock( &Spinlock ); |
---|
160 | rtems_test_assert( status == 0 ); |
---|
161 | |
---|
162 | puts( "pthread_spin_unlock( &Spinlock ) -- OK" ); |
---|
163 | status = pthread_spin_unlock( &Spinlock ); |
---|
164 | rtems_test_assert( status == 0 ); |
---|
165 | |
---|
166 | /* Let another thread lock a spinlock and we contend with it */ |
---|
167 | |
---|
168 | mainThreadSpinning = 0; |
---|
169 | |
---|
170 | /* Create a helper task */ |
---|
171 | rstatus = rtems_task_create( |
---|
172 | rtems_build_name( 'S', 'P', 'I', 'N' ), |
---|
173 | 1, |
---|
174 | RTEMS_MINIMUM_STACK_SIZE, |
---|
175 | RTEMS_DEFAULT_MODES, |
---|
176 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
177 | &taskid |
---|
178 | ); |
---|
179 | rtems_test_assert( rstatus == RTEMS_SUCCESSFUL ); |
---|
180 | |
---|
181 | rstatus = rtems_task_start( taskid, SpinlockThread, 0 ); |
---|
182 | rtems_test_assert( rstatus == RTEMS_SUCCESSFUL ); |
---|
183 | /* We should be preempted immediately. The thread is expected to: |
---|
184 | * + verify we haven't set the main thread spinning flag |
---|
185 | * + lock the spinlock |
---|
186 | * + delay |
---|
187 | */ |
---|
188 | |
---|
189 | mainThreadSpinning = 1; |
---|
190 | puts( "pthread_spin_lock( &Spinlock ) -- OK" ); |
---|
191 | status = pthread_spin_lock( &Spinlock ); |
---|
192 | rtems_test_assert( status == 0 ); |
---|
193 | |
---|
194 | /* The thread wakes up, unlocks spin lock, and deletes itself. |
---|
195 | * So when we get back here, about a second has passed and we now |
---|
196 | * have the spinlock locked. |
---|
197 | */ |
---|
198 | |
---|
199 | /* spin lock should be locked when we return so destroying it gives busy */ |
---|
200 | puts( "pthread_spin_destroy( &Spinlock ) -- EBUSY" ); |
---|
201 | status = pthread_spin_destroy( &Spinlock ); |
---|
202 | rtems_test_assert( status == EBUSY ); |
---|
203 | |
---|
204 | /* Unlock it for a normal destroy */ |
---|
205 | puts( "pthread_spin_unlock( &Spinlock ) -- OK" ); |
---|
206 | status = pthread_spin_unlock( &Spinlock ); |
---|
207 | rtems_test_assert( status == 0 ); |
---|
208 | |
---|
209 | puts( "pthread_spin_destroy( &Spinlock ) -- OK" ); |
---|
210 | status = pthread_spin_destroy( &Spinlock ); |
---|
211 | rtems_test_assert( status == 0 ); |
---|
212 | |
---|
213 | /*************** END OF TEST *****************/ |
---|
214 | puts( "*** END OF POSIX SPINLOCK TEST 01 ***" ); |
---|
215 | exit(0); |
---|
216 | } |
---|