1 /* 2 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * 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 // Struct for _WInputMethod_enable|disableNativeIME method 65 struct EnableNativeIMEStruct { 66 jobject self; 67 jobject peer; 68 jint context; 69 jboolean useNativeCompWindow; 70 }; 71 72 /* 73 * class JNILocalFrame 74 * Push/PopLocalFrame helper 75 */ 76 class JNILocalFrame { 77 public: 78 INLINE JNILocalFrame(JNIEnv *env, int size) { 79 m_env = env; 80 int result = m_env->PushLocalFrame(size); 81 if (result < 0) { 82 DASSERT(FALSE); 83 throw std::bad_alloc(); 84 } 85 } 86 INLINE ~JNILocalFrame() { m_env->PopLocalFrame(NULL); } 87 private: 88 JNIEnv* m_env; 89 }; 90 91 /* 92 * class CriticalSection 93 * ~~~~~ ~~~~~~~~~~~~~~~~ 94 * Lightweight intra-process thread synchronization. Can only be used with 95 * other critical sections, and only within the same process. 96 */ 97 class CriticalSection { 98 public: 99 INLINE CriticalSection() { ::InitializeCriticalSection(&rep); } 100 INLINE ~CriticalSection() { ::DeleteCriticalSection(&rep); } 101 102 class Lock { 103 public: 104 INLINE Lock(const CriticalSection& cs) : critSec(cs) { 105 (const_cast<CriticalSection &>(critSec)).Enter(); 106 } 107 INLINE ~Lock() { 108 (const_cast<CriticalSection &>(critSec)).Leave(); 109 } 110 private: 111 const CriticalSection& critSec; 112 }; 113 friend class Lock; 114 115 private: 116 CRITICAL_SECTION rep; 117 118 CriticalSection(const CriticalSection&); 119 const CriticalSection& operator =(const CriticalSection&); 120 121 public: 122 virtual void Enter() { 123 ::EnterCriticalSection(&rep); 124 } 125 virtual BOOL TryEnter() { 126 return ::TryEnterCriticalSection(&rep); 127 } 128 virtual void Leave() { 129 ::LeaveCriticalSection(&rep); 130 } 131 }; 132 133 // Macros for using CriticalSection objects that help trace 134 // lock/unlock actions 135 136 /* Use THIS_FILE when it is available. */ 137 #ifndef THIS_FILE 138 #define THIS_FILE __FILE__ 139 #endif 140 141 #define CRITICAL_SECTION_ENTER(cs) { \ 142 J2dTraceLn4(J2D_TRACE_VERBOSE2, \ 143 "CS.Wait: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ 144 GetCurrentThreadId(), &(cs), THIS_FILE, __LINE__); \ 145 (cs).Enter(); \ 146 J2dTraceLn4(J2D_TRACE_VERBOSE2, \ 147 "CS.Enter: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ 148 GetCurrentThreadId(), &(cs), THIS_FILE, __LINE__); \ 149 } 150 151 #define CRITICAL_SECTION_LEAVE(cs) { \ 152 J2dTraceLn4(J2D_TRACE_VERBOSE2, \ 153 "CS.Leave: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ 154 GetCurrentThreadId(), &(cs), THIS_FILE, __LINE__); \ 155 (cs).Leave(); \ 156 J2dTraceLn4(J2D_TRACE_VERBOSE2, \ 157 "CS.Left: tid, cs, file, line = 0x%x, 0x%x, %s, %d", \ 158 GetCurrentThreadId(), &(cs), THIS_FILE, __LINE__); \ 159 } 160 161 /************************************************************************ 162 * AwtToolkit class 163 */ 164 165 class AwtToolkit { 166 public: 167 enum { 168 KB_STATE_SIZE = 256 169 }; 170 171 /* java.awt.Toolkit method ids */ 172 static jmethodID getDefaultToolkitMID; 173 static jmethodID getFontMetricsMID; 174 static jmethodID insetsMID; 175 176 /* sun.awt.windows.WToolkit ids */ 177 static jmethodID windowsSettingChangeMID; 178 static jmethodID displayChangeMID; 179 180 BOOL m_isDynamicLayoutSet; 181 182 AwtToolkit(); 183 ~AwtToolkit(); 184 185 BOOL Initialize(BOOL localPump); 186 BOOL Dispose(); 187 188 void SetDynamicLayout(BOOL dynamic); 189 BOOL IsDynamicLayoutSet(); 190 BOOL IsDynamicLayoutSupported(); 191 BOOL IsDynamicLayoutActive(); 192 BOOL areExtraMouseButtonsEnabled(); 193 void setExtraMouseButtonsEnabled(BOOL enable); 194 static UINT GetNumberOfButtons(); 195 196 INLINE BOOL localPump() { return m_localPump; } 197 INLINE BOOL VerifyComponents() { return FALSE; } // TODO: Use new DebugHelper class to set this flag 198 INLINE HWND GetHWnd() { return m_toolkitHWnd; } 199 200 INLINE HMODULE GetModuleHandle() { return m_dllHandle; } 201 INLINE void SetModuleHandle(HMODULE h) { m_dllHandle = h; } 202 203 INLINE static DWORD MainThread() { return GetInstance().m_mainThreadId; } 204 INLINE void VerifyActive() throw (awt_toolkit_shutdown) { 205 if (!m_isActive && m_mainThreadId != ::GetCurrentThreadId()) { 206 throw awt_toolkit_shutdown(); 207 } 208 } 209 INLINE BOOL IsDisposed() { return m_isDisposed; } 210 static UINT GetMouseKeyState(); 211 static void GetKeyboardState(PBYTE keyboardState); 212 213 static ATOM RegisterClass(); 214 static void UnregisterClass(); 215 INLINE LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0) { 216 if (!m_isDisposed) { 217 return ::SendMessage(GetHWnd(), msg, wParam, lParam); 218 } else { 219 return NULL; 220 } 221 } 222 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, 223 LPARAM lParam); 224 static LRESULT CALLBACK GetMessageFilter(int code, WPARAM wParam, 225 LPARAM lParam); 226 static LRESULT CALLBACK ForegroundIdleFilter(int code, WPARAM wParam, 227 LPARAM lParam); 228 static LRESULT CALLBACK MouseLowLevelHook(int code, WPARAM wParam, 229 LPARAM lParam); 230 231 INLINE static AwtToolkit& GetInstance() { return theInstance; } 232 INLINE void SetPeer(JNIEnv *env, jobject wToolkit) { 233 AwtToolkit &tk = AwtToolkit::GetInstance(); 234 if (tk.m_peer != NULL) { 235 env->DeleteGlobalRef(tk.m_peer); 236 } 237 tk.m_peer = (wToolkit != NULL) ? env->NewGlobalRef(wToolkit) : NULL; 238 } 239 240 INLINE jobject GetPeer() { 241 return m_peer; 242 } 243 244 // is this thread the main thread? 245 246 INLINE static BOOL IsMainThread() { 247 return GetInstance().m_mainThreadId == ::GetCurrentThreadId(); 248 } 249 250 // post a message to the message pump thread 251 252 INLINE BOOL PostMessage(UINT msg, WPARAM wp=0, LPARAM lp=0) { 253 return ::PostMessage(GetHWnd(), msg, wp, lp); 254 } 255 256 // cause the message pump thread to call the function synchronously now! 257 258 INLINE void * InvokeFunction(void*(*ftn)(void)) { 259 return (void *)SendMessage(WM_AWT_INVOKE_VOID_METHOD, (WPARAM)ftn, 0); 260 } 261 INLINE void InvokeFunction(void (*ftn)(void)) { 262 InvokeFunction((void*(*)(void))ftn); 263 } 264 INLINE void * InvokeFunction(void*(*ftn)(void *), void* param) { 265 return (void *)SendMessage(WM_AWT_INVOKE_METHOD, (WPARAM)ftn, 266 (LPARAM)param); 267 } 268 INLINE void InvokeFunction(void (*ftn)(void *), void* param) { 269 InvokeFunction((void*(*)(void*))ftn, param); 270 } 271 272 INLINE CriticalSection &GetSyncCS() { return m_Sync; } 273 274 void *SyncCall(void*(*ftn)(void *), void* param); 275 void SyncCall(void (*ftn)(void *), void *param); 276 void *SyncCall(void *(*ftn)(void)); 277 void SyncCall(void (*ftn)(void)); 278 279 // cause the message pump thread to call the function later ... 280 281 INLINE void InvokeFunctionLater(void (*ftn)(void *), void* param) { 282 if (!PostMessage(WM_AWT_INVOKE_METHOD, (WPARAM)ftn, (LPARAM)param)) { 283 JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); 284 JNU_ThrowInternalError(env, "Message not posted, native event queue may be full."); 285 } 286 } 287 288 // cause the message pump thread to synchronously synchronize on the handle 289 290 INLINE void WaitForSingleObject(HANDLE handle) { 291 SendMessage(WM_AWT_WAIT_FOR_SINGLE_OBJECT, 0, (LPARAM)handle); 292 } 293 294 /* 295 * Create an AwtXxxx C++ component using a given factory 296 */ 297 typedef void (*ComponentFactory)(void*, void*); 298 static void CreateComponent(void* hComponent, void* hParent, 299 ComponentFactory compFactory, BOOL isParentALocalReference=TRUE); 300 301 static void DestroyComponentHWND(HWND hwnd); 302 303 // constants used to PostQuitMessage 304 305 static const int EXIT_ENCLOSING_LOOP; 306 static const int EXIT_ALL_ENCLOSING_LOOPS; 307 308 // ... 309 310 void QuitMessageLoop(int status); 311 312 UINT MessageLoop(IDLEPROC lpIdleFunc, PEEKMESSAGEPROC lpPeekMessageFunc); 313 BOOL PumpWaitingMessages(PEEKMESSAGEPROC lpPeekMessageFunc); 314 void PumpToDestroy(class AwtComponent* p); 315 void ProcessMsg(MSG& msg); 316 BOOL PreProcessMsg(MSG& msg); 317 BOOL PreProcessMouseMsg(class AwtComponent* p, MSG& msg); 318 BOOL PreProcessKeyMsg(class AwtComponent* p, MSG& msg); 319 320 /* Create an ID which maps to an AwtObject pointer, such as a menu. */ 321 UINT CreateCmdID(AwtObject* object); 322 323 // removes cmd id mapping 324 void RemoveCmdID(UINT id); 325 326 /* Return the AwtObject associated with its ID. */ 327 AwtObject* LookupCmdID(UINT id); 328 329 /* Return the current application icon. */ 330 HICON GetAwtIcon(); 331 HICON GetAwtIconSm(); 332 333 // Calculate a wave-like value out of the integer 'value' and 334 // the specified period. 335 // The argument 'value' is an integer 0, 1, 2, ... *infinity*. 336 // 337 // Examples: 338 // Period == 3 339 // Generated sequence: 0 1 2 1 0 ..... 340 // 341 // Period == 4 342 // Generated sequence: 0 1 2 3 2 1 0 ..... 343 static inline UINT CalculateWave(UINT value, const UINT period) { 344 if (period < 2) { 345 return 0; 346 } 347 // -2 is necessary to avoid repeating extreme values (0 and period-1) 348 value %= period * 2 -2; 349 if (value >= period) { 350 value = period * 2 -2 - value; 351 } 352 return value; 353 } 354 355 HICON GetSecurityWarningIcon(UINT index, UINT w, UINT h); 356 357 /* Turns on/off dialog modality for the system. */ 358 INLINE AwtDialog* SetModal(AwtDialog* frame) { 359 AwtDialog* previousDialog = m_pModalDialog; 360 m_pModalDialog = frame; 361 return previousDialog; 362 }; 363 INLINE void ResetModal(AwtDialog* oldFrame) { m_pModalDialog = oldFrame; }; 364 INLINE BOOL IsModal() { return (m_pModalDialog != NULL); }; 365 INLINE AwtDialog* GetModalDialog(void) { return m_pModalDialog; }; 366 367 /* Stops the current message pump (normally a modal dialog pump) */ 368 INLINE void StopMessagePump() { m_breakOnError = TRUE; } 369 370 /* Debug settings */ 371 INLINE void SetVerbose(long flag) { m_verbose = (flag != 0); } 372 INLINE void SetVerify(long flag) { m_verifyComponents = (flag != 0); } 373 INLINE void SetBreak(long flag) { m_breakOnError = (flag != 0); } 374 INLINE void SetHeapCheck(long flag); 375 376 static void SetBusy(BOOL busy); 377 378 /* Set and get the default input method Window handler. */ 379 INLINE void SetInputMethodWindow(HWND inputMethodHWnd) { m_inputMethodHWnd = inputMethodHWnd; } 380 INLINE HWND GetInputMethodWindow() { return m_inputMethodHWnd; } 381 382 static VOID CALLBACK PrimaryIdleFunc(); 383 static VOID CALLBACK SecondaryIdleFunc(); 384 static BOOL CALLBACK CommonPeekMessageFunc(MSG& msg); 385 static BOOL activateKeyboardLayout(HKL hkl); 386 387 HANDLE m_waitEvent; 388 volatile DWORD eventNumber; 389 volatile BOOL isInDoDragDropLoop; 390 private: 391 HWND CreateToolkitWnd(LPCTSTR name); 392 393 BOOL m_localPump; 394 DWORD m_mainThreadId; 395 HWND m_toolkitHWnd; 396 HWND m_inputMethodHWnd; 397 BOOL m_verbose; 398 BOOL m_isActive; // set to FALSE at beginning of Dispose 399 BOOL m_isDisposed; // set to TRUE at end of Dispose 400 BOOL m_areExtraMouseButtonsEnabled; 401 402 BOOL m_vmSignalled; // set to TRUE if QUERYENDSESSION has successfully 403 // raised SIGTERM 404 405 BOOL m_verifyComponents; 406 BOOL m_breakOnError; 407 408 BOOL m_breakMessageLoop; 409 UINT m_messageLoopResult; 410 411 class AwtComponent* m_lastMouseOver; 412 BOOL m_mouseDown; 413 414 HHOOK m_hGetMessageHook; 415 HHOOK m_hMouseLLHook; 416 UINT_PTR m_timer; 417 418 class AwtCmdIDList* m_cmdIDs; 419 BYTE m_lastKeyboardState[KB_STATE_SIZE]; 420 CriticalSection m_lockKB; 421 422 static AwtToolkit theInstance; 423 424 /* The current modal dialog frame (normally NULL). */ 425 AwtDialog* m_pModalDialog; 426 427 /* The WToolkit peer instance */ 428 jobject m_peer; 429 430 HMODULE m_dllHandle; /* The module handle. */ 431 432 CriticalSection m_Sync; 433 434 /* track display changes - used by palette-updating code. 435 This is a workaround for a windows bug that prevents 436 WM_PALETTECHANGED event from occurring immediately after 437 a WM_DISPLAYCHANGED event. 438 */ 439 private: 440 BOOL m_displayChanged; /* Tracks displayChanged events */ 441 // 0 means we are not embedded. 442 DWORD m_embedderProcessID; 443 444 public: 445 BOOL HasDisplayChanged() { return m_displayChanged; } 446 void ResetDisplayChanged() { m_displayChanged = FALSE; } 447 void RegisterEmbedderProcessId(HWND); 448 BOOL IsEmbedderProcessId(const DWORD processID) const 449 { 450 return m_embedderProcessID && (processID == m_embedderProcessID); 451 } 452 453 private: 454 static JNIEnv *m_env; 455 static DWORD m_threadId; 456 public: 457 static void SetEnv(JNIEnv *env); 458 static JNIEnv* GetEnv(); 459 460 static BOOL GetScreenInsets(int screenNum, RECT * rect); 461 462 // If the DWM is active, this function uses 463 // DwmGetWindowAttribute()/DWMWA_EXTENDED_FRAME_BOUNDS. 464 // Otherwise, fall back to regular ::GetWindowRect(). 465 // See 6711576 for more details. 466 static void GetWindowRect(HWND hWnd, LPRECT lpRect); 467 468 private: 469 // The window handle of a toplevel window last seen under the mouse cursor. 470 // See MouseLowLevelHook() for details. 471 HWND m_lastWindowUnderMouse; 472 public: 473 HWND GetWindowUnderMouse() { return m_lastWindowUnderMouse; } 474 475 void InstallMouseLowLevelHook(); 476 void UninstallMouseLowLevelHook(); 477 478 479 /* AWT preloading (early Toolkit thread start) 480 */ 481 public: 482 /* Toolkit preload action class. 483 * Preload actions should be registered with 484 * AwtToolkit::getInstance().GetPreloadThread().AddAction(). 485 * AwtToolkit thread calls InitImpl method at the beghining 486 * and CleanImpl(false) before exiting for all registered actions. 487 * If an application provides own Toolkit thread 488 * (sun.awt.windows.WToolkit.embeddedInit), the thread calls Clean(true) 489 * for each action. 490 */ 491 class PreloadThread; // forward declaration 492 class PreloadAction { 493 friend class PreloadThread; 494 public: 495 PreloadAction() : initThreadId(0), pNext(NULL) {} 496 virtual ~PreloadAction() {} 497 498 protected: 499 // called by PreloadThread or as result 500 // of EnsureInited() call (on Toolkit thread!). 501 virtual void InitImpl() = 0; 502 503 // called by PreloadThread (before exiting). 504 // reInit == false: normal shutdown; 505 // reInit == true: PreloadThread is shutting down due external 506 // Toolkit thread was provided. 507 virtual void CleanImpl(bool reInit) = 0; 508 509 public: 510 // Initialized the action on the Toolkit thread if not yet initialized. 511 bool EnsureInited(); 512 513 // returns thread ID which the action was inited on (0 if not inited) 514 DWORD GetInitThreadID(); 515 516 // Allows to deinitialize action earlier. 517 // The method must be called on the Toolkit thread only. 518 // returns true on success, 519 // false if the action was inited on other thread. 520 bool Clean(); 521 522 private: 523 unsigned initThreadId; 524 // lock for Init/Clean 525 CriticalSection initLock; 526 527 // Chain support (for PreloadThread) 528 PreloadAction *pNext; // for action chain used by PreloadThread 529 void SetNext(PreloadAction *pNext) { this->pNext = pNext; } 530 PreloadAction *GetNext() { return pNext; } 531 532 // wrapper for AwtToolkit::InvokeFunction 533 static void InitWrapper(void *param); 534 535 void Init(); 536 void Clean(bool reInit); 537 538 }; 539 540 /** Toolkit preload thread class. 541 */ 542 class PreloadThread { 543 public: 544 PreloadThread(); 545 ~PreloadThread(); 546 547 // adds action & start the thread if not yet started 548 bool AddAction(PreloadAction *pAction); 549 550 // sets termination flag; returns true if the thread is running. 551 // wrongThread specifies cause of the termination: 552 // false means termination on the application shutdown; 553 // wrongThread is used as reInit parameter for action cleanup. 554 bool Terminate(bool wrongThread); 555 bool InvokeAndTerminate(void(_cdecl *fn)(void *), void *param); 556 557 // waits for the thread completion; 558 // use the method after Terminate() only if Terminate() returned true 559 INLINE void Wait4Finish() { 560 ::WaitForSingleObject(hFinished, INFINITE); 561 } 562 563 INLINE unsigned GetThreadId() { 564 CriticalSection::Lock lock(threadLock); 565 return threadId; 566 } 567 INLINE bool IsWrongThread() { 568 CriticalSection::Lock lock(threadLock); 569 return wrongThread; 570 } 571 // returns true if the current thread is "preload" thread 572 bool OnPreloadThread(); 573 574 private: 575 // data access lock 576 CriticalSection threadLock; 577 578 // the thread status 579 enum Status { 580 None = -1, // initial 581 Preloading = 0, // preloading in progress 582 RunningToolkit, // Running as Toolkit thread 583 Cleaning, // exited from Toolkit thread proc, cleaning 584 Finished // 585 } status; 586 587 // "wrong thread" flag 588 bool wrongThread; 589 590 // thread proc (calls (this)param->ThreadProc()) 591 static unsigned WINAPI StaticThreadProc(void *param); 592 unsigned ThreadProc(); 593 594 INLINE void AwakeThread() { 595 ::SetEvent(hAwake); 596 } 597 598 // if threadId != 0 -> we are running 599 unsigned threadId; 600 // ThreadProc sets the event on exit 601 HANDLE hFinished; 602 // ThreadProc waits on the event for NewAction/Terminate/InvokeAndTerminate 603 HANDLE hAwake; 604 605 // function/param to invoke (InvokeAndTerminate) 606 // if execFunc == NULL => just terminate 607 void(_cdecl *execFunc)(void *); 608 void *execParam; 609 610 // action chain 611 PreloadAction *pActionChain; 612 PreloadAction *pLastProcessedAction; 613 614 // returns next action in the list (NULL if no more actions) 615 PreloadAction* GetNextAction(); 616 617 }; 618 619 INLINE PreloadThread& GetPreloadThread() { return preloadThread; } 620 621 private: 622 PreloadThread preloadThread; 623 624 }; 625 626 627 /* creates an instance of T and assigns it to the argument, but only if 628 the argument is initially NULL. Supposed to be thread-safe. 629 returns the new value of the argument. I'm not using volatile here 630 as InterlockedCompareExchange ensures volatile semantics 631 and acquire/release. 632 The function is useful when used with static POD NULL-initialized 633 pointers, as they are guaranteed to be NULL before any dynamic 634 initialization takes place. This function turns such a pointer 635 into a thread-safe singleton, working regardless of dynamic 636 initialization order. Destruction problem is not solved, 637 we don't need it here. 638 */ 639 640 template<typename T> inline T* SafeCreate(T* &pArg) { 641 /* this implementation has no locks, it just destroys the object if it 642 fails to be the first to init. another way would be using a special 643 flag pointer value to mark the pointer as "being initialized". */ 644 T* pTemp = (T*)InterlockedCompareExchangePointer((void**)&pArg, NULL, NULL); 645 if (pTemp != NULL) return pTemp; 646 T* pNew = new T; 647 pTemp = (T*)InterlockedCompareExchangePointer((void**)&pArg, pNew, NULL); 648 if (pTemp != NULL) { 649 // we failed it - another thread has already initialized pArg 650 delete pNew; 651 return pTemp; 652 } else { 653 return pNew; 654 } 655 } 656 657 #endif /* AWT_TOOLKIT_H */