Skip to content

Commit b1b1192

Browse files
committed
Fix multiple data race
1 parent 9650d13 commit b1b1192

8 files changed

Lines changed: 33 additions & 11 deletions

File tree

modules/FileSystem/File.mpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export namespace CppUtils::FileSystem
99
auto&& function,
1010
bool recursively = false) -> void
1111
{
12-
if (not std::filesystem::exists(directoryPath) or
13-
not std::filesystem::is_directory(directoryPath))
12+
if (not std::filesystem::exists(directoryPath) or not std::filesystem::is_directory(directoryPath))
1413
return;
1514
if (recursively)
1615
{

modules/Terminal/AreaBuffer.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export namespace CppUtils::Terminal
1111
public:
1212
[[nodiscard]] virtual auto getChar(const Container::Size2& position) const noexcept -> const CharAttributes& = 0;
1313
virtual auto setChar(const Container::Size2& position, const CharAttributes& c) noexcept -> void = 0;
14-
[[nodiscard]] virtual auto getSize() const noexcept -> const Container::Size2& = 0;
14+
[[nodiscard]] virtual auto getSize() const noexcept -> Container::Size2 = 0;
1515

1616
[[nodiscard]] inline virtual auto toString() const -> std::string
1717
{

modules/Terminal/Canvas.mpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export namespace CppUtils::Terminal
171171

172172
inline auto print() noexcept -> void
173173
{
174+
auto lock = std::scoped_lock{m_printMutex};
174175
if (m_widget)
175176
{
176177
auto view = WritableAreaView{*this, m_viewport};
@@ -197,6 +198,7 @@ export namespace CppUtils::Terminal
197198
}
198199

199200
private:
201+
std::mutex m_printMutex;
200202
bool m_firstPrint = true;
201203
std::atomic_bool m_stopEvent = false;
202204
DynamicAreaBuffer m_previousBuffer;

modules/Terminal/DynamicAreaBuffer.mpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@ export namespace CppUtils::Terminal
2828

2929
inline auto setSize(const Container::Size2& size) noexcept -> void
3030
{
31+
auto lock = std::unique_lock{m_mutex};
3132
m_size = size;
3233
m_buffer = Buffer{m_size.height(), Line{m_size.width()}};
3334
}
3435

35-
[[nodiscard]] inline auto getSize() const noexcept -> const Container::Size2& final
36+
[[nodiscard]] inline auto getSize() const noexcept -> Container::Size2 final
3637
{
38+
auto lock = std::unique_lock{m_mutex};
3739
return m_size;
3840
}
3941

4042
[[nodiscard]] auto getChar(const Container::Size2& position) const noexcept -> const CharAttributes& final
4143
{
44+
auto lock = std::unique_lock{m_mutex};
4245
return m_buffer[position.y()][position.x()];
4346
}
4447

4548
auto setChar(const Container::Size2& position, const CharAttributes& c) noexcept -> void final
4649
{
50+
auto lock = std::unique_lock{m_mutex};
4751
if (position.x() >= m_size.width() or position.y() >= m_size.height())
4852
return;
4953
m_buffer[position.y()][position.x()] = c;
@@ -55,6 +59,7 @@ export namespace CppUtils::Terminal
5559
}
5660

5761
private:
62+
mutable std::mutex m_mutex;
5863
Container::Size2 m_size;
5964
Buffer m_buffer;
6065
};

modules/Terminal/FixedAreaBuffer.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export namespace CppUtils::Terminal
1515
using Buffer = std::array<Line, Height>;
1616
static constexpr auto size = Container::Size2{Width, Height};
1717

18-
[[nodiscard]] inline constexpr auto getSize() const noexcept -> const Container::Size2& override
18+
[[nodiscard]] inline constexpr auto getSize() const noexcept -> Container::Size2 override
1919
{
2020
return size;
2121
}

modules/Thread/Scheduler.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export namespace CppUtils::Thread
169169
}
170170
}
171171

172-
if (tasksToRun.empty())
172+
if (std::empty(tasksToRun))
173173
return;
174174

175175
m_processingTasks += std::size(tasksToRun);

tests/Terminal/Canvas.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
7373
auto scheduler = CppUtils::Thread::Scheduler{};
7474
scheduler.schedule([&canvas]() mutable {
7575
canvas.close();
76-
}, 1s);
76+
}, 500ms);
7777

7878
for (auto i = 0.f; i <= 100.f; i += 1.f)
7979
{

tests/Thread/Scheduler.mpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ export namespace CppUtils::UnitTest::Thread::Scheduler
103103
suite.addTest("Order of execution", [&] {
104104
auto scheduler = CppUtils::Thread::Scheduler{};
105105
auto executionOrder = std::vector<std::size_t>{};
106-
auto record = [&](std::size_t value) { executionOrder.push_back(value); };
106+
auto mutex = std::mutex{};
107+
auto record = [&](std::size_t value) {
108+
auto lock = std::unique_lock{mutex};
109+
executionOrder.push_back(value);
110+
};
107111

108112
scheduler.schedule([&] { record(1); }, 100ms);
109113
scheduler.schedule([&] { record(2); }, 50ms);
@@ -121,8 +125,12 @@ export namespace CppUtils::UnitTest::Thread::Scheduler
121125
using TimePoint = CppUtils::Thread::Scheduler::TimePoint;
122126
auto scheduler = CppUtils::Thread::Scheduler{};
123127
auto times = std::vector<TimePoint>{};
128+
auto mutex = std::mutex{};
124129

125-
auto record = [&](TimePoint timePoint) { times.push_back(timePoint); };
130+
auto record = [&](TimePoint timePoint) {
131+
auto lock = std::unique_lock{mutex};
132+
times.push_back(timePoint);
133+
};
126134

127135
scheduler.schedule([&] { record(Clock::now()); }, 100ms);
128136
scheduler.schedule([&] { record(Clock::now()); }, 250ms);
@@ -141,8 +149,12 @@ export namespace CppUtils::UnitTest::Thread::Scheduler
141149
using TimePoint = CppUtils::Thread::Scheduler::TimePoint;
142150
auto scheduler = CppUtils::Thread::Scheduler{200ms};
143151
auto times = std::vector<TimePoint>{};
152+
auto mutex = std::mutex{};
144153

145-
auto record = [&](TimePoint timePoint) { times.push_back(timePoint); };
154+
auto record = [&](TimePoint timePoint) {
155+
auto lock = std::unique_lock{mutex};
156+
times.push_back(timePoint);
157+
};
146158

147159
scheduler.schedule([&] { record(Clock::now()); }, 100ms);
148160
scheduler.schedule([&] { record(Clock::now()); }, 250ms);
@@ -159,7 +171,11 @@ export namespace CppUtils::UnitTest::Thread::Scheduler
159171
suite.addTest("Schedule events in reverse order", [&] {
160172
auto scheduler = CppUtils::Thread::Scheduler{};
161173
auto executionOrder = std::vector<std::size_t>{};
162-
auto record = [&](std::size_t value) { executionOrder.push_back(value); };
174+
auto mutex = std::mutex{};
175+
auto record = [&](std::size_t value) {
176+
auto lock = std::unique_lock{mutex};
177+
executionOrder.push_back(value);
178+
};
163179

164180
scheduler.schedule([&] { record(2); }, 200ms);
165181
scheduler.schedule([&] { record(1); }, 100ms);

0 commit comments

Comments
 (0)