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

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • 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
38/* forward declarations to avoid warnings */
39void *POSIX_Init(void *argument);
40void *Middle(void *argument);
41void *Low(void *argument);
42
43pthread_cond_t  CondID;
44pthread_mutex_t MutexID;
45struct timespec sleepTime;
46
47void *Low(
48  void *argument
49)
50{
51  long end_time;
52
53  end_time = benchmark_timer_read();
54
55  put_time(
56    TEST_CASE,
57    end_time,
58    OPERATION_COUNT,
59    0,
60    0
61  );
62
63  puts( "*** END OF POSIX TIME TEST PSXTMCOND" TEST_NUMBER " ***" );
64
65  rtems_test_exit( 0 );
66  return NULL;
67}
68
69void *Middle(
70  void *argument
71)
72{
73  int             rc;
74
75
76  rc = pthread_mutex_lock(&MutexID);
77  rtems_test_assert( rc == 0 );
78
79  /* block and switch to another task here */
80
81  #if   defined(USE_WAIT)
82    rc = pthread_cond_wait( &CondID, &MutexID );
83    rtems_test_assert( rc == 0 );
84
85  #elif defined(USE_TIMEDWAIT_WITH_VALUE)
86   
87    rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
88    rtems_test_assert( rc == 0 );
89
90  #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
91    {
92      /* override sleepTime with something obviously in the past */
93      sleepTime.tv_sec = 0;
94      sleepTime.tv_nsec = 5;
95
96      /* this does all the work of timedwait but immediately returns */
97      rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
98      rtems_test_assert(rc == ETIMEDOUT);
99      benchmark_timer_read();
100    }
101  #endif
102
103  pthread_mutex_unlock(&MutexID);
104  #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
105    /*
106     * In this case, unlock does not switch to another thread. so we need
107     * to explicitly yield. If we do not yield, then we will measure the
108     * time required to do an implicit pthread_exit() which is undesirable
109     * from a measurement viewpoint.
110     */
111    sched_yield();
112  #endif
113  return NULL;
114}
115
116void *POSIX_Init(
117  void *argument
118)
119{
120  int             i;
121  int             status;
122  pthread_t       threadId;
123  int             rc;
124  struct timeval  tp;
125
126  puts( "\n\n*** POSIX TIME TEST PSXTMCOND" TEST_NUMBER " ***" );
127
128  rc =  gettimeofday(&tp, NULL);
129  rtems_test_assert( rc == 0 );
130
131  /* Convert from timeval to timespec */
132  sleepTime.tv_sec  = tp.tv_sec;
133  sleepTime.tv_nsec = tp.tv_usec * 1000;
134  sleepTime.tv_nsec += 1;
135
136  rc = pthread_cond_init(&CondID, NULL);
137  rtems_test_assert( rc == 0 );
138
139  rc = pthread_mutex_init(&MutexID, NULL);
140  rtems_test_assert( rc == 0 );
141
142  rc = pthread_mutex_lock(&MutexID);
143  rtems_test_assert( rc == 0 );
144
145  for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) {
146    status = pthread_create( &threadId, NULL, Middle, NULL );
147    rtems_test_assert( !status );
148  }
149
150  status = pthread_create( &threadId, NULL, Low, NULL );
151  rtems_test_assert( !status );
152
153  /* start the timer and switch through all the other tasks */
154  benchmark_timer_initialize();
155
156  rc = pthread_mutex_unlock(&MutexID);
157  rtems_test_assert( rc == 0 );
158
159  /* Should never return. */
160  return NULL;
161}
162
163/* configuration information */
164
165#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
166#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
167
168#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
169#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
170#define CONFIGURE_POSIX_INIT_THREAD_TABLE
171#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2
172#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2
173
174#define CONFIGURE_INIT
175
176#include <rtems/confdefs.h>
177  /* end of file */
Note: See TracBrowser for help on using the repository browser.