From fee68fc612a3c6c67c55aa194223002bdfda7261 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Fri, 2 Aug 2019 23:00:30 -0400 Subject: quake: synchronize Mod_LoadModel and S_LoadSound This is not a very pretty fix, but code that doesn't crash is better than code that crashes... "If it runs, it's done." Change-Id: Ia1d0c537e5e5e60fb80cf7d7de2332e1c712806f --- apps/plugins/sdl/progs/quake/model.c | 26 +++++++++++++++++++++++++- apps/plugins/sdl/progs/quake/snd_mem.c | 23 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/apps/plugins/sdl/progs/quake/model.c b/apps/plugins/sdl/progs/quake/model.c index 7648590db4..64c53dfc3e 100644 --- a/apps/plugins/sdl/progs/quake/model.c +++ b/apps/plugins/sdl/progs/quake/model.c @@ -255,6 +255,18 @@ Loads a model into the cache */ model_t *Mod_LoadModel (model_t *mod, qboolean crash) { + // prevents crashes + extern struct mutex snd_mutex; + extern int snd_mutex_init; + + if(!snd_mutex_init) + { + rb->mutex_init(&snd_mutex); + snd_mutex_init = 1; + } + + rb->mutex_lock(&snd_mutex); + //printf("loadmodel 1"); unsigned *buf; byte stackbuf[1024]; // avoid dirtying the cache heap @@ -264,13 +276,17 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) if (Cache_Check (&mod->cache)) { mod->needload = NL_PRESENT; + rb->mutex_unlock(&snd_mutex); return mod; } } else { if (mod->needload == NL_PRESENT) - return mod; + { + rb->mutex_unlock(&snd_mutex); + return mod; + } } // @@ -283,17 +299,23 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) //printf("loadmodel 2"); buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf)); + //printf("LoadModel0: %08x (%08x)", buf[0], buf); if (!buf) { if (crash) Sys_Error ("Mod_NumForName: %s not found", mod->name); + + rb->mutex_unlock(&snd_mutex); return NULL; } // // allocate a new model // + //printf("LoadModel1: %08x (%08x)", buf[0], buf); + COM_FileBase (mod->name, loadname); + //printf("LoadModel2: %08x (%08x)", buf[0], buf); loadmodel = mod; @@ -316,10 +338,12 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) break; default: + //printf("unkn %08x (&=%08x), nat %08x def to brush", LittleLongUnaligned(buf[0]), &buf[0], buf[0]); Mod_LoadBrushModel (mod, buf); break; } + rb->mutex_unlock(&snd_mutex); return mod; } diff --git a/apps/plugins/sdl/progs/quake/snd_mem.c b/apps/plugins/sdl/progs/quake/snd_mem.c index 71e32aa056..8b0a5ca9f7 100644 --- a/apps/plugins/sdl/progs/quake/snd_mem.c +++ b/apps/plugins/sdl/progs/quake/snd_mem.c @@ -89,6 +89,10 @@ void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data) //============================================================================= +// used to synchronize with Mod_LoadModel, which causes crashes if not done. +struct mutex snd_mutex; +int snd_mutex_init = 0; + /* ============== S_LoadSound @@ -96,6 +100,7 @@ S_LoadSound */ sfxcache_t *S_LoadSound (sfx_t *s) { + //return NULL; char namebuffer[256]; byte *data; wavinfo_t info; @@ -104,11 +109,21 @@ sfxcache_t *S_LoadSound (sfx_t *s) sfxcache_t *sc; byte stackbuf[1*1024]; // avoid dirtying the cache heap -// see if still in memory +// see if still in memory (no mutex) sc = Cache_Check (&s->cache); if (sc) + { return sc; + } + + if(!snd_mutex_init) + { + rb->mutex_init(&snd_mutex); + snd_mutex_init = 1; + } + rb->mutex_lock(&snd_mutex); + //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf); // load it in Q_strcpy(namebuffer, "sound/"); @@ -120,6 +135,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) if (!data) { + rb->mutex_unlock(&snd_mutex); Con_Printf ("Couldn't load %s\n", namebuffer); return NULL; } @@ -127,6 +143,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) info = GetWavinfo (s->name, data, com_filesize); if (info.channels != 1) { + rb->mutex_unlock(&snd_mutex); Con_Printf ("%s is a stereo sample\n",s->name); return NULL; } @@ -138,7 +155,10 @@ sfxcache_t *S_LoadSound (sfx_t *s) sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name); if (!sc) + { + rb->mutex_unlock(&snd_mutex); return NULL; + } sc->length = info.samples; sc->loopstart = info.loopstart; @@ -148,6 +168,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) ResampleSfx (s, sc->speed, sc->width, data + info.dataofs); + rb->mutex_unlock(&snd_mutex); return sc; } -- cgit v1.2.3