blob: 5090a71b78d6126e6b9d0acdbe429c0bb99b87e1 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: Craig Silverstein.
30//
31// A simple mutex wrapper, supporting locks and read-write locks.
32// You should assume the locks are *not* re-entrant.
33//
34// This class is meant to be internal-only and should be wrapped by an
35// internal namespace. Before you use this module, please give the
36// name of your internal namespace for this module. Or, if you want
37// to expose it, you'll want to move it to the Google namespace. We
38// cannot put this class in global namespace because there can be some
39// problems when we have multiple versions of Mutex in each shared object.
40//
41// NOTE: by default, we have #ifdef'ed out the TryLock() method.
42// This is for two reasons:
43// 1) TryLock() under Windows is a bit annoying (it requires a
44// #define to be defined very early).
45// 2) TryLock() is broken for NO_THREADS mode, at least in NDEBUG
46// mode.
47// If you need TryLock(), and either these two caveats are not a
48// problem for you, or you're willing to work around them, then
49// feel free to #define GMUTEX_TRYLOCK, or to remove the #ifdefs
50// in the code below.
51//
52// CYGWIN NOTE: Cygwin support for rwlock seems to be buggy:
53// http://www.cygwin.com/ml/cygwin/2008-12/msg00017.html
54// Because of that, we might as well use windows locks for
55// cygwin. They seem to be more reliable than the cygwin pthreads layer.
56//
57// TRICKY IMPLEMENTATION NOTE:
58// This class is designed to be safe to use during
59// dynamic-initialization -- that is, by global constructors that are
60// run before main() starts. The issue in this case is that
61// dynamic-initialization happens in an unpredictable order, and it
62// could be that someone else's dynamic initializer could call a
63// function that tries to acquire this mutex -- but that all happens
64// before this mutex's constructor has run. (This can happen even if
65// the mutex and the function that uses the mutex are in the same .cc
66// file.) Basically, because Mutex does non-trivial work in its
67// constructor, it's not, in the naive implementation, safe to use
68// before dynamic initialization has run on it.
69//
70// The solution used here is to pair the actual mutex primitive with a
71// bool that is set to true when the mutex is dynamically initialized.
72// (Before that it's false.) Then we modify all mutex routines to
73// look at the bool, and not try to lock/unlock until the bool makes
74// it to true (which happens after the Mutex constructor has run.)
75//
76// This works because before main() starts -- particularly, during
77// dynamic initialization -- there are no threads, so a) it's ok that
78// the mutex operations are a no-op, since we don't need locking then
79// anyway; and b) we can be quite confident our bool won't change
80// state between a call to Lock() and a call to Unlock() (that would
81// require a global constructor in one translation unit to call Lock()
82// and another global constructor in another translation unit to call
83// Unlock() later, which is pretty perverse).
84//
85// That said, it's tricky, and can conceivably fail; it's safest to
86// avoid trying to acquire a mutex in a global constructor, if you
87// can. One way it can fail is that a really smart compiler might
88// initialize the bool to true at static-initialization time (too
89// early) rather than at dynamic-initialization time. To discourage
90// that, we set is_safe_ to true in code (not the constructor
91// colon-initializer) and set it to true via a function that always
92// evaluates to true, but that the compiler can't know always
93// evaluates to true. This should be good enough.
94
95#ifndef CERES_INTERNAL_MUTEX_H_
96#define CERES_INTERNAL_MUTEX_H_
97
Keir Mierleff71d742012-08-10 17:05:15 -070098#if defined(CERES_NO_THREADS)
Keir Mierle8ebb0732012-04-30 23:09:08 -070099 typedef int MutexType; // to keep a lock-count
100#elif defined(_WIN32) || defined(__CYGWIN32__) || defined(__CYGWIN64__)
Keir Mierleff71d742012-08-10 17:05:15 -0700101# define CERES_WIN32_LEAN_AND_MEAN // We only need minimal includes
102# ifdef CERES_GMUTEX_TRYLOCK
Keir Mierle8ebb0732012-04-30 23:09:08 -0700103 // We need Windows NT or later for TryEnterCriticalSection(). If you
104 // don't need that functionality, you can remove these _WIN32_WINNT
105 // lines, and change TryLock() to assert(0) or something.
106# ifndef _WIN32_WINNT
107# define _WIN32_WINNT 0x0400
108# endif
109# endif
110// To avoid macro definition of ERROR.
Keir Mierleff71d742012-08-10 17:05:15 -0700111# define CERES_NOGDI
Keir Mierle8ebb0732012-04-30 23:09:08 -0700112// To avoid macro definition of min/max.
Keir Mierleff71d742012-08-10 17:05:15 -0700113# define CERES_NOMINMAX
Keir Mierle8ebb0732012-04-30 23:09:08 -0700114# include <windows.h>
115 typedef CRITICAL_SECTION MutexType;
116#elif defined(CERES_HAVE_PTHREAD) && defined(CERES_HAVE_RWLOCK)
117 // Needed for pthread_rwlock_*. If it causes problems, you could take it
118 // out, but then you'd have to unset CERES_HAVE_RWLOCK (at least on linux --
119 // it *does* cause problems for FreeBSD, or MacOSX, but isn't needed for
120 // locking there.)
121# if defined(__linux__) && !defined(_XOPEN_SOURCE)
122# define _XOPEN_SOURCE 500 // may be needed to get the rwlock calls
123# endif
124# include <pthread.h>
125 typedef pthread_rwlock_t MutexType;
126#elif defined(CERES_HAVE_PTHREAD)
127# include <pthread.h>
128 typedef pthread_mutex_t MutexType;
129#else
130# error Need to implement mutex.h for your architecture, or #define NO_THREADS
131#endif
132
133// We need to include these header files after defining _XOPEN_SOURCE
134// as they may define the _XOPEN_SOURCE macro.
135#include <assert.h>
136#include <stdlib.h> // for abort()
137
138namespace ceres {
139namespace internal {
140
141class Mutex {
142 public:
143 // Create a Mutex that is not held by anybody. This constructor is
144 // typically used for Mutexes allocated on the heap or the stack.
145 // See below for a recommendation for constructing global Mutex
146 // objects.
147 inline Mutex();
148
149 // Destructor
150 inline ~Mutex();
151
152 inline void Lock(); // Block if needed until free then acquire exclusively
153 inline void Unlock(); // Release a lock acquired via Lock()
Keir Mierleff71d742012-08-10 17:05:15 -0700154#ifdef CERES_GMUTEX_TRYLOCK
Keir Mierle8ebb0732012-04-30 23:09:08 -0700155 inline bool TryLock(); // If free, Lock() and return true, else return false
156#endif
157 // Note that on systems that don't support read-write locks, these may
158 // be implemented as synonyms to Lock() and Unlock(). So you can use
159 // these for efficiency, but don't use them anyplace where being able
160 // to do shared reads is necessary to avoid deadlock.
161 inline void ReaderLock(); // Block until free or shared then acquire a share
162 inline void ReaderUnlock(); // Release a read share of this Mutex
163 inline void WriterLock() { Lock(); } // Acquire an exclusive lock
164 inline void WriterUnlock() { Unlock(); } // Release a lock from WriterLock()
165
166 // TODO(hamaji): Do nothing, implement correctly.
167 inline void AssertHeld() {}
168
169 private:
170 MutexType mutex_;
171 // We want to make sure that the compiler sets is_safe_ to true only
172 // when we tell it to, and never makes assumptions is_safe_ is
173 // always true. volatile is the most reliable way to do that.
174 volatile bool is_safe_;
175
176 inline void SetIsSafe() { is_safe_ = true; }
177
178 // Catch the error of writing Mutex when intending MutexLock.
179 Mutex(Mutex* /*ignored*/) {}
180 // Disallow "evil" constructors
181 Mutex(const Mutex&);
182 void operator=(const Mutex&);
183};
184
185// Now the implementation of Mutex for various systems
Keir Mierleff71d742012-08-10 17:05:15 -0700186#if defined(CERES_NO_THREADS)
Keir Mierle8ebb0732012-04-30 23:09:08 -0700187
188// When we don't have threads, we can be either reading or writing,
189// but not both. We can have lots of readers at once (in no-threads
190// mode, that's most likely to happen in recursive function calls),
191// but only one writer. We represent this by having mutex_ be -1 when
192// writing and a number > 0 when reading (and 0 when no lock is held).
193//
194// In debug mode, we assert these invariants, while in non-debug mode
195// we do nothing, for efficiency. That's why everything is in an
196// assert.
197
198Mutex::Mutex() : mutex_(0) { }
199Mutex::~Mutex() { assert(mutex_ == 0); }
200void Mutex::Lock() { assert(--mutex_ == -1); }
201void Mutex::Unlock() { assert(mutex_++ == -1); }
Keir Mierleff71d742012-08-10 17:05:15 -0700202#ifdef CERES_GMUTEX_TRYLOCK
Keir Mierle8ebb0732012-04-30 23:09:08 -0700203bool Mutex::TryLock() { if (mutex_) return false; Lock(); return true; }
204#endif
205void Mutex::ReaderLock() { assert(++mutex_ > 0); }
206void Mutex::ReaderUnlock() { assert(mutex_-- > 0); }
207
208#elif defined(_WIN32) || defined(__CYGWIN32__) || defined(__CYGWIN64__)
209
210Mutex::Mutex() { InitializeCriticalSection(&mutex_); SetIsSafe(); }
211Mutex::~Mutex() { DeleteCriticalSection(&mutex_); }
212void Mutex::Lock() { if (is_safe_) EnterCriticalSection(&mutex_); }
213void Mutex::Unlock() { if (is_safe_) LeaveCriticalSection(&mutex_); }
214#ifdef GMUTEX_TRYLOCK
215bool Mutex::TryLock() { return is_safe_ ?
216 TryEnterCriticalSection(&mutex_) != 0 : true; }
217#endif
218void Mutex::ReaderLock() { Lock(); } // we don't have read-write locks
219void Mutex::ReaderUnlock() { Unlock(); }
220
221#elif defined(CERES_HAVE_PTHREAD) && defined(CERES_HAVE_RWLOCK)
222
Keir Mierleff71d742012-08-10 17:05:15 -0700223#define CERES_SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \
224 if (is_safe_ && fncall(&mutex_) != 0) abort(); \
Keir Mierle8ebb0732012-04-30 23:09:08 -0700225} while (0)
226
227Mutex::Mutex() {
228 SetIsSafe();
229 if (is_safe_ && pthread_rwlock_init(&mutex_, NULL) != 0) abort();
230}
Keir Mierleff71d742012-08-10 17:05:15 -0700231Mutex::~Mutex() { CERES_SAFE_PTHREAD(pthread_rwlock_destroy); }
232void Mutex::Lock() { CERES_SAFE_PTHREAD(pthread_rwlock_wrlock); }
233void Mutex::Unlock() { CERES_SAFE_PTHREAD(pthread_rwlock_unlock); }
234#ifdef CERES_GMUTEX_TRYLOCK
Keir Mierle8ebb0732012-04-30 23:09:08 -0700235bool Mutex::TryLock() { return is_safe_ ?
236 pthread_rwlock_trywrlock(&mutex_) == 0 :
237 true; }
238#endif
Keir Mierleff71d742012-08-10 17:05:15 -0700239void Mutex::ReaderLock() { CERES_SAFE_PTHREAD(pthread_rwlock_rdlock); }
240void Mutex::ReaderUnlock() { CERES_SAFE_PTHREAD(pthread_rwlock_unlock); }
241#undef CERES_SAFE_PTHREAD
Keir Mierle8ebb0732012-04-30 23:09:08 -0700242
243#elif defined(CERES_HAVE_PTHREAD)
244
Keir Mierleff71d742012-08-10 17:05:15 -0700245#define CERES_SAFE_PTHREAD(fncall) do { /* run fncall if is_safe_ is true */ \
246 if (is_safe_ && fncall(&mutex_) != 0) abort(); \
Keir Mierle8ebb0732012-04-30 23:09:08 -0700247} while (0)
248
249Mutex::Mutex() {
250 SetIsSafe();
251 if (is_safe_ && pthread_mutex_init(&mutex_, NULL) != 0) abort();
252}
Keir Mierleff71d742012-08-10 17:05:15 -0700253Mutex::~Mutex() { CERES_SAFE_PTHREAD(pthread_mutex_destroy); }
254void Mutex::Lock() { CERES_SAFE_PTHREAD(pthread_mutex_lock); }
255void Mutex::Unlock() { CERES_SAFE_PTHREAD(pthread_mutex_unlock); }
256#ifdef CERES_GMUTEX_TRYLOCK
Keir Mierle8ebb0732012-04-30 23:09:08 -0700257bool Mutex::TryLock() { return is_safe_ ?
258 pthread_mutex_trylock(&mutex_) == 0 : true; }
259#endif
260void Mutex::ReaderLock() { Lock(); }
261void Mutex::ReaderUnlock() { Unlock(); }
Keir Mierleff71d742012-08-10 17:05:15 -0700262#undef CERES_SAFE_PTHREAD
Keir Mierle8ebb0732012-04-30 23:09:08 -0700263
264#endif
265
266// --------------------------------------------------------------------------
267// Some helper classes
268
Keir Mierleff71d742012-08-10 17:05:15 -0700269// Note: The weird "Ceres" prefix for the class is a workaround for having two
270// similar mutex.h files included in the same translation unit. This is a
271// problem because macros do not respect C++ namespaces, and as a result, this
272// does not work well (e.g. inside Chrome). The offending macros are
273// "MutexLock(x) COMPILE_ASSERT(false)". To work around this, "Ceres" is
274// prefixed to the class names; this permits defining the classes.
275
276// CeresMutexLock(mu) acquires mu when constructed and releases it when destroyed.
277class CeresMutexLock {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700278 public:
Keir Mierleff71d742012-08-10 17:05:15 -0700279 explicit CeresMutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
280 ~CeresMutexLock() { mu_->Unlock(); }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700281 private:
282 Mutex * const mu_;
283 // Disallow "evil" constructors
Keir Mierleff71d742012-08-10 17:05:15 -0700284 CeresMutexLock(const CeresMutexLock&);
285 void operator=(const CeresMutexLock&);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700286};
287
Keir Mierleff71d742012-08-10 17:05:15 -0700288// CeresReaderMutexLock and CeresWriterMutexLock do the same, for rwlocks
289class CeresReaderMutexLock {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700290 public:
Keir Mierleff71d742012-08-10 17:05:15 -0700291 explicit CeresReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
292 ~CeresReaderMutexLock() { mu_->ReaderUnlock(); }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700293 private:
294 Mutex * const mu_;
295 // Disallow "evil" constructors
Keir Mierleff71d742012-08-10 17:05:15 -0700296 CeresReaderMutexLock(const CeresReaderMutexLock&);
297 void operator=(const CeresReaderMutexLock&);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700298};
299
Keir Mierleff71d742012-08-10 17:05:15 -0700300class CeresWriterMutexLock {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700301 public:
Keir Mierleff71d742012-08-10 17:05:15 -0700302 explicit CeresWriterMutexLock(Mutex *mu) : mu_(mu) { mu_->WriterLock(); }
303 ~CeresWriterMutexLock() { mu_->WriterUnlock(); }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700304 private:
305 Mutex * const mu_;
306 // Disallow "evil" constructors
Keir Mierleff71d742012-08-10 17:05:15 -0700307 CeresWriterMutexLock(const CeresWriterMutexLock&);
308 void operator=(const CeresWriterMutexLock&);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700309};
310
311// Catch bug where variable name is omitted, e.g. MutexLock (&mu);
Keir Mierleff71d742012-08-10 17:05:15 -0700312#define CeresMutexLock(x) \
313 COMPILE_ASSERT(0, ceres_mutex_lock_decl_missing_var_name)
314#define CeresReaderMutexLock(x) \
315 COMPILE_ASSERT(0, ceres_rmutex_lock_decl_missing_var_name)
316#define CeresWriterMutexLock(x) \
317 COMPILE_ASSERT(0, ceres_wmutex_lock_decl_missing_var_name)
Keir Mierle8ebb0732012-04-30 23:09:08 -0700318
319} // namespace internal
320} // namespace ceres
321
322#endif // CERES_INTERNAL_MUTEX_H_