source: rtems/testsuites/psxtests/psx09/init.c @ 7e811af1

4.104.114.84.95
Last change on this file since 7e811af1 was 7e811af1, checked in by Joel Sherrill <joel.sherrill@…>, on 08/07/96 at 21:27:25

basic sporadic server test.

sporadic server with priority ceiling mutex test.

  • Property mode set to 100644
File size: 6.8 KB
Line 
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
17void 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
36void *POSIX_Init(
37  void *argument
38)
39{
40  int                  status;
41  int                  schedpolicy;
42  int                  priority;
43  struct sched_param   schedparam;
44  char                 buffer[ 80 ];
45  pthread_mutexattr_t  attr;
46
47  puts( "\n\n*** POSIX TEST 9 ***" );
48
49  /* set the time of day, and print our buffer in multiple ways */
50
51  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
52
53  /* get id of this thread */
54
55  Init_id = pthread_self();
56  printf( "Init's ID is 0x%08x\n", Init_id );
57 
58#if 0
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#endif
103  /* now see if this works if we are holding a priority ceiling mutex */
104
105  empty_line();
106
107
108  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
109  assert( !status );
110
111  schedparam.ss_replenish_period.tv_sec = 0;
112  schedparam.ss_replenish_period.tv_nsec = 500000000;  /* 1/2 second */
113  schedparam.ss_initial_budget.tv_sec = 0;
114  schedparam.ss_initial_budget.tv_nsec = 250000000;    /* 1/4 second */
115
116#define HIGH_PRIORITY 150
117#define MEDIUM_PRIORITY 131
118#define LOW_PRIORITY 100
119
120  schedparam.sched_priority = HIGH_PRIORITY;
121  schedparam.ss_low_priority = LOW_PRIORITY;
122 
123  puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" );
124  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
125  assert( !status );
126
127  puts( "Init: Initializing mutex attributes for priority ceiling" );
128  status = pthread_mutexattr_init( &attr );
129  assert( !status );
130
131  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
132  assert( !status );
133 
134  status = pthread_mutexattr_setprioceiling( &attr, MEDIUM_PRIORITY );
135  assert( !status );
136 
137  puts( "Init: Creating a mutex" );
138  status = pthread_mutex_init( &Mutex_id, &attr );
139  if ( status )
140    printf( "status = %d\n", status );
141  assert( !status );
142
143  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
144  assert( !status );
145 
146  priority = schedparam.sched_priority;
147  sprintf( buffer, " - new priority = %d", priority );
148  print_current_time( "Init: ", buffer );
149
150  /* go into a loop consuming CPU time to watch our priority lower */
151
152  for ( ; ; ) {
153    status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
154    assert( !status );
155
156    if ( schedparam.sched_priority != LOW_PRIORITY )
157      continue;
158
159    priority = schedparam.sched_priority;
160    sprintf( buffer, " - new priority = %d", priority );
161    print_current_time( "Init: ", buffer );
162
163    puts( "Init: pthread_mutex_lock acquire the lock" );
164    status = pthread_mutex_lock( &Mutex_id );
165    if ( status )
166      printf( "status = %d\n", status );
167    assert( !status );
168
169    status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
170    assert( !status );
171
172    priority = schedparam.sched_priority;
173    sprintf( buffer, " - new priority = %d", priority );
174    print_current_time( "Init: ", buffer );
175
176    break;
177  }
178
179  /* now spin waiting for our budget to be replenished */
180
181  for ( ; ; ) {
182    status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
183    assert( !status );
184 
185    if ( schedparam.sched_priority == HIGH_PRIORITY )
186      break;
187  }
188 
189  priority = schedparam.sched_priority;
190  sprintf( buffer, " - new priority = %d", priority );
191  print_current_time( "Init: ", buffer );
192 
193  /* with this unlock we should be able to go to low priority */
194
195  puts( "Init: unlock mutex" );
196  status = pthread_mutex_unlock( &Mutex_id );
197  if ( status )
198    printf( "status = %d\n", status );
199  assert( !status );
200
201  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
202  assert( !status );
203
204  priority = schedparam.sched_priority;
205  sprintf( buffer, " - new priority = %d", priority );
206  print_current_time( "Init: ", buffer );
207
208  for ( ; ; ) {
209    status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
210    assert( !status );
211 
212    if ( schedparam.sched_priority == LOW_PRIORITY )
213      break;
214  }
215
216  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
217  assert( !status );
218 
219  priority = schedparam.sched_priority;
220  sprintf( buffer, " - new priority = %d", priority );
221  print_current_time( "Init: ", buffer );
222
223  puts( "*** END OF POSIX TEST 9 ***" );
224  exit( 0 );
225
226  return NULL; /* just so the compiler thinks we returned something */
227}
Note: See TracBrowser for help on using the repository browser.