Bug 1568860 - Part 1: Use PageStyle from the currently selected element in the fonts...
[gecko.git] / storage / SQLiteMutex.h
blob77598be71971c27c8aa339cae5b95ed0c612f3e6
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 {
24 public:
25 /**
26 * Constructs a wrapper for a sqlite3_mutex that has deadlock detecting.
28 * @param aName
29 * A name which can be used to reference this mutex.
31 explicit SQLiteMutex(const char* aName)
32 : BlockingResourceBase(aName, eMutex), mMutex(nullptr) {}
34 /**
35 * Sets the mutex that we are wrapping. We generally do not have access to
36 * our mutex at class construction, so we have to set it once we get access to
37 * it.
39 * @param aMutex
40 * The sqlite3_mutex that we are going to wrap.
42 void initWithMutex(sqlite3_mutex* aMutex) {
43 NS_ASSERTION(aMutex, "You must pass in a valid mutex!");
44 NS_ASSERTION(!mMutex, "A mutex has already been set for this!");
45 mMutex = aMutex;
48 /**
49 * After a connection has been successfully closed, its mutex is a dangling
50 * pointer, and as such it should be destroyed.
52 void destroy() { mMutex = NULL; }
54 #if !defined(DEBUG) || defined(MOZ_SYSTEM_SQLITE)
55 /**
56 * Acquires the mutex.
58 void lock() { ::sqlite3_mutex_enter(mMutex); }
60 /**
61 * Releases the mutex.
63 void unlock() { ::sqlite3_mutex_leave(mMutex); }
65 /**
66 * Asserts that the current thread owns the mutex.
68 void assertCurrentThreadOwns() {}
70 /**
71 * Asserts that the current thread does not own the mutex.
73 void assertNotCurrentThreadOwns() {}
75 #else
76 void lock() {
77 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
79 // While SQLite Mutexes may be recursive, in our own code we do not want to
80 // treat them as such.
82 CheckAcquire();
83 ::sqlite3_mutex_enter(mMutex);
84 Acquire(); // Call is protected by us holding the mutex.
87 void unlock() {
88 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
90 // While SQLite Mutexes may be recursive, in our own code we do not want to
91 // treat them as such.
92 Release(); // Call is protected by us holding the mutex.
93 ::sqlite3_mutex_leave(mMutex);
96 void assertCurrentThreadOwns() {
97 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
98 MOZ_ASSERT(sqlite3_mutex_held(mMutex),
99 "Mutex is not held, but we expect it to be!");
102 void assertNotCurrentThreadOwns() {
103 MOZ_ASSERT(mMutex, "No mutex associated with this wrapper!");
104 MOZ_ASSERT(sqlite3_mutex_notheld(mMutex),
105 "Mutex is held, but we expect it to not be!");
107 #endif // ifndef DEBUG
109 private:
110 sqlite3_mutex* mMutex;
114 * Automatically acquires the mutex when it enters scope, and releases it when
115 * it leaves scope.
117 class MOZ_STACK_CLASS SQLiteMutexAutoLock {
118 public:
119 explicit SQLiteMutexAutoLock(SQLiteMutex& aMutex) : mMutex(aMutex) {
120 mMutex.lock();
123 ~SQLiteMutexAutoLock() { mMutex.unlock(); }
125 private:
126 SQLiteMutex& mMutex;
130 * Automatically releases the mutex when it enters scope, and acquires it when
131 * it leaves scope.
133 class MOZ_STACK_CLASS SQLiteMutexAutoUnlock {
134 public:
135 explicit SQLiteMutexAutoUnlock(SQLiteMutex& aMutex) : mMutex(aMutex) {
136 mMutex.unlock();
139 ~SQLiteMutexAutoUnlock() { mMutex.lock(); }
141 private:
142 SQLiteMutex& mMutex;
145 } // namespace storage
146 } // namespace mozilla
148 #endif // mozilla_storage_SQLiteMutex_h_