diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index 26d00a7f6..205788596 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -1233,7 +1233,7 @@ void HWR_DrawMD2(gl_vissprite_t *spr) sprinfo = &spriteinfo[spr->mobj->sprite]; } } - else + else if (spr->mobj->skin) { md2 = &md2_models[spr->mobj->sprite]; sprinfo = &spriteinfo[spr->mobj->sprite]; diff --git a/src/m_argv.c b/src/m_argv.c index 36c73d61c..6219b191d 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -13,6 +13,13 @@ #include +#ifdef HAVE_THREADS +#include +#define local_for_thread thread_local +#else +#define local_for_thread +#endif + #include "doomdef.h" #include "command.h" #include "m_argv.h" @@ -31,8 +38,11 @@ char **myargv = NULL; boolean myargmalloc = false; /** \brief founded the parm + * + * thread_local because it can potentially be used from multiple threads (global variables aaaaaa) + * (myargc and myargv don't have to be thread_local because they are never modified after being set up) */ -static INT32 found; +static local_for_thread INT32 found; /** \brief The M_CheckParm function @@ -91,7 +101,7 @@ INT32 M_CheckProtoParam(const char *check) const char *M_GetProtoParam(void) { INT32 i; - static char *string; + char *string; for (i = 1; i < myargc; i++) { diff --git a/src/m_menu.c b/src/m_menu.c index a89146494..1b3b2e81b 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -3983,6 +3983,10 @@ menudemo_t *demolist = NULL; // Replays that that have been checked to match wit menudemo_t *demolist_all = NULL; // All replays size_t demolist_all_size = 0; boolean replaynamesloaded = false; +// Whenever we start a replay load thread, we increment this number and then thread will remember current value, +// and by the end it will compare it with current value, and if it is equal it means thread can store data it has loaded. +// If it's not equal, that means we started more than one thread (left and re-entered menu for example) +size_t replaythreadnum = 0; #ifdef HAVE_THREADS I_Mutex replayquerymutex; @@ -4012,12 +4016,14 @@ static SINT8 replayScrollDelay = TICRATE, replayScrollDir = 1; static void ReplayNamesLoadThread(void* userdata) { + (void)userdata; + Lock_search_state(); size_t demolist_all_size_local = demolist_all_size; menudemo_t *demolist_all_local = (menudemo_t*)malloc(sizeof(menudemo_t)*demolist_all_size_local); memcpy(demolist_all_local, demolist_all, sizeof(menudemo_t)*demolist_all_size_local); - char *replaydirpath = (char*)userdata; + size_t localreplaythreadnum = replaythreadnum; Unlock_search_state(); @@ -4037,7 +4043,7 @@ static void ReplayNamesLoadThread(void* userdata) Lock_search_state(); - if (fastcmp(menupath, replaydirpath) && demolist_all) + if (replaythreadnum == localreplaythreadnum && demolist_all) { memcpy(demolist_all, demolist_all_local, sizeof(menudemo_t)*demolist_all_size); replaynamesloaded = true; @@ -4046,25 +4052,23 @@ static void ReplayNamesLoadThread(void* userdata) Unlock_search_state(); // Was allocated before thread start, we need to free it - free(replaydirpath); free(demolist_all_local); } static void LoadReplayNames(void) { - char *replaydirpath = strdup(menupath); - Lock_search_state(); + replaythreadnum++; replaynamesloaded = false; Unlock_search_state(); #ifdef HAVE_THREADS - if (!I_SpawnThread("replay-names-load", ReplayNamesLoadThread, replaydirpath)) + if (!I_SpawnThread("replay-names-load", ReplayNamesLoadThread, NULL)) { - free(replaydirpath); + ; } #else - ReplayNamesLoadThread(replaydirpath); + ReplayNamesLoadThread(NULL); #endif } @@ -4103,8 +4107,12 @@ static void M_HutCheckReplays(size_t maxnum) // Mark as done replayqueryfound = replayquerycheck = sizedirmenu; + Lock_search_state(); + memcpy(demolist, demolist_all, sizeof(menudemo_t) * sizedirmenu); + Unlock_search_state(); + return; } @@ -4253,13 +4261,15 @@ static boolean M_HandleReplayHutQuery(INT32 choice) char tmp[MAXREPLAYQUERY+1]; memcpy(tmp, replayqueryinput_buffer, MAXREPLAYQUERY+1); - Lock_search_state(); + // Honestly not sure if locks for replaynamesloaded are necessary... + // Thread sanitizer does report race condition whenever replaynamesloaded is accessed without lock + // but really it is only ever set to false in main thread and only set to true in replay load thread, + // and both are done via locks. If replaynamesloaded is true, it is never gonna suddenly switch from true to false + // while we're in main thread, and if it would switch from false to true it is not really a big deal, we will just skip + // 1 frame where replay names were loaded + if (!replaynamesloaded) - { - Unlock_search_state(); return false; - } - Unlock_search_state(); if (M_TextInputHandle(&replayqueryinput, choice)) {