Make it ok to call nsUUIDGenerator from off the main thread. Bug 334983,

r=vlad, sr=darin
This commit is contained in:
bzbarsky%mit.edu 2006-05-14 22:41:47 +00:00
parent 3f0d8f2e42
commit 792a02421d
3 changed files with 32 additions and 12 deletions

View File

@ -48,18 +48,34 @@
#include "nsMemory.h"
#include "nsAutoLock.h"
#include "nsUUIDGenerator.h"
NS_IMPL_ISUPPORTS1(nsUUIDGenerator, nsIUUIDGenerator)
NS_IMPL_THREADSAFE_ISUPPORTS1(nsUUIDGenerator, nsIUUIDGenerator)
nsUUIDGenerator::nsUUIDGenerator()
: mInitialized(PR_FALSE)
: mLock(nsnull)
{
}
nsUUIDGenerator::~nsUUIDGenerator()
{
if (mLock) {
PR_DestroyLock(mLock);
}
}
nsresult
nsUUIDGenerator::Init()
{
mLock = PR_NewLock();
NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
// We're a service, so we're guaranteed that Init() is not going
// to be reentered while we're inside Init().
#if !defined(XP_WIN) && !defined(XP_MACOSX)
/* initialize random number generator using NSPR random noise */
unsigned int seed;
@ -90,7 +106,6 @@ nsUUIDGenerator::Init()
#endif /* non XP_WIN and non XP_MACOSX */
mInitialized = PR_TRUE;
return NS_OK;
}
@ -114,12 +129,10 @@ nsUUIDGenerator::GenerateUUID(nsID** ret)
NS_IMETHODIMP
nsUUIDGenerator::GenerateUUIDInPlace(nsID* id)
{
if (!mInitialized) {
nsresult rv = Init();
if (NS_FAILED(rv))
return rv;
}
// The various code in this method is probably not threadsafe, so lock
// across the whole method.
nsAutoLock lock(mLock);
#if defined(XP_WIN)
HRESULT hr = CoCreateGuid((GUID*)id);
if (NS_FAILED(hr))

View File

@ -40,6 +40,7 @@
#define _NSUUIDGENERATOR_H_
#include "nsIUUIDGenerator.h"
#include "prlock.h"
class nsUUIDGenerator : public nsIUUIDGenerator {
public:
@ -49,12 +50,18 @@ public:
NS_DECL_NSIUUIDGENERATOR
protected:
nsresult Init();
PRBool mInitialized;
private:
~nsUUIDGenerator();
protected:
PRLock* mLock;
#if !defined(XP_WIN) && !defined(XP_MACOSX)
char mState[32];
PRUint8 mRBytes;
#endif
};
#define NS_UUID_GENERATOR_CONTRACTID "@mozilla.org/uuid-generator;1"

View File

@ -220,7 +220,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsHashPropertyBag, Init)
NS_GENERIC_AGGREGATED_CONSTRUCTOR_INIT(nsProperties, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUUIDGenerator)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUUIDGenerator, Init)
#ifdef XP_MACOSX
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMacUtilsImpl)