changeset 2090:0d7c73f42a0b

Use helper functions for resource lib mutex locking.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 25 Feb 2019 12:43:08 +0200
parents dcca36701cdd
children 3b3acb6b4ba0
files src/dmres.c
diffstat 1 files changed, 35 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmres.c	Tue Feb 19 12:18:12 2019 +0200
+++ b/src/dmres.c	Mon Feb 25 12:43:08 2019 +0200
@@ -23,6 +23,24 @@
 #endif
 
 
+static inline int dmLockLibMutex(DMResourceLib *lib)
+{
+    if (lib != NULL)
+        return dmMutexLock(lib->mutex);
+    else
+        return DMERR_OK;
+}
+
+
+static inline int dmUnlockLibMutex(DMResourceLib *lib)
+{
+    if (lib != NULL)
+        return dmMutexUnlock(lib->mutex);
+    else
+        return DMERR_OK;
+}
+
+
 DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags)
 {
     DMResource *node = dmMalloc0(sizeof(DMResource));
@@ -69,7 +87,7 @@
     if (node != NULL)
     {
 #ifdef DM_DEBUG
-        if (node->lib != NULL) dmMutexLock(node->lib->mutex);
+        dmLockLibMutex(node->lib);
         if (node->refcount > 0)
         {
             dmErrorMsg(
@@ -78,7 +96,7 @@
                 node, node->refcount, node->filename,
                 node->fops, (node->fops != NULL) ? node->fops->name : "UNDEF");
         }
-        if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
+        dmUnlockLibMutex(node->lib);
 #endif
         dmResourceFreeResData(node);
         dmResourceFreeRawData(node);
@@ -134,7 +152,7 @@
     if (lib == NULL)
         return NULL;
 
-    dmMutexLock(lib->mutex);
+    dmLockLibMutex(lib);
 
     for (node = lib->resources; node != NULL; node = node->next)
     {
@@ -145,7 +163,7 @@
         }
     }
 
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
 
     return found;
 }
@@ -983,10 +1001,10 @@
 
 int dmResourceRef(DMResource *node)
 {
-    if (node->lib != NULL) dmMutexLock(node->lib->mutex);
+    dmLockLibMutex(node->lib);
     node->atime = time(NULL);
     node->refcount++;
-    if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
+    dmUnlockLibMutex(node->lib);
 
     return node->refcount;
 }
@@ -994,9 +1012,9 @@
 
 int dmResourceUnref(DMResource *node)
 {
-    if (node->lib != NULL) dmMutexLock(node->lib->mutex);
+    dmLockLibMutex(node->lib);
     node->refcount--;
-    if (node->lib != NULL) dmMutexUnlock(node->lib->mutex);
+    dmUnlockLibMutex(node->lib);
 
     return node->refcount;
 }
@@ -1011,7 +1029,7 @@
     if (hdir == NULL)
         return dmGetErrno();
 
-    dmMutexLock(lib->mutex);
+    dmLockLibMutex(lib);
 
     do
     {
@@ -1042,7 +1060,7 @@
     } while (dh != NULL);
 
 out:
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
 
 #ifdef __WIN32
 #else
@@ -1157,7 +1175,7 @@
     if (lib == NULL)
         return DMERR_NULLPTR;
 
-    dmMutexLock(lib->mutex);
+    dmLockLibMutex(lib);
 
     // Shutdown possible subsystems
 #ifdef DM_USE_PACKFS
@@ -1186,7 +1204,7 @@
 
     // Etc.
     dmFree(lib->resPath);
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
     dmDestroyMutex(lib->mutex);
     return DMERR_OK;
 }
@@ -1196,7 +1214,7 @@
 {
     int ret = DMERR_OK;
 
-    dmMutexLock(lib->mutex);
+    dmLockLibMutex(lib);
 
     // Initialize preloading
     if (lib->preload == NULL || start)
@@ -1229,11 +1247,11 @@
         lib->preload = lib->preload->next;
     }
 
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
     return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS;
 
 error:
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
     return ret;
 }
 
@@ -1242,7 +1260,7 @@
 {
     DMResource *node;
     const int stamp = time(NULL);
-    dmMutexLock(lib->mutex);
+    dmLockLibMutex(lib);
 
     for (node = lib->resources; node != NULL; node = node->next)
     {
@@ -1261,7 +1279,7 @@
         }
     }
 
-    dmMutexUnlock(lib->mutex);
+    dmUnlockLibMutex(lib);
 }