summaryrefslogtreecommitdiff
path: root/src/components/include/utils/threads/thread_delegate.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/include/utils/threads/thread_delegate.h')
-rw-r--r--src/components/include/utils/threads/thread_delegate.h64
1 files changed, 50 insertions, 14 deletions
diff --git a/src/components/include/utils/threads/thread_delegate.h b/src/components/include/utils/threads/thread_delegate.h
index 47e68f1e8..66ad30241 100644
--- a/src/components/include/utils/threads/thread_delegate.h
+++ b/src/components/include/utils/threads/thread_delegate.h
@@ -35,30 +35,66 @@
#include <pthread.h>
+#include "utils/lock.h"
+
namespace threads {
+enum ThreadState {
+ kInit = 0,
+ kStarted = 1,
+ kStopReq = 2
+};
+
+class Thread;
+
/**
* Thread procedure interface.
* Look for "threads/thread.h" for example
*/
class ThreadDelegate {
- public:
+ public:
+ ThreadDelegate()
+ : state_(kInit),
+ thread_(NULL) {
+ }
+ /**
+ * \brief Thread procedure.
+ */
+ virtual void threadMain() = 0;
+
+ /**
+ * Should be called to free all resources allocated in threadMain
+ * and exiting threadMain
+ * This function should be blocking and return only when threadMain() will be
+ * finished in other case segmantation failes are possible
+ */
+ virtual void exitThreadMain();
- /**
- * Thread procedure.
- */
- virtual void threadMain() = 0;
+ virtual ~ThreadDelegate();
- /**
- * Should be called to free all resources allocated in threadMain
- * and exiting threadMain
- * This function should be blocking and return only when threadMain() will be
- * finished in other case segmantation failes are possible
- */
- virtual bool exitThreadMain() {
- return false;
+ Thread* thread() const {
+ return thread_;
+ }
+
+ void set_thread(Thread *thread);
+
+ bool ImproveState(unsigned int to) {
+ state_lock_.Lock();
+ if ((state_ + 1 == to) || (to == kInit && state_ == kStopReq)) {
+ state_ = to;
}
- virtual ~ThreadDelegate() { }
+ state_lock_.Unlock();
+ return state_ == to;
+ }
+
+ unsigned int state() const {
+ return state_;
+ }
+
+ private:
+ volatile unsigned int state_;
+ sync_primitives::SpinMutex state_lock_;
+ Thread* thread_;
};
} // namespace threads