forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databasetasks.cpp
101 lines (88 loc) · 2.39 KB
/
databasetasks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2016 Mark Samman <[email protected]>
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "databasetasks.h"
#include "tasks.h"
extern Dispatcher g_dispatcher;
void DatabaseTasks::start()
{
db.connect();
ThreadHolder::start();
}
void DatabaseTasks::threadMain()
{
std::unique_lock<std::mutex> taskLockUnique(taskLock, std::defer_lock);
while (getState() != THREAD_STATE_TERMINATED) {
taskLockUnique.lock();
if (tasks.empty()) {
taskSignal.wait(taskLockUnique);
}
if (!tasks.empty()) {
DatabaseTask task = std::move(tasks.front());
tasks.pop_front();
taskLockUnique.unlock();
runTask(task);
} else {
taskLockUnique.unlock();
}
}
}
void DatabaseTasks::addTask(const std::string& query, const std::function<void(DBResult_ptr, bool)>& callback/* = nullptr*/, bool store/* = false*/)
{
bool signal = false;
taskLock.lock();
if (getState() == THREAD_STATE_RUNNING) {
signal = tasks.empty();
tasks.emplace_back(query, callback, store);
}
taskLock.unlock();
if (signal) {
taskSignal.notify_one();
}
}
void DatabaseTasks::runTask(const DatabaseTask& task)
{
bool success;
DBResult_ptr result;
if (task.store) {
result = db.storeQuery(task.query);
success = true;
} else {
result = nullptr;
success = db.executeQuery(task.query);
}
if (task.callback) {
g_dispatcher.addTask(createTask(std::bind(task.callback, result, success)));
}
}
void DatabaseTasks::flush()
{
while (!tasks.empty()) {
runTask(tasks.front());
tasks.pop_front();
}
}
void DatabaseTasks::shutdown()
{
taskLock.lock();
setState(THREAD_STATE_TERMINATED);
flush();
taskLock.unlock();
taskSignal.notify_one();
}