SimpleMutex.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. ///
  12. #include "SimpleMutex.h"
  13. #include "RakAssert.h"
  14. using namespace RakNet;
  15. SimpleMutex::SimpleMutex() //: isInitialized(false)
  16. {
  17. // Prior implementation of Initializing in Lock() was not threadsafe
  18. Init();
  19. }
  20. SimpleMutex::~SimpleMutex()
  21. {
  22. // if (isInitialized==false)
  23. // return;
  24. #ifdef _WIN32
  25. // CloseHandle(hMutex);
  26. DeleteCriticalSection(&criticalSection);
  27. #else
  28. pthread_mutex_destroy(&hMutex);
  29. #endif
  30. }
  31. #ifdef _WIN32
  32. #ifdef _DEBUG
  33. #include <stdio.h>
  34. #endif
  35. #endif
  36. void SimpleMutex::Lock(void)
  37. {
  38. // if (isInitialized==false)
  39. // Init();
  40. #ifdef _WIN32
  41. /*
  42. DWORD d = WaitForSingleObject(hMutex, INFINITE);
  43. #ifdef _DEBUG
  44. if (d==WAIT_FAILED)
  45. {
  46. LPVOID messageBuffer;
  47. FormatMessage(
  48. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  49. FORMAT_MESSAGE_FROM_SYSTEM |
  50. FORMAT_MESSAGE_IGNORE_INSERTS,
  51. NULL,
  52. GetLastError(),
  53. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  54. (LPTSTR) &messageBuffer,
  55. 0,
  56. NULL
  57. );
  58. // Process any inserts in messageBuffer.
  59. // ...
  60. // Display the string.
  61. //MessageBox( NULL, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION );
  62. RAKNET_DEBUG_PRINTF("SimpleMutex error: %s", messageBuffer);
  63. // Free the buffer.
  64. LocalFree( messageBuffer );
  65. }
  66. RakAssert(d==WAIT_OBJECT_0);
  67. */
  68. EnterCriticalSection(&criticalSection);
  69. #else
  70. int error = pthread_mutex_lock(&hMutex);
  71. (void) error;
  72. RakAssert(error==0);
  73. #endif
  74. }
  75. void SimpleMutex::Unlock(void)
  76. {
  77. // if (isInitialized==false)
  78. // return;
  79. #ifdef _WIN32
  80. // ReleaseMutex(hMutex);
  81. LeaveCriticalSection(&criticalSection);
  82. #else
  83. int error = pthread_mutex_unlock(&hMutex);
  84. (void) error;
  85. RakAssert(error==0);
  86. #endif
  87. }
  88. void SimpleMutex::Init(void)
  89. {
  90. #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
  91. InitializeCriticalSectionEx(&criticalSection,0,CRITICAL_SECTION_NO_DEBUG_INFO);
  92. #elif defined(_WIN32)
  93. // hMutex = CreateMutex(NULL, FALSE, 0);
  94. // RakAssert(hMutex);
  95. InitializeCriticalSection(&criticalSection);
  96. #else
  97. int error = pthread_mutex_init(&hMutex, 0);
  98. (void) error;
  99. RakAssert(error==0);
  100. #endif
  101. // isInitialized=true;
  102. }
粤ICP备19079148号