Fixed task notification problem in nsrunloop

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@10219 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2001-06-21 04:49:20 +00:00
parent 70d49d8d1d
commit ddbf0da92f
4 changed files with 120 additions and 22 deletions

View file

@ -1,3 +1,10 @@
2001-06-21 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSRunLoop.m: ([runMode:beforeDate:]) Check for task
completion and send out notifications if required.
* Source/NSTask.m: ([waitUntilExit]) schedule a timer so that the
run loop waits rather than polling as fast as the CPU allows.
2001-06-20 Richard Frith-Macdonald <rfm@gnu.org> 2001-06-20 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSTask.m: handleSignal() ... reset signal handler for * Source/NSTask.m: handleSignal() ... reset signal handler for

View file

@ -50,6 +50,7 @@
static int debug_run_loop = 0; static int debug_run_loop = 0;
static NSDate *theFuture = nil; static NSDate *theFuture = nil;
extern BOOL GSCheckTasks();
/* /*
@ -1016,7 +1017,6 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
- (void) acceptInputForMode: (NSString*)mode - (void) acceptInputForMode: (NSString*)mode
beforeDate: limit_date beforeDate: limit_date
{ {
extern BOOL GSCheckTasks();
NSTimeInterval ti; NSTimeInterval ti;
struct timeval timeout; struct timeval timeout;
void *select_timeout; void *select_timeout;
@ -1059,6 +1059,7 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
{ {
/* The LIMIT_DATE has already past; return immediately without /* The LIMIT_DATE has already past; return immediately without
polling any inputs. */ polling any inputs. */
GSCheckTasks();
[self _checkPerformers]; [self _checkPerformers];
GSNotifyASAP(); GSNotifyASAP();
if (debug_run_loop) if (debug_run_loop)
@ -1352,6 +1353,13 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
{ {
printf ("\tNSRunLoop run mode with date already past\n"); printf ("\tNSRunLoop run mode with date already past\n");
} }
/*
* Notify if any tasks have completed.
*/
if (GSCheckTasks() == YES)
{
GSNotifyASAP();
}
return NO; return NO;
} }
@ -1363,6 +1371,13 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
{ {
printf ("\tNSRunLoop run mode with nothing to do\n"); printf ("\tNSRunLoop run mode with nothing to do\n");
} }
/*
* Notify if any tasks have completed.
*/
if (GSCheckTasks() == YES)
{
GSNotifyASAP();
}
return NO; return NO;
} }

View file

@ -515,6 +515,8 @@ pty_slave(const char* name)
- (void) waitUntilExit - (void) waitUntilExit
{ {
NSTimer *timer = nil;
while ([self isRunning]) while ([self isRunning])
{ {
NSDate *limit; NSDate *limit;
@ -523,10 +525,19 @@ pty_slave(const char* name)
* Poll at 0.1 second intervals. * Poll at 0.1 second intervals.
*/ */
limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 0.1]; limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 0.1];
if (timer = nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
target: nil
selector: @selector(class)
userInfo: nil
repeats: YES];
}
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate: limit]; beforeDate: limit];
RELEASE(limit); RELEASE(limit);
} }
[timer invalidate];
} }
@end @end

View file

@ -1,27 +1,92 @@
#include <Foundation/Foundation.h>
#include <stdio.h>
#import <Foundation/Foundation.h>
#if 1 @interface TaskMan : NSObject
int main ()
{ {
id pool = [NSAutoreleasePool new]; NSMutableArray *taskList;
id o = [NSObject new];
printf ("Hello from object at 0x%x\n", (unsigned)[o self]);
exit (0);
} }
#else
int main (int argc, char **argv) -nextTask:(NSNotification *) aNotification;
@end
@implementation TaskMan
-init
{ {
NSString *string; NSTask *aTask;
id pool = [NSAutoreleasePool new];
NSProcessInfo *info = [NSProcessInfo processInfo]; self = [super init];
NSUserDefaults *defaults;
[[NSNotificationCenter defaultCenter] addObserver:self
NSLog(@"Temporary directory - %@", NSTemporaryDirectory()); selector:@selector(nextTask:)
[info setProcessName: @"TestProcess"]; name:NSTaskDidTerminateNotification
defaults = [NSUserDefaults standardUserDefaults]; object:nil];
NSLog(@"%@", [defaults dictionaryRepresentation]);
return 0; taskList = [[NSMutableArray alloc] init];
aTask = [[NSTask alloc] init];
[aTask setLaunchPath:@"/bin/ls"];
[aTask setArguments:nil];
[taskList addObject:aTask];
aTask = [[NSTask alloc] init];
[aTask setLaunchPath:@"/bin/ps"];
[aTask setArguments:nil];
[taskList addObject:aTask];
aTask = [[NSTask alloc] init];
[aTask setLaunchPath:@"/bin/pwd"];
[aTask setArguments:nil];
[taskList addObject:aTask];
aTask = [[NSTask alloc] init];
[aTask setLaunchPath:@"/bin/date"];
[aTask setArguments:nil];
[taskList addObject:aTask];
[[taskList objectAtIndex:0] launch];
return self;
} }
#endif
-nextTask:(NSNotification *) aNotification
{
if ([[aNotification object] terminationStatus] == 0) {
[NSNotification notificationWithName:@"CommandCompletedSuccessfully"
object:self];
} else {
[NSNotification notificationWithName:@"CommandFailed"
object:self];
}
[taskList removeObjectAtIndex:0];
if ([taskList count] > 0)
[[taskList objectAtIndex:0] launch];
else
exit(0);
return self;
}
@end
int main(int argc, char **argv, char** env)
{
NSAutoreleasePool *pool;
TaskMan *aTaskMan;
int i = 0;
pool = [NSAutoreleasePool new];
aTaskMan = [[TaskMan alloc] init];
while(1) {
[[NSRunLoop currentRunLoop] runOnceBeforeDate:
[NSDate dateWithTimeIntervalSinceNow: 5]];
/* Uncomment the following line, and the app will complete all tasks */
/* otherwise it will hang */
//printf("%d\n", i++);
// NSLog(@"");
}
exit(0);
}