Audaspace:
authorJoerg Mueller <nexyon@gmail.com>
Wed, 24 Oct 2012 21:33:44 +0000 (21:33 +0000)
committerJoerg Mueller <nexyon@gmail.com>
Wed, 24 Oct 2012 21:33:44 +0000 (21:33 +0000)
RAII locking implementation. This should fix bug [#32096] Background music stops when playing 3D sounds.

15 files changed:
intern/audaspace/CMakeLists.txt
intern/audaspace/OpenAL/AUD_OpenALDevice.cpp
intern/audaspace/intern/AUD_AnimateableProperty.cpp
intern/audaspace/intern/AUD_AnimateableProperty.h
intern/audaspace/intern/AUD_C-API.cpp
intern/audaspace/intern/AUD_IDevice.h
intern/audaspace/intern/AUD_ILockable.h [new file with mode: 0644]
intern/audaspace/intern/AUD_MutexLock.h [new file with mode: 0644]
intern/audaspace/intern/AUD_SequencerEntry.cpp
intern/audaspace/intern/AUD_SequencerEntry.h
intern/audaspace/intern/AUD_SequencerFactory.cpp
intern/audaspace/intern/AUD_SequencerFactory.h
intern/audaspace/intern/AUD_SequencerHandle.cpp
intern/audaspace/intern/AUD_SequencerReader.cpp
intern/audaspace/intern/AUD_SoftwareDevice.cpp

index dc4ca7903cd4409bdf0a2fe3ebebab6d3976ad8b..1671194653a22de0d11dd1bb0cfb4a9ee9965ba3 100644 (file)
@@ -94,6 +94,7 @@ set(SRC
        intern/AUD_IDevice.h
        intern/AUD_IFactory.h
        intern/AUD_IHandle.h
+       intern/AUD_ILockable.h
        intern/AUD_IReader.h
        intern/AUD_IWriter.h
        intern/AUD_JOSResampleFactory.cpp
@@ -108,6 +109,7 @@ set(SRC
        intern/AUD_Mixer.h
        intern/AUD_MixerFactory.cpp
        intern/AUD_MixerFactory.h
+       intern/AUD_MutexLock.h
        intern/AUD_NULLDevice.cpp
        intern/AUD_NULLDevice.h
        intern/AUD_PyInit.h
index f68d41f6a1c6126a602d8dcf0b3dc8329fc244aa..c7d9612020b6d3482cc254ef861084176d3e06d8 100644 (file)
@@ -31,6 +31,7 @@
 #include "AUD_IFactory.h"
 #include "AUD_IReader.h"
 #include "AUD_ConverterReader.h"
+#include "AUD_MutexLock.h"
 
 #include <cstring>
 #include <limits>
@@ -125,7 +126,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause()
 {
        if(m_status)
        {
-               m_device->lock();
+               AUD_MutexLock lock(*m_device);
 
                if(m_status == AUD_STATUS_PLAYING)
                {
@@ -135,12 +136,9 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause()
                        alSourcePause(m_source);
 
                        m_status = AUD_STATUS_PAUSED;
-                       m_device->unlock();
 
                        return true;
                }
-
-               m_device->unlock();
        }
 
        return false;
@@ -150,7 +148,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume()
 {
        if(m_status)
        {
-               m_device->lock();
+               AUD_MutexLock lock(*m_device);
 
                if(m_status == AUD_STATUS_PAUSED)
                {
@@ -159,11 +157,8 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume()
 
                        m_device->start();
                        m_status = AUD_STATUS_PLAYING;
-                       m_device->unlock();
                        return true;
                }
-
-               m_device->unlock();
        }
 
        return false;
@@ -174,7 +169,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop()
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
 
        // AUD_XXX Create a reference of our own object so that it doesn't get
        // deleted before the end of this function
@@ -185,8 +183,6 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop()
        else
                m_device->m_pausedSounds.remove(This);
 
-       m_device->unlock();
-
        alDeleteSources(1, &m_source);
        if(!m_isBuffered)
                alDeleteBuffers(CYCLE_BUFFERS, m_buffers);
@@ -208,11 +204,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setKeep(bool keep)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       m_keep = keep;
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       m_keep = keep;
 
        return true;
 }
@@ -222,7 +219,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
 
        if(m_isBuffered)
                alSourcef(m_source, AL_SEC_OFFSET, position);
@@ -272,17 +272,18 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position)
                }
        }
 
-       m_device->unlock();
-
        return true;
 }
 
 float AUD_OpenALDevice::AUD_OpenALHandle::getPosition()
 {
        if(!m_status)
-               return 0.0f;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return 0.0f;
 
        float position = 0.0f;
 
@@ -295,8 +296,6 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPosition()
                                         CYCLE_BUFFERS) / (float)specs.rate;
        }
 
-       m_device->unlock();
-
        return position;
 }
 
@@ -310,13 +309,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolume()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_GAIN, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_GAIN, &result);
 
        return result;
 }
@@ -326,11 +326,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolume(float volume)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_GAIN, volume);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_GAIN, volume);
 
        return true;
 }
@@ -340,13 +341,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPitch()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_PITCH, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_PITCH, &result);
 
        return result;
 }
@@ -356,11 +358,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setPitch(float pitch)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_PITCH, pitch);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_PITCH, pitch);
 
        return true;
 }
@@ -385,13 +388,14 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setStopCallback(stopCallback callback,
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
 
        m_stop = callback;
        m_stop_data = data;
 
-       m_device->unlock();
-
        return true;
 }
 
@@ -404,15 +408,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceLocation()
        AUD_Vector3 result = AUD_Vector3(0, 0, 0);
 
        if(!m_status)
-               return result;
+               return false;
+
+       AUD_MutexLock lock(*m_device);
 
-       m_device->lock();
+       if(!m_status)
+               return result;
 
        ALfloat p[3];
        alGetSourcefv(m_source, AL_POSITION, p);
 
-       m_device->unlock();
-
        result = AUD_Vector3(p[0], p[1], p[2]);
 
        return result;
@@ -423,11 +428,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceLocation(const AUD_Vector3& lo
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get());
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get());
 
        return true;
 }
@@ -437,15 +443,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceVelocity()
        AUD_Vector3 result = AUD_Vector3(0, 0, 0);
 
        if(!m_status)
-               return result;
+               return false;
+
+       AUD_MutexLock lock(*m_device);
 
-       m_device->lock();
+       if(!m_status)
+               return result;
 
        ALfloat v[3];
        alGetSourcefv(m_source, AL_VELOCITY, v);
 
-       m_device->unlock();
-
        result = AUD_Vector3(v[0], v[1], v[2]);
 
        return result;
@@ -456,11 +463,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceVelocity(const AUD_Vector3& ve
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get());
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get());
 
        return true;
 }
@@ -472,9 +480,6 @@ AUD_Quaternion AUD_OpenALDevice::AUD_OpenALHandle::getSourceOrientation()
 
 bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaternion& orientation)
 {
-       if(!m_status)
-               return false;
-
        ALfloat direction[3];
        direction[0] = -2 * (orientation.w() * orientation.y() +
                                                 orientation.x() * orientation.z());
@@ -482,11 +487,16 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaterni
                                                orientation.z() * orientation.y());
        direction[2] = 2 * (orientation.x() * orientation.x() +
                                                orientation.y() * orientation.y()) - 1;
-       m_device->lock();
 
-       alSourcefv(m_source, AL_DIRECTION, direction);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
+
+       alSourcefv(m_source, AL_DIRECTION, direction);
 
        m_orientation = orientation;
 
@@ -500,11 +510,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::isRelative()
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result);
 
        return result;
 }
@@ -514,11 +525,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setRelative(bool relative)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcei(m_source, AL_SOURCE_RELATIVE, relative);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcei(m_source, AL_SOURCE_RELATIVE, relative);
 
        return true;
 }
@@ -528,13 +540,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMaximum()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_MAX_GAIN, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_MAX_GAIN, &result);
 
        return result;
 }
@@ -544,11 +557,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMaximum(float volume)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_MAX_GAIN, volume);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_MAX_GAIN, volume);
 
        return true;
 }
@@ -558,13 +572,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMinimum()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_MIN_GAIN, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_MIN_GAIN, &result);
 
        return result;
 }
@@ -574,11 +589,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMinimum(float volume)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_MIN_GAIN, volume);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_MIN_GAIN, volume);
 
        return true;
 }
@@ -588,13 +604,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceMaximum()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_MAX_DISTANCE, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_MAX_DISTANCE, &result);
 
        return result;
 }
@@ -604,11 +621,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceMaximum(float distance)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_MAX_DISTANCE, distance);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_MAX_DISTANCE, distance);
 
        return true;
 }
@@ -618,13 +636,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceReference()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result);
 
        return result;
 }
@@ -634,11 +653,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceReference(float distance)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_REFERENCE_DISTANCE, distance);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_REFERENCE_DISTANCE, distance);
 
        return true;
 }
@@ -648,13 +668,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getAttenuation()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result);
 
        return result;
 }
@@ -664,11 +685,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setAttenuation(float factor)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_ROLLOFF_FACTOR, factor);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_ROLLOFF_FACTOR, factor);
 
        return true;
 }
@@ -678,13 +700,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleOuter()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result);
 
        return result;
 }
@@ -694,11 +717,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleOuter(float angle)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle);
 
        return true;
 }
@@ -708,13 +732,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleInner()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result);
 
        return result;
 }
@@ -724,11 +749,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleInner(float angle)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_CONE_INNER_ANGLE, angle);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_CONE_INNER_ANGLE, angle);
 
        return true;
 }
@@ -738,13 +764,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeVolumeOuter()
        float result = std::numeric_limits<float>::quiet_NaN();
 
        if(!m_status)
-               return result;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result);
+       if(!m_status)
+               return result;
 
-       m_device->unlock();
+       alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result);
 
        return result;
 }
@@ -754,11 +781,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeVolumeOuter(float volume)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       alSourcef(m_source, AL_CONE_OUTER_GAIN, volume);
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       alSourcef(m_source, AL_CONE_OUTER_GAIN, volume);
 
        return true;
 }
@@ -776,7 +804,7 @@ static void *AUD_openalRunThread(void *device)
 
 void AUD_OpenALDevice::start(bool join)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(!m_playing)
        {
@@ -793,8 +821,6 @@ void AUD_OpenALDevice::start(bool join)
 
                m_playing = true;
        }
-
-       unlock();
 }
 
 void AUD_OpenALDevice::updateStreams()
@@ -1177,7 +1203,8 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea
        if(!getFormat(format, specs))
                return AUD_Reference<AUD_IHandle>();
 
-       lock();
+       AUD_MutexLock lock(*this);
+
        alcSuspendContext(m_context);
 
        AUD_Reference<AUD_OpenALDevice::AUD_OpenALHandle> sound;
@@ -1190,7 +1217,6 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea
        catch(AUD_Exception&)
        {
                alcProcessContext(m_context);
-               unlock();
                throw;
        }
 
@@ -1201,8 +1227,6 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea
 
        start();
 
-       unlock();
-
        return AUD_Reference<AUD_IHandle>(sound);
 }
 
@@ -1285,7 +1309,8 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IFactory> fa
 
 void AUD_OpenALDevice::stopAll()
 {
-       lock();
+       AUD_MutexLock lock(*this);
+
        alcSuspendContext(m_context);
 
        while(!m_playingSounds.empty())
@@ -1295,7 +1320,6 @@ void AUD_OpenALDevice::stopAll()
                m_pausedSounds.front()->stop();
 
        alcProcessContext(m_context);
-       unlock();
 }
 
 void AUD_OpenALDevice::lock()
index 8a5c538c09425178093043deea52d5d9a2ef8d74..0b333e687ff375502880a00ea8dbf753bcdbf750 100644 (file)
@@ -28,6 +28,7 @@
 
 
 #include "AUD_AnimateableProperty.h"
+#include "AUD_MutexLock.h"
 
 #include <cstring>
 #include <cmath>
@@ -63,17 +64,15 @@ void AUD_AnimateableProperty::unlock()
 
 void AUD_AnimateableProperty::write(const float* data)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_isAnimated = false;
        memcpy(getBuffer(), data, m_count * sizeof(float));
-
-       unlock();
 }
 
 void AUD_AnimateableProperty::write(const float* data, int position, int count)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_isAnimated = true;
 
@@ -87,18 +86,15 @@ void AUD_AnimateableProperty::write(const float* data, int position, int count)
 
        for(int i = pos; i < position; i++)
                memcpy(buf + i * m_count, buf + (pos - 1) * m_count, m_count * sizeof(float));
-
-       unlock();
 }
 
 void AUD_AnimateableProperty::read(float position, float* out)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(!m_isAnimated)
        {
                memcpy(out, getBuffer(), m_count * sizeof(float));
-               unlock();
                return;
        }
 
@@ -147,8 +143,6 @@ void AUD_AnimateableProperty::read(float position, float* out)
                                         (t3 - 2 * t2 + t) * m0 + (t3 - t2) * m1;
                }
        }
-
-       unlock();
 }
 
 bool AUD_AnimateableProperty::isAnimated() const
index 2f25e330ebd19ca52d5f3b2930a8a57981062733..322748ad5717918350034d2f553a852d0b1d7436 100644 (file)
 #define __AUD_ANIMATEABLEPROPERTY_H__
 
 #include "AUD_Buffer.h"
+#include "AUD_ILockable.h"
 
 #include <pthread.h>
 
 /**
  * This class saves animation data for float properties.
  */
-class AUD_AnimateableProperty : private AUD_Buffer
+class AUD_AnimateableProperty : private AUD_Buffer, public AUD_ILockable
 {
 private:
        /// The count of floats for a single property.
@@ -68,12 +69,12 @@ public:
        /**
         * Locks the property.
         */
-       void lock();
+       virtual void lock();
 
        /**
         * Unlocks the previously locked property.
         */
-       void unlock();
+       virtual void unlock();
 
        /**
         * Writes the properties value and marks it non-animated.
index 46bba237cffa683aa743a5ea0d38633704ee8c6d..a1da90b073ef195d9fdee696a53caab9023ada79 100644 (file)
@@ -68,6 +68,7 @@
 #include "AUD_SequencerFactory.h"
 #include "AUD_SequencerEntry.h"
 #include "AUD_SilenceFactory.h"
+#include "AUD_MutexLock.h"
 
 #ifdef WITH_SDL
 #include "AUD_SDLDevice.h"
@@ -858,13 +859,12 @@ AUD_Handle *AUD_pauseAfter(AUD_Handle *handle, float seconds)
        AUD_Reference<AUD_IFactory> silence = new AUD_SilenceFactory;
        AUD_Reference<AUD_IFactory> limiter = new AUD_LimiterFactory(silence, 0, seconds);
 
-       AUD_device->lock();
+       AUD_MutexLock lock(*AUD_device);
 
        try {
                AUD_Handle handle2 = AUD_device->play(limiter);
                if (!handle2.isNull()) {
                        handle2->setStopCallback((stopCallback)pauseSound, handle);
-                       AUD_device->unlock();
                        return new AUD_Handle(handle2);
                }
        }
@@ -872,8 +872,6 @@ AUD_Handle *AUD_pauseAfter(AUD_Handle *handle, float seconds)
        {
        }
 
-       AUD_device->unlock();
-
        return NULL;
 }
 
index 1d6f8ca6efb3780cc77c7b15bfad086cc2ea8bd4..f4d6635e79a164bd295f02f4f8d916f6766b0e5f 100644 (file)
@@ -35,6 +35,7 @@
 #include "AUD_IFactory.h"
 #include "AUD_IReader.h"
 #include "AUD_IHandle.h"
+#include "AUD_ILockable.h"
 
 /**
  * This class represents an output device for sound sources.
@@ -44,7 +45,7 @@
  * \warning Thread safety must be insured so that no reader is beeing called
  *          twice at the same time.
  */
-class AUD_IDevice
+class AUD_IDevice : public AUD_ILockable
 {
 public:
        /**
diff --git a/intern/audaspace/intern/AUD_ILockable.h b/intern/audaspace/intern/AUD_ILockable.h
new file mode 100644 (file)
index 0000000..9bc4175
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef AUD_ILOCKABLE_H
+#define AUD_ILOCKABLE_H
+
+/**
+ * This class provides an interface for lockable objects.
+ * The main reason for this interface is to be used with AUD_MutexLock.
+ */
+class AUD_ILockable
+{
+public:
+       /**
+        * Locks the object.
+        */
+       virtual void lock()=0;
+       /**
+        * Unlocks the previously locked object.
+        */
+       virtual void unlock()=0;
+};
+
+#endif // AUD_ILOCKABLE_H
diff --git a/intern/audaspace/intern/AUD_MutexLock.h b/intern/audaspace/intern/AUD_MutexLock.h
new file mode 100644 (file)
index 0000000..b6f6d2b
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef AUD_MUTEXLOCK_H
+#define AUD_MUTEXLOCK_H
+
+#include "AUD_ILockable.h"
+
+class AUD_MutexLock
+{
+public:
+       inline AUD_MutexLock(AUD_ILockable& lockable) :
+               lockable(lockable)
+       {
+               lockable.lock();
+       }
+
+       inline ~AUD_MutexLock()
+       {
+               lockable.unlock();
+       }
+
+private:
+       AUD_ILockable& lockable;
+};
+
+#endif // AUD_MUTEXLOCK_H
index 6e6e23976269ebf1993d6b2ffde69320ec6145bd..96fd15a0bf88b313a70b4d6457b669c9770990c2 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "AUD_SequencerEntry.h"
 #include "AUD_SequencerReader.h"
+#include "AUD_MutexLock.h"
 
 #include <cmath>
 #include <limits>
@@ -87,20 +88,18 @@ void AUD_SequencerEntry::unlock()
 
 void AUD_SequencerEntry::setSound(AUD_Reference<AUD_IFactory> sound)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(m_sound.get() != sound.get())
        {
                m_sound = sound;
                m_sound_status++;
        }
-
-       unlock();
 }
 
 void AUD_SequencerEntry::move(float begin, float end, float skip)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(m_begin != begin || m_skip != skip || m_end != end)
        {
@@ -109,17 +108,13 @@ void AUD_SequencerEntry::move(float begin, float end, float skip)
                m_end = end;
                m_pos_status++;
        }
-
-       unlock();
 }
 
 void AUD_SequencerEntry::mute(bool mute)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_muted = mute;
-
-       unlock();
 }
 
 int AUD_SequencerEntry::getID() const
@@ -150,7 +145,7 @@ void AUD_SequencerEntry::updateAll(float volume_max, float volume_min, float dis
                                                                   float distance_reference, float attenuation, float cone_angle_outer,
                                                                   float cone_angle_inner, float cone_volume_outer)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(volume_max != m_volume_max)
        {
@@ -199,8 +194,6 @@ void AUD_SequencerEntry::updateAll(float volume_max, float volume_min, float dis
                m_cone_volume_outer = cone_volume_outer;
                m_status++;
        }
-
-       unlock();
 }
 
 bool AUD_SequencerEntry::isRelative()
@@ -210,15 +203,13 @@ bool AUD_SequencerEntry::isRelative()
 
 void AUD_SequencerEntry::setRelative(bool relative)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        if(m_relative != relative)
        {
                m_relative = relative;
                m_status++;
        }
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getVolumeMaximum()
@@ -228,12 +219,10 @@ float AUD_SequencerEntry::getVolumeMaximum()
 
 void AUD_SequencerEntry::setVolumeMaximum(float volume)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_volume_max = volume;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getVolumeMinimum()
@@ -243,12 +232,10 @@ float AUD_SequencerEntry::getVolumeMinimum()
 
 void AUD_SequencerEntry::setVolumeMinimum(float volume)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_volume_min = volume;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getDistanceMaximum()
@@ -258,12 +245,10 @@ float AUD_SequencerEntry::getDistanceMaximum()
 
 void AUD_SequencerEntry::setDistanceMaximum(float distance)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_distance_max = distance;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getDistanceReference()
@@ -273,12 +258,10 @@ float AUD_SequencerEntry::getDistanceReference()
 
 void AUD_SequencerEntry::setDistanceReference(float distance)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_distance_reference = distance;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getAttenuation()
@@ -288,12 +271,10 @@ float AUD_SequencerEntry::getAttenuation()
 
 void AUD_SequencerEntry::setAttenuation(float factor)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_attenuation = factor;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getConeAngleOuter()
@@ -303,12 +284,10 @@ float AUD_SequencerEntry::getConeAngleOuter()
 
 void AUD_SequencerEntry::setConeAngleOuter(float angle)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_cone_angle_outer = angle;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getConeAngleInner()
@@ -318,12 +297,10 @@ float AUD_SequencerEntry::getConeAngleInner()
 
 void AUD_SequencerEntry::setConeAngleInner(float angle)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_cone_angle_inner = angle;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerEntry::getConeVolumeOuter()
@@ -333,10 +310,8 @@ float AUD_SequencerEntry::getConeVolumeOuter()
 
 void AUD_SequencerEntry::setConeVolumeOuter(float volume)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_cone_volume_outer = volume;
        m_status++;
-
-       unlock();
 }
index 7ff6fffea11521f8c744b35618521792a7df9b17..46efc52d66b2c508589fe4fb1a311a37ab7bfef6 100644 (file)
 #include "AUD_Reference.h"
 #include "AUD_AnimateableProperty.h"
 #include "AUD_IFactory.h"
+#include "AUD_ILockable.h"
 
 #include <pthread.h>
 
 /**
  * This class represents a sequenced entry in a sequencer factory.
  */
-class AUD_SequencerEntry
+class AUD_SequencerEntry : public AUD_ILockable
 {
        friend class AUD_SequencerHandle;
 private:
@@ -130,12 +131,12 @@ public:
        /**
         * Locks the entry.
         */
-       void lock();
+       virtual void lock();
 
        /**
         * Unlocks the previously locked entry.
         */
-       void unlock();
+       virtual void unlock();
 
        /**
         * Sets the sound of the entry.
index fd495f467cfdd2f99a881258679d9a92b695e2e9..2cc0a656cf15130796ddfd8f86b7323fffcaea1b 100644 (file)
@@ -30,6 +30,7 @@
 #include "AUD_SequencerFactory.h"
 #include "AUD_SequencerReader.h"
 #include "AUD_3DMath.h"
+#include "AUD_MutexLock.h"
 
 AUD_SequencerFactory::AUD_SequencerFactory(AUD_Specs specs, float fps, bool muted) :
        m_specs(specs),
@@ -75,30 +76,24 @@ void AUD_SequencerFactory::unlock()
 
 void AUD_SequencerFactory::setSpecs(AUD_Specs specs)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_specs = specs;
        m_status++;
-
-       unlock();
 }
 
 void AUD_SequencerFactory::setFPS(float fps)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_fps = fps;
-
-       unlock();
 }
 
 void AUD_SequencerFactory::mute(bool muted)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_muted = muted;
-
-       unlock();
 }
 
 bool AUD_SequencerFactory::getMute() const
@@ -113,12 +108,10 @@ float AUD_SequencerFactory::getSpeedOfSound() const
 
 void AUD_SequencerFactory::setSpeedOfSound(float speed)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_speed_of_sound = speed;
        m_status++;
-
-       unlock();
 }
 
 float AUD_SequencerFactory::getDopplerFactor() const
@@ -128,12 +121,10 @@ float AUD_SequencerFactory::getDopplerFactor() const
 
 void AUD_SequencerFactory::setDopplerFactor(float factor)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_doppler_factor = factor;
        m_status++;
-
-       unlock();
 }
 
 AUD_DistanceModel AUD_SequencerFactory::getDistanceModel() const
@@ -143,12 +134,10 @@ AUD_DistanceModel AUD_SequencerFactory::getDistanceModel() const
 
 void AUD_SequencerFactory::setDistanceModel(AUD_DistanceModel model)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_distance_model = model;
        m_status++;
-
-       unlock();
 }
 
 AUD_AnimateableProperty* AUD_SequencerFactory::getAnimProperty(AUD_AnimateablePropertyType type)
@@ -168,26 +157,22 @@ AUD_AnimateableProperty* AUD_SequencerFactory::getAnimProperty(AUD_AnimateablePr
 
 AUD_Reference<AUD_SequencerEntry> AUD_SequencerFactory::add(AUD_Reference<AUD_IFactory> sound, float begin, float end, float skip)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        AUD_Reference<AUD_SequencerEntry> entry = new AUD_SequencerEntry(sound, begin, end, skip, m_id++);
 
        m_entries.push_front(entry);
        m_entry_status++;
 
-       unlock();
-
        return entry;
 }
 
 void AUD_SequencerFactory::remove(AUD_Reference<AUD_SequencerEntry> entry)
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        m_entries.remove(entry);
        m_entry_status++;
-
-       unlock();
 }
 
 AUD_Reference<AUD_IReader> AUD_SequencerFactory::createQualityReader()
index 38241112020f23ed277659c251bf161d28c38901..38cbc2d1867f38d40ffb0f2004353d3cafb95682 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "AUD_IFactory.h"
 #include "AUD_AnimateableProperty.h"
+#include "AUD_ILockable.h"
 
 #include <list>
 #include <pthread.h>
@@ -41,7 +42,7 @@ class AUD_SequencerEntry;
 /**
  * This factory represents sequenced entries to play a sound scene.
  */
-class AUD_SequencerFactory : public AUD_IFactory
+class AUD_SequencerFactory : public AUD_IFactory, public AUD_ILockable
 {
        friend class AUD_SequencerReader;
 private:
@@ -104,12 +105,12 @@ public:
        /**
         * Locks the factory.
         */
-       void lock();
+       virtual void lock();
 
        /**
         * Unlocks the previously locked factory.
         */
-       void unlock();
+       virtual void unlock();
 
        /**
         * Sets the audio output specification.
index f4bfae6cee75480180cc8aa7a46c202218985e1d..38d2ed38a7edaeb02cba9b02953cd723b39f3ed3 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "AUD_SequencerHandle.h"
 #include "AUD_ReadDevice.h"
+#include "AUD_MutexLock.h"
 
 AUD_SequencerHandle::AUD_SequencerHandle(AUD_Reference<AUD_SequencerEntry> entry, AUD_ReadDevice& device) :
        m_entry(entry),
@@ -68,7 +69,7 @@ void AUD_SequencerHandle::update(float position, float frame, float fps)
 {
        if(!m_handle.isNull())
        {
-               m_entry->lock();
+               AUD_MutexLock lock(*m_entry);
                if(position >= m_entry->m_end && m_entry->m_end >= 0)
                        m_handle->pause();
                else if(position >= m_entry->m_begin)
@@ -134,7 +135,6 @@ void AUD_SequencerHandle::update(float position, float frame, float fps)
 
                if(m_entry->m_muted)
                        m_handle->setVolume(0);
-               m_entry->unlock();
        }
 }
 
@@ -142,11 +142,10 @@ void AUD_SequencerHandle::seek(float position)
 {
        if(!m_handle.isNull())
        {
-               m_entry->lock();
+               AUD_MutexLock lock(*m_entry);
                if(position >= m_entry->m_end && m_entry->m_end >= 0)
                {
                        m_handle->pause();
-                       m_entry->unlock();
                        return;
                }
 
@@ -160,6 +159,5 @@ void AUD_SequencerHandle::seek(float position)
                        m_handle->pause();
                else
                        m_handle->resume();
-               m_entry->unlock();
        }
 }
index 2e41a99d3dbe63b2093ed908f19b812f2884c37a..39a4d29e6e4c39442d17451895789b9da2ea920b 100644 (file)
@@ -28,6 +28,7 @@
 
 
 #include "AUD_SequencerReader.h"
+#include "AUD_MutexLock.h"
 
 typedef std::list<AUD_Reference<AUD_SequencerHandle> >::iterator AUD_HandleIterator;
 typedef std::list<AUD_Reference<AUD_SequencerEntry> >::iterator AUD_EntryIterator;
@@ -77,7 +78,7 @@ AUD_Specs AUD_SequencerReader::getSpecs() const
 
 void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
 {
-       m_factory->lock();
+       AUD_MutexLock lock(*m_factory);
 
        if(m_factory->m_status != m_status)
        {
@@ -197,8 +198,6 @@ void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
                time += float(len) / float(specs.rate);
        }
 
-       m_factory->unlock();
-
        m_position += length;
 
        eos = false;
index 1d993abab73e9f1939d47d0b23760b046184ddda..14f0c6429e9496ad603a019a0c46f4b473370e72 100644 (file)
@@ -33,6 +33,7 @@
 #include "AUD_IFactory.h"
 #include "AUD_JOSResampleReader.h"
 #include "AUD_LinearResampleReader.h"
+#include "AUD_MutexLock.h"
 
 #include <cstring>
 #include <cmath>
@@ -226,7 +227,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::pause()
 {
        if(m_status)
        {
-               m_device->lock();
+               AUD_MutexLock lock(*m_device);
 
                if(m_status == AUD_STATUS_PLAYING)
                {
@@ -236,12 +237,9 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::pause()
                        if(m_device->m_playingSounds.empty())
                                m_device->playing(m_device->m_playback = false);
                        m_status = AUD_STATUS_PAUSED;
-                       m_device->unlock();
 
                        return true;
                }
-
-               m_device->unlock();
        }
 
        return false;
@@ -251,7 +249,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::resume()
 {
        if(m_status)
        {
-               m_device->lock();
+               AUD_MutexLock lock(*m_device);
 
                if(m_status == AUD_STATUS_PAUSED)
                {
@@ -261,11 +259,9 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::resume()
                        if(!m_device->m_playback)
                                m_device->playing(m_device->m_playback = true);
                        m_status = AUD_STATUS_PLAYING;
-                       m_device->unlock();
                        return true;
                }
 
-               m_device->unlock();
        }
 
        return false;
@@ -276,7 +272,10 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::stop()
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
 
        // AUD_XXX Create a reference of our own object so that it doesn't get
        // deleted before the end of this function
@@ -292,7 +291,6 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::stop()
        else
                m_device->m_pausedSounds.remove(This);
 
-       m_device->unlock();
        m_status = AUD_STATUS_INVALID;
        return true;
 }
@@ -310,11 +308,12 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::setKeep(bool keep)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       m_keep = keep;
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       m_keep = keep;
 
        return true;
 }
@@ -324,11 +323,12 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::seek(float position)
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       m_reader->seek((int)(position * m_reader->getSpecs().rate));
+       if(!m_status)
+               return false;
 
-       m_device->unlock();
+       m_reader->seek((int)(position * m_reader->getSpecs().rate));
 
        return true;
 }
@@ -336,13 +336,14 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::seek(float position)
 float AUD_SoftwareDevice::AUD_SoftwareHandle::getPosition()
 {
        if(!m_status)
-               return 0.0f;
+               return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
 
-       float position = m_reader->getPosition() / (float)m_device->m_specs.rate;
+       if(!m_status)
+               return 0.0f;
 
-       m_device->unlock();
+       float position = m_reader->getPosition() / (float)m_device->m_specs.rate;
 
        return position;
 }
@@ -407,13 +408,14 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::setStopCallback(stopCallback callba
        if(!m_status)
                return false;
 
-       m_device->lock();
+       AUD_MutexLock lock(*m_device);
+
+       if(!m_status)
+               return false;
 
        m_stop = callback;
        m_stop_data = data;
 
-       m_device->unlock();
-
        return true;
 }
 
@@ -691,7 +693,7 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length)
 {
        m_buffer.assureSize(length * AUD_SAMPLE_SIZE(m_specs));
 
-       lock();
+       AUD_MutexLock lock(*this);
 
        {
                AUD_Reference<AUD_SoftwareDevice::AUD_SoftwareHandle> sound;
@@ -775,8 +777,6 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length)
                        sound->pause();
                }
        }
-
-       unlock();
 }
 
 void AUD_SoftwareDevice::setPanning(AUD_IHandle* handle, float pan)
@@ -833,12 +833,12 @@ AUD_Reference<AUD_IHandle> AUD_SoftwareDevice::play(AUD_Reference<AUD_IReader> r
        // play sound
        AUD_Reference<AUD_SoftwareDevice::AUD_SoftwareHandle> sound = new AUD_SoftwareDevice::AUD_SoftwareHandle(this, reader, pitch, resampler, mapper, keep);
 
-       lock();
+       AUD_MutexLock lock(*this);
+
        m_playingSounds.push_back(sound);
 
        if(!m_playback)
                playing(m_playback = true);
-       unlock();
 
        return AUD_Reference<AUD_IHandle>(sound);
 }
@@ -850,15 +850,13 @@ AUD_Reference<AUD_IHandle> AUD_SoftwareDevice::play(AUD_Reference<AUD_IFactory>
 
 void AUD_SoftwareDevice::stopAll()
 {
-       lock();
+       AUD_MutexLock lock(*this);
 
        while(!m_playingSounds.empty())
                m_playingSounds.front()->stop();
 
        while(!m_pausedSounds.empty())
                m_pausedSounds.front()->stop();
-
-       unlock();
 }
 
 void AUD_SoftwareDevice::lock()