MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / ThreadMain

Function ThreadMain

test/other/test_malloc_multithreading.c:29–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27#define BASE_SIZE 1000
28
29void *ThreadMain(void *arg) {
30 puts("thread started");
31 running++;
32 void** allocations = (void**)malloc(AT_ONCE * sizeof(void*));
33 for (int i = 0; i < AT_ONCE; i++) {
34 allocations[i] = NULL;
35 }
36 for (int i = 0; i < (TOTAL / WORKERS); i++) {
37 int rem = i & (AT_ONCE - 1);
38 void** allocation = &allocations[rem];
39 if (*allocation) {
40 free(*allocation);
41 }
42 *allocation = malloc(BASE_SIZE + rem);
43 if (!*allocation) {
44 puts("failed to allocate");
45 abort();
46 }
47 char* data = (char*)*allocation;
48 *data = i;
49 }
50 int total = 0;
51 for (int i = 0; i < AT_ONCE; i++) {
52 void* allocation = allocations[i];
53 if (!allocation) {
54 puts("did not have allocation");
55 abort();
56 }
57 char* data = (char*)allocation;
58 total += *data;
59 free(allocation);
60 }
61 free(allocations);
62 printf("thread exiting with total %d\n", total);
63 if (atomic_fetch_sub(&running, 1) == 1) {
64#if VERBOSE
65 double end = emscripten_date_now();
66 printf("total time %.2f\n", end - start);
67#endif
68 printf("Done.\n");
69 }
70 pthread_exit(0);
71}
72
73void CreateThread(int i) {
74 pthread_attr_t attr;

Callers 1

mainFunction · 0.70

Calls 7

putsFunction · 0.85
printfFunction · 0.85
emscripten_date_nowFunction · 0.85
mallocFunction · 0.50
freeFunction · 0.50
abortFunction · 0.50
atomic_fetch_subFunction · 0.50

Tested by

no test coverage detected