author | dav |
Tue, 16 Sep 2008 12:17:02 +0400 | |
changeset 1962 | 6c293d33645b |
parent 1954 | b93b85df3211 |
child 2472 | b7aba00cabb6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
105
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* The Toolkit class has two functions: it instantiates the AWT |
|
28 |
* ToolkitPeer's native methods, and provides the DLL's core functions. |
|
29 |
* |
|
30 |
* There are two ways this DLL can be used: either as a dynamically- |
|
31 |
* loaded Java native library from the interpreter, or by a Windows- |
|
32 |
* specific app. The first manner requires that the Toolkit provide |
|
33 |
* all support needed so the app can function as a first-class Windows |
|
34 |
* app, while the second assumes that the app will provide that |
|
35 |
* functionality. Which mode this DLL functions in is determined by |
|
36 |
* which initialization paradigm is used. If the Toolkit is constructed |
|
37 |
* normally, then the Toolkit will have its own pump. If it is explicitly |
|
38 |
* initialized for an embedded environment (via a static method on |
|
39 |
* sun.awt.windows.WToolkit), then it will rely on an external message |
|
40 |
* pump. |
|
41 |
* |
|
42 |
* The most basic functionality needed is a Windows message pump (also |
|
43 |
* known as a message loop). When an Java app is started as a console |
|
44 |
* app by the interpreter, the Toolkit needs to provide that message |
|
45 |
* pump if the AWT is dynamically loaded. |
|
46 |
*/ |
|
47 |
||
48 |
#ifndef AWT_TOOLKIT_H |
|
49 |
#define AWT_TOOLKIT_H |
|
50 |
||
51 |
#include "awt.h" |
|
52 |
#include "awtmsg.h" |
|
53 |
#include "Trace.h" |
|
54 |
||
55 |
#include "sun_awt_windows_WToolkit.h" |
|
56 |
||
57 |
class AwtObject; |
|
58 |
class AwtDialog; |
|
59 |
class AwtDropTarget; |
|
60 |
||
61 |
typedef VOID (CALLBACK* IDLEPROC)(VOID); |
|
62 |
typedef BOOL (CALLBACK* PEEKMESSAGEPROC)(MSG&); |
|
63 |
||
64 |
/* |
|
65 |
* class JNILocalFrame |
|
66 |
* Push/PopLocalFrame helper |
|
67 |
*/ |
|
68 |
class JNILocalFrame { |
|
69 |
public: |
|
70 |
INLINE JNILocalFrame(JNIEnv *env, int size) { |
|
71 |
m_env = env; |
|
72 |
int result = m_env->PushLocalFrame(size); |
|
73 |
if (result < 0) { |
|
74 |
DASSERT(FALSE); |
|
75 |
JNU_ThrowOutOfMemoryError(m_env, "Can't allocate localRefs"); |
|
76 |
} |
|
77 |
} |
|
78 |
INLINE ~JNILocalFrame() { m_env->PopLocalFrame(NULL); } |
|
79 |
private: |
|
80 |
JNIEnv* m_env; |
|
81 |
}; |
|
82 |
||
83 |
/* |
|
84 |
* class CriticalSection |
|
85 |
* ~~~~~ ~~~~~~~~~~~~~~~~ |
|
86 |
* Lightweight intra-process thread synchronization. Can only be used with |
|
87 |
* other critical sections, and only within the same process. |
|
88 |
*/ |
|
89 |
class CriticalSection { |
|
90 |
public: |
|
1954 | 91 |
INLINE CriticalSection() { ::InitializeCriticalSection(&rep); } |
92 |
INLINE ~CriticalSection() { ::DeleteCriticalSection(&rep); } |
|
2 | 93 |
|
94 |
class Lock { |
|
95 |
public: |
|
96 |
INLINE Lock(const CriticalSection& cs) : critSec(cs) { |
|
97 |
(const_cast<CriticalSection &>(critSec)).Enter(); |
|
98 |
} |
|
99 |
INLINE ~Lock() { |
|
100 |
(const_cast<CriticalSection &>(critSec)).Leave(); |
|
101 |
} |
|
102 |
private: |
|
103 |
const CriticalSection& critSec; |
|
104 |
}; |
|
105 |
friend Lock; |
|
106 |
||
107 |
private: |
|
108 |
CRITICAL_SECTION rep; |
|
109 |
||
110 |
CriticalSection(const CriticalSection&); |
|
111 |
const CriticalSection& operator =(const CriticalSection&); |
|
112 |
||
113 |
public: |
|
1954 | 114 |
virtual void Enter() { |
115 |
::EnterCriticalSection(&rep); |
|
2 | 116 |
} |
1954 | 117 |
virtual BOOL TryEnter() { |
118 |
return ::TryEnterCriticalSection(&rep); |
|
2 | 119 |
} |
1954 | 120 |
virtual void Leave() { |
2 | 121 |
::LeaveCriticalSection(&rep); |
122 |
} |
|
123 |
}; |
|
124 |
||
125 |
// Macros for using CriticalSection objects that help trace |
|
126 |
// lock/unlock actions |
|
127 |
#define CRITICAL_SECTION_ENTER(cs) { \ |
|
128 |
J2dTraceLn4(J2D_TRACE_VERBOSE2, \ |
|
129 |
"CS.Wait: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ |
|
130 |
GetCurrentThreadId(), &(cs), __FILE__, __LINE__); \ |
|
131 |
(cs).Enter(); \ |
|
132 |
J2dTraceLn4(J2D_TRACE_VERBOSE2, \ |
|
133 |
"CS.Enter: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ |
|
134 |
GetCurrentThreadId(), &(cs), __FILE__, __LINE__); \ |
|
135 |
} |
|
136 |
||
137 |
#define CRITICAL_SECTION_LEAVE(cs) { \ |
|
138 |
J2dTraceLn4(J2D_TRACE_VERBOSE2, \ |
|
139 |
"CS.Leave: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ |
|
140 |
GetCurrentThreadId(), &(cs), __FILE__, __LINE__); \ |
|
141 |
(cs).Leave(); \ |
|
142 |
J2dTraceLn4(J2D_TRACE_VERBOSE2, \ |
|
143 |
"CS.Left: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ |
|
144 |
GetCurrentThreadId(), &(cs), __FILE__, __LINE__); \ |
|
145 |
} |
|
146 |
||
147 |
/************************************************************************ |
|
148 |
* AwtToolkit class |
|
149 |
*/ |
|
150 |
||
151 |
class AwtToolkit { |
|
152 |
public: |
|
153 |
enum { |
|
154 |
KB_STATE_SIZE = 256 |
|
155 |
}; |
|
156 |
||
157 |
/* java.awt.Toolkit method ids */ |
|
158 |
static jmethodID getDefaultToolkitMID; |
|
159 |
static jmethodID getFontMetricsMID; |
|
160 |
static jmethodID insetsMID; |
|
161 |
||
162 |
/* sun.awt.windows.WToolkit ids */ |
|
163 |
static jmethodID windowsSettingChangeMID; |
|
164 |
static jmethodID displayChangeMID; |
|
165 |
||
166 |
BOOL m_isDynamicLayoutSet; |
|
167 |
||
168 |
AwtToolkit(); |
|
169 |
~AwtToolkit(); |
|
170 |
||
171 |
BOOL Initialize(BOOL localPump); |
|
172 |
BOOL Dispose(); |
|
173 |
||
174 |
void SetDynamicLayout(BOOL dynamic); |
|
175 |
BOOL IsDynamicLayoutSet(); |
|
176 |
BOOL IsDynamicLayoutSupported(); |
|
177 |
BOOL IsDynamicLayoutActive(); |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
1954
diff
changeset
|
178 |
BOOL areExtraMouseButtonsEnabled(); |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
1954
diff
changeset
|
179 |
void setExtraMouseButtonsEnabled(BOOL enable); |
2 | 180 |
|
181 |
INLINE BOOL localPump() { return m_localPump; } |
|
182 |
INLINE BOOL VerifyComponents() { return FALSE; } // TODO: Use new DebugHelper class to set this flag |
|
183 |
INLINE HWND GetHWnd() { return m_toolkitHWnd; } |
|
184 |
||
185 |
INLINE HMODULE GetModuleHandle() { return m_dllHandle; } |
|
186 |
INLINE void SetModuleHandle(HMODULE h) { m_dllHandle = h; } |
|
187 |
||
188 |
INLINE static DWORD MainThread() { return GetInstance().m_mainThreadId; } |
|
189 |
INLINE void VerifyActive() throw (awt_toolkit_shutdown) { |
|
190 |
if (!m_isActive && m_mainThreadId != ::GetCurrentThreadId()) { |
|
191 |
throw awt_toolkit_shutdown(); |
|
192 |
} |
|
193 |
} |
|
194 |
INLINE BOOL IsDisposed() { return m_isDisposed; } |
|
195 |
static UINT GetMouseKeyState(); |
|
196 |
static void GetKeyboardState(PBYTE keyboardState); |
|
197 |
||
198 |
static ATOM RegisterClass(); |
|
199 |
static void UnregisterClass(); |
|
200 |
INLINE LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0) { |
|
201 |
if (!m_isDisposed) { |
|
202 |
return ::SendMessage(GetHWnd(), msg, wParam, lParam); |
|
203 |
} else { |
|
204 |
return NULL; |
|
205 |
} |
|
206 |
} |
|
207 |
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, |
|
208 |
LPARAM lParam); |
|
209 |
static LRESULT CALLBACK GetMessageFilter(int code, WPARAM wParam, |
|
210 |
LPARAM lParam); |
|
211 |
static LRESULT CALLBACK ForegroundIdleFilter(int code, WPARAM wParam, |
|
212 |
LPARAM lParam); |
|
213 |
||
214 |
INLINE static AwtToolkit& GetInstance() { return theInstance; } |
|
215 |
INLINE void SetPeer(JNIEnv *env, jobject wToolkit) { |
|
216 |
AwtToolkit &tk = AwtToolkit::GetInstance(); |
|
217 |
if (tk.m_peer != NULL) { |
|
218 |
env->DeleteGlobalRef(tk.m_peer); |
|
219 |
} |
|
220 |
tk.m_peer = (wToolkit != NULL) ? env->NewGlobalRef(wToolkit) : NULL; |
|
221 |
} |
|
222 |
||
223 |
INLINE jobject GetPeer() { |
|
224 |
return m_peer; |
|
225 |
} |
|
226 |
||
227 |
// is this thread the main thread? |
|
228 |
||
229 |
INLINE static BOOL IsMainThread() { |
|
230 |
return GetInstance().m_mainThreadId == ::GetCurrentThreadId(); |
|
231 |
} |
|
232 |
||
233 |
// post a message to the message pump thread |
|
234 |
||
235 |
INLINE BOOL PostMessage(UINT msg, WPARAM wp=0, LPARAM lp=0) { |
|
236 |
return ::PostMessage(GetHWnd(), msg, wp, lp); |
|
237 |
} |
|
238 |
||
239 |
// cause the message pump thread to call the function synchronously now! |
|
240 |
||
241 |
INLINE void * InvokeFunction(void*(*ftn)(void)) { |
|
242 |
return (void *)SendMessage(WM_AWT_INVOKE_VOID_METHOD, (WPARAM)ftn, 0); |
|
243 |
} |
|
244 |
INLINE void InvokeFunction(void (*ftn)(void)) { |
|
245 |
InvokeFunction((void*(*)(void))ftn); |
|
246 |
} |
|
247 |
INLINE void * InvokeFunction(void*(*ftn)(void *), void* param) { |
|
248 |
return (void *)SendMessage(WM_AWT_INVOKE_METHOD, (WPARAM)ftn, |
|
249 |
(LPARAM)param); |
|
250 |
} |
|
251 |
INLINE void InvokeFunction(void (*ftn)(void *), void* param) { |
|
252 |
InvokeFunction((void*(*)(void*))ftn, param); |
|
253 |
} |
|
254 |
||
255 |
INLINE CriticalSection &GetSyncCS() { return m_Sync; } |
|
256 |
||
257 |
void *SyncCall(void*(*ftn)(void *), void* param); |
|
258 |
void SyncCall(void (*ftn)(void *), void *param); |
|
259 |
void *SyncCall(void *(*ftn)(void)); |
|
260 |
void SyncCall(void (*ftn)(void)); |
|
261 |
||
262 |
// cause the message pump thread to call the function later ... |
|
263 |
||
264 |
INLINE void InvokeFunctionLater(void (*ftn)(void *), void* param) { |
|
265 |
if (!PostMessage(WM_AWT_INVOKE_METHOD, (WPARAM)ftn, (LPARAM)param)) { |
|
266 |
JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
267 |
JNU_ThrowInternalError(env, "Message not posted, native event queue may be full."); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
// cause the message pump thread to synchronously synchronize on the handle |
|
272 |
||
273 |
INLINE void WaitForSingleObject(HANDLE handle) { |
|
274 |
SendMessage(WM_AWT_WAIT_FOR_SINGLE_OBJECT, 0, (LPARAM)handle); |
|
275 |
} |
|
276 |
||
277 |
/* |
|
278 |
* Create an AwtXxxx C++ component using a given factory |
|
279 |
*/ |
|
280 |
typedef void (*ComponentFactory)(void*, void*); |
|
281 |
static void CreateComponent(void* hComponent, void* hParent, |
|
282 |
ComponentFactory compFactory, BOOL isParentALocalReference=TRUE); |
|
283 |
||
284 |
static void DestroyComponentHWND(HWND hwnd); |
|
285 |
||
286 |
// constants used to PostQuitMessage |
|
287 |
||
288 |
static const int EXIT_ENCLOSING_LOOP; |
|
289 |
static const int EXIT_ALL_ENCLOSING_LOOPS; |
|
290 |
||
291 |
// ... |
|
292 |
||
293 |
void QuitMessageLoop(int status); |
|
294 |
||
295 |
UINT MessageLoop(IDLEPROC lpIdleFunc, PEEKMESSAGEPROC lpPeekMessageFunc); |
|
296 |
BOOL PumpWaitingMessages(PEEKMESSAGEPROC lpPeekMessageFunc); |
|
297 |
BOOL PreProcessMsg(MSG& msg); |
|
298 |
BOOL PreProcessMouseMsg(class AwtComponent* p, MSG& msg); |
|
299 |
BOOL PreProcessKeyMsg(class AwtComponent* p, MSG& msg); |
|
300 |
||
301 |
/* Create an ID which maps to an AwtObject pointer, such as a menu. */ |
|
302 |
UINT CreateCmdID(AwtObject* object); |
|
303 |
||
304 |
// removes cmd id mapping |
|
305 |
void RemoveCmdID(UINT id); |
|
306 |
||
307 |
/* Return the AwtObject associated with its ID. */ |
|
308 |
AwtObject* LookupCmdID(UINT id); |
|
309 |
||
310 |
/* Return the current application icon. */ |
|
311 |
HICON GetAwtIcon(); |
|
312 |
HICON GetAwtIconSm(); |
|
313 |
||
314 |
/* Turns on/off dialog modality for the system. */ |
|
315 |
INLINE AwtDialog* SetModal(AwtDialog* frame) { |
|
316 |
AwtDialog* previousDialog = m_pModalDialog; |
|
317 |
m_pModalDialog = frame; |
|
318 |
return previousDialog; |
|
319 |
}; |
|
320 |
INLINE void ResetModal(AwtDialog* oldFrame) { m_pModalDialog = oldFrame; }; |
|
321 |
INLINE BOOL IsModal() { return (m_pModalDialog != NULL); }; |
|
322 |
INLINE AwtDialog* GetModalDialog(void) { return m_pModalDialog; }; |
|
323 |
||
324 |
/* Stops the current message pump (normally a modal dialog pump) */ |
|
325 |
INLINE void StopMessagePump() { m_breakOnError = TRUE; } |
|
326 |
||
327 |
/* Debug settings */ |
|
328 |
INLINE void SetVerbose(long flag) { m_verbose = (flag != 0); } |
|
329 |
INLINE void SetVerify(long flag) { m_verifyComponents = (flag != 0); } |
|
330 |
INLINE void SetBreak(long flag) { m_breakOnError = (flag != 0); } |
|
331 |
INLINE void SetHeapCheck(long flag); |
|
332 |
||
333 |
static void SetBusy(BOOL busy); |
|
334 |
||
335 |
/* Set and get the default input method Window handler. */ |
|
336 |
INLINE void SetInputMethodWindow(HWND inputMethodHWnd) { m_inputMethodHWnd = inputMethodHWnd; } |
|
337 |
INLINE HWND GetInputMethodWindow() { return m_inputMethodHWnd; } |
|
338 |
||
339 |
static VOID CALLBACK PrimaryIdleFunc(); |
|
340 |
static VOID CALLBACK SecondaryIdleFunc(); |
|
341 |
static BOOL CALLBACK CommonPeekMessageFunc(MSG& msg); |
|
342 |
static BOOL activateKeyboardLayout(HKL hkl); |
|
343 |
||
344 |
HANDLE m_waitEvent; |
|
345 |
DWORD eventNumber; |
|
346 |
private: |
|
347 |
HWND CreateToolkitWnd(LPCTSTR name); |
|
348 |
||
349 |
BOOL m_localPump; |
|
350 |
DWORD m_mainThreadId; |
|
351 |
HWND m_toolkitHWnd; |
|
352 |
HWND m_inputMethodHWnd; |
|
353 |
BOOL m_verbose; |
|
354 |
BOOL m_isActive; // set to FALSE at beginning of Dispose |
|
355 |
BOOL m_isDisposed; // set to TRUE at end of Dispose |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
1954
diff
changeset
|
356 |
BOOL m_areExtraMouseButtonsEnabled; |
2 | 357 |
|
358 |
BOOL m_vmSignalled; // set to TRUE if QUERYENDSESSION has successfully |
|
359 |
// raised SIGTERM |
|
360 |
||
361 |
BOOL m_verifyComponents; |
|
362 |
BOOL m_breakOnError; |
|
363 |
||
364 |
BOOL m_breakMessageLoop; |
|
365 |
UINT m_messageLoopResult; |
|
366 |
||
367 |
class AwtComponent* m_lastMouseOver; |
|
368 |
BOOL m_mouseDown; |
|
369 |
||
370 |
HHOOK m_hGetMessageHook; |
|
371 |
UINT_PTR m_timer; |
|
372 |
||
373 |
class AwtCmdIDList* m_cmdIDs; |
|
374 |
BYTE m_lastKeyboardState[KB_STATE_SIZE]; |
|
375 |
CriticalSection m_lockKB; |
|
376 |
||
377 |
static AwtToolkit theInstance; |
|
378 |
||
379 |
/* The current modal dialog frame (normally NULL). */ |
|
380 |
AwtDialog* m_pModalDialog; |
|
381 |
||
382 |
/* The WToolkit peer instance */ |
|
383 |
jobject m_peer; |
|
384 |
||
385 |
HMODULE m_dllHandle; /* The module handle. */ |
|
386 |
||
387 |
CriticalSection m_Sync; |
|
388 |
||
389 |
/* track display changes - used by palette-updating code. |
|
390 |
This is a workaround for a windows bug that prevents |
|
391 |
WM_PALETTECHANGED event from occurring immediately after |
|
392 |
a WM_DISPLAYCHANGED event. |
|
393 |
*/ |
|
394 |
private: |
|
395 |
BOOL m_displayChanged; /* Tracks displayChanged events */ |
|
105
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
396 |
// 0 means we are not embedded. |
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
397 |
DWORD m_embedderProcessID; |
2 | 398 |
|
399 |
public: |
|
400 |
BOOL HasDisplayChanged() { return m_displayChanged; } |
|
401 |
void ResetDisplayChanged() { m_displayChanged = FALSE; } |
|
105
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
402 |
void RegisterEmbedderProcessId(HWND); |
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
403 |
BOOL IsEmbedderProcessId(const DWORD processID) const |
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
404 |
{ |
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
405 |
return m_embedderProcessID && (processID == m_embedderProcessID); |
e08ac2105adc
6595651: Focus transfers broken for applications embedding AWT across processes
son
parents:
2
diff
changeset
|
406 |
} |
2 | 407 |
|
408 |
private: |
|
409 |
static JNIEnv *m_env; |
|
410 |
static HANDLE m_thread; |
|
411 |
public: |
|
412 |
static void SetEnv(JNIEnv *env); |
|
413 |
static JNIEnv* GetEnv(); |
|
414 |
}; |
|
415 |
||
416 |
/* |
|
417 |
* Class to encapsulate the extraction of the java string contents |
|
418 |
* into a buffer and the cleanup of the buffer |
|
419 |
*/ |
|
420 |
class JavaStringBuffer { |
|
421 |
public: |
|
422 |
JavaStringBuffer(JNIEnv *env, jstring jstr); |
|
423 |
INLINE ~JavaStringBuffer() { delete[] buffer; } |
|
424 |
INLINE operator LPTSTR() { return buffer; } |
|
425 |
INLINE operator LPARAM() { return (LPARAM)buffer; } /* for SendMessage */ |
|
426 |
||
427 |
private: |
|
428 |
LPTSTR buffer; |
|
429 |
}; |
|
430 |
||
431 |
/* creates an instance of T and assigns it to the argument, but only if |
|
432 |
the argument is initially NULL. Supposed to be thread-safe. |
|
433 |
returns the new value of the argument. I'm not using volatile here |
|
434 |
as InterlockedCompareExchange ensures volatile semantics |
|
435 |
and acquire/release. |
|
436 |
The function is useful when used with static POD NULL-initialized |
|
437 |
pointers, as they are guaranteed to be NULL before any dynamic |
|
438 |
initialization takes place. This function turns such a pointer |
|
439 |
into a thread-safe singleton, working regardless of dynamic |
|
440 |
initialization order. Destruction problem is not solved, |
|
441 |
we don't need it here. |
|
442 |
*/ |
|
443 |
||
444 |
template<typename T> inline T* SafeCreate(T* &pArg) { |
|
445 |
/* this implementation has no locks, it just destroys the object if it |
|
446 |
fails to be the first to init. another way would be using a special |
|
447 |
flag pointer value to mark the pointer as "being initialized". */ |
|
448 |
T* pTemp = (T*)InterlockedCompareExchangePointer((void**)&pArg, NULL, NULL); |
|
449 |
if (pTemp != NULL) return pTemp; |
|
450 |
T* pNew = new T; |
|
451 |
pTemp = (T*)InterlockedCompareExchangePointer((void**)&pArg, pNew, NULL); |
|
452 |
if (pTemp != NULL) { |
|
453 |
// we failed it - another thread has already initialized pArg |
|
454 |
delete pNew; |
|
455 |
return pTemp; |
|
456 |
} else { |
|
457 |
return pNew; |
|
458 |
} |
|
459 |
} |
|
460 |
||
461 |
#endif /* AWT_TOOLKIT_H */ |