Mercurial > hg > dmlib
comparison src/dmres.c @ 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 | 6e28a33ed851 |
children | 78a0f44aa8b5 |
comparison
equal
deleted
inserted
replaced
2089:dcca36701cdd | 2090:0d7c73f42a0b |
---|---|
21 # include <unistd.h> | 21 # include <unistd.h> |
22 # include <dirent.h> | 22 # include <dirent.h> |
23 #endif | 23 #endif |
24 | 24 |
25 | 25 |
26 static inline int dmLockLibMutex(DMResourceLib *lib) | |
27 { | |
28 if (lib != NULL) | |
29 return dmMutexLock(lib->mutex); | |
30 else | |
31 return DMERR_OK; | |
32 } | |
33 | |
34 | |
35 static inline int dmUnlockLibMutex(DMResourceLib *lib) | |
36 { | |
37 if (lib != NULL) | |
38 return dmMutexUnlock(lib->mutex); | |
39 else | |
40 return DMERR_OK; | |
41 } | |
42 | |
43 | |
26 DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags) | 44 DMResource *dmResourceNew(DMResourceLib *lib, const char *filename, const size_t size, const int flags) |
27 { | 45 { |
28 DMResource *node = dmMalloc0(sizeof(DMResource)); | 46 DMResource *node = dmMalloc0(sizeof(DMResource)); |
29 if (node == NULL) | 47 if (node == NULL) |
30 return NULL; | 48 return NULL; |
67 void dmResourceFree(DMResource *node) | 85 void dmResourceFree(DMResource *node) |
68 { | 86 { |
69 if (node != NULL) | 87 if (node != NULL) |
70 { | 88 { |
71 #ifdef DM_DEBUG | 89 #ifdef DM_DEBUG |
72 if (node->lib != NULL) dmMutexLock(node->lib->mutex); | 90 dmLockLibMutex(node->lib); |
73 if (node->refcount > 0) | 91 if (node->refcount > 0) |
74 { | 92 { |
75 dmErrorMsg( | 93 dmErrorMsg( |
76 "Attempting to dmResourceFree(%p) node that has resfs %d > 0: '%s'.\n" | 94 "Attempting to dmResourceFree(%p) node that has resfs %d > 0: '%s'.\n" |
77 "FOPS=%p, fops->name=%s\n", | 95 "FOPS=%p, fops->name=%s\n", |
78 node, node->refcount, node->filename, | 96 node, node->refcount, node->filename, |
79 node->fops, (node->fops != NULL) ? node->fops->name : "UNDEF"); | 97 node->fops, (node->fops != NULL) ? node->fops->name : "UNDEF"); |
80 } | 98 } |
81 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); | 99 dmUnlockLibMutex(node->lib); |
82 #endif | 100 #endif |
83 dmResourceFreeResData(node); | 101 dmResourceFreeResData(node); |
84 dmResourceFreeRawData(node); | 102 dmResourceFreeRawData(node); |
85 dmFree(node->filename); | 103 dmFree(node->filename); |
86 dmFree(node); | 104 dmFree(node); |
132 DMResource *node, *found = NULL; | 150 DMResource *node, *found = NULL; |
133 | 151 |
134 if (lib == NULL) | 152 if (lib == NULL) |
135 return NULL; | 153 return NULL; |
136 | 154 |
137 dmMutexLock(lib->mutex); | 155 dmLockLibMutex(lib); |
138 | 156 |
139 for (node = lib->resources; node != NULL; node = node->next) | 157 for (node = lib->resources; node != NULL; node = node->next) |
140 { | 158 { |
141 if (strcmp(node->filename, filename) == 0) | 159 if (strcmp(node->filename, filename) == 0) |
142 { | 160 { |
143 found = node; | 161 found = node; |
144 break; | 162 break; |
145 } | 163 } |
146 } | 164 } |
147 | 165 |
148 dmMutexUnlock(lib->mutex); | 166 dmUnlockLibMutex(lib); |
149 | 167 |
150 return found; | 168 return found; |
151 } | 169 } |
152 | 170 |
153 | 171 |
981 return dmfwrite(str, strlen(str), 1, fh) == 1 ? 1 : EOF; | 999 return dmfwrite(str, strlen(str), 1, fh) == 1 ? 1 : EOF; |
982 } | 1000 } |
983 | 1001 |
984 int dmResourceRef(DMResource *node) | 1002 int dmResourceRef(DMResource *node) |
985 { | 1003 { |
986 if (node->lib != NULL) dmMutexLock(node->lib->mutex); | 1004 dmLockLibMutex(node->lib); |
987 node->atime = time(NULL); | 1005 node->atime = time(NULL); |
988 node->refcount++; | 1006 node->refcount++; |
989 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); | 1007 dmUnlockLibMutex(node->lib); |
990 | 1008 |
991 return node->refcount; | 1009 return node->refcount; |
992 } | 1010 } |
993 | 1011 |
994 | 1012 |
995 int dmResourceUnref(DMResource *node) | 1013 int dmResourceUnref(DMResource *node) |
996 { | 1014 { |
997 if (node->lib != NULL) dmMutexLock(node->lib->mutex); | 1015 dmLockLibMutex(node->lib); |
998 node->refcount--; | 1016 node->refcount--; |
999 if (node->lib != NULL) dmMutexUnlock(node->lib->mutex); | 1017 dmUnlockLibMutex(node->lib); |
1000 | 1018 |
1001 return node->refcount; | 1019 return node->refcount; |
1002 } | 1020 } |
1003 | 1021 |
1004 | 1022 |
1009 struct dirent *dh; | 1027 struct dirent *dh; |
1010 DIR *hdir = opendir(path); | 1028 DIR *hdir = opendir(path); |
1011 if (hdir == NULL) | 1029 if (hdir == NULL) |
1012 return dmGetErrno(); | 1030 return dmGetErrno(); |
1013 | 1031 |
1014 dmMutexLock(lib->mutex); | 1032 dmLockLibMutex(lib); |
1015 | 1033 |
1016 do | 1034 do |
1017 { | 1035 { |
1018 DMResource *node = NULL; | 1036 DMResource *node = NULL; |
1019 dh = readdir(hdir); | 1037 dh = readdir(hdir); |
1040 dmResourceInsert(lib, node); | 1058 dmResourceInsert(lib, node); |
1041 } | 1059 } |
1042 } while (dh != NULL); | 1060 } while (dh != NULL); |
1043 | 1061 |
1044 out: | 1062 out: |
1045 dmMutexUnlock(lib->mutex); | 1063 dmUnlockLibMutex(lib); |
1046 | 1064 |
1047 #ifdef __WIN32 | 1065 #ifdef __WIN32 |
1048 #else | 1066 #else |
1049 closedir(hdir); | 1067 closedir(hdir); |
1050 #endif | 1068 #endif |
1155 DMResource *node; | 1173 DMResource *node; |
1156 | 1174 |
1157 if (lib == NULL) | 1175 if (lib == NULL) |
1158 return DMERR_NULLPTR; | 1176 return DMERR_NULLPTR; |
1159 | 1177 |
1160 dmMutexLock(lib->mutex); | 1178 dmLockLibMutex(lib); |
1161 | 1179 |
1162 // Shutdown possible subsystems | 1180 // Shutdown possible subsystems |
1163 #ifdef DM_USE_PACKFS | 1181 #ifdef DM_USE_PACKFS |
1164 if (lib->flags & DRF_USE_PACK) | 1182 if (lib->flags & DRF_USE_PACK) |
1165 { | 1183 { |
1184 node = next; | 1202 node = next; |
1185 } | 1203 } |
1186 | 1204 |
1187 // Etc. | 1205 // Etc. |
1188 dmFree(lib->resPath); | 1206 dmFree(lib->resPath); |
1189 dmMutexUnlock(lib->mutex); | 1207 dmUnlockLibMutex(lib); |
1190 dmDestroyMutex(lib->mutex); | 1208 dmDestroyMutex(lib->mutex); |
1191 return DMERR_OK; | 1209 return DMERR_OK; |
1192 } | 1210 } |
1193 | 1211 |
1194 | 1212 |
1195 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total) | 1213 int dmResourcesPreload(DMResourceLib *lib, BOOL start, int *loaded, int *total) |
1196 { | 1214 { |
1197 int ret = DMERR_OK; | 1215 int ret = DMERR_OK; |
1198 | 1216 |
1199 dmMutexLock(lib->mutex); | 1217 dmLockLibMutex(lib); |
1200 | 1218 |
1201 // Initialize preloading | 1219 // Initialize preloading |
1202 if (lib->preload == NULL || start) | 1220 if (lib->preload == NULL || start) |
1203 { | 1221 { |
1204 DMResource *node; | 1222 DMResource *node; |
1227 | 1245 |
1228 (*loaded)++; | 1246 (*loaded)++; |
1229 lib->preload = lib->preload->next; | 1247 lib->preload = lib->preload->next; |
1230 } | 1248 } |
1231 | 1249 |
1232 dmMutexUnlock(lib->mutex); | 1250 dmUnlockLibMutex(lib); |
1233 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; | 1251 return (lib->preload == NULL) ? DMERR_OK : DMERR_PROGRESS; |
1234 | 1252 |
1235 error: | 1253 error: |
1236 dmMutexUnlock(lib->mutex); | 1254 dmUnlockLibMutex(lib); |
1237 return ret; | 1255 return ret; |
1238 } | 1256 } |
1239 | 1257 |
1240 | 1258 |
1241 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags) | 1259 void dmResourcePrune(DMResourceLib *lib, const int agems, int const flags) |
1242 { | 1260 { |
1243 DMResource *node; | 1261 DMResource *node; |
1244 const int stamp = time(NULL); | 1262 const int stamp = time(NULL); |
1245 dmMutexLock(lib->mutex); | 1263 dmLockLibMutex(lib); |
1246 | 1264 |
1247 for (node = lib->resources; node != NULL; node = node->next) | 1265 for (node = lib->resources; node != NULL; node = node->next) |
1248 { | 1266 { |
1249 // Check if node has refcount of 0 and is | 1267 // Check if node has refcount of 0 and is |
1250 // not marked as persistent resource | 1268 // not marked as persistent resource |
1259 dmResourceFreeRawData(node); | 1277 dmResourceFreeRawData(node); |
1260 } | 1278 } |
1261 } | 1279 } |
1262 } | 1280 } |
1263 | 1281 |
1264 dmMutexUnlock(lib->mutex); | 1282 dmUnlockLibMutex(lib); |
1265 } | 1283 } |
1266 | 1284 |
1267 | 1285 |
1268 /* Helper resource access routines | 1286 /* Helper resource access routines |
1269 */ | 1287 */ |