summaryrefslogtreecommitdiff
path: root/source/tests/test_interprocess.c
blob: 027f4ae9eb56341e9113a7a17034d09f3b834290 (plain)
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
#include "../kit/shared_memory.h"
#include "../kit/shared_mutex.h"
#include "../kit/threads.h"

#include <stdio.h>

#define NAME "kit_test_interprocess"

enum { DATA_SIZE = 64, TIMEOUT = 3 };

typedef struct {
  shared_mutex_t m;
  i8             state;
  i8             bytes[DATA_SIZE];
} shared_data_t;

enum { STATE_INIT, STATE_READY, STATE_DONE };

int run_writer() {
  shared_memory_t mem = shared_memory_open(
      SZ(NAME), sizeof(shared_data_t), SHARED_MEMORY_CREATE);

  if (mem.status != KIT_OK) {
    printf("Writer: kit_shared_memory_open failed.\n");
    fflush(stdout);
    return 1;
  }

  shared_data_t *p = (shared_data_t *) mem.bytes;

  shared_mutex_init(&p->m);

  unique_lock(&p->m);
  p->state = STATE_INIT;
  unique_unlock(&p->m);

  thrd_yield();

  unique_lock(&p->m);
  for (int i = 0; i < DATA_SIZE; i++) p->bytes[i] = i;
  p->state = STATE_READY;
  unique_unlock(&p->m);

  struct timespec t0;
  timespec_get(&t0, TIME_UTC);

  shared_lock(&p->m);
  while (p->state != STATE_DONE) {
    shared_unlock(&p->m);

    struct timespec t1;
    timespec_get(&t1, TIME_UTC);

    if (t1.tv_sec - t0.tv_sec > TIMEOUT) {
      printf("Writer: Timeout.\n");
      shared_memory_close(&mem);
      return 1;
    }

    thrd_yield();
    shared_lock(&p->m);
  }
  shared_unlock(&p->m);

  if (shared_memory_close(&mem) != KIT_OK) {
    printf("Writer: kit_shared_memory_close failed.\n");
    fflush(stdout);
    return 1;
  }

  return 0;
}

int run_reader() {
  struct timespec t0;
  timespec_get(&t0, TIME_UTC);

  shared_memory_t mem;
  for (;;) {
    mem = shared_memory_open(SZ(NAME), sizeof(shared_data_t),
                             SHARED_MEMORY_OPEN);
    if (mem.status == KIT_OK)
      break;

    struct timespec t1;
    timespec_get(&t1, TIME_UTC);

    if (t1.tv_sec - t0.tv_sec > TIMEOUT) {
      printf("Reader: Timeout.\n");
      return 1;
    }

    struct timespec delay = { .tv_sec = 0, .tv_nsec = 1000000 };
    thrd_sleep(&delay, NULL);
  }

  shared_data_t *p = (shared_data_t *) mem.bytes;

  shared_lock(&p->m);
  while (p->state != STATE_READY) {
    shared_unlock(&p->m);

    struct timespec t1;
    timespec_get(&t1, TIME_UTC);

    if (t1.tv_sec - t0.tv_sec > TIMEOUT) {
      printf("Reader: Timeout.\n");
      return 1;
    }

    thrd_yield();
    shared_lock(&p->m);
  }

  i32 status = 0;

  for (i32 i = 0; i < DATA_SIZE; i++)
    if (p->bytes[i] != i) {
      printf("Reader: Wrong byte %d\n", i);
      fflush(stdout);
      status = 1;
    }

  shared_unlock(&p->m);

  unique_lock(&p->m);
  p->state = STATE_DONE;
  unique_unlock(&p->m);

  if (shared_memory_close(&mem) != KIT_OK) {
    printf("Reader: kit_shared_memory_close failed.\n");
    fflush(stdout);
    status = 1;
  }

  return status;
}

int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Wrong number of command line arguments %d\n", argc);
    return 1;
  }

  if (strcmp(argv[1], "writer") == 0) {
    struct timespec t0;
    timespec_get(&t0, TIME_UTC);

    i32 status = run_writer();

    struct timespec t1;
    timespec_get(&t1, TIME_UTC);

    i64 sec  = t1.tv_sec - t0.tv_sec;
    i64 nsec = t1.tv_nsec - t0.tv_nsec;

    printf("Writer: Done in %.2lf msec\n",
           (sec * 1000000000 + nsec) * 0.000001);
    fflush(stdout);

    return status;
  }

  if (strcmp(argv[1], "reader") == 0)
    return run_reader();

  if (strcmp(argv[1], "clean") == 0) {
    shared_memory_clean(SZ(NAME));
    return 0;
  }

  printf("Invalid command line argument \"%s\"\n", argv[1]);
  return 1;
}