1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2009. |
---|
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.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #ifdef HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <sched.h> |
---|
17 | |
---|
18 | #define CONFIGURE_INIT |
---|
19 | #include "system.h" |
---|
20 | #include <errno.h> |
---|
21 | #include "pritime.h" |
---|
22 | |
---|
23 | void print_schedparam( |
---|
24 | char *prefix, |
---|
25 | struct sched_param *schedparam |
---|
26 | ); |
---|
27 | |
---|
28 | int HIGH_PRIORITY; |
---|
29 | int MEDIUM_PRIORITY; |
---|
30 | int LOW_PRIORITY; |
---|
31 | |
---|
32 | void print_schedparam( |
---|
33 | char *prefix, |
---|
34 | struct sched_param *schedparam |
---|
35 | ) |
---|
36 | { |
---|
37 | printf( "%ssched priority = %d\n", prefix, schedparam->sched_priority ); |
---|
38 | #if defined(_POSIX_SPORADIC_SERVER) |
---|
39 | printf( "%ssched_ss_low_priority = %d\n", |
---|
40 | prefix, schedparam->sched_ss_low_priority ); |
---|
41 | printf( "%ssched_ss_repl_period = (%" PRIdtime_t ", %ld)\n", prefix, |
---|
42 | schedparam->sched_ss_repl_period.tv_sec, |
---|
43 | schedparam->sched_ss_repl_period.tv_nsec ); |
---|
44 | printf( "%ssched_ss_init_budget = (%" PRIdtime_t ", %ld)\n", prefix, |
---|
45 | schedparam->sched_ss_init_budget.tv_sec, |
---|
46 | schedparam->sched_ss_init_budget.tv_nsec ); |
---|
47 | #else |
---|
48 | printf( "%s_POSIX_SPORADIC_SERVER is not defined\n" ); |
---|
49 | #endif |
---|
50 | } |
---|
51 | |
---|
52 | void *POSIX_Init( |
---|
53 | void *argument |
---|
54 | ) |
---|
55 | { |
---|
56 | int status; |
---|
57 | int passes; |
---|
58 | int schedpolicy; |
---|
59 | int priority; |
---|
60 | struct sched_param schedparam; |
---|
61 | char buffer[ 80 ]; |
---|
62 | pthread_mutexattr_t attr; |
---|
63 | time_t start; |
---|
64 | time_t now; |
---|
65 | |
---|
66 | puts( "\n\n*** POSIX TEST 9 ***" ); |
---|
67 | |
---|
68 | /* set the time of day, and print our buffer in multiple ways */ |
---|
69 | |
---|
70 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
71 | |
---|
72 | /* get id of this thread */ |
---|
73 | |
---|
74 | Init_id = pthread_self(); |
---|
75 | printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id ); |
---|
76 | |
---|
77 | /* try to use this thread as a sporadic server */ |
---|
78 | |
---|
79 | puts( "Init: pthread_getschedparam - SUCCESSFUL" ); |
---|
80 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
81 | rtems_test_assert( !status ); |
---|
82 | |
---|
83 | priority = schedparam.sched_priority; |
---|
84 | sprintf( buffer, " - current priority = %d", priority ); |
---|
85 | print_current_time( "Init: ", buffer ); |
---|
86 | |
---|
87 | schedparam.sched_ss_repl_period.tv_sec = 0; |
---|
88 | schedparam.sched_ss_repl_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
89 | schedparam.sched_ss_init_budget.tv_sec = 0; |
---|
90 | schedparam.sched_ss_init_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
91 | |
---|
92 | schedparam.sched_priority = sched_get_priority_max(SCHED_SPORADIC); |
---|
93 | schedparam.sched_ss_low_priority = sched_get_priority_max(SCHED_SPORADIC) - 2; |
---|
94 | |
---|
95 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
96 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
97 | rtems_test_assert( !status ); |
---|
98 | |
---|
99 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
100 | rtems_test_assert( !status ); |
---|
101 | |
---|
102 | priority = schedparam.sched_priority; |
---|
103 | sprintf( buffer, " - new priority = %d", priority ); |
---|
104 | print_current_time( "Init: ", buffer ); |
---|
105 | |
---|
106 | /* go into a loop consuming CPU time to watch our priority change */ |
---|
107 | |
---|
108 | for ( passes=0 ; passes <= 3 ; ) { |
---|
109 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
110 | rtems_test_assert( !status ); |
---|
111 | |
---|
112 | if ( priority != schedparam.sched_priority ) { |
---|
113 | priority = schedparam.sched_priority; |
---|
114 | sprintf( buffer, " - new priority = %d", priority ); |
---|
115 | print_current_time( "Init: ", buffer ); |
---|
116 | passes++; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /* now see if this works if we are holding a priority ceiling mutex */ |
---|
121 | |
---|
122 | empty_line(); |
---|
123 | |
---|
124 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
125 | rtems_test_assert( !status ); |
---|
126 | |
---|
127 | schedparam.sched_ss_repl_period.tv_sec = 0; |
---|
128 | schedparam.sched_ss_repl_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
129 | schedparam.sched_ss_init_budget.tv_sec = 0; |
---|
130 | schedparam.sched_ss_init_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
131 | |
---|
132 | HIGH_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ); |
---|
133 | MEDIUM_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 2; |
---|
134 | LOW_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 4; |
---|
135 | |
---|
136 | schedparam.sched_priority = HIGH_PRIORITY; |
---|
137 | schedparam.sched_ss_low_priority = LOW_PRIORITY; |
---|
138 | |
---|
139 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
140 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
141 | rtems_test_assert( !status ); |
---|
142 | |
---|
143 | puts( "Init: Initializing mutex attributes for priority ceiling" ); |
---|
144 | status = pthread_mutexattr_init( &attr ); |
---|
145 | rtems_test_assert( !status ); |
---|
146 | |
---|
147 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
148 | rtems_test_assert( !status ); |
---|
149 | |
---|
150 | puts( "Init: Creating a mutex" ); |
---|
151 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
152 | if ( status ) |
---|
153 | printf( "status = %d\n", status ); |
---|
154 | rtems_test_assert( !status ); |
---|
155 | |
---|
156 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
157 | rtems_test_assert( !status ); |
---|
158 | |
---|
159 | priority = schedparam.sched_priority; |
---|
160 | sprintf( buffer, " - new priority = %d", priority ); |
---|
161 | print_current_time( "Init: ", buffer ); |
---|
162 | |
---|
163 | /* go into a loop consuming CPU time to watch our priority NOT lower */ |
---|
164 | |
---|
165 | start = time( &start ); |
---|
166 | |
---|
167 | puts( "Init: pthread_mutex_lock acquire the lock" ); |
---|
168 | status = pthread_mutex_lock( &Mutex_id ); |
---|
169 | if ( status ) |
---|
170 | printf( "status = %d %s\n", status, strerror(status) ); |
---|
171 | rtems_test_assert( !status ); |
---|
172 | |
---|
173 | for ( ; ; ) { |
---|
174 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
175 | rtems_test_assert( !status ); |
---|
176 | |
---|
177 | if ( schedparam.sched_priority == LOW_PRIORITY ) { |
---|
178 | puts( "ERROR - Init's priority lowered while holding mutex" ); |
---|
179 | rtems_test_exit(0); |
---|
180 | } |
---|
181 | |
---|
182 | now = time( &now ); |
---|
183 | if ( now - start > 3 ) |
---|
184 | break; |
---|
185 | |
---|
186 | priority = schedparam.sched_priority; |
---|
187 | sprintf( buffer, " - new priority = %d", priority ); |
---|
188 | print_current_time( "Init: ", buffer ); |
---|
189 | |
---|
190 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
191 | rtems_test_assert( !status ); |
---|
192 | |
---|
193 | priority = schedparam.sched_priority; |
---|
194 | sprintf( buffer, " - new priority = %d", priority ); |
---|
195 | print_current_time( "Init: ", buffer ); |
---|
196 | |
---|
197 | break; |
---|
198 | } |
---|
199 | |
---|
200 | /* with this unlock we should be able to go to low priority */ |
---|
201 | |
---|
202 | puts( "Init: unlock mutex" ); |
---|
203 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
204 | if ( status ) |
---|
205 | printf( "status = %d\n", status ); |
---|
206 | rtems_test_assert( !status ); |
---|
207 | |
---|
208 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
209 | rtems_test_assert( !status ); |
---|
210 | |
---|
211 | priority = schedparam.sched_priority; |
---|
212 | sprintf( buffer, " - new priority = %d", priority ); |
---|
213 | print_current_time( "Init: ", buffer ); |
---|
214 | |
---|
215 | for ( ; ; ) { |
---|
216 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
217 | rtems_test_assert( !status ); |
---|
218 | |
---|
219 | if ( schedparam.sched_priority == LOW_PRIORITY ) |
---|
220 | break; |
---|
221 | } |
---|
222 | |
---|
223 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
224 | rtems_test_assert( !status ); |
---|
225 | |
---|
226 | priority = schedparam.sched_priority; |
---|
227 | sprintf( buffer, " - new priority = %d", priority ); |
---|
228 | print_current_time( "Init: ", buffer ); |
---|
229 | |
---|
230 | puts( "*** END OF POSIX TEST 9 ***" ); |
---|
231 | rtems_test_exit( 0 ); |
---|
232 | |
---|
233 | return NULL; /* just so the compiler thinks we returned something */ |
---|
234 | } |
---|