Skip to content

Commit ce57936

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Mark thread-safe methods of DataLoader as const. (#4756)
Summary: Pull Request resolved: #4756 Every DataLoader must not modify its internal state after initialization so that it can be shared and used by multiple threads simultaneously. Reviewed By: dbort Differential Revision: D61406229
1 parent b75e7d7 commit ce57936

File tree

10 files changed

+45
-43
lines changed

10 files changed

+45
-43
lines changed

backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
return [NSData dataWithContentsOfURL:url];
2626
}
2727

28-
class DataLoaderImpl: public DataLoader {
28+
class DataLoaderImpl final : public DataLoader {
2929
public:
3030
DataLoaderImpl(std::string filePath)
3131
:data_(read_data(filePath))
3232
{}
3333

3434
Result<FreeableBuffer> load(
35-
size_t offset, size_t size, __ET_UNUSED const DataLoader::SegmentInfo& segment_info) override {
35+
size_t offset, size_t size, __ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
3636
NSData *subdata = [data_ subdataWithRange:NSMakeRange(offset, size)];
3737
return FreeableBuffer(subdata.bytes, size, nullptr);
3838
}
@@ -42,7 +42,7 @@
4242
}
4343

4444
private:
45-
NSData *data_;
45+
NSData * const data_;
4646
};
4747

4848
using Buffer = std::vector<uint8_t>;

examples/apple/coreml/executor_runner/main.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ Args parse_command_line_args(NSArray<NSString *> *args) {
154154
return data;
155155
}
156156

157-
class DataLoaderImpl: public DataLoader {
157+
class DataLoaderImpl final : public DataLoader {
158158
public:
159159
DataLoaderImpl(const std::string& filePath)
160160
:data_(read_data(filePath))
161161
{}
162162

163-
Result<FreeableBuffer> load(size_t offset, size_t size, __ET_UNUSED const DataLoader::SegmentInfo& segment_info) override {
163+
Result<FreeableBuffer> load(size_t offset, size_t size, __ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
164164
NSData *subdata = [data_ subdataWithRange:NSMakeRange(offset, size)];
165165
return FreeableBuffer(subdata.bytes, size, nullptr);
166166
}
@@ -170,7 +170,7 @@ Args parse_command_line_args(NSArray<NSString *> *args) {
170170
}
171171

172172
private:
173-
NSData *data_;
173+
NSData * const data_;
174174
};
175175

176176
using Buffer = std::vector<uint8_t>;

extension/data_loader/buffer_data_loader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ namespace util {
2525
* This can be used to wrap data that is directly embedded into the firmware
2626
* image, or to wrap data that was allocated elsewhere.
2727
*/
28-
class BufferDataLoader : public DataLoader {
28+
class BufferDataLoader final : public DataLoader {
2929
public:
3030
BufferDataLoader(const void* data, size_t size)
3131
: data_(reinterpret_cast<const uint8_t*>(data)), size_(size) {}
3232

3333
__ET_NODISCARD Result<FreeableBuffer> load(
3434
size_t offset,
3535
size_t size,
36-
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) override {
36+
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
3737
ET_CHECK_OR_RETURN_ERROR(
3838
offset + size <= size_,
3939
InvalidArgument,
@@ -52,7 +52,7 @@ class BufferDataLoader : public DataLoader {
5252
size_t offset,
5353
size_t size,
5454
__ET_UNUSED const SegmentInfo& segment_info,
55-
void* buffer) override {
55+
void* buffer) const override {
5656
ET_CHECK_OR_RETURN_ERROR(
5757
buffer != nullptr,
5858
InvalidArgument,

extension/data_loader/file_data_loader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void FreeSegment(void* context, void* data, __ET_UNUSED size_t size) {
122122
Result<FreeableBuffer> FileDataLoader::load(
123123
size_t offset,
124124
size_t size,
125-
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) {
125+
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) const {
126126
ET_CHECK_OR_RETURN_ERROR(
127127
// Probably had its value moved to another instance.
128128
fd_ >= 0,
@@ -209,7 +209,7 @@ __ET_NODISCARD Error FileDataLoader::load_into(
209209
size_t offset,
210210
size_t size,
211211
__ET_UNUSED const SegmentInfo& segment_info,
212-
void* buffer) {
212+
void* buffer) const {
213213
ET_CHECK_OR_RETURN_ERROR(
214214
// Probably had its value moved to another instance.
215215
fd_ >= 0,

extension/data_loader/file_data_loader.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace util {
2525
* Note that this will keep the file open for the duration of its lifetime, to
2626
* avoid the overhead of opening it again for every load() call.
2727
*/
28-
class FileDataLoader : public DataLoader {
28+
class FileDataLoader final : public DataLoader {
2929
public:
3030
/**
3131
* Creates a new FileDataLoader that wraps the named file.
@@ -57,26 +57,26 @@ class FileDataLoader : public DataLoader {
5757
file_size_(rhs.file_size_),
5858
alignment_(rhs.alignment_),
5959
fd_(rhs.fd_) {
60-
rhs.file_name_ = nullptr;
61-
rhs.file_size_ = 0;
62-
rhs.alignment_ = 0;
63-
rhs.fd_ = -1;
60+
const_cast<const char*&>(rhs.file_name_) = nullptr;
61+
const_cast<size_t&>(rhs.file_size_) = 0;
62+
const_cast<size_t&>(rhs.alignment_) = 0;
63+
const_cast<int&>(rhs.fd_) = -1;
6464
}
6565

6666
~FileDataLoader() override;
6767

6868
__ET_NODISCARD Result<FreeableBuffer> load(
6969
size_t offset,
7070
size_t size,
71-
const DataLoader::SegmentInfo& segment_info) override;
71+
const DataLoader::SegmentInfo& segment_info) const override;
7272

7373
__ET_NODISCARD Result<size_t> size() const override;
7474

7575
__ET_NODISCARD Error load_into(
7676
size_t offset,
7777
size_t size,
7878
__ET_UNUSED const SegmentInfo& segment_info,
79-
void* buffer) override;
79+
void* buffer) const override;
8080

8181
private:
8282
FileDataLoader(
@@ -94,10 +94,10 @@ class FileDataLoader : public DataLoader {
9494
FileDataLoader& operator=(const FileDataLoader&) = delete;
9595
FileDataLoader& operator=(FileDataLoader&&) = delete;
9696

97-
const char* file_name_; // Owned by the instance.
98-
size_t file_size_;
99-
size_t alignment_;
100-
int fd_; // Owned by the instance.
97+
const char* const file_name_; // Owned by the instance.
98+
const size_t file_size_;
99+
const size_t alignment_;
100+
const int fd_; // Owned by the instance.
101101
};
102102

103103
} // namespace util

extension/data_loader/mmap_data_loader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void MunmapSegment(void* context, void* data, size_t size) {
149149
Result<FreeableBuffer> MmapDataLoader::load(
150150
size_t offset,
151151
size_t size,
152-
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) {
152+
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) const {
153153
ET_CHECK_OR_RETURN_ERROR(
154154
// Probably had its value moved to another instance.
155155
fd_ >= 0,

extension/data_loader/mmap_data_loader.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace util {
2323
* Note that this will keep the file open for the duration of its lifetime, to
2424
* avoid the overhead of opening it again for every load() call.
2525
*/
26-
class MmapDataLoader : public DataLoader {
26+
class MmapDataLoader final : public DataLoader {
2727
public:
2828
/**
2929
* Describes how and whether to lock loaded pages with `mlock()`.
@@ -77,19 +77,19 @@ class MmapDataLoader : public DataLoader {
7777
page_size_(rhs.page_size_),
7878
fd_(rhs.fd_),
7979
mlock_config_(rhs.mlock_config_) {
80-
rhs.file_name_ = nullptr;
81-
rhs.file_size_ = 0;
82-
rhs.page_size_ = 0;
83-
rhs.fd_ = -1;
84-
rhs.mlock_config_ = MlockConfig::NoMlock;
80+
const_cast<const char*&>(rhs.file_name_) = nullptr;
81+
const_cast<size_t&>(rhs.file_size_) = 0;
82+
const_cast<size_t&>(rhs.page_size_) = 0;
83+
const_cast<int&>(rhs.fd_) = -1;
84+
const_cast<MlockConfig&>(rhs.mlock_config_) = MlockConfig::NoMlock;
8585
}
8686

8787
~MmapDataLoader() override;
8888

8989
__ET_NODISCARD Result<FreeableBuffer> load(
9090
size_t offset,
9191
size_t size,
92-
const DataLoader::SegmentInfo& segment_info) override;
92+
const DataLoader::SegmentInfo& segment_info) const override;
9393

9494
__ET_NODISCARD Result<size_t> size() const override;
9595

@@ -111,11 +111,11 @@ class MmapDataLoader : public DataLoader {
111111
MmapDataLoader& operator=(const MmapDataLoader&) = delete;
112112
MmapDataLoader& operator=(MmapDataLoader&&) = delete;
113113

114-
const char* file_name_; // String data is owned by the instance.
115-
size_t file_size_;
116-
size_t page_size_;
117-
int fd_; // Owned by the instance.
118-
MlockConfig mlock_config_;
114+
const char* const file_name_; // String data is owned by the instance.
115+
const size_t file_size_;
116+
const size_t page_size_;
117+
const int fd_; // Owned by the instance.
118+
const MlockConfig mlock_config_;
119119
};
120120

121121
} // namespace util

extension/data_loader/shared_ptr_data_loader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ namespace util {
2424
*
2525
* This can be used to wrap data that was allocated elsewhere.
2626
*/
27-
class SharedPtrDataLoader : public DataLoader {
27+
class SharedPtrDataLoader final : public DataLoader {
2828
public:
2929
SharedPtrDataLoader(std::shared_ptr<void> data, size_t size)
3030
: data_(data), size_(size) {}
3131

3232
__ET_NODISCARD Result<FreeableBuffer> load(
3333
size_t offset,
3434
size_t size,
35-
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) override {
35+
__ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
3636
ET_CHECK_OR_RETURN_ERROR(
3737
offset + size <= size_,
3838
InvalidArgument,

runtime/core/data_loader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class DataLoader {
8888
* @returns a `FreeableBuffer` that owns the loaded data.
8989
*/
9090
__ET_NODISCARD virtual Result<FreeableBuffer>
91-
load(size_t offset, size_t size, const SegmentInfo& segment_info) = 0;
91+
load(size_t offset, size_t size, const SegmentInfo& segment_info) const = 0;
9292

9393
/**
9494
* Loads data from the underlying data source into the provided buffer.
@@ -108,7 +108,7 @@ class DataLoader {
108108
size_t offset,
109109
size_t size,
110110
const SegmentInfo& segment_info,
111-
void* buffer) {
111+
void* buffer) const {
112112
// Using a stub implementation here instead of pure virtual to expand the
113113
// data_loader interface in a backwards compatible way.
114114
(void)buffer;

runtime/executor/test/backend_integration_test.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ StubBackend StubBackend::singleton_;
164164
* A DataLoader that wraps a real DataLoader and records the operations
165165
* performed on it and the FreeableBuffers it loads.
166166
*/
167-
class DataLoaderSpy : public DataLoader {
167+
class DataLoaderSpy final : public DataLoader {
168168
public:
169169
/// A record of an operation performed on this DataLoader.
170170
struct Operation {
@@ -178,8 +178,10 @@ class DataLoaderSpy : public DataLoader {
178178

179179
explicit DataLoaderSpy(DataLoader* delegate) : delegate_(delegate) {}
180180

181-
Result<FreeableBuffer>
182-
load(size_t offset, size_t size, const SegmentInfo& segment_info) override {
181+
Result<FreeableBuffer> load(
182+
size_t offset,
183+
size_t size,
184+
const SegmentInfo& segment_info) const override {
183185
Result<FreeableBuffer> buf = delegate_->load(offset, size, segment_info);
184186
if (!buf.ok()) {
185187
return buf.error();
@@ -268,7 +270,7 @@ class DataLoaderSpy : public DataLoader {
268270
/// The real loader to delegate to.
269271
DataLoader* delegate_;
270272

271-
std::vector<Operation> operations_;
273+
mutable std::vector<Operation> operations_;
272274
};
273275

274276
constexpr size_t kDefaultNonConstMemBytes = 32 * 1024;

0 commit comments

Comments
 (0)