libutils/source/system/AsyncDeleter.cpp

64 lines
2.3 KiB
C++
Raw Normal View History

2017-10-29 09:24:06 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program 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.
*
* This program 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 <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "AsyncDeleter.h"
AsyncDeleter * AsyncDeleter::deleterInstance = NULL;
AsyncDeleter::AsyncDeleter()
2018-03-11 16:44:04 +01:00
: CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff)
, exitApplication(false) {
2017-10-29 09:24:06 +01:00
}
2018-03-11 16:44:04 +01:00
AsyncDeleter::~AsyncDeleter() {
2017-10-29 09:24:06 +01:00
exitApplication = true;
}
2018-03-11 16:44:04 +01:00
void AsyncDeleter::triggerDeleteProcess(void) {
if(!deleterInstance){
return;
}
2017-10-29 09:24:06 +01:00
//! to trigger the event after GUI process is finished execution
//! this function is used to swap elements from one to next array
2018-03-11 16:44:04 +01:00
if(!deleterInstance->deleteElements.empty()) {
2017-10-29 09:24:06 +01:00
deleterInstance->deleteMutex.lock();
2018-03-11 16:44:04 +01:00
while(!deleterInstance->deleteElements.empty()) {
2017-10-29 09:24:06 +01:00
deleterInstance->realDeleteElements.push(deleterInstance->deleteElements.front());
deleterInstance->deleteElements.pop();
}
deleterInstance->deleteMutex.unlock();
deleterInstance->resumeThread();
}
}
2018-03-11 16:44:04 +01:00
void AsyncDeleter::executeThread(void) {
while(!exitApplication || !realDeleteElements.empty()) {
2017-10-29 09:24:06 +01:00
if(realDeleteElements.empty()) suspendThread();
//! delete elements that require post process deleting
//! because otherwise they would block or do invalid access on GUI thread
2018-03-11 16:44:04 +01:00
while(!realDeleteElements.empty()) {
2017-10-29 09:24:06 +01:00
deleteMutex.lock();
AsyncDeleter::Element *element = realDeleteElements.front();
realDeleteElements.pop();
deleteMutex.unlock();
delete element;
}
}
}