ccondition_variable.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "condition_variable.hpp"
  39. #include "thread.hpp"
  40. #ifdef CPP_FREERTOS_CONDITION_VARIABLES
  41. using namespace cpp_freertos;
  42. ConditionVariable::ConditionVariable()
  43. : Lock(), WaitList()
  44. {
  45. }
  46. void ConditionVariable::AddToWaitList(Thread *thread)
  47. {
  48. //
  49. // Lock the internal condition variable state
  50. //
  51. Lock.Lock();
  52. //
  53. // Add the thread to the condition variable wait list.
  54. //
  55. WaitList.push_back(thread);
  56. //
  57. // Drop the internal condition variable lock.
  58. //
  59. Lock.Unlock();
  60. }
  61. void ConditionVariable::Signal()
  62. {
  63. //
  64. // Lock the internal condition variable state
  65. //
  66. Lock.Lock();
  67. if ( !WaitList.empty() ) {
  68. Thread *thr = WaitList.front();
  69. WaitList.pop_front();
  70. thr->Signal();
  71. }
  72. //
  73. // Drop the internal condition variable lock.
  74. //
  75. Lock.Unlock();
  76. }
  77. void ConditionVariable::Broadcast()
  78. {
  79. //
  80. // Lock the internal condition variable state
  81. //
  82. Lock.Lock();
  83. while ( !WaitList.empty() ) {
  84. Thread *thr = WaitList.front();
  85. WaitList.pop_front();
  86. thr->Signal();
  87. }
  88. //
  89. // Drop the internal condition variable lock.
  90. //
  91. Lock.Unlock();
  92. }
  93. #endif