Browse Source

fix deadlock on entity remove

Kolja Strohm 3 years ago
parent
commit
399b6618d8

+ 3 - 0
FactoryCraft/Dimension.cpp

@@ -185,6 +185,7 @@ Entity* Dimension::getEntity( int id )
 
 void Dimension::removeEntity( int id )
 {
+    currentGame->lockWorld();
     cs.lock();
     int index = 0;
     for( Entity* e : *entities )
@@ -194,9 +195,11 @@ void Dimension::removeEntity( int id )
             currentGame->setVisibility( e, 0 );
             entities->remove( index );
             cs.unlock();
+            currentGame->unlockWorld();
             return;
         }
         index++;
     }
     cs.unlock();
+    currentGame->unlockWorld();
 }

+ 11 - 1
FactoryCraft/World.cpp

@@ -92,7 +92,7 @@ void World::setChunk( Chunk* chunk, int dimensionId )
 
 void World::thread()
 {
-    new AsynchronCall( [this]() {
+    new AsynchronCall( "World Update", [this]() {
         while( true )
         {
             zScreenPtr->lock();
@@ -250,4 +250,14 @@ void World::setTarget( Framework::Model3D* zTarget )
                 currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
         }
     }
+}
+
+void World::lockWorld()
+{
+    renderedWorld->lock();
+}
+
+void World::unlockWorld()
+{
+    renderedWorld->unlock();
 }

+ 2 - 0
FactoryCraft/World.h

@@ -45,4 +45,6 @@ public:
     int getCurrentPlayerId() const;
     Entity* getCurrentPlayerEntity() const;
     void setTarget( Framework::Model3D* zTarget );
+    void lockWorld();
+    void unlockWorld();
 };

+ 0 - 94
enc_temp_folder/3a42a274dd97048b8aa306a3eb81718/Game.cpp

@@ -1,94 +0,0 @@
-#include "Game.h"
-#include "Initialisierung.h"
-#include "Globals.h"
-
-#include <AsynchronCall.h>
-#include <Bildschirm.h>
-
-Game::Game( Bildschirm* zScreen )
-    : Menu( zScreen )
-{
-    logout = initKnopf( 10, 10, 200, 20, Knopf::Style::Normal, "Verlassen" );
-    logout->setMausEreignis( [this, zScreen]( void* p, void* o, MausEreignis me ) {
-        if( me.id == ME_RLinks )
-        {
-            logout->removeStyle( Knopf::Style::Erlaubt );
-            new AsynchronCall( [this, zScreen]() {
-                if( network->leaveGame() )
-                {
-                    currentGame->release();
-                    currentGame = 0;
-                    zScreen->lock();
-                    hide();
-                    menuRegister->get( "directConnect" )->show();
-                    zScreen->unlock();
-                }
-                logout->addStyle( Knopf::Style::Erlaubt );
-            } );
-        }
-        return 1;
-    } );
-    elements.add( logout );
-    debug = initTextFeld( 10, 40, 500, 40, TextFeld::Style::Text | TextFeld::Style::Mehrzeilig, "" );
-    elements.add( debug );
-    invB = new Bild();
-    invB->neuBild( zScreen->getBackBufferSize().x - 20, 50, 0 );
-    invB->setAlpha3D( 1 );
-    inventory = initBildZ( 10, zScreen->getBackBufferSize().y - 60, zScreen->getBackBufferSize().x - 20, 50, BildZ::Style::Sichtbar | BildZ::Style::Alpha, invB );
-    elements.add( inventory );
-}
-
-void Game::updatePosition( Vec3<float> position, bool target, Vec3<int> targetPos )
-{
-    Text txt = "Position: (";
-    txt.setPrecision( 2 );
-    txt += position.x;
-    txt += ", ";
-    txt += position.y;
-    txt += ", ";
-    txt += position.z;
-    txt += ")";
-    if( target )
-    {
-        txt += "\nTarget: (";
-        txt += targetPos.x;
-        txt += ", ";
-        txt += targetPos.y;
-        txt += ", ";
-        txt += targetPos.z;
-        txt += ")";
-    }
-    debug->setText( txt );
-}
-
-void Game::updateInventory( Framework::Array<ItemSlot*>& itemBar, int pos )
-{
-    invB->fillRegion( 0, 0, invB->getBreite(), invB->getHeight(), 0 );
-    TextRenderer tr;
-    tr.setSchriftZ( dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()) );
-    tr.setSchriftSize( 12 );
-    int index = 0;
-    for( auto slot : itemBar )
-    {
-        if( pos == index )
-        {
-            invB->fillRegion( 50 * index, 0, 50, 50, 0x7F202020 );
-        }
-        if( slot && slot->zStack() && slot->zStack()->zItem() )
-        {
-            Bild* icon = itemIcons->z( slot->zStack()->zItem()->zItemType()->getId() );
-            invB->drawBild( 50 * index, 0, 50, 50, *icon );
-            tr.renderText( 5 + index * 50, 5, slot->zStack()->zItem()->getName(), *invB, 0xFFFFFFFF );
-            int size = slot->zStack()->getSize();
-            const char* units[] = { "", "K", "M", "G", "T", "P" };
-            int index = 0;
-            for( ; index < 6 && size > 1024; index++ )
-                size = size / 1024;
-            Text count = size;
-            count += units[ index ];
-            tr.renderText( 45 - tr.getTextBreite( count ) + index * 50, 45 - tr.getTextHeight( count ), count, *invB, 0xFFFFFFFF );
-        }
-        index++;
-    }
-    inventory->setRender();
-}

+ 0 - 94
enc_temp_folder/4bd6d7d4a23757a254aa0a7352999e/Game.cpp

@@ -1,94 +0,0 @@
-#include "Game.h"
-#include "Initialisierung.h"
-#include "Globals.h"
-
-#include <AsynchronCall.h>
-#include <Bildschirm.h>
-
-Game::Game( Bildschirm* zScreen )
-    : Menu( zScreen )
-{
-    logout = initKnopf( 10, 10, 200, 20, Knopf::Style::Normal, "Verlassen" );
-    logout->setMausEreignis( [this, zScreen]( void* p, void* o, MausEreignis me ) {
-        if( me.id == ME_RLinks )
-        {
-            logout->removeStyle( Knopf::Style::Erlaubt );
-            new AsynchronCall( [this, zScreen]() {
-                if( network->leaveGame() )
-                {
-                    currentGame->release();
-                    currentGame = 0;
-                    zScreen->lock();
-                    hide();
-                    menuRegister->get( "directConnect" )->show();
-                    zScreen->unlock();
-                }
-                logout->addStyle( Knopf::Style::Erlaubt );
-            } );
-        }
-        return 1;
-    } );
-    elements.add( logout );
-    debug = initTextFeld( 10, 40, 500, 40, TextFeld::Style::Text | TextFeld::Style::Mehrzeilig, "" );
-    elements.add( debug );
-    invB = new Bild();
-    invB->neuBild( zScreen->getBackBufferSize().x - 20, 50, 0 );
-    invB->setAlpha3D( 1 );
-    inventory = initBildZ( 10, zScreen->getBackBufferSize().y - 60, zScreen->getBackBufferSize().x - 20, 50, BildZ::Style::Sichtbar | BildZ::Style::Alpha, invB );
-    elements.add( inventory );
-}
-
-void Game::updatePosition( Vec3<float> position, bool target, Vec3<int> targetPos )
-{
-    Text txt = "Position: (";
-    txt.setPrecision( 2 );
-    txt += position.x;
-    txt += ", ";
-    txt += position.y;
-    txt += ", ";
-    txt += position.z;
-    txt += ")";
-    if( target )
-    {
-        txt += "\nTarget: (";
-        txt += targetPos.x;
-        txt += ", ";
-        txt += targetPos.y;
-        txt += ", ";
-        txt += targetPos.z;
-        txt += ")";
-    }
-    debug->setText( txt );
-}
-
-void Game::updateInventory( Framework::Array<ItemSlot*>& itemBar, int pos )
-{
-    invB->fillRegion( 0, 0, invB->getBreite(), invB->getHeight(), 0 );
-    TextRenderer tr;
-    tr.setSchriftZ( dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()) );
-    tr.setSchriftSize( 12 );
-    int index = 0;
-    for( auto slot : itemBar )
-    {
-        if( pos == index )
-        {
-            invB->fillRegion( 50 * index, 0, 50, 50, 0x7F202020 );
-        }
-        if( slot && slot->zStack() && slot->zStack()->zItem() )
-        {
-            Bild* icon = itemIcons->z( slot->zStack()->zItem()->zItemType()->getId() );
-            invB->alphaBild( 50 * index, 0, 50, 50, *icon );
-            tr.renderText( 5 + index * 50, 5, slot->zStack()->zItem()->getName(), *invB, 0xFFFFFFFF );
-            int size = slot->zStack()->getSize();
-            const char* units[] = { "", "K", "M", "G", "T", "P" };
-            int index = 0;
-            for( ; index < 6 && size > 1024; index++ )
-                size = size / 1024;
-            Text count = size;
-            count += units[ index ];
-            tr.renderText( 45 - tr.getTextBreite( count ) + index * 50, 45 - tr.getTextHeight( count ), count, *invB, 0xFFFFFFFF );
-        }
-        index++;
-    }
-    inventory->setRender();
-}