Skip to content

Commit 561ecfb

Browse files
committed
fixed two tsan issues
1 parent 08ced26 commit 561ecfb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

CobaltFusion/Executor.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,15 @@ void Executor::Add(std::function<void()> fn)
191191

192192
void Executor::Synchronize()
193193
{
194-
Call([] {});
194+
assert(!IsExecutorThread() && "Calling Synchronize() inside Call() will cause a deadlock");
195+
SynchronizeInternally();
196+
}
197+
198+
void Executor::SynchronizeInternally()
199+
{
200+
std::promise<bool> sync;
201+
Add([&sync]() { sync.set_value(true); });
202+
sync.get_future().get();
195203
}
196204

197205
ScheduledCall TimedExecutor::CallAt(const TimePoint& at, std::function<void()> fn)

include/CobaltFusion/Executor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Executor
104104
template <typename Fn>
105105
auto Call(Fn fn)
106106
{
107-
assert(!IsExecutorThread());
107+
assert(!IsExecutorThread() && "Calling Call() inside Call() will cause a deadlock");
108108
std::packaged_task<decltype(fn())()> task(fn);
109109
Add([&task]() { task(); });
110110
return task.get_future().get();
@@ -126,6 +126,7 @@ class Executor
126126
void Synchronize();
127127

128128
protected:
129+
void SynchronizeInternally();
129130
void SetExecutorThread();
130131
void SetExecutorThread(std::thread::id id);
131132
void Add(std::function<void()> fn);
@@ -138,7 +139,7 @@ class Executor
138139

139140
private:
140141
SynchronizedQueue<std::function<void()>> m_q;
141-
std::thread::id m_threadId;
142+
std::atomic<std::thread::id> m_threadId;
142143
};
143144

144145
class TimedExecutor : private ExecutorBase,

0 commit comments

Comments
 (0)