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"
17 * Wrapper class for sqlite3_mutexes. To be used whenever we want to use a
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
27 * Constructs a wrapper for a sqlite3_mutex that has deadlock detecting.
30 * A name which can be used to reference this mutex.
32 explicit SQLiteMutex(const char *aName
)
33 : BlockingResourceBase(aName
, eMutex
)
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
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!");
53 #if !defined(DEBUG) || defined(MOZ_NATIVE_SQLITE)
59 sqlite3_mutex_enter(mMutex
);
67 sqlite3_mutex_leave(mMutex
);
71 * Asserts that the current thread owns the mutex.
73 void assertCurrentThreadOwns()
78 * Asserts that the current thread does not own the mutex.
80 void assertNotCurrentThreadOwns()
87 NS_ASSERTION(mMutex
, "No mutex associated with this wrapper!");
89 // While SQLite Mutexes may be recursive, in our own code we do not want to
90 // treat them as such.
93 sqlite3_mutex_enter(mMutex
);
94 Acquire(); // Call is protected by us holding the mutex.
99 NS_ASSERTION(mMutex
, "No mutex associated with this wrapper!");
101 // While SQLite Mutexes may be recursive, in our own code we do not want to
102 // treat them as such.
103 Release(); // Call is protected by us holding the mutex.
104 sqlite3_mutex_leave(mMutex
);
107 void assertCurrentThreadOwns()
109 NS_ASSERTION(mMutex
, "No mutex associated with this wrapper!");
110 NS_ASSERTION(sqlite3_mutex_held(mMutex
),
111 "Mutex is not held, but we expect it to be!");
114 void assertNotCurrentThreadOwns()
116 NS_ASSERTION(mMutex
, "No mutex associated with this wrapper!");
117 NS_ASSERTION(sqlite3_mutex_notheld(mMutex
),
118 "Mutex is held, but we expect it to not be!");
120 #endif // ifndef DEBUG
123 sqlite3_mutex
*mMutex
;
127 * Automatically acquires the mutex when it enters scope, and releases it when
130 class MOZ_STACK_CLASS SQLiteMutexAutoLock
133 explicit SQLiteMutexAutoLock(SQLiteMutex
&aMutex
)
139 ~SQLiteMutexAutoLock()
149 * Automatically releases the mutex when it enters scope, and acquires it when
152 class MOZ_STACK_CLASS SQLiteMutexAutoUnlock
155 explicit SQLiteMutexAutoUnlock(SQLiteMutex
&aMutex
)
161 ~SQLiteMutexAutoUnlock()
170 } // namespace storage
171 } // namespace mozilla
173 #endif // mozilla_storage_SQLiteMutex_h_