mirror of
https://github.com/tuxbox-neutrino/libstb-hal.git
synced 2025-08-26 15:02:58 +02:00
add mutex and thread abstraction
This commit is contained in:
22
generic-pc/mutex_abstraction.cpp
Normal file
22
generic-pc/mutex_abstraction.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "mutex_abstraction.h"
|
||||
|
||||
Mutex::Mutex() :
|
||||
mMutex()
|
||||
{
|
||||
pthread_mutex_init(&mMutex, 0);
|
||||
}
|
||||
|
||||
Mutex::~Mutex()
|
||||
{
|
||||
pthread_mutex_destroy(&mMutex);
|
||||
}
|
||||
|
||||
void Mutex::lock()
|
||||
{
|
||||
pthread_mutex_lock(&mMutex);
|
||||
}
|
||||
|
||||
void Mutex::unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&mMutex);
|
||||
}
|
20
generic-pc/mutex_abstraction.h
Normal file
20
generic-pc/mutex_abstraction.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef _MUTEX_ABSTRACTION_H
|
||||
#define _MUTEX_ABSTRACTION_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
class Mutex
|
||||
{
|
||||
pthread_mutex_t mMutex;
|
||||
|
||||
Mutex(const Mutex&);
|
||||
const Mutex& operator=(const Mutex&);
|
||||
|
||||
public:
|
||||
Mutex();
|
||||
virtual ~Mutex();
|
||||
void lock();
|
||||
void unlock();
|
||||
};
|
||||
|
||||
#endif
|
33
generic-pc/thread_abstraction.cpp
Normal file
33
generic-pc/thread_abstraction.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "thread_abstraction.h"
|
||||
|
||||
Thread::Thread() :
|
||||
mIsRunning(false),
|
||||
mThread()
|
||||
{
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
// if thread is still running on object destruction, cancel thread the hard way:
|
||||
if (mIsRunning)
|
||||
{
|
||||
pthread_cancel(mThread);
|
||||
}
|
||||
}
|
||||
|
||||
void Thread::startThread()
|
||||
{
|
||||
mIsRunning = true;
|
||||
pthread_create(&mThread, 0, &Thread::runThread, this);
|
||||
}
|
||||
|
||||
void Thread::joinThread()
|
||||
{
|
||||
pthread_join(mThread, 0);
|
||||
mIsRunning = false;
|
||||
}
|
||||
|
||||
void* Thread::runThread(void* ptr)
|
||||
{
|
||||
((Thread*)ptr)->run();
|
||||
}
|
25
generic-pc/thread_abstraction.h
Normal file
25
generic-pc/thread_abstraction.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _THREAD_ABSTRACTION_H
|
||||
#define _THREAD_ABSTRACTION_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
class Thread
|
||||
{
|
||||
bool mIsRunning;
|
||||
pthread_t mThread;
|
||||
|
||||
static void* runThread(void*);
|
||||
Thread(const Thread&);
|
||||
const Thread& operator=(const Thread&);
|
||||
|
||||
public:
|
||||
Thread();
|
||||
~Thread();
|
||||
void startThread();
|
||||
void joinThread();
|
||||
|
||||
protected:
|
||||
virtual void run() = 0;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user