Browse Source

fix race conditions on redirected std::cout

Kolja Strohm 1 year ago
parent
commit
125e019f89
4 changed files with 31 additions and 16 deletions
  1. 4 4
      FactoryCraft/Chest.cpp
  2. 4 0
      FactoryCraft/Entity.cpp
  3. 22 11
      FactoryCraft/Start.cpp
  4. 1 1
      FactoryCraft/TickWorker.cpp

+ 4 - 4
FactoryCraft/Chest.cpp

@@ -92,18 +92,18 @@ void Chest::interact(Item* zItem, Entity* zActor)
             << getPos().z
             << "\" "
                "width=\"610\" "
-               "height=\"470\">"
-            << "<inventory id=\"chest_inventory\" margin-bottom=\"9\" "
+               "height=\"480\">"
+            << "<inventory id=\"chest_inventory\" margin-bottom=\"18\" "
                "align-bottom=\"player_label\" align-left=\"start\" "
                "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
                "numSlots=\"30\" slotNameFilter=\"\" target=\""
             << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
             << getPos().z << "\"/>"
-            << "<text id=\"player_label\" width=\"100%\" style=\""
+            << "<text id=\"player_label\" width=\"100%\" height=\"auto\" style=\""
             << std::uppercase << std::hex
             << (TextFeld::Style::Text | TextFeld::Style::Center) << std::dec
             << std::nouppercase
-            << "\"margin-bottom=\"9\" align-bottom=\"player_inventory\">Player "
+            << "\" margin-bottom=\"9\" align-bottom=\"player_inventory\">Player "
                "Inventory</text>"
             << "<inventory id=\"player_inventory\" margin-bottom=\"18\" "
                "align-bottom=\"item_bar\" align-left=\"start\" "

+ 4 - 0
FactoryCraft/Entity.cpp

@@ -250,6 +250,10 @@ void Entity::useItem(int typeId, Item* zItem, bool left)
             }
             cs.unlock();
         }
+        else if (left)
+        {
+            useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
+        }
     }
     else
     {

+ 22 - 11
FactoryCraft/Start.cpp

@@ -35,8 +35,8 @@ private:
     std::ostream& console;
     std::ostream& file;
     bool hasFile = 0;
-    Critical cs;
     int infoLength;
+    Critical cs;
 
 public:
     DuplicatingStreamBuf(std::ostream& console, std::ostream& file)
@@ -55,14 +55,26 @@ public:
           infoLength(0)
     {}
 
-    int sync() override
+    void __CLR_OR_THIS_CALL _Lock() override
     {
         cs.lock();
-        if (str().length() == 0 && !Game::INSTANCE)
+    }
+
+    void __CLR_OR_THIS_CALL _Unlock() override
+    {
+        cs.unlock();
+    }
+
+    int sync() override
+    {
+        _Lock();
+        std::string value = str();
+        if (value.length() == 0 && !Game::INSTANCE)
         {
-            cs.unlock();
+            _Unlock();
             return 0;
         }
+        str("");
         if (infoLength > 0)
         {
             console << /* store cursor position */ "\033[s"
@@ -70,11 +82,11 @@ public:
                     << /* clear the line */ "\x1b[2K"
                     << /* return to beginning of line */ "\r";
         }
-        int newLines = Text(str().c_str()).anzahlVon('\n');
-        if (str().length() > 0)
+        int newLines = Text(value.c_str()).anzahlVon('\n');
+        if (value.length() > 0)
         {
-            console << str();
-            if (str().c_str()[str().length() - 1] != '\n')
+            console << value;
+            if (value.c_str()[value.length() - 1] != '\n')
             {
                 console << "\n";
                 newLines++;
@@ -118,9 +130,8 @@ public:
         {
             infoLength = 0;
         }
-        if (hasFile) file << str() << std::flush;
-        str("");
-        cs.unlock();
+        if (hasFile) file << value << std::flush;
+        _Unlock();
         return 0;
     }
 };

+ 1 - 1
FactoryCraft/TickWorker.cpp

@@ -36,7 +36,7 @@ void TickWorker::thread()
     tid = (pid_t)syscall(SYS_gettid);
 #endif
     Framework::Text txt = Framework::Text("exiting tick worker ") + tid + "\n";
-    std::cout << txt.getText() << std::flush;
+    std::cout << txt.getText();
 }
 
 bool TickWorker::isWaiting() const