cread_write_lock.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #include "read_write_lock.hpp"
  39. using namespace cpp_freertos;
  40. ReadWriteLock::ReadWriteLock()
  41. : ReadCount(0)
  42. {
  43. ReadLock = xSemaphoreCreateMutex();
  44. if (ReadLock == NULL) {
  45. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  46. throw ReadWriteLockCreateException();
  47. #else
  48. configASSERT(!"ReadWriteLock Constructor Failed");
  49. #endif
  50. }
  51. //
  52. // ResourceLock CANNOT be a mutex. In FreeRTOS, as in most OS's,
  53. // a thread is not allowed to unlock another thread's mutex. But
  54. // the very nature of a Reader Lock allows an arbitrary ordering
  55. // of unlocks when multiple threads hold the reader lock.
  56. // Semaphores are not subject to this constraint.
  57. //
  58. ResourceLock = xSemaphoreCreateBinary();
  59. if (ResourceLock == NULL) {
  60. vSemaphoreDelete(ReadLock);
  61. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  62. throw ReadWriteLockCreateException();
  63. #else
  64. configASSERT(!"ReadWriteLock Constructor Failed");
  65. #endif
  66. }
  67. //
  68. // Initialize it as "full", so it behaves similar to a mutex.
  69. //
  70. xSemaphoreGive(ResourceLock);
  71. }
  72. ReadWriteLock::~ReadWriteLock()
  73. {
  74. vSemaphoreDelete(ReadLock);
  75. vSemaphoreDelete(ResourceLock);
  76. }
  77. void ReadWriteLockPreferReader::ReaderLock()
  78. {
  79. xSemaphoreTake(ReadLock, portMAX_DELAY);
  80. ReadCount++;
  81. if (ReadCount == 1) {
  82. xSemaphoreTake(ResourceLock, portMAX_DELAY);
  83. }
  84. xSemaphoreGive(ReadLock);
  85. }
  86. void ReadWriteLockPreferReader::ReaderUnlock()
  87. {
  88. xSemaphoreTake(ReadLock, portMAX_DELAY);
  89. ReadCount--;
  90. if (ReadCount == 0) {
  91. xSemaphoreGive(ResourceLock);
  92. }
  93. xSemaphoreGive(ReadLock);
  94. }
  95. void ReadWriteLockPreferReader::WriterLock()
  96. {
  97. xSemaphoreTake(ResourceLock, portMAX_DELAY);
  98. }
  99. void ReadWriteLockPreferReader::WriterUnlock()
  100. {
  101. xSemaphoreGive(ResourceLock);
  102. }
  103. ReadWriteLockPreferWriter::ReadWriteLockPreferWriter()
  104. : ReadWriteLock(),
  105. WriteCount(0)
  106. {
  107. WriteLock = xSemaphoreCreateMutex();
  108. if (WriteLock == NULL) {
  109. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  110. throw ReadWriteLockCreateException();
  111. #else
  112. configASSERT(!"ReadWriteLockPreferWriter Constructor Failed");
  113. #endif
  114. }
  115. //
  116. // BlockReadersLock CANNOT be a mutex. In FreeRTOS, as in most OS's,
  117. // a thread is not allowed to unlock another thread's mutex. But
  118. // the very nature of a Reader Lock allows an arbitrary ordering
  119. // of unlocks when multiple threads hold the reader lock.
  120. // Semaphores are not subject to this constraint.
  121. //
  122. BlockReadersLock = xSemaphoreCreateBinary();
  123. if (BlockReadersLock == NULL) {
  124. vSemaphoreDelete(WriteLock);
  125. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  126. throw ReadWriteLockCreateException();
  127. #else
  128. configASSERT(!"ReadWriteLockPreferWriter Constructor Failed");
  129. #endif
  130. }
  131. //
  132. // Initialize it as "full", so it behaves similar to a mutex.
  133. //
  134. xSemaphoreGive(BlockReadersLock);
  135. }
  136. ReadWriteLockPreferWriter::~ReadWriteLockPreferWriter()
  137. {
  138. vSemaphoreDelete(WriteLock);
  139. vSemaphoreDelete(BlockReadersLock);
  140. }
  141. void ReadWriteLockPreferWriter::ReaderLock()
  142. {
  143. xSemaphoreTake(BlockReadersLock, portMAX_DELAY);
  144. xSemaphoreTake(ReadLock, portMAX_DELAY);
  145. ReadCount++;
  146. if (ReadCount == 1) {
  147. xSemaphoreTake(ResourceLock, portMAX_DELAY);
  148. }
  149. xSemaphoreGive(ReadLock);
  150. xSemaphoreGive(BlockReadersLock);
  151. }
  152. void ReadWriteLockPreferWriter::ReaderUnlock()
  153. {
  154. xSemaphoreTake(ReadLock, portMAX_DELAY);
  155. ReadCount--;
  156. if (ReadCount == 0) {
  157. xSemaphoreGive(ResourceLock);
  158. }
  159. xSemaphoreGive(ReadLock);
  160. }
  161. void ReadWriteLockPreferWriter::WriterLock()
  162. {
  163. xSemaphoreTake(WriteLock, portMAX_DELAY);
  164. WriteCount++;
  165. if (WriteCount == 1) {
  166. xSemaphoreTake(BlockReadersLock, portMAX_DELAY);
  167. }
  168. xSemaphoreGive(WriteLock);
  169. xSemaphoreTake(ResourceLock, portMAX_DELAY);
  170. }
  171. void ReadWriteLockPreferWriter::WriterUnlock()
  172. {
  173. xSemaphoreGive(ResourceLock);
  174. xSemaphoreTake(WriteLock, portMAX_DELAY);
  175. WriteCount--;
  176. if (WriteCount == 0) {
  177. xSemaphoreGive(BlockReadersLock);
  178. }
  179. xSemaphoreGive(WriteLock);
  180. }