mutex.hpp #3

  • //
  • guest/
  • ShadauxCat/
  • Sprawl/
  • Mainline/
  • threading/
  • mutex.hpp
  • View
  • Commits
  • Open Download .zip Download (2 KB)
#pragma once

#ifdef _WIN32
#	include <Windows.h>
typedef	SRWLOCK MutexType;
#else
#	include <pthread.h>
typedef pthread_mutex_t MutexType;
#endif

#include "../common/noncopyable.hpp"

namespace sprawl
{
	namespace threading
	{
		class Mutex;
		class ScopedLock;
		class SharedLock;
	}
}

class sprawl::threading::Mutex : public sprawl::noncopyable
{
public:
	void Lock();
	bool TryLock();
	void Unlock();

	Mutex();
	~Mutex();
	Mutex(Mutex&& other)
		: m_mutexImpl(other.m_mutexImpl)
	{
		other.m_mutexImpl = MutexType();
	}

	MutexType& GetNativeMutex() { return m_mutexImpl; }
private:
	MutexType m_mutexImpl;
};



class sprawl::threading::ScopedLock
{
public:
	ScopedLock(Mutex& mutex);
	~ScopedLock();
private:
	Mutex& m_mutex;
};

inline sprawl::threading::ScopedLock::ScopedLock(Mutex& mutex)
	: m_mutex(mutex)
{
	m_mutex.Lock();
}

inline sprawl::threading::ScopedLock::~ScopedLock()
{
	m_mutex.Unlock();
}



enum class LockType
{
	Immediate,
	TryLock,
	Deferred
};

class sprawl::threading::SharedLock
{
public:
	SharedLock(Mutex& mutex, LockType type = LockType::Immediate);
	SharedLock();
	SharedLock(SharedLock&& other);
	~SharedLock();

	void Lock() { m_mutex->Lock(); }
	bool TryLock() { return m_mutex->TryLock(); }
	void Unlock() { m_mutex->Unlock(); }

	bool IsOwned() { return m_owned; }

	void Release() { m_mutex = nullptr; m_owned = false; }

	SharedLock& operator=(SharedLock&& other);

	Mutex* GetMutex() { return m_mutex; }
private:
	Mutex* m_mutex;
	bool m_owned;
};

inline sprawl::threading::SharedLock::SharedLock(Mutex& mutex, LockType type)
	: m_mutex(&mutex)
	, m_owned(false)
{
	switch(type)
	{
		case LockType::Immediate:
			m_mutex->Lock();
			m_owned = true;
			break;
		case LockType::TryLock:
			m_owned = m_mutex->TryLock();
			break;
		case LockType::Deferred:
		default:
			break;
	}
}

inline sprawl::threading::SharedLock::SharedLock()
	: m_mutex(nullptr)
	, m_owned(false)
{
	//
}

inline sprawl::threading::SharedLock::SharedLock(sprawl::threading::SharedLock&& other)
	: m_mutex(other.m_mutex)
	, m_owned(other.m_owned)
{
	other.m_mutex = nullptr;
	other.m_owned = false;
}

inline sprawl::threading::SharedLock::~SharedLock()
{
	if(m_owned)
	{
		m_mutex->Unlock();
	}
}

inline sprawl::threading::SharedLock& sprawl::threading::SharedLock::operator=(sprawl::threading::SharedLock&& other)
{
	m_mutex = other.m_mutex;
	m_owned = other.m_owned;
	other.m_mutex = nullptr;
	other.m_owned = false;
	return *this;
}

#ifdef _WIN32
#	include "mutex_windows.inl"
#else
#	include "mutex_linux.inl"
#endif
# Change User Description Committed
#3 16225 ShadauxCat - Renamed OpaquePtr to OpaqueType, which is more correct as it isn't a pointer.
- Added alignment restriction to OpaqueType
- Changed Mutex implementation on Windows to use faster SRWLOCK instead of CRITICAL_SECTION (cannot mirror this change on Linux because pthread_cond_wait can't accept a pthread_rwlock_t)

#review-16226
#2 14833 ShadauxCat First checkin of logging module.

Also fixes the following issues:

-Added UpperBound() and LowerBound() to BinaryTree and created appropriate unit tests
-Added Sync() to ThreadManager to force it to run all tasks to completion and not return until it has no tasks left
-Fixed a bug in String::format() where a non-numeric value inside {} would be treated as an empty {}; it now simply prints whatever the value was. (i.e., "{blah}".format(foo) simply returns "{blah}")
-Added Reset() to sprawl::StringBuilder
-Disabled the switch-enum warning flag in gcc because it's stupid and ridiculous that a default case doesn't shut it up
-Made sprawl::Mutex movable. This may turn out to be a bad idea but it enabled keeping them in a map.
-Fixed a name collission between HashMap and BinaryTree; both defined sprawl::collections::detail::UnderlyingType and ::MethodType. Prefixed the ones in BinaryTree with "Tree". This isn't the best solution, but it works for now.

#review-14834
#1 12508 ShadauxCat -Added threading library.
Currently only functional for Linux; Windows will fail to link. (I will fix this soon.)
-Fixed missing move and copy constructors in List and ForwardList
-Fixed broken move constructor in HashMap
-Fixed missing const get() in HashMap
-Fixed broken operator-> in ListIterator
-Added sprawl::noncopyable
-Added sketch headers for filesystem library
-Made StringLiteral hashable, added special hashes for pointers and integers in murmur3
-Fixed compiler warning in async_network
-Updated memory allocators to use new threading library for mutexes
-Added accessibility to sprawl::StringLiteral to be able toa ccess its pointer and length and perform pointer comparisons

#review-12504