gfxVRMutex.h (1672B)
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/. */ 6 7 #ifndef GFX_VR_MUTEX_H 8 #define GFX_VR_MUTEX_H 9 10 #if defined(XP_WIN) 11 # include "nsTString.h" 12 #endif 13 14 namespace mozilla { 15 namespace gfx { 16 17 #if defined(XP_WIN) 18 class WaitForMutex { 19 public: 20 explicit WaitForMutex(HANDLE handle) : mHandle(handle), mStatus(false) { 21 MOZ_ASSERT(mHandle); 22 23 DWORD dwWaitResult; 24 dwWaitResult = WaitForSingleObject(mHandle, // handle to mutex 25 INFINITE); // no time-out interval 26 27 switch (dwWaitResult) { 28 // The thread got ownership of the mutex 29 case WAIT_OBJECT_0: 30 mStatus = true; 31 break; 32 33 // The thread got ownership of an abandoned mutex 34 // The shmem is in an indeterminate state 35 case WAIT_ABANDONED: 36 mStatus = false; 37 break; 38 default: 39 mStatus = false; 40 break; 41 } 42 } 43 44 ~WaitForMutex() { 45 if (mHandle && !ReleaseMutex(mHandle)) { 46 # ifdef MOZILLA_INTERNAL_API 47 nsAutoCString msg; 48 msg.AppendPrintf("WaitForMutex %p ReleaseMutex error \"%lu\".", mHandle, 49 GetLastError()); 50 NS_WARNING(msg.get()); 51 # endif 52 MOZ_ASSERT(false, "Failed to release mutex."); 53 } 54 } 55 56 bool GetStatus() { return mStatus; } 57 58 private: 59 HANDLE mHandle; 60 bool mStatus; 61 }; 62 #endif 63 64 } // namespace gfx 65 } // namespace mozilla 66 67 #endif /* GFX_VR_MUTEX_H */