Home | History | Annotate | Download | only in common
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 2008-2010, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *   file name:  mutex.cpp
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 */
     13 
     14 #include "unicode/utypes.h"
     15 #include "mutex.h"
     16 
     17 U_NAMESPACE_BEGIN
     18 
     19 void *SimpleSingleton::getInstance(InstantiatorFn *instantiator, const void *context,
     20                                    void *&duplicate,
     21                                    UErrorCode &errorCode) {
     22     duplicate=NULL;
     23     if(U_FAILURE(errorCode)) {
     24         return NULL;
     25     }
     26     void *instance;
     27     UMTX_CHECK(NULL, fInstance, instance);
     28     if(instance!=NULL) {
     29         return instance;
     30     } else {
     31         instance=instantiator(context, errorCode);
     32         Mutex mutex;
     33         if(fInstance==NULL && U_SUCCESS(errorCode)) {
     34             fInstance=instance;
     35         } else {
     36             duplicate=instance;
     37         }
     38         return fInstance;
     39     }
     40 }
     41 
     42 void *TriStateSingleton::getInstance(InstantiatorFn *instantiator, const void *context,
     43                                      void *&duplicate,
     44                                      UErrorCode &errorCode) {
     45     duplicate=NULL;
     46     if(U_FAILURE(errorCode)) {
     47         return NULL;
     48     }
     49     int8_t haveInstance;
     50     UMTX_CHECK(NULL, fHaveInstance, haveInstance);
     51     if(haveInstance>0) {
     52         return fInstance;  // instance was created
     53     } else if(haveInstance<0) {
     54         errorCode=fErrorCode;  // instance creation failed
     55         return NULL;
     56     } else /* haveInstance==0 */ {
     57         void *instance=instantiator(context, errorCode);
     58         Mutex mutex;
     59         if(fHaveInstance==0) {
     60             if(U_SUCCESS(errorCode)) {
     61                 fInstance=instance;
     62                 instance=NULL;
     63                 fHaveInstance=1;
     64             } else {
     65                 fErrorCode=errorCode;
     66                 fHaveInstance=-1;
     67             }
     68         } else {
     69             errorCode=fErrorCode;
     70         }
     71         duplicate=instance;
     72         return fInstance;
     73     }
     74 }
     75 
     76 void TriStateSingleton::reset() {
     77     fInstance=NULL;
     78     fErrorCode=U_ZERO_ERROR;
     79     fHaveInstance=0;
     80 }
     81 
     82 #if UCONFIG_NO_SERVICE
     83 
     84 /* If UCONFIG_NO_SERVICE, then there is no invocation of Mutex elsewhere in
     85    common, so add one here to force an export */
     86 static Mutex *aMutex = 0;
     87 
     88 /* UCONFIG_NO_SERVICE */
     89 #endif
     90 
     91 U_NAMESPACE_END
     92