PIThreadNotifier, rewrite PIObject::deleteLater()
tests for PIThreadNotifier and PIObject::deleteLater()
This commit is contained in:
@@ -698,28 +698,30 @@ bool PIObject::Connection::disconnect() {
|
||||
PRIVATE_DEFINITION_START(PIObject::Deleter)
|
||||
PIThread thread;
|
||||
PIConditionVariable cond_var;
|
||||
PIMutex cond_mutex, queue_mutex;
|
||||
PIVector<PIObject*> obj_queue;
|
||||
PRIVATE_DEFINITION_END(PIObject::Deleter)
|
||||
|
||||
|
||||
PIObject::Deleter::Deleter() {
|
||||
//piCout << "Deleter start ...";
|
||||
stopping = started = posted = false;
|
||||
CONNECTL(&(PRIVATE->thread), started, [this](){proc();});
|
||||
PRIVATE->thread.startOnce();
|
||||
while (!started) piMSleep(PIP_MIN_MSLEEP);
|
||||
PRIVATE->thread.setSlot([this](){
|
||||
PIVector<PIObject*> oq;
|
||||
PRIVATE->thread.lock();
|
||||
while(PRIVATE->obj_queue.isEmpty()) PRIVATE->cond_var.wait(PRIVATE->thread.mutex());
|
||||
oq.swap(PRIVATE->obj_queue);
|
||||
PRIVATE->thread.unlock();
|
||||
for (PIObject * o : oq) deleteObject(o);
|
||||
});
|
||||
PRIVATE->thread.start();
|
||||
}
|
||||
|
||||
|
||||
PIObject::Deleter::~Deleter() {
|
||||
//piCout << "~Deleter ...";
|
||||
stopping = true;
|
||||
PRIVATE->thread.stop();
|
||||
PRIVATE->cond_var.notifyAll();
|
||||
#ifndef WINDOWS
|
||||
while (PRIVATE->thread.isRunning()) piMSleep(PIP_MIN_MSLEEP);
|
||||
#endif
|
||||
deleteAll();
|
||||
PRIVATE->thread.waitForFinish();
|
||||
for (PIObject * o : PRIVATE->obj_queue) deleteObject(o);
|
||||
//piCout << "~Deleter ok";
|
||||
}
|
||||
|
||||
@@ -733,46 +735,13 @@ PIObject::Deleter * PIObject::Deleter::instance() {
|
||||
void PIObject::Deleter::post(PIObject * o) {
|
||||
if (!o->isPIObject()) return;
|
||||
//piCout << "[Deleter] post" << o << "...";
|
||||
PRIVATE->queue_mutex.lock();
|
||||
if (!PRIVATE->obj_queue.contains(o))
|
||||
PRIVATE->thread.lock();
|
||||
if (!PRIVATE->obj_queue.contains(o)) {
|
||||
PRIVATE->obj_queue << o;
|
||||
PRIVATE->queue_mutex.unlock();
|
||||
PRIVATE->cond_var.notifyAll();
|
||||
posted = true;
|
||||
//piCout << "[Deleter] post" << o << "done";
|
||||
}
|
||||
|
||||
|
||||
void PIObject::Deleter::proc() {
|
||||
//piCout << "[Deleter] proc start";
|
||||
while (!stopping) {
|
||||
//piMSleep(1);
|
||||
//piCout << "[Deleter] proc wait ...";
|
||||
if (posted) {
|
||||
posted = false;
|
||||
started = true;
|
||||
} else {
|
||||
PRIVATE->cond_mutex.lock();
|
||||
started = true;
|
||||
PRIVATE->cond_var.wait(PRIVATE->cond_mutex);
|
||||
PRIVATE->cond_mutex.unlock();
|
||||
}
|
||||
//piCout << "[Deleter] proc wait done";
|
||||
deleteAll();
|
||||
PRIVATE->cond_var.notifyAll();
|
||||
}
|
||||
//piCout << "[Deleter] proc end ok";
|
||||
}
|
||||
|
||||
|
||||
void PIObject::Deleter::deleteAll() {
|
||||
PIVector<PIObject*> oq;
|
||||
PRIVATE->queue_mutex.lock();
|
||||
oq = PRIVATE->obj_queue;
|
||||
//piCout << "[Deleter] deleteAll" << oq.size_s() << "...";
|
||||
PRIVATE->obj_queue.clear();
|
||||
PRIVATE->queue_mutex.unlock();
|
||||
piForeach (PIObject * o, oq)
|
||||
deleteObject(o);
|
||||
PRIVATE->thread.unlock();
|
||||
//piCout << "[Deleter] post" << o << "done";
|
||||
}
|
||||
|
||||
|
||||
@@ -782,7 +751,7 @@ void PIObject::Deleter::deleteObject(PIObject * o) {
|
||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic ...";
|
||||
while (o->isInEvent()) piMSleep(PIP_MIN_MSLEEP);
|
||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "wait atomic done";
|
||||
if (o->isPIObject()) delete o;
|
||||
delete o;
|
||||
}
|
||||
//piCout << "[Deleter] delete" << (uintptr_t)o << "done";
|
||||
}
|
||||
|
||||
@@ -503,10 +503,7 @@ private:
|
||||
static Deleter * instance();
|
||||
void post(PIObject * o);
|
||||
private:
|
||||
void proc();
|
||||
void deleteAll();
|
||||
void deleteObject(PIObject * o);
|
||||
std::atomic_bool stopping, started, posted;
|
||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||
};
|
||||
|
||||
|
||||
@@ -29,5 +29,6 @@
|
||||
#include "piconditionvar.h"
|
||||
#include "pithreadpoolexecutor.h"
|
||||
#include "pithreadpoolloop.h"
|
||||
#include "pithreadnotifier.h"
|
||||
|
||||
#endif // PITHREADMODULE_H
|
||||
|
||||
39
libs/main/thread/pithreadnotifier.cpp
Normal file
39
libs/main/thread/pithreadnotifier.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Class for simply notify and wait in different threads
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pithreadnotifier.h"
|
||||
|
||||
|
||||
PIThreadNotifier::PIThreadNotifier() : cnt(0) {}
|
||||
|
||||
|
||||
void PIThreadNotifier::wait() {
|
||||
m.lock();
|
||||
while (cnt == 0) v.wait(m);
|
||||
cnt--;
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
|
||||
void PIThreadNotifier::notifyOnce() {
|
||||
m.lock();
|
||||
cnt++;
|
||||
v.notifyAll();
|
||||
m.unlock();
|
||||
}
|
||||
52
libs/main/thread/pithreadnotifier.h
Normal file
52
libs/main/thread/pithreadnotifier.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*! @file pithreadnotifier.h
|
||||
* @brief Class for simply notify and wait in different threads
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Class for simply notify and wait in different threads
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PITHREADNOTIFIER_H
|
||||
#define PITHREADNOTIFIER_H
|
||||
|
||||
#include "piconditionvar.h"
|
||||
|
||||
|
||||
class PIP_EXPORT PIThreadNotifier {
|
||||
public:
|
||||
PIThreadNotifier();
|
||||
|
||||
//! Start waiting, return if other thread call \a notifyOnce().
|
||||
//! If \a notifyOnce() has been called before, then returns immediately.
|
||||
//! If notifyOnce() has been called "n" times, then returns immediately "n" times,
|
||||
//! but only if wait in one thread.
|
||||
//! If many threads waiting, then If notifyOnce() has been called "n" times,
|
||||
//! All threads total returns "n" times in random sequence.
|
||||
void wait();
|
||||
|
||||
//! Notify one waiting thread, wich waiting on \a wait() function.
|
||||
//! If many threads waiting, then notify randomly one.
|
||||
//! If call this "n" times, then notify any waiting threads totally "n" times.
|
||||
void notifyOnce();
|
||||
|
||||
private:
|
||||
ullong cnt;
|
||||
PIMutex m;
|
||||
PIConditionVariable v;
|
||||
};
|
||||
|
||||
#endif // PITHREADNOTIFIER_H
|
||||
Reference in New Issue
Block a user