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
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
SyncSafe<ThreadState> state = SyncSafe<ThreadState>(ThreadState::UNINITIALISED);
|
|
|
|
|
|
|
|
|
|
protected:
|
2021-05-29 21:39:00 +01:00
|
|
|
virtual void setThreadState(ThreadState state) {
|
2021-04-04 21:04:12 +01:00
|
|
|
this->state.setValue(state);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disables signal interrupts on current thread.
|
|
|
|
|
*/
|
|
|
|
|
void blockAllSignalsOnCurrentThread() {
|
|
|
|
|
sigset_t set = {};
|
|
|
|
|
sigfillset(&set);
|
|
|
|
|
sigprocmask(SIG_SETMASK, &set, NULL);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void setName(std::string name) {
|
|
|
|
|
// POSIX thread names cannot exceed 16 characters, including the terminating null byte.
|
|
|
|
|
assert(name.size() <= 15);
|
|
|
|
|
|
|
|
|
|
pthread_setname_np(pthread_self(), name.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2021-05-29 21:39:00 +01:00
|
|
|
virtual ThreadState getThreadState() {
|
2021-04-04 21:04:12 +01:00
|
|
|
return this->state.getValue();
|
|
|
|
|
};
|
|
|
|
|
};
|
2021-05-29 21:39:00 +01:00
|
|
|
}
|