Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_CRITICALSECTION_H_
7 : #define MOZILLA_GFX_CRITICALSECTION_H_
8 :
9 : #ifdef WIN32
10 : #include <windows.h>
11 : #else
12 : #include <pthread.h>
13 : #include "mozilla/DebugOnly.h"
14 : #endif
15 :
16 : namespace mozilla {
17 : namespace gfx {
18 :
19 : #ifdef WIN32
20 :
21 : class CriticalSection {
22 : public:
23 : CriticalSection() { ::InitializeCriticalSection(&mCriticalSection); }
24 :
25 : ~CriticalSection() { ::DeleteCriticalSection(&mCriticalSection); }
26 :
27 : void Enter() { ::EnterCriticalSection(&mCriticalSection); }
28 :
29 : void Leave() { ::LeaveCriticalSection(&mCriticalSection); }
30 :
31 : protected:
32 : CRITICAL_SECTION mCriticalSection;
33 : };
34 :
35 : #else
36 : // posix
37 :
38 : class PosixCondvar;
39 : class CriticalSection {
40 : public:
41 9 : CriticalSection() {
42 18 : DebugOnly<int> err = pthread_mutex_init(&mMutex, nullptr);
43 9 : MOZ_ASSERT(!err);
44 9 : }
45 :
46 12 : ~CriticalSection() {
47 12 : DebugOnly<int> err = pthread_mutex_destroy(&mMutex);
48 6 : MOZ_ASSERT(!err);
49 6 : }
50 :
51 0 : void Enter() {
52 0 : DebugOnly<int> err = pthread_mutex_lock(&mMutex);
53 0 : MOZ_ASSERT(!err);
54 0 : }
55 :
56 0 : void Leave() {
57 0 : DebugOnly<int> err = pthread_mutex_unlock(&mMutex);
58 0 : MOZ_ASSERT(!err);
59 0 : }
60 :
61 : protected:
62 : pthread_mutex_t mMutex;
63 : friend class PosixCondVar;
64 : };
65 :
66 : #endif
67 :
68 : /// RAII helper.
69 : struct CriticalSectionAutoEnter {
70 0 : explicit CriticalSectionAutoEnter(CriticalSection* aSection) : mSection(aSection) { mSection->Enter(); }
71 0 : ~CriticalSectionAutoEnter() { mSection->Leave(); }
72 : protected:
73 : CriticalSection* mSection;
74 : };
75 :
76 :
77 : } // namespace
78 : } // namespace
79 :
80 : #endif
|