source: rtems/testsuites/psxtests/psx09/init.c @ 39615f4

4.104.115
Last change on this file since 39615f4 was 39615f4, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/27/09 at 14:10:54

Use PRIxpthread_t to print pthread_t's.

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