Skip to content

Commit

Permalink
libdoomsday: Games collection needs to be thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent ce9b475 commit 9583bdd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doomsday/libs/doomsday/include/doomsday/games.h
Expand Up @@ -118,7 +118,7 @@ class LIBDOOMSDAY_PUBLIC Games
/**
* Returns a list of all the Game instances in the collection.
*/
All const &all() const;
All all() const;

de::LoopResult forAll(const std::function<de::LoopResult (Game &)>& callback) const;

Expand Down
14 changes: 13 additions & 1 deletion doomsday/libs/doomsday/src/games.cpp
Expand Up @@ -40,6 +40,7 @@ using namespace de;

DE_PIMPL(Games)
, DE_OBSERVES(res::Bundles, Identify)
, public Lockable
{
/// The actual collection.
All games;
Expand Down Expand Up @@ -156,6 +157,7 @@ Game &Games::nullGame() // static

int Games::numPlayable() const
{
DE_GUARD(d);
int count = 0;
for (Game *game : d->games)
{
Expand All @@ -169,6 +171,7 @@ int Games::numPlayable() const

int Games::numPlayable(String const &family) const
{
DE_GUARD(d);
int count = 0;
for (Game *game : d->games)
{
Expand All @@ -182,6 +185,7 @@ int Games::numPlayable(String const &family) const

GameProfile const *Games::firstPlayable() const
{
DE_GUARD(d);
for (Game *game : d->games)
{
if (game->profile().isPlayable()) return &game->profile();
Expand All @@ -191,6 +195,7 @@ GameProfile const *Games::firstPlayable() const

Game &Games::operator[](String const &id) const
{
DE_GUARD(d);
if (id.isEmpty())
{
return *d->nullGame;
Expand All @@ -206,11 +211,13 @@ Game &Games::operator[](String const &id) const

bool Games::contains(String const &id) const
{
DE_GUARD(d);
return d->findById(id) != nullptr;
}

Game &Games::byIndex(int idx) const
{
DE_GUARD(d);
if (idx < 0 || idx > d->games.sizei())
{
/// @throw NotFoundError No game is associated with index @a idx.
Expand All @@ -221,16 +228,19 @@ Game &Games::byIndex(int idx) const

void Games::clear()
{
DE_GUARD(d);
d->clear();
}

Games::All const &Games::all() const
Games::All Games::all() const
{
DE_GUARD(d);
return d->games;
}

int Games::collectAll(GameList &collected)
{
DE_GUARD(d);
int numFoundSoFar = collected.sizei();
for (Game *game : d->games)
{
Expand All @@ -241,6 +251,7 @@ int Games::collectAll(GameList &collected)

Game &Games::defineGame(String const &id, Record const &parameters)
{
DE_GUARD(d);
LOG_AS("Games");

// Game IDs must be unique. Ensure that is the case.
Expand Down Expand Up @@ -338,6 +349,7 @@ void Games::checkReadiness()
return LoopContinue;
});
*/
DE_GUARD(d);

Set<Game const *> playable;
forAll([&playable] (Game &game)
Expand Down

0 comments on commit 9583bdd

Please sign in to comment.