4 * This file contains Windows-specific procedures for the notifier,
5 * which is the lowest-level part of the Tcl event loop. This file
6 * works together with ../generic/tclNotify.c.
8 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 #include "tclWinInt.h"
20 * The follwing static indicates whether this module has been initialized.
23 static int initialized = 0;
25 #define INTERVAL_TIMER 1 /* Handle of interval timer. */
27 #define WM_WAKEUP WM_USER /* Message that is send by
28 * Tcl_AlertNotifier. */
30 * The following static structure contains the state information for the
31 * Windows implementation of the Tcl notifier. One of these structures
32 * is created for each thread that is using the notifier.
35 typedef struct ThreadSpecificData {
36 CRITICAL_SECTION crit; /* Monitor for this notifier. */
37 DWORD thread; /* Identifier for thread associated with this
39 HANDLE event; /* Event object used to wake up the notifier
41 int pending; /* Alert message pending, this field is
42 * locked by the notifierMutex. */
43 HWND hwnd; /* Messaging window. */
44 int timeout; /* Current timeout value. */
45 int timerActive; /* 1 if interval timer is running. */
48 static Tcl_ThreadDataKey dataKey;
50 extern TclStubs tclStubs;
52 * The following static indicates the number of threads that have
53 * initialized notifiers. It controls the lifetime of the TclNotifier
56 * You must hold the notifierMutex lock before accessing this variable.
59 static int notifierCount = 0;
60 TCL_DECLARE_MUTEX(notifierMutex)
63 * Static routines defined in this file.
66 static LRESULT CALLBACK NotifierProc(HWND hwnd, UINT message,
67 WPARAM wParam, LPARAM lParam);
71 *----------------------------------------------------------------------
75 * Initializes the platform specific notifier state.
78 * Returns a handle to the notifier state for this thread..
83 *----------------------------------------------------------------------
89 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
93 * Register Notifier window class if this is the first thread to
97 Tcl_MutexLock(¬ifierMutex);
98 if (notifierCount == 0) {
100 class.cbClsExtra = 0;
101 class.cbWndExtra = 0;
102 class.hInstance = TclWinGetTclInstance();
103 class.hbrBackground = NULL;
104 class.lpszMenuName = NULL;
105 class.lpszClassName = "TclNotifier";
106 class.lpfnWndProc = NotifierProc;
108 class.hCursor = NULL;
110 if (!RegisterClassA(&class)) {
111 panic("Unable to register TclNotifier window class");
115 Tcl_MutexUnlock(¬ifierMutex);
118 tsdPtr->timerActive = 0;
120 InitializeCriticalSection(&tsdPtr->crit);
123 tsdPtr->thread = GetCurrentThreadId();
124 tsdPtr->event = CreateEvent(NULL, TRUE /* manual */,
125 FALSE /* !signaled */, NULL);
127 return (ClientData) tsdPtr;
131 *----------------------------------------------------------------------
133 * Tcl_FinalizeNotifier --
135 * This function is called to cleanup the notifier state before
136 * a thread is terminated.
142 * May dispose of the notifier window and class.
144 *----------------------------------------------------------------------
148 Tcl_FinalizeNotifier(clientData)
149 ClientData clientData; /* Pointer to notifier data. */
151 ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData;
153 DeleteCriticalSection(&tsdPtr->crit);
154 CloseHandle(tsdPtr->event);
157 * Clean up the timer and messaging window for this thread.
161 KillTimer(tsdPtr->hwnd, INTERVAL_TIMER);
162 DestroyWindow(tsdPtr->hwnd);
166 * If this is the last thread to use the notifier, unregister
167 * the notifier window class.
170 Tcl_MutexLock(¬ifierMutex);
172 if (notifierCount == 0) {
173 UnregisterClassA("TclNotifier", TclWinGetTclInstance());
175 Tcl_MutexUnlock(¬ifierMutex);
179 *----------------------------------------------------------------------
181 * Tcl_AlertNotifier --
183 * Wake up the specified notifier from any thread. This routine
184 * is called by the platform independent notifier code whenever
185 * the Tcl_ThreadAlert routine is called. This routine is
186 * guaranteed not to be called on a given notifier after
187 * Tcl_FinalizeNotifier is called for that notifier. This routine
188 * is typically called from a thread other than the notifier's
195 * Sends a message to the messaging window for the notifier
196 * if there isn't already one pending.
198 *----------------------------------------------------------------------
202 Tcl_AlertNotifier(clientData)
203 ClientData clientData; /* Pointer to thread data. */
205 ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData;
208 * Note that we do not need to lock around access to the hwnd
209 * because the race condition has no effect since any race condition
210 * implies that the notifier thread is already awake.
215 * We do need to lock around access to the pending flag.
218 EnterCriticalSection(&tsdPtr->crit);
219 if (!tsdPtr->pending) {
220 PostMessage(tsdPtr->hwnd, WM_WAKEUP, 0, 0);
223 LeaveCriticalSection(&tsdPtr->crit);
225 SetEvent(tsdPtr->event);
230 *----------------------------------------------------------------------
234 * This procedure sets the current notifier timer value. The
235 * notifier will ensure that Tcl_ServiceAll() is called after
236 * the specified interval, even if no events have occurred.
242 * Replaces any previous timer.
244 *----------------------------------------------------------------------
249 Tcl_Time *timePtr) /* Maximum block time, or NULL. */
251 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
255 * Allow the notifier to be hooked. This may not make sense
256 * on Windows, but mirrors the UNIX hook.
259 if (tclStubs.tcl_SetTimer != Tcl_SetTimer) {
260 tclStubs.tcl_SetTimer(timePtr);
265 * We only need to set up an interval timer if we're being called
266 * from an external event loop. If we don't have a window handle
267 * then we just return immediately and let Tcl_WaitForEvent handle
279 * Make sure we pass a non-zero value into the timeout argument.
280 * Windows seems to get confused by zero length timers.
283 timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
288 tsdPtr->timeout = timeout;
290 tsdPtr->timerActive = 1;
291 SetTimer(tsdPtr->hwnd, INTERVAL_TIMER,
292 (unsigned long) tsdPtr->timeout, NULL);
294 tsdPtr->timerActive = 0;
295 KillTimer(tsdPtr->hwnd, INTERVAL_TIMER);
300 *----------------------------------------------------------------------
302 * Tcl_ServiceModeHook --
304 * This function is invoked whenever the service mode changes.
310 * If this is the first time the notifier is set into
311 * TCL_SERVICE_ALL, then the communication window is created.
313 *----------------------------------------------------------------------
317 Tcl_ServiceModeHook(mode)
318 int mode; /* Either TCL_SERVICE_ALL, or
319 * TCL_SERVICE_NONE. */
321 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
324 * If this is the first time that the notifier has been used from a
325 * modal loop, then create a communication window. Note that after
326 * this point, the application needs to service events in a timely
327 * fashion or Windows will hang waiting for the window to respond
328 * to synchronous system messages. At some point, we may want to
329 * consider destroying the window if we leave the modal loop, but
330 * for now we'll leave it around.
333 if (mode == TCL_SERVICE_ALL && !tsdPtr->hwnd) {
334 tsdPtr->hwnd = CreateWindowA("TclNotifier", "TclNotifier", WS_TILED,
335 0, 0, 0, 0, NULL, NULL, TclWinGetTclInstance(), NULL);
337 * Send an initial message to the window to ensure that we wake up the
338 * notifier once we get into the modal loop. This will force the
339 * notifier to recompute the timeout value and schedule a timer
343 Tcl_AlertNotifier((ClientData)tsdPtr);
348 *----------------------------------------------------------------------
352 * This procedure is invoked by Windows to process events on
353 * the notifier window. Messages will be sent to this window
354 * in response to external timer events or calls to
358 * A standard windows result.
361 * Services any pending events.
363 *----------------------------------------------------------------------
366 static LRESULT CALLBACK
373 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
375 if (message == WM_WAKEUP) {
376 EnterCriticalSection(&tsdPtr->crit);
378 LeaveCriticalSection(&tsdPtr->crit);
379 } else if (message != WM_TIMER) {
380 return DefWindowProc(hwnd, message, wParam, lParam);
384 * Process all of the runnable events.
392 *----------------------------------------------------------------------
394 * Tcl_WaitForEvent --
396 * This function is called by Tcl_DoOneEvent to wait for new
397 * events on the message queue. If the block time is 0, then
398 * Tcl_WaitForEvent just polls the event queue without blocking.
401 * Returns -1 if a WM_QUIT message is detected, returns 1 if
402 * a message was dispatched, otherwise returns 0.
405 * Dispatches a message to a window procedure, which could do
408 *----------------------------------------------------------------------
413 Tcl_Time *timePtr) /* Maximum block time, or NULL. */
415 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
417 DWORD timeout, result;
421 * Allow the notifier to be hooked. This may not make
422 * sense on windows, but mirrors the UNIX hook.
425 if (tclStubs.tcl_WaitForEvent != Tcl_WaitForEvent) {
426 return tclStubs.tcl_WaitForEvent(timePtr);
430 * Compute the timeout in milliseconds.
434 timeout = timePtr->sec * 1000 + timePtr->usec / 1000;
440 * Check to see if there are any messages in the queue before waiting
441 * because MsgWaitForMultipleObjects will not wake up if there are events
442 * currently sitting in the queue.
445 if (!PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
447 * Wait for something to happen (a signal from another thread, a
448 * message, or timeout).
451 result = MsgWaitForMultipleObjects(1, &tsdPtr->event, FALSE, timeout,
456 * Check to see if there are any messages to process.
459 if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
461 * Retrieve and dispatch the first message.
464 result = GetMessage(&msg, NULL, 0, 0);
467 * We received a request to exit this thread (WM_QUIT), so
468 * propagate the quit message and start unwinding.
471 PostQuitMessage(msg.wParam);
473 } else if (result == -1) {
475 * We got an error from the system. I have no idea why this would
476 * happen, so we'll just unwind.
481 TranslateMessage(&msg);
482 DispatchMessage(&msg);
489 ResetEvent(tsdPtr->event);
494 *----------------------------------------------------------------------
498 * Delay execution for the specified number of milliseconds.
506 *----------------------------------------------------------------------
511 int ms; /* Number of milliseconds to sleep. */