Files
BloomPatched/src/Helpers/Thread.hpp

61 lines
1.4 KiB
C++
Raw Normal View History

2021-04-04 21:04:12 +01:00
#pragma once
#include <csignal>
#include <cassert>
#include "SyncSafe.hpp"
namespace Bloom
{
enum class ThreadState
{
UNINITIALISED,
READY,
STOPPED,
STARTING,
SHUTDOWN_INITIATED,
};
class Thread
{
public:
2022-01-11 21:12:25 +00:00
Thread() = default;
virtual ~Thread() = default;
Thread(const Thread& other) = delete;
Thread(Thread&& other) = delete;
Thread& operator = (const Thread& other) = delete;
Thread& operator = (Thread&& other) = delete;
virtual ThreadState getThreadState() {
2022-04-14 22:58:00 +01:00
auto lock = this->state.acquireLock();
return this->state.getValue();
2022-04-14 22:58:00 +01:00
}
2021-04-04 21:04:12 +01:00
protected:
virtual void setThreadState(ThreadState state) {
2021-04-04 21:04:12 +01:00
this->state.setValue(state);
2022-04-14 23:08:28 +01:00
}
2021-04-04 21:04:12 +01:00
/**
* Disables signal interrupts on current thread.
*/
void blockAllSignalsOnCurrentThread() {
sigset_t set = {};
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
2022-04-14 23:08:28 +01:00
}
2021-04-04 21:04:12 +01:00
2022-04-14 23:08:28 +01:00
void setName(const std::string& name) {
2021-04-04 21:04:12 +01:00
// POSIX thread names cannot exceed 16 characters, including the terminating null byte.
assert(name.size() <= 15);
pthread_setname_np(pthread_self(), name.c_str());
}
private:
SyncSafe<ThreadState> state = SyncSafe<ThreadState>(ThreadState::UNINITIALISED);
2021-04-04 21:04:12 +01:00
};
}