1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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_GFX_CRITICALSECTION_H_
8 #define MOZILLA_GFX_CRITICALSECTION_H_
14 # include "mozilla/DebugOnly.h"
22 class CriticalSection
{
24 CriticalSection() { ::InitializeCriticalSection(&mCriticalSection
); }
26 ~CriticalSection() { ::DeleteCriticalSection(&mCriticalSection
); }
28 void Enter() { ::EnterCriticalSection(&mCriticalSection
); }
30 void Leave() { ::LeaveCriticalSection(&mCriticalSection
); }
33 CRITICAL_SECTION mCriticalSection
;
40 class CriticalSection
{
43 DebugOnly
<int> err
= pthread_mutex_init(&mMutex
, nullptr);
48 DebugOnly
<int> err
= pthread_mutex_destroy(&mMutex
);
53 DebugOnly
<int> err
= pthread_mutex_lock(&mMutex
);
58 DebugOnly
<int> err
= pthread_mutex_unlock(&mMutex
);
63 pthread_mutex_t mMutex
;
64 friend class PosixCondVar
;
70 struct CriticalSectionAutoEnter final
{
71 explicit CriticalSectionAutoEnter(CriticalSection
* aSection
)
72 : mSection(aSection
) {
75 ~CriticalSectionAutoEnter() { mSection
->Leave(); }
78 CriticalSection
* mSection
;
82 } // namespace mozilla