Skip to content

Commit 9527a2f

Browse files
author
Siva Chandra Reddy
committed
[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
1 parent 3a37d08 commit 9527a2f

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

libc/src/__support/File/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_object_library(
55
HDRS
66
file.h
77
DEPENDS
8+
libc.src.__support.threads.thread
89
libc.include.errno
910
libc.src.errno.errno
1011
)

libc/src/__support/File/file.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
1010
#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
1111

12+
#include "src/__support/threads/mutex.h"
13+
1214
#include <stddef.h>
1315
#include <stdint.h>
1416

@@ -64,10 +66,7 @@ class File {
6466
CloseFunc *platform_close;
6567
FlushFunc *platform_flush;
6668

67-
// Platform specific functions to lock and unlock file for mutually exclusive
68-
// access from threads in a multi-threaded application.
69-
LockFunc *platform_lock;
70-
UnlockFunc *platform_unlock;
69+
Mutex mutex;
7170

7271
void *buf; // Pointer to the stream buffer for buffered streams
7372
size_t bufsize; // Size of the buffer pointed to by |buf|.
@@ -110,28 +109,26 @@ class File {
110109
// like stdout do not require invocation of the constructor which can
111110
// potentially lead to static initialization order fiasco.
112111
constexpr File(WriteFunc *wf, ReadFunc *rf, SeekFunc *sf, CloseFunc *cf,
113-
FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf, void *buffer,
114-
size_t buffer_size, int buffer_mode, bool owned,
115-
ModeFlags modeflags)
112+
FlushFunc *ff, void *buffer, size_t buffer_size,
113+
int buffer_mode, bool owned, ModeFlags modeflags)
116114
: platform_write(wf), platform_read(rf), platform_seek(sf),
117-
platform_close(cf), platform_flush(ff), platform_lock(lf),
118-
platform_unlock(ulf), buf(buffer), bufsize(buffer_size),
119-
bufmode(buffer_mode), own_buf(owned), mode(modeflags), pos(0),
120-
prev_op(FileOp::NONE), read_limit(0), eof(false), err(false) {}
115+
platform_close(cf), platform_flush(ff), mutex(false, false, false),
116+
buf(buffer), bufsize(buffer_size), bufmode(buffer_mode), own_buf(owned),
117+
mode(modeflags), pos(0), prev_op(FileOp::NONE), read_limit(0),
118+
eof(false), err(false) {}
121119

122120
// This function helps initialize the various fields of the File data
123121
// structure after a allocating memory for it via a call to malloc.
124122
static void init(File *f, WriteFunc *wf, ReadFunc *rf, SeekFunc *sf,
125-
CloseFunc *cf, FlushFunc *ff, LockFunc *lf, UnlockFunc *ulf,
126-
void *buffer, size_t buffer_size, int buffer_mode,
127-
bool owned, ModeFlags modeflags) {
123+
CloseFunc *cf, FlushFunc *ff, void *buffer,
124+
size_t buffer_size, int buffer_mode, bool owned,
125+
ModeFlags modeflags) {
126+
Mutex::init(&f->mutex, false, false, false);
128127
f->platform_write = wf;
129128
f->platform_read = rf;
130129
f->platform_seek = sf;
131130
f->platform_close = cf;
132131
f->platform_flush = ff;
133-
f->platform_lock = lf;
134-
f->platform_unlock = ulf;
135132
f->buf = reinterpret_cast<uint8_t *>(buffer);
136133
f->bufsize = buffer_size;
137134
f->bufmode = buffer_mode;
@@ -163,8 +160,8 @@ class File {
163160
// Closes the file stream and frees up all resources owned by it.
164161
int close();
165162

166-
void lock() { platform_lock(this); }
167-
void unlock() { platform_unlock(this); }
163+
void lock() { mutex.lock(); }
164+
void unlock() { mutex.unlock(); }
168165

169166
bool error() const { return err; }
170167
void clearerr() { err = false; }

libc/test/src/__support/File/file_test.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,12 @@ class StringFile : public __llvm_libc::File {
3030
static int str_close(__llvm_libc::File *f) { return 0; }
3131
static int str_flush(__llvm_libc::File *f) { return 0; }
3232

33-
// TODO: Add a proper locking system and tests which exercise that.
34-
static void str_lock(__llvm_libc::File *f) {}
35-
static void str_unlock(__llvm_libc::File *f) {}
36-
3733
public:
3834
explicit StringFile(char *buffer, size_t buflen, int bufmode, bool owned,
3935
ModeFlags modeflags)
4036
: __llvm_libc::File(&str_write, &str_read, &str_seek, &str_close,
41-
&str_flush, &str_lock, &str_unlock, buffer, buflen,
42-
bufmode, owned, modeflags),
37+
&str_flush, buffer, buflen, bufmode, owned,
38+
modeflags),
4339
pos(0), eof_marker(0), write_append(false) {
4440
if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND))
4541
write_append = true;
@@ -48,8 +44,7 @@ class StringFile : public __llvm_libc::File {
4844
void init(char *buffer, size_t buflen, int bufmode, bool owned,
4945
ModeFlags modeflags) {
5046
File::init(this, &str_write, &str_read, &str_seek, &str_close, &str_flush,
51-
&str_lock, &str_unlock, buffer, buflen, bufmode, owned,
52-
modeflags);
47+
buffer, buflen, bufmode, owned, modeflags);
5348
pos = eof_marker = 0;
5449
if (modeflags & static_cast<ModeFlags>(__llvm_libc::File::OpenMode::APPEND))
5550
write_append = true;

0 commit comments

Comments
 (0)