summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.6/common/mutex.cpp
blob: 8da3c7e0f675ecb37069df0606989ce3b374404b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
*******************************************************************************
*
*   Copyright (C) 2008-2010, International Business Machines
*   Corporation and others.  All Rights Reserved.
*
*******************************************************************************
*   file name:  mutex.cpp
*   encoding:   US-ASCII
*   tab size:   8 (not used)
*   indentation:4
*/

#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;  // instance was created
    } else if(haveInstance<0) {
        errorCode=fErrorCode;  // instance creation failed
        return NULL;
    } else /* haveInstance==0 */ {
        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

/* If UCONFIG_NO_SERVICE, then there is no invocation of Mutex elsewhere in
   common, so add one here to force an export */
static Mutex *aMutex = 0;

/* UCONFIG_NO_SERVICE */
#endif

U_NAMESPACE_END