source: rtems/c/src/tests/psxtests/psx05/init.c @ 76117f2b

4.104.114.84.95
Last change on this file since 76117f2b was 76117f2b, checked in by Joel Sherrill <joel.sherrill@…>, on 08/09/96 at 19:20:54

changed style of output to match other tests

  • Property mode set to 100644
File size: 10.2 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_mutexattr(
18  char                *msg,
19  pthread_mutexattr_t *attr
20)
21{
22  int status;
23  int protocol;
24  int prioceiling;
25  int pshared;
26
27  /* protocol */
28
29  status = pthread_mutexattr_getprotocol( attr, &protocol );
30  assert( !status );
31
32  printf( "%smutex protocol is (%d) -- ", msg, protocol );
33  switch ( protocol ) {
34    case PTHREAD_PRIO_NONE:
35      puts( "PTHREAD_PRIO_NONE" );
36      break;
37    case PTHREAD_PRIO_INHERIT:
38      puts( "PTHREAD_PRIO_INHERIT" );
39      break;
40    case PTHREAD_PRIO_PROTECT:
41      puts( "PTHREAD_PRIO_PROTECT" );
42      break;
43    default:
44      puts( "UNKNOWN" );
45      assert( 0 );
46      break;
47  }
48
49  /* priority ceiling */
50
51  status = pthread_mutexattr_getprioceiling( attr, &prioceiling );
52  assert( !status );
53  printf( "%smutex priority ceiling is %d\n", msg, prioceiling );
54
55  /* process shared */
56
57  status = pthread_mutexattr_getpshared( attr, &pshared );
58  assert( !status );
59  printf( "%smutex process shared is (%d) -- ", msg, pshared );
60  switch ( pshared ) {
61    case PTHREAD_PROCESS_PRIVATE:
62      puts( "PTHREAD_PROCESS_PRIVATE" );
63      break;
64    case PTHREAD_PROCESS_SHARED:
65      puts( "PTHREAD_PROCESS_SHARED" );
66      break;
67    default:
68      puts( "UNKNOWN" );
69      assert( 0 );
70      break;
71  }
72}
73
74void *POSIX_Init(
75  void *argument
76)
77{
78  int                  status;
79  pthread_mutexattr_t  attr;
80  struct timespec      times;
81  struct sched_param   param;
82  int                  policy;
83  int                  ceiling;
84  int                  old_ceiling;
85
86  puts( "\n\n*** POSIX TEST 5 ***" );
87
88  /* set the time of day, and print our buffer in multiple ways */
89
90  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
91
92  /* get id of this thread */
93
94  Init_id = pthread_self();
95  printf( "Init's ID is 0x%08x\n", Init_id );
96 
97  /* basic checkout of mutex attributes */
98
99  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
100  status = pthread_mutexattr_init( &attr );
101  assert( !status );
102
103  Print_mutexattr( "Init: ", &attr );
104
105  puts( "Init: Changing mutex attributes" );
106  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
107  assert( !status );
108 
109  status = pthread_mutexattr_setprioceiling( &attr, 128 );
110  assert( !status );
111 
112  status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
113  assert( !status );
114 
115  Print_mutexattr( "Init: ", &attr );
116
117  puts( "Init: Resetting mutex attributes" );
118  status = pthread_mutexattr_init( &attr );
119  assert( !status );
120
121  /* create a thread */
122
123  status = pthread_create( &Task_id, NULL, Task_1, NULL );
124  assert( !status );
125
126  /* now try some basic mutex operations */
127
128  empty_line();
129
130  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
131  status = pthread_mutex_init( &Mutex_id, &attr );
132  if ( status )
133    printf( "status = %d\n", status );
134  assert( !status );
135
136  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
137  status = pthread_mutex_trylock( &Mutex_id );
138  if ( status )
139    printf( "status = %d\n", status );
140  assert( !status );
141
142  puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
143  status = pthread_mutex_trylock( &Mutex_id );
144  if ( status != EDEADLK )
145    printf( "status = %d\n", status );
146  assert( status == EDEADLK );
147
148  puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
149  status = pthread_mutex_lock( &Mutex_id );
150  if ( status != EDEADLK )
151    printf( "status = %d\n", status );
152  assert( status == EDEADLK );
153
154  puts( "Init: Sleep 1 second" );
155
156  sleep( 1 );
157 
158     /* switch to task 1 */
159
160  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
161  status = pthread_mutex_unlock( &Mutex_id );
162  if ( status )
163    printf( "status = %d\n", status );
164  assert( !status );
165
166  puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
167  status = pthread_mutex_unlock( &Mutex_id );
168  if ( status != EPERM )
169    printf( "status = %d\n", status );
170  assert( status == EPERM );
171
172  times.tv_sec = 0;
173  times.tv_nsec = 500000000;
174  puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
175  status = pthread_mutex_timedlock( &Mutex_id, &times );
176  if ( status != EAGAIN )
177    printf( "status = %d\n", status );
178  assert( status == EAGAIN );
179
180     /* switch to idle */
181
182  puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
183
184  /* destroy a mutex */
185
186  empty_line();
187
188  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
189  status = pthread_mutex_init( &Mutex2_id, &attr );
190  if ( status )
191    printf( "status = %d\n", status );
192  assert( !status );
193
194  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
195  status = pthread_mutexattr_destroy( &attr );
196  assert( !status );
197
198  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
199  status = pthread_mutex_destroy( &Mutex2_id );
200  assert( !status );
201 
202  /* destroy a busy mutex */
203 
204  empty_line();
205 
206  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
207  status = pthread_mutexattr_init( &attr );
208  assert( !status );
209
210  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
211  status = pthread_mutex_init( &Mutex2_id, &attr );
212  assert( !status );
213 
214  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
215  status = pthread_mutex_trylock( &Mutex2_id );
216  if ( status )
217    printf( "status = %d\n", status );
218  assert( !status );
219
220  puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
221  status = pthread_mutex_destroy( &Mutex2_id );
222  if ( status != EBUSY )
223    printf( "status = %d\n", status );
224  assert( status == EBUSY );
225
226  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
227  status = pthread_mutex_unlock( &Mutex2_id );
228  assert( !status );
229
230  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
231  status = pthread_mutex_destroy( &Mutex2_id );
232  assert( !status );
233
234  /* priority inherit mutex */
235
236  empty_line();
237 
238  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
239  status = pthread_mutexattr_init( &attr );
240  assert( !status );
241
242  puts(
243    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
244  );
245  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
246  assert( !status );
247
248  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
249  status = pthread_mutex_init( &Mutex2_id, &attr );
250  assert( !status );
251
252  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
253  status = pthread_mutex_trylock( &Mutex2_id );
254  assert( !status );
255
256  /* create a thread at a lower priority */
257 
258  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
259  assert( !status );
260 
261  /* set priority of Task2 to highest priority */
262 
263  param.sched_priority = 255;
264 
265  puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
266  status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
267  assert( !status );
268
269  /* switching to Task2 */
270
271  status = pthread_getschedparam( pthread_self(), &policy, &param );
272  assert( !status );
273  printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
274
275  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
276  status = pthread_mutex_unlock( &Mutex2_id );
277  assert( !status );
278 
279  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
280  status = pthread_mutexattr_destroy( &attr );
281  assert( !status );
282
283  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
284  status = pthread_mutex_destroy( &Mutex2_id );
285  assert( !status );
286 
287  /* priority ceiling mutex */
288 
289  empty_line();
290 
291  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
292  status = pthread_mutexattr_init( &attr );
293  assert( !status );
294 
295  puts(
296    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
297  );
298  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
299  assert( !status );
300 
301  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
302  status = pthread_mutex_init( &Mutex2_id, &attr );
303  assert( !status );
304 
305  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
306  assert( !status );
307  printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
308 
309  puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" );
310  status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling );
311  assert( !status );
312  printf(
313    "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
314  );
315 
316  status = pthread_getschedparam( pthread_self(), &policy, &param );
317  assert( !status );
318  printf(
319    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
320  );
321
322  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
323  status = pthread_mutex_trylock( &Mutex2_id );
324  assert( !status );
325 
326  status = pthread_getschedparam( pthread_self(), &policy, &param );
327  assert( !status );
328  printf(
329    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
330  );
331
332  /* create a thread at a higher priority */
333 
334  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
335  assert( !status );
336 
337  /* set priority of Task3 to highest priority */
338 
339  param.sched_priority = 199;
340 
341  status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
342  assert( !status );
343  puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
344 
345  /* DOES NOT SWITCH to Task3 */
346
347  puts( "Init: Sleep 1 second" );
348  assert( !status );
349  sleep( 1 );
350 
351  /* switch to task 3 */
352 
353  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
354  status = pthread_mutex_unlock( &Mutex2_id );
355  assert( !status );
356 
357  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
358  assert( !status );
359  printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
360 
361  /* set priority of Init to highest priority */
362 
363  param.sched_priority = 255;
364 
365  status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
366  assert( !status );
367  puts( "Init: pthread_setschedparam - set Init priority to highest" );
368 
369  puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
370  status = pthread_mutex_lock( &Mutex2_id );
371  if ( status != EDEADLK )
372    printf( "status = %d\n", status );
373  assert( status == EINVAL );
374
375  puts( "*** END OF POSIX TEST 5 ***" );
376  exit( 0 );
377
378  return NULL; /* just so the compiler thinks we returned something */
379}
Note: See TracBrowser for help on using the repository browser.