File: concurrentqueue_internal_debug.h

package info (click to toggle)
concurrentqueue 1.0.4%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,656 kB
  • sloc: cpp: 37,309; makefile: 99; ansic: 67; python: 46; sh: 11
file content (87 lines) | stat: -rw-r--r-- 1,712 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once

//#define MCDBGQ_TRACKMEM 1
//#define MCDBGQ_NOLOCKFREE_FREELIST 1
//#define MCDBGQ_USEDEBUGFREELIST 1
//#define MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX 1
//#define MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH 1

#if defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
namespace moodycamel { namespace debug {
	struct DebugMutex {
		DebugMutex() { InitializeCriticalSectionAndSpinCount(&cs, 0x400); }
		~DebugMutex() { DeleteCriticalSection(&cs); }
		
		void lock() { EnterCriticalSection(&cs); }
		void unlock() { LeaveCriticalSection(&cs); }
		
	private:
		CRITICAL_SECTION cs;
	};
} }
#else
#include <mutex>
namespace moodycamel { namespace debug {
	struct DebugMutex {
		void lock() { m.lock(); }
		void unlock() { m.unlock(); }
		
	private:
		std::mutex m;
	};
} }
#define
#endif

namespace moodycamel { namespace debug {
	struct DebugLock {
		explicit DebugLock(DebugMutex& mutex)
			: mutex(mutex)
		{
			mutex.lock();
		}
		
		~DebugLock()
		{
			mutex.unlock();
		}
		
	private:
		DebugMutex& mutex;
	};
	
	
	template<typename N>
	struct DebugFreeList {
		DebugFreeList() : head(nullptr) { }
		DebugFreeList(DebugFreeList&& other) : head(other.head) { other.head = nullptr; }
		void swap(DebugFreeList& other) { std::swap(head, other.head); }
		
		inline void add(N* node)
		{
			DebugLock lock(mutex);
			node->freeListNext = head;
			head = node;
		}
		
		inline N* try_get()
		{
			DebugLock lock(mutex);
			if (head == nullptr) {
				return nullptr;
			}
			
			auto prevHead = head;
			head = head->freeListNext;
			return prevHead;
		}
		
		N* head_unsafe() const { return head; }
		
	private:
		N* head;
		DebugMutex mutex;
	};
} }