osmutex.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
  2. Copyright (c) 2014-2017 Datalight, Inc.
  3. All Rights Reserved Worldwide.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; use version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. /* Businesses and individuals that for commercial or other reasons cannot
  16. comply with the terms of the GPLv2 license may obtain a commercial license
  17. before incorporating Reliance Edge into proprietary software for
  18. distribution in any form. Visit http://www.datalight.com/reliance-edge for
  19. more information.
  20. */
  21. /** @file
  22. @brief Implements a synchronization object to provide mutual exclusion.
  23. */
  24. #include "ch.h"
  25. #include "hal.h"
  26. #if (HAL_USE_SDMMC == TRUE)
  27. #include "sama_sdmmc_lld.h"
  28. #if SDMMC_USE_RELEDGE_LIB == 1
  29. #include <redfs.h>
  30. #include <redosdeviations.h>
  31. #if REDCONF_TASK_COUNT > 1U
  32. static semaphore_t red_sem;
  33. /** @brief Initialize the mutex.
  34. After initialization, the mutex is in the released state.
  35. The behavior of calling this function when the mutex is still initialized
  36. is undefined.
  37. @return A negated ::REDSTATUS code indicating the operation result.
  38. @retval 0 Operation was successful.
  39. */
  40. REDSTATUS RedOsMutexInit(void)
  41. {
  42. chSemObjectInit(&red_sem, 1);
  43. return 0;
  44. }
  45. /** @brief Uninitialize the mutex.
  46. The behavior of calling this function when the mutex is not initialized is
  47. undefined; likewise, the behavior of uninitializing the mutex when it is
  48. in the acquired state is undefined.
  49. @return A negated ::REDSTATUS code indicating the operation result.
  50. @retval 0 Operation was successful.
  51. */
  52. REDSTATUS RedOsMutexUninit(void)
  53. {
  54. chSemReset(&red_sem, 0);
  55. return 0;
  56. }
  57. /** @brief Acquire the mutex.
  58. The behavior of calling this function when the mutex is not initialized is
  59. undefined; likewise, the behavior of recursively acquiring the mutex is
  60. undefined.
  61. */
  62. void RedOsMutexAcquire(void)
  63. {
  64. chSemWaitTimeout(&red_sem, TIME_INFINITE);
  65. }
  66. /** @brief Release the mutex.
  67. The behavior is undefined in the following cases:
  68. - Releasing the mutex when the mutex is not initialized.
  69. - Releasing the mutex when it is not in the acquired state.
  70. - Releasing the mutex from a task or thread other than the one which
  71. acquired the mutex.
  72. */
  73. void RedOsMutexRelease(void)
  74. {
  75. chSemSignal(&red_sem);
  76. }
  77. #endif
  78. #endif
  79. #endif