Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions test/pthread/test_pthread_cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,44 @@

#include <pthread.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <emscripten/console.h>

_Atomic long res = 43;
static void cleanup_handler(void *arg)
{
_Atomic bool done_cleanup = false;

void cleanup_handler(void *arg) {
long a = (long)arg;
emscripten_outf("Called clean-up handler with arg %ld", a);
res -= a;
assert(a == 42);
done_cleanup = true;
}

static void *thread_start(void *arg)
{
void *thread_start(void *arg) {
pthread_cleanup_push(cleanup_handler, (void*)42);
emscripten_out("Thread started!");
for(;;)
{
while (1) {
pthread_testcancel();
}
res = 1000; // Shouldn't ever reach here.
assert(false);
pthread_cleanup_pop(0);
}

pthread_t thr;

int main()
{
int main() {
pthread_t thr;
int s = pthread_create(&thr, NULL, thread_start, (void*)0);
assert(s == 0);
emscripten_out("Canceling thread..");
s = pthread_cancel(thr);
assert(s == 0);

for(;;)
{
int result = res;
if (result == 1)
{
emscripten_outf("After canceling, shared variable = %d", result);
return 0;
}
while (!done_cleanup) {
}

__builtin_trap();
emscripten_outf("After canceling, cleanup complete");
return 0;
}
2 changes: 1 addition & 1 deletion test/pthread/test_pthread_cancel.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Canceling thread..
Thread started!
Called clean-up handler with arg 42
After canceling, shared variable = 1
After canceling, cleanup complete
25 changes: 18 additions & 7 deletions test/pthread/test_pthread_cancel_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,31 @@
#include <assert.h>
#include <unistd.h>
#include <errno.h>

#ifdef __EMSCRIPTEN__
#include <emscripten/console.h>
#else
void emscripten_out(const char* msg) {
printf("%s\n", msg);
}

void emscripten_outf(const char* msg, ...) {
printf("%s\n", msg);
}
#endif

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
_Atomic long res = 43;
_Atomic int started = false;
_Atomic bool started = false;
_Atomic bool done_cleanup = false;

static void cleanup_handler(void *arg)
{
void cleanup_handler(void *arg) {
long a = (long)arg;
emscripten_outf("Called clean-up handler with arg %ld", a);
res -= a;
assert(a == 42);
done_cleanup = true;
}

static void *thread_start(void *arg) {
void *thread_start(void *arg) {
// Setup thread for async cancellation only
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
Expand Down Expand Up @@ -61,7 +72,7 @@ int main() {
s = pthread_cancel(thr);
assert(s == 0);
// Busy wait until thread cancel handler has been run
while (res != 1) {
while (!done_cleanup) {
sched_yield();
}

Expand Down
Loading