1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_S4U_COND_VARIABLE_HPP
7 #define SIMGRID_S4U_COND_VARIABLE_HPP
9 #include <condition_variable>
11 #include <utility> // std::swap
13 #include <simgrid/simix.h>
14 #include <simgrid/s4u/mutex.hpp>
21 /** A condition variable
23 * This is based on std::condition_variable and should respect the same
24 * semantic. But we currently use (only) double for both durations and
27 XBT_PUBLIC_CLASS ConditionVariable {
32 ConditionVariable(ConditionVariable* cond) : cond_(SIMIX_cond_ref(cond->cond_)) {}
35 // Copy+move (with the copy-and-swap idiom):
36 ConditionVariable(ConditionVariable const& cond) : cond_(SIMIX_cond_ref(cond.cond_)) {}
37 friend void swap(ConditionVariable& first, ConditionVariable& second)
40 swap(first.cond_, second.cond_);
42 ConditionVariable& operator=(ConditionVariable cond)
47 ConditionVariable(ConditionVariable&& cond) : cond_(nullptr)
54 return cond_ != nullptr;
59 void wait(std::unique_lock<Mutex>& lock);
60 std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
61 std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
63 // Variants which takes a predicate:
66 void wait(std::unique_lock<Mutex>& lock, P pred)
72 bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
75 if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
80 bool wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
82 return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
90 XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
91 void notify() { notify_one(); }
97 }} // namespace simgrid::s4u
99 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */