#ifndef RPI_THREAD_H
#define RPI_THREAD_H
#include <stdint.h>
#include <pthread.h>
namespace rgb_matrix {
class Thread {
public:
Thread();
virtual ~Thread();
void WaitStopped();
virtual void Start(int realtime_priority = 0, uint32_t cpu_affinity_mask = 0);
virtual void Run() = 0;
private:
static void *PthreadCallRun(void *tobject);
bool started_;
pthread_t thread_;
};
class Mutex {
public:
Mutex() { pthread_mutex_init(&mutex_, NULL); }
~Mutex() { pthread_mutex_destroy(&mutex_); }
void Lock() { pthread_mutex_lock(&mutex_); }
void Unlock() { pthread_mutex_unlock(&mutex_); }
bool WaitOn(pthread_cond_t *cond, long timeout_ms = -1);
private:
pthread_mutex_t mutex_;
};
class MutexLock {
public:
MutexLock(Mutex *m) : mutex_(m) { mutex_->Lock(); }
~MutexLock() { mutex_->Unlock(); }
private:
Mutex *const mutex_;
};
}
#endif