From e9eb48cdafb99553522d0aa1846ce31179063065 Mon Sep 17 00:00:00 2001
From: akeley98 <dza724@gmail.com>
Date: Tue, 22 Dec 2020 02:50:33 -0800
Subject: [PATCH] Add synchronization hack for maybe_fix_endian

---
 src/voxels.cc | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/voxels.cc b/src/voxels.cc
index f009614..4637cdb 100644
--- a/src/voxels.cc
+++ b/src/voxels.cc
@@ -257,6 +257,18 @@ static inline bool maybe_fix_endian(
         #endif
     }
 
+    // I just realized the horrible things this function can do if run
+    // concurrently, so fix this here. This won't protect in case
+    // multiple myricube instances are running, but it's better than
+    // nothing (this function truly is a HORRIBLE hack, but I kinda
+    // need it now for bug-for-bug compatibility).
+    static std::mutex the_mutex;
+    std::lock_guard guard(the_mutex);
+    if (group.magic_number != old_magic_number) {
+        fprintf(stderr, "maybe_fix_endian: fixed by another thread\n");
+        return false;
+    }
+
     for (int zH = 0; zH < edge_chunks; ++zH) {
     for (int yH = 0; yH < edge_chunks; ++yH) {
     for (int xH = 0; xH < edge_chunks; ++xH) {