CriticalSectionLock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * PackageLicenseDeclared: Apache-2.0
  3. * Copyright (c) 2017 ARM Limited
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #ifndef MBED_CRITICALSECTIONLOCK_H
  18. #define MBED_CRITICALSECTIONLOCK_H
  19. #include "platform/mbed_critical.h"
  20. #include "platform/mbed_toolchain.h"
  21. namespace mbed {
  22. /** \addtogroup platform */
  23. /** @{*/
  24. /**
  25. * \defgroup platform_CriticalSectionLock CriticalSectionLock functions
  26. * @{
  27. */
  28. /** RAII object for disabling, then restoring, interrupt state
  29. * Usage:
  30. * @code
  31. *
  32. * // RAII style usage
  33. * unsigned int atomic_counter_increment(unsigned int &counter) {
  34. * CriticalSectionLock lock;
  35. * // Code in this block will run with interrupts disabled
  36. * // Interrupts will be restored to their previous state automatically
  37. * // at the end of function scope
  38. * return ++counter;
  39. * }
  40. *
  41. * // free locking usage
  42. * unsigned int atomic_counter_decrement(unsigned int &counter) {
  43. * CriticalSectionLock::enable();
  44. * // Code in this block will run with interrupts disabled
  45. * counter--;
  46. * CriticalSectionLock::disable(); // need explicitly to disable critical section lock
  47. * // interrupts will be restored to their previous state here
  48. * return counter;
  49. * }
  50. *
  51. * @endcode
  52. */
  53. class CriticalSectionLock {
  54. public:
  55. CriticalSectionLock()
  56. {
  57. core_util_critical_section_enter();
  58. }
  59. ~CriticalSectionLock()
  60. {
  61. core_util_critical_section_exit();
  62. }
  63. /** Mark the start of a critical section
  64. * @deprecated This function is inconsistent with RAII and is being removed in the future. Replaced by static function CriticalSectionLock::enable.
  65. *
  66. */
  67. MBED_DEPRECATED_SINCE("mbed-os-5.8",
  68. "This function is inconsistent with RAII and is being removed in the future."
  69. "Replaced by static function CriticalSectionLock::enable.")
  70. void lock()
  71. {
  72. core_util_critical_section_enter();
  73. }
  74. /** Mark the end of a critical section
  75. * @deprecated This function is inconsistent with RAII and is being removed in the future. Replaced by static function CriticalSectionLock::enable.
  76. *
  77. */
  78. MBED_DEPRECATED_SINCE("mbed-os-5.8",
  79. "This function is inconsistent with RAII and is being removed in the future."
  80. "Replaced by static function CriticalSectionLock::disable.")
  81. void unlock()
  82. {
  83. core_util_critical_section_exit();
  84. }
  85. /** Mark the start of a critical section
  86. */
  87. static void enable()
  88. {
  89. core_util_critical_section_enter();
  90. }
  91. /** Mark the end of a critical section
  92. */
  93. static void disable()
  94. {
  95. core_util_critical_section_exit();
  96. }
  97. };
  98. /**@}*/
  99. /**@}*/
  100. } // namespace mbed
  101. #endif