| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : worker_thread.cpp |
| 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 |
#include "worker_thread.h" |
| 26 |
|
| 27 |
WorkerThread::WorkerThread() |
| 28 |
: wxThread(wxTHREAD_JOINABLE) |
| 29 |
, m_notifiedWindow(NULL) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
WorkerThread::~WorkerThread() { Stop(); } |
| 34 |
|
| 35 |
static ThreadRequest* QueueGet(std::mutex& m, std::queue<ThreadRequest*>& q, std::condition_variable& cv) |
| 36 |
{ |
| 37 |
std::unique_lock<std::mutex> lock{ m }; // acquiring the mutex |
| 38 |
while(q.empty()) { |
| 39 |
cv.wait(lock); // waiting to be notified of new data |
| 40 |
} |
| 41 |
|
| 42 |
ThreadRequest* req = q.front(); // moving the front element |
| 43 |
q.pop(); // which is immediately deleted |
| 44 |
|
| 45 |
return req; |
| 46 |
} |
| 47 |
|
| 48 |
static void QueuePut(std::mutex& m, std::queue<ThreadRequest*>& q, std::condition_variable& cv, ThreadRequest* req) |
| 49 |
{ |
| 50 |
std::unique_lock<std::mutex> lock{ m }; // Acquires the mutex |
| 51 |
q.push(req); // Forward the param to the |
| 52 |
lock.unlock(); // operation on the wrapped collection |
| 53 |
cv.notify_one(); |
| 54 |
} |
| 55 |
|
| 56 |
void* WorkerThread::Entry() |
| 57 |
{ |
| 58 |
while(true) { |
| 59 |
// Did we get a request to terminate? |
| 60 |
if(TestDestroy()) break; |
| 61 |
|
| 62 |
// Get the next entry from the queue |
| 63 |
ThreadRequest* request = QueueGet(m_mutex, m_Q, m_cv); |
| 64 |
if(request == nullptr) { |
| 65 |
// this dummy message was sent to tell us to exit |
| 66 |
break; |
| 67 |
} |
| 68 |
// Call user's implementation for processing request |
| 69 |
ProcessRequest(request); |
| 70 |
wxDELETE(request); |
| 71 |
} |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
void WorkerThread::Add(ThreadRequest* request) |
| 76 |
{ |
| 77 |
if(!request) { return; } |
| 78 |
QueuePut(m_mutex, m_Q, m_cv, request); |
| 79 |
} |
| 80 |
|
| 81 |
void WorkerThread::Stop() |
| 82 |
{ |
| 83 |
// Notify the thread to exit and |
| 84 |
// wait for it |
| 85 |
QueuePut(m_mutex, m_Q, m_cv, nullptr); // Make sure that the thread wakes up |
| 86 |
|
| 87 |
if(IsAlive()) { |
| 88 |
Delete(NULL, wxTHREAD_WAIT_BLOCK); |
| 89 |
|
| 90 |
} else { |
| 91 |
Wait(wxTHREAD_WAIT_BLOCK); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void WorkerThread::Start(int priority) |
| 96 |
{ |
| 97 |
Create(); |
| 98 |
SetPriority(priority); |
| 99 |
Run(); |
| 100 |
} |
| 101 |
|
| 102 |
void WorkerThread::ClearQueue() |
| 103 |
{ |
| 104 |
std::unique_lock<std::mutex> lk(m_mutex); |
| 105 |
while(!m_Q.empty()) { |
| 106 |
m_Q.pop(); |
| 107 |
} |
| 108 |
} |