bmalloc: Miscellaneous cleanup
[WebKit-https.git] / Source / bmalloc / bmalloc / StaticMutex.h
index ec47dd18a97a3bbcca01bd44cbcce0fcecf5df32..4404e952dd46b895c0e9bd21f0b3c124b6a31d03 100644 (file)
@@ -28,6 +28,8 @@
 
 #include "BAssert.h"
 #include <atomic>
+#include <mutex>
+#include <thread>
 
 // A fast replacement for std::mutex, for use in static storage.
 
@@ -52,6 +54,27 @@ private:
     std::atomic_flag m_flag;
 };
 
+static inline void sleep(
+    std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds duration)
+{
+    if (duration == std::chrono::milliseconds(0))
+        return;
+    
+    lock.unlock();
+    std::this_thread::sleep_for(duration);
+    lock.lock();
+}
+
+static inline void waitUntilFalse(
+    std::unique_lock<StaticMutex>& lock, std::chrono::milliseconds sleepDuration,
+    bool& condition)
+{
+    while (condition) {
+        condition = false;
+        sleep(lock, sleepDuration);
+    }
+}
+
 inline void StaticMutex::init()
 {
     m_flag.clear();