Mutex.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 ErrorCode::INVALID_STATE;
  24. }
  25. return mtx_init(mutex.as<mtx_t>(), mtx_plain) != thrd_success
  26. ? ErrorCode::MUTEX_ERROR
  27. : ErrorCode::NONE;
  28. }
  29. check_return Core::Error Core::Mutex::lock() {
  30. return mtx_lock(mutex.as<mtx_t>()) != thrd_success ? ErrorCode::MUTEX_ERROR
  31. : ErrorCode::NONE;
  32. }
  33. check_return Core::Error Core::Mutex::unlock() {
  34. return mtx_unlock(mutex.as<mtx_t>()) != thrd_success
  35. ? ErrorCode::MUTEX_ERROR
  36. : ErrorCode::NONE;
  37. }