File: ThreadSync.h

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (158 lines) | stat: -rw-r--r-- 3,167 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once

#include <CommonWindows.h>

#if defined(WITH_THREADS) || defined(DOXYGEN_GENERATING_OUTPUT)

#if !defined(DEATH_TARGET_WINDOWS)
#	include <pthread.h>
#endif

namespace nCine
{
	/// Mutex for threads synchronization
	class Mutex
	{
		friend class CondVariable;

	public:
		Mutex();
		~Mutex();

		Mutex(const Mutex&) = delete;
		Mutex& operator=(const Mutex&) = delete;

		void Lock();
		void Unlock();
		int TryLock();

#if defined(WITH_TRACY)
		inline int try_lock() {
			return TryLock();
		}
#endif

	private:
#if defined(DEATH_TARGET_WINDOWS)
		CRITICAL_SECTION handle_;
#else
		pthread_mutex_t mutex_;
#endif
	};

	/// Condition variable for threads synchronization
	/*! Windows version based on the <em>TinyThread++</em> library implementation.
	 *  More info at: http://www.cs.wustl.edu/~schmidt/win32-cv-1.html */
	class CondVariable
	{
	public:
		CondVariable();
		~CondVariable();

		CondVariable(const CondVariable&) = delete;
		CondVariable& operator=(const CondVariable&) = delete;

		void Wait(Mutex& mutex);
		void Signal();
		void Broadcast();

	private:
#if defined(DEATH_TARGET_WINDOWS)
		HANDLE events_[2];
		unsigned int waitersCount_;
		CRITICAL_SECTION waitersCountLock_;

		void WaitEvents();
#else
		pthread_cond_t cond_;
#endif
	};

	/// Read/write lock for threads synchronization
	class ReadWriteLock
	{
	public:
		ReadWriteLock();
		~ReadWriteLock();

		ReadWriteLock(const ReadWriteLock&) = delete;
		ReadWriteLock& operator=(const ReadWriteLock&) = delete;

		inline void EnterReadLock() {
#if defined(DEATH_TARGET_WINDOWS)
			::AcquireSRWLockShared(&rwlock_);
#else
			pthread_rwlock_rdlock(&rwlock_);
#endif
		}
		inline void EnterWriteLock() {
#if defined(DEATH_TARGET_WINDOWS)
			::AcquireSRWLockExclusive(&rwlock_);
#else
			pthread_rwlock_wrlock(&rwlock_);
#endif
		}
		inline int TryEnterReadLock() {
#if defined(DEATH_TARGET_WINDOWS)
			return ::TryAcquireSRWLockShared(&rwlock_);
#else
			return pthread_rwlock_tryrdlock(&rwlock_);
#endif
		}
		inline int TryEnterWriteLock() {
#if defined(DEATH_TARGET_WINDOWS)
			return ::TryAcquireSRWLockExclusive(&rwlock_);
#else
			return pthread_rwlock_trywrlock(&rwlock_);
#endif
		}
		inline void ExitReadLock() {
#if defined(DEATH_TARGET_WINDOWS)
			::ReleaseSRWLockShared(&rwlock_);
#else
			pthread_rwlock_unlock(&rwlock_);
#endif
		}
		inline void ExitWriteLock() {
#if defined(DEATH_TARGET_WINDOWS)
			::ReleaseSRWLockExclusive(&rwlock_);
#else
			pthread_rwlock_unlock(&rwlock_);
#endif
		}

	private:
#if defined(DEATH_TARGET_WINDOWS)
		SRWLOCK rwlock_;
#else
		pthread_rwlock_t rwlock_;
#endif
	};

#if !defined(DEATH_TARGET_ANDROID) && !defined(DEATH_TARGET_APPLE) && !defined(DEATH_TARGET_WINDOWS)

	/// Barrier for threads synchronization
	class Barrier
	{
	public:
		/// Creates a barrier for the specified amount of waiting threads
		explicit Barrier(std::uint32_t count);
		~Barrier();

		Barrier(const Barrier&) = delete;
		Barrier& operator=(const Barrier&) = delete;

		/// The calling thread waits at the barrier
		inline std::int32_t Wait() {
			return pthread_barrier_wait(&barrier_);
		}

	private:
		pthread_barrier_t barrier_;
	};

#endif

}

#endif