Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
DatabaseQueue.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "DatabaseQueue.h"
7#include "SQLOperation.h"
9
10#include <future>
11#include <mutex>
12
13namespace Skyfire
14{
15 namespace
16 {
17 thread_local MySQLConnection* CurrentDatabaseConnection = nullptr;
18 }
19
21 {
23 : executor(), closed(false)
24 {
25 executor.KeepAlive();
26 }
27
29 std::mutex stateLock;
30 bool closed;
31 };
32
37
42
44 {
45 if (!operation)
46 return;
47
48 std::lock_guard<std::mutex> guard(_impl->stateLock);
49 if (_impl->closed)
50 return;
51
52 _impl->executor.Post(
53 [operation]
54 {
55 operation->SetConnection(CurrentDatabaseConnection);
56 operation->call();
57 delete operation;
58 });
59 }
60
61 int DatabaseQueue::run(MySQLConnection* connection)
62 {
63 if (!connection)
64 return -1;
65
66 BindConnection(connection);
67 _impl->executor.Run();
69 return 0;
70 }
71
73 {
74 std::lock_guard<std::mutex> guard(_impl->stateLock);
75 if (_impl->closed)
76 return;
77
78 _impl->closed = true;
79 _impl->executor.ResetWork();
80 }
81
83 {
84 std::shared_ptr<std::promise<void> > barrier(new std::promise<void>());
85 std::future<void> ready = barrier->get_future();
86
87 {
88 std::lock_guard<std::mutex> guard(_impl->stateLock);
89 if (_impl->closed)
90 return;
91
92 _impl->executor.Post(
93 [barrier]
94 {
95 barrier->set_value();
96 });
97 }
98
99 ready.wait();
100 }
101
103 {
104 return _impl->executor;
105 }
106
107 void DatabaseQueue::BindConnection(MySQLConnection* connection)
108 {
109 CurrentDatabaseConnection = connection;
110 }
111
113 {
114 CurrentDatabaseConnection = nullptr;
115 }
116}
virtual void SetConnection(MySQLConnection *con)
virtual int call()
std::unique_ptr< Impl > _impl
void BindConnection(MySQLConnection *connection)
void enqueue(SQLOperation *operation)
Asio::IoContextExecutor & GetExecutor()
int run(MySQLConnection *connection)
Skyfire::Asio::IoContextExecutor executor