File: cpp11_thread_local.cpp

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 (35 lines) | stat: -rw-r--r-- 814 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
#include <emscripten.h>
#include <emscripten/wasm_worker.h>
#include <assert.h>

thread_local int tls = 1;

void main_thread_func()
{
  assert(!emscripten_current_thread_is_wasm_worker());
  EM_ASM(out($0), tls);
#ifdef REPORT_RESULT
  REPORT_RESULT(tls);
#endif
}

void worker_main()
{
  assert(emscripten_current_thread_is_wasm_worker());
  assert(tls != 42);
  assert(tls != 0);
  assert(tls == 1);
  tls = 123456; // Try to write garbage data to the memory location.
  emscripten_wasm_worker_post_function_v(0, main_thread_func);
}

char stack[1024];

int main()
{
  EM_ASM(out($0), tls);
  assert(!emscripten_current_thread_is_wasm_worker());
  tls = 42;
  emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(stack, sizeof(stack));
  emscripten_wasm_worker_post_function_v(worker, worker_main);
}