Skip to content

Commit

Permalink
Add more error checking in SdlMutex
Browse files Browse the repository at this point in the history
Throw on lock/unlock.
  • Loading branch information
realnc committed Jul 6, 2022
1 parent 91dd1b6 commit 0ef7386
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 16 additions & 0 deletions src/SdlMutex.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
// This is copyrighted software. More information is at the end of this file.
#include "SdlMutex.h"
#include <stdexcept>

SdlMutex::SdlMutex()
{
if (not mutex_) {
throw std::runtime_error(SDL_GetError());
}
}

void SdlMutex::lock()
{
if (SDL_LockMutex(mutex_) != 0) {
throw std::runtime_error(SDL_GetError());
}
}

void SdlMutex::unlock()
{
if (SDL_UnlockMutex(mutex_) != 0) {
throw std::runtime_error(SDL_GetError());
}
}

/*
Copyright (C) 2021 Nikos Chantziaras.
Expand Down
11 changes: 2 additions & 9 deletions src/SdlMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <SDL_error.h>
#include <SDL_mutex.h>
#include <SDL_version.h>
#include <stdexcept>

/*
* RAII wrapper for SDL_mutex. Satisfies std's "Lockable" (SDL 2) or "BasicLockable" (SDL 1)
Expand All @@ -22,10 +21,7 @@ class SdlMutex final
SdlMutex(const SdlMutex&) = delete;
auto operator=(const SdlMutex&) -> SdlMutex& = delete;

void lock() noexcept
{
SDL_LockMutex(mutex_);
}
void lock();

#if SDL_VERSION_ATLEAST(2, 0, 0)
bool try_lock() noexcept
Expand All @@ -34,10 +30,7 @@ class SdlMutex final
}
#endif

void unlock() noexcept
{
SDL_UnlockMutex(mutex_);
}
void unlock();

private:
SDL_mutex* mutex_ = SDL_CreateMutex();
Expand Down

0 comments on commit 0ef7386

Please sign in to comment.