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

5
Last change on this file since ef23838 was ef23838, checked in by Sebastian Huber <sebastian.huber@…>, on 12/03/18 at 12:10:21

score: Avoid sbintime_t in API headers

The sbintime_t is a non-POSIX type and not visible if strict standard
options are selected.

Move implementation details from <rtems/score/timestamp.h> to
<rtems/score/timestampimpl.h>.

Update #3598.

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