PlatformConditionVariable.h (2105B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ConditionVariable_h 8 #define mozilla_ConditionVariable_h 9 10 #include "mozilla/PlatformMutex.h" 11 #include "mozilla/TimeStamp.h" 12 #if !defined(XP_WIN) && !defined(__wasi__) 13 # include <pthread.h> 14 #endif 15 16 namespace mozilla { 17 18 enum class CVStatus { NoTimeout, Timeout }; 19 20 namespace detail { 21 22 class ConditionVariableImpl { 23 public: 24 struct PlatformData; 25 26 MFBT_API ConditionVariableImpl(); 27 MFBT_API ~ConditionVariableImpl(); 28 29 // Wake one thread that is waiting on this condition. 30 MFBT_API void notify_one(); 31 32 // Wake all threads that are waiting on this condition. 33 MFBT_API void notify_all(); 34 35 // Atomically release |lock| and sleep the current thread of execution on 36 // this condition variable. 37 // |lock| will be re-acquired before this function returns. 38 // The thread may be woken from sleep from another thread via notify_one() 39 // or notify_all(), but may also wake spuriously. The caller should recheck 40 // its predicate after this function returns, typically in a while loop. 41 MFBT_API void wait(MutexImpl& lock); 42 43 MFBT_API CVStatus wait_for(MutexImpl& lock, 44 const mozilla::TimeDuration& rel_time); 45 46 private: 47 ConditionVariableImpl(const ConditionVariableImpl&) = delete; 48 ConditionVariableImpl& operator=(const ConditionVariableImpl&) = delete; 49 50 PlatformData* platformData(); 51 52 #if !defined(XP_WIN) && !defined(__wasi__) 53 void* platformData_[sizeof(pthread_cond_t) / sizeof(void*)]; 54 static_assert(sizeof(pthread_cond_t) / sizeof(void*) != 0 && 55 sizeof(pthread_cond_t) % sizeof(void*) == 0, 56 "pthread_cond_t must have pointer alignment"); 57 #else 58 void* platformData_[4]; 59 #endif 60 }; 61 62 } // namespace detail 63 64 } // namespace mozilla 65 66 #endif // mozilla_ConditionVariable_h