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