source: rtems/testsuites/psxtmtests/psxtmcond08/init.c @ de549c3

4.11
Last change on this file since de549c3 was de549c3, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 3, 2014 at 3:16:43 PM

Misc psxtmtests: Use uint32_t not long for end_time to match printf() format expectations

  • Property mode set to 100644
File size: 4.1 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.org/license/LICENSE.
8 */
9
10#if   defined(USE_WAIT)
11  #define TEST_NUMBER "08"
12  #define TEST_CASE "pthread_cond_wait: blocking"
13#elif defined(USE_TIMEDWAIT_WITH_VALUE)
14  #define TEST_NUMBER "09"
15  #define TEST_CASE "pthread_cond_timedwait: blocking"
16#elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
17  #define TEST_NUMBER "10"
18  #define TEST_CASE "pthread_cond_timedwait: time in past error"
19#else
20  #error "How am I being compiled?"
21#endif
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <stdio.h>
28#include <time.h>
29#include <errno.h>
30#include <sched.h>
31#include <timesys.h>
32#include <tmacros.h>
33#include <rtems/timerdrv.h>
34#include "test_support.h"
35
36#include <pthread.h>
37
38const char rtems_test_name[] = "PSXTMCOND 08";
39
40/* forward declarations to avoid warnings */
41void *POSIX_Init(void *argument);
42void *Middle(void *argument);
43void *Low(void *argument);
44
45pthread_cond_t  CondID;
46pthread_mutex_t MutexID;
47struct timespec sleepTime;
48
49void *Low(
50  void *argument
51)
52{
53  uint32_t end_time;
54
55  end_time = benchmark_timer_read();
56
57  put_time(
58    TEST_CASE,
59    end_time,
60    OPERATION_COUNT,
61    0,
62    0
63  );
64
65  TEST_END();
66
67  rtems_test_exit( 0 );
68  return NULL;
69}
70
71void *Middle(
72  void *argument
73)
74{
75  int             rc;
76
77
78  rc = pthread_mutex_lock(&MutexID);
79  rtems_test_assert( rc == 0 );
80
81  /* block and switch to another task here */
82
83  #if   defined(USE_WAIT)
84    rc = pthread_cond_wait( &CondID, &MutexID );
85    rtems_test_assert( rc == 0 );
86
87  #elif defined(USE_TIMEDWAIT_WITH_VALUE)
88   
89    rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
90    rtems_test_assert( rc == 0 );
91
92  #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
93    {
94      /* override sleepTime with something obviously in the past */
95      sleepTime.tv_sec = 0;
96      sleepTime.tv_nsec = 5;
97
98      /* this does all the work of timedwait but immediately returns */
99      rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
100      rtems_test_assert(rc == ETIMEDOUT);
101      benchmark_timer_read();
102    }
103  #endif
104
105  pthread_mutex_unlock(&MutexID);
106  #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
107    /*
108     * In this case, unlock does not switch to another thread. so we need
109     * to explicitly yield. If we do not yield, then we will measure the
110     * time required to do an implicit pthread_exit() which is undesirable
111     * from a measurement viewpoint.
112     */
113    sched_yield();
114  #endif
115  return NULL;
116}
117
118void *POSIX_Init(
119  void *argument
120)
121{
122  int             i;
123  int             status;
124  pthread_t       threadId;
125  int             rc;
126  struct timeval  tp;
127
128  TEST_BEGIN();
129
130  rc =  gettimeofday(&tp, NULL);
131  rtems_test_assert( rc == 0 );
132
133  /* Convert from timeval to timespec */
134  sleepTime.tv_sec  = tp.tv_sec;
135  sleepTime.tv_nsec = tp.tv_usec * 1000;
136  sleepTime.tv_nsec += 1;
137
138  rc = pthread_cond_init(&CondID, NULL);
139  rtems_test_assert( rc == 0 );
140
141  rc = pthread_mutex_init(&MutexID, NULL);
142  rtems_test_assert( rc == 0 );
143
144  rc = pthread_mutex_lock(&MutexID);
145  rtems_test_assert( rc == 0 );
146
147  for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) {
148    status = pthread_create( &threadId, NULL, Middle, NULL );
149    rtems_test_assert( !status );
150  }
151
152  status = pthread_create( &threadId, NULL, Low, NULL );
153  rtems_test_assert( !status );
154
155  /* start the timer and switch through all the other tasks */
156  benchmark_timer_initialize();
157
158  rc = pthread_mutex_unlock(&MutexID);
159  rtems_test_assert( rc == 0 );
160
161  /* Should never return. */
162  return NULL;
163}
164
165/* configuration information */
166
167#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
168#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
169
170#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
171#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
172#define CONFIGURE_POSIX_INIT_THREAD_TABLE
173#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
174#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2
175
176#define CONFIGURE_INIT
177
178#include <rtems/confdefs.h>
179  /* end of file */
Note: See TracBrowser for help on using the repository browser.