]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/mutex.h
2 ******************************************************************************
4 * Copyright (C) 1997-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 //----------------------------------------------------------------------------
12 // Lightweight C++ wrapper for umtx_ C mutex functions
14 // Author: Alan Liu 1/31/97
16 // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop.
17 // 04/07/1999 srl refocused as a thin wrapper
19 //----------------------------------------------------------------------------
23 #include "unicode/utypes.h"
24 #include "unicode/uobject.h"
29 //----------------------------------------------------------------------------
30 // Code within that accesses shared static or global data should
31 // should instantiate a Mutex object while doing so. You should make your own
32 // private mutex where possible.
38 // void Function(int arg1, int arg2)
40 // static Object* foo; // Shared read-write object
41 // Mutex mutex(&myMutex); // or no args for the global lock
43 // // When 'mutex' goes out of scope and gets destroyed here, the lock is released
46 // Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function
47 // returning a Mutex. This is a common mistake which silently slips through the
51 class U_COMMON_API Mutex
: public UMemory
{
53 inline Mutex(UMTX
*mutex
= NULL
);
59 Mutex(const Mutex
&other
); // forbid copying of this class
60 Mutex
&operator=(const Mutex
&other
); // forbid copying of this class
63 inline Mutex::Mutex(UMTX
*mutex
)
69 inline Mutex::~Mutex()
74 // common code for singletons ---------------------------------------------- ***
77 * Function pointer for the instantiator parameter of
78 * SimpleSingleton::getInstance() and TriStateSingleton::getInstance().
79 * The function creates some object, optionally using the context parameter.
80 * The function need not check for U_FAILURE(errorCode).
82 typedef void *InstantiatorFn(const void *context
, UErrorCode
&errorCode
);
85 * Singleton struct with shared instantiation/mutexing code.
86 * Simple: Does not remember if a previous instantiation failed.
87 * Best used if the instantiation can really only fail with an out-of-memory error,
88 * otherwise use a TriStateSingleton.
89 * Best used via SimpleSingletonWrapper or similar.
90 * Define a static SimpleSingleton instance via the STATIC_SIMPLE_SINGLETON macro.
92 struct SimpleSingleton
{
96 * Returns the singleton instance, or NULL if it could not be created.
97 * Calls the instantiator with the context if the instance has not been
98 * created yet. In a race condition, the duplicate may not be NULL.
99 * The caller must delete the duplicate.
100 * The caller need not initialize the duplicate before the call.
102 void *getInstance(InstantiatorFn
*instantiator
, const void *context
,
104 UErrorCode
&errorCode
);
106 * Resets the fields. The caller must have deleted the singleton instance.
108 * Call this from a cleanup function.
110 void reset() { fInstance
=NULL
; }
113 #define STATIC_SIMPLE_SINGLETON(name) static SimpleSingleton name={ NULL }
116 * Handy wrapper for an SimpleSingleton.
117 * Intended for temporary use on the stack, to make the SimpleSingleton easier to deal with.
118 * Takes care of the duplicate deletion and type casting.
121 class SimpleSingletonWrapper
{
123 SimpleSingletonWrapper(SimpleSingleton
&s
) : singleton(s
) {}
124 void deleteInstance() {
125 delete (T
*)singleton
.fInstance
;
128 T
*getInstance(InstantiatorFn
*instantiator
, const void *context
,
129 UErrorCode
&errorCode
) {
131 T
*instance
=(T
*)singleton
.getInstance(instantiator
, context
, duplicate
, errorCode
);
132 delete (T
*)duplicate
;
136 SimpleSingleton
&singleton
;
140 * Singleton struct with shared instantiation/mutexing code.
141 * Tri-state: Instantiation succeeded/failed/not attempted yet.
142 * Best used via TriStateSingletonWrapper or similar.
143 * Define a static TriStateSingleton instance via the STATIC_TRI_STATE_SINGLETON macro.
145 struct TriStateSingleton
{
147 UErrorCode fErrorCode
;
148 int8_t fHaveInstance
;
151 * Returns the singleton instance, or NULL if it could not be created.
152 * Calls the instantiator with the context if the instance has not been
153 * created yet. In a race condition, the duplicate may not be NULL.
154 * The caller must delete the duplicate.
155 * The caller need not initialize the duplicate before the call.
156 * The singleton creation is only attempted once. If it fails,
157 * the singleton will then always return NULL.
159 void *getInstance(InstantiatorFn
*instantiator
, const void *context
,
161 UErrorCode
&errorCode
);
163 * Resets the fields. The caller must have deleted the singleton instance.
165 * Call this from a cleanup function.
170 #define STATIC_TRI_STATE_SINGLETON(name) static TriStateSingleton name={ NULL, U_ZERO_ERROR, 0 }
173 * Handy wrapper for an TriStateSingleton.
174 * Intended for temporary use on the stack, to make the TriStateSingleton easier to deal with.
175 * Takes care of the duplicate deletion and type casting.
178 class TriStateSingletonWrapper
{
180 TriStateSingletonWrapper(TriStateSingleton
&s
) : singleton(s
) {}
181 void deleteInstance() {
182 delete (T
*)singleton
.fInstance
;
185 T
*getInstance(InstantiatorFn
*instantiator
, const void *context
,
186 UErrorCode
&errorCode
) {
188 T
*instance
=(T
*)singleton
.getInstance(instantiator
, context
, duplicate
, errorCode
);
189 delete (T
*)duplicate
;
193 TriStateSingleton
&singleton
;