|
9 | 9 | #ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
|
10 | 10 | #define LLVM_LIBC_SRC_SUPPORT_OSUTIL_FILE_H
|
11 | 11 |
|
| 12 | +#include "src/__support/threads/mutex.h" |
| 13 | + |
12 | 14 | #include <stddef.h>
|
13 | 15 | #include <stdint.h>
|
14 | 16 |
|
@@ -64,10 +66,7 @@ class File {
|
64 | 66 | CloseFunc *platform_close;
|
65 | 67 | FlushFunc *platform_flush;
|
66 | 68 |
|
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; |
71 | 70 |
|
72 | 71 | void *buf; // Pointer to the stream buffer for buffered streams
|
73 | 72 | size_t bufsize; // Size of the buffer pointed to by |buf|.
|
@@ -110,28 +109,26 @@ class File {
|
110 | 109 | // like stdout do not require invocation of the constructor which can
|
111 | 110 | // potentially lead to static initialization order fiasco.
|
112 | 111 | 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) |
116 | 114 | : 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) {} |
121 | 119 |
|
122 | 120 | // This function helps initialize the various fields of the File data
|
123 | 121 | // structure after a allocating memory for it via a call to malloc.
|
124 | 122 | 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); |
128 | 127 | f->platform_write = wf;
|
129 | 128 | f->platform_read = rf;
|
130 | 129 | f->platform_seek = sf;
|
131 | 130 | f->platform_close = cf;
|
132 | 131 | f->platform_flush = ff;
|
133 |
| - f->platform_lock = lf; |
134 |
| - f->platform_unlock = ulf; |
135 | 132 | f->buf = reinterpret_cast<uint8_t *>(buffer);
|
136 | 133 | f->bufsize = buffer_size;
|
137 | 134 | f->bufmode = buffer_mode;
|
@@ -163,8 +160,8 @@ class File {
|
163 | 160 | // Closes the file stream and frees up all resources owned by it.
|
164 | 161 | int close();
|
165 | 162 |
|
166 |
| - void lock() { platform_lock(this); } |
167 |
| - void unlock() { platform_unlock(this); } |
| 163 | + void lock() { mutex.lock(); } |
| 164 | + void unlock() { mutex.unlock(); } |
168 | 165 |
|
169 | 166 | bool error() const { return err; }
|
170 | 167 | void clearerr() { err = false; }
|
|
0 commit comments