Bug 1507748 - Add a pointer cursor to the flexbox inspector buttons. r=mtigley
[gecko.git] / storage / SQLiteMutex.h
blob9256be74e63a8e8c4cf7f3f4ddcc5595a3a5cf0a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_storage_SQLiteMutex_h_
8 #define mozilla_storage_SQLiteMutex_h_
10 #include "mozilla/BlockingResourceBase.h"
11 #include "sqlite3.h"
13 namespace mozilla {
14 namespace storage {
16 /**
17 * Wrapper class for sqlite3_mutexes. To be used whenever we want to use a
18 * sqlite3_mutex.
20 * @warning Never EVER wrap the same sqlite3_mutex with a different SQLiteMutex.
21 * If you do this, you void the deadlock detector's warranty!
23 class SQLiteMutex : private BlockingResourceBase
25 public:
26 /**
27 * Constructs a wrapper for a sqlite3_mutex that has deadlock detecting.
29 * @param aName
30 * A name which can be used to reference this mutex.
32 explicit SQLiteMutex(const char *aName)
33 : BlockingResourceBase(aName, eMutex)
34 , mMutex(nullptr)
38 /**
39 * Sets the mutex that we are wrapping. We generally do not have access to
40 * our mutex at class construction, so we have to set it once we get access to
41 * it.
43 * @param aMutex
44 * The sqlite3_mutex that we are going to wrap.
46 void initWithMutex(sqlite3_mutex *aMutex)
48 NS_ASSERTION(aMutex, "You must pass in a valid mutex!");
49 NS_ASSERTION(!mMutex, "A mutex has already been set for this!");
50 mMutex = aMutex;
53 /**
54 * After a connection has been successfully closed, its mutex is a dangling
55 * pointer, and as such it should be destroyed.
57 void destroy() {
58 mMutex = NULL;
61 #if !defined(DEBUG) || defined(MOZ_SYSTEM_SQLITE)
62 /**
63 * Acquires the mutex.
65 void lock()
67 ::sqlite3_mutex_enter(mMutex);
70 /**
71 * Releases the mutex.
73 void unlock()
75 ::sqlite3_mutex_leave(mMutex);
78 /**
79 * Asserts that the current thread owns the mutex.
81 void assertCurrentThreadOwns()
85 /**
86 * Asserts that the current thread does not own the mutex.
88 void assertNotCurrentThreadOwns()
92 #else
93 void lock()
95 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
97 // While SQLite Mutexes may be recursive, in our own code we do not want to
98 // treat them as such.
100 CheckAcquire();
101 ::sqlite3_mutex_enter(mMutex);
102 Acquire(); // Call is protected by us holding the mutex.
105 void unlock()
107 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
109 // While SQLite Mutexes may be recursive, in our own code we do not want to
110 // treat them as such.
111 Release(); // Call is protected by us holding the mutex.
112 ::sqlite3_mutex_leave(mMutex);
115 void assertCurrentThreadOwns()
117 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
118 MOZ_ASSERT(sqlite3_mutex_held(mMutex),
119 "Mutex is not held, but we expect it to be!");
122 void assertNotCurrentThreadOwns()
124 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
125 MOZ_ASSERT(sqlite3_mutex_notheld(mMutex),
126 "Mutex is held, but we expect it to not be!");
128 #endif // ifndef DEBUG
130 private:
131 sqlite3_mutex *mMutex;
135 * Automatically acquires the mutex when it enters scope, and releases it when
136 * it leaves scope.
138 class MOZ_STACK_CLASS SQLiteMutexAutoLock
140 public:
141 explicit SQLiteMutexAutoLock(SQLiteMutex &aMutex)
142 : mMutex(aMutex)
144 mMutex.lock();
147 ~SQLiteMutexAutoLock()
149 mMutex.unlock();
152 private:
153 SQLiteMutex &mMutex;
157 * Automatically releases the mutex when it enters scope, and acquires it when
158 * it leaves scope.
160 class MOZ_STACK_CLASS SQLiteMutexAutoUnlock
162 public:
163 explicit SQLiteMutexAutoUnlock(SQLiteMutex &aMutex)
164 : mMutex(aMutex)
166 mMutex.unlock();
169 ~SQLiteMutexAutoUnlock()
171 mMutex.lock();
174 private:
175 SQLiteMutex &mMutex;
178 } // namespace storage
179 } // namespace mozilla
181 #endif // mozilla_storage_SQLiteMutex_h_