123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include "core/Thread.hpp"
- #include "ErrorSimulator.hpp"
- #include "core/Logger.hpp"
- using Core::Mutex;
- using Core::MutexGuard;
- using Core::Thread;
- void Mutex::lock() noexcept {
- try {
- FAIL_STEP_THROW();
- mutex.lock();
- } catch(std::exception& e) {
- REPORT(LogLevel::ERROR, "Could not lock mutex: #", e.what());
- }
- }
- void Mutex::unlock() noexcept {
- try {
- FAIL_STEP_THROW();
- mutex.unlock();
- } catch(std::exception& e) {
- REPORT(LogLevel::ERROR, "Could not unlock mutex: #", e.what());
- }
- }
- MutexGuard::MutexGuard(Mutex& m) : mutex(m) {
- mutex.lock();
- }
- MutexGuard::~MutexGuard() {
- mutex.unlock();
- }
- Thread::Thread() : thread() {
- }
- Thread::Thread(Thread&& other) noexcept : thread() {
- swap(other);
- }
- Thread::~Thread() {
- join();
- }
- Thread& Thread::operator=(Thread&& other) noexcept {
- if(this != &other) {
- join();
- thread = Core::move(other.thread);
- }
- return *this;
- }
- void Core::Thread::swap(Thread& other) {
- Core::swap(thread, other.thread);
- }
- bool Thread::start(Function f, void* p) {
- if(thread.joinable()) {
- return true;
- }
- try {
- FAIL_STEP_THROW();
- thread = std::thread([f, p]() { f(p); });
- } catch(std::exception& e) {
- REPORT(LogLevel::ERROR, "Could not start thread: #", e.what());
- return true;
- }
- return false;
- }
- bool Thread::join() {
- if(thread.joinable()) {
- try {
- FAIL_STEP_THROW();
- thread.join();
- } catch(std::exception& e) {
- REPORT(LogLevel::ERROR, "Could not join thread: #", e.what());
- return true;
- }
- }
- return false;
- }
|