Mutex.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "core/thread/Mutex.hpp"
  2. #include <threads.h>
  3. #include "core/utils/Meta.hpp"
  4. #include "core/utils/Utility.hpp"
  5. static void reset(mtx_t* m) {
  6. Core::memorySet(m, 0, sizeof(mtx_t));
  7. }
  8. Core::Mutex::Mutex() : mutex() {
  9. CORE_ASSERT_ALIGNED_DATA(mutex, mtx_t);
  10. reset(mutex.as<mtx_t>());
  11. }
  12. static bool doesExist(mtx_t* t) {
  13. mtx_t zero{};
  14. return !Core::memoryCompare(&zero, t, sizeof(mtx_t));
  15. }
  16. Core::Mutex::~Mutex() {
  17. if(doesExist(mutex.as<mtx_t>())) {
  18. mtx_destroy(mutex.as<mtx_t>());
  19. }
  20. }
  21. check_return Core::Error Core::Mutex::init() {
  22. if(doesExist(mutex.as<mtx_t>())) {
  23. return Error::INVALID_STATE;
  24. }
  25. return mtx_init(mutex.as<mtx_t>(), mtx_plain) != thrd_success
  26. ? Error::MUTEX_ERROR
  27. : Error::NONE;
  28. }
  29. check_return Core::Error Core::Mutex::lock() {
  30. return mtx_lock(mutex.as<mtx_t>()) != thrd_success ? Error::MUTEX_ERROR
  31. : Error::NONE;
  32. }
  33. check_return Core::Error Core::Mutex::unlock() {
  34. return mtx_unlock(mutex.as<mtx_t>()) != thrd_success ? Error::MUTEX_ERROR
  35. : Error::NONE;
  36. }