Skip to content

Instantly share code, notes, and snippets.

@kripken
Created August 27, 2020 00:10
Show Gist options
  • Save kripken/6c9cd1d8e575c09e5636e68cae435bdd to your computer and use it in GitHub Desktop.
Save kripken/6c9cd1d8e575c09e5636e68cae435bdd to your computer and use it in GitHub Desktop.
(func $ThreadMain\28void*\29 (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
(loop $label$1
(local.set $0
(call $__cxa_allocate_exception
(i32.const 4)
)
)
(block $label$2
(block $label$3
(block $label$4
(if
(i32.and
(local.get $1)
(i32.const 3)
)
(block
(i32.store
(local.get $0)
(i32.const 1078530000)
)
(i32.store
- (i32.const 2948)
+ (i32.const 0)
(i32.const 0)
)
(call $invoke_viii
(i32.const 1)
(local.get $0)
(i32.const 1452)
(i32.const 0)
)
(local.set $0
(i32.load
- (i32.const 2948)
+ (i32.const 0)
)
)
(i32.store
- (i32.const 2948)
+ (i32.const 0)
(i32.const 0)
)
(br_if $label$3
(i32.ne
(local.get $0)
(i32.const 1)
)
)
(br $label$4)
)
)
(i32.store
(local.get $0)
(local.get $1)
)
(i32.store
- (i32.const 2948)
+ (i32.const 0)
(i32.const 0)
)
(call $invoke_viii
(i32.const 1)
(local.get $0)
(i32.const 1440)
(i32.const 0)
)
(local.set $0
(i32.load
- (i32.const 2948)
+ (i32.const 0)
)
)
(i32.store
- (i32.const 2948)
+ (i32.const 0)
(i32.const 0)
)
(br_if $label$3
(i32.ne
(local.get $0)
(i32.const 1)
)
)
)
(local.set $0
(call $__cxa_find_matching_catch_4
(i32.const 1440)
(i32.const 1452)
)
)
(if
(i32.eq
(local.tee $2
(call $getTempRet0)
)
// Copyright 2019 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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <emscripten.h>
#include <atomic>
#define NUM_THREADS 2
#define TOTAL 1000
#define THREAD_ADDS 750
#define MAIN_ADDS 5
static std::atomic<int> sum;
static std::atomic<int> total;
void *ThreadMain(void *arg) {
for (int i = 0; i < TOTAL; i++) {
try {
// Throw two different types, to make sure we check throwing and landing
// pad behavior.
if (i & 3) {
throw 3.14159f;
}
throw i;
} catch (int x) {
total += x;
} catch (float f) {
sum++;
// wait for a change, so we see interleaved processing.
int last = sum.load();
while (sum.load() == last) {}
}
}
pthread_exit((void*)TOTAL);
}
pthread_t thread[NUM_THREADS];
void CreateThread(int i)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
static int counter = 1;
int rc = pthread_create(&thread[i], &attr, ThreadMain, (void*)i);
assert(rc == 0);
pthread_attr_destroy(&attr);
}
void loop() {
static int main_adds = 0;
int worker_adds = sum.load() - main_adds;
sum++;
main_adds++;
printf("main iter %d : %d\n", main_adds, worker_adds);
if (worker_adds == NUM_THREADS * THREAD_ADDS &&
main_adds >= MAIN_ADDS) {
printf("done: %d.\n", total.load());
emscripten_cancel_main_loop();
exit(0);
}
}
int main() {
// Create initial threads.
for(int i = 0; i < NUM_THREADS; ++i) {
printf("maek\n");
CreateThread(i);
}
emscripten_set_main_loop(loop, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment