[libc][NFC] Keep the mutex with the base File data structure.

This is now possible because we have a platform independent abstraction
for mutexes.

Reviewed By: lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D121773
This commit is contained in:
Siva Chandra Reddy 2022-03-16 06:56:34 +00:00
parent 3a37d08b35
commit 9527a2f58f
3 changed files with 19 additions and 26 deletions

View File

@ -5,6 +5,7 @@ add_object_library(
HDRS HDRS
file.h file.h
DEPENDS DEPENDS
libc.src.__support.threads.thread
libc.include.errno libc.include.errno
libc.src.errno.errno libc.src.errno.errno
) )

View File

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H #ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H #define LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
#include "src/__support/threads/mutex.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -64,10 +66,7 @@ private:
CloseFunc *platform_close; CloseFunc *platform_close;
FlushFunc *platform_flush; FlushFunc *platform_flush;
// Platform specific functions to lock and unlock file for mutually exclusive Mutex mutex;
// access from threads in a multi-threaded application.
LockFunc *platform_lock;
UnlockFunc *platform_unlock;
void *buf; // Pointer to the stream buffer for buffered streams void *buf; // Pointer to the stream buffer for buffered streams
size_t bufsize; // Size of the buffer pointed to by |buf|. size_t bufsize; // Size of the buffer pointed to by |buf|.
@ -110,28 +109,26 @@ public:
// like stdout do not require invocation of the constructor which can // like stdout do not require invocation of the constructor which can
// potentially lead to static initialization order fiasco. // potentially lead to static initialization order fiasco.
constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, CloseFunc *cf, constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, CloseFunc *cf,
FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf, void *buffer, FlushFunc *ff, void *buffer, size_t buffer_size,
size_t buffer_size, int buffer_mode, bool owned, int buffer_mode, bool owned, ModeFlags modeflags)
ModeFlags modeflags)
: platform_write(wf), platform_read(rf), platform_seek(sf), : platform_write(wf), platform_read(rf), platform_seek(sf),
platform_close(cf), platform_flush(ff), platform_lock(lf), platform_close(cf), platform_flush(ff), mutex(false, false, false),
platform_unlock(ulf), buf(buffer), bufsize(buffer_size), buf(buffer), bufsize(buffer_size), bufmode(buffer_mode), own_buf(owned),
bufmode(buffer_mode), own_buf(owned), mode(modeflags), pos(0), mode(modeflags), pos(0), prev_op(FileOp::NONE), read_limit(0),
prev_op(FileOp::NONE), read_limit(0), eof(false), err(false) {} eof(false), err(false) {}
// This function helps initialize the various fields of the File data // This function helps initialize the various fields of the File data
// structure after a allocating memory for it via a call to malloc. // structure after a allocating memory for it via a call to malloc.
static void init(File *f, WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, static void init(File *f, WriteFunc *wf, ReadFunc *rf, SeekFunc *sf,
CloseFunc *cf, FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf, CloseFunc *cf, FlushFunc *ff, void *buffer,
void *buffer, size_t buffer_size, int buffer_mode, size_t buffer_size, int buffer_mode, bool owned,
bool owned, ModeFlags modeflags) { ModeFlags modeflags) {
Mutex::init(&f->mutex, false, false, false);
f->platform_write = wf; f->platform_write = wf;
f->platform_read = rf; f->platform_read = rf;
f->platform_seek = sf; f->platform_seek = sf;
f->platform_close = cf; f->platform_close = cf;
f->platform_flush = ff; f->platform_flush = ff;
f->platform_lock = lf;
f->platform_unlock = ulf;
f->buf = reinterpret_cast<uint8_t *>(buffer); f->buf = reinterpret_cast<uint8_t *>(buffer);
f->bufsize = buffer_size; f->bufsize = buffer_size;
f->bufmode = buffer_mode; f->bufmode = buffer_mode;
@ -163,8 +160,8 @@ public:
// Closes the file stream and frees up all resources owned by it. // Closes the file stream and frees up all resources owned by it.
int close(); int close();
void lock() { platform_lock(this); } void lock() { mutex.lock(); }
void unlock() { platform_unlock(this); } void unlock() { mutex.unlock(); }
bool error() const { return err; } bool error() const { return err; }
void clearerr() { err = false; } void clearerr() { err = false; }

View File

@ -30,16 +30,12 @@ class StringFile : public __llvm_libc::File {
static int str_close(__llvm_libc::File *f) { return 0; } static int str_close(__llvm_libc::File *f) { return 0; }
static int str_flush(__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: public:
explicit StringFile(char *buffer, size_t buflen, int bufmode, bool owned, explicit StringFile(char *buffer, size_t buflen, int bufmode, bool owned,
ModeFlags modeflags) ModeFlags modeflags)
: __llvm_libc::File(&str_write, &str_read, &str_seek, &str_close, : __llvm_libc::File(&str_write, &str_read, &str_seek, &str_close,
&str_flush, &str_lock, &str_unlock, buffer, buflen, &str_flush, buffer, buflen, bufmode, owned,
bufmode, owned, modeflags), modeflags),
pos(0), eof_marker(0), write_append(false) { pos(0), eof_marker(0), write_append(false) {
if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND)) if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND))
write_append = true; write_append = true;
@ -48,8 +44,7 @@ public:
void init(char *buffer, size_t buflen, int bufmode, bool owned, void init(char *buffer, size_t buflen, int bufmode, bool owned,
ModeFlags modeflags) { ModeFlags modeflags) {
File::init(this, &str_write, &str_read, &str_seek, &str_close, &str_flush, File::init(this, &str_write, &str_read, &str_seek, &str_close, &str_flush,
&str_lock, &str_unlock, buffer, buflen, bufmode, owned, buffer, buflen, bufmode, owned, modeflags);
modeflags);
pos = eof_marker = 0; pos = eof_marker = 0;
if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND)) if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND))
write_append = true; write_append = true;