source: rtems/testsuites/psxtmtests/psxtmcond08/init.c @ 72f3b05

4.115
Last change on this file since 72f3b05 was 72f3b05, checked in by Christopher Kerl <zargyyoyo@…>, on 01/11/13 at 20:33:37

Create POSIX Timing Test psxtmcond09

Modifications were made to this submission allow the same init.c
file to be ran by three tests that were very similar using posix
pthread_cond_timedwait and pthread_cond_wait.

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