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