databasetasks.h

main 61 lines · 1.8 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifndef FS_DATABASETASKS_H_9CBA08E9F5FEBA7275CCEE6560059576
21#define FS_DATABASETASKS_H_9CBA08E9F5FEBA7275CCEE6560059576
22
23#include <condition_variable>
24#include "thread_holder_base.h"
25#include "database.h"
26#include "enums.h"
27
28struct DatabaseTask {
29 DatabaseTask(std::string&& query, std::function<void(DBResult_ptr, bool)>&& callback, bool store) :
30 query(std::move(query)), callback(std::move(callback)), store(store) {}
31
32 std::string query;
33 std::function<void(DBResult_ptr, bool)> callback;
34 bool store;
35};
36
37class DatabaseTasks : public ThreadHolder<DatabaseTasks>
38{
39 public:
40 DatabaseTasks() = default;
41 void start();
42 void flush();
43 void shutdown();
44
45 void addTask(std::string query, std::function<void(DBResult_ptr, bool)> callback = nullptr, bool store = false);
46
47 void threadMain();
48 private:
49 void runTask(const DatabaseTask& task);
50
51 Database db;
52 std::thread thread;
53 std::list<DatabaseTask> tasks;
54 std::mutex taskLock;
55 std::condition_variable taskSignal;
56};
57
58extern DatabaseTasks g_databaseTasks;
59
60#endif
61
Top