-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from turbolent/improve-shared-memory
- Loading branch information
Showing
9 changed files
with
179 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
LDFLAGS += -lm -L../../wasi -lw2c2wasi -L../../futex -lw2c2futex | ||
CFLAGS += -O0 -g -DWASM_THREADS_PTHREADS | ||
W2C2 ?= ../../w2c2/w2c2 | ||
|
||
pthreads: pthreads.o main.o | ||
$(CC) $(CFLAGS) $^ -o pthreads $(LDFLAGS) | ||
|
||
%.c: src/%.wasm | ||
$(W2C2) -g $< $@ | ||
|
||
%.o: %.c | ||
$(CC) -I../../w2c2 -c $(CFLAGS) $< -o $@ | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f *.o pthreads pthreads.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
#include "w2c2_base.h" | ||
#include "../../wasi/wasi.h" | ||
#include "pthreads.h" | ||
|
||
void | ||
trap( | ||
Trap trap | ||
) { | ||
fprintf(stderr, "TRAP: %s\n", trapDescription(trap)); | ||
abort(); | ||
} | ||
|
||
|
||
wasmMemory* | ||
wasiMemory( | ||
void* instance | ||
) { | ||
return pthreads_memory((pthreadsInstance*)instance); | ||
} | ||
|
||
extern char** environ; | ||
|
||
wasmMemory* mem = NULL; | ||
|
||
void* resolveImport(const char* moduleName, const char* importName) { | ||
if (strcmp(moduleName, "env") == 0 && strcmp(importName, "memory") == 0) { | ||
return mem; | ||
} | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
pthreadsInstance instance; | ||
|
||
mem = WASM_MEMORY_ALLOCATE_SHARED(10, 20); | ||
|
||
pthreadsInstantiate(&instance, resolveImport); | ||
|
||
if (!wasiInit(argc, argv, environ)) { | ||
fprintf(stderr, "failed to initialize WASI\n"); | ||
return 1; | ||
} | ||
|
||
if (!wasiFileDescriptorAdd(-1, "/", NULL)) { | ||
fprintf(stderr, "failed to add preopen\n"); | ||
return 1; | ||
} | ||
|
||
|
||
pthreads__start(&instance); | ||
|
||
pthreadsFreeInstance(&instance); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CC = ${WASI_SDK_PATH}/bin/clang | ||
|
||
pthreads.wasm: main.c | ||
$(CC) --target=wasm32-wasi-threads --sysroot ${WASI_SDK_PATH}/share/wasi-sysroot -O0 -g -pthread -Wl,--import-memory,--export-memory,--max-memory=67108864 -o $@ $< | ||
|
||
.PHONY: | ||
clean: | ||
rm pthreads.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <stdio.h> | ||
#include <pthread.h> | ||
|
||
#define NUM_THREADS 4 | ||
#define NUM_ITER 10 | ||
|
||
int g_count = 0; | ||
|
||
static void* thread(void *arg) { | ||
printf("Entering thread\n"); | ||
|
||
for (int i = 0; i < NUM_ITER; i++) { | ||
printf( | ||
"Incremented counter: %d\n", | ||
__atomic_add_fetch(&g_count, 1, __ATOMIC_SEQ_CST) | ||
); | ||
} | ||
|
||
printf("Leaving thread\n"); | ||
|
||
return NULL; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
pthread_t tids[NUM_THREADS]; | ||
|
||
printf("Starting threads\n"); | ||
|
||
for (int i = 0; i < NUM_THREADS; i++) { | ||
if (pthread_create(&tids[i], NULL, thread, NULL) != 0) { | ||
printf("Thread creation failed\n"); | ||
} | ||
} | ||
|
||
printf("Joining threads\n"); | ||
|
||
for (int i = 0; i < NUM_THREADS; i++) { | ||
if (pthread_join(tids[i], NULL) != 0) { | ||
printf("Thread join failed\n"); | ||
} | ||
} | ||
|
||
printf( | ||
"Value of counter after update: %d (expected=%d)\n", | ||
g_count, | ||
NUM_THREADS * NUM_ITER | ||
); | ||
if (g_count != NUM_THREADS * NUM_ITER) { | ||
__builtin_trap(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters