2010-10-07 15:54:07 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
|
|
|
|
|
*
|
|
|
|
|
* 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/>.
|
2010-08-18 02:25:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _WORKERTHREAD_H
|
|
|
|
|
#define _WORKERTHREAD_H
|
|
|
|
|
|
|
|
|
|
#include <ace/Task.h>
|
|
|
|
|
#include <ace/Activation_Queue.h>
|
|
|
|
|
|
2010-12-04 23:42:23 +01:00
|
|
|
#include "DatabaseWorkerPool.h"
|
2010-12-04 23:14:37 +01:00
|
|
|
#include "SQLOperation.h"
|
|
|
|
|
#include "MySQLConnection.h"
|
|
|
|
|
#include "MySQLThreading.h"
|
|
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
class MySQLConnection;
|
|
|
|
|
|
2010-12-06 20:06:31 +01:00
|
|
|
// Forward declare
|
|
|
|
|
template <class T> class DatabaseWorkerPool;
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2010-09-27 00:20:56 +02:00
|
|
|
class DatabaseWorker : public ACE_Task_Base
|
2010-08-18 02:25:52 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2010-12-06 19:28:05 +01:00
|
|
|
DatabaseWorker(ACE_Activation_Queue* new_queue, DatabaseWorkerPool<T>* pool) :
|
2010-12-04 21:50:36 +01:00
|
|
|
m_queue(new_queue),
|
2010-12-06 19:28:05 +01:00
|
|
|
m_pool(pool)
|
2010-12-04 21:50:36 +01:00
|
|
|
{
|
|
|
|
|
/// Assign thread to task
|
|
|
|
|
activate();
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
|
|
|
|
|
///- Inherited from ACE_Task_Base
|
2010-12-04 21:50:36 +01:00
|
|
|
int svc()
|
|
|
|
|
{
|
|
|
|
|
if (!m_queue)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
SQLOperation *request = NULL;
|
2010-12-06 19:52:37 +01:00
|
|
|
MySQLConnection* con = NULL;
|
2010-12-04 21:50:36 +01:00
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
request = (SQLOperation*)(m_queue->dequeue());
|
|
|
|
|
if (!request)
|
|
|
|
|
break;
|
|
|
|
|
|
2010-12-06 19:52:37 +01:00
|
|
|
con = m_pool->GetFreeConnection();
|
2010-12-04 21:50:36 +01:00
|
|
|
request->SetConnection(con);
|
|
|
|
|
request->call();
|
|
|
|
|
con->Unlock();
|
|
|
|
|
|
|
|
|
|
delete request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int activate()
|
|
|
|
|
{
|
|
|
|
|
/* THR_DETACHED:
|
|
|
|
|
Create an asynchronous thread. The exit status of the thread would not be available to any other threads.
|
|
|
|
|
The thread resources are reclaimed by the operating system whenever the thread is terminated. */
|
|
|
|
|
|
|
|
|
|
/* THR_NEW_LWP:
|
|
|
|
|
Create an explicit kernel-level thread (as opposed to a user-level thread). */
|
|
|
|
|
|
|
|
|
|
ACE_Task_Base::activate(THR_NEW_LWP | THR_DETACHED, 1);
|
|
|
|
|
return 0; //^ - Spawn one thread to handle this task.
|
|
|
|
|
// However more of these tasks may be activated
|
|
|
|
|
// See DatabaseWorkerPool ctor.
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-26 20:55:09 +02:00
|
|
|
int wait() { return ACE_Task_Base::wait(); }
|
2010-08-18 02:25:52 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
DatabaseWorker() : ACE_Task_Base() {}
|
|
|
|
|
ACE_Activation_Queue* m_queue;
|
2010-12-06 19:28:05 +01:00
|
|
|
DatabaseWorkerPool<T>* m_pool; // Databasepool we operate on
|
2010-08-18 02:25:52 +02:00
|
|
|
};
|
|
|
|
|
|
2010-08-18 07:17:04 +02:00
|
|
|
#endif
|