File: GSRunLoopCtxt.m

package info (click to toggle)
gnustep-base 1.28.1%2Breally1.28.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,008 kB
  • sloc: objc: 223,137; ansic: 35,562; sh: 184; makefile: 128; cpp: 122; xml: 32
file content (617 lines) | stat: -rw-r--r-- 15,677 bytes parent folder | download | duplicates (3)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/**
 * The GSRunLoopCtxt stores context information to handle polling for
 * events.  This information is associated with a particular runloop
 * mode, and persists throughout the life of the runloop instance.
 *
 *	NB.  This class is private to NSRunLoop and must not be subclassed.
 */

#import "common.h"

#import "Foundation/NSError.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSNotificationQueue.h"
#import "Foundation/NSPort.h"
#import "Foundation/NSStream.h"
#import "../GSRunLoopCtxt.h"
#import "../GSRunLoopWatcher.h"
#import "../GSPrivate.h"

#define	FDCOUNT	1024

static SEL	wRelSel;
static SEL	wRetSel;
static IMP	wRelImp;
static IMP	wRetImp;

static void
wRelease(NSMapTable* t, void* w)
{
  (*wRelImp)((id)w, wRelSel);
}

static void
wRetain(NSMapTable* t, const void* w)
{
  (*wRetImp)((id)w, wRetSel);
}

static const NSMapTableValueCallBacks WatcherMapValueCallBacks = 
{
  wRetain,
  wRelease,
  0
};

@implementation	GSRunLoopCtxt

+ (void) initialize
{
  wRelSel = @selector(release);
  wRetSel = @selector(retain);
  wRelImp = [[GSRunLoopWatcher class] instanceMethodForSelector: wRelSel];
  wRetImp = [[GSRunLoopWatcher class] instanceMethodForSelector: wRetSel];
}

- (void) dealloc
{
  RELEASE(mode);
  GSIArrayEmpty(performers);
  NSZoneFree(performers->zone, (void*)performers);
  GSIArrayEmpty(timers);
  NSZoneFree(timers->zone, (void*)timers);
  GSIArrayEmpty(watchers);
  NSZoneFree(watchers->zone, (void*)watchers);
  if (handleMap != 0)
    {
      NSFreeMapTable(handleMap);
    }
  if (winMsgMap != 0)
    {
      NSFreeMapTable(winMsgMap);
    }
  GSIArrayEmpty(_trigger);
  NSZoneFree(_trigger->zone, (void*)_trigger);
  [super dealloc];
}

/**
 * Remove any callback for the specified event which is set for an
 * uncompleted poll operation.<br />
 * This is called by nested event loops on contexts in outer loops
 * when they handle an event ... removing the event from the outer
 * loop ensures that it won't get handled twice, once by the inner
 * loop and once by the outer one.
 */
- (void) endEvent: (void*)data
              for: (GSRunLoopWatcher*)watcher
{
  if (completed == NO)
    {
      unsigned i = GSIArrayCount(_trigger);

      while (i-- > 0)
	{
	  GSIArrayItem	item = GSIArrayItemAtIndex(_trigger, i);

	  if (item.obj == (id)watcher)
	    {
	      GSIArrayRemoveItemAtIndex(_trigger, i);
	    }
	}
      switch (watcher->type)
	{
	  case ET_RPORT:
	  case ET_HANDLE:
	    NSMapRemove(handleMap, data);
	    break;
	  case ET_WINMSG:
	    NSMapRemove(winMsgMap, data);
	    break;
	  case ET_TRIGGER:
	    // Already handled
	    break;
	  default:
	    NSLog(@"Ending an event of unexpected type (%d)", watcher->type);
	    break;
	}
    }
}

/**
 * Mark this poll context as having completed, so that if we are
 * executing a re-entrant poll, the enclosing poll operations
 * know they can stop what they are doing because an inner
 * operation has done the job.
 */
- (void) endPoll
{
  completed = YES;
}

- (id) init
{
  [NSException raise: NSInternalInconsistencyException
	      format: @"-init may not be called for GSRunLoopCtxt"];
  return nil;
}

- (id) initWithMode: (NSString*)theMode extra: (void*)e
{
  self = [super init];
  if (self != nil)
    {
      NSZone	*z;

      mode = [theMode copy];
      extra = e;
      z = [self zone];
      performers = NSZoneMalloc(z, sizeof(GSIArray_t));
      timers = NSZoneMalloc(z, sizeof(GSIArray_t));
      watchers = NSZoneMalloc(z, sizeof(GSIArray_t));
      _trigger = NSZoneMalloc(z, sizeof(GSIArray_t));
      GSIArrayInitWithZoneAndCapacity(performers, z, 8);
      GSIArrayInitWithZoneAndCapacity(timers, z, 8);
      GSIArrayInitWithZoneAndCapacity(watchers, z, 8);
      GSIArrayInitWithZoneAndCapacity(_trigger, z, 8);

      handleMap = NSCreateMapTable(NSIntegerMapKeyCallBacks,
              WatcherMapValueCallBacks, 0);
      winMsgMap = NSCreateMapTable(NSIntegerMapKeyCallBacks,
              WatcherMapValueCallBacks, 0);
    }
  return self;
}

/*
 * If there is a generic watcher (watching hwnd == 0),
 * loop through all events, and send them to the correct
 * watcher (if there are any) and then process the rest right here.
 * Return a flag to say whether any messages were handled.
 */
- (BOOL) processAllWindowsMessages:(int)num_winMsgs within: (NSArray*)contexts
{
  MSG			msg;
  GSRunLoopWatcher	*generic = nil;
  unsigned		i;
  BOOL			handled = NO;

  if (num_winMsgs > 0)
    {
      generic = NSMapGet(winMsgMap,0);
    }
  
  if (generic != nil && generic->_invalidated == NO)
    {
      while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
	{
	  if (num_winMsgs > 0)
	    {
	      HANDLE		handle;
	      GSRunLoopWatcher	*watcher;

	      handle = msg.hwnd;
	      watcher = (GSRunLoopWatcher*)NSMapGet(winMsgMap,
		(void*)handle);
	      if (watcher == nil || watcher->_invalidated == YES)
		{
		  handle = 0;	// Generic
		  watcher
		    = (GSRunLoopWatcher*)NSMapGet(winMsgMap, (void*)handle);
		}
	      if (watcher != nil && watcher->_invalidated == NO)
		{
		  i = [contexts count];
		  while (i-- > 0)
		    {
		      GSRunLoopCtxt *c = [contexts objectAtIndex: i];

		      if (c != self)
			{ 
			  [c endEvent: (void*)handle for: watcher];
			}
		    }
		  handled = YES;
		  /*
		   * The watcher is still valid - so call the
		   * receiver's event handling method.
		   */
		  [watcher->receiver receivedEvent: watcher->data
					      type: watcher->type
					     extra: (void*)&msg
					   forMode: mode];
		  continue;
		}
	    }
	  TranslateMessage(&msg); 
	  DispatchMessage(&msg);
	}
    }
  else
    {
      if (num_winMsgs > 0)
	{
	  unsigned		num = num_winMsgs;
	  NSMapEnumerator	hEnum;
	  HANDLE		handle;
	  GSRunLoopWatcher	*watcher;

	  hEnum = NSEnumerateMapTable(winMsgMap);
	  while (NSNextMapEnumeratorPair(&hEnum, &handle, (void**)&watcher))
	    {
	      if (watcher->_invalidated == NO)
		{
		  while (PeekMessage(&msg, handle, 0, 0, PM_REMOVE))
		    {
		      i = [contexts count];
		      while (i-- > 0)
			{
			  GSRunLoopCtxt *c = [contexts objectAtIndex: i];
			      
			  if (c != self)
			    {
			      [c endEvent: (void*)handle for: watcher];
			    }
			}
		      handled = YES;
		      [watcher->receiver receivedEvent: watcher->data
						  type: watcher->type
						 extra: (void*)&msg
					       forMode: mode];
		    }
		}
	      num--;
	    }
	  NSEndMapTableEnumeration(&hEnum);
	} 
    }
  return handled;
}

- (BOOL) pollUntil: (int)milliseconds within: (NSArray*)contexts
{
  GSRunLoopThreadInfo   *threadInfo = GSRunLoopInfoForThread(nil);
  NSMapEnumerator	hEnum;
  GSRunLoopWatcher	*watcher;
  HANDLE		handleArray[MAXIMUM_WAIT_OBJECTS-1];
  int			num_handles;
  int			num_winMsgs;
  unsigned		count;
  unsigned		i;
  void			*handle;
  int			wait_timeout;
  DWORD			wait_return;
  BOOL			immediate = NO;
  BOOL			existingMessages = NO;

  // Set timeout how much time should wait
  if (milliseconds >= 0)
    {
      wait_timeout = milliseconds;
    }
  else
    {
      wait_timeout = INFINITE;
    }

  NSResetMapTable(handleMap);
  NSResetMapTable(winMsgMap);
  GSIArrayRemoveAllItems(_trigger);

  i = GSIArrayCount(watchers);
  num_handles = 1;              // One handle for signals from other threads
  num_winMsgs = 0;

  while (i-- > 0)
    {
      GSRunLoopWatcher	*info;
      BOOL		trigger;
      
      info = GSIArrayItemAtIndex(watchers, i).obj;
      if (info->_invalidated == YES)
	{
	  GSIArrayRemoveItemAtIndex(watchers, i);
	}
      else if ([info runLoopShouldBlock: &trigger] == NO)
	{
	  if (trigger == YES)
	    {
	      immediate = YES;
	      GSIArrayAddItem(_trigger, (GSIArrayItem)(id)info);
	    }
	}
      else
	{
	  HANDLE	handle;

	  switch (info->type)
	    {
	      case ET_HANDLE:
		handle = (HANDLE)(size_t)info->data;
		NSMapInsert(handleMap, (void*)handle, info);
		num_handles++;
		break;
	      case ET_RPORT:
		{
		  id port = info->receiver;
		  NSInteger port_hd_size = FDCOUNT;
		  NSInteger port_hd_count = FDCOUNT;
		  NSInteger port_hd_buffer[FDCOUNT];
		  NSInteger *port_hd_array = port_hd_buffer;

		  [port getFds: port_hd_array count: &port_hd_count];
                  while (port_hd_count > port_hd_size)
                    {
                      if (port_hd_array != port_hd_buffer) free(port_hd_array);
                      port_hd_size = port_hd_count;
                      port_hd_count = port_hd_size;
                      port_hd_array = malloc(sizeof(NSInteger)*port_hd_size);
                      [port getFds: port_hd_array count: &port_hd_count];
                    }
		  NSDebugMLLog(@"NSRunLoop", @"listening to %d port handles",
		    port_hd_count);
		  while (port_hd_count--)
		    {
		      NSMapInsert(handleMap, 
			(void*)(size_t) port_hd_array[port_hd_count],
			info);
		      num_handles++;
		    }
                  if (port_hd_array != port_hd_buffer) free(port_hd_array);
		}
		break;
	      case ET_WINMSG:
		handle = (HANDLE)(size_t)info->data;
		NSMapInsert(winMsgMap, (void*)handle, info);
		num_winMsgs++;
		break;
	      case ET_TRIGGER:
		break;
	    }
	}
    }
    
  /*
   * If there are notifications in the 'idle' queue, we try an
   * instantaneous select so that, if there is no input pending,
   * we can service the queue.  Similarly, if a task has completed,
   * we need to deliver its notifications.
   */
  if (GSPrivateCheckTasks() || GSPrivateNotifyMore(mode) || immediate == YES)
    {
      wait_timeout = 0;
    }

  handleArray[0] = threadInfo->event; // Signal from other thread
  num_handles = NSCountMapTable(handleMap) + 1;
  if (num_handles >= MAXIMUM_WAIT_OBJECTS)
    {
      NSLog(@"Too many handles to wait for ... only using %d of %d",
        MAXIMUM_WAIT_OBJECTS-1, num_handles);
      num_handles = MAXIMUM_WAIT_OBJECTS-1;
    }
  count = num_handles - 1;	// Count of handles excluding thread event
  if (count > 0)
    {
      i = 1 + (fairStart++ % count);
      hEnum = NSEnumerateMapTable(handleMap);
      while (count-- > 0
	&& NSNextMapEnumeratorPair(&hEnum, &handle, (void**)&watcher))
	{
	  if (i >= num_handles)
	    {
	      i = 1;
	    }
	  handleArray[i++] = (HANDLE)handle;
	}
      NSEndMapTableEnumeration(&hEnum);
    }

  completed = NO;

  /* Clear all the windows messages first before we wait,
   * since MsgWaitForMultipleObjects only signals on NEW messages
   */
  if ([self processAllWindowsMessages: num_winMsgs within: contexts] == YES)
    {
      // Processed something ... no need to wait.
      wait_timeout = 0;
      num_winMsgs = 0;
      existingMessages = YES;
    }

  if (num_winMsgs > 0)
    {
      NSDebugMLLog(@"NSRunLoop",
	@"wait for messages and %d handles for %d milliseconds",
	num_handles, wait_timeout);
	
      /*
       * Wait for signalled events or window messages.
       */
      wait_return = MsgWaitForMultipleObjects(num_handles, handleArray, 
        NO, wait_timeout, QS_ALLINPUT);
    }
  else if (num_handles > 0)
    {
      NSDebugMLLog(@"NSRunLoop",
	@"wait for %d handles for %d milliseconds", num_handles, wait_timeout);

      /*
       * We are not interested in windows messages ... just wait for
       * signalled events.
       */
      wait_return = WaitForMultipleObjects(num_handles, handleArray, 
        NO, wait_timeout);
    }
  else
    {
      NSDebugMLLog(@"NSRunLoop",
	@"wait for %d milliseconds", wait_timeout);
      SleepEx(wait_timeout, TRUE);
      wait_return = WAIT_TIMEOUT;
    }

  // check wait errors
  if (WAIT_FAILED == wait_return
    || (wait_return >= WAIT_ABANDONED_0 
      && wait_return < WAIT_ABANDONED_0 + num_handles))
    {
      int	i;
      BOOL	found = NO;

      NSDebugMLLog(@"NSRunLoop", @"WaitForMultipleObjects() error in "
	@"-pollUntil:within: %@", [NSError _last]);
      /*
       * Check each handle in turn until either we find one which has an
       * event signalled, or we find the one which caused the original
       * wait to fail ... so the callback routine for that handle can
       * deal with the problem.
       */
      for (i = 0; i < num_handles; i++)
	{
	  handleArray[0] = handleArray[i];
	  wait_return = WaitForMultipleObjects(1, handleArray, NO, 0);
	  if (wait_return != WAIT_TIMEOUT)
	    {
	      wait_return = WAIT_OBJECT_0;
	      found = YES;
	      break;
	    }
	}
      if (found == NO)
	{
	  NSLog(@"WaitForMultipleObjects() error in "
	    @"-pollUntil:within: %@", [NSError _last]);
	  abort ();        
	}
    }

  /*
   * Trigger any watchers which are set up to trigger for every runloop wait.
   */
  count = GSIArrayCount(_trigger);
  completed = NO;
  while (count-- > 0)
    {
      GSRunLoopWatcher	*watcher;

      watcher = (GSRunLoopWatcher*)GSIArrayItemAtIndex(_trigger, count).obj;
      if (watcher->_invalidated == NO)
	{
	  NSDebugMLLog(@"NSRunLoop", @"trigger watcher %@", watcher);
	  i = [contexts count];
	  while (i-- > 0)
	    {
	      GSRunLoopCtxt	*c = [contexts objectAtIndex: i];

	      if (c != self)
		{
		  [c endEvent: (void*)watcher for: watcher];
		}
	    }
	  /*
	   * The watcher is still valid - so call its
	   * receivers event handling method.
	   */
	  [watcher->receiver receivedEvent: watcher->data
				      type: watcher->type
				     extra: watcher->data
				   forMode: mode];
	}
      GSPrivateNotifyASAP(mode);
    }

  if (WAIT_TIMEOUT == wait_return)
    {
      // there is no event to handle
      if (existingMessages)
	{
	  NSDebugMLLog(@"NSRunLoop", @"processed windows messages");
	}
      else
	{
	  NSDebugMLLog(@"NSRunLoop", @"timeout without events");
	  completed = YES;
	  return NO;        
	}
    }
  else if (WAIT_OBJECT_0 + num_handles == wait_return)
    {
      // one or more windows message
      NSDebugMLLog(@"NSRunLoop", @"processing windows messages");
      [self processAllWindowsMessages: num_winMsgs within: contexts];
    }
  else if ((i = wait_return - WAIT_OBJECT_0) >= 0 && i < num_handles)
    {
      /* Look the event that WaitForMultipleObjects() says is ready;
       * get the corresponding fd for that handle event and notify
       * the corresponding object for the ready fd.
       */
      NSDebugMLLog(@"NSRunLoop", @"Handle signalled %d", i);
      
      handle = handleArray[i];

      if (handle == threadInfo->event)
	{
	  watcher = nil;
	  NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
	  [threadInfo fire];
	}
      else
	{
	  watcher = (GSRunLoopWatcher*)NSMapGet(handleMap, (void*)handle);
	  NSDebugMLLog(@"NSRunLoop", @"Fire watcher %@", watcher);
	}
      if (watcher != nil && watcher->_invalidated == NO)
	{
	  i = [contexts count];
	  while (i-- > 0)
	    {
	      GSRunLoopCtxt *c = [contexts objectAtIndex: i];

	      if (c != self)
		{ 
		  [c endEvent: (void*)handle for: watcher];
		}
	    }
	  /*
	   * The watcher is still valid - so call its receivers
	   * event handling method.
	   */
	  [watcher->receiver receivedEvent: watcher->data
				      type: watcher->type
				     extra: (void*)handle
				   forMode: mode];
	}
    }
  else
    {
      NSDebugMLLog(@"NSRunLoop", @"unexpected result %d", wait_return);
      GSPrivateNotifyASAP(mode);
      completed = NO;
      return NO;        
    }

  GSPrivateNotifyASAP(mode);
  completed = YES;
  return YES;
}

+ (BOOL) awakenedBefore: (NSDate*)when
{
  GSRunLoopThreadInfo   *threadInfo = GSRunLoopInfoForThread(nil);
  NSTimeInterval	ti = (when == nil) ? 0.0 : [when timeIntervalSinceNow];
  int			milliseconds = (ti <= 0.0) ? 0 : (int)(ti*1000);
  HANDLE		h = threadInfo->event;

  if (WaitForMultipleObjects(1, &h, NO, milliseconds) != WAIT_TIMEOUT)
    {
      NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
      [threadInfo fire];
      return YES;
    }
  return NO;
}

@end