1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * COPYRIGHT (c) 1989-2009. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <sched.h> |
---|
34 | |
---|
35 | #define CONFIGURE_INIT |
---|
36 | #include "system.h" |
---|
37 | #include <errno.h> |
---|
38 | #include "pritime.h" |
---|
39 | |
---|
40 | const char rtems_test_name[] = "PSX 9"; |
---|
41 | |
---|
42 | static int CEILING_PRIORITY; |
---|
43 | static int HIGH_PRIORITY; |
---|
44 | static int LOW_PRIORITY; |
---|
45 | |
---|
46 | static int get_current_prio( pthread_t thread ) |
---|
47 | { |
---|
48 | rtems_status_code sc; |
---|
49 | rtems_task_priority prio; |
---|
50 | int max; |
---|
51 | |
---|
52 | sc = rtems_task_set_priority( thread, RTEMS_CURRENT_PRIORITY, &prio ); |
---|
53 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
54 | |
---|
55 | max = sched_get_priority_max( SCHED_FIFO ); |
---|
56 | |
---|
57 | return max + 1 - (int) prio; |
---|
58 | } |
---|
59 | |
---|
60 | static void *mutex_lock_task(void *arg) |
---|
61 | { |
---|
62 | pthread_mutex_t *mtx; |
---|
63 | int eno; |
---|
64 | |
---|
65 | mtx = arg; |
---|
66 | |
---|
67 | eno = pthread_mutex_lock( mtx ); |
---|
68 | rtems_test_assert( eno == 0 ); |
---|
69 | |
---|
70 | sched_yield(); |
---|
71 | |
---|
72 | eno = pthread_mutex_unlock( mtx ); |
---|
73 | rtems_test_assert( eno == 0 ); |
---|
74 | |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | static void test_destroy_locked_mutex(void) |
---|
79 | { |
---|
80 | pthread_mutex_t mtx; |
---|
81 | pthread_t th; |
---|
82 | int eno; |
---|
83 | |
---|
84 | eno = pthread_mutex_init( &mtx, NULL ); |
---|
85 | rtems_test_assert( eno == 0 ); |
---|
86 | |
---|
87 | eno = pthread_create( &th, NULL, mutex_lock_task, &mtx ); |
---|
88 | rtems_test_assert( eno == 0 ); |
---|
89 | |
---|
90 | sched_yield(); |
---|
91 | |
---|
92 | eno = pthread_mutex_destroy( &mtx ); |
---|
93 | rtems_test_assert( eno == EBUSY ); |
---|
94 | |
---|
95 | sched_yield(); |
---|
96 | |
---|
97 | eno = pthread_mutex_destroy( &mtx ); |
---|
98 | rtems_test_assert( eno == 0 ); |
---|
99 | |
---|
100 | eno = pthread_join( th, NULL ); |
---|
101 | rtems_test_assert( eno == 0 ); |
---|
102 | } |
---|
103 | |
---|
104 | void *POSIX_Init( |
---|
105 | void *argument |
---|
106 | ) |
---|
107 | { |
---|
108 | int status; |
---|
109 | int passes; |
---|
110 | int schedpolicy; |
---|
111 | int priority; |
---|
112 | struct sched_param schedparam; |
---|
113 | char buffer[ 80 ]; |
---|
114 | pthread_mutexattr_t attr; |
---|
115 | time_t start; |
---|
116 | time_t now; |
---|
117 | |
---|
118 | TEST_BEGIN(); |
---|
119 | |
---|
120 | CEILING_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ); |
---|
121 | HIGH_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 1; |
---|
122 | LOW_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 2; |
---|
123 | |
---|
124 | test_destroy_locked_mutex(); |
---|
125 | |
---|
126 | /* set the time of day, and print our buffer in multiple ways */ |
---|
127 | |
---|
128 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
129 | |
---|
130 | /* get id of this thread */ |
---|
131 | |
---|
132 | Init_id = pthread_self(); |
---|
133 | printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id ); |
---|
134 | |
---|
135 | /* try to use this thread as a sporadic server */ |
---|
136 | |
---|
137 | puts( "Init: pthread_getschedparam - SUCCESSFUL" ); |
---|
138 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
139 | rtems_test_assert( !status ); |
---|
140 | |
---|
141 | priority = schedparam.sched_priority; |
---|
142 | sprintf( buffer, " - current priority = %d", priority ); |
---|
143 | print_current_time( "Init: ", buffer ); |
---|
144 | |
---|
145 | schedparam.sched_ss_repl_period.tv_sec = 0; |
---|
146 | schedparam.sched_ss_repl_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
147 | schedparam.sched_ss_init_budget.tv_sec = 0; |
---|
148 | schedparam.sched_ss_init_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
149 | |
---|
150 | schedparam.sched_priority = HIGH_PRIORITY; |
---|
151 | schedparam.sched_ss_low_priority = LOW_PRIORITY; |
---|
152 | |
---|
153 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
154 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
155 | rtems_test_assert( !status ); |
---|
156 | |
---|
157 | sprintf( buffer, " - new priority = %d", get_current_prio( pthread_self() ) ); |
---|
158 | print_current_time( "Init: ", buffer ); |
---|
159 | |
---|
160 | /* go into a loop consuming CPU time to watch our priority change */ |
---|
161 | |
---|
162 | for ( passes=0 ; passes <= 3 ; ) { |
---|
163 | int current_priority; |
---|
164 | |
---|
165 | current_priority = get_current_prio( pthread_self() ); |
---|
166 | |
---|
167 | if ( priority != current_priority ) { |
---|
168 | priority = current_priority; |
---|
169 | sprintf( buffer, " - new priority = %d", priority ); |
---|
170 | print_current_time( "Init: ", buffer ); |
---|
171 | passes++; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /* now see if this works if we are holding a priority ceiling mutex */ |
---|
176 | |
---|
177 | empty_line(); |
---|
178 | |
---|
179 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
180 | rtems_test_assert( !status ); |
---|
181 | |
---|
182 | schedparam.sched_ss_repl_period.tv_sec = 0; |
---|
183 | schedparam.sched_ss_repl_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
184 | schedparam.sched_ss_init_budget.tv_sec = 0; |
---|
185 | schedparam.sched_ss_init_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
186 | |
---|
187 | schedparam.sched_priority = HIGH_PRIORITY; |
---|
188 | schedparam.sched_ss_low_priority = LOW_PRIORITY; |
---|
189 | |
---|
190 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
191 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
192 | rtems_test_assert( !status ); |
---|
193 | |
---|
194 | puts( "Init: Initializing mutex attributes for priority ceiling" ); |
---|
195 | status = pthread_mutexattr_init( &attr ); |
---|
196 | rtems_test_assert( !status ); |
---|
197 | |
---|
198 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); |
---|
199 | rtems_test_assert( !status ); |
---|
200 | |
---|
201 | status = pthread_mutexattr_setprioceiling( &attr, CEILING_PRIORITY ); |
---|
202 | rtems_test_assert( !status ); |
---|
203 | |
---|
204 | puts( "Init: Creating a mutex" ); |
---|
205 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
206 | if ( status ) |
---|
207 | printf( "status = %d\n", status ); |
---|
208 | rtems_test_assert( !status ); |
---|
209 | |
---|
210 | sprintf( buffer, " - new priority = %d", get_current_prio( pthread_self() ) ); |
---|
211 | print_current_time( "Init: ", buffer ); |
---|
212 | |
---|
213 | /* go into a loop consuming CPU time to watch our priority NOT lower */ |
---|
214 | |
---|
215 | start = time( &start ); |
---|
216 | |
---|
217 | puts( "Init: pthread_mutex_lock acquire the lock" ); |
---|
218 | status = pthread_mutex_lock( &Mutex_id ); |
---|
219 | if ( status ) |
---|
220 | printf( "status = %d %s\n", status, strerror(status) ); |
---|
221 | rtems_test_assert( !status ); |
---|
222 | |
---|
223 | do { |
---|
224 | priority = get_current_prio( pthread_self() ); |
---|
225 | |
---|
226 | if ( priority != CEILING_PRIORITY ) { |
---|
227 | puts( "ERROR - Init's priority lowered while holding mutex" ); |
---|
228 | rtems_test_exit(0); |
---|
229 | } |
---|
230 | |
---|
231 | now = time( &now ); |
---|
232 | } while ( now - start < 3 ); |
---|
233 | |
---|
234 | /* with this unlock we should be able to go to low priority */ |
---|
235 | |
---|
236 | puts( "Init: unlock mutex" ); |
---|
237 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
238 | if ( status ) |
---|
239 | printf( "status = %d\n", status ); |
---|
240 | rtems_test_assert( !status ); |
---|
241 | |
---|
242 | sprintf( buffer, " - new priority = %d", get_current_prio( pthread_self() ) ); |
---|
243 | print_current_time( "Init: ", buffer ); |
---|
244 | |
---|
245 | for ( ; ; ) { |
---|
246 | if ( get_current_prio( pthread_self() ) == LOW_PRIORITY ) |
---|
247 | break; |
---|
248 | } |
---|
249 | |
---|
250 | sprintf( buffer, " - new priority = %d", get_current_prio( pthread_self() ) ); |
---|
251 | print_current_time( "Init: ", buffer ); |
---|
252 | |
---|
253 | TEST_END(); |
---|
254 | rtems_test_exit( 0 ); |
---|
255 | |
---|
256 | return NULL; /* just so the compiler thinks we returned something */ |
---|
257 | } |
---|