libfoedus-core
FOEDUS Core Library
shared_cond.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_SOC_SHARED_COND_HPP_
19 #define FOEDUS_SOC_SHARED_COND_HPP_
20 
21 #include <pthread.h>
22 #include <stdint.h>
23 
24 #include "foedus/cxx11.hpp"
27 
28 namespace foedus {
29 namespace soc {
40  public:
41  SharedCond() : initialized_(false), waiters_(0), notifiers_(0) { initialize(); }
43 
44  // Disable copy constructors
46  SharedCond& operator=(const SharedCond&) CXX11_FUNC_DELETE;
47 
48  void initialize();
49  void uninitialize();
50  bool is_initialized() const { return initialized_; }
51 
65  void wait(SharedMutexScope* scope);
66 
79  bool timedwait(SharedMutexScope* scope, uint64_t timeout_nanosec);
80 
98  void broadcast(SharedMutexScope* scope);
99 
107  void broadcast_nolock();
108 
119  void signal(SharedMutexScope* scope);
120 
128  SharedMutex* get_mutex() { return &mutex_; }
129 
134  bool exists_waiters() const { return waiters_ != 0; }
135 
136  private:
138  bool initialized_;
139 
140  SharedMutex mutex_;
141  pthread_cond_t cond_;
142  pthread_condattr_t attr_;
144  uint32_t waiters_;
146  uint32_t notifiers_;
147 
148  void common_assert(SharedMutexScope* scope);
149 };
150 
151 } // namespace soc
152 } // namespace foedus
153 #endif // FOEDUS_SOC_SHARED_COND_HPP_
void wait(SharedMutexScope *scope)
Unconditionally wait for the event.
Definition: shared_cond.cpp:90
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void signal(SharedMutexScope *scope)
Unblock one waiter.
A conditional variable that can be placed in shared memory and used from multiple processes...
Definition: shared_cond.hpp:39
A mutex that can be placed in shared memory and used from multiple processes.
bool timedwait(SharedMutexScope *scope, uint64_t timeout_nanosec)
Wait for the event up to the given timeout.
void broadcast(SharedMutexScope *scope)
Unblock all waiters.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
bool is_initialized() const
Definition: shared_cond.hpp:50
Auto-lock scope object for SharedMutex.
#define CXX11_FUNC_DELETE
Used in public headers in place of " = delete" of C++11.
Definition: cxx11.hpp:128
bool exists_waiters() const
A non-synchronized method to tell seemingly whether there is a waiter or not.
void broadcast_nolock()
Unblock all waiters without a mutex held by the signaller.
SharedMutex * get_mutex()
Returns the mutex that protects this condition variable.