1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 nsUpdateMutex_h__
8 #define nsUpdateMutex_h__
10 #include "nsIUpdateService.h"
11 #include "nsProfileLock.h"
12 #include "mozilla/StaticMutex.h"
15 * A primitive object type suitable for acquiring the update mutex. It is
16 * composed of two parts:
17 * - a nsProfileLock taken on the update directory, to ensure that if two
18 * instances running from the same application path try to acquire the
19 * update mutex simultaneously, only one of them succeeds;
20 * - a StaticMutex, to ensure that even within the same instance of the
21 * application, it is never possible to successfully acquire two
22 * UpdateMutexImpl objects simultaneously.
24 * While the second part is not strictly required, it makes reasoning about
25 * these objects easier, and it helps us simulate an acquisition coming from
26 * another instance in tests.
28 * Contrary to a nsIUpdateMutex object, an UpdateMutexImpl object does not
29 * keep track of whether it is currently locked or unlocked. Therefore, it is
30 * the responsibility of the caller to guarantee the following:
31 * - a call to Unlock() must only occur after a matching successful call to
33 * - no second call to TryLock() should ever occur after a successful first
34 * call to TryLock(), unless a call to Unlock() occured in the middle.
36 class MOZ_CAPABILITY("mutex") UpdateMutexImpl
{
38 [[nodiscard
]] bool TryLock() MOZ_TRY_ACQUIRE(true);
40 void Unlock() MOZ_CAPABILITY_RELEASE();
43 static mozilla::StaticMutex sInProcessMutex
;
45 nsProfileLock mCrossProcessLock
;
49 * An XPCOM wrapper for the UpdateMutexImpl primitive type, achieving the same
50 * goals but through a safe XPCOM-compatible nsIUpdateMutex interface.
52 * Contrary to UpdateMutexImpl objects, nsUpdateMutex objects track whether
53 * they are currently locked or unlocked. It is therefore always safe to call
54 * TryLock() or Unlock() on a nsUpdateMutex object.
56 * See nsIUpdateMutex in nsUpdateService.idl for more details.
58 class nsUpdateMutex final
: public nsIUpdateMutex
{
60 explicit nsUpdateMutex() = default;
62 NS_DECL_THREADSAFE_ISUPPORTS
63 NS_DECL_NSIUPDATEMUTEX
66 UpdateMutexImpl mUpdateMutexImpl
;
69 virtual ~nsUpdateMutex() {
76 #endif // nsUpdateMutex_h__