2 *******************************************************************************
4 * Copyright (C) 2008-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
14 #include "unicode/utypes.h"
20 void *SimpleSingleton::getInstance(InstantiatorFn
*instantiator
, const void *context
,
22 UErrorCode
&errorCode
) {
24 if(U_FAILURE(errorCode
)) {
27 // TODO: With atomicops.h: void *instance = (void*)Acquire_Load(&fInstance);
28 // and remove UMTX_ACQUIRE_BARRIER below.
29 void *instance
=ANNOTATE_UNPROTECTED_READ(fInstance
);
31 ANNOTATE_HAPPENS_AFTER(&fInstance
);
36 // Attempt to create the instance.
37 // If a race occurs, then the losing thread will assign its new instance
38 // to the "duplicate" parameter, and the caller deletes it.
39 instance
=instantiator(context
, errorCode
);
40 UMTX_RELEASE_BARRIER
; // Release-barrier before fInstance=instance;
42 if(fInstance
==NULL
&& U_SUCCESS(errorCode
)) {
43 U_ASSERT(instance
!=NULL
);
44 ANNOTATE_HAPPENS_BEFORE(&fInstance
);
45 // TODO: With atomicops.h: Release_Store(&fInstance, (AtomicWord)instance);
46 // and remove UMTX_RELEASE_BARRIER above.
57 * Initial state: Instance creation not attempted yet.
58 * fInstance=NULL && U_SUCCESS(fErrorCode)
60 * Instance creation succeeded:
61 * fInstance!=NULL && U_SUCCESS(fErrorCode)
63 * Instance creation failed:
64 * fInstance=NULL && U_FAILURE(fErrorCode)
65 * We will not attempt again to create the instance.
67 * fInstance changes at most once.
68 * fErrorCode changes at most twice (intial->failed->succeeded).
70 void *TriStateSingleton::getInstance(InstantiatorFn
*instantiator
, const void *context
,
72 UErrorCode
&errorCode
) {
74 if(U_FAILURE(errorCode
)) {
77 // TODO: With atomicops.h: void *instance = (void*)Acquire_Load(&fInstance);
78 // and remove UMTX_ACQUIRE_BARRIER below.
79 void *instance
=ANNOTATE_UNPROTECTED_READ(fInstance
);
81 ANNOTATE_HAPPENS_AFTER(&fInstance
);
83 // instance was created
87 // The read access to fErrorCode is thread-unsafe, but harmless because
88 // at worst multiple threads race to each create a new instance,
89 // and all losing threads delete their duplicates.
90 UErrorCode localErrorCode
=ANNOTATE_UNPROTECTED_READ(fErrorCode
);
91 if(U_FAILURE(localErrorCode
)) {
92 // instance creation failed
93 errorCode
=localErrorCode
;
97 // First attempt to create the instance.
98 // If a race occurs, then the losing thread will assign its new instance
99 // to the "duplicate" parameter, and the caller deletes it.
100 instance
=instantiator(context
, errorCode
);
101 UMTX_RELEASE_BARRIER
; // Release-barrier before fInstance=instance;
103 if(fInstance
==NULL
&& U_SUCCESS(errorCode
)) {
104 // instance creation newly succeeded
105 U_ASSERT(instance
!=NULL
);
106 ANNOTATE_HAPPENS_BEFORE(&fInstance
);
107 // TODO: With atomicops.h: Release_Store(&fInstance, (AtomicWord)instance);
108 // and remove UMTX_RELEASE_BARRIER above.
110 // Set fErrorCode on the off-chance that a previous instance creation failed.
111 fErrorCode
=errorCode
;
112 // Completed state transition: initial->succeeded, or failed->succeeded.
114 // Record a duplicate if we lost the race, or
115 // if we got an instance but its creation failed anyway.
117 if(fInstance
==NULL
&& U_SUCCESS(fErrorCode
) && U_FAILURE(errorCode
)) {
118 // instance creation newly failed
119 fErrorCode
=errorCode
;
120 // Completed state transition: initial->failed.
126 void TriStateSingleton::reset() {
128 fErrorCode
=U_ZERO_ERROR
;
131 #if UCONFIG_NO_SERVICE
133 /* If UCONFIG_NO_SERVICE, then there is no invocation of Mutex elsewhere in
134 common, so add one here to force an export */
135 static Mutex
*aMutex
= 0;
137 /* UCONFIG_NO_SERVICE */