Semaphores.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include <stdint.h>
  19. #include <AP_HAL/AP_HAL_Boards.h>
  20. #include <AP_HAL/AP_HAL_Macros.h>
  21. #include <AP_HAL/Semaphores.h>
  22. #include "AP_HAL_ChibiOS_Namespace.h"
  23. class ChibiOS::Semaphore : public AP_HAL::Semaphore {
  24. public:
  25. Semaphore();
  26. virtual bool give() override;
  27. virtual bool take(uint32_t timeout_ms) override;
  28. virtual bool take_nonblocking() override;
  29. // methods within HAL_ChibiOS only
  30. bool check_owner(void);
  31. void assert_owner(void);
  32. protected:
  33. // to avoid polluting the global namespace with the 'ch' variable,
  34. // we declare the lock as a uint64_t, and cast inside the cpp file
  35. uint64_t _lock[2];
  36. };
  37. // a recursive semaphore, allowing for a thread to take it more than
  38. // once. It must be released the same number of times it is taken
  39. class ChibiOS::Semaphore_Recursive : public ChibiOS::Semaphore {
  40. public:
  41. Semaphore_Recursive();
  42. bool give() override;
  43. bool take(uint32_t timeout_ms) override;
  44. bool take_nonblocking() override;
  45. private:
  46. uint32_t count;
  47. };