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.org/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 | const char rtems_test_name[] = "PSXSPIN 2"; |
---|
30 | |
---|
31 | /* forward declarations to avoid warnings */ |
---|
32 | int test_main(void); |
---|
33 | rtems_task SpinlockThread(rtems_task_argument arg); |
---|
34 | |
---|
35 | pthread_spinlock_t Spinlock; |
---|
36 | |
---|
37 | rtems_task SpinlockThread(rtems_task_argument arg) |
---|
38 | { |
---|
39 | int status; |
---|
40 | |
---|
41 | puts( "pthread_spin_trylock( &Spinlock ) -- EBUSY" ); |
---|
42 | status = pthread_spin_trylock( &Spinlock ); |
---|
43 | rtems_test_assert( status == EBUSY ); |
---|
44 | |
---|
45 | puts( "pthread_spin_unlock( &Spinlock ) -- EPERM" ); |
---|
46 | status = pthread_spin_unlock( &Spinlock ); |
---|
47 | rtems_test_assert( status == EPERM ); |
---|
48 | |
---|
49 | rtems_task_delete( RTEMS_SELF ); |
---|
50 | } |
---|
51 | |
---|
52 | /* |
---|
53 | * main entry point to the test |
---|
54 | */ |
---|
55 | |
---|
56 | #if defined(__rtems__) |
---|
57 | int test_main(void) |
---|
58 | #else |
---|
59 | int main( |
---|
60 | int argc, |
---|
61 | char **argv |
---|
62 | ) |
---|
63 | #endif |
---|
64 | { |
---|
65 | int status; |
---|
66 | rtems_status_code rstatus; |
---|
67 | rtems_id taskid; |
---|
68 | |
---|
69 | TEST_BEGIN(); |
---|
70 | |
---|
71 | /* This successfully creates one */ |
---|
72 | puts( "pthread_spin_init( &Spinlock, PTHREAD_PROCESS_PRIVATE ) -- OK" ); |
---|
73 | status = pthread_spin_init( &Spinlock, PTHREAD_PROCESS_PRIVATE ); |
---|
74 | rtems_test_assert( status == 0 ); |
---|
75 | |
---|
76 | /* Lock it */ |
---|
77 | puts( "pthread_spin_lock( &Spinlock ) -- OK" ); |
---|
78 | status = pthread_spin_lock( &Spinlock ); |
---|
79 | rtems_test_assert( status == 0 ); |
---|
80 | |
---|
81 | /* Create a helper task */ |
---|
82 | rstatus = rtems_task_create( |
---|
83 | rtems_build_name( 'S', 'P', 'I', 'N' ), |
---|
84 | 1, |
---|
85 | RTEMS_MINIMUM_STACK_SIZE, |
---|
86 | RTEMS_DEFAULT_MODES, |
---|
87 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
88 | &taskid |
---|
89 | ); |
---|
90 | rtems_test_assert( rstatus == RTEMS_SUCCESSFUL ); |
---|
91 | |
---|
92 | rstatus = rtems_task_start( taskid, SpinlockThread, 0 ); |
---|
93 | rtems_test_assert( rstatus == RTEMS_SUCCESSFUL ); |
---|
94 | |
---|
95 | sleep(1); |
---|
96 | |
---|
97 | puts( "pthread_spin_unlock( &Spinlock ) -- OK" ); |
---|
98 | status = pthread_spin_unlock( &Spinlock ); |
---|
99 | rtems_test_assert( status == 0 ); |
---|
100 | |
---|
101 | puts( "pthread_spin_destroy( &Spinlock ) -- OK" ); |
---|
102 | status = pthread_spin_destroy( &Spinlock ); |
---|
103 | rtems_test_assert( status == 0 ); |
---|
104 | |
---|
105 | /*************** END OF TEST *****************/ |
---|
106 | TEST_END(); |
---|
107 | exit(0); |
---|
108 | } |
---|