diff --git a/cia.c b/cia.c index adbd990..b640837 100644 --- a/cia.c +++ b/cia.c @@ -30,6 +30,7 @@ #include "src/stdlib-thread/thread.c" // Module stdlib_file +#include "src/stdlib-file/common.c" #include "src/stdlib-file/file.c" #include "src/stdlib-file/fmt.c" diff --git a/src/stdlib-file/common.c b/src/stdlib-file/common.c new file mode 100644 index 0000000..e6dfd39 --- /dev/null +++ b/src/stdlib-file/common.c @@ -0,0 +1,8 @@ + +FILE *stdin; +FILE *stdout; +FILE *stderr; + +static Cia_Pool _page_allocator; +static Cia_Pool _file_pool; +static Cia_Mutex _g_pool_mutex; diff --git a/src/stdlib-file/file.c b/src/stdlib-file/file.c index 533aa30..1c68464 100644 --- a/src/stdlib-file/file.c +++ b/src/stdlib-file/file.c @@ -1,12 +1,4 @@ -FILE *stdin; -FILE *stdout; -FILE *stderr; - -static Cia_Pool _page_allocator; -static Cia_Pool _file_pool; -static Cia_Mutex _g_pool_mutex; - static void _fileapi_init() { cia_pool_create(&_file_pool, cia_allocator_pages(), 0x1000, sizeof(FILE), 16); stdin = cia_pool_alloc(&_file_pool); diff --git a/src/stdlib-file/fmt.c b/src/stdlib-file/fmt.c index 28cb017..276cf61 100644 --- a/src/stdlib-file/fmt.c +++ b/src/stdlib-file/fmt.c @@ -82,6 +82,7 @@ int printf(const char *restrict fmt, ...) { i64 buf_start_idx = 0; i64 buf_end_idx = 0; u64 unused; + cia_mutex_lock(&stdout->mutex); for(i64 i = 0; fmt[i] != 0;) { buf_start_idx = i; buf_end_idx = i; @@ -98,7 +99,7 @@ int printf(const char *restrict fmt, ...) { , &unused ); if(status != _RT_STATUS_OK) { - return -1; + goto _error; } ret_printed += buf_size; } @@ -129,16 +130,21 @@ int printf(const char *restrict fmt, ...) { int ch = va_arg(args, int); _RT_Status status = _rt_file_write(&stdout->rt_file, 1, &ch, &unused); if(status != _RT_STATUS_OK) { - return -1; + goto _error; } arg_chars = 1; } i += 1; if(arg_chars < 0) { - return -1; + goto _error; } ret_printed += arg_chars; } + cia_mutex_unlock(&stdout->mutex); va_end(args); return ret_printed; +_error: + cia_mutex_unlock(&stdout->mutex); + va_end(args); + return -1; }