From: Holger Schemel Date: Wed, 23 Jun 2021 11:11:57 +0000 (+0200) Subject: fixed bugs with thread functions not being re-entrant X-Git-Tag: 4.3.0.0~118 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=892d769ce57c28769dce83cbef5592852458f33d;p=rocksndiamonds.git fixed bugs with thread functions not being re-entrant This change fixes potential problems with thread functions being called several times in parallel (which may happen if the network operations performed by these functions really take long). In these cases, the thread's data structure (see previous commit) will not be used exclusively anymore. To fix this, the thread's data structure will be allocated separately for each invocation of the corresponding thread function. --- diff --git a/src/files.c b/src/files.c index 4878aa83..93a4b597 100644 --- a/src/files.c +++ b/src/files.c @@ -9109,21 +9109,23 @@ static int DownloadServerScoreToCacheThread(void *data_raw) DownloadServerScoreToCache(data->level_nr, data->score_cache_filename); + checked_free(data->score_cache_filename); + checked_free(data); + return 0; } static void DownloadServerScoreToCacheAsThread(int nr) { - static struct DownloadServerScoreToCacheThreadData data; + struct DownloadServerScoreToCacheThreadData *data = + checked_malloc(sizeof(struct DownloadServerScoreToCacheThreadData)); char *score_cache_filename = getScoreCacheFilename(nr); - checked_free(data.score_cache_filename); - - data.level_nr = nr; - data.score_cache_filename = getStringCopy(score_cache_filename); + data->level_nr = nr; + data->score_cache_filename = getStringCopy(score_cache_filename); ExecuteAsThread(DownloadServerScoreToCacheThread, - "DownloadServerScoreToCache", &data, + "DownloadServerScoreToCache", data, "download scores from server"); } @@ -9384,23 +9386,25 @@ static int UploadScoreToServerThread(void *data_raw) data->score_tape_filename, &data->score_entry); + checked_free(data->score_tape_filename); + checked_free(data); + return 0; } static void UploadScoreToServerAsThread(int nr) { - static struct UploadScoreToServerThreadData data; + struct UploadScoreToServerThreadData *data = + checked_malloc(sizeof(struct UploadScoreToServerThreadData)); struct ScoreEntry *score_entry = &scores.entry[scores.last_added]; char *score_tape_filename = getScoreTapeFilename(score_entry->tape_basename, nr); - checked_free(data.score_tape_filename); - - data.level_nr = nr; - data.score_entry = *score_entry; - data.score_tape_filename = getStringCopy(score_tape_filename); + data->level_nr = nr; + data->score_entry = *score_entry; + data->score_tape_filename = getStringCopy(score_tape_filename); ExecuteAsThread(UploadScoreToServerThread, - "UploadScoreToServer", &data, + "UploadScoreToServer", data, "upload score to server"); }