source: rtems/testsuites/psxtmtests/psxtmcond08/psxtmcond08impl.h

Last change on this file was 94fa540, checked in by Joel Sherrill <joel@…>, on 04/07/22 at 16:16:26

testsuites/psxtmtests: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 *  COPYRIGHT (c) 1989-2013.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#if !defined(OPERATION_COUNT)
30#define OPERATION_COUNT 100
31#endif
32
33#if   defined(USE_WAIT)
34  #define TEST_NUMBER "08"
35  #define TEST_CASE "pthread_cond_wait: blocking"
36#elif defined(USE_TIMEDWAIT_WITH_VALUE)
37  #define TEST_NUMBER "09"
38  #define TEST_CASE "pthread_cond_timedwait: blocking"
39#elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
40  #define TEST_NUMBER "10"
41  #define TEST_CASE "pthread_cond_timedwait: time in past error"
42#else
43  #error "How am I being compiled?"
44#endif
45
46#ifdef HAVE_CONFIG_H
47#include "config.h"
48#endif
49
50#include <sys/time.h>
51#include <stdio.h>
52#include <time.h>
53#include <errno.h>
54#include <sched.h>
55#include <timesys.h>
56#include <tmacros.h>
57#include <rtems/btimer.h>
58#include "test_support.h"
59
60#include <pthread.h>
61
62const char rtems_test_name[] = "PSXTMCOND " TEST_NUMBER;
63
64/* forward declarations to avoid warnings */
65void *POSIX_Init(void *argument);
66void *Middle(void *argument);
67void *Low(void *argument);
68
69pthread_cond_t  CondID;
70pthread_mutex_t MutexID;
71struct timespec sleepTime;
72
73void *Low(
74  void *argument
75)
76{
77  uint32_t end_time;
78
79  end_time = benchmark_timer_read();
80
81  put_time(
82    TEST_CASE,
83    end_time,
84    OPERATION_COUNT,
85    0,
86    0
87  );
88
89  TEST_END();
90
91  rtems_test_exit( 0 );
92  return NULL;
93}
94
95void *Middle(
96  void *argument
97)
98{
99  int             rc;
100
101
102  rc = pthread_mutex_lock(&MutexID);
103  rtems_test_assert( rc == 0 );
104
105  /* block and switch to another task here */
106
107  #if   defined(USE_WAIT)
108    rc = pthread_cond_wait( &CondID, &MutexID );
109    rtems_test_assert( rc == 0 );
110
111  #elif defined(USE_TIMEDWAIT_WITH_VALUE)
112    /* adjust sleepTime to get something obviously in the future */
113    ++sleepTime.tv_sec;
114
115    rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
116    rtems_test_assert( rc == 0 );
117
118  #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
119    {
120      /* override sleepTime with something obviously in the past */
121      sleepTime.tv_sec = 0;
122      sleepTime.tv_nsec = 5;
123
124      /* this does all the work of timedwait but immediately returns */
125      rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
126      rtems_test_assert(rc == ETIMEDOUT);
127      benchmark_timer_read();
128    }
129  #endif
130
131  pthread_mutex_unlock(&MutexID);
132  #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
133    /*
134     * In this case, unlock does not switch to another thread. so we need
135     * to explicitly yield. If we do not yield, then we will measure the
136     * time required to do an implicit pthread_exit() which is undesirable
137     * from a measurement viewpoint.
138     */
139    sched_yield();
140  #endif
141  return NULL;
142}
143
144void *POSIX_Init(
145  void *argument
146)
147{
148  int             i;
149  int             status;
150  pthread_t       threadId;
151  int             rc;
152  struct timeval  tp;
153
154  TEST_BEGIN();
155
156  rc =  gettimeofday(&tp, NULL);
157  rtems_test_assert( rc == 0 );
158
159  /* Convert from timeval to timespec */
160  sleepTime.tv_sec  = tp.tv_sec;
161  sleepTime.tv_nsec = tp.tv_usec * 1000;
162
163  rc = pthread_cond_init(&CondID, NULL);
164  rtems_test_assert( rc == 0 );
165
166  rc = pthread_mutex_init(&MutexID, NULL);
167  rtems_test_assert( rc == 0 );
168
169  rc = pthread_mutex_lock(&MutexID);
170  rtems_test_assert( rc == 0 );
171
172  for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) {
173    status = pthread_create( &threadId, NULL, Middle, NULL );
174    rtems_test_assert( !status );
175  }
176
177  status = pthread_create( &threadId, NULL, Low, NULL );
178  rtems_test_assert( !status );
179
180  /* start the timer and switch through all the other tasks */
181  benchmark_timer_initialize();
182
183  rc = pthread_mutex_unlock(&MutexID);
184  rtems_test_assert( rc == 0 );
185
186  /* Should never return. */
187  return NULL;
188}
189
190/* configuration information */
191
192#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
193#define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
194
195#define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
196#define CONFIGURE_POSIX_INIT_THREAD_TABLE
197
198#define CONFIGURE_INIT
199
200#include <rtems/confdefs.h>
201  /* end of file */
Note: See TracBrowser for help on using the repository browser.