| 1 | /* |
| 2 | * tclWinNotify.c -- |
| 3 | * |
| 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. |
| 7 | * |
| 8 | * Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 9 | * |
| 10 | * See the file "license.terms" for information on usage and redistribution |
| 11 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 | * |
| 13 | * RCS: @(#) $Id: tclWinNotify.c,v 1.11.2.1 2003/03/21 03:24:09 dgp Exp $ |
| 14 | */ |
| 15 | |
| 16 | #include <windows.h> |
| 17 | #include "Lang.h" |
| 18 | #include "tkWin.h" |
| 19 | |
| 20 | #if defined(TCL_EVENT_IMPLEMENT) |
| 21 | |
| 22 | /* |
| 23 | * The follwing static indicates whether this module has been initialized. |
| 24 | */ |
| 25 | |
| 26 | #define INTERVAL_TIMER 1 /* Handle of interval timer. */ |
| 27 | |
| 28 | #define WM_WAKEUP WM_USER /* Message that is send by |
| 29 | * Tcl_AlertNotifier. */ |
| 30 | /* |
| 31 | * The following static structure contains the state information for the |
| 32 | * Windows implementation of the Tcl notifier. One of these structures |
| 33 | * is created for each thread that is using the notifier. |
| 34 | */ |
| 35 | |
| 36 | typedef struct ThreadSpecificData { |
| 37 | CRITICAL_SECTION crit; /* Monitor for this notifier. */ |
| 38 | DWORD thread; /* Identifier for thread associated with this |
| 39 | * notifier. */ |
| 40 | HANDLE event; /* Event object used to wake up the notifier |
| 41 | * thread. */ |
| 42 | int pending; /* Alert message pending, this field is |
| 43 | * locked by the notifierMutex. */ |
| 44 | HWND hwnd; /* Messaging window. */ |
| 45 | int timeout; /* Current timeout value. */ |
| 46 | int timerActive; /* 1 if interval timer is running. */ |
| 47 | } ThreadSpecificData; |
| 48 | |
| 49 | static Tcl_ThreadDataKey dataKey; |
| 50 | |
| 51 | extern TclStubs tclStubs; |
| 52 | extern Tcl_NotifierProcs tclOriginalNotifier; |
| 53 | |
| 54 | /* |
| 55 | * The following static indicates the number of threads that have |
| 56 | * initialized notifiers. It controls the lifetime of the TclNotifier |
| 57 | * window class. |
| 58 | * |
| 59 | * You must hold the notifierMutex lock before accessing this variable. |
| 60 | */ |
| 61 | |
| 62 | static int notifierCount = 0; |
| 63 | TCL_DECLARE_MUTEX(notifierMutex) |
| 64 | |
| 65 | /* |
| 66 | * Static routines defined in this file. |
| 67 | */ |
| 68 | |
| 69 | static LRESULT CALLBACK NotifierProc(HWND hwnd, UINT message, |
| 70 | WPARAM wParam, LPARAM lParam); |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | *---------------------------------------------------------------------- |
| 75 | * |
| 76 | * Tcl_InitNotifier -- |
| 77 | * |
| 78 | * Initializes the platform specific notifier state. |
| 79 | * |
| 80 | * Results: |
| 81 | * Returns a handle to the notifier state for this thread.. |
| 82 | * |
| 83 | * Side effects: |
| 84 | * None. |
| 85 | * |
| 86 | *---------------------------------------------------------------------- |
| 87 | */ |
| 88 | |
| 89 | ClientData |
| 90 | Tcl_InitNotifier() |
| 91 | { |
| 92 | ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 93 | WNDCLASS class; |
| 94 | |
| 95 | /* |
| 96 | * Register Notifier window class if this is the first thread to |
| 97 | * use this module. |
| 98 | */ |
| 99 | |
| 100 | Tcl_MutexLock(¬ifierMutex); |
| 101 | if (notifierCount == 0) { |
| 102 | class.style = 0; |
| 103 | class.cbClsExtra = 0; |
| 104 | class.cbWndExtra = 0; |
| 105 | class.hInstance = TclWinGetTclInstance(); |
| 106 | class.hbrBackground = NULL; |
| 107 | class.lpszMenuName = NULL; |
| 108 | class.lpszClassName = "TclNotifier"; |
| 109 | class.lpfnWndProc = NotifierProc; |
| 110 | class.hIcon = NULL; |
| 111 | class.hCursor = NULL; |
| 112 | |
| 113 | if (!RegisterClassA(&class)) { |
| 114 | panic("Unable to register TclNotifier window class"); |
| 115 | } |
| 116 | } |
| 117 | notifierCount++; |
| 118 | Tcl_MutexUnlock(¬ifierMutex); |
| 119 | |
| 120 | tsdPtr->pending = 0; |
| 121 | tsdPtr->timerActive = 0; |
| 122 | |
| 123 | InitializeCriticalSection(&tsdPtr->crit); |
| 124 | |
| 125 | tsdPtr->hwnd = NULL; |
| 126 | tsdPtr->thread = GetCurrentThreadId(); |
| 127 | tsdPtr->event = CreateEvent(NULL, TRUE /* manual */, |
| 128 | FALSE /* !signaled */, NULL); |
| 129 | |
| 130 | return (ClientData) tsdPtr; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | *---------------------------------------------------------------------- |
| 135 | * |
| 136 | * Tcl_FinalizeNotifier -- |
| 137 | * |
| 138 | * This function is called to cleanup the notifier state before |
| 139 | * a thread is terminated. |
| 140 | * |
| 141 | * Results: |
| 142 | * None. |
| 143 | * |
| 144 | * Side effects: |
| 145 | * May dispose of the notifier window and class. |
| 146 | * |
| 147 | *---------------------------------------------------------------------- |
| 148 | */ |
| 149 | |
| 150 | void |
| 151 | Tcl_FinalizeNotifier(clientData) |
| 152 | ClientData clientData; /* Pointer to notifier data. */ |
| 153 | { |
| 154 | ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData; |
| 155 | |
| 156 | /* |
| 157 | * Only finalize the notifier if a notifier was installed in the |
| 158 | * current thread; there is a route in which this is not |
| 159 | * guaranteed to be true (when tclWin32Dll.c:DllMain() is called |
| 160 | * with the flag DLL_PROCESS_DETACH by the OS, which could be |
| 161 | * doing so from a thread that's never previously been involved |
| 162 | * with Tcl, e.g. the task manager) so this check is important. |
| 163 | * |
| 164 | * Fixes Bug #217982 reported by Hugh Vu and Gene Leache. |
| 165 | */ |
| 166 | if (tsdPtr == NULL) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | DeleteCriticalSection(&tsdPtr->crit); |
| 171 | CloseHandle(tsdPtr->event); |
| 172 | |
| 173 | /* |
| 174 | * Clean up the timer and messaging window for this thread. |
| 175 | */ |
| 176 | |
| 177 | if (tsdPtr->hwnd) { |
| 178 | KillTimer(tsdPtr->hwnd, INTERVAL_TIMER); |
| 179 | DestroyWindow(tsdPtr->hwnd); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * If this is the last thread to use the notifier, unregister |
| 184 | * the notifier window class. |
| 185 | */ |
| 186 | |
| 187 | Tcl_MutexLock(¬ifierMutex); |
| 188 | notifierCount--; |
| 189 | if (notifierCount == 0) { |
| 190 | UnregisterClassA("TclNotifier", TclWinGetTclInstance()); |
| 191 | } |
| 192 | Tcl_MutexUnlock(¬ifierMutex); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | *---------------------------------------------------------------------- |
| 197 | * |
| 198 | * Tcl_AlertNotifier -- |
| 199 | * |
| 200 | * Wake up the specified notifier from any thread. This routine |
| 201 | * is called by the platform independent notifier code whenever |
| 202 | * the Tcl_ThreadAlert routine is called. This routine is |
| 203 | * guaranteed not to be called on a given notifier after |
| 204 | * Tcl_FinalizeNotifier is called for that notifier. This routine |
| 205 | * is typically called from a thread other than the notifier's |
| 206 | * thread. |
| 207 | * |
| 208 | * Results: |
| 209 | * None. |
| 210 | * |
| 211 | * Side effects: |
| 212 | * Sends a message to the messaging window for the notifier |
| 213 | * if there isn't already one pending. |
| 214 | * |
| 215 | *---------------------------------------------------------------------- |
| 216 | */ |
| 217 | |
| 218 | void |
| 219 | Tcl_AlertNotifier(clientData) |
| 220 | ClientData clientData; /* Pointer to thread data. */ |
| 221 | { |
| 222 | ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData; |
| 223 | |
| 224 | /* |
| 225 | * Note that we do not need to lock around access to the hwnd |
| 226 | * because the race condition has no effect since any race condition |
| 227 | * implies that the notifier thread is already awake. |
| 228 | */ |
| 229 | |
| 230 | if (tsdPtr->hwnd) { |
| 231 | /* |
| 232 | * We do need to lock around access to the pending flag. |
| 233 | */ |
| 234 | |
| 235 | EnterCriticalSection(&tsdPtr->crit); |
| 236 | if (!tsdPtr->pending) { |
| 237 | PostMessage(tsdPtr->hwnd, WM_WAKEUP, 0, 0); |
| 238 | } |
| 239 | tsdPtr->pending = 1; |
| 240 | LeaveCriticalSection(&tsdPtr->crit); |
| 241 | } else { |
| 242 | SetEvent(tsdPtr->event); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | *---------------------------------------------------------------------- |
| 248 | * |
| 249 | * Tcl_SetTimer -- |
| 250 | * |
| 251 | * This procedure sets the current notifier timer value. The |
| 252 | * notifier will ensure that Tcl_ServiceAll() is called after |
| 253 | * the specified interval, even if no events have occurred. |
| 254 | * |
| 255 | * Results: |
| 256 | * None. |
| 257 | * |
| 258 | * Side effects: |
| 259 | * Replaces any previous timer. |
| 260 | * |
| 261 | *---------------------------------------------------------------------- |
| 262 | */ |
| 263 | |
| 264 | void |
| 265 | Tcl_SetTimer( |
| 266 | Tcl_Time *timePtr) /* Maximum block time, or NULL. */ |
| 267 | { |
| 268 | ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 269 | UINT timeout; |
| 270 | |
| 271 | /* |
| 272 | * Allow the notifier to be hooked. This may not make sense |
| 273 | * on Windows, but mirrors the UNIX hook. |
| 274 | */ |
| 275 | |
| 276 | #ifndef _LANG |
| 277 | if (tclStubs.tcl_SetTimer != tclOriginalNotifier.setTimerProc) { |
| 278 | tclStubs.tcl_SetTimer(timePtr); |
| 279 | return; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | /* |
| 284 | * We only need to set up an interval timer if we're being called |
| 285 | * from an external event loop. If we don't have a window handle |
| 286 | * then we just return immediately and let Tcl_WaitForEvent handle |
| 287 | * timeouts. |
| 288 | */ |
| 289 | |
| 290 | if (!tsdPtr->hwnd) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | if (!timePtr) { |
| 295 | timeout = 0; |
| 296 | } else { |
| 297 | /* |
| 298 | * Make sure we pass a non-zero value into the timeout argument. |
| 299 | * Windows seems to get confused by zero length timers. |
| 300 | */ |
| 301 | |
| 302 | timeout = timePtr->sec * 1000 + timePtr->usec / 1000; |
| 303 | if (timeout == 0) { |
| 304 | timeout = 1; |
| 305 | } |
| 306 | } |
| 307 | tsdPtr->timeout = timeout; |
| 308 | if (timeout != 0) { |
| 309 | tsdPtr->timerActive = 1; |
| 310 | SetTimer(tsdPtr->hwnd, INTERVAL_TIMER, |
| 311 | (unsigned long) tsdPtr->timeout, NULL); |
| 312 | } else { |
| 313 | tsdPtr->timerActive = 0; |
| 314 | KillTimer(tsdPtr->hwnd, INTERVAL_TIMER); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | *---------------------------------------------------------------------- |
| 320 | * |
| 321 | * Tcl_ServiceModeHook -- |
| 322 | * |
| 323 | * This function is invoked whenever the service mode changes. |
| 324 | * |
| 325 | * Results: |
| 326 | * None. |
| 327 | * |
| 328 | * Side effects: |
| 329 | * If this is the first time the notifier is set into |
| 330 | * TCL_SERVICE_ALL, then the communication window is created. |
| 331 | * |
| 332 | *---------------------------------------------------------------------- |
| 333 | */ |
| 334 | |
| 335 | void |
| 336 | Tcl_ServiceModeHook(mode) |
| 337 | int mode; /* Either TCL_SERVICE_ALL, or |
| 338 | * TCL_SERVICE_NONE. */ |
| 339 | { |
| 340 | ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 341 | |
| 342 | /* |
| 343 | * If this is the first time that the notifier has been used from a |
| 344 | * modal loop, then create a communication window. Note that after |
| 345 | * this point, the application needs to service events in a timely |
| 346 | * fashion or Windows will hang waiting for the window to respond |
| 347 | * to synchronous system messages. At some point, we may want to |
| 348 | * consider destroying the window if we leave the modal loop, but |
| 349 | * for now we'll leave it around. |
| 350 | */ |
| 351 | |
| 352 | if (mode == TCL_SERVICE_ALL && !tsdPtr->hwnd) { |
| 353 | tsdPtr->hwnd = CreateWindowA("TclNotifier", "TclNotifier", WS_TILED, |
| 354 | 0, 0, 0, 0, NULL, NULL, TclWinGetTclInstance(), NULL); |
| 355 | /* |
| 356 | * Send an initial message to the window to ensure that we wake up the |
| 357 | * notifier once we get into the modal loop. This will force the |
| 358 | * notifier to recompute the timeout value and schedule a timer |
| 359 | * if one is needed. |
| 360 | */ |
| 361 | |
| 362 | Tcl_AlertNotifier((ClientData)tsdPtr); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | *---------------------------------------------------------------------- |
| 368 | * |
| 369 | * NotifierProc -- |
| 370 | * |
| 371 | * This procedure is invoked by Windows to process events on |
| 372 | * the notifier window. Messages will be sent to this window |
| 373 | * in response to external timer events or calls to |
| 374 | * TclpAlertTsdPtr-> |
| 375 | * |
| 376 | * Results: |
| 377 | * A standard windows result. |
| 378 | * |
| 379 | * Side effects: |
| 380 | * Services any pending events. |
| 381 | * |
| 382 | *---------------------------------------------------------------------- |
| 383 | */ |
| 384 | |
| 385 | static LRESULT CALLBACK |
| 386 | NotifierProc( |
| 387 | HWND hwnd, |
| 388 | UINT message, |
| 389 | WPARAM wParam, |
| 390 | LPARAM lParam) |
| 391 | { |
| 392 | ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 393 | |
| 394 | if (message == WM_WAKEUP) { |
| 395 | EnterCriticalSection(&tsdPtr->crit); |
| 396 | tsdPtr->pending = 0; |
| 397 | LeaveCriticalSection(&tsdPtr->crit); |
| 398 | } else if (message != WM_TIMER) { |
| 399 | return DefWindowProc(hwnd, message, wParam, lParam); |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Process all of the runnable events. |
| 404 | */ |
| 405 | |
| 406 | Tcl_ServiceAll(); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | *---------------------------------------------------------------------- |
| 412 | * |
| 413 | * Tcl_WaitForEvent -- |
| 414 | * |
| 415 | * This function is called by Tcl_DoOneEvent to wait for new |
| 416 | * events on the message queue. If the block time is 0, then |
| 417 | * Tcl_WaitForEvent just polls the event queue without blocking. |
| 418 | * |
| 419 | * Results: |
| 420 | * Returns -1 if a WM_QUIT message is detected, returns 1 if |
| 421 | * a message was dispatched, otherwise returns 0. |
| 422 | * |
| 423 | * Side effects: |
| 424 | * Dispatches a message to a window procedure, which could do |
| 425 | * anything. |
| 426 | * |
| 427 | *---------------------------------------------------------------------- |
| 428 | */ |
| 429 | |
| 430 | int |
| 431 | Tcl_WaitForEvent( |
| 432 | Tcl_Time *timePtr) /* Maximum block time, or NULL. */ |
| 433 | { |
| 434 | ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 435 | MSG msg; |
| 436 | DWORD timeout, result; |
| 437 | int status; |
| 438 | |
| 439 | /* |
| 440 | * Allow the notifier to be hooked. This may not make |
| 441 | * sense on windows, but mirrors the UNIX hook. |
| 442 | */ |
| 443 | |
| 444 | #ifndef _LANG |
| 445 | if (tclStubs.tcl_WaitForEvent != tclOriginalNotifier.waitForEventProc) { |
| 446 | return tclStubs.tcl_WaitForEvent(timePtr); |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | /* |
| 451 | * Compute the timeout in milliseconds. |
| 452 | */ |
| 453 | |
| 454 | if (timePtr) { |
| 455 | timeout = timePtr->sec * 1000 + timePtr->usec / 1000; |
| 456 | } else { |
| 457 | timeout = INFINITE; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Check to see if there are any messages in the queue before waiting |
| 462 | * because MsgWaitForMultipleObjects will not wake up if there are events |
| 463 | * currently sitting in the queue. |
| 464 | */ |
| 465 | |
| 466 | if (!PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { |
| 467 | /* |
| 468 | * Wait for something to happen (a signal from another thread, a |
| 469 | * message, or timeout). |
| 470 | */ |
| 471 | |
| 472 | result = MsgWaitForMultipleObjects(1, &tsdPtr->event, FALSE, timeout, |
| 473 | QS_ALLINPUT); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Check to see if there are any messages to process. |
| 478 | */ |
| 479 | |
| 480 | if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { |
| 481 | /* |
| 482 | * Retrieve and dispatch the first message. |
| 483 | */ |
| 484 | |
| 485 | result = GetMessage(&msg, NULL, 0, 0); |
| 486 | if (result == 0) { |
| 487 | /* |
| 488 | * We received a request to exit this thread (WM_QUIT), so |
| 489 | * propagate the quit message and start unwinding. |
| 490 | */ |
| 491 | |
| 492 | PostQuitMessage((int) msg.wParam); |
| 493 | status = -1; |
| 494 | } else if (result == -1) { |
| 495 | /* |
| 496 | * We got an error from the system. I have no idea why this would |
| 497 | * happen, so we'll just unwind. |
| 498 | */ |
| 499 | |
| 500 | status = -1; |
| 501 | } else { |
| 502 | TranslateMessage(&msg); |
| 503 | DispatchMessage(&msg); |
| 504 | status = 1; |
| 505 | } |
| 506 | } else { |
| 507 | status = 0; |
| 508 | } |
| 509 | |
| 510 | ResetEvent(tsdPtr->event); |
| 511 | return status; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | *---------------------------------------------------------------------- |
| 516 | * |
| 517 | * Tcl_Sleep -- |
| 518 | * |
| 519 | * Delay execution for the specified number of milliseconds. |
| 520 | * |
| 521 | * Results: |
| 522 | * None. |
| 523 | * |
| 524 | * Side effects: |
| 525 | * Time passes. |
| 526 | * |
| 527 | *---------------------------------------------------------------------- |
| 528 | */ |
| 529 | |
| 530 | void |
| 531 | Tcl_Sleep(ms) |
| 532 | int ms; /* Number of milliseconds to sleep. */ |
| 533 | { |
| 534 | /* |
| 535 | * Simply calling 'Sleep' for the requisite number of milliseconds |
| 536 | * can make the process appear to wake up early because it isn't |
| 537 | * synchronized with the CPU performance counter that is used in |
| 538 | * tclWinTime.c. This behavior is probably benign, but messes |
| 539 | * up some of the corner cases in the test suite. We get around |
| 540 | * this problem by repeating the 'Sleep' call as many times |
| 541 | * as necessary to make the clock advance by the requisite amount. |
| 542 | */ |
| 543 | |
| 544 | Tcl_Time now; /* Current wall clock time */ |
| 545 | Tcl_Time desired; /* Desired wakeup time */ |
| 546 | DWORD sleepTime = ms; /* Time to sleep */ |
| 547 | |
| 548 | Tcl_GetTime( &now ); |
| 549 | desired.sec = now.sec + ( ms / 1000 ); |
| 550 | desired.usec = now.usec + 1000 * ( ms % 1000 ); |
| 551 | if ( desired.usec > 1000000 ) { |
| 552 | ++desired.sec; |
| 553 | desired.usec -= 1000000; |
| 554 | } |
| 555 | |
| 556 | for ( ; ; ) { |
| 557 | Sleep( sleepTime ); |
| 558 | Tcl_GetTime( &now ); |
| 559 | if ( now.sec > desired.sec ) { |
| 560 | break; |
| 561 | } else if ( ( now.sec == desired.sec ) |
| 562 | && ( now.usec >= desired.usec ) ) { |
| 563 | break; |
| 564 | } |
| 565 | sleepTime = ( ( 1000 * ( desired.sec - now.sec ) ) |
| 566 | + ( ( desired.usec - now.usec ) / 1000 ) ); |
| 567 | } |
| 568 | |
| 569 | } |
| 570 | |
| 571 | #endif |