critical_section_api.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2006-2017 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MBED_CRITICAL_SECTION_API_H
  19. #define MBED_CRITICAL_SECTION_API_H
  20. #include <stdbool.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * \defgroup hal_critical Critical Section HAL functions
  26. * @{
  27. */
  28. /**
  29. * Mark the start of a critical section
  30. *
  31. * This function will be called by core_util_critical_section_enter() each time
  32. * the application requests to enter a critical section. The purpose of the
  33. * critical section is to ensure mutual-exclusion synchronisation of the
  34. * processor by preventing any change in processor control, the default
  35. * behaviour requires storing the state of interrupts in the system before
  36. * disabling them.
  37. *
  38. * The critical section code supports nesting. When a thread has entered a
  39. * critical section it can make additional calls to
  40. * core_util_critical_section_enter() without deadlocking itself. The critical
  41. * section driver API tracks the number of nested calls to the critical section.
  42. * The critical section will only be exited when
  43. * core_util_critical_section_exit() has been called once for each time it
  44. * entered the critical section.
  45. *
  46. * On the first call to enter a critical section this function MUST store the
  47. * state of any interrupts or other application settings it will modify to
  48. * facilitate the critical section.
  49. *
  50. * Each successive call to enter the critical section MUST ignore storing or
  51. * modifying any application state.
  52. *
  53. * The default implementation of this function which will save the current state
  54. * of interrupts before disabling them. This implementation can be found in
  55. * mbed_critical_section_api.c. This behaviour is can be overridden on a per
  56. * platform basis by providing a different implementation within the correct
  57. * targets directory.
  58. */
  59. void hal_critical_section_enter(void);
  60. /** Mark the end of a critical section.
  61. *
  62. * The purpose of this function is to restore any state that was modified upon
  63. * entering the critical section, allowing other threads or interrupts to change
  64. * the processor control.
  65. *
  66. * This function will be called once by core_util_critical_section_exit() per
  67. * critical section on last call to exit. When called, the application MUST
  68. * restore the saved interrupt/application state that was saved when entering
  69. * the critical section.
  70. *
  71. * There is a default implementation of this function, it will restore the state
  72. * of interrupts that were previously saved when hal_critical_section_enter was
  73. * first called, this implementation can be found in
  74. * mbed_critical_section_api.c. This behaviour is overridable by providing a
  75. * different function implementation within the correct targets directory.
  76. */
  77. void hal_critical_section_exit(void);
  78. /** Determine if the application is currently running in a critical section
  79. *
  80. * The purpose of this function is to inform the caller whether or not the
  81. * application is running in a critical section. This is done by checking if
  82. * the current interrupt state has been saved in the underlying implementation,
  83. * this could also be done by checking the state of the interrupts at the time
  84. * of calling.
  85. *
  86. * @return True if running in a critical section, false if not.
  87. */
  88. bool hal_in_critical_section(void);
  89. /**@}*/
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif // MBED_CRITICAL_SECTION_API_H
  94. /** @}*/