Let findmultiplefiles use a mutex when accessing each file query

This commit is contained in:
James R 2020-08-24 04:33:44 -07:00
parent bb07f12b42
commit 83560485d1
5 changed files with 45 additions and 14 deletions

View file

@ -1023,13 +1023,13 @@ filestatus_t checkfilemd5(char *filename, const UINT8 *wantedmd5sum)
// Rewritten by Monster Iestyn to be less stupid
// Note: if completepath is true, "filename" is modified, but only if FS_FOUND is going to be returned
// (Don't worry about WinCE's version of filesearch, nobody cares about that OS anymore)
filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, boolean completepath)
filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, boolean completepath, void *mutex_ptr)
{
filestatus_t homecheck; // store result of last file search
boolean badmd5 = false; // store whether md5 was bad from either of the first two searches (if nothing was found in the third)
// first, check SRB2's "home" directory
homecheck = filesearch(n, files, srb2home, checkmd5, completepath, 10);
homecheck = filesearch(n, files, srb2home, checkmd5, completepath, 10, mutex_ptr);
if (homecheck == FS_FOUND) // we found the file, so return that we have :)
return FS_FOUND;
@ -1038,7 +1038,7 @@ filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, bool
// if not found at all, just move on without doing anything
// next, check SRB2's "path" directory
homecheck = filesearch(n, files, srb2path, checkmd5, completepath, 10);
homecheck = filesearch(n, files, srb2path, checkmd5, completepath, 10, mutex_ptr);
if (homecheck == FS_FOUND) // we found the file, so return that we have :)
return FS_FOUND;
@ -1048,9 +1048,9 @@ filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, bool
// finally check "." directory
#ifdef _arch_dreamcast
homecheck = filesearch(n, files, "/cd", checkmd5, completepath, 10);
homecheck = filesearch(n, files, "/cd", checkmd5, completepath, 10, mutex_ptr);
#else
homecheck = filesearch(n, files, ".", checkmd5, completepath, 10);
homecheck = filesearch(n, files, ".", checkmd5, completepath, 10, mutex_ptr);
#endif
if (homecheck != FS_NOTFOUND) // if not found this time, fall back on the below return statement
@ -1062,7 +1062,7 @@ filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, bool
filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean completepath)
{
filequery_t q = { FS_NOTFOUND, wantedmd5sum, filename };
return findmultiplefiles(1, &q, ( wantedmd5sum != NULL ), completepath);
return findmultiplefiles(1, &q, ( wantedmd5sum != NULL ), completepath, NULL);
}
#ifdef HAVE_CURL

View file

@ -33,6 +33,7 @@
#include "m_misc.h"
#include "z_zone.h"
#include "m_menu.h" // Addons_option_Onchange
#include "i_threads.h"
#if (defined (_WIN32) && !defined (_WIN32_WCE)) && defined (_MSC_VER) && !defined (_XBOX)
@ -445,7 +446,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut)
#else
filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, boolean checkmd5, boolean completepath, int maxsearchdepth)
filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, boolean checkmd5, boolean completepath, int maxsearchdepth, void *mutex_ptr)
{
filestatus_t retval = FS_NOTFOUND;
const UINT8 *wantedmd5sum = NULL;
@ -455,9 +456,20 @@ filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, b
int depthleft = maxsearchdepth;
char searchpath[1024];
size_t *searchpathindex;
int matched;
int files_left = 0;
int i;
#ifdef HAVE_THREADS
#define Lock() I_lock_mutex ((I_mutex *)mutex_ptr)
#define Unlock() I_unlock_mutex (*(I_mutex *)mutex_ptr)
#else
#define Lock() ((void)0)
#define Unlock() ((void)0)
#endif
Lock();
for (i = 0; i < nfiles; ++i)
{
if (files[i].status != FS_FOUND)
@ -466,6 +478,8 @@ filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, b
}
}
Unlock();
if (! files_left)
{
return FS_FOUND;
@ -537,27 +551,41 @@ filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, b
{
for (i = 0; i < nfiles; ++i)
{
Lock();
if (checkmd5)
{
wantedmd5sum = files[i].wantedmd5sum;
}
if (
matched = (
files[i].status != FS_FOUND &&
!strcasecmp(files[i].filename, dent->d_name)
){
);
Unlock();
if (matched)
{
switch (checkfilemd5(searchpath, wantedmd5sum))
{
case FS_FOUND:
Lock();
if (completepath)
strcpy(files[i].filename,searchpath);
else
strcpy(files[i].filename,dent->d_name);
files[i].status = FS_FOUND;
Unlock();
files_left--;
break;
case FS_MD5SUMBAD:
Lock();
retval = files[i].status = FS_MD5SUMBAD;
Unlock();
break;
default: // prevent some compiler warnings
break;
@ -573,6 +601,9 @@ filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, b
free(dirhandle);
return ( (files_left) ? retval : FS_FOUND );
#undef Lock
#undef Unlock
}
char exttable[NUM_EXT_TABLE][7] = { // maximum extension length (currently 4) plus 3 (null terminator, stop, and length including previous two)

View file

@ -35,11 +35,11 @@ extern consvar_t cv_addons_option, cv_addons_folder, cv_addons_md5, cv_addons_sh
*/
filestatus_t filesearch(int nfiles, filequery_t *queries, const char *startpath,
boolean checkmd5, boolean completepath, int maxsearchdepth);
boolean checkmd5, boolean completepath, int maxsearchdepth, void *mutex_ptr);
/* this is a better place for it tbh (im still too lazy to move the other) */
filestatus_t findmultiplefiles(int nfiles, filequery_t *files,
boolean checkmd5, boolean completepath);
boolean checkmd5, boolean completepath, void *mutex_ptr);
#define menudepth 20

View file

@ -3624,7 +3624,7 @@ static const char *searchWad(const char *searchDir)
strcpy(tempsw, WADKEYWORD1);
fsquery.status = FS_NOTFOUND;
fstemp = filesearch(1,&fsquery,searchDir,false,true,20);
fstemp = filesearch(1,&fsquery,searchDir,false,true,20,NULL);
if (fstemp == FS_FOUND)
{
pathonly(tempsw);
@ -3632,7 +3632,7 @@ static const char *searchWad(const char *searchDir)
}
strcpy(tempsw, WADKEYWORD2);
fstemp = filesearch(1, &fsquery, searchDir, false, true, 20);
fstemp = filesearch(1, &fsquery, searchDir, false, true, 20, NULL);
if (fstemp == FS_FOUND)
{
pathonly(tempsw);

View file

@ -871,7 +871,7 @@ INT32 W_InitMultipleFiles(char **filenames, boolean addons)
n++;
}
findmultiplefiles(n, q, false, true);
findmultiplefiles(n, q, false, true, NULL);
for (i = 0; i < n; ++i)
{