source: rtems/testsuites/psxtests/psx09/init.c @ c45ca10

4.104.115
Last change on this file since c45ca10 was c45ca10, checked in by Joel Sherrill <joel.sherrill@…>, on 08/19/09 at 17:28:19

2009-08-19 Joel Sherrill <joel.sherrill@…>

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