libfoedus-core
FOEDUS Core Library
shared_mutex.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_MUTEX_HPP_
19 #define FOEDUS_SOC_SHARED_MUTEX_HPP_
20 
21 #include <pthread.h>
22 #include <stdint.h>
23 
24 #include "foedus/cxx11.hpp"
25 
26 namespace foedus {
27 namespace soc {
48  public:
49  SharedMutex() : initialized_(false), recursive_(false) { initialize(); }
51 
52  // Disable copy constructors
54  SharedMutex& operator=(const SharedMutex&) CXX11_FUNC_DELETE;
55 
56  void initialize(bool recursive = false);
57  void uninitialize();
58  bool is_initialized() const { return initialized_; }
59  bool is_recursive() const { return recursive_; }
60 
62  void lock();
63 
69  bool timedlock(uint64_t timeout_nanosec);
70 
75  bool trylock();
76 
78  void unlock();
79 
80  pthread_mutex_t* get_raw_mutex() { return &mutex_; }
81 
82  private:
84  bool initialized_;
86  bool recursive_;
87 
88  pthread_mutex_t mutex_;
89  pthread_mutexattr_t attr_;
90 };
91 
101  public:
102  SharedMutexScope(SharedMutex* mutex, bool lock_initially = true)
103  : mutex_(mutex), locked_by_me_(false) {
104  if (lock_initially) {
105  lock();
106  }
107  }
109 
110  // Disable copy constructors
112  SharedMutexScope& operator=(const SharedMutexScope&) CXX11_FUNC_DELETE;
113 
114  bool is_locked_by_me() const { return locked_by_me_; }
115  SharedMutex* get_mutex() const { return mutex_; }
116 
117  void lock();
118  void unlock();
119 
120  private:
121  SharedMutex* const mutex_;
122  bool locked_by_me_;
123 };
124 
125 } // namespace soc
126 } // namespace foedus
127 #endif // FOEDUS_SOC_SHARED_MUTEX_HPP_
SharedMutexScope(SharedMutex *mutex, bool lock_initially=true)
bool timedlock(uint64_t timeout_nanosec)
Try lock up to the given timeout.
void initialize(bool recursive=false)
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
pthread_mutex_t * get_raw_mutex()
A mutex that can be placed in shared memory and used from multiple processes.
SharedMutex * get_mutex() const
bool trylock()
Instantaneously try the lock.
void lock()
Unconditionally lock.
#define CXX11_FINAL
Used in public headers in place of "final" of C++11.
Definition: cxx11.hpp:131
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 is_recursive() const
bool is_initialized() const
void unlock()
Unlock it.