libfoedus-core
FOEDUS Core Library
rendezvous_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_THREAD_RENDEZVOUS_IMPL_HPP_
19 #define FOEDUS_THREAD_RENDEZVOUS_IMPL_HPP_
20 #include <atomic>
21 #include <chrono>
22 
23 #include "foedus/assert_nd.hpp"
25 
26 namespace foedus {
27 namespace thread {
49 class Rendezvous final {
50  public:
51  Rendezvous() : signaled_(false) {}
52 
53  // not copyable, assignable.
54  Rendezvous(const Rendezvous &other) = delete;
55  Rendezvous& operator=(const Rendezvous &other) = delete;
56  Rendezvous(Rendezvous &&other) = delete;
57  Rendezvous& operator=(Rendezvous &&other) = delete;
58 
64  void wait() {
65  if (is_signaled()) {
66  return;
67  }
68  condition_.wait([this]{ return is_signaled(); });
69  }
70 
77  template<class REP, class PERIOD>
78  bool wait_for(const std::chrono::duration<REP, PERIOD>& timeout) {
79  if (is_signaled()) {
80  return true;
81  }
82  return condition_.wait_for(timeout, [this]{ return is_signaled(); });
83  }
84 
91  template< class CLOCK, class DURATION >
92  bool wait_until(const std::chrono::time_point<CLOCK, DURATION>& until) {
93  if (is_signaled()) {
94  return true;
95  }
96  return condition_.wait_until(until, [this]{ return is_signaled(); });
97  }
98 
106  void signal() {
108  condition_.notify_all([this]{ signaled_.store(true); });
109  // we must not put ANYTHING after this because notified waiters might have already
110  // deleted this object. notify_broadcast() guarantees that itself finishes before
111  // destruction, but no guarantee after that.
112  }
113 
115  bool is_signaled() const {
116  return signaled_.load();
117  }
119  bool is_signaled_weak() const {
120  return signaled_.load(std::memory_order_relaxed);
121  }
122 
123  private:
125  ConditionVariable condition_;
127  std::atomic<bool> signaled_;
128 };
129 
130 
131 } // namespace thread
132 } // namespace foedus
133 #endif // FOEDUS_THREAD_RENDEZVOUS_IMPL_HPP_
void notify_all(SIGNAL_ACTION signal_action)
Notify all waiters that the event has happened.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
bool is_signaled() const
returns whether this thread has stopped (if the thread hasn't started, false too).
void wait()
Block until the event happens.
The frequently appearing triplet of condition_varible, "signal" flag for spurious wakeup...
void signal()
Notify all waiters that the event has happened.
bool wait_for(const std::chrono::duration< REP, PERIOD > &timeout, PREDICATE predicate)
Block until the event happens or the given period elapses.
Rendezvous & operator=(const Rendezvous &other)=delete
bool wait_until(const std::chrono::time_point< CLOCK, DURATION > &until)
Block until the event happens or the given time point arrives.
An analogue of pthread's condition variable and std::condition_variable to avoid glibc's bug in pthre...
bool wait_until(const std::chrono::time_point< CLOCK, DURATION > &until, PREDICATE predicate)
Block until the event happens or the given time point arrives.
void wait(PREDICATE predicate)
Block until the event happens.
bool wait_for(const std::chrono::duration< REP, PERIOD > &timeout)
Block until the event happens or the given period elapses.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
bool is_signaled_weak() const
non-atomic is_signaled().