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>
12 #include <utility> // std::swap
14 #include <boost/intrusive_ptr.hpp>
18 #include <simgrid/simix.h>
19 #include <simgrid/s4u/mutex.hpp>
26 /** A condition variable
28 * This is based on std::condition_variable and should respect the same
29 * semantic. But we currently use (only) double for both durations and
32 XBT_PUBLIC_CLASS ConditionVariable {
36 ConditionVariable(smx_cond_t cond) : cond_(cond) {}
39 ConditionVariable(ConditionVariable const&) = delete;
40 ConditionVariable& operator=(ConditionVariable const&) = delete;
42 friend XBT_PUBLIC(void) intrusive_ptr_add_ref(ConditionVariable* cond);
43 friend XBT_PUBLIC(void) intrusive_ptr_release(ConditionVariable* cond);
44 using Ptr = boost::intrusive_ptr<ConditionVariable>;
46 static Ptr createConditionVariable();
50 void wait(std::unique_lock<Mutex>& lock);
51 std::cv_status wait_until(std::unique_lock<Mutex>& lock, double timeout_time);
52 std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
54 // Variants which takes a predicate:
57 void wait(std::unique_lock<Mutex>& lock, P pred)
63 bool wait_until(std::unique_lock<Mutex>& lock, double timeout_time, P pred)
66 if (this->wait_until(lock, timeout_time) == std::cv_status::timeout)
71 bool wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
73 return this->wait_until(lock, SIMIX_get_clock() + duration, std::move(pred));
81 XBT_ATTRIB_DEPRECATED("Use notify_one() instead")
82 void notify() { notify_one(); }
85 using ConditionVariablePtr = ConditionVariable::Ptr;
87 }} // namespace simgrid::s4u
89 #endif /* SIMGRID_S4U_COND_VARIABLE_HPP */