MPD 0.17~git
src/db_lock.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2011 The Music Player Daemon Project
00003  * http://www.musicpd.org
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018  */
00019 
00026 #ifndef MPD_DB_LOCK_H
00027 #define MPD_DB_LOCK_H
00028 
00029 #include "check.h"
00030 
00031 #include <glib.h>
00032 #include <assert.h>
00033 #include <stdbool.h>
00034 
00035 extern GStaticMutex db_mutex;
00036 
00037 #ifndef NDEBUG
00038 
00039 extern GThread *db_mutex_holder;
00040 
00044 G_GNUC_PURE
00045 static inline bool
00046 holding_db_lock(void)
00047 {
00048         return db_mutex_holder == g_thread_self();
00049 }
00050 
00051 #endif
00052 
00057 static inline void
00058 db_lock(void)
00059 {
00060         assert(!holding_db_lock());
00061 
00062         g_static_mutex_lock(&db_mutex);
00063 
00064         assert(db_mutex_holder == NULL);
00065 #ifndef NDEBUG
00066         db_mutex_holder = g_thread_self();
00067 #endif
00068 }
00069 
00073 static inline void
00074 db_unlock(void)
00075 {
00076         assert(holding_db_lock());
00077 #ifndef NDEBUG
00078         db_mutex_holder = NULL;
00079 #endif
00080 
00081         g_static_mutex_unlock(&db_mutex);
00082 }
00083 
00084 #endif