File: test_pthread_proxying_dropped_work.c

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 (55 lines) | stat: -rw-r--r-- 1,369 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
#include <assert.h>
#include <emscripten/console.h>
#include <emscripten/proxying.h>
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>

em_proxying_queue* queue;

_Atomic int exploded = 0;

void explode(void* arg) {
  exploded = 1;
  assert(false);
}

void* proxy_to_self(void* arg) {
  emscripten_proxy_async(queue, pthread_self(), explode, NULL);
  return NULL;
}

void* do_nothing(void* arg) {
  return NULL;
}

int main() {
  emscripten_console_log("start");
  queue = em_proxying_queue_create();
  assert(queue);

  // Check that proxying to a thread that exits without a live runtime causes
  // the work to be dropped without other errors.
  pthread_t worker;
  pthread_create(&worker, NULL, do_nothing, NULL);
  emscripten_proxy_async(queue, worker, explode, NULL);

  // Check that a thread proxying to itself but exiting without a live runtime
  // causes the work to be dropped without other errors.
  pthread_t self_proxier;
  pthread_create(&self_proxier, NULL, proxy_to_self, NULL);

  pthread_join(worker, NULL);
  pthread_join(self_proxier, NULL);

  // Wait a bit (50 ms) to see if the `assert(pthread_self())` in
  // emscripten_proxy_execute_queue fires or if we explode.
  struct timespec time = {
    .tv_sec = 0,
    .tv_nsec = 50 * 1000 * 1000,
  };
  nanosleep(&time, NULL);

  assert(!exploded);
  emscripten_console_log("done");
}