source: rtems/testsuites/psxtmtests/psxtmsem05/init.c @ 50162e0

4.115
Last change on this file since 50162e0 was 50162e0, checked in by Joel Sherrill <joel.sherrill@…>, on 12/07/13 at 18:18:02

psxtmtests: Make output more uniform

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2013.
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
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <errno.h>
15#include <fcntl.h>
16#include <semaphore.h>
17#include <coverhd.h>
18#include <tmacros.h>
19#include <timesys.h>
20#include "test_support.h"
21#include <pthread.h>
22#include <sched.h>
23#include <rtems/timerdrv.h>
24
25/* forward declarations to avoid warnings */
26void *POSIX_Init(void *argument);
27void *Middle(void *argument);
28void *Low(void *argument);
29
30#define MAX_SEMS  2
31
32sem_t   sem1;
33
34void *Low(
35  void *argument
36)
37{
38  int      status;
39  benchmark_timer_t end_time;
40
41    status = sem_wait(&sem1); /* semaphore blocks */
42  end_time = benchmark_timer_read();
43
44  rtems_test_assert( status == 0 );
45
46  put_time(
47    "sem_post: thread waiting preempt",
48    end_time,
49    OPERATION_COUNT,
50    0,
51    0
52  );
53
54  puts( "*** END OF POSIX TIME TEST PSXTMSEM05 ***" );
55  rtems_test_exit( 0 );
56  return NULL;
57}
58
59void *Middle(
60  void *argument
61)
62{
63  int status;
64
65    status = sem_wait(&sem1); /* semaphore blocks */
66  rtems_test_assert( status == 0 );
67
68    /* thread switch occurs */
69
70    status = sem_post(&sem1);
71  rtems_test_assert( status == 0 );
72
73    /* thread switch occurs */
74
75  /* should never return */
76  rtems_test_assert( FALSE );
77  return NULL;
78}
79
80void *POSIX_Init(
81  void *argument
82)
83{
84  int                 i;
85  int                 status;
86  pthread_t           threadId;
87  pthread_attr_t      attr;
88  struct sched_param  param;
89
90  puts( "\n\n*** POSIX TIME TEST PSXTMSEM05  ***" );
91
92  /*
93   * Deliberately create the semaphore BEFORE the threads.  This way the
94   * threads should preempt this thread and block as they are created.
95   */
96  status = sem_init( &sem1, 0, 1 );
97  rtems_test_assert( status == 0 );
98
99  /*
100   * Obtain the semaphore so the threads will block.
101   */
102  status = sem_wait( &sem1 );
103  rtems_test_assert( status == 0 );
104
105  /*
106   * Now lower our priority
107   */
108  status = pthread_attr_init( &attr );
109  rtems_test_assert( status == 0 );
110
111  status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
112  rtems_test_assert( status == 0 );
113
114  status = pthread_attr_setschedpolicy( &attr, SCHED_RR );
115  rtems_test_assert( status == 0 );
116
117  param.sched_priority = 2;
118  status = pthread_attr_setschedparam( &attr, &param );
119  rtems_test_assert( status == 0 );
120
121  /*
122   * And create rest of threads as more important than we are.  They
123   * will preempt us as they are created and block.
124   */
125  for ( i=0 ; i < OPERATION_COUNT ; i++ ) {
126
127    param.sched_priority = 3 + i;
128    status = pthread_attr_setschedparam( &attr, &param );
129    rtems_test_assert( status == 0 );
130
131    status = pthread_create(
132      &threadId,
133      &attr,
134      (i == OPERATION_COUNT - 1) ? Low : Middle,
135      NULL
136    );
137    rtems_test_assert( status == 0 );
138  }
139 
140  /*
141   * Now start the timer which will be stopped in Low.
142   * Release the semaphore so threads unblock and preempt.
143   */
144  benchmark_timer_initialize();
145
146    status = sem_post( &sem1 );
147      /* thread switch occurs */
148
149  /* should never return */
150  rtems_test_assert( FALSE );
151  return NULL;
152}
153
154/* configuration information */
155
156#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
157#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
158
159#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
160#define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES  MAX_SEMS
161#define CONFIGURE_POSIX_INIT_THREAD_TABLE
162
163#define CONFIGURE_INIT
164
165#include <rtems/confdefs.h>
166  /* end of file */
Note: See TracBrowser for help on using the repository browser.