1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include "core/thread/Mutex.hpp"
- #include <string.h>
- #include <threads.h>
- #include "core/utils/Meta.hpp"
- #include "core/utils/Utility.hpp"
- static void reset(mtx_t* m) {
- memset(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 memcmp(&zero, t, sizeof(mtx_t)) != 0;
- }
- 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;
- }
|