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