summaryrefslogtreecommitdiff
path: root/Runtime/Threads/SimpleLock.h
blob: ace47aa7a5ca4065396c7211a1d7b2c40bfa0747 (plain)
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
#ifndef SIMPLE_LOCK_H
#define SIMPLE_LOCK_H

#if SUPPORT_THREADS

#include "AtomicOps.h"
#include "Semaphore.h"

// Simple, non-recursive mutual exclusion lock. Efficient when there is low contention.
// First tries to attain lock using atomic ops, then waits for lock using semaphores.
// Same idea as described here: http://preshing.com/20120226/roll-your-own-lightweight-mutex

class SimpleLock : public NonCopyable
{
public:
	SimpleLock() : m_Count(0) {}

	class AutoLock : public NonCopyable
	{
	public:
		AutoLock( SimpleLock& lock ) : m_Lock(lock)
		{
			m_Lock.Lock();
		}

		~AutoLock()
		{
			m_Lock.Unlock();
		}

	private:
		SimpleLock& m_Lock;
	};

	void Lock()
	{
		if (AtomicIncrement(&m_Count) != 1)
			m_Semaphore.WaitForSignal();
	}

	void Unlock()
	{
		if (AtomicDecrement(&m_Count) != 0)
			m_Semaphore.Signal();
	}

private:
	volatile int m_Count;
	Semaphore m_Semaphore;
};

#endif // SUPPORT_THREADS
#endif