SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleMutex.cpp
Go to the documentation of this file.
1 /*
2  * Original work: 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  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
18 
19 
20 
21 #include "slikenet/SimpleMutex.h"
22 #include "slikenet/assert.h"
23 
24 using namespace SLNet;
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 SimpleMutex::SimpleMutex() //: isInitialized(false)
62 {
63 
64 
65 
66 
67 
68 
69 
70  // Prior implementation of Initializing in Lock() was not threadsafe
71  Init();
72 }
73 
75 {
76 // if (isInitialized==false)
77 // return;
78 #ifdef _WIN32
79  // CloseHandle(hMutex);
80  DeleteCriticalSection(&criticalSection);
81 
82 
83 
84 
85 
86 
87 #else
88  pthread_mutex_destroy(&hMutex);
89 #endif
90 
91 
92 
93 
94 
95 
96 
97 }
98 
99 #ifdef _WIN32
100 #ifdef _DEBUG
101 #include <stdio.h>
102 #endif
103 #endif
104 
106 {
107 // if (isInitialized==false)
108 // Init();
109 
110 #ifdef _WIN32
111  /*
112  DWORD d = WaitForSingleObject(hMutex, INFINITE);
113  #ifdef _DEBUG
114  if (d==WAIT_FAILED)
115  {
116  LPVOID messageBuffer;
117  FormatMessage(
118  FORMAT_MESSAGE_ALLOCATE_BUFFER |
119  FORMAT_MESSAGE_FROM_SYSTEM |
120  FORMAT_MESSAGE_IGNORE_INSERTS,
121  NULL,
122  GetLastError(),
123  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
124  (LPTSTR) &messageBuffer,
125  0,
126  NULL
127  );
128  // Process any inserts in messageBuffer.
129  // ...
130  // Display the string.
131  //MessageBox( NULL, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION );
132  RAKNET_DEBUG_PRINTF("SimpleMutex error: %s", messageBuffer);
133  // Free the buffer.
134  LocalFree( messageBuffer );
135 
136  }
137 
138  RakAssert(d==WAIT_OBJECT_0);
139  */
140  EnterCriticalSection(&criticalSection);
141 
142 
143 
144 
145 
146 
147 #else
148  int error = pthread_mutex_lock(&hMutex);
149  (void) error;
150  RakAssert(error==0);
151 #endif
152 }
153 
155 {
156 // if (isInitialized==false)
157 // return;
158 #ifdef _WIN32
159  // ReleaseMutex(hMutex);
160  LeaveCriticalSection(&criticalSection);
161 
162 
163 
164 
165 
166 
167 #else
168  int error = pthread_mutex_unlock(&hMutex);
169  (void) error;
170  RakAssert(error==0);
171 #endif
172 }
173 
174 void SimpleMutex::Init(void)
175 {
176 #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
177  InitializeCriticalSectionEx(&criticalSection,0,CRITICAL_SECTION_NO_DEBUG_INFO);
178 #elif defined(_WIN32)
179  // hMutex = CreateMutex(NULL, FALSE, 0);
180  // RakAssert(hMutex);
181  InitializeCriticalSection(&criticalSection);
182 
183 
184 
185 
186 
187 
188 
189 
190 #else
191  int error = pthread_mutex_init(&hMutex, 0);
192  (void) error;
193  RakAssert(error==0);
194 #endif
195 // isInitialized=true;
196 }