OregonCore  revision be9e804-git
Your Favourite TBC server
SqlDelayThread.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
19 #include "Database/SqlOperations.h"
20 #include "DatabaseEnv.h"
21 
22 SqlDelayThread::SqlDelayThread(Database* db) : m_dbEngine(db), m_running(true)
23 {
24 }
25 
27 {
28  mysql_thread_init();
29 
30  SqlAsyncTask* s = NULL;
31 
32  ACE_Time_Value _time(2);
33  while (m_running)
34  {
35  // if the running state gets turned off while sleeping
36  // empty the queue before exiting
37  s = dynamic_cast<SqlAsyncTask*> (m_sqlQueue.dequeue());
38  if (s)
39  {
40  s->call();
41  delete s;
42  }
43  }
44 
45  mysql_thread_end();
46 }
47 
49 {
50  // wait for queue to become empty, so no query
51  // will be missed and also no memory leak will be created
52  while (m_sqlQueue.method_count())
54 
55  m_running = false;
56  m_sqlQueue.queue()->deactivate();
57 }
58 
60 {
61  int res = m_sqlQueue.enqueue(new SqlAsyncTask(m_dbEngine, sql));
62  return (res != -1);
63 }
64 
void run() override
Database * m_dbEngine
volatile bool m_running
static void Sleep(unsigned long msecs)
Definition: Threading.cpp:237
SqlQueue m_sqlQueue
bool Delay(SqlOperation *sql)
true
Definition: Log.cpp:534