src: added ReaderWriterLock, helpers and Semaphore

This commit is contained in:
malte_langkabel
2016-10-10 17:37:38 +02:00
parent 072cccf852
commit 86f652d221
10 changed files with 199 additions and 2 deletions
+9
View File
@@ -334,6 +334,15 @@ add_files(
utility/solution/SolutionParserUtility.h
utility/solution/SolutionParserVisualStudio.cpp
utility/solution/SolutionParserVisualStudio.h
utility/synchronization/ReaderWriterLock.cpp
utility/synchronization/ReaderWriterLock.h
utility/synchronization/ScopedReaderLock.cpp
utility/synchronization/ScopedReaderLock.h
utility/synchronization/ScopedWriterLock.cpp
utility/synchronization/ScopedWriterLock.h
utility/synchronization/Semaphore.cpp
utility/synchronization/Semaphore.h
utility/text/TextAccess.cpp
utility/text/TextAccess.h
+1 -2
View File
@@ -13,7 +13,6 @@
#include <Windows.h>
#endif // WINDOWS
class AppPath
{
public:
@@ -26,4 +25,4 @@ private:
static std::string m_appPath;
};
#endif // APP_PATH_H
#endif // APP_PATH_H
@@ -0,0 +1,54 @@
#include "utility/synchronization/ReaderWriterLock.h"
ReaderWriterLock::ReaderWriterLock()
: m_readerCount(0)
, m_allowedWriters(1)
, m_allowedReaders(1)
{
}
ReaderWriterLock::ReaderWriterLock(const ReaderWriterLock &lock)
: m_readerCount(0)
, m_allowedWriters(1)
, m_allowedReaders(1)
{
}
ReaderWriterLock::~ReaderWriterLock()
{
}
void ReaderWriterLock::readerLock()
{
m_allowedReaders.wait();
m_allowedReaders.post();
std::lock_guard<std::mutex> lock(m_readerCountMutex);
m_readerCount++;
if (m_readerCount == 1)
{
m_allowedWriters.wait();
}
}
void ReaderWriterLock::readerUnlock()
{
std::lock_guard<std::mutex> lock(m_readerCountMutex);
m_readerCount--;
if (m_readerCount == 0)
{
m_allowedWriters.post();
}
}
void ReaderWriterLock::writerLock()
{
m_allowedReaders.wait();
m_allowedWriters.wait();
}
void ReaderWriterLock::writerUnlock()
{
m_allowedWriters.post();
m_allowedReaders.post();
}
@@ -0,0 +1,30 @@
#ifndef READER_WRITER_LOCK_H
#define READER_WRITER_LOCK_H
#include <mutex>
#include "Semaphore.h"
class ReaderWriterLock
{
public:
ReaderWriterLock();
private:
ReaderWriterLock(const ReaderWriterLock &lock);
public:
~ReaderWriterLock();
void readerLock();
void readerUnlock();
void writerLock();
void writerUnlock();
private:
int m_readerCount;
std::mutex m_readerCountMutex;
Semaphore m_allowedReaders, m_allowedWriters;
};
#endif // READER_WRITER_LOCK_H
@@ -0,0 +1,12 @@
#include "utility/synchronization/ScopedReaderLock.h"
ScopedReaderLock::ScopedReaderLock(ReaderWriterLock& lock)
: m_lock(lock)
{
m_lock.readerLock();
}
ScopedReaderLock::~ScopedReaderLock()
{
m_lock.readerUnlock();
}
@@ -0,0 +1,16 @@
#ifndef SCOPED_READER_LOCK_H
#define SCOPED_READER_LOCK_H
#include "utility/synchronization/ReaderWriterLock.h"
class ScopedReaderLock
{
public:
ScopedReaderLock(ReaderWriterLock& lock);
~ScopedReaderLock();
private:
ReaderWriterLock& m_lock;
};
#endif // SCOPED_READER_LOCK_H
@@ -0,0 +1,12 @@
#include "utility/synchronization/ScopedWriterLock.h"
ScopedWriterLock::ScopedWriterLock(ReaderWriterLock& lock)
: m_lock(lock)
{
m_lock.writerLock();
}
ScopedWriterLock::~ScopedWriterLock()
{
m_lock.writerUnlock();
}
@@ -0,0 +1,16 @@
#ifndef SCOPED_WRITER_LOCK_H
#define SCOPED_WRITER_LOCK_H
#include "utility/synchronization/ReaderWriterLock.h"
class ScopedWriterLock
{
public:
ScopedWriterLock(ReaderWriterLock& lock);
~ScopedWriterLock();
private:
ReaderWriterLock& m_lock;
};
#endif // SCOPED_WRITER_LOCK_H
@@ -0,0 +1,27 @@
#include "utility/synchronization/Semaphore.h"
Semaphore::Semaphore(unsigned int counter)
: m_counter(counter)
{
}
Semaphore::~Semaphore()
{
}
void Semaphore::wait()
{
std::unique_lock<std::mutex> lock(m_counterMutex);
while(m_counter == 0)
{
m_conditionVariable.wait(lock);
}
m_counter--;
}
void Semaphore::post()
{
std::unique_lock<std::mutex> lock(m_counterMutex);
m_counter++;
m_conditionVariable.notify_one();
}
@@ -0,0 +1,22 @@
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include <mutex>
#include <condition_variable>
class Semaphore
{
public:
Semaphore(unsigned int counter);
~Semaphore();
void wait();
void post();
private:
unsigned int m_counter;
std::mutex m_counterMutex;
std::condition_variable m_conditionVariable;
};
#endif // SEMAPHORE_H