1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2013. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <coverhd.h> |
---|
15 | #include <tmacros.h> |
---|
16 | #include <timesys.h> |
---|
17 | #include "test_support.h" |
---|
18 | #include <pthread.h> |
---|
19 | #include <sched.h> |
---|
20 | #include <rtems/timerdrv.h> |
---|
21 | |
---|
22 | const char rtems_test_name[] = "PSXTMRWLOCK 07"; |
---|
23 | |
---|
24 | /* forward declarations to avoid warnings */ |
---|
25 | void *POSIX_Init(void *argument); |
---|
26 | void *Middle(void *argument); |
---|
27 | void *Low(void *argument); |
---|
28 | |
---|
29 | pthread_rwlock_t rwlock; |
---|
30 | |
---|
31 | void *Low( |
---|
32 | void *argument |
---|
33 | ) |
---|
34 | { |
---|
35 | int status; |
---|
36 | benchmark_timer_t end_time; |
---|
37 | |
---|
38 | /* write locking */ |
---|
39 | status = pthread_rwlock_wrlock(&rwlock); |
---|
40 | end_time = benchmark_timer_read(); |
---|
41 | |
---|
42 | rtems_test_assert( status == 0 ); |
---|
43 | |
---|
44 | put_time( |
---|
45 | "pthread_rwlock_unlock: thread waiting preempt", |
---|
46 | end_time, |
---|
47 | OPERATION_COUNT, |
---|
48 | 0, |
---|
49 | 0 |
---|
50 | ); |
---|
51 | |
---|
52 | TEST_END(); |
---|
53 | rtems_test_exit( 0 ); |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | |
---|
57 | void *Middle( |
---|
58 | void *argument |
---|
59 | ) |
---|
60 | { |
---|
61 | int status; |
---|
62 | |
---|
63 | /* write locking */ |
---|
64 | status = pthread_rwlock_wrlock(&rwlock); |
---|
65 | rtems_test_assert( status == 0 ); |
---|
66 | |
---|
67 | /* thread switch occurs */ |
---|
68 | |
---|
69 | status = pthread_rwlock_unlock(&rwlock); /* unlock the rwlock */ |
---|
70 | rtems_test_assert( status == 0 ); |
---|
71 | /* thread switch occurs */ |
---|
72 | |
---|
73 | /* should never return */ |
---|
74 | rtems_test_assert( FALSE ); |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | void *POSIX_Init( |
---|
79 | void *argument |
---|
80 | ) |
---|
81 | { |
---|
82 | int i; |
---|
83 | int status; |
---|
84 | pthread_t threadId; |
---|
85 | pthread_attr_t attr; |
---|
86 | struct sched_param param; |
---|
87 | pthread_rwlockattr_t rw_attr; |
---|
88 | |
---|
89 | TEST_BEGIN(); |
---|
90 | |
---|
91 | /* |
---|
92 | * Deliberately create the lock BEFORE the threads. This way the |
---|
93 | * threads should preempt this thread and block as they are created. |
---|
94 | */ |
---|
95 | status = pthread_rwlockattr_init( &rw_attr ); |
---|
96 | rtems_test_assert( status == 0 ); |
---|
97 | status = pthread_rwlock_init( &rwlock, &rw_attr ); |
---|
98 | rtems_test_assert( status == 0 ); |
---|
99 | |
---|
100 | /* |
---|
101 | * Obtain the lock so the threads will block. |
---|
102 | */ |
---|
103 | status = pthread_rwlock_wrlock(&rwlock); |
---|
104 | rtems_test_assert( status == 0 ); |
---|
105 | |
---|
106 | /* |
---|
107 | * Now lower our priority |
---|
108 | */ |
---|
109 | status = pthread_attr_init( &attr ); |
---|
110 | rtems_test_assert( status == 0 ); |
---|
111 | |
---|
112 | status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); |
---|
113 | rtems_test_assert( status == 0 ); |
---|
114 | |
---|
115 | status = pthread_attr_setschedpolicy( &attr, SCHED_RR ); |
---|
116 | rtems_test_assert( status == 0 ); |
---|
117 | |
---|
118 | param.sched_priority = 2; |
---|
119 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
120 | rtems_test_assert( status == 0 ); |
---|
121 | |
---|
122 | /* |
---|
123 | * And create rest of threads as more important than we are. They |
---|
124 | * will preempt us as they are created and block. |
---|
125 | */ |
---|
126 | for ( i=0 ; i < OPERATION_COUNT ; i++ ) { |
---|
127 | |
---|
128 | param.sched_priority = 3 + i; |
---|
129 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
130 | rtems_test_assert( status == 0 ); |
---|
131 | |
---|
132 | status = pthread_create( |
---|
133 | &threadId, |
---|
134 | &attr, |
---|
135 | (i == OPERATION_COUNT - 1) ? Low : Middle, |
---|
136 | NULL |
---|
137 | ); |
---|
138 | rtems_test_assert( status == 0 ); |
---|
139 | } |
---|
140 | |
---|
141 | /* |
---|
142 | * Now start the timer which will be stopped in Low |
---|
143 | * Release the lock. Threads unblock and preempt. |
---|
144 | */ |
---|
145 | benchmark_timer_initialize(); |
---|
146 | |
---|
147 | status = pthread_rwlock_unlock(&rwlock); |
---|
148 | /* thread switch occurs */ |
---|
149 | |
---|
150 | /* should never return */ |
---|
151 | rtems_test_assert( FALSE ); |
---|
152 | return NULL; |
---|
153 | } |
---|
154 | |
---|
155 | /* configuration information */ |
---|
156 | |
---|
157 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
158 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
159 | |
---|
160 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
161 | #define CONFIGURE_MAXIMUM_POSIX_RWLOCKS 1 |
---|
162 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
163 | |
---|
164 | #define CONFIGURE_INIT |
---|
165 | |
---|
166 | #include <rtems/confdefs.h> |
---|
167 | /* end of file */ |
---|