fixed bugs with thread functions not being re-entrant
authorHolger Schemel <info@artsoft.org>
Wed, 23 Jun 2021 11:11:57 +0000 (13:11 +0200)
committerHolger Schemel <info@artsoft.org>
Wed, 23 Jun 2021 11:11:57 +0000 (13:11 +0200)
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.

src/files.c

index 4878aa8368e42bfdf1ddf59b7e901bd877a381dd..93a4b59724af8d9fb458d2e0f9bdcd27ffae2df5 100644 (file)
@@ -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");
 }