summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxclockrealtime01/init.c
blob: 8deac3cf88ad49b114d07ac5832faeb45b40c2d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

/*
 * This test program runs also on GNU/Linux.  Use
 *
 * cc init.c -pthread -Wall -Wextra -lrt && sudo ./a.out
 *
 * to run it.  It must run probably as root for the clock_settime().
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <mqueue.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#ifdef __rtems__

#include "tmacros.h"

#else /* __rtems__ */

#include <assert.h>

#define rtems_test_assert(x) assert(x)

#endif /* __rtems__ */

static void assert_eno(const char *hint, int eno, int expected_eno)
{
  const char *warn;

  if (eno != expected_eno) {
    warn = "WARNING: ";
  } else {
    warn = "";
  }

  printf(
    "%s%s: actual '%s', expected '%s'\n",
    warn,
    hint,
    strerror(eno),
    strerror(expected_eno)
  );
#ifdef __rtems__
  rtems_test_assert(eno == expected_eno);
#endif /* __rtems__ */
}

static void assert_rv(const char *hint, int rv, int expected_eno)
{
  int eno;

  if (rv != 0) {
    eno = errno;
  } else {
    eno = 0;
  }

  assert_eno(hint, eno, expected_eno);
}

typedef struct {
  char data;
} test_msg;

#define MSG_COUNT 1

#define MSG_SIZE sizeof(test_msg)

typedef struct {
  int value;
  pthread_mutex_t mtx;
  pthread_cond_t cnd;
} event;

typedef enum {
  ACTION_NONE,
  ACTION_MTX_LOCK,
  ACTION_CND_WAIT,
  ACTION_RW_RDLOCK,
  ACTION_RW_WRLOCK,
  ACTION_SEM_WAIT,
  ACTION_MQ_SEND,
  ACTION_MQ_RECV,
  ACTION_CLOCK_NANOSLEEP,
  ACTION_TERMINATE
} test_action;

typedef enum {
  MODE_TIMEOUT_FINITE,
  MODE_TIMEOUT_NEGATIVE_SEC,
  MODE_TIMEOUT_NEGATIVE_NSEC,
  MODE_TIMEOUT_NEGATIVE_SEC_NSEC,
  MODE_TIMEOUT_HUGE_NSEC,
#ifdef __rtems__
  MODE_TIMEOUT_NULL,
#endif /* __rtems__ */
  MODE_TIMEOUT_PRACTICALLY_INFINITE,
  MODE_COUNT
} test_mode;

typedef struct {
  test_mode mode;
  pthread_t worker;
  event action;
  event done;
  pthread_mutex_t mtx;
  pthread_mutex_t mtx2;
  pthread_cond_t cnd;
  pthread_rwlock_t rw;
  sem_t sem;
  mqd_t mq;
  int counter[ACTION_TERMINATE + 1];
} test_context;

static test_context test_instance;

static void event_init(event *e)
{
  int eno;

  eno = pthread_mutex_init(&e->mtx, NULL);
  rtems_test_assert(eno == 0);

  eno = pthread_cond_init(&e->cnd, NULL);
  rtems_test_assert(eno == 0);
}

static void event_destroy(event *e)
{
  int eno;

  eno = pthread_mutex_destroy(&e->mtx);
  rtems_test_assert(eno == 0);

  eno = pthread_cond_destroy(&e->cnd);
  rtems_test_assert(eno == 0);
}

static void event_post(event *e, int value)
{
  int eno;

  eno = pthread_mutex_lock(&e->mtx);
  rtems_test_assert(eno == 0);

  e->value = value;

  eno = pthread_cond_signal(&e->cnd);
  rtems_test_assert(eno == 0);

  eno = pthread_mutex_unlock(&e->mtx);
  rtems_test_assert(eno == 0);
}

static int event_get(event *e)
{
  int eno;
  int value;

  eno = pthread_mutex_lock(&e->mtx);
  rtems_test_assert(eno == 0);

  while (true) {
    value = e->value;

    if (value != 0) {
      e->value = 0;
      break;
    }

    eno = pthread_cond_wait(&e->cnd, &e->mtx);
    rtems_test_assert(eno == 0);
  }

  eno = pthread_mutex_unlock(&e->mtx);
  rtems_test_assert(eno == 0);

  return value;
}

static void *worker(void *arg)
{
  test_context *ctx;
  test_action action;
  test_mode mode;
  int eno;
  int unused;

  ctx = arg;
  mode = ctx->mode;

  eno = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &unused);
  rtems_test_assert(eno == 0);

  eno = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &unused);
  rtems_test_assert(eno == 0);

  while ((action = event_get(&ctx->action)) != ACTION_TERMINATE) {
    int rv;
    struct timespec to_storage;
    const struct timespec *to;
    int expected_eno;
    test_msg msg;
    unsigned prio;

    switch (mode) {
      case MODE_TIMEOUT_FINITE:
        rv = clock_gettime(CLOCK_REALTIME, &to_storage);
        rtems_test_assert(rv == 0);

        to_storage.tv_sec += 3600;
        to = &to_storage;
        expected_eno = ETIMEDOUT;
        break;
      case MODE_TIMEOUT_NEGATIVE_SEC:
        to_storage.tv_sec = -1;
        to_storage.tv_nsec = 1;
        to = &to_storage;
        expected_eno = ETIMEDOUT;
        break;
      case MODE_TIMEOUT_NEGATIVE_NSEC:
        to_storage.tv_sec = 1;
        to_storage.tv_nsec = -1;
        to = &to_storage;
        expected_eno = EINVAL;
        break;
      case MODE_TIMEOUT_NEGATIVE_SEC_NSEC:
        to_storage.tv_sec = -1;
        to_storage.tv_nsec = -1;
        to = &to_storage;
        expected_eno = EINVAL;
        break;
      case MODE_TIMEOUT_HUGE_NSEC:
        to_storage.tv_sec = 1;
        to_storage.tv_nsec = LONG_MAX;
        to = &to_storage;
        expected_eno = EINVAL;
        break;
#ifdef __rtems__
      case MODE_TIMEOUT_NULL:
        to = NULL;
        expected_eno = EINVAL;
        break;
#endif /* __rtems__ */
      case MODE_TIMEOUT_PRACTICALLY_INFINITE:
        to_storage.tv_sec = INT64_MAX;
        to_storage.tv_nsec = 999999999;
        to = &to_storage;
        expected_eno = -1;
        break;
      default:
        to = NULL;
        expected_eno = -1;
        rtems_test_assert(0);
        break;
    }

    switch (action) {
      case ACTION_MTX_LOCK:
        eno = pthread_mutex_timedlock(&ctx->mtx, to);
        assert_eno("pthread_mutex_timedlock", eno, expected_eno);
        break;
      case ACTION_CND_WAIT:
        eno = pthread_mutex_lock(&ctx->mtx2);
        rtems_test_assert(eno == 0);

        eno = pthread_cond_timedwait(&ctx->cnd, &ctx->mtx2, to);
        assert_eno("pthread_cond_timedwait", eno, expected_eno);

        eno = pthread_mutex_unlock(&ctx->mtx2);
        rtems_test_assert(eno == 0);
        break;
      case ACTION_RW_RDLOCK:
        eno = pthread_rwlock_timedrdlock(&ctx->rw, to);
        assert_eno("pthread_rwlock_timedrdlock", eno, expected_eno);
        break;
      case ACTION_RW_WRLOCK:
        eno = pthread_rwlock_timedwrlock(&ctx->rw, to);
        assert_eno("pthread_rwlock_timedwrlock", eno, expected_eno);
        break;
      case ACTION_SEM_WAIT:
        errno = 0;
        rv = sem_timedwait(&ctx->sem, to);
        assert_rv("sem_timedwait", rv, expected_eno);
        break;
      case ACTION_MQ_SEND:
        msg.data = 13;
        rv = mq_send(ctx->mq, &msg.data, sizeof(msg), 7);
        rtems_test_assert(rv == 0);
        msg.data = 31;
        errno = 0;
        rv = mq_timedsend(ctx->mq, &msg.data, sizeof(msg), 5, to);
        assert_rv("mq_timedsend", rv, expected_eno);
        break;
      case ACTION_MQ_RECV:
        msg.data = 0;
        prio = 0;
        rv = mq_receive(ctx->mq, &msg.data, sizeof(msg), &prio);
        rtems_test_assert(rv == 1);
        rtems_test_assert(msg.data == 13);
        rtems_test_assert(prio == 7);
        msg.data = 0;
        prio = 0;
        errno = 0;
        rv = mq_timedreceive(ctx->mq, &msg.data, sizeof(msg), &prio, to);
        assert_rv("mq_timedreceive", rv, expected_eno);
        rtems_test_assert(msg.data == 0);
        rtems_test_assert(prio == 0);
        break;
      case ACTION_CLOCK_NANOSLEEP:
        rv = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, to, NULL);

        if (expected_eno == ETIMEDOUT) {
          assert_rv("clock_nanosleep", rv, 0);
        } else {
          assert_rv("clock_nanosleep", rv, expected_eno);
        }
        break;
      default:
        rtems_test_assert(0);
        break;
    }

    ++ctx->counter[action];

    event_post(&ctx->done, 1);
  }

  return ctx;
}

static void delay(void)
{
  int rv;

  rv = usleep(100000);
  rtems_test_assert(rv == 0);
}

static void run(
  test_context *ctx,
  const char *test,
  test_mode mode,
  void (*f)(void)
)
{
  test_action action;
  int eno;
  void *status;

  printf("*** %s ***\n", test);
  ctx->mode = mode;

  eno = pthread_create(&ctx->worker, NULL, worker, ctx);
  rtems_test_assert(eno == 0);

  for (action = ACTION_MTX_LOCK; action < ACTION_TERMINATE; ++action) {
    event_post(&ctx->action, action);
    delay();

    (*f)();

    event_get(&ctx->done);
  }

  event_post(&ctx->action, ACTION_TERMINATE);

  status = NULL;
  eno = pthread_join(ctx->worker, &status);
  rtems_test_assert(eno == 0);
  rtems_test_assert(status == ctx);
}

static void timeout_finite(void)
{
  struct timespec now;
  int rv;

  rv = clock_gettime(CLOCK_REALTIME, &now);
  rtems_test_assert(rv == 0);

  now.tv_sec += 7200;

  rv = clock_settime(CLOCK_REALTIME, &now);
  rtems_test_assert(rv == 0);
}

static void test_timeout_finite(test_context *ctx)
{
  run(ctx, "timeout finite", MODE_TIMEOUT_FINITE, timeout_finite);
}

static void do_nothing(void)
{
  /* Do nothing */
}

static void test_timeout_negative_sec(test_context *ctx)
{
  run(ctx, "timeout negative sec", MODE_TIMEOUT_NEGATIVE_SEC, do_nothing);
}

static void test_timeout_negative_nsec(test_context *ctx)
{
  run(ctx, "timout negative nsec", MODE_TIMEOUT_NEGATIVE_NSEC, do_nothing);
}

static void test_timeout_negative_sec_nsec(test_context *ctx)
{
  run(
    ctx,
    "timout negative sec and nsec",
    MODE_TIMEOUT_NEGATIVE_SEC_NSEC,
    do_nothing
  );
}

static void test_timeout_huge_nsec(test_context *ctx)
{
  run(ctx, "timout huge nsec", MODE_TIMEOUT_HUGE_NSEC, do_nothing);
}

#ifdef __rtems__
static void test_timeout_null(test_context *ctx)
{
  run(ctx, "timeout NULL", MODE_TIMEOUT_NULL, do_nothing);
}
#endif /* __rtems__ */

static void test_timeout_practically_infinite(test_context *ctx)
{
  test_action action;
  int eno;

  puts("*** timeout practically infinite ***");
  ctx->mode = MODE_TIMEOUT_PRACTICALLY_INFINITE;

  for (action = ACTION_MTX_LOCK; action < ACTION_TERMINATE; ++action) {
    void *status;

    eno = pthread_create(&ctx->worker, NULL, worker, ctx);
    rtems_test_assert(eno == 0);

    event_post(&ctx->action, action);
    delay();

    eno = pthread_cancel(ctx->worker);
    rtems_test_assert(eno == 0);

    status = NULL;
    eno = pthread_join(ctx->worker, &status);
    rtems_test_assert(eno == 0);
    rtems_test_assert(status == PTHREAD_CANCELED);
  }
}

static void test(test_context *ctx)
{
  test_action action;
  int eno;
  int rv;
  mode_t mode;
  struct mq_attr ma;

  event_init(&ctx->action);
  event_init(&ctx->done);

  eno = pthread_mutex_init(&ctx->mtx, NULL);
  rtems_test_assert(eno == 0);

  eno = pthread_mutex_lock(&ctx->mtx);
  rtems_test_assert(eno == 0);

  eno = pthread_mutex_init(&ctx->mtx2, NULL);
  rtems_test_assert(eno == 0);

  eno = pthread_cond_init(&ctx->cnd, NULL);
  rtems_test_assert(eno == 0);

  eno = pthread_rwlock_init(&ctx->rw, NULL);
  rtems_test_assert(eno == 0);

  eno = pthread_rwlock_wrlock(&ctx->rw);
  rtems_test_assert(eno == 0);

  rv = sem_init(&ctx->sem, 0, 0);
  rtems_test_assert(rv == 0);

  mode = S_IRWXU | S_IRWXG | S_IRWXO;
  memset(&ma, 0, sizeof(ma));
  ma.mq_maxmsg = MSG_COUNT;
  ma.mq_msgsize = MSG_SIZE;
  ctx->mq = mq_open("/mq", O_CREAT | O_RDWR, mode, &ma);
  rtems_test_assert(ctx->mq != (mqd_t) -1);

  test_timeout_finite(ctx);
  test_timeout_negative_sec(ctx);
  test_timeout_negative_nsec(ctx);
  test_timeout_negative_sec_nsec(ctx);
  test_timeout_huge_nsec(ctx);
#ifdef __rtems__
  test_timeout_null(ctx);
#endif /* __rtems__ */
  test_timeout_practically_infinite(ctx);

  event_destroy(&ctx->action);
  event_destroy(&ctx->done);

  eno = pthread_mutex_unlock(&ctx->mtx);
  rtems_test_assert(eno == 0);

  eno = pthread_mutex_destroy(&ctx->mtx);
  rtems_test_assert(eno == 0);

  eno = pthread_mutex_destroy(&ctx->mtx2);
#ifdef __rtems__
  rtems_test_assert(eno == 0);
#else /* __rtems__ */
  rtems_test_assert(eno == 0 || eno == EBUSY);
#endif /* __rtems__ */

  eno = pthread_cond_destroy(&ctx->cnd);
  rtems_test_assert(eno == 0);

  eno = pthread_rwlock_unlock(&ctx->rw);
  rtems_test_assert(eno == 0);

  eno = pthread_rwlock_destroy(&ctx->rw);
  rtems_test_assert(eno == 0);

  rv = sem_destroy(&ctx->sem);
  rtems_test_assert(rv == 0);

  rv = mq_close(ctx->mq);
  rtems_test_assert(rv == 0);

  rv = mq_unlink("/mq");
  rtems_test_assert(rv == 0);

  for (action = ACTION_MTX_LOCK; action < ACTION_TERMINATE; ++action) {
    rtems_test_assert(ctx->counter[action] == MODE_COUNT - 1);
  }
}

#ifdef __rtems__

const char rtems_test_name[] = "PSXCLOCKREALTIME 1";

static void *POSIX_Init(void *arg)
{
  TEST_BEGIN();
  test(&test_instance);
  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
#define CONFIGURE_MAXIMUM_POSIX_MESSAGE_QUEUES 1

#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>

#else /* __rtems__ */

int main(void)
{
  test(&test_instance);
  return 0;
}

#endif /* __rtems__ */