1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef mozilla_Mutex_h
40 #define mozilla_Mutex_h
44 #include "mozilla/AutoRestore.h"
45 #include "mozilla/BlockingResourceBase.h"
50 // - Mutex, a non-recursive mutex
51 // - MutexAutoLock, an RAII class for ensuring that Mutexes are properly
52 // locked and unlocked
53 // - MutexAutoUnlock, complementary sibling to MutexAutoLock
55 // Using MutexAutoLock/MutexAutoUnlock is MUCH preferred to making bare
56 // calls to Mutex.Lock and Unlock.
63 * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this
64 * mutex within a scope, instead of calling Lock/Unlock directly.
67 class NS_COM_GLUE Mutex
: BlockingResourceBase
72 * @param name A name which can reference this lock
73 * @returns If failure, nsnull
74 * If success, a valid Mutex* which must be destroyed
75 * by Mutex::DestroyMutex()
77 Mutex(const char* name
) :
78 BlockingResourceBase(name
, eMutex
)
82 NS_RUNTIMEABORT("Can't allocate mozilla::Mutex");
91 "improperly constructed Lock or double free");
92 // NSPR does consistency checks for us
93 PR_DestroyLock(mLock
);
117 * AssertCurrentThreadOwns
120 void AssertCurrentThreadOwns () const
125 * AssertNotCurrentThreadOwns
128 void AssertNotCurrentThreadOwns () const
136 void AssertCurrentThreadOwns () const
138 PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mLock
);
141 void AssertNotCurrentThreadOwns () const
146 #endif // ifndef DEBUG
151 Mutex
& operator=(const Mutex
&);
155 friend class CondVar
;
161 * Acquires the Mutex when it enters scope, and releases it when it leaves
164 * MUCH PREFERRED to bare calls to Mutex.Lock and Unlock.
166 class NS_COM_GLUE NS_STACK_CLASS MutexAutoLock
171 * The constructor aquires the given lock. The destructor
174 * @param aLock A valid mozilla::Mutex* returned by
175 * mozilla::Mutex::NewMutex.
177 MutexAutoLock(mozilla::Mutex
& aLock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM
) :
180 MOZILLA_GUARD_OBJECT_NOTIFIER_INIT
;
181 NS_ASSERTION(mLock
, "null mutex");
185 ~MutexAutoLock(void) {
191 MutexAutoLock(MutexAutoLock
&);
192 MutexAutoLock
& operator=(MutexAutoLock
&);
193 static void* operator new(size_t) CPP_THROW_NEW
;
194 static void operator delete(void*);
196 mozilla::Mutex
* mLock
;
197 MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
203 * Releases the Mutex when it enters scope, and re-acquires it when it leaves
206 * MUCH PREFERRED to bare calls to Mutex.Unlock and Lock.
208 class NS_COM_GLUE NS_STACK_CLASS MutexAutoUnlock
211 MutexAutoUnlock(mozilla::Mutex
& aLock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM
) :
214 MOZILLA_GUARD_OBJECT_NOTIFIER_INIT
;
215 NS_ASSERTION(mLock
, "null lock");
226 MutexAutoUnlock(MutexAutoUnlock
&);
227 MutexAutoUnlock
& operator =(MutexAutoUnlock
&);
228 static void* operator new(size_t) CPP_THROW_NEW
;
229 static void operator delete(void*);
231 mozilla::Mutex
* mLock
;
232 MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
236 } // namespace mozilla
239 #endif // ifndef mozilla_Mutex_h