File: test_pthread_proxy_to_pthread.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (47 lines) | stat: -rw-r--r-- 1,395 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
/*
 * Copyright 2017 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

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

#include <emscripten/em_asm.h>
#include <emscripten/eventloop.h>
#include <emscripten/threading.h>

_Atomic bool done_callback = false;

void main_thread_callback(void* user_data) {
  printf("main_thread_callback called\n");
  done_callback = true;
}

void set_timeout_on_main() {
  int is_worker = EM_ASM_INT(return ENVIRONMENT_IS_WORKER);
  printf("main: ENVIRONMENT_IS_WORKER: %d\n", is_worker);
  // Verify that we can do async work here on the main thread
  // without causing the runtime to exit.
  emscripten_set_immediate(main_thread_callback, NULL);
}

int main() {
  int is_worker = EM_ASM_INT(return ENVIRONMENT_IS_WORKER);
  printf("ENVIRONMENT_IS_WORKER: %d\n", is_worker);
  assert(is_worker);

  emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, set_timeout_on_main);
  while (!done_callback) {}

  // Repeat the same process to ensure the main thread is still responsive.
  done_callback = false;

  emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, set_timeout_on_main);
  while (!done_callback) {}

  printf("main done\n");
  return 0;
}