Bug 1160914 - Use locks for gmp-clearkey threadsafe refcounting when std::atomic is not easily available. r=edwin

This commit is contained in:
Chris Pearce 2015-05-06 11:40:42 +12:00
parent 08c186dbef
commit efb6bf0811
5 changed files with 69 additions and 17 deletions

View File

@ -23,6 +23,7 @@
#include "gmp-api/gmp-decryption.h"
#include "Endian.h"
#include <assert.h>
#include <string.h>
using namespace mozilla;

View File

@ -24,6 +24,7 @@
#include "ClearKeyBase64.h"
#include "ArrayUtils.h"
#include <assert.h>
#include <memory.h>
#include "Endian.h"
#include "openaes/oaes_lib.h"
@ -539,3 +540,10 @@ ClearKeyUtils::IsValidSessionId(const char* aBuff, uint32_t aLength)
}
return true;
}
GMPMutex* GMPCreateMutex() {
GMPMutex* mutex;
auto err = GetPlatform()->createmutex(&mutex);
assert(mutex);
return GMP_FAILED(err) ? nullptr : mutex;
}

View File

@ -20,6 +20,7 @@
#include <stdint.h>
#include <string>
#include <vector>
#include <assert.h>
#include "gmp-decryption.h"
#define CLEARKEY_KEY_LEN ((size_t)16)
@ -75,4 +76,25 @@ Contains(const Container& aContainer, const Element& aElement)
return aContainer.find(aElement) != aContainer.end();
}
class AutoLock {
public:
explicit AutoLock(GMPMutex* aMutex)
: mMutex(aMutex)
{
assert(aMutex);
if (mMutex) {
mMutex->Acquire();
}
}
~AutoLock() {
if (mMutex) {
mMutex->Release();
}
}
private:
GMPMutex* mMutex;
};
GMPMutex* GMPCreateMutex();
#endif // __ClearKeyUtils_h__

View File

@ -18,8 +18,44 @@
#define __RefCount_h__
#include <stdint.h>
#include <atomic>
#include <assert.h>
#include "ClearKeyUtils.h"
#if defined(_MSC_VER)
#include <atomic>
typedef std::atomic<uint32_t> AtomicRefCount;
#else
class AtomicRefCount {
public:
explicit AtomicRefCount(uint32_t aValue)
: mCount(aValue)
, mMutex(GMPCreateMutex())
{
assert(mMutex);
}
~AtomicRefCount()
{
if (mMutex) {
mMutex->Destroy();
}
}
uint32_t operator--() {
AutoLock lock(mMutex);
return --mCount;
}
uint32_t operator++() {
AutoLock lock(mMutex);
return ++mCount;
}
operator uint32_t() {
AutoLock lock(mMutex);
return mCount;
}
private:
uint32_t mCount;
GMPMutex* mMutex;
};
#endif
// Note: Thread safe.
class RefCounted {
@ -45,7 +81,7 @@ protected:
{
assert(!mRefCount);
}
std::atomic<uint32_t> mRefCount;
AtomicRefCount mRefCount;
};
template<class T>

View File

@ -243,21 +243,6 @@ inline uint32_t MicrosecondsToRTPTime(Microseconds us) {
return uint32_t(0xffffffff & (us * 90000) / 1000000);
}
class AutoLock {
public:
AutoLock(GMPMutex* aMutex)
: mMutex(aMutex)
{
assert(aMutex);
mMutex->Acquire();
}
~AutoLock() {
mMutex->Release();
}
private:
GMPMutex* mMutex;
};
void dump(const uint8_t* data, uint32_t len, const char* filename);
HRESULT