| 27 | #define BASE_SIZE 1000 |
| 28 | |
| 29 | void *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 | |
| 73 | void CreateThread(int i) { |
| 74 | pthread_attr_t attr; |
no test coverage detected