source: rtems/testsuites/psxtmtests/psxtmcond08/init.c @ 2da28d2

4.115
Last change on this file since 2da28d2 was 2da28d2, checked in by Alexander Krutwig <alexander.krutwig@…>, on Mar 31, 2015 at 1:14:28 PM

psxtmtests: Use timeout in more distant future

Adds future compatibility with strict monontonic software timestamps.

  • 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/btimer.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    /* adjust sleepTime to get something obviously in the future */
89    ++sleepTime.tv_sec;
90
91    rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
92    rtems_test_assert( rc == 0 );
93
94  #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
95    {
96      /* override sleepTime with something obviously in the past */
97      sleepTime.tv_sec = 0;
98      sleepTime.tv_nsec = 5;
99
100      /* this does all the work of timedwait but immediately returns */
101      rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
102      rtems_test_assert(rc == ETIMEDOUT);
103      benchmark_timer_read();
104    }
105  #endif
106
107  pthread_mutex_unlock(&MutexID);
108  #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
109    /*
110     * In this case, unlock does not switch to another thread. so we need
111     * to explicitly yield. If we do not yield, then we will measure the
112     * time required to do an implicit pthread_exit() which is undesirable
113     * from a measurement viewpoint.
114     */
115    sched_yield();
116  #endif
117  return NULL;
118}
119
120void *POSIX_Init(
121  void *argument
122)
123{
124  int             i;
125  int             status;
126  pthread_t       threadId;
127  int             rc;
128  struct timeval  tp;
129
130  TEST_BEGIN();
131
132  rc =  gettimeofday(&tp, NULL);
133  rtems_test_assert( rc == 0 );
134
135  /* Convert from timeval to timespec */
136  sleepTime.tv_sec  = tp.tv_sec;
137  sleepTime.tv_nsec = tp.tv_usec * 1000;
138
139  rc = pthread_cond_init(&CondID, NULL);
140  rtems_test_assert( rc == 0 );
141
142  rc = pthread_mutex_init(&MutexID, NULL);
143  rtems_test_assert( rc == 0 );
144
145  rc = pthread_mutex_lock(&MutexID);
146  rtems_test_assert( rc == 0 );
147
148  for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) {
149    status = pthread_create( &threadId, NULL, Middle, NULL );
150    rtems_test_assert( !status );
151  }
152
153  status = pthread_create( &threadId, NULL, Low, NULL );
154  rtems_test_assert( !status );
155
156  /* start the timer and switch through all the other tasks */
157  benchmark_timer_initialize();
158
159  rc = pthread_mutex_unlock(&MutexID);
160  rtems_test_assert( rc == 0 );
161
162  /* Should never return. */
163  return NULL;
164}
165
166/* configuration information */
167
168#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
169#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
170
171#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
172#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
173#define CONFIGURE_POSIX_INIT_THREAD_TABLE
174#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
175#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2
176
177#define CONFIGURE_INIT
178
179#include <rtems/confdefs.h>
180  /* end of file */
Note: See TracBrowser for help on using the repository browser.