RavEngine
Loading...
Searching...
No Matches
concurrentqueue_internal_debug.h
1#pragma once
2
3//#define MCDBGQ_TRACKMEM 1
4//#define MCDBGQ_NOLOCKFREE_FREELIST 1
5//#define MCDBGQ_USEDEBUGFREELIST 1
6//#define MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX 1
7//#define MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH 1
8
9#if defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__)
10#define WIN32_LEAN_AND_MEAN
11#include <windows.h>
12namespace moodycamel { namespace debug {
13 struct DebugMutex {
14 DebugMutex() { InitializeCriticalSectionAndSpinCount(&cs, 0x400); }
15 ~DebugMutex() { DeleteCriticalSection(&cs); }
16
17 void lock() { EnterCriticalSection(&cs); }
18 void unlock() { LeaveCriticalSection(&cs); }
19
20 private:
21 CRITICAL_SECTION cs;
22 };
23} }
24#else
25#include <mutex>
26namespace moodycamel { namespace debug {
27 struct DebugMutex {
28 void lock() { m.lock(); }
29 void unlock() { m.unlock(); }
30
31 private:
32 std::mutex m;
33 };
34} }
35#define
36#endif
37
38namespace moodycamel { namespace debug {
39 struct DebugLock {
40 explicit DebugLock(DebugMutex& mutex)
41 : mutex(mutex)
42 {
43 mutex.lock();
44 }
45
46 ~DebugLock()
47 {
48 mutex.unlock();
49 }
50
51 private:
52 DebugMutex& mutex;
53 };
54
55
56 template<typename N>
57 struct DebugFreeList {
58 DebugFreeList() : head(nullptr) { }
59 DebugFreeList(DebugFreeList&& other) : head(other.head) { other.head = nullptr; }
60 void swap(DebugFreeList& other) { std::swap(head, other.head); }
61
62 inline void add(N* node)
63 {
64 DebugLock lock(mutex);
65 node->freeListNext = head;
66 head = node;
67 }
68
69 inline N* try_get()
70 {
71 DebugLock lock(mutex);
72 if (head == nullptr) {
73 return nullptr;
74 }
75
76 auto prevHead = head;
77 head = head->freeListNext;
78 return prevHead;
79 }
80
81 N* head_unsafe() const { return head; }
82
83 private:
84 N* head;
85 DebugMutex mutex;
86 };
87} }
Definition concurrentqueue_internal_debug.h:27