source: rtems/testsuites/psxtests/psxclock/init.c @ 3e81d52

5
Last change on this file since 3e81d52 was 3e81d52, checked in by Sebastian Huber <sebastian.huber@…>, on 10/29/17 at 19:29:05

posix: Use far future for very long timeouts

Close #3205.

  • Property mode set to 100644
File size: 8.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.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <time.h>
15#include <errno.h>
16#include <stdint.h>
17
18#include "pmacros.h"
19#include "pritime.h"
20
21#include <rtems.h>
22#include <rtems/score/todimpl.h>
23
24const char rtems_test_name[] = "PSXCLOCK";
25
26static void check_enosys(int status)
27{
28  if ( (status == -1) && (errno == ENOSYS) )
29    return;
30  puts( "ERROR -- did not return ENOSYS as expected" );
31  rtems_test_exit(0);
32}
33
34typedef struct {
35  int counter;
36  struct timespec delta;
37} nanosleep_contex;
38
39static void task_nanosleep( rtems_task_argument arg )
40{
41  nanosleep_contex *ctx;
42
43  ctx = (nanosleep_contex *) arg;
44  ++ctx->counter;
45  nanosleep( &ctx->delta, NULL );
46}
47
48static void test_far_future_nanosleep( void )
49{
50  rtems_status_code sc;
51  rtems_id          id;
52  nanosleep_contex  ctx;
53
54  sc = rtems_task_create(
55    rtems_build_name( 'N', 'A', 'N', 'O' ),
56    1,
57    RTEMS_MINIMUM_STACK_SIZE,
58    RTEMS_DEFAULT_MODES,
59    RTEMS_DEFAULT_ATTRIBUTES,
60    &id
61  );
62  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
63
64  ctx.counter = 0;
65  ctx.delta.tv_sec = INT64_MAX;
66  ctx.delta.tv_nsec = 999999999;
67  sc = rtems_task_start( id, task_nanosleep, (rtems_task_argument) &ctx );
68  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
69
70  sc = rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
71  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
72
73  rtems_test_assert( ctx.counter == 1 );
74
75  sc = rtems_task_delete( id );
76  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
77}
78
79static rtems_task Init(
80  rtems_task_argument argument
81)
82{
83  struct timespec tv;
84  struct timespec tr;
85  int             sc;
86  time_t          seconds;
87  time_t          seconds1;
88  unsigned int    remaining;
89  struct tm       tm;
90  struct timespec delay_request;
91
92  TEST_BEGIN();
93
94  tm_build_time( &tm, TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
95
96  /* error cases in clock_gettime and clock_settime */
97
98  puts( "Init: clock_gettime - EINVAL (NULL timespec)" );
99  sc = clock_gettime( CLOCK_REALTIME, NULL );
100  rtems_test_assert( sc == -1 );
101  rtems_test_assert( errno == EINVAL );
102
103  puts( "Init: clock_gettime - EINVAL (invalid clockid)" );
104  sc = clock_gettime( (clockid_t)-1, &tv );
105  rtems_test_assert( sc == -1 );
106  rtems_test_assert( errno == EINVAL );
107
108  puts( "Init: clock_settime - EINVAL (invalid clockid)" );
109  sc = clock_settime( (clockid_t)-1, &tv );
110  rtems_test_assert( sc == -1 );
111  rtems_test_assert( errno == EINVAL );
112
113  /* way back near the dawn of time :D */
114  tv.tv_sec = 1;
115  tv.tv_nsec = 0;
116  printf( ctime( &tv.tv_sec ) );
117  puts( "Init: clock_settime - before 1988 EINVAL" );
118  sc = clock_settime( CLOCK_REALTIME, &tv );
119  rtems_test_assert( sc == -1 );
120  rtems_test_assert( errno == EINVAL );
121
122  /* exercise clock_getres */
123
124  puts( "Init: clock_getres - EINVAL (invalid clockid)" );
125  sc = clock_getres( (clockid_t) -1, &tv );
126  rtems_test_assert( sc == -1 );
127  rtems_test_assert( errno == EINVAL );
128
129  puts( "Init: clock_getres - EINVAL (NULL resolution)" );
130  sc = clock_getres( CLOCK_REALTIME, NULL );
131  rtems_test_assert( sc == -1 );
132  rtems_test_assert( errno == EINVAL );
133
134  puts( "Init: clock_getres - SUCCESSFUL" );
135  sc = clock_getres( CLOCK_REALTIME, &tv );
136  printf( "Init: resolution = sec (%" PRIdtime_t "), nsec (%ld)\n", tv.tv_sec, tv.tv_nsec );
137  rtems_test_assert( !sc );
138
139  /* set the time of day, and print our buffer in multiple ways */
140
141  tv.tv_sec = mktime( &tm );
142  rtems_test_assert( tv.tv_sec != -1 );
143
144  tv.tv_nsec = 0;
145
146  /* now set the time of day */
147
148  empty_line();
149
150  printf( asctime( &tm ) );
151  puts( "Init: clock_settime - SUCCESSFUL" );
152  sc = clock_settime( CLOCK_REALTIME, &tv );
153  rtems_test_assert( !sc );
154
155  printf( asctime( &tm ) );
156  printf( ctime( &tv.tv_sec ) );
157
158  /* use sleep to delay */
159
160  remaining = sleep( 3 );
161  rtems_test_assert( !remaining );
162
163  /* print new times to make sure it has changed and we can get the realtime */
164  sc = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &tv );
165  rtems_test_assert( !sc );
166  printf("Time since boot: (%" PRIdtime_t ", %ld)\n", tv.tv_sec,tv.tv_nsec );
167
168  sc = clock_gettime( CLOCK_REALTIME, &tv );
169  rtems_test_assert( !sc );
170
171  printf( ctime( &tv.tv_sec ) );
172
173  seconds = time( NULL );
174  printf( ctime( &seconds ) );
175
176  /*  just to have the value copied out through the parameter */
177
178  seconds = time( &seconds1 );
179  rtems_test_assert( seconds == seconds1 );
180
181  /* check the time remaining */
182
183  printf( "Init: seconds remaining (%d)\n", (int)remaining );
184  rtems_test_assert( !remaining );
185
186  test_far_future_nanosleep();
187
188  /* error cases in nanosleep */
189
190  empty_line();
191  puts( "Init: nanosleep - EINVAL (NULL time)" );
192  sc = nanosleep ( NULL, &tr );
193  rtems_test_assert( sc == -1 );
194  rtems_test_assert( errno == EINVAL );
195
196  tv.tv_sec = 0;
197  tv.tv_nsec = TOD_NANOSECONDS_PER_SECOND * 2;
198  puts( "Init: nanosleep - EINVAL (too many nanoseconds)" );
199  sc = nanosleep ( &tv, &tr );
200  rtems_test_assert( sc == -1 );
201  rtems_test_assert( errno == EINVAL );
202
203  /* this is an error */
204  tv.tv_sec = -1;
205  tv.tv_nsec = 0;
206  puts( "Init: nanosleep - negative seconds - EINVAL" );
207  sc = nanosleep ( &tv, &tr );
208  rtems_test_assert( sc == -1 );
209  rtems_test_assert( errno == EINVAL );
210
211  /* this is also an error */
212  tv.tv_sec = 0;
213  tv.tv_nsec = -1;
214  puts( "Init: nanosleep - negative nanoseconds - EINVAL" );
215  sc = nanosleep ( &tv, &tr );
216  rtems_test_assert( sc == -1 );
217  rtems_test_assert( errno == EINVAL );
218
219  /* this is actually a small delay */
220  tv.tv_sec = 0;
221  tv.tv_nsec = 1;
222  puts( "Init: nanosleep - delay so small results in one tick" );
223  sc = nanosleep ( &tv, &tr );
224  rtems_test_assert( !sc );
225  rtems_test_assert( !tr.tv_sec );
226  rtems_test_assert( !tr.tv_nsec );
227
228  /* use nanosleep to yield */
229
230  tv.tv_sec = 0;
231  tv.tv_nsec = 0;
232
233  puts( "Init: nanosleep - yield with remaining" );
234  sc = nanosleep ( &tv, &tr );
235  rtems_test_assert( !sc );
236  rtems_test_assert( !tr.tv_sec );
237  rtems_test_assert( !tr.tv_nsec );
238
239  puts( "Init: nanosleep - yield with NULL time remaining" );
240  sc = nanosleep ( &tv, NULL );
241  rtems_test_assert( !sc );
242  rtems_test_assert( !tr.tv_sec );
243  rtems_test_assert( !tr.tv_nsec );
244
245  /* use nanosleep to delay */
246
247  tv.tv_sec = 3;
248  tv.tv_nsec = 500000;
249
250  puts( "Init: nanosleep - 1.05 seconds" );
251  sc = nanosleep ( &tv, &tr );
252  rtems_test_assert( !sc );
253
254  /* print the current real time again */
255  sc = clock_gettime( CLOCK_REALTIME, &tv );
256  rtems_test_assert( !sc );
257  printf( ctime( &tv.tv_sec ) );
258
259  /* check the time remaining */
260
261  printf( "Init: sec (%" PRIdtime_t "), nsec (%ld) remaining\n", tr.tv_sec, tr.tv_nsec );
262  rtems_test_assert( !tr.tv_sec && !tr.tv_nsec );
263
264  puts( "Init: nanosleep - 1.35 seconds" );
265  delay_request.tv_sec = 1;
266  delay_request.tv_nsec = 35000000;
267  sc = nanosleep( &delay_request, NULL );
268  rtems_test_assert( !sc );
269
270  /* print the current real time again */
271  sc = clock_gettime( CLOCK_REALTIME, &tv );
272  rtems_test_assert( !sc );
273  printf( ctime( &tv.tv_sec ) );
274
275  empty_line();
276  puts( "clock_gettime - CLOCK_THREAD_CPUTIME_ID -- ENOSYS" );
277  #if defined(_POSIX_THREAD_CPUTIME)
278    {
279      struct timespec tp;
280      sc = clock_gettime( CLOCK_THREAD_CPUTIME_ID, &tp );
281      check_enosys( sc );
282    }
283  #endif
284
285  puts( "clock_settime - CLOCK_PROCESS_CPUTIME_ID -- ENOSYS" );
286  #if defined(_POSIX_CPUTIME)
287    {
288      struct timespec tp;
289      sc = clock_settime( CLOCK_PROCESS_CPUTIME_ID, &tp );
290      check_enosys( sc );
291    }
292  #endif
293
294  puts( "clock_settime - CLOCK_THREAD_CPUTIME_ID -- ENOSYS" );
295  #if defined(_POSIX_THREAD_CPUTIME)
296    {
297      struct timespec tp;
298      sc = clock_settime( CLOCK_THREAD_CPUTIME_ID, &tp );
299      check_enosys( sc );
300    }
301  #endif
302
303  TEST_END();
304  rtems_test_exit(0);
305}
306
307
308/* configuration information */
309#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
310#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
311
312#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
313
314#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
315#define CONFIGURE_MAXIMUM_TASKS             2
316
317#define CONFIGURE_INIT
318#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.