/[dtapublic]/projs/trunk/shared_source/c_tcl_base_7_5_w_mods/tclwinnotify.c
ViewVC logotype

Contents of /projs/trunk/shared_source/c_tcl_base_7_5_w_mods/tclwinnotify.c

Parent Directory Parent Directory | Revision Log Revision Log


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

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25