make fwrite threadsafe

This commit is contained in:
flysand7 2023-09-10 11:30:56 +11:00
parent afa8017b5b
commit 8c563ec1b2
2 changed files with 19 additions and 1 deletions

View File

@ -2,11 +2,13 @@
#pragma once
#include <cia/def.h>
#include <cia/sync.h>
#include <tinyrt.h>
typedef struct FILE FILE;
struct FILE {
_RT_File rt_file;
Cia_Mutex mutex;
};
#define EOF (-1)

View File

@ -5,6 +5,7 @@ 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);
@ -15,6 +16,9 @@ static void _fileapi_init() {
stdin->rt_file = _rt_file_stdin;
stdout->rt_file = _rt_file_stdout;
stderr->rt_file = _rt_file_stderr;
cia_mutex_init(&stdin->mutex);
cia_mutex_init(&stdout->mutex);
cia_mutex_init(&stderr->mutex);
}
FILE *fopen(char const *restrict filename, char const *restrict mode) {
@ -30,7 +34,9 @@ FILE *fopen(char const *restrict filename, char const *restrict mode) {
if(status != _RT_STATUS_OK) {
return NULL;
}
FILE *file = cia_pool_alloc(&_file_pool);
cia_mutex_lock(&_g_pool_mutex);
FILE *file = cia_pool_alloc(&_file_pool); // TODO: not thread safe
cia_mutex_unlock(&_g_pool_mutex);
file->rt_file = rt_file;
return file;
}
@ -38,7 +44,9 @@ FILE *fopen(char const *restrict filename, char const *restrict mode) {
int fgetc(FILE *file) {
int c = 0;
u64 bytes_read;
cia_mutex_lock(&file->mutex);
_RT_Status status = _rt_file_read(1, &c, &file->rt_file, &bytes_read);
cia_mutex_unlock(&file->mutex);
if(status == _RT_STATUS_FILE_EOF) {
return EOF;
}
@ -50,7 +58,9 @@ int fgetc(FILE *file) {
int fputc(int c, FILE *file) {
u64 bytes_written;
cia_mutex_lock(&file->mutex);
_RT_Status status = _rt_file_write(&file->rt_file, 1, &c, &bytes_written);
cia_mutex_unlock(&file->mutex);
if(status != _RT_STATUS_OK) {
return EOF;
}
@ -59,7 +69,9 @@ int fputc(int c, FILE *file) {
size_t fread(void *restrict buf, size_t size, size_t count, FILE *restrict file) {
u64 bytes_read;
cia_mutex_lock(&file->mutex);
_RT_Status status = _rt_file_read(size*count, buf, &file->rt_file, &bytes_read);
cia_mutex_unlock(&file->mutex);
if(status != _RT_STATUS_OK) {
return 0;
}
@ -68,7 +80,9 @@ size_t fread(void *restrict buf, size_t size, size_t count, FILE *restrict file)
size_t fwrite(void const *restrict buf, size_t size, size_t count, FILE *restrict file) {
u64 bytes_written;
cia_mutex_lock(&file->mutex);
_RT_Status status = _rt_file_write(&file->rt_file, size*count, (void *)buf, &bytes_written);
cia_mutex_unlock(&file->mutex);
if(status != _RT_STATUS_OK) {
return 0;
}
@ -80,6 +94,8 @@ int fclose(FILE *file) {
if(status != _RT_STATUS_OK) {
return EOF;
}
cia_mutex_lock(&_g_pool_mutex);
cia_pool_free(&_file_pool, file);
cia_mutex_unlock(&_g_pool_mutex);
return 0;
}