source: rtems/testsuites/psxtests/psxspin01/test.c @ 6c2de60

4.115
Last change on this file since 6c2de60 was 6c2de60, checked in by Joel Sherrill <joel.sherrill@…>, on 05/11/12 at 19:12:11

psxtests - Eliminate missing prototype warnings

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