File: thread_stack.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 (43 lines) | stat: -rw-r--r-- 1,520 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
#include <emscripten.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/stack.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#define THREAD_STACK_SIZE 1024
#define NUM_THREADS 2
void *thread_stack[NUM_THREADS];

volatile int threadsOk = 0;

void test_stack(int i)
{
  EM_ASM(out(`In thread ${$0}, stack low addr=0x${$1.toString(16)}, emscripten_stack_get_base()=0x${$2.toString(16)}, emscripten_stack_get_end()=0x${$3.toString(16)}, THREAD_STACK_SIZE=0x${$4.toString(16)}`),
    i, thread_stack[i], emscripten_stack_get_base(), emscripten_stack_get_end(), THREAD_STACK_SIZE);
  assert(emscripten_stack_get_base() == (uintptr_t)thread_stack[i] + THREAD_STACK_SIZE);
  assert(emscripten_stack_get_end() == (uintptr_t)thread_stack[i]);

  int ok = __sync_fetch_and_add(&threadsOk, 1);
  EM_ASM(out($0), ok);
  if (ok == 1)
  {
    EM_ASM(out(`Test finished!`));
#ifdef REPORT_RESULT
    REPORT_RESULT(0);
#endif
  }
}

int main()
{
  EM_ASM(out(`Main thread stack base=0x${$0.toString(16)}, end=0x${$1.toString(16)}`), emscripten_stack_get_base(), emscripten_stack_get_end());

  for(int i = 0; i < NUM_THREADS; ++i)
  {
    thread_stack[i] = memalign(16, THREAD_STACK_SIZE);
    emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(thread_stack[i], THREAD_STACK_SIZE);
    EM_ASM(out(`Created thread ${$0} with stack ptr=0x${$1.toString(16)}, size=0x${$2.toString(16)}`), i, thread_stack[i], THREAD_STACK_SIZE);
    emscripten_wasm_worker_post_function_vi(worker, test_stack, i);
  }
}