#include "unicode/utypes.h"
#include "mutex.h"
U_NAMESPACE_BEGIN
void *SimpleSingleton::getInstance(InstantiatorFn *instantiator, const void *context,
void *&duplicate,
UErrorCode &errorCode) {
duplicate=NULL;
if(U_FAILURE(errorCode)) {
return NULL;
}
void *instance;
UMTX_CHECK(NULL, fInstance, instance);
if(instance!=NULL) {
return instance;
} else {
instance=instantiator(context, errorCode);
Mutex mutex;
if(fInstance==NULL && U_SUCCESS(errorCode)) {
fInstance=instance;
} else {
duplicate=instance;
}
return fInstance;
}
}
void *TriStateSingleton::getInstance(InstantiatorFn *instantiator, const void *context,
void *&duplicate,
UErrorCode &errorCode) {
duplicate=NULL;
if(U_FAILURE(errorCode)) {
return NULL;
}
int8_t haveInstance;
UMTX_CHECK(NULL, fHaveInstance, haveInstance);
if(haveInstance>0) {
return fInstance; } else if(haveInstance<0) {
errorCode=fErrorCode; return NULL;
} else {
void *instance=instantiator(context, errorCode);
Mutex mutex;
if(fHaveInstance==0) {
if(U_SUCCESS(errorCode)) {
fInstance=instance;
instance=NULL;
fHaveInstance=1;
} else {
fErrorCode=errorCode;
fHaveInstance=-1;
}
} else {
errorCode=fErrorCode;
}
duplicate=instance;
return fInstance;
}
}
void TriStateSingleton::reset() {
fInstance=NULL;
fErrorCode=U_ZERO_ERROR;
fHaveInstance=0;
}
#if UCONFIG_NO_SERVICE
static Mutex *aMutex = 0;
#endif
U_NAMESPACE_END