Revert "Simplify/optimize sf::AudioResource implementation"

This reverts commit cede2def8b as it's
still possible to force a wrong destruction order.
This commit is contained in:
Lukas Dürrenberger 2024-07-03 11:37:34 +02:00
parent fbd8407a5f
commit 121fa9cf74
2 changed files with 28 additions and 50 deletions

View File

@ -29,6 +29,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <memory>
namespace sf
{
@ -43,7 +45,7 @@ public:
/// \brief Copy constructor
///
////////////////////////////////////////////////////////////
AudioResource(const AudioResource&);
AudioResource(const AudioResource&) = default;
////////////////////////////////////////////////////////////
/// \brief Copy assignment
@ -55,7 +57,7 @@ public:
/// \brief Move constructor
///
////////////////////////////////////////////////////////////
AudioResource(AudioResource&&) noexcept;
AudioResource(AudioResource&&) noexcept = default;
////////////////////////////////////////////////////////////
/// \brief Move assignment
@ -63,18 +65,18 @@ public:
////////////////////////////////////////////////////////////
AudioResource& operator=(AudioResource&&) noexcept = default;
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~AudioResource();
protected:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
AudioResource();
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::shared_ptr<void> m_device; //!< Sound device
};
} // namespace sf

View File

@ -28,58 +28,34 @@
#include <SFML/Audio/AudioDevice.hpp>
#include <SFML/Audio/AudioResource.hpp>
#include <memory>
#include <mutex>
#include <optional>
namespace
{
////////////////////////////////////////////////////////////
struct DeviceState
{
std::mutex mutex;
std::optional<sf::priv::AudioDevice> device;
unsigned int referenceCounter{};
};
////////////////////////////////////////////////////////////
DeviceState& getDeviceState()
{
static DeviceState deviceState;
return deviceState;
}
} // namespace
namespace sf
{
////////////////////////////////////////////////////////////
AudioResource::AudioResource()
{
auto& [mutex, device, referenceCounter] = getDeviceState();
const std::lock_guard guard{mutex};
AudioResource::AudioResource() :
m_device(
[]
{
// Ensure we only ever create a single instance of an
// AudioDevice that is shared between all AudioResources
static std::mutex mutex;
static std::weak_ptr<priv::AudioDevice> weakAudioDevice;
if (referenceCounter++ == 0u)
device.emplace();
}
const std::lock_guard lock(mutex);
////////////////////////////////////////////////////////////
AudioResource::~AudioResource()
{
auto& [mutex, device, referenceCounter] = getDeviceState();
const std::lock_guard guard{mutex};
auto audioDevice = weakAudioDevice.lock();
if (--referenceCounter == 0u)
device.reset();
}
if (audioDevice == nullptr)
{
audioDevice = std::make_shared<priv::AudioDevice>();
weakAudioDevice = audioDevice;
}
////////////////////////////////////////////////////////////
AudioResource::AudioResource(const AudioResource&) : AudioResource{}
{
}
////////////////////////////////////////////////////////////
AudioResource::AudioResource(AudioResource&&) noexcept : AudioResource{}
return audioDevice;
}())
{
}