File: test_sem.c

package info (click to toggle)
zix 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 828 kB
  • sloc: ansic: 9,083; cpp: 479; python: 127; makefile: 6
file content (97 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (2)
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
// Copyright 2012-2021 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC

#undef NDEBUG

#include <zix/attributes.h>
#include <zix/sem.h>
#include <zix/status.h>
#include <zix/thread.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

static ZixSem   sem;
static unsigned n_signals = 1024;

static ZixThreadResult ZIX_THREAD_FUNC
reader(void* ZIX_UNUSED(arg))
{
  printf("Reader starting\n");

  for (unsigned i = 0; i < n_signals; ++i) {
    assert(!zix_sem_wait(&sem));
  }

  printf("Reader finished\n");
  return ZIX_THREAD_RESULT;
}

static ZixThreadResult ZIX_THREAD_FUNC
writer(void* ZIX_UNUSED(arg))
{
  printf("Writer starting\n");

  for (unsigned i = 0; i < n_signals; ++i) {
    assert(!zix_sem_post(&sem));
  }

  printf("Writer finished\n");
  return ZIX_THREAD_RESULT;
}

static void
test_try_wait(void)
{
  assert(!zix_sem_init(&sem, 0));
  assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE);
  assert(!zix_sem_post(&sem));
  assert(!zix_sem_try_wait(&sem));
  assert(zix_sem_try_wait(&sem) == ZIX_STATUS_UNAVAILABLE);
  assert(!zix_sem_destroy(&sem));
}

static void
test_timed_wait(void)
{
  assert(!zix_sem_init(&sem, 0));
  assert(zix_sem_timed_wait(&sem, 0, 0) == ZIX_STATUS_TIMEOUT);
  assert(zix_sem_timed_wait(&sem, 0, 999999999) == ZIX_STATUS_TIMEOUT);
  assert(!zix_sem_post(&sem));
  assert(!zix_sem_timed_wait(&sem, 5, 0));
  assert(!zix_sem_post(&sem));
  assert(!zix_sem_timed_wait(&sem, 1000, 0));
  assert(!zix_sem_destroy(&sem));
}

int
main(int argc, char** argv)
{
  if (argc > 2) {
    printf("Usage: %s N_SIGNALS\n", argv[0]);
    return 1;
  }

  if (argc > 1) {
    n_signals = (unsigned)strtol(argv[1], NULL, 10);
  }

  test_try_wait();
  test_timed_wait();

  printf("Testing %u signals...\n", n_signals);

  assert(!zix_sem_init(&sem, 0));

  ZixThread reader_thread; // NOLINT
  assert(!zix_thread_create(&reader_thread, 128, reader, NULL));

  ZixThread writer_thread; // NOLINT
  assert(!zix_thread_create(&writer_thread, 128, writer, NULL));

  assert(!zix_thread_join(reader_thread));
  assert(!zix_thread_join(writer_thread));
  assert(!zix_sem_destroy(&sem));
  return 0;
}