diff --git a/src/util/thread.cpp b/src/util/thread.cpp new file mode 100644 index 0000000..c07182c --- /dev/null +++ b/src/util/thread.cpp @@ -0,0 +1,70 @@ +/* + * boblight + * Copyright (C) Bob 2009 + * + * boblight is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * boblight is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "thread.h" + +CThread::CThread() +{ + m_running = false; +} + +CThread::~CThread() +{ + StopThread(); +} + +void CThread::StartThread() +{ + m_stop = false; + m_running = true; + pthread_create(&m_thread, NULL, ThreadFunction, reinterpret_cast(this)); +} + +void* CThread::ThreadFunction(void* args) +{ + CThread* thread = reinterpret_cast(args); + thread->Process(); + thread->m_running = false; +} + +void CThread::Process() +{ +} + +void CThread::StopThread() +{ + AsyncStopThread(); + JoinThread(); +} + +void CThread::AsyncStopThread() +{ + m_stop = true; +} + +void CThread::JoinThread() +{ + if (m_running) + pthread_join(m_thread, NULL); +} + +bool CThread::IsRunning() +{ + return m_running; +} + diff --git a/src/util/thread.h b/src/util/thread.h new file mode 100644 index 0000000..14de2ed --- /dev/null +++ b/src/util/thread.h @@ -0,0 +1,50 @@ +/* + * boblight + * Copyright (C) Bob 2009 + * + * boblight is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * boblight is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef CTHREAD +#define CTHREAD + +#define __STDC_CONSTANT_MACROS +#define __STDC_LIMIT_MACROS +#include + +#include +#include + +class CThread +{ + public: + CThread(); + ~CThread(); + void StartThread(); + void StopThread(); + void AsyncStopThread(); + void JoinThread(); + bool IsRunning(); + + protected: + pthread_t m_thread; + volatile bool m_stop; + volatile bool m_running; + + static void* ThreadFunction(void* args); + virtual void Process(); +}; + +#endif //CTHREAD + diff --git a/wscript b/wscript index a448456..bd1c5e4 100644 --- a/wscript +++ b/wscript @@ -42,6 +42,7 @@ def build(bld): src/util/timeutils.cpp\ src/util/condition.cpp\ src/util/tcpsocket.cpp\ + src/util/thread.cpp\ src/fft.cpp', use=['m','pthread','rt', 'jack', 'fftw3', 'fftw3f', 'samplerate'], includes='./src',