Skip to content

Commit 3eb8800

Browse files
committed
use raw pointer
1 parent db15f9b commit 3eb8800

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

include/neug/main/neug_db.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,7 @@ class NeugDB {
237237
*
238238
* @return true if a NeugDBService is associated with this database.
239239
*/
240-
inline bool HasActiveService() const {
241-
return active_service_.load() != nullptr;
242-
}
240+
bool HasActiveService() const;
243241

244242
/**
245243
* @brief Create a new connection to the database for query execution.
@@ -422,11 +420,11 @@ class NeugDB {
422420
// Serializes the check-and-set sections of Close() and RegisterService()
423421
// so that closing the database and registering a service can never
424422
// interleave.
425-
std::mutex service_mutex_;
423+
mutable std::mutex service_mutex_;
426424

427425
// The NeugDBService currently associated with this database, nullptr if
428-
// none. At most one service can be associated at any given time.
429-
std::atomic<NeugDBService*> active_service_{nullptr};
426+
// none. All access is protected by service_mutex_.
427+
NeugDBService* active_service_{nullptr};
430428
};
431429

432430
} // namespace neug

src/main/neug_db.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void NeugDB::Close() {
183183
// closed flag update are atomic with respect to service registration,
184184
// so no rollback or re-check is needed and Close() stays idempotent.
185185
std::lock_guard<std::mutex> lock(service_mutex_);
186-
if (HasActiveService()) {
186+
if (active_service_ != nullptr) {
187187
THROW_RUNTIME_ERROR(
188188
"Cannot close NeugDB while a NeugDBService is still associated "
189189
"with it. Stop and destroy the service first.");
@@ -227,14 +227,24 @@ void NeugDB::Close() {
227227
}
228228

229229
std::shared_ptr<Connection> NeugDB::Connect() {
230-
if (HasActiveService()) {
230+
std::lock_guard<std::mutex> lock(service_mutex_);
231+
if (IsClosed()) {
232+
THROW_RUNTIME_ERROR(
233+
"Cannot create connection on a closed NeugDB instance.");
234+
}
235+
if (active_service_ != nullptr) {
231236
THROW_RUNTIME_ERROR(
232237
"Cannot create connection while the database is being served by a "
233238
"NeugDBService.");
234239
}
235240
return connection_manager_->CreateConnection();
236241
}
237242

243+
bool NeugDB::HasActiveService() const {
244+
std::lock_guard<std::mutex> lock(service_mutex_);
245+
return active_service_ != nullptr;
246+
}
247+
238248
void NeugDB::RegisterService(NeugDBService* svc) {
239249
// Serialized with Close(): either the database is closed first (and this
240250
// registration is rejected), or the service registers first (and Close()
@@ -245,20 +255,22 @@ void NeugDB::RegisterService(NeugDBService* svc) {
245255
THROW_RUNTIME_ERROR(
246256
"Cannot register a NeugDBService on a closed NeugDB instance.");
247257
}
248-
NeugDBService* expected = nullptr;
249-
if (!active_service_.compare_exchange_strong(expected, svc)) {
258+
if (active_service_ != nullptr) {
250259
THROW_RUNTIME_ERROR(
251260
"NeugDB instance is already associated with a NeugDBService. Only "
252261
"one service instance is allowed per database.");
253262
}
263+
active_service_ = svc;
254264
}
255265

256266
void NeugDB::UnregisterService(NeugDBService* svc) {
257-
NeugDBService* expected = svc;
258-
if (!active_service_.compare_exchange_strong(expected, nullptr)) {
267+
std::lock_guard<std::mutex> lock(service_mutex_);
268+
if (active_service_ != svc) {
259269
LOG(WARNING) << "UnregisterService: the given service is not the active "
260270
"service of this database.";
271+
return;
261272
}
273+
active_service_ = nullptr;
262274
}
263275

264276
void NeugDB::RemoveConnection(std::shared_ptr<Connection> conn) {

0 commit comments

Comments
 (0)