Skip to content

Commit a6a1ba2

Browse files
authored
Convert test_malloc_multithreading to C. NFC (#25328)
1 parent f9e4785 commit a6a1ba2

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

test/other/test_malloc_multithreading.cpp renamed to test/other/test_malloc_multithreading.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6-
#include <atomic>
7-
#include <cassert>
6+
#include <assert.h>
7+
#include <stdatomic.h>
88
#include <stdio.h>
99
#include <stdlib.h>
1010
#include <pthread.h>
@@ -17,7 +17,7 @@ double start;
1717

1818
pthread_t thread[MAX_WORKERS] = {};
1919

20-
std::atomic<int> running = 0;
20+
_Atomic int running = 0;
2121

2222
#ifndef TOTAL
2323
#define TOTAL 100000000
@@ -35,16 +35,16 @@ void *ThreadMain(void *arg) {
3535
}
3636
for (int i = 0; i < (TOTAL / WORKERS); i++) {
3737
int rem = i & (AT_ONCE - 1);
38-
void*& allocation = allocations[rem];
39-
if (allocation) {
40-
free(allocation);
38+
void** allocation = &allocations[rem];
39+
if (*allocation) {
40+
free(*allocation);
4141
}
42-
allocation = malloc(BASE_SIZE + rem);
43-
if (!allocation) {
42+
*allocation = malloc(BASE_SIZE + rem);
43+
if (!*allocation) {
4444
puts("failed to allocate");
4545
abort();
4646
}
47-
char* data = (char*)allocation;
47+
char* data = (char*)*allocation;
4848
*data = i;
4949
}
5050
int total = 0;
@@ -60,7 +60,7 @@ void *ThreadMain(void *arg) {
6060
}
6161
free(allocations);
6262
printf("thread exiting with total %d\n", total);
63-
if (running.fetch_sub(1) == 1) {
63+
if (atomic_fetch_sub(&running, 1) == 1) {
6464
#if VERBOSE
6565
double end = emscripten_date_now();
6666
printf("total time %.2f\n", end - start);

test/test_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ def output_parser(output):
967967

968968
def test_malloc_multithreading(self):
969969
# Multithreaded malloc test. For emcc we use mimalloc here.
970-
src = read_file(test_file('other/test_malloc_multithreading.cpp'))
970+
src = read_file(test_file('other/test_malloc_multithreading.c'))
971971
# TODO measure with different numbers of cores and not fixed 4
972972
self.do_benchmark('malloc_multithreading', src, 'Done.', shared_args=['-DWORKERS=4', '-pthread'], emcc_args=['-sEXIT_RUNTIME', '-sMALLOC=mimalloc', '-sMINIMAL_RUNTIME=0', '-sINITIAL_MEMORY=512MB'])
973973

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7931,7 +7931,7 @@ def test_malloc_multithreading(self, allocator, args):
79317931
'-sTOTAL_STACK=1mb',
79327932
f'-sMALLOC={allocator}',
79337933
]
7934-
self.do_other_test('test_malloc_multithreading.cpp', cflags=args)
7934+
self.do_other_test('test_malloc_multithreading.c', cflags=args)
79357935

79367936
@parameterized({
79377937
'': ([], 'testbind.js'),

0 commit comments

Comments
 (0)