From da871d063e4c24d64317b5a2df69a59e3b60c9b7 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Tue, 4 Oct 2016 16:03:07 +0200
Subject: Fix data-race in DummyEventLoop::processEvents()

Previously DummyEventLoop::processEvents() wrote to the
hasEvents_ member without having locked the corresponding
mutex. Changed code to use C++11 atomics instead.

Test-Information:

All unit tests pass on macOS 10.12.

Change-Id: I13aa1566d55b7c0098f927e9c8061547f7056f5d

diff --git a/Swiften/EventLoop/DummyEventLoop.cpp b/Swiften/EventLoop/DummyEventLoop.cpp
index d47124b..4dfbac3 100644
--- a/Swiften/EventLoop/DummyEventLoop.cpp
+++ b/Swiften/EventLoop/DummyEventLoop.cpp
@@ -6,16 +6,16 @@
 
 #include <Swiften/EventLoop/DummyEventLoop.h>
 
-#include <iostream>
+#include <Swiften/Base/Log.h>
 
 namespace Swift {
 
-DummyEventLoop::DummyEventLoop() : hasEvents_(false) {
+DummyEventLoop::DummyEventLoop() {
 }
 
 DummyEventLoop::~DummyEventLoop() {
     if (hasEvents()) {
-        std::cerr << "DummyEventLoop: Unhandled events at destruction time" << std::endl;
+        SWIFT_LOG(warning) << "DummyEventLoop: Unhandled events at destruction time" << std::endl;
     }
 }
 
@@ -27,12 +27,10 @@ void DummyEventLoop::processEvents() {
 }
 
 bool DummyEventLoop::hasEvents() {
-    std::lock_guard<std::mutex> lock(hasEventsMutex_);
     return hasEvents_;
 }
 
 void DummyEventLoop::eventPosted() {
-    std::lock_guard<std::mutex> lock(hasEventsMutex_);
     hasEvents_ = true;
 }
 
diff --git a/Swiften/EventLoop/DummyEventLoop.h b/Swiften/EventLoop/DummyEventLoop.h
index e411096..da2a360 100644
--- a/Swiften/EventLoop/DummyEventLoop.h
+++ b/Swiften/EventLoop/DummyEventLoop.h
@@ -6,8 +6,7 @@
 
 #pragma once
 
-#include <deque>
-#include <mutex>
+#include <atomic>
 
 #include <Swiften/Base/API.h>
 #include <Swiften/EventLoop/EventLoop.h>
@@ -25,7 +24,6 @@ namespace Swift {
             virtual void eventPosted();
 
         private:
-            bool hasEvents_;
-            std::mutex hasEventsMutex_;
+            std::atomic<bool> hasEvents_ = ATOMIC_VAR_INIT(false);
     };
 }
-- 
cgit v0.10.2-6-g49f6