Files
TCPlayerbotCore/src/server/database/Database/DatabaseWorker.cpp
T

62 lines
1.6 KiB
C++
Raw Normal View History

2010-12-13 09:18:49 +01:00
/*
2016-01-01 00:34:25 +01:00
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
2010-12-13 09:18:49 +01:00
*
* 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 2 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 "DatabaseEnv.h"
#include "DatabaseWorker.h"
#include "SQLOperation.h"
#include "MySQLConnection.h"
#include "MySQLThreading.h"
#include "ProducerConsumerQueue.h"
2010-12-13 09:18:49 +01:00
DatabaseWorker::DatabaseWorker(ProducerConsumerQueue<SQLOperation*>* newQueue, MySQLConnection* connection)
2010-12-13 09:18:49 +01:00
{
_connection = connection;
_queue = newQueue;
_cancelationToken = false;
_workerThread = std::thread(&DatabaseWorker::WorkerThread, this);
2010-12-13 09:18:49 +01:00
}
DatabaseWorker::~DatabaseWorker()
2010-12-13 09:18:49 +01:00
{
_cancelationToken = true;
_queue->Cancel();
_workerThread.join();
}
void DatabaseWorker::WorkerThread()
{
if (!_queue)
return;
2010-12-13 09:18:49 +01:00
2014-07-17 15:42:57 +02:00
for (;;)
2010-12-13 09:18:49 +01:00
{
SQLOperation* operation = nullptr;
2010-12-13 09:18:49 +01:00
_queue->WaitAndPop(operation);
2010-12-13 09:18:49 +01:00
if (_cancelationToken || !operation)
return;
2010-12-13 09:18:49 +01:00
operation->SetConnection(_connection);
operation->call();
delete operation;
}
2010-12-13 09:18:49 +01:00
}