Re: Running run loops in 10.6 (was: Need -[NSTask waitUntilExitOrTimeout:])
Re: Running run loops in 10.6 (was: Need -[NSTask waitUntilExitOrTimeout:])
- Subject: Re: Running run loops in 10.6 (was: Need -[NSTask waitUntilExitOrTimeout:])
- From: Jerry Krinock <email@hidden>
- Date: Sat, 19 Sep 2009 23:38:36 -0700
Trying to find a way to "tickle" a run loop into returning in Mac OS
10.6, I wrote another little tool which, instead of spawning a short-
duration NSTask, sends a message to a mach port which has been added
to the current run loop. To my surprise, result is the same. Running
in Mac OS 10.5, when the message is received, the run loop returns
from runMode:beforeDate:. Running in Mac OS 10.6, it never returns.
Now I understand that there is this new thing called Grand Central
Dispatch with dispatch queues. But the documentation on run loops in
the Threading Programming Guide still says that ports in particular
are input sources to run loops, and the documentation for -
runMode:beforeDate: says that it "returns after ... the first input
source is processed".
Why does -runMode:beforeDate: not return after processing input from a
port in Mac OS 10.6?
Can anyone provide a little code which will cause the following line
to unblock in Mac OS 10.6?
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDatedistantFuture]] ;
Sincerely,
Jerry Krinock
************** New Demo Tool: Mach Ports
#import <Cocoa/Cocoa.h>
@interface MessageSender : NSObject {
}
+ (void)sendMessage:(NSTimer*)timer ;
@end
@implementation MessageSender
+ (void)sendMessage:(NSTimer*)timer {
NSPort* receivePort = [timer userInfo] ;
NSPort* sendPort = [NSMachPort port] ;
NSData* data = [@"Hello" dataUsingEncoding:NSUTF8StringEncoding] ;
NSPortMessage* message = [[NSPortMessage alloc]
initWithSendPort:sendPort
receivePort:receivePort
components:
[NSArray arrayWithObject:data]] ;
BOOL didSend = [message sendBeforeDate:[NSDate
dateWithTimeIntervalSinceNow:0.2]] ;
NSLog(@"Sent message; %@.", didSend ? @"succeeded" : @"failed") ;
[message release] ;
}
@end
int main(int argc, const char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
NSPort* receivePort = [NSMachPort port] ;
[[NSRunLoop currentRunLoop] addPort:receivePort
forMode:NSDefaultRunLoopMode] ;
[NSTimer scheduledTimerWithTimeInterval:1.0
target:[MessageSenderclass]
selector:@selector(sendMessage:)
userInfo:receivePort
repeats:YES] ;
NSLog(@"Will run run loop") ;
NSInteger i = 0 ;
NSInteger msgLimit = 3 ;
while (YES) {
NSLog(@" top of loop") ;
BOOL moreInputSources = [[NSRunLoop currentRunLoop]
runMode:NSDefaultRunLoopMode
beforeDate:
[NSDatedistantFuture]] ;
NSLog(@"Run loop has returned from running") ;
NSLog(@" moreInputSources = %d", moreInputSources) ;
if (!moreInputSources) {
NSLog(@"Breaking because no more input sources.") ;
break ;
}
i++ ;
if (i > 2) {
NSLog(@"Breaking because we've sent %d messages.",
msgLimit) ;
break ;
}
}
NSLog(@"Run loop has quit. Exitting.") ;
[pool release] ;
return 0 ;
}
*********** Log output running in Mac OS 10.5:
TestTool 23:11:22.831 TestTool[236:10b] Will run run loop
TestTool 23:11:22.836 TestTool[236:10b] top of loop
TestTool 23:11:23.832 TestTool[236:10b] Sent message; succeeded.
TestTool 23:11:23.839 TestTool[236:10b] Run loop has returned from
running
TestTool 23:11:23.841 TestTool[236:10b] moreInputSources = 1
TestTool 23:11:23.841 TestTool[236:10b] top of loop
TestTool 23:11:24.832 TestTool[236:10b] Sent message; succeeded.
TestTool 23:11:24.832 TestTool[236:10b] Run loop has returned from
running
TestTool 23:11:24.833 TestTool[236:10b] moreInputSources = 1
TestTool 23:11:24.833 TestTool[236:10b] top of loop
TestTool 23:11:25.832 TestTool[236:10b] Sent message; succeeded.
TestTool 23:11:25.834 TestTool[236:10b] Run loop has returned from
running
TestTool 23:11:25.834 TestTool[236:10b] moreInputSources = 1
TestTool 23:11:25.835 TestTool[236:10b] Breaking because we've sent 3
messages.
TestTool 23:11:25.835 TestTool[236:10b] Run loop has quit. Exitting.
********** Log output running in Mac OS 10.6:
Snow-Leopards-Mac-Pro:~ sl$ /Applications/TestTool
TestTool 22:50:06.485 TestTool[154:903] Will run run loop
TestTool 22:50:06.489 TestTool[154:903] top of loop
TestTool 22:50:07.485 TestTool[154:903] Sent message; succeeded.
TestTool 22:50:08.485 TestTool[154:903] Sent message; succeeded.
TestTool 22:50:09.485 TestTool[154:903] Sent message; succeeded.
TestTool 22:50:10.485 TestTool[154:903] Sent message; succeeded.
TestTool 22:50:11.485 TestTool[154:903] Sent message; succeeded.
...continues forever like this, never exits.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden