| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : worker_thread.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#ifndef WORKER_THREAD_H |
| 26 |
#define WORKER_THREAD_H |
| 27 |
|
| 28 |
#include <deque> |
| 29 |
#include "wx/thread.h" |
| 30 |
#include "wx/event.h" |
| 31 |
#include "codelite_exports.h" |
| 32 |
#include <wx/msgqueue.h> |
| 33 |
#include <queue> |
| 34 |
#include <condition_variable> |
| 35 |
#include <mutex> |
| 36 |
|
| 37 |
/** |
| 38 |
* Base class for thread requests, |
| 39 |
*/ |
| 40 |
class WXDLLIMPEXP_CL ThreadRequest |
| 41 |
{ |
| 42 |
public: |
| 43 |
ThreadRequest(){}; |
| 44 |
virtual ~ThreadRequest(){}; |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* Worker Thread class |
| 49 |
* usually user should define the ProcessRequest method |
| 50 |
*/ |
| 51 |
class WXDLLIMPEXP_CL WorkerThread : public wxThread |
| 52 |
{ |
| 53 |
protected: |
| 54 |
wxEvtHandler* m_notifiedWindow; |
| 55 |
std::mutex m_mutex; |
| 56 |
std::condition_variable m_cv; |
| 57 |
std::queue<ThreadRequest*> m_Q; |
| 58 |
|
| 59 |
public: |
| 60 |
/** |
| 61 |
* Default constructor. |
| 62 |
*/ |
| 63 |
WorkerThread(); |
| 64 |
|
| 65 |
/** |
| 66 |
* Destructor. |
| 67 |
*/ |
| 68 |
virtual ~WorkerThread(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Thread execution point. |
| 72 |
*/ |
| 73 |
virtual void* Entry(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Called when the thread exits |
| 77 |
* whether it terminates normally or is stopped with Delete() (but not when it is Kill()'ed!) |
| 78 |
*/ |
| 79 |
virtual void OnExit(){}; |
| 80 |
|
| 81 |
/** |
| 82 |
* Add a request to the worker thread |
| 83 |
* \param request request to execute. |
| 84 |
*/ |
| 85 |
void Add(ThreadRequest* request); |
| 86 |
|
| 87 |
/** |
| 88 |
* @brief clear the request queue |
| 89 |
*/ |
| 90 |
void ClearQueue(); |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the window to be notified when a change was done |
| 94 |
* between current source file tree and the actual tree. |
| 95 |
* \param evtHandler |
| 96 |
*/ |
| 97 |
void SetNotifyWindow(wxEvtHandler* evtHandler) { m_notifiedWindow = evtHandler; } |
| 98 |
|
| 99 |
wxEvtHandler* GetNotifiedWindow() { return m_notifiedWindow; } |
| 100 |
/** |
| 101 |
* Stops the thread |
| 102 |
* This function returns only when the thread is terminated. |
| 103 |
* \note This call must be called from the context of other thread (e.g. main thread) |
| 104 |
*/ |
| 105 |
void Stop(); |
| 106 |
|
| 107 |
/** |
| 108 |
* Start the thread as joinable thread. |
| 109 |
* \note This call must be called from the context of other thread (e.g. main thread) |
| 110 |
*/ |
| 111 |
void Start(int priority = WXTHREAD_DEFAULT_PRIORITY); |
| 112 |
|
| 113 |
/** |
| 114 |
* Process request from the other thread |
| 115 |
* \param request ThreadRequest object to process |
| 116 |
*/ |
| 117 |
virtual void ProcessRequest(ThreadRequest* request) = 0; |
| 118 |
}; |
| 119 |
|
| 120 |
#endif // WORKER_THREAD_H |