diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt --- a/libc/src/__support/File/CMakeLists.txt +++ b/libc/src/__support/File/CMakeLists.txt @@ -5,6 +5,7 @@ HDRS file.h DEPENDS + libc.src.__support.threads.thread libc.include.errno libc.src.errno.errno ) diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h --- a/libc/src/__support/File/file.h +++ b/libc/src/__support/File/file.h @@ -9,6 +9,8 @@ #ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H #define LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H +#include "src/__support/threads/mutex.h" + #include #include @@ -64,10 +66,7 @@ CloseFunc *platform_close; FlushFunc *platform_flush; - // Platform specific functions to lock and unlock file for mutually exclusive - // access from threads in a multi-threaded application. - LockFunc *platform_lock; - UnlockFunc *platform_unlock; + Mutex mutex; void *buf; // Pointer to the stream buffer for buffered streams size_t bufsize; // Size of the buffer pointed to by |buf|. @@ -110,28 +109,26 @@ // like stdout do not require invocation of the constructor which can // potentially lead to static initialization order fiasco. constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, CloseFunc *cf, - FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf, void *buffer, - size_t buffer_size, int buffer_mode, bool owned, - ModeFlags modeflags) + FlushFunc *ff, void *buffer, size_t buffer_size, + int buffer_mode, bool owned, ModeFlags modeflags) : platform_write(wf), platform_read(rf), platform_seek(sf), - platform_close(cf), platform_flush(ff), platform_lock(lf), - platform_unlock(ulf), buf(buffer), bufsize(buffer_size), - bufmode(buffer_mode), own_buf(owned), mode(modeflags), pos(0), - prev_op(FileOp::NONE), read_limit(0), eof(false), err(false) {} + platform_close(cf), platform_flush(ff), mutex(false, false, false), + buf(buffer), bufsize(buffer_size), bufmode(buffer_mode), own_buf(owned), + mode(modeflags), pos(0), prev_op(FileOp::NONE), read_limit(0), + eof(false), err(false) {} // This function helps initialize the various fields of the File data // structure after a allocating memory for it via a call to malloc. static void init(File *f, WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, - CloseFunc *cf, FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf, - void *buffer, size_t buffer_size, int buffer_mode, - bool owned, ModeFlags modeflags) { + CloseFunc *cf, FlushFunc *ff, void *buffer, + size_t buffer_size, int buffer_mode, bool owned, + ModeFlags modeflags) { + Mutex::init(&f->mutex, false, false, false); f->platform_write = wf; f->platform_read = rf; f->platform_seek = sf; f->platform_close = cf; f->platform_flush = ff; - f->platform_lock = lf; - f->platform_unlock = ulf; f->buf = reinterpret_cast(buffer); f->bufsize = buffer_size; f->bufmode = buffer_mode; @@ -163,8 +160,8 @@ // Closes the file stream and frees up all resources owned by it. int close(); - void lock() { platform_lock(this); } - void unlock() { platform_unlock(this); } + void lock() { mutex.lock(); } + void unlock() { mutex.unlock(); } bool error() const { return err; } void clearerr() { err = false; } diff --git a/libc/test/src/__support/File/file_test.cpp b/libc/test/src/__support/File/file_test.cpp --- a/libc/test/src/__support/File/file_test.cpp +++ b/libc/test/src/__support/File/file_test.cpp @@ -30,16 +30,12 @@ static int str_close(__llvm_libc::File *f) { return 0; } static int str_flush(__llvm_libc::File *f) { return 0; } - // TODO: Add a proper locking system and tests which exercise that. - static void str_lock(__llvm_libc::File *f) {} - static void str_unlock(__llvm_libc::File *f) {} - public: explicit StringFile(char *buffer, size_t buflen, int bufmode, bool owned, ModeFlags modeflags) : __llvm_libc::File(&str_write, &str_read, &str_seek, &str_close, - &str_flush, &str_lock, &str_unlock, buffer, buflen, - bufmode, owned, modeflags), + &str_flush, buffer, buflen, bufmode, owned, + modeflags), pos(0), eof_marker(0), write_append(false) { if (modeflags & static_cast(__llvm_libc::File::OpenMode::APPEND)) write_append = true; @@ -48,8 +44,7 @@ void init(char *buffer, size_t buflen, int bufmode, bool owned, ModeFlags modeflags) { File::init(this, &str_write, &str_read, &str_seek, &str_close, &str_flush, - &str_lock, &str_unlock, buffer, buflen, bufmode, owned, - modeflags); + buffer, buflen, bufmode, owned, modeflags); pos = eof_marker = 0; if (modeflags & static_cast(__llvm_libc::File::OpenMode::APPEND)) write_append = true;