cxx_Semaphore.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include "cxx_semaphore.h"
  10. using namespace rtthread;
  11. /**
  12. * @brief Semaphore class constructor.
  13. * @param name Semaphore name
  14. * @param count Initial semaphore count
  15. */
  16. Semaphore::Semaphore(const char *name, int32_t count)
  17. {
  18. rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
  19. }
  20. /**
  21. * @brief Wait on the semaphore.
  22. * @param millisec Timeout in milliseconds (-1 for infinite wait).
  23. * @return true if the semaphore was successfully taken, false on timeout.
  24. */
  25. bool Semaphore::wait(int32_t millisec)
  26. {
  27. rt_int32_t tick;
  28. if (millisec < 0)
  29. tick = -1;
  30. else
  31. tick = rt_tick_from_millisecond(millisec);
  32. return rt_sem_take(&mID, tick) == RT_EOK;
  33. }
  34. /**
  35. * @brief Release the semaphore.
  36. */
  37. void Semaphore::release(void)
  38. {
  39. rt_sem_release(&mID);
  40. }
  41. /**
  42. * @brief Detach the semaphore when the object is destroyed.
  43. */
  44. Semaphore::~Semaphore()
  45. {
  46. rt_sem_detach(&mID);
  47. }