From 51a2d09eb7fd8bc22a25e151878e6809b58bc21f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 2 Jan 2013 20:56:21 +0100 Subject: db_lock: convert to C++ --- Makefile.am | 4 +- src/DatabaseLock.cxx | 33 ++++++++++++++ src/DatabaseLock.hxx | 98 ++++++++++++++++++++++++++++++++++++++++ src/DatabaseSave.cxx | 2 +- src/Directory.cxx | 2 +- src/PlaylistVector.cxx | 2 +- src/StickerCommands.cxx | 2 +- src/UpdateArchive.cxx | 2 +- src/UpdateContainer.cxx | 2 +- src/UpdateDatabase.cxx | 2 +- src/UpdateSong.cxx | 2 +- src/UpdateWalk.cxx | 2 +- src/db/SimpleDatabasePlugin.cxx | 4 +- src/db_lock.c | 33 -------------- src/db_lock.h | 99 ----------------------------------------- 15 files changed, 144 insertions(+), 145 deletions(-) create mode 100644 src/DatabaseLock.cxx create mode 100644 src/DatabaseLock.hxx delete mode 100644 src/db_lock.c delete mode 100644 src/db_lock.h diff --git a/Makefile.am b/Makefile.am index 5811dac95..5cbc44da2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -234,7 +234,7 @@ src_mpd_SOURCES = \ src/DatabaseQueue.cxx src/DatabaseQueue.hxx \ src/DatabasePlaylist.cxx src/DatabasePlaylist.hxx \ src/db_error.h \ - src/db_lock.c src/db_lock.h \ + src/DatabaseLock.cxx src/DatabaseLock.hxx \ src/DatabaseSave.cxx src/DatabaseSave.hxx \ src/DatabasePlugin.hxx \ src/DatabaseVisitor.hxx \ @@ -1072,7 +1072,7 @@ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/DatabaseSelection.cxx \ src/Directory.cxx src/DirectorySave.cxx \ src/PlaylistVector.cxx src/PlaylistDatabase.cxx \ - src/db_lock.c src/DatabaseSave.cxx \ + src/DatabaseLock.cxx src/DatabaseSave.cxx \ src/Song.cxx src/song_sort.c src/SongSave.cxx \ src/tag.c src/tag_pool.c src/TagSave.cxx \ src/path.c \ diff --git a/src/DatabaseLock.cxx b/src/DatabaseLock.cxx new file mode 100644 index 000000000..872b9bfd3 --- /dev/null +++ b/src/DatabaseLock.cxx @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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 "config.h" +#include "DatabaseLock.hxx" +#include "gcc.h" + +#if GCC_CHECK_VERSION(4, 2) +/* workaround for a warning caused by G_STATIC_MUTEX_INIT */ +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +GStaticMutex db_mutex = G_STATIC_MUTEX_INIT; + +#ifndef NDEBUG +GThread *db_mutex_holder; +#endif diff --git a/src/DatabaseLock.hxx b/src/DatabaseLock.hxx new file mode 100644 index 000000000..6646fb43d --- /dev/null +++ b/src/DatabaseLock.hxx @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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. + */ + +/** \file + * + * Support for locking data structures from the database, for safe + * multi-threading. + */ + +#ifndef MPD_DB_LOCK_HXX +#define MPD_DB_LOCK_HXX + +#include "check.h" + +#include +#include + +extern GStaticMutex db_mutex; + +#ifndef NDEBUG + +extern GThread *db_mutex_holder; + +/** + * Does the current thread hold the database lock? + */ +G_GNUC_PURE +static inline bool +holding_db_lock(void) +{ + return db_mutex_holder == g_thread_self(); +} + +#endif + +/** + * Obtain the global database lock. This is needed before + * dereferencing a #song or #directory. It is not recursive. + */ +static inline void +db_lock(void) +{ + assert(!holding_db_lock()); + + g_static_mutex_lock(&db_mutex); + + assert(db_mutex_holder == NULL); +#ifndef NDEBUG + db_mutex_holder = g_thread_self(); +#endif +} + +/** + * Release the global database lock. + */ +static inline void +db_unlock(void) +{ + assert(holding_db_lock()); +#ifndef NDEBUG + db_mutex_holder = NULL; +#endif + + g_static_mutex_unlock(&db_mutex); +} + +#ifdef __cplusplus + +class ScopeDatabaseLock { +public: + ScopeDatabaseLock() { + db_lock(); + } + + ~ScopeDatabaseLock() { + db_unlock(); + } +}; + +#endif + +#endif diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx index 711d5b2f3..771699848 100644 --- a/src/DatabaseSave.cxx +++ b/src/DatabaseSave.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "DatabaseSave.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include "directory.h" #include "DirectorySave.hxx" #include "song.h" diff --git a/src/Directory.cxx b/src/Directory.cxx index a371dc41a..59859a0fd 100644 --- a/src/Directory.cxx +++ b/src/Directory.cxx @@ -21,13 +21,13 @@ #include "directory.h" #include "SongFilter.hxx" #include "PlaylistVector.hxx" +#include "DatabaseLock.hxx" extern "C" { #include "song.h" #include "song_sort.h" #include "path.h" #include "util/list_sort.h" -#include "db_lock.h" } #include diff --git a/src/PlaylistVector.cxx b/src/PlaylistVector.cxx index ea3172d0d..b9f671929 100644 --- a/src/PlaylistVector.cxx +++ b/src/PlaylistVector.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "PlaylistVector.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include #include diff --git a/src/StickerCommands.cxx b/src/StickerCommands.cxx index 7d23354fc..ad09f9b46 100644 --- a/src/StickerCommands.cxx +++ b/src/StickerCommands.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "StickerCommands.hxx" #include "SongPrint.hxx" +#include "DatabaseLock.hxx" extern "C" { #include "protocol/result.h" @@ -27,7 +28,6 @@ extern "C" { #include "sticker_print.h" #include "song_sticker.h" #include "database.h" -#include "db_lock.h" } #include diff --git a/src/UpdateArchive.cxx b/src/UpdateArchive.cxx index c57534ea4..441cf71c2 100644 --- a/src/UpdateArchive.cxx +++ b/src/UpdateArchive.cxx @@ -20,7 +20,7 @@ #include "config.h" /* must be first for large file support */ #include "UpdateArchive.hxx" #include "UpdateInternal.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include "directory.h" #include "song.h" diff --git a/src/UpdateContainer.cxx b/src/UpdateContainer.cxx index 68f8c3b98..7a904a1e6 100644 --- a/src/UpdateContainer.cxx +++ b/src/UpdateContainer.cxx @@ -21,7 +21,7 @@ #include "UpdateContainer.hxx" #include "UpdateInternal.hxx" #include "UpdateDatabase.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include "directory.h" #include "song.h" #include "decoder_plugin.h" diff --git a/src/UpdateDatabase.cxx b/src/UpdateDatabase.cxx index e959a4ecc..6ef6e3429 100644 --- a/src/UpdateDatabase.cxx +++ b/src/UpdateDatabase.cxx @@ -23,7 +23,7 @@ #include "PlaylistVector.hxx" #include "directory.h" #include "song.h" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include #include diff --git a/src/UpdateSong.cxx b/src/UpdateSong.cxx index 8241d46f2..48357b9cd 100644 --- a/src/UpdateSong.cxx +++ b/src/UpdateSong.cxx @@ -23,7 +23,7 @@ #include "UpdateIO.hxx" #include "UpdateDatabase.hxx" #include "UpdateContainer.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include "directory.h" #include "song.h" #include "decoder_plugin.h" diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index 33220ba76..860342001 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -23,7 +23,7 @@ #include "UpdateDatabase.hxx" #include "UpdateSong.hxx" #include "UpdateArchive.hxx" -#include "db_lock.h" +#include "DatabaseLock.hxx" #include "directory.h" #include "song.h" #include "PlaylistVector.hxx" diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index 4b92da6cf..85d707f52 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -23,10 +23,10 @@ #include "DatabaseHelpers.hxx" #include "SongFilter.hxx" #include "DatabaseSave.hxx" +#include "DatabaseLock.hxx" +#include "db_error.h" extern "C" { -#include "db_error.h" -#include "db_lock.h" #include "conf.h" } diff --git a/src/db_lock.c b/src/db_lock.c deleted file mode 100644 index 53759673d..000000000 --- a/src/db_lock.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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 "config.h" -#include "db_lock.h" -#include "gcc.h" - -#if GCC_CHECK_VERSION(4, 2) -/* workaround for a warning caused by G_STATIC_MUTEX_INIT */ -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" -#endif - -GStaticMutex db_mutex = G_STATIC_MUTEX_INIT; - -#ifndef NDEBUG -GThread *db_mutex_holder; -#endif diff --git a/src/db_lock.h b/src/db_lock.h deleted file mode 100644 index eed71eec0..000000000 --- a/src/db_lock.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * http://www.musicpd.org - * - * 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. - */ - -/** \file - * - * Support for locking data structures from the database, for safe - * multi-threading. - */ - -#ifndef MPD_DB_LOCK_H -#define MPD_DB_LOCK_H - -#include "check.h" - -#include -#include -#include - -extern GStaticMutex db_mutex; - -#ifndef NDEBUG - -extern GThread *db_mutex_holder; - -/** - * Does the current thread hold the database lock? - */ -G_GNUC_PURE -static inline bool -holding_db_lock(void) -{ - return db_mutex_holder == g_thread_self(); -} - -#endif - -/** - * Obtain the global database lock. This is needed before - * dereferencing a #song or #directory. It is not recursive. - */ -static inline void -db_lock(void) -{ - assert(!holding_db_lock()); - - g_static_mutex_lock(&db_mutex); - - assert(db_mutex_holder == NULL); -#ifndef NDEBUG - db_mutex_holder = g_thread_self(); -#endif -} - -/** - * Release the global database lock. - */ -static inline void -db_unlock(void) -{ - assert(holding_db_lock()); -#ifndef NDEBUG - db_mutex_holder = NULL; -#endif - - g_static_mutex_unlock(&db_mutex); -} - -#ifdef __cplusplus - -class ScopeDatabaseLock { -public: - ScopeDatabaseLock() { - db_lock(); - } - - ~ScopeDatabaseLock() { - db_unlock(); - } -}; - -#endif - -#endif -- cgit v1.2.3