2017-09-30 23:51:01 +02:00
|
|
|
#include "DbWorker.h"
|
|
|
|
#include "Operation.h"
|
2018-03-06 22:22:19 +01:00
|
|
|
#include "Util/LockedWaitQueue.h"
|
2017-09-30 23:51:01 +02:00
|
|
|
|
2019-03-11 22:48:33 +01:00
|
|
|
Sapphire::Db::DbWorker::DbWorker( Sapphire::LockedWaitQueue< std::shared_ptr< Operation > >* newQueue,
|
|
|
|
DbConnection* pConn )
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_pConn = pConn;
|
|
|
|
m_queue = newQueue;
|
|
|
|
m_cancelationToken = false;
|
|
|
|
m_workerThread = std::thread( &DbWorker::workerThread, this );
|
2017-09-30 23:51:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
Sapphire::Db::DbWorker::~DbWorker()
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_cancelationToken = true;
|
|
|
|
m_queue->cancel();
|
|
|
|
m_workerThread.join();
|
2017-09-30 23:51:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
void Sapphire::Db::DbWorker::workerThread()
|
2017-09-30 23:51:01 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !m_queue )
|
|
|
|
return;
|
2017-09-30 23:51:01 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
while( true )
|
|
|
|
{
|
2018-10-24 12:53:26 +02:00
|
|
|
std::shared_ptr< Operation > operation = nullptr;
|
2017-09-30 23:51:01 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_queue->waitAndPop( operation );
|
2017-09-30 23:51:01 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_cancelationToken || !operation )
|
|
|
|
return;
|
2017-09-30 23:51:01 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
operation->setConnection( m_pConn );
|
|
|
|
operation->call();
|
|
|
|
}
|
2017-09-30 23:51:01 +02:00
|
|
|
}
|