# HG changeset patch # User Matti Hamalainen # Date 1551091388 -7200 # Node ID 0d7c73f42a0bcb3715c8be8a67bf5d629221658f # Parent dcca36701cdd2fa9c1377867d2706ba1f53f67a0 Use helper functions for resource lib mutex locking. diff -r dcca36701cdd -r 0d7c73f42a0b src/dmres.c --- 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); }