Add implemention of DDRunLoopPoker with a CFRunLoopSource
authorDave Dribin <dave@dribin.org>
Sat Nov 21 17:04:30 2009 -0600 (2009-11-21)
changeset 166f33dc5af8882
parent 165 0d52867dd5bc
child 167 de4178bcaacf
Add implemention of DDRunLoopPoker with a CFRunLoopSource
lib/DDRunLoopPoker.h
lib/DDRunLoopPoker.m
tests/DDRunLoopPokerTest.m
     1.1 --- a/lib/DDRunLoopPoker.h	Mon Sep 21 02:06:52 2009 -0500
     1.2 +++ b/lib/DDRunLoopPoker.h	Sat Nov 21 17:04:30 2009 -0600
     1.3 @@ -29,6 +29,7 @@
     1.4  {
     1.5      NSRunLoop * _runLoop;
     1.6      NSPort * _pokerPort;
     1.7 +    CFRunLoopSourceRef _pokerSource;
     1.8  }
     1.9  
    1.10  + (id)pokerWithRunLoop:(NSRunLoop *)runLoop;
     2.1 --- a/lib/DDRunLoopPoker.m	Mon Sep 21 02:06:52 2009 -0500
     2.2 +++ b/lib/DDRunLoopPoker.m	Sat Nov 21 17:04:30 2009 -0600
     2.3 @@ -24,7 +24,11 @@
     2.4  
     2.5  #import "DDRunLoopPoker.h"
     2.6  
     2.7 +#define USE_MACH_PORT 1
     2.8 +
     2.9 +#if USE_MACH_PORT
    2.10  static const uint32_t kPokeMessage = 100;
    2.11 +#endif
    2.12  
    2.13  @implementation DDRunLoopPoker
    2.14  
    2.15 @@ -46,9 +50,30 @@
    2.16          return nil;
    2.17      
    2.18      _runLoop = [runLoop retain];
    2.19 +#if USE_MACH_PORT
    2.20      _pokerPort = [[NSMachPort port] retain];
    2.21      [_runLoop addPort:_pokerPort forMode:NSDefaultRunLoopMode];
    2.22      [_runLoop addPort:_pokerPort forMode:NSModalPanelRunLoopMode];
    2.23 +#else
    2.24 +    // We can set all callbacks to NULL. We're just using this as a way
    2.25 +    // to ensure there's at least on source on the run loop and to wake
    2.26 +    // up the run loop. We don't need to do anything when it fires.
    2.27 +    CFRunLoopSourceContext context = {
    2.28 +        .info = 0,
    2.29 +        .retain = NULL,
    2.30 +        .release = NULL,
    2.31 +        .copyDescription = NULL,
    2.32 +        .equal = NULL,
    2.33 +        .hash = NULL,
    2.34 +        .schedule = NULL,
    2.35 +        .cancel = NULL,
    2.36 +        .perform = NULL,
    2.37 +    };
    2.38 +    _pokerSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
    2.39 +    CFRunLoopRef cfRunLoop = [_runLoop getCFRunLoop];
    2.40 +    CFRunLoopAddSource(cfRunLoop, _pokerSource, (CFStringRef)NSDefaultRunLoopMode);
    2.41 +    CFRunLoopAddSource(cfRunLoop, _pokerSource, (CFStringRef)NSModalPanelRunLoopMode);
    2.42 +#endif
    2.43      
    2.44      return self;
    2.45  }
    2.46 @@ -69,6 +94,7 @@
    2.47  
    2.48  - (void)dispose;
    2.49  {
    2.50 +#if USE_MACH_PORT
    2.51      if (_pokerPort != nil)
    2.52      {
    2.53          [_runLoop removePort:_pokerPort forMode:NSModalPanelRunLoopMode];
    2.54 @@ -77,6 +103,17 @@
    2.55      [_pokerPort invalidate];
    2.56      [_pokerPort release];
    2.57      _pokerPort = nil;
    2.58 +#else
    2.59 +    CFRunLoopRef cfRunLoop = [_runLoop getCFRunLoop];
    2.60 +    if (_pokerSource != NULL)
    2.61 +    {
    2.62 +        CFRunLoopRemoveSource(cfRunLoop, _pokerSource, (CFStringRef)NSModalPanelRunLoopMode);
    2.63 +        CFRunLoopRemoveSource(cfRunLoop, _pokerSource, (CFStringRef)NSDefaultRunLoopMode);
    2.64 +    }
    2.65 +    CFRunLoopSourceInvalidate(_pokerSource);
    2.66 +    CFRelease(_pokerSource);
    2.67 +    _pokerSource = NULL;
    2.68 +#endif
    2.69      
    2.70      [_runLoop release];
    2.71      _runLoop = nil;
    2.72 @@ -84,6 +121,7 @@
    2.73  
    2.74  - (void)pokeRunLoop;
    2.75  {
    2.76 +#if USE_MACH_PORT
    2.77      NSPortMessage * message = [[NSPortMessage alloc] initWithSendPort:_pokerPort
    2.78                                                            receivePort:nil
    2.79                                                             components:nil];
    2.80 @@ -91,6 +129,11 @@
    2.81      
    2.82      [message setMsgid:kPokeMessage];
    2.83      [message sendBeforeDate:[NSDate date]];
    2.84 +#elif 1
    2.85 +    CFRunLoopSourceSignal(_pokerSource);
    2.86 +    CFRunLoopRef cfRunLoop = [_runLoop getCFRunLoop];
    2.87 +    CFRunLoopWakeUp(cfRunLoop);
    2.88 +#endif
    2.89  }
    2.90  
    2.91  @end
     3.1 --- a/tests/DDRunLoopPokerTest.m	Mon Sep 21 02:06:52 2009 -0500
     3.2 +++ b/tests/DDRunLoopPokerTest.m	Sat Nov 21 17:04:30 2009 -0600
     3.3 @@ -58,11 +58,11 @@
     3.4      NSRunLoop * currentRunLoop = [NSRunLoop currentRunLoop];
     3.5      _poker = [DDRunLoopPoker pokerWithRunLoop:currentRunLoop];
     3.6      
     3.7 -    NSDate * startDate = [NSDate date];
     3.8 +    NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
     3.9      BOOL result = [currentRunLoop runMode:NSDefaultRunLoopMode
    3.10                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.0]];
    3.11      STAssertTrue(result, nil);
    3.12 -    NSTimeInterval waitTime = [startDate timeIntervalSinceNow];
    3.13 +    NSTimeInterval waitTime = [NSDate timeIntervalSinceReferenceDate] - startTime;
    3.14      STAssertEqualsWithAccuracy(waitTime, 0.0, 0.01, nil);
    3.15      
    3.16      [_poker dispose];
    3.17 @@ -84,11 +84,11 @@
    3.18                               toTarget:self
    3.19                             withObject:nil];
    3.20      
    3.21 -    NSDate * startDate = [NSDate date];
    3.22 +    NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
    3.23      BOOL result = [currentRunLoop runMode:NSDefaultRunLoopMode
    3.24                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
    3.25      STAssertTrue(result, nil);
    3.26 -    NSTimeInterval waitTime = [startDate timeIntervalSinceNow];
    3.27 +    NSTimeInterval waitTime = [NSDate timeIntervalSinceReferenceDate] - startTime;
    3.28      STAssertEqualsWithAccuracy(waitTime, 0.0, 0.1, nil);
    3.29      
    3.30      [_poker dispose];