| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- \doxysection{sync.\+hpp}
- \label{sync_8hpp_source}\index{C:/Users/yoneda/Documents/Pico\_Projects/tinykernel/tinykernel/include/tinykernel/sync.hpp@{C:/Users/yoneda/Documents/Pico\_Projects/tinykernel/tinykernel/include/tinykernel/sync.hpp}}
- \textbf{ [詳解]}
- \begin{DoxyCode}{0}
- \DoxyCodeLine{00001\ \textcolor{preprocessor}{\#pragma\ once}}
- \DoxyCodeLine{00002\ }
- \DoxyCodeLine{00003\ \textcolor{preprocessor}{\#include\ "{}kernel.hpp"{}}}
- \DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <cstdint>}}
- \DoxyCodeLine{00005\ }
- \DoxyCodeLine{00006\ \textcolor{keyword}{namespace\ }tk\ \{}
- \DoxyCodeLine{00007\ }
- \DoxyCodeLine{00024\ \textcolor{keyword}{class\ }Semaphore\ \{}
- \DoxyCodeLine{00025\ \textcolor{keyword}{public}:}
- \DoxyCodeLine{00031\ \ \ \ \ \textcolor{keyword}{explicit}\ Semaphore(int32\_t\ initial\_count\ =\ 1,\ int32\_t\ max\_count\ =\ 1)}
- \DoxyCodeLine{00032\ \ \ \ \ \ \ \ \ :\ count\_(initial\_count),\ max\_count\_(max\_count)\ \{\}}
- \DoxyCodeLine{00033\ }
- \DoxyCodeLine{00039\ \ \ \ \ \textcolor{keywordtype}{void}\ acquire()\ \{}
- \DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00041\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (count\_\ <=\ 0)\ \{}
- \DoxyCodeLine{00042\ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::current\_task\_slot();}
- \DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ |=\ (task\_mask\_t\{1\}\ <<\ slot);}
- \DoxyCodeLine{00044\ \ \ \ \ \ \ \ \ \ \ \ \ detail::block\_current\_task\_locked();}
- \DoxyCodeLine{00045\ \ \ \ \ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00046\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 他タスクから\ release()\ されて再開したら再チェック}}
- \DoxyCodeLine{00047\ \ \ \ \ \ \ \ \ \ \ \ \ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00048\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00049\ \ \ \ \ \ \ \ \ count\_-\/-\/;}
- \DoxyCodeLine{00050\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00051\ \ \ \ \ \}}
- \DoxyCodeLine{00052\ }
- \DoxyCodeLine{00059\ \ \ \ \ \textcolor{keywordtype}{bool}\ try\_acquire()\ \{}
- \DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ success\ =\ \textcolor{keyword}{false};}
- \DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (count\_\ >\ 0)\ \{}
- \DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ \ \ \ \ count\_-\/-\/;}
- \DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ \ \ \ \ success\ =\ \textcolor{keyword}{true};}
- \DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ success;}
- \DoxyCodeLine{00068\ \ \ \ \ \}}
- \DoxyCodeLine{00069\ }
- \DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordtype}{void}\ release()\ \{}
- \DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (count\_\ <\ max\_count\_)\ \{}
- \DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ \ \ \ \ count\_++;}
- \DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wait\_mask\_\ !=\ 0)\ \{}
- \DoxyCodeLine{00079\ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::find\_highest\_prio\_waiter(wait\_mask\_);}
- \DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (slot\ !=\ 0xFF)\ \{}
- \DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ \&=\ \string~(task\_mask\_t\{1\}\ <<\ slot);}
- \DoxyCodeLine{00082\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ detail::wake\_task\_locked(TaskHandle\{slot\});}
- \DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00085\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00086\ \ \ \ \ \}}
- \DoxyCodeLine{00087\ }
- \DoxyCodeLine{00089\ \ \ \ \ int32\_t\ count()\textcolor{keyword}{\ const\ }\{}
- \DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ int32\_t\ c\ =\ count\_;}
- \DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ c;}
- \DoxyCodeLine{00094\ \ \ \ \ \}}
- \DoxyCodeLine{00095\ }
- \DoxyCodeLine{00096\ \textcolor{keyword}{private}:}
- \DoxyCodeLine{00097\ \ \ \ \ int32\_t\ count\_;}
- \DoxyCodeLine{00098\ \ \ \ \ int32\_t\ max\_count\_;}
- \DoxyCodeLine{00099\ \ \ \ \ task\_mask\_t\ wait\_mask\_\{0\};}
- \DoxyCodeLine{00100\ \};}
- \DoxyCodeLine{00101\ }
- \DoxyCodeLine{00105\ \textcolor{keyword}{using\ }BinarySemaphore\ =\ Semaphore;}
- \DoxyCodeLine{00106\ }
- \DoxyCodeLine{00123\ \textcolor{keyword}{class\ }Mutex\ \{}
- \DoxyCodeLine{00124\ \textcolor{keyword}{public}:}
- \DoxyCodeLine{00125\ \ \ \ \ Mutex()\ =\ \textcolor{keywordflow}{default};}
- \DoxyCodeLine{00126\ }
- \DoxyCodeLine{00132\ \ \ \ \ \textcolor{keywordtype}{void}\ lock()\ \{}
- \DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
- \DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (locked\_)\ \{}
- \DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ |=\ (task\_mask\_t\{1\}\ <<\ my\_slot);}
- \DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \ \ \ \ detail::block\_current\_task\_locked();}
- \DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \ \ \ \ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{true};}
- \DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ my\_slot;}
- \DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00144\ \ \ \ \ \}}
- \DoxyCodeLine{00145\ }
- \DoxyCodeLine{00152\ \ \ \ \ \textcolor{keywordtype}{bool}\ try\_lock()\ \{}
- \DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
- \DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ success\ =\ \textcolor{keyword}{false};}
- \DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!locked\_)\ \{}
- \DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{true};}
- \DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ my\_slot;}
- \DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ \ \ \ \ success\ =\ \textcolor{keyword}{true};}
- \DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ success;}
- \DoxyCodeLine{00163\ \ \ \ \ \}}
- \DoxyCodeLine{00164\ }
- \DoxyCodeLine{00171\ \ \ \ \ \textcolor{keywordtype}{void}\ unlock()\ \{}
- \DoxyCodeLine{00172\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00173\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
- \DoxyCodeLine{00174\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (locked\_\ \&\&\ owner\_slot\_\ ==\ my\_slot)\ \{}
- \DoxyCodeLine{00175\ \ \ \ \ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{false};}
- \DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ 0xFF;}
- \DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wait\_mask\_\ !=\ 0)\ \{}
- \DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::find\_highest\_prio\_waiter(wait\_mask\_);}
- \DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (slot\ !=\ 0xFF)\ \{}
- \DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ \&=\ \string~(task\_mask\_t\{1\}\ <<\ slot);}
- \DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ detail::wake\_task\_locked(TaskHandle\{slot\});}
- \DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \}}
- \DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00186\ \ \ \ \ \}}
- \DoxyCodeLine{00187\ }
- \DoxyCodeLine{00189\ \ \ \ \ \textcolor{keywordtype}{bool}\ is\_locked()\textcolor{keyword}{\ const\ }\{}
- \DoxyCodeLine{00190\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
- \DoxyCodeLine{00191\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ lk\ =\ locked\_;}
- \DoxyCodeLine{00192\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
- \DoxyCodeLine{00193\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ lk;}
- \DoxyCodeLine{00194\ \ \ \ \ \}}
- \DoxyCodeLine{00195\ }
- \DoxyCodeLine{00196\ \textcolor{keyword}{private}:}
- \DoxyCodeLine{00197\ \ \ \ \ \textcolor{keywordtype}{bool}\ locked\_\{\textcolor{keyword}{false}\};}
- \DoxyCodeLine{00198\ \ \ \ \ uint8\_t\ owner\_slot\_\{0xFF\};}
- \DoxyCodeLine{00199\ \ \ \ \ task\_mask\_t\ wait\_mask\_\{0\};}
- \DoxyCodeLine{00200\ \};}
- \DoxyCodeLine{00201\ }
- \DoxyCodeLine{00210\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Lockable>}
- \DoxyCodeLine{00211\ \textcolor{keyword}{class\ }LockGuard\ \{}
- \DoxyCodeLine{00212\ \textcolor{keyword}{public}:}
- \DoxyCodeLine{00217\ \ \ \ \ \textcolor{keyword}{explicit}\ LockGuard(Lockable\&\ m)\ :\ m\_(m)\ \{\ m\_.lock();\ \}}
- \DoxyCodeLine{00218\ }
- \DoxyCodeLine{00220\ \ \ \ \ \string~LockGuard()\ \{\ m\_.unlock();\ \}}
- \DoxyCodeLine{00221\ }
- \DoxyCodeLine{00222\ \ \ \ \ LockGuard(\textcolor{keyword}{const}\ LockGuard\&)\ =\ \textcolor{keyword}{delete};}
- \DoxyCodeLine{00223\ \ \ \ \ LockGuard\&\ operator=(\textcolor{keyword}{const}\ LockGuard\&)\ =\ \textcolor{keyword}{delete};}
- \DoxyCodeLine{00224\ }
- \DoxyCodeLine{00225\ \textcolor{keyword}{private}:}
- \DoxyCodeLine{00226\ \ \ \ \ Lockable\&\ m\_;}
- \DoxyCodeLine{00227\ \};}
- \DoxyCodeLine{00228\ }
- \DoxyCodeLine{00229\ \}\ \ \textcolor{comment}{//\ namespace\ tk}}
- \end{DoxyCode}
|