sync_8hpp_source.tex 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. \doxysection{sync.\+hpp}
  2. \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}}
  3. \textbf{ [詳解]}
  4. \begin{DoxyCode}{0}
  5. \DoxyCodeLine{00001\ \textcolor{preprocessor}{\#pragma\ once}}
  6. \DoxyCodeLine{00002\ }
  7. \DoxyCodeLine{00003\ \textcolor{preprocessor}{\#include\ "{}kernel.hpp"{}}}
  8. \DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ <cstdint>}}
  9. \DoxyCodeLine{00005\ }
  10. \DoxyCodeLine{00006\ \textcolor{keyword}{namespace\ }tk\ \{}
  11. \DoxyCodeLine{00007\ }
  12. \DoxyCodeLine{00024\ \textcolor{keyword}{class\ }Semaphore\ \{}
  13. \DoxyCodeLine{00025\ \textcolor{keyword}{public}:}
  14. \DoxyCodeLine{00031\ \ \ \ \ \textcolor{keyword}{explicit}\ Semaphore(int32\_t\ initial\_count\ =\ 1,\ int32\_t\ max\_count\ =\ 1)}
  15. \DoxyCodeLine{00032\ \ \ \ \ \ \ \ \ :\ count\_(initial\_count),\ max\_count\_(max\_count)\ \{\}}
  16. \DoxyCodeLine{00033\ }
  17. \DoxyCodeLine{00039\ \ \ \ \ \textcolor{keywordtype}{void}\ acquire()\ \{}
  18. \DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  19. \DoxyCodeLine{00041\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (count\_\ <=\ 0)\ \{}
  20. \DoxyCodeLine{00042\ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::current\_task\_slot();}
  21. \DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ |=\ (task\_mask\_t\{1\}\ <<\ slot);}
  22. \DoxyCodeLine{00044\ \ \ \ \ \ \ \ \ \ \ \ \ detail::block\_current\_task\_locked();}
  23. \DoxyCodeLine{00045\ \ \ \ \ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  24. \DoxyCodeLine{00046\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ 他タスクから\ release()\ されて再開したら再チェック}}
  25. \DoxyCodeLine{00047\ \ \ \ \ \ \ \ \ \ \ \ \ save\ =\ detail::sched\_lock();}
  26. \DoxyCodeLine{00048\ \ \ \ \ \ \ \ \ \}}
  27. \DoxyCodeLine{00049\ \ \ \ \ \ \ \ \ count\_-\/-\/;}
  28. \DoxyCodeLine{00050\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  29. \DoxyCodeLine{00051\ \ \ \ \ \}}
  30. \DoxyCodeLine{00052\ }
  31. \DoxyCodeLine{00059\ \ \ \ \ \textcolor{keywordtype}{bool}\ try\_acquire()\ \{}
  32. \DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  33. \DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ success\ =\ \textcolor{keyword}{false};}
  34. \DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (count\_\ >\ 0)\ \{}
  35. \DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ \ \ \ \ count\_-\/-\/;}
  36. \DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ \ \ \ \ success\ =\ \textcolor{keyword}{true};}
  37. \DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \}}
  38. \DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  39. \DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ success;}
  40. \DoxyCodeLine{00068\ \ \ \ \ \}}
  41. \DoxyCodeLine{00069\ }
  42. \DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordtype}{void}\ release()\ \{}
  43. \DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  44. \DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (count\_\ <\ max\_count\_)\ \{}
  45. \DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ \ \ \ \ count\_++;}
  46. \DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \}}
  47. \DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wait\_mask\_\ !=\ 0)\ \{}
  48. \DoxyCodeLine{00079\ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::find\_highest\_prio\_waiter(wait\_mask\_);}
  49. \DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (slot\ !=\ 0xFF)\ \{}
  50. \DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ \&=\ \string~(task\_mask\_t\{1\}\ <<\ slot);}
  51. \DoxyCodeLine{00082\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ detail::wake\_task\_locked(TaskHandle\{slot\});}
  52. \DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
  53. \DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \}}
  54. \DoxyCodeLine{00085\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  55. \DoxyCodeLine{00086\ \ \ \ \ \}}
  56. \DoxyCodeLine{00087\ }
  57. \DoxyCodeLine{00089\ \ \ \ \ int32\_t\ count()\textcolor{keyword}{\ const\ }\{}
  58. \DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  59. \DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ int32\_t\ c\ =\ count\_;}
  60. \DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  61. \DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ c;}
  62. \DoxyCodeLine{00094\ \ \ \ \ \}}
  63. \DoxyCodeLine{00095\ }
  64. \DoxyCodeLine{00096\ \textcolor{keyword}{private}:}
  65. \DoxyCodeLine{00097\ \ \ \ \ int32\_t\ count\_;}
  66. \DoxyCodeLine{00098\ \ \ \ \ int32\_t\ max\_count\_;}
  67. \DoxyCodeLine{00099\ \ \ \ \ task\_mask\_t\ wait\_mask\_\{0\};}
  68. \DoxyCodeLine{00100\ \};}
  69. \DoxyCodeLine{00101\ }
  70. \DoxyCodeLine{00105\ \textcolor{keyword}{using\ }BinarySemaphore\ =\ Semaphore;}
  71. \DoxyCodeLine{00106\ }
  72. \DoxyCodeLine{00123\ \textcolor{keyword}{class\ }Mutex\ \{}
  73. \DoxyCodeLine{00124\ \textcolor{keyword}{public}:}
  74. \DoxyCodeLine{00125\ \ \ \ \ Mutex()\ =\ \textcolor{keywordflow}{default};}
  75. \DoxyCodeLine{00126\ }
  76. \DoxyCodeLine{00132\ \ \ \ \ \textcolor{keywordtype}{void}\ lock()\ \{}
  77. \DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  78. \DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
  79. \DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{while}\ (locked\_)\ \{}
  80. \DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ |=\ (task\_mask\_t\{1\}\ <<\ my\_slot);}
  81. \DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \ \ \ \ detail::block\_current\_task\_locked();}
  82. \DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  83. \DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \ \ \ \ save\ =\ detail::sched\_lock();}
  84. \DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \}}
  85. \DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{true};}
  86. \DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ my\_slot;}
  87. \DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  88. \DoxyCodeLine{00144\ \ \ \ \ \}}
  89. \DoxyCodeLine{00145\ }
  90. \DoxyCodeLine{00152\ \ \ \ \ \textcolor{keywordtype}{bool}\ try\_lock()\ \{}
  91. \DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  92. \DoxyCodeLine{00154\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
  93. \DoxyCodeLine{00155\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ success\ =\ \textcolor{keyword}{false};}
  94. \DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!locked\_)\ \{}
  95. \DoxyCodeLine{00157\ \ \ \ \ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{true};}
  96. \DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ my\_slot;}
  97. \DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ \ \ \ \ success\ =\ \textcolor{keyword}{true};}
  98. \DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \}}
  99. \DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  100. \DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ success;}
  101. \DoxyCodeLine{00163\ \ \ \ \ \}}
  102. \DoxyCodeLine{00164\ }
  103. \DoxyCodeLine{00171\ \ \ \ \ \textcolor{keywordtype}{void}\ unlock()\ \{}
  104. \DoxyCodeLine{00172\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  105. \DoxyCodeLine{00173\ \ \ \ \ \ \ \ \ uint8\_t\ my\_slot\ =\ detail::current\_task\_slot();}
  106. \DoxyCodeLine{00174\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (locked\_\ \&\&\ owner\_slot\_\ ==\ my\_slot)\ \{}
  107. \DoxyCodeLine{00175\ \ \ \ \ \ \ \ \ \ \ \ \ locked\_\ =\ \textcolor{keyword}{false};}
  108. \DoxyCodeLine{00176\ \ \ \ \ \ \ \ \ \ \ \ \ owner\_slot\_\ =\ 0xFF;}
  109. \DoxyCodeLine{00177\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (wait\_mask\_\ !=\ 0)\ \{}
  110. \DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ uint8\_t\ slot\ =\ detail::find\_highest\_prio\_waiter(wait\_mask\_);}
  111. \DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (slot\ !=\ 0xFF)\ \{}
  112. \DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ wait\_mask\_\ \&=\ \string~(task\_mask\_t\{1\}\ <<\ slot);}
  113. \DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ detail::wake\_task\_locked(TaskHandle\{slot\});}
  114. \DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
  115. \DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
  116. \DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \}}
  117. \DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  118. \DoxyCodeLine{00186\ \ \ \ \ \}}
  119. \DoxyCodeLine{00187\ }
  120. \DoxyCodeLine{00189\ \ \ \ \ \textcolor{keywordtype}{bool}\ is\_locked()\textcolor{keyword}{\ const\ }\{}
  121. \DoxyCodeLine{00190\ \ \ \ \ \ \ \ \ uint32\_t\ save\ =\ detail::sched\_lock();}
  122. \DoxyCodeLine{00191\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ lk\ =\ locked\_;}
  123. \DoxyCodeLine{00192\ \ \ \ \ \ \ \ \ detail::sched\_unlock(save);}
  124. \DoxyCodeLine{00193\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ lk;}
  125. \DoxyCodeLine{00194\ \ \ \ \ \}}
  126. \DoxyCodeLine{00195\ }
  127. \DoxyCodeLine{00196\ \textcolor{keyword}{private}:}
  128. \DoxyCodeLine{00197\ \ \ \ \ \textcolor{keywordtype}{bool}\ locked\_\{\textcolor{keyword}{false}\};}
  129. \DoxyCodeLine{00198\ \ \ \ \ uint8\_t\ owner\_slot\_\{0xFF\};}
  130. \DoxyCodeLine{00199\ \ \ \ \ task\_mask\_t\ wait\_mask\_\{0\};}
  131. \DoxyCodeLine{00200\ \};}
  132. \DoxyCodeLine{00201\ }
  133. \DoxyCodeLine{00210\ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Lockable>}
  134. \DoxyCodeLine{00211\ \textcolor{keyword}{class\ }LockGuard\ \{}
  135. \DoxyCodeLine{00212\ \textcolor{keyword}{public}:}
  136. \DoxyCodeLine{00217\ \ \ \ \ \textcolor{keyword}{explicit}\ LockGuard(Lockable\&\ m)\ :\ m\_(m)\ \{\ m\_.lock();\ \}}
  137. \DoxyCodeLine{00218\ }
  138. \DoxyCodeLine{00220\ \ \ \ \ \string~LockGuard()\ \{\ m\_.unlock();\ \}}
  139. \DoxyCodeLine{00221\ }
  140. \DoxyCodeLine{00222\ \ \ \ \ LockGuard(\textcolor{keyword}{const}\ LockGuard\&)\ =\ \textcolor{keyword}{delete};}
  141. \DoxyCodeLine{00223\ \ \ \ \ LockGuard\&\ operator=(\textcolor{keyword}{const}\ LockGuard\&)\ =\ \textcolor{keyword}{delete};}
  142. \DoxyCodeLine{00224\ }
  143. \DoxyCodeLine{00225\ \textcolor{keyword}{private}:}
  144. \DoxyCodeLine{00226\ \ \ \ \ Lockable\&\ m\_;}
  145. \DoxyCodeLine{00227\ \};}
  146. \DoxyCodeLine{00228\ }
  147. \DoxyCodeLine{00229\ \}\ \ \textcolor{comment}{//\ namespace\ tk}}
  148. \end{DoxyCode}