Skip to content

Commit

Permalink
Locking the Aseprite data moving behind a mutex.
Browse files Browse the repository at this point in the history
This should squish the last data races around this part.

The lock policy is simple:
- The `AsepriteConnection` locks the data when it has to write a frame
- The main thread will try to lock to not block the whole update/render
  loop.
  • Loading branch information
Eiyeron committed Feb 12, 2022
1 parent 247dcd7 commit da2ff72
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/AsepriteConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void AsepriteConnection::onMessage(std::shared_ptr<ix::ConnectionState> connecti
newImage.pixels.emplace_back(nextByte);
data += 4;
}
lastReadyImage = std::move(newImage);

{
std::scoped_lock imageLock(lastReadyImageMutex);
lastReadyImage = std::move(newImage);
}
}
}
break;
Expand Down
2 changes: 2 additions & 0 deletions src/AsepriteConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdint>
#include <memory>
#include <vector>
#include <mutex>

struct AsepriteImage
{
Expand All @@ -25,6 +26,7 @@ struct AsepriteImage

struct AsepriteConnection
{
mutable std::mutex lastReadyImageMutex;
AsepriteImage lastReadyImage;
bool connected = false;

Expand Down
7 changes: 6 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ int start()
else
{
ClearBackground(darkBackground ? DARKGRAY : WHITE);
AsepriteImage lastImage = std::move(imageServer.lastReadyImage);
AsepriteImage lastImage;
if (imageServer.lastReadyImageMutex.try_lock())
{
lastImage = std::move(imageServer.lastReadyImage);
imageServer.lastReadyImageMutex.unlock();
}
Image currentImage;
currentImage.width = lastImage.width;
currentImage.height = lastImage.height;
Expand Down

0 comments on commit da2ff72

Please sign in to comment.