fix events under tsan
The test had an actual bug (go figure...), and it turns out events need
the same fix as mutexes to help tsan figure out what's going on
Change-Id: I711850b635616b0df4df7be788ddebb71c88fc5e
diff --git a/aos/common/event_test.cc b/aos/common/event_test.cc
index fff7522..d8671eb 100644
--- a/aos/common/event_test.cc
+++ b/aos/common/event_test.cc
@@ -34,8 +34,9 @@
// Tests that tsan understands that events establish a happens-before
// relationship.
TEST_F(EventTest, ThreadSanitizer) {
- for (int i = 0; i < 1000; ++i) {
+ for (int i = 0; i < 3000; ++i) {
int variable = 0;
+ test_event.Clear();
::std::thread thread([this, &variable]() {
test_event.Wait();
--variable;
@@ -50,12 +51,16 @@
// Tests that an event blocks correctly.
TEST_F(EventTest, Blocks) {
time::Time start_time, finish_time;
- ::std::thread thread([this, &start_time, &finish_time]() {
+ // Without this, it sometimes manages to fail under tsan.
+ Event started;
+ ::std::thread thread([this, &start_time, &finish_time, &started]() {
start_time = time::Time::Now();
+ started.Set();
test_event.Wait();
finish_time = time::Time::Now();
});
static const time::Time kWaitTime = time::Time::InSeconds(0.05);
+ started.Wait();
time::SleepFor(kWaitTime);
test_event.Set();
thread.join();
diff --git a/aos/linux_code/ipc_lib/aos_sync.cc b/aos/linux_code/ipc_lib/aos_sync.cc
index ffd1061..311d88d 100644
--- a/aos/linux_code/ipc_lib/aos_sync.cc
+++ b/aos/linux_code/ipc_lib/aos_sync.cc
@@ -411,6 +411,10 @@
return -1;
}
}
+#ifdef AOS_SANITIZER_thread
+ // Help tsan figure out that we're synchronizing on this.
+ __sync_add_and_fetch(m, 0);
+#endif
return 0;
}