File: test_pthread_proxying_cpp.cpp

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (283 lines) | stat: -rw-r--r-- 6,478 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
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
#include <cassert>
#include <condition_variable>
#include <emscripten/proxying.h>
#include <emscripten/eventloop.h>
#include <iostream>
#include <sched.h>

using namespace emscripten;

// The worker threads we will use. `looper` sits in a loop, continuously
// processing work as it becomes available, while `returner` returns to the JS
// event loop each time it processes work.
std::thread looper;
std::thread returner;

// The queue used to send work to both `looper` and `returner`.
ProxyingQueue queue;

// Whether `looper` should exit.
std::atomic<bool> should_quit{false};

void looper_main() {
  while (!should_quit) {
    queue.execute();
    sched_yield();
  }
}

void returner_main() {
  // Return back to the event loop while keeping the runtime alive.
  // Note that we can't use `emscripten_exit_with_live_runtime` here without
  // introducing a memory leak due to way to C++11 threads interact with
  // unwinding. See https://github.com/emscripten-core/emscripten/issues/17091.
  emscripten_runtime_keepalive_push();
}

void test_proxy_async() {
  std::cout << "Testing async proxying\n";

  int i = 0;

  std::mutex mutex;
  std::condition_variable cond;
  std::thread::id executor;

  // Proxy to ourselves.
  queue.proxyAsync(pthread_self(), [&]() {
    i = 1;
    executor = std::this_thread::get_id();
  });
  assert(i == 0);
  queue.execute();
  assert(i == 1);
  assert(executor == std::this_thread::get_id());

  // Proxy to looper.
  {
    queue.proxyAsync(looper.native_handle(), [&]() {
      {
        std::unique_lock<std::mutex> lock(mutex);
        i = 2;
      }
      executor = std::this_thread::get_id();
      cond.notify_one();
    });
    std::unique_lock<std::mutex> lock(mutex);
    cond.wait(lock, [&]() { return i == 2; });
    assert(executor == looper.get_id());
  }

  // Proxy to returner.
  {
    queue.proxyAsync(returner.native_handle(), [&]() {
      {
        std::unique_lock<std::mutex> lock(mutex);
        i = 3;
      }
      executor = std::this_thread::get_id();
      cond.notify_one();
    });
    std::unique_lock<std::mutex> lock(mutex);
    cond.wait(lock, [&]() { return i == 3; });
    assert(executor == returner.get_id());
  }
}

void test_proxy_sync() {
  std::cout << "Testing sync proxying\n";

  int i = 0;
  std::thread::id executor;

  // Proxy to looper.
  {
    queue.proxySync(looper.native_handle(), [&]() {
      i = 2;
      executor = std::this_thread::get_id();
    });
    assert(i == 2);
    assert(executor == looper.get_id());
  }

  // Proxy to returner.
  {
    queue.proxySync(returner.native_handle(), [&]() {
      i = 3;
      executor = std::this_thread::get_id();
    });
    assert(i == 3);
    assert(executor == returner.get_id());
  }
}

void test_proxy_sync_with_ctx(void) {
  std::cout << "Testing sync_with_ctx proxying\n";

  int i = 0;
  std::thread::id executor;

  // Proxy to looper.
  {
    queue.proxySyncWithCtx(looper.native_handle(), [&](auto ctx) {
      i = 2;
      executor = std::this_thread::get_id();
      ctx.finish();
    });
    assert(i == 2);
    assert(executor == looper.get_id());
  }

  // Proxy to returner.
  {
    queue.proxySyncWithCtx(returner.native_handle(), [&](auto ctx) {
      i = 3;
      executor = std::this_thread::get_id();
      auto finish = (void(*)(void*))emscripten_proxy_finish;
      emscripten_async_call(finish, ctx.ctx, 0);
    });
    assert(i == 3);
    assert(executor == returner.get_id());
  }
}

void test_proxy_callback(void) {
  std::cout << "Testing callback proxying\n";

  int i = 0;
  thread_local int j = 0;
  std::thread::id executor;

  // Proxy to ourselves.
  queue.proxyCallback(
    pthread_self(),
    [&]() {
      i = 1;
      executor = std::this_thread::get_id();
    },
    [&]() { j = 1; },
    {});
  assert(i == 0);
  queue.execute();
  assert(i == 1);
  assert(executor == std::this_thread::get_id());
  assert(j == 1);

  // Proxy to looper.
  {
    queue.proxyCallback(
      looper.native_handle(),
      [&]() {
        i = 2;
        executor = std::this_thread::get_id();
      },
      [&]() { j = 2; },
      {});
    // TODO: Add a way to wait for work before executing it.
    while (j != 2) {
      queue.execute();
    }
    assert(i == 2);
    assert(executor == looper.get_id());
  }

  // Proxy to returner.
  {
    queue.proxyCallback(
      returner.native_handle(),
      [&]() {
        i = 3;
        executor = std::this_thread::get_id();
      },
      [&]() { j = 3; },
      {});
    // TODO: Add a way to wait for work before executing it.
    while (j != 3) {
      queue.execute();
    }
    assert(i == 3);
    assert(executor == returner.get_id());
  }
}

void test_proxy_callback_with_ctx(void) {
  std::cout << "Testing callback_with_ctx proxying\n";

  int i = 0;
  thread_local int j = 0;
  std::thread::id executor;

  // Proxy to ourselves.
  queue.proxyCallbackWithCtx(
    pthread_self(),
    [&](auto ctx) {
      i = 1;
      executor = std::this_thread::get_id();
      ctx.finish();
    },
    [&]() { j = 1; },
    {});
  assert(i == 0);
  queue.execute();
  assert(i == 1);
  assert(executor == std::this_thread::get_id());
  assert(j == 1);

  // Proxy to looper.
  {
    queue.proxyCallbackWithCtx(
      looper.native_handle(),
      [&](auto ctx) {
        i = 2;
        executor = std::this_thread::get_id();
        ctx.finish();
      },
      [&]() { j = 2; },
      {});
    // TODO: Add a way to wait for work before executing it.
    while (j != 2) {
      queue.execute();
    }
    assert(i == 2);
    assert(executor == looper.get_id());
  }

  // Proxy to returner.
  {
    queue.proxyCallbackWithCtx(
      returner.native_handle(),
      [&](auto ctx) {
        i = 3;
        executor = std::this_thread::get_id();
        auto finish = (void (*)(void*))emscripten_proxy_finish;
        emscripten_async_call(finish, ctx.ctx, 0);
      },
      [&]() { j = 3; },
      {});
    // TODO: Add a way to wait for work before executing it.
    while (j != 3) {
      queue.execute();
    }
    assert(i == 3);
    assert(executor == returner.get_id());
  }
}

int main(int argc, char* argv[]) {
  looper = std::thread(looper_main);
  returner = std::thread(returner_main);

  test_proxy_async();
  test_proxy_sync();
  test_proxy_sync_with_ctx();
  test_proxy_callback();
  test_proxy_callback_with_ctx();

  should_quit = true;
  looper.join();

  pthread_cancel(returner.native_handle());
  returner.join();

  std::cout << "done\n";
}