source: rtems/testsuites/psxtests/psx09/init.c @ 4562dd8

4.115
Last change on this file since 4562dd8 was 4562dd8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/30/11 at 02:53:32

2011-09-30 Ralf Corsépius <ralf.corsepius@…>

  • psx01/init.c, psx07/init.c, psx09/init.c, psx12/init.c, psxclock/init.c, psxtimer01/psxtimer.c: Include "pritime.h". Use PRIdtime_t to print time_t.
  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[1b4f2b30]1/*
[2e7e636f]2 *  COPYRIGHT (c) 1989-2009.
[7e811af1]3 *  On-Line Applications Research Corporation (OAR).
4 *
[98e4ebf5]5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
[3c48599]7 *  http://www.rtems.com/license/LICENSE.
[7e811af1]8 *
9 *  $Id$
10 */
11
[cafefbf]12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
[98843fb8]16#include <sched.h>
17
[7e811af1]18#define CONFIGURE_INIT
19#include "system.h"
20#include <errno.h>
[4562dd8]21#include "pritime.h"
[7e811af1]22
[2e7e636f]23void print_schedparam(
24  char               *prefix,
25  struct sched_param *schedparam
26);
27
[5abf63d]28int HIGH_PRIORITY;
29int MEDIUM_PRIORITY;
30int LOW_PRIORITY;
31
[7e811af1]32void 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)
[66c9ad8]39  printf( "%ssched_ss_low_priority     = %d\n",
40      prefix, schedparam->sched_ss_low_priority );
[4562dd8]41  printf( "%ssched_ss_repl_period = (%" PRIdtime_t ", %ld)\n", prefix,
[66c9ad8]42     schedparam->sched_ss_repl_period.tv_sec,
43     schedparam->sched_ss_repl_period.tv_nsec );
[4562dd8]44  printf( "%ssched_ss_init_budget = (%" PRIdtime_t ", %ld)\n", prefix,
[66c9ad8]45     schedparam->sched_ss_init_budget.tv_sec,
46     schedparam->sched_ss_init_budget.tv_nsec );
[7e811af1]47#else
48  printf( "%s_POSIX_SPORADIC_SERVER is not defined\n" );
49#endif
50}
51
52void *POSIX_Init(
53  void *argument
54)
55{
56  int                  status;
[84b03316]57  int                  passes;
[7e811af1]58  int                  schedpolicy;
59  int                  priority;
60  struct sched_param   schedparam;
61  char                 buffer[ 80 ];
62  pthread_mutexattr_t  attr;
[c45ca10]63  time_t               start;
64  time_t               now;
[7e811af1]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();
[39615f4]75  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
[1b4f2b30]76
[7e811af1]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 );
[33c46f1]81  rtems_test_assert( !status );
[7e811af1]82
83  priority = schedparam.sched_priority;
84  sprintf( buffer, " - current priority = %d", priority );
85  print_current_time( "Init: ", buffer );
86
[66c9ad8]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 */
[7e811af1]91
[5abf63d]92  schedparam.sched_priority = sched_get_priority_max(SCHED_SPORADIC);
[66c9ad8]93  schedparam.sched_ss_low_priority = sched_get_priority_max(SCHED_SPORADIC) - 2;
[7e811af1]94
95  puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" );
96  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
[33c46f1]97  rtems_test_assert( !status );
[7e811af1]98
99  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
[33c46f1]100  rtems_test_assert( !status );
[7e811af1]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 );
[33c46f1]110    rtems_test_assert( !status );
[1b4f2b30]111
[7e811af1]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 );
[33c46f1]125  rtems_test_assert( !status );
[7e811af1]126
[66c9ad8]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 */
[7e811af1]131
[5abf63d]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;
[7e811af1]135
136  schedparam.sched_priority = HIGH_PRIORITY;
[66c9ad8]137  schedparam.sched_ss_low_priority = LOW_PRIORITY;
[1b4f2b30]138
[7e811af1]139  puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" );
140  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
[33c46f1]141  rtems_test_assert( !status );
[7e811af1]142
143  puts( "Init: Initializing mutex attributes for priority ceiling" );
144  status = pthread_mutexattr_init( &attr );
[33c46f1]145  rtems_test_assert( !status );
[7e811af1]146
[c45ca10]147  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
[33c46f1]148  rtems_test_assert( !status );
[1b4f2b30]149
[7e811af1]150  puts( "Init: Creating a mutex" );
151  status = pthread_mutex_init( &Mutex_id, &attr );
152  if ( status )
153    printf( "status = %d\n", status );
[33c46f1]154  rtems_test_assert( !status );
[7e811af1]155
156  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
[33c46f1]157  rtems_test_assert( !status );
[1b4f2b30]158
[7e811af1]159  priority = schedparam.sched_priority;
160  sprintf( buffer, " - new priority = %d", priority );
161  print_current_time( "Init: ", buffer );
162
[c45ca10]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) );
[33c46f1]171  rtems_test_assert( !status );
[7e811af1]172
173  for ( ; ; ) {
174    status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
[33c46f1]175    rtems_test_assert( !status );
[7e811af1]176
[c45ca10]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 );
[b1274bd9]183    if ( now - start > 3 )
[c45ca10]184      break;
[7e811af1]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 );
[33c46f1]191    rtems_test_assert( !status );
[7e811af1]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
[1b4f2b30]202  puts( "Init: unlock mutex" );
[7e811af1]203  status = pthread_mutex_unlock( &Mutex_id );
204  if ( status )
205    printf( "status = %d\n", status );
[33c46f1]206  rtems_test_assert( !status );
[7e811af1]207
208  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
[33c46f1]209  rtems_test_assert( !status );
[7e811af1]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 );
[33c46f1]217    rtems_test_assert( !status );
[1b4f2b30]218
[7e811af1]219    if ( schedparam.sched_priority == LOW_PRIORITY )
220      break;
221  }
222
223  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
[33c46f1]224  rtems_test_assert( !status );
[1b4f2b30]225
[7e811af1]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 ***" );
[d802489]231  rtems_test_exit( 0 );
[7e811af1]232
233  return NULL; /* just so the compiler thinks we returned something */
234}
Note: See TracBrowser for help on using the repository browser.