12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #include "core/thread/Mutex.hpp"
- #include <threads.h>
- #include "core/utils/Meta.hpp"
- #include "core/utils/Utility.hpp"
- static void reset(mtx_t* m) {
- Core::memorySet(m, 0, sizeof(mtx_t));
- }
- Core::Mutex::Mutex() : mutex() {
- CORE_ASSERT_ALIGNED_DATA(mutex, mtx_t);
- reset(mutex.as<mtx_t>());
- }
- static bool doesExist(mtx_t* t) {
- mtx_t zero{};
- return !Core::memoryCompare(&zero, t, sizeof(mtx_t));
- }
- Core::Mutex::~Mutex() {
- if(doesExist(mutex.as<mtx_t>())) {
- mtx_destroy(mutex.as<mtx_t>());
- }
- }
- check_return Core::Error Core::Mutex::init() {
- if(doesExist(mutex.as<mtx_t>())) {
- return ErrorCode::INVALID_STATE;
- }
- return mtx_init(mutex.as<mtx_t>(), mtx_plain) != thrd_success
- ? ErrorCode::MUTEX_ERROR
- : ErrorCode::NONE;
- }
- check_return Core::Error Core::Mutex::lock() {
- return mtx_lock(mutex.as<mtx_t>()) != thrd_success ? ErrorCode::MUTEX_ERROR
- : ErrorCode::NONE;
- }
- check_return Core::Error Core::Mutex::unlock() {
- return mtx_unlock(mutex.as<mtx_t>()) != thrd_success
- ? ErrorCode::MUTEX_ERROR
- : ErrorCode::NONE;
- }
|