source: rtems/testsuites/psxtests/psxspin01/test.c @ 2317457

4.104.115
Last change on this file since 2317457 was 2317457, checked in by Joel Sherrill <joel.sherrill@…>, on 12/08/09 at 17:52:53

2009-12-08 Joel Sherrill <joel.sherrill@…>

  • include/pmacros.h, psx01/task.c, psx02/init.c, psx02/task.c, psx03/init.c, psx03/task.c, psx04/init.c, psx04/task1.c, psx04/task2.c, psx04/task3.c, psx05/init.c, psx05/task.c, psx05/task2.c, psx05/task3.c, psx06/init.c, psx06/task.c, psx06/task2.c, psx07/init.c, psx08/init.c, psx08/task2.c, psx08/task3.c, psx09/init.c, psx10/init.c, psx10/task.c, psx10/task2.c, psx10/task3.c, psx11/init.c, psx11/task.c, psx12/init.c, psxalarm01/init.c, psxbarrier01/test.c, psxcancel01/init.c, psxchroot01/test.c, psxclock/init.c, psxfile01/test.c, psxfile01/test_cat.c, psxfile01/test_extend.c, psxfile01/test_write.c, psxitimer/init.c, psxkey01/task.c, psxkey02/init.c, psxkey03/init.c, psxmount/test.c, psxmsgq01/init.c, psxmsgq03/init.c, psxmsgq04/init.c, psxreaddir/test.c, psxrwlock01/test.c, psxsem01/init.c, psxsignal01/init.c, psxsignal01/task1.c, psxsignal02/init.c, psxsignal03/init.c, psxsignal05/init.c, psxspin01/test.c, psxspin02/test.c, psxstack01/init.c, psxstat/test.c, psxtime/test.c, psxualarm/init.c: Use rtems_test_assert() consistently instead of system assert(). rtems_test_assert() is designed to integrate into the RTEMS test suite infrastructure.
  • Property mode set to 100644
File size: 6.5 KB
Line 
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
22pthread_spinlock_t Spinlock;
23
24volatile int mainThreadSpinning;
25
26rtems_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__)
58int test_main(void)
59#else
60int 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}
Note: See TracBrowser for help on using the repository browser.