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