Fix %I64d not loading correct int sizes

This commit is contained in:
flysand7 2023-09-12 21:07:02 +11:00
parent 5453813e39
commit 8c5c5adeca
4 changed files with 51 additions and 64 deletions

View File

@ -155,6 +155,8 @@ cc_defines_pop()
cc_flags_pop()
# Build the ciabatta
cc_flags_push()
cc_flags.append('-Wno-format'); # until i find way to disable clang warning on %I64d
print_step("Building lib/cia.a\n")
assemble_obj('bin/thread-entry.o', [f'arch/{target_abi}_{target_arch}/thread-entry.asm'], ['-f "elf64"'])
compile_obj('bin/cia.o', ['cia.c'])
@ -163,3 +165,5 @@ archive('lib/cia.a', ['bin/cia.o', 'bin/thread-entry.o'])
# Build the test
if test_file != None:
compile_exe('a', [test_file, 'lib/cia.a'], ['-Wl,-dynamic-linker,lib/ld-cia.so'])
cc_flags_pop()

View File

@ -108,14 +108,43 @@ int printf(const char *restrict fmt, ...) {
}
// fmt[i] == '%'
i += 1;
// Check int sizes
int int_arg_size = 32;
if(fmt[i] == 'I') {
if(fmt[i+1] == '3' && fmt[i+2] == '2') {
int_arg_size = 32;
}
else if(fmt[i+1] == '6' && fmt[i+2] == '4') {
int_arg_size = 64;
}
else {
goto _error;
}
i += 3;
}
int arg_chars;
if(fmt[i] == 'd') {
int arg = va_arg(args, int);
arg_chars = print_int((i64)arg);
i64 arg;
if(int_arg_size == 32) {
arg = (i64)va_arg(args, i32);
}
else if(int_arg_size == 64) {
arg = va_arg(args, i64);
}
else {
goto _error;
}
arg_chars = print_int(arg);
}
else if(fmt[i] == 'u') {
unsigned int arg = va_arg(args, unsigned int);
u32 arg;
if(int_arg_size == 32) {
arg = va_arg(args, u32);
}
else if(int_arg_size == 64) {
arg = va_arg(args, u64);
}
arg_chars = print_uint((u64)arg);
}
else if(fmt[i] == 'p') {

View File

@ -4,8 +4,8 @@
#include <stdio.h>
#if 1
enum memory_order store_memory_order = memory_order_acquire;
enum memory_order load_memory_order = memory_order_release;
enum memory_order store_memory_order = memory_order_release;
enum memory_order load_memory_order = memory_order_acquire;
#else
enum memory_order store_memory_order = memory_order_seq_cst;
enum memory_order load_memory_order = memory_order_seq_cst;
@ -35,8 +35,7 @@ static Shared_State g_shared_state = {0};
int tx(void *ctx) {
arbitrary_delay(delay_that_works);
Shared_State *ss = ctx;
char msg[] = "Thread tx running\n";
fwrite(msg, 1, sizeof msg, stdout);
printf("Thread %s running\n", "tx");
atomic_store_explicit(&ss->x, 1, store_memory_order);
return 0;
}
@ -45,8 +44,7 @@ int tx(void *ctx) {
int ty(void *ctx) {
arbitrary_delay(delay_that_works);
Shared_State *ss = ctx;
char msg[] = "Thread ty running\n";
fwrite(msg, 1, sizeof msg, stdout);
printf("Thread %s running\n", "ty");
atomic_store_explicit(&ss->y, 1, store_memory_order);
return 0;
}
@ -55,8 +53,7 @@ int ty(void *ctx) {
int t1(void *ctx) {
arbitrary_delay(delay_that_works);
Shared_State *ss = ctx;
char msg[] = "Thread t1 running\n";
fwrite(msg, 1, sizeof msg, stdout);
printf("Thread %s running\n", "t1");
while(atomic_load_explicit(&ss->x, load_memory_order) == 0)
;
if(atomic_load_explicit(&ss->y, load_memory_order) == 1) {
@ -69,8 +66,7 @@ int t1(void *ctx) {
int t2(void *ctx) {
arbitrary_delay(delay_that_works);
Shared_State *ss = ctx;
char msg[] = "Thread t2 running\n";
fwrite(msg, 1, sizeof msg, stdout);
printf("Thread %s running\n", "t2");
while(atomic_load_explicit(&ss->y, load_memory_order) == 0)
;
if(atomic_load_explicit(&ss->x, load_memory_order) == 1) {
@ -88,8 +84,8 @@ int main() {
for(int i = 0; i < 4; ++i) {
int status = thrd_create(&threads[i], thread_funcs[i], &g_shared_state);
if(status != thrd_success) {
char msg[] = "Thread unable to start\n";
fwrite(msg, 1, sizeof msg, stdout);
printf("Thread unable to start\n");
return 1;
}
}
// Wait for threads to complete
@ -99,9 +95,6 @@ int main() {
}
// Check to see the cnt variable
int result = atomic_load_explicit(&g_shared_state.cnt, memory_order_relaxed);
if(result == 0) {
char msg[] = "This shouldn't happen in sequentially-consistent model!\n";
fwrite(msg, 1, sizeof msg, stdout);
}
printf("result: %d\n", result);
return 0;
}

View File

@ -5,65 +5,30 @@
#include <stdatomic.h>
#include <cia/sync.h>
static void print_string_n(char *str, u64 len) {
fwrite(str, 1, len, stdout);
}
static void print_char(char c) {
print_string_n(&c, 1);
}
static void print_string(char *str) {
int str_len = 0;
while(str[str_len] != 0) {
str_len += 1;
}
print_string_n(str, str_len);
}
static void print_int(i64 number) {
if(number < 0) {
print_char('-');
number = -number;
}
char buf[20];
buf[19] = 0;
char *p = buf + sizeof buf - 1;
do {
*--p = (number%10) + '0';
number /= 10;
} while(number > 0);
print_string(p);
}
static Cia_Mutex g_print_mutex;
static Cia_Mutex g_mutex;
static volatile i64 counter = 0;
int thrd_func(void *arg) {
print_string("child thread: ok!\n");
printf("child thread: ok!\n");
for(int i = 0; i < 100000; ++i) {
cia_mutex_lock(&g_mutex);
counter += 1;
cia_mutex_unlock(&g_mutex);
}
cia_mutex_lock(&g_print_mutex);
print_string("child thread: counter = ");
print_int(counter);
print_char('\n');
cia_mutex_unlock(&g_print_mutex);
printf("child thread: counter = %I64d\n", counter);
return 0;
}
int main() {
print_string("main thread: before\n");
printf("main thread: before\n");
cia_mutex_init(&g_mutex);
cia_mutex_init(&g_print_mutex);
thrd_t thrd;
int status = thrd_create(&thrd, thrd_func, NULL);
if(status == thrd_error) {
print_string("main thread: error creating child thread\n");
printf("main thread: error creating child thread\n");
return 1;
}
print_string("main thread: after!\n");
printf("main thread: after!\n");
for(int i = 0; i < 100000; ++i) {
cia_mutex_lock(&g_mutex);
counter += 1;
@ -71,10 +36,6 @@ int main() {
}
int exit_code;
thrd_join(thrd, &exit_code);
cia_mutex_lock(&g_print_mutex);
print_string("main thread: counter = ");
print_int(counter);
print_char('\n');
cia_mutex_unlock(&g_print_mutex);
printf("main thread: counter = %I64d\n", counter);
return 0;
}