/**************************************************************************/1/* condition_variable.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/os/mutex.h"33#include "core/os/safe_binary_mutex.h"3435#ifdef THREADS_ENABLED3637#ifdef MINGW_ENABLED38#define MINGW_STDTHREAD_REDUNDANCY_WARNING39#include "thirdparty/mingw-std-threads/mingw.condition_variable.h"40#define THREADING_NAMESPACE mingw_stdthread41#else42#include <condition_variable>43#define THREADING_NAMESPACE std44#endif4546// An object one or multiple threads can wait on a be notified by some other.47// Normally, you want to use a semaphore for such scenarios, but when the48// condition is something different than a count being greater than zero49// (which is the built-in logic in a semaphore) or you want to provide your50// own mutex to tie the wait-notify to some other behavior, you need to use this.5152class ConditionVariable {53mutable THREADING_NAMESPACE::condition_variable condition;5455public:56template <typename BinaryMutexT>57_ALWAYS_INLINE_ void wait(const MutexLock<BinaryMutexT> &p_lock) const {58condition.wait(p_lock._get_lock());59}6061template <int Tag>62_ALWAYS_INLINE_ void wait(const MutexLock<SafeBinaryMutex<Tag>> &p_lock) const {63condition.wait(p_lock.mutex._get_lock());64}6566_ALWAYS_INLINE_ void notify_one() const {67condition.notify_one();68}6970_ALWAYS_INLINE_ void notify_all() const {71condition.notify_all();72}73};7475#else // No threads.7677class ConditionVariable {78public:79template <typename BinaryMutexT>80void wait(const MutexLock<BinaryMutexT> &p_lock) const {}81void notify_one() const {}82void notify_all() const {}83};8485#endif // THREADS_ENABLED868788