\doxysection{Lock\+Step.\+hpp}
\hypertarget{LockStep_8hpp_source}{}\label{LockStep_8hpp_source}\index{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/UI/LockStep.hpp@{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/UI/LockStep.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ }
\DoxyCodeLine{00002\ \textcolor{preprocessor}{\#ifndef\ GYM\_PCB\_UI\_LOCKSTEP\_H}}
\DoxyCodeLine{00003\ \textcolor{preprocessor}{\#define\ GYM\_PCB\_UI\_LOCKSTEP\_H}}
\DoxyCodeLine{00004\ }
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ <mutex>}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#include\ <condition\_variable>}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ \textcolor{keyword}{class\ }StepLock}
\DoxyCodeLine{00009\ \{}
\DoxyCodeLine{00010\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00011\ \ \ \ \ StepLock(uint\ granularity\ =\ 0)\ :\ mGranularity(granularity)\ \{\ \}}
\DoxyCodeLine{00012\ \ \ \ \ \textcolor{keywordtype}{void}\ setGranularity(uint\ n)\ \{\ \textcolor{keywordflow}{if}\ (n\ ==\ 0\ \&\&\ mGranularity\ !=\ 0)\ signal();\ mGranularity\ =\ n;\ \}}
\DoxyCodeLine{00013\ \ \ \ \ uint\ getGranularity()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ mGranularity;\ \}}
\DoxyCodeLine{00014\ \ \ \ \ \textcolor{keywordtype}{void}\ wait(uint\ granularity\ =\ 1);}
\DoxyCodeLine{00015\ \ \ \ \ \textcolor{keywordtype}{void}\ signal();}
\DoxyCodeLine{00016\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00017\ \ \ \ \ std::mutex\ mLock;}
\DoxyCodeLine{00018\ \ \ \ \ std::condition\_variable\ mCV;}
\DoxyCodeLine{00019\ \ \ \ \ uint\ mSeq\{0\};}
\DoxyCodeLine{00020\ \ \ \ \ uint\ mGranularity\{0\};}
\DoxyCodeLine{00021\ \};}
\DoxyCodeLine{00022\ }
\DoxyCodeLine{00023\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{void}\ StepLock::wait(uint\ granularity)}
\DoxyCodeLine{00024\ \{}
\DoxyCodeLine{00025\ \ \ \ \ \textcolor{keywordflow}{if}\ (granularity\ <=\ mGranularity)\ \{}
\DoxyCodeLine{00026\ \ \ \ \ \ \ \ \ std::unique\_lock\ lock(mLock);}
\DoxyCodeLine{00027\ \ \ \ \ \ \ \ \ mCV.wait(lock,\ [\textcolor{keyword}{this},\ curr\ =\ mSeq]\{\ \textcolor{keywordflow}{return}\ mSeq\ !=\ curr;\ \});}
\DoxyCodeLine{00028\ \ \ \ \ \}}
\DoxyCodeLine{00029\ \}}
\DoxyCodeLine{00030\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{void}\ StepLock::signal()}
\DoxyCodeLine{00031\ \{}
\DoxyCodeLine{00032\ \ \ \ \ \textcolor{keywordflow}{if}\ (mGranularity)\ \{}
\DoxyCodeLine{00033\ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{00034\ \ \ \ \ \ \ \ \ std::lock\_guard\ lock(mLock);}
\DoxyCodeLine{00035\ \ \ \ \ \ \ \ \ mSeq++;}
\DoxyCodeLine{00036\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00037\ \ \ \ \ \ \ \ \ mCV.notify\_one();}
\DoxyCodeLine{00038\ \ \ \ \ \}}
\DoxyCodeLine{00039\ \}}
\DoxyCodeLine{00040\ }
\DoxyCodeLine{00041\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ GYM\_PCB\_UI\_LOCKSTEP\_H}}

\end{DoxyCode}
