Skip to content

Commit 0e9183d

Browse files
Aidan63Aidan LeeApprentice-Alchemist
authored
C++11 std mutex and condition variable (#1298)
* Add new mutex and condition types * Update c functions to use new impl * recursive mutex * Update src/hx/thread/ConditionVariable.cpp Co-authored-by: Zeta <53486764+Apprentice-Alchemist@users.noreply.github.com> * Non recursive mutex for condition variable * Remove timed wait which isn't actually used anywhere * Revert "Remove timed wait which isn't actually used anywhere" This reverts commit 63ea825. * Use an atomic_int for the thread id * Update telemetry implementations to use std::mutex * std::mutex for __hxcpp_field_to_id * std::mutex for profiler * std::mutex for debugging * std::mutex for gc * try recursive mutex for telemetry * Revert "std::mutex for gc" This reverts commit 33ea04d. * std::mutex for finaliser and root protection * std::mutex for large object heap * recursive mutex for special object and state change locks * pointers to condition variables * Use wait overload instead of manual while loops * non recursive mutex for thread pool lock * non recursive mutex for special object lock * non recursive mutex for state change lock * Remove function not used in haxe --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com> Co-authored-by: Zeta <53486764+Apprentice-Alchemist@users.noreply.github.com>
1 parent dc27a8d commit 0e9183d

File tree

13 files changed

+393
-483
lines changed

13 files changed

+393
-483
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
HX_DECLARE_CLASS2(hx, thread, ConditionVariable)
8+
9+
namespace hx
10+
{
11+
namespace thread
12+
{
13+
struct ConditionVariable_obj : public hx::Object
14+
{
15+
ConditionVariable_obj();
16+
17+
void acquire();
18+
bool tryAcquire();
19+
void release();
20+
void wait();
21+
void signal();
22+
void broadcast();
23+
24+
private:
25+
struct Impl;
26+
27+
Impl* impl;
28+
};
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
HX_DECLARE_CLASS2(hx, thread, RecursiveMutex)
8+
9+
namespace hx
10+
{
11+
namespace thread
12+
{
13+
struct RecursiveMutex_obj : public hx::Object
14+
{
15+
RecursiveMutex_obj();
16+
17+
void acquire();
18+
void release();
19+
bool tryAcquire();
20+
21+
private:
22+
struct Impl;
23+
24+
Impl* impl;
25+
};
26+
}
27+
}

src/hx/Debug.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <hx/Telemetry.h>
99
#include <hx/Unordered.h>
1010
#include <hx/OS.h>
11+
#include <mutex>
1112

1213

1314
#if defined(HXCPP_CATCH_SEGV) && !defined(_MSC_VER)
@@ -45,7 +46,7 @@ namespace hx
4546
const char* EXTERN_CLASS_NAME = "extern";
4647

4748
#ifdef HXCPP_STACK_IDS
48-
static HxMutex sStackMapMutex;
49+
static std::mutex sStackMapMutex;
4950
typedef UnorderedMap<int, StackContext *> StackMap;
5051
static StackMap sStackMap;
5152
#endif
@@ -245,9 +246,10 @@ void StackContext::onThreadAttach()
245246
#ifdef HXCPP_STACK_IDS
246247
mThreadId = __hxcpp_GetCurrentThreadNumber();
247248

248-
sStackMapMutex.Lock();
249-
sStackMap[mThreadId] = this;
250-
sStackMapMutex.Unlock();
249+
{
250+
std::lock_guard<std::mutex> guard(sStackMapMutex);
251+
sStackMap[mThreadId] = this;
252+
}
251253
#endif
252254

253255
#ifdef HXCPP_DEBUGGER
@@ -302,9 +304,10 @@ void StackContext::onThreadDetach()
302304
#endif
303305

304306
#ifdef HXCPP_STACK_IDS
305-
sStackMapMutex.Lock();
306-
sStackMap.erase(mThreadId);
307-
sStackMapMutex.Unlock();
307+
{
308+
std::lock_guard<std::mutex> guard(sStackMapMutex);
309+
sStackMap.erase(mThreadId);
310+
}
308311
mThreadId = 0;
309312
#endif
310313

@@ -317,18 +320,17 @@ void StackContext::onThreadDetach()
317320
void StackContext::getAllStackIds( QuickVec<int> &outIds )
318321
{
319322
outIds.clear();
320-
sStackMapMutex.Lock();
323+
324+
std::lock_guard<std::mutex> guard(sStackMapMutex);
325+
321326
for(StackMap::iterator i=sStackMap.begin(); i!=sStackMap.end(); ++i)
322327
outIds.push(i->first);
323-
sStackMapMutex.Unlock();
324328
}
325329

326330
StackContext *StackContext::getStackForId(int id)
327331
{
328-
sStackMapMutex.Lock();
329-
StackContext *result = sStackMap[id];
330-
sStackMapMutex.Unlock();
331-
return result;
332+
std::lock_guard<std::mutex> guard(sStackMapMutex);
333+
return sStackMap[id];
332334
}
333335
#endif
334336

0 commit comments

Comments
 (0)