1 /* Copyright (c) 2006-2018. 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. */
10 #include <xbt/log.hpp>
12 #include "simgrid/s4u/ConditionVariable.hpp"
13 #include "simgrid/simix.h"
14 #include "src/kernel/activity/ConditionVariableImpl.hpp"
19 ConditionVariablePtr ConditionVariable::create()
21 smx_cond_t cond = simcall_cond_init();
22 return ConditionVariablePtr(&cond->cond_, false);
28 void ConditionVariable::wait(MutexPtr lock)
30 simcall_cond_wait(cond_, lock->mutex_);
33 void ConditionVariable::wait(std::unique_lock<Mutex>& lock)
35 simcall_cond_wait(cond_, lock.mutex()->mutex_);
38 std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout)
40 // The simcall uses -1 for "any timeout" but we don't want this:
45 simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
46 return std::cv_status::no_timeout;
49 // If the exception was a timeout, we have to take the lock again:
50 if (e.category == timeout_error) {
53 return std::cv_status::timeout;
59 // Another exception: should we reaquire the lock?
66 std::cv_status ConditionVariable::wait_until(std::unique_lock<Mutex>& lock, double timeout_time)
68 double now = SIMIX_get_clock();
70 if (timeout_time < now)
73 timeout = timeout_time - now;
74 return this->wait_for(lock, timeout);
80 void ConditionVariable::notify_one()
82 simgrid::simix::kernelImmediate([this]() { cond_->signal(); });
85 void ConditionVariable::notify_all()
87 simgrid::simix::kernelImmediate([this]() { cond_->broadcast(); });
90 void intrusive_ptr_add_ref(ConditionVariable* cond)
92 intrusive_ptr_add_ref(cond->cond_);
95 void intrusive_ptr_release(ConditionVariable* cond)
97 intrusive_ptr_release(cond->cond_);
101 } // namespace simgrid