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");
}
|