summaryrefslogtreecommitdiff
path: root/connect/mitsock/OTidle.c
blob: 8072369d3daeec16dde65b0f46f3f56ac1520b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* $Copyright:
 *
 * Copyright � 1998-1999 by the Massachusetts Institute of Technology.
 * 
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  Furthermore if you modify
 * this software you must label your software as modified software and not
 * distribute it in such a fashion that it might be confused with the
 * original MIT software. M.I.T. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * Individual source code files are copyright MIT, Cygnus Support,
 * OpenVision, Oracle, Sun Soft, FundsXpress, and others.
 * 
 * Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira,
 * and Zephyr are trademarks of the Massachusetts Institute of Technology
 * (MIT).  No commercial use of these trademarks may be made without prior
 * written permission of MIT.
 * 
 * "Commercial use" means use of a name in a product or other for-profit
 * manner.  It does NOT prevent a commercial firm from referring to the MIT
 * trademarks in order to convey information (although in doing so,
 * recognition of their trademark status should be given).
 * $
 */


#include <Threads.h>			// declaration for YieldToAnyThread
#include <Resources.h>			// GetResFile, SetResFile
#include "IdleInternal.h"


/* Global Variables */
Boolean           gIsThreaded		= false;
Boolean           gShouldIdle		= true;
UInt32            gEventSleepTime	= kDefaultEventSleepTime;
UInt32            gIdleFrequency	= kDefaultIdleFrequency;
UInt16            gEventMask 		= kDefaultEventMask;
IdleEventHandler *gFirstEventHandler= NULL;
IdleEventHandler *gLastEventHandler	= NULL;

/*
 * FindEventHandlerbyUPP() - finds the first event handler with a given UPP
 *                           returns nil if there is no such UPP
 */
IdleEventHandler *FindEventHandlerByUPP(IdleEventHandlerUPP eventHandlerUPP)
{
  IdleEventHandler *handler = gFirstEventHandler;
  
  while(handler != nil)
    {
      if(handler->UPP == eventHandlerUPP)
        break;
      else
        handler = handler->next;
    }
    
  return(handler);
}


/*
 * RecalculateEventMask() - recalculates the global event mask
 */
void RecalculateEventMask(void)
{
  IdleEventHandler *handler = gFirstEventHandler;
  
  gEventMask = 0;
  handler = gFirstEventHandler;
  while(handler != nil)
    {
      if(handler->active)
        gEventMask |= handler->mask;
      
      handler = handler->next;
    }
}


/*
 * IdleAddEventHandler() - adds an idle event handler. You should only call this if the 
 *                         library is running in single thread mode.  In this way, 
 *                         the application can handle events that come in while 
 *                         waiting on the network.  mask is the eventMask you want.
 *
 *                         Handlers are active by default. Call IdleSetActive to change this.
 */
OSStatus IdleAddEventHandler(IdleEventHandlerUPP eventHandlerUPP, UInt16 mask, UInt32 refCon)
{
  IdleEventHandler *handler;
  
  handler = malloc(sizeof(IdleEventHandler));
  if(handler == nil)
    return(paramErr);
    
  handler->UPP  =   eventHandlerUPP;
  handler->mask =   mask;
  handler->refCon = refCon;
  handler->active = 1L;
  
  /* Add to linked list */
  handler->prev = gLastEventHandler;
  handler->next = nil;
  if (gLastEventHandler != NULL) gLastEventHandler->next = handler;
  
  /* repoint the last event handler */
  if(gFirstEventHandler == nil)
    gFirstEventHandler = handler;
  gLastEventHandler = handler;
  
  /* Add our mask to the global mask */
  gEventMask |= handler->mask;
  
  return(noErr);
}


/*
 * IdleRemoveEventHandler() - Removes an idle event handler. You should only call this 
 *                            if the library is running in single thread mode. In this way, 
 *                            the application can handle events that come in while 
 *                            waiting on the network.  Returns paramErr if it fails.
 */
OSStatus IdleRemoveEventHandler(IdleEventHandlerUPP eventHandlerUPP)
{
  IdleEventHandler *handler;
  
  handler = FindEventHandlerByUPP(eventHandlerUPP);
  
  if(handler == nil)
    return(paramErr);
  
  /* fix forward links */
  if(handler == gFirstEventHandler)
    gFirstEventHandler = handler->next;      /* make the next entry the first one */
  else
    { 
      IdleEventHandler *previous;
      
      previous = handler->prev;
      previous->next = handler->next;
    }

  /* fix backward links  */
  if(handler == gLastEventHandler)
    gLastEventHandler = handler->prev;      /* make the previous entry the last one */
  else
    {
      IdleEventHandler *next;
      
      next = handler->next;
      next->prev = handler->prev;
    }
    
  free(handler);                        /* release memory */
  
  RecalculateEventMask();
  
  return(noErr);
}


/*
 * IdleSetActive() - Makes the Event Handler active
 */
void IdleSetActive(IdleEventHandlerUPP eventHandlerUPP)
{
  IdleEventHandler *handler;
  
  handler = FindEventHandlerByUPP(eventHandlerUPP);
  
  if(handler != nil)
    {
      handler->active++;
      gEventMask |= handler->mask;
    }
  return;
}


/*
 * IdleSetInactive() - Makes the Event Handler inactive
 */
void IdleSetInactive(IdleEventHandlerUPP eventHandlerUPP)
{
  IdleEventHandler *handler;
  
  handler = FindEventHandlerByUPP(eventHandlerUPP);
  
  if(handler == nil)
    return;
    
  if(handler->active > 0)
    handler->active--;
    
  RecalculateEventMask();
  return;
}


/*
 * IdleSetIdleFrequency() - Sets the time the idle loop waits before calling 
 *                          WaitNextEvent() or YieldToAnyThread()
 *                          (the result is in ticks -- 1/60 of a second)
 */
void IdleSetIdleFrequency(UInt32 idleFrequency)
{
  gIdleFrequency = idleFrequency;
  return;
}


/*
 * IdleGetIdleFrequency() - Gets the time the idle loop waits before calling 
 *                          WaitNextEvent() or YieldToAnyThread()
 *                          (the result is in ticks -- 1/60 of a second)
 */
UInt32 IdleGetIdleFrequency(void)
{
  return(gIdleFrequency);
}


/*
 * IdleSetEventSleepTime() - Sets the time WaitNextEvent() will sleep when waiting for
 *                           an event -- only used in non-threaded mode or when the
 *                           main thread makes blocking calls
 *                           (sleepTime is in ticks -- 1/60 of a second)
 */
void IdleSetEventSleepTime(UInt32 eventSleepTime)
{
  gEventSleepTime = eventSleepTime;
  return;
}


/*
 * IdleGetEventSleepTime() - Gets the time WaitNextEvent() will sleep when waiting for
 *                           an event -- only used in non-threaded mode or when the
 *                           main thread makes blocking calls
 *                           (the result is in ticks -- 1/60 of a second)
 */
UInt32 IdleGetEventSleepTime(void)
{
  return(gEventSleepTime);
}


/*
 * IdleSetThreaded() -- Sets whether we are running in multi-threaded mode
 */
void IdleSetThreaded(Boolean isThreaded)
{
  gIsThreaded = isThreaded;
  return;
}


/*
 * IdleGetThreaded() -- Gets whether we are running in multi-threaded mode
 */
Boolean IdleGetThreaded(void)
{
  return(gIsThreaded);
}


/*
 * IdleSetShouldIdle() -- Sets whether we should idle at all (used when you can't idle)
 */
void IdleSetShouldIdle(Boolean shouldIdle)
{
  gShouldIdle = shouldIdle;
  return;
}


/*
 * IdleGetShouldIdle() -- Sets whether we should idle at all 
 */
Boolean IdleGetShouldIdle(void)
{
  return(gShouldIdle);
}


/*
 * Idle() - Give time to other threads or the system while we wait.
 */
OSStatus Idle(void)
{
  OSStatus             theError;
  EventRecord          theEvent;
  static UInt32        lastCheck = 0;
  UInt32               now = TickCount();
  
  /* Don't idle if we shouldn't!  We're probably at some scary system time */
  if(gShouldIdle == false)
    return(noErr);
  
  /* If we are multi-threaded call YieldToAnyThread, otherwise call WaitNextEvent */
  if(gIsThreaded)
    {
      /* We are not the main thread -- we should just give time
         to the other threads.  The main thread will take care of events */
      if(now > (lastCheck + gIdleFrequency))
        {
          ThreadID      currentThread;
          
          theError = GetCurrentThread(&currentThread);
          if(theError != noErr)
            return(theError);

          theError = YieldToAnyThread();
          if(theError != noErr)
            return(theError);
            
          lastCheck = now;
        }
    }
  else
    {
      /* We are the main thread.  We need to give time to the system periodically */
      if(now > (lastCheck + gIdleFrequency))
        {
          /* WaitNextEvent can change the resource file (through patches like Retrospect does) */
          SInt16 saveResFile = CurResFile(); 
          
          Boolean retval;
          /* If we are the main thread, we always need to give time to the system */
          retval = WaitNextEvent(gEventMask, &theEvent, gEventSleepTime, nil);
          
          if((retval == true) || (theEvent.what == nullEvent))
            {
              IdleEventHandler *handler = gFirstEventHandler;
              
			  while(handler != nil)
			    {
			      if(handler->active > 0)
			        {
			          Boolean handled = false;
					  handled = CallIdleEventHandlerProc(handler->UPP, &theEvent, handler->refCon);
			          if(handled && theEvent.what != nullEvent)
			            break; /* non-null event handled -- stop */
			        }
			      handler = handler->next;
			    }
			}
          
          lastCheck = now;
          
          /* Restore the res file to whatever it was when before WaitNextEvent and the callback */
          UseResFile(saveResFile);
        }
    }
  
  return(noErr);
}