cxx_Semaphore.cpp 657 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. Semaphore::Semaphore(const char *name, int32_t count)
  12. {
  13. rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
  14. }
  15. bool Semaphore::wait(int32_t millisec)
  16. {
  17. rt_int32_t tick;
  18. if (millisec < 0)
  19. tick = -1;
  20. else
  21. tick = rt_tick_from_millisecond(millisec);
  22. return rt_sem_take(&mID, tick) == RT_EOK;
  23. }
  24. void Semaphore::release(void)
  25. {
  26. rt_sem_release(&mID);
  27. }
  28. Semaphore::~Semaphore()
  29. {
  30. rt_sem_detach(&mID);
  31. }