Minor optimisations and bugfixes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@4889 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-09-14 10:03:02 +00:00
parent b9b1e3afef
commit 00ee7cea0c
3 changed files with 111 additions and 74 deletions

View file

@ -1,3 +1,9 @@
Tue Sep 14 11:22:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSTimer.m: minor optimisations
* Source/NSRunLoop.m: minor optimisations, plus bugfix for performers
(was removing performers after they had been fired - shouldn't).
Mon Sep 13 6:45:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Mon Sep 13 6:45:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
Miscellaneous minor bugfixes plus, Miscellaneous minor bugfixes plus,

View file

@ -25,6 +25,7 @@
#include <config.h> #include <config.h>
#include <base/preface.h> #include <base/preface.h>
#include <base/fast.x>
#include <Foundation/NSMapTable.h> #include <Foundation/NSMapTable.h>
#include <Foundation/NSDate.h> #include <Foundation/NSDate.h>
#include <Foundation/NSValue.h> #include <Foundation/NSValue.h>
@ -193,6 +194,7 @@ static NSComparisonResult aSort(GSIArrayItem i0, GSIArrayItem i1)
*/ */
@interface RunLoopPerformer: NSObject <GCFinalization> @interface RunLoopPerformer: NSObject <GCFinalization>
{ {
@public
SEL selector; SEL selector;
id target; id target;
id argument; id argument;
@ -243,11 +245,11 @@ static NSComparisonResult aSort(GSIArrayItem i0, GSIArrayItem i1)
[timer invalidate]; [timer invalidate];
} }
- initWithSelector: (SEL)aSelector - (id) initWithSelector: (SEL)aSelector
target: (id)aTarget target: (id)aTarget
argument: (id)anArgument argument: (id)anArgument
order: (unsigned int)theOrder order: (unsigned int)theOrder
modes: (NSArray*)theModes modes: (NSArray*)theModes
{ {
self = [super init]; self = [super init];
if (self) if (self)
@ -301,18 +303,19 @@ static NSComparisonResult aSort(GSIArrayItem i0, GSIArrayItem i1)
object: (id)arg object: (id)arg
{ {
NSMutableArray *array; NSMutableArray *array;
int i; unsigned i;
RETAIN(target); RETAIN(target);
RETAIN(arg); RETAIN(arg);
array = [[NSRunLoop currentInstance] _timedPerformers]; array = [[NSRunLoop currentInstance] _timedPerformers];
for (i = [array count]; i > 0; i--) i = [array count];
while (i-- > 0)
{ {
if ([[array objectAtIndex: i-1] matchesSelector: aSelector if ([[array objectAtIndex: i] matchesSelector: aSelector
target: target target: target
argument: arg]) argument: arg])
{ {
[array removeObjectAtIndex: i-1]; [array removeObjectAtIndex: i];
} }
} }
RELEASE(arg); RELEASE(arg);
@ -445,24 +448,31 @@ static NSComparisonResult aSort(GSIArrayItem i0, GSIArrayItem i1)
- (void) _checkPerformers - (void) _checkPerformers
{ {
RunLoopPerformer *item; unsigned count = [_performers count];
NSArray *array = [NSArray arrayWithArray: _performers];
int count = [array count];
unsigned pos;
int i;
for (i = 0; i < count; i++) if (count > 0)
{ {
item = (RunLoopPerformer*)[array objectAtIndex: i]; RunLoopPerformer *array[count];
unsigned i;
pos = [_performers indexOfObjectIdenticalTo: item]; [_performers getObjects: array];
if (pos != NSNotFound) /*
* Retain performers in case firing them makes them get removed.
*/
#if GS_WITH_GC == 0
for (i = 0; i < count; i++)
[array[i] retain];
#endif
for (i = 0; i < count; i++)
{ {
if ([[item modes] containsObject: _current_mode]) RunLoopPerformer *item = array[i];
if ([item->modes containsObject: _current_mode])
{ {
[_performers removeObjectAtIndex: pos];
[item fire]; [item fire];
} }
RELEASE(array[i]);
} }
} }
} }
@ -470,39 +480,39 @@ static NSComparisonResult aSort(GSIArrayItem i0, GSIArrayItem i1)
@implementation NSRunLoop(GNUstepExtensions) @implementation NSRunLoop(GNUstepExtensions)
+ currentInstance + (id) currentInstance
{ {
return [self currentRunLoop]; return [self currentRunLoop];
} }
+ (NSString*) currentMode + (NSString*) currentMode
{ {
return [[NSRunLoop currentRunLoop] currentMode]; return [[self currentRunLoop] currentMode];
} }
+ (void) run + (void) run
{ {
[[NSRunLoop currentRunLoop] run]; [[self currentRunLoop] run];
} }
+ (void) runUntilDate: date + (void) runUntilDate: date
{ {
[[NSRunLoop currentRunLoop] runUntilDate: date]; [[self currentRunLoop] runUntilDate: date];
} }
+ (void) runUntilDate: date forMode: (NSString*)mode + (void) runUntilDate: date forMode: (NSString*)mode
{ {
[[NSRunLoop currentRunLoop] runUntilDate: date forMode: mode]; [[self currentRunLoop] runUntilDate: date forMode: mode];
} }
+ (BOOL) runOnceBeforeDate: date + (BOOL) runOnceBeforeDate: date
{ {
return [[NSRunLoop currentRunLoop] runOnceBeforeDate: date]; return [[self currentRunLoop] runOnceBeforeDate: date];
} }
+ (BOOL) runOnceBeforeDate: date forMode: (NSString*)mode + (BOOL) runOnceBeforeDate: date forMode: (NSString*)mode
{ {
return [[NSRunLoop currentRunLoop] runOnceBeforeDate: date forMode: mode]; return [[self currentRunLoop] runOnceBeforeDate: date forMode: mode];
} }
- (void) addEvent: (void*)data - (void) addEvent: (void*)data
@ -704,7 +714,7 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
{ {
static NSString *key = @"NSRunLoopThreadKey"; static NSString *key = @"NSRunLoopThreadKey";
NSMutableDictionary *d; NSMutableDictionary *d;
NSRunLoop* r; NSRunLoop *r;
d = GSCurrentThreadDictionary(); d = GSCurrentThreadDictionary();
r = [d objectForKey: key]; r = [d objectForKey: key];
@ -1078,34 +1088,38 @@ const NSMapTableValueCallBacks ArrayMapValueCallBacks =
/* Do the pre-listening set-up for the file descriptors of this mode. */ /* Do the pre-listening set-up for the file descriptors of this mode. */
{ {
GSIArray watchers; GSIArray watchers;
watchers = NSMapGet(_mode_2_watchers, mode); watchers = NSMapGet(_mode_2_watchers, mode);
if (watchers) { if (watchers)
int i; {
unsigned i = GSIArrayCount(watchers);
for (i = GSIArrayCount(watchers); i > 0; i--) { while (i-- > 0)
RunLoopWatcher *info; {
int fd; RunLoopWatcher *info;
int fd;
info = GSIArrayItemAtIndex(watchers, i-1).obj; info = GSIArrayItemAtIndex(watchers, i).obj;
if (info->_invalidated == YES) { if (info->_invalidated == YES)
GSIArrayRemoveItemAtIndex(watchers, i-1); {
GSIArrayRemoveItemAtIndex(watchers, i);
continue; continue;
} }
switch (info->type) { switch (info->type)
{
case ET_WDESC: case ET_WDESC:
fd = (int)info->data; fd = (int)info->data;
FD_SET (fd, &write_fds); FD_SET (fd, &write_fds);
NSMapInsert(_wfdMap, (void*)fd, info); NSMapInsert(_wfdMap, (void*)fd, info);
num_inputs++; num_inputs++;
break; break;
case ET_RDESC: case ET_RDESC:
fd = (int)info->data; fd = (int)info->data;
FD_SET (fd, &fds); FD_SET (fd, &fds);
NSMapInsert(_rfdMap, (void*)fd, info); NSMapInsert(_rfdMap, (void*)fd, info);
num_inputs++; num_inputs++;
break; break;
case ET_RPORT: case ET_RPORT:
@ -1331,26 +1345,30 @@ id NSDefaultRunLoopMode = @"NSDefaultRunLoopMode";
} }
- (void) cancelPerformSelector: (SEL)aSelector - (void) cancelPerformSelector: (SEL)aSelector
target: target target: (id) target
argument: argument argument: (id) argument
{ {
RunLoopPerformer *item; unsigned count = [_performers count];
int count = [_performers count];
int i;
RETAIN(target); if (count > 0)
RETAIN(argument);
for (i = count; i > 0; i--)
{ {
item = (RunLoopPerformer*)[_performers objectAtIndex: (i-1)]; RunLoopPerformer *array[count];
if ([item matchesSelector: aSelector target: target argument: argument]) [_performers getObjects: array];
RETAIN(target);
RETAIN(argument);
while (count--)
{ {
[_performers removeObjectAtIndex: (i-1)]; if ([array[count] matchesSelector: aSelector
target: target
argument: argument] == YES)
{
[_performers removeObjectAtIndex: count];
}
} }
RELEASE(argument);
RELEASE(target);
} }
RELEASE(argument);
RELEASE(target);
} }
- (void) configureAsServer - (void) configureAsServer
@ -1359,8 +1377,8 @@ id NSDefaultRunLoopMode = @"NSDefaultRunLoopMode";
} }
- (void) performSelector: (SEL)aSelector - (void) performSelector: (SEL)aSelector
target: target target: (id)target
argument: argument argument: (id)argument
order: (unsigned int)order order: (unsigned int)order
modes: (NSArray*)modes modes: (NSArray*)modes
{ {
@ -1379,11 +1397,13 @@ id NSDefaultRunLoopMode = @"NSDefaultRunLoopMode";
} }
else else
{ {
int i; RunLoopPerformer *array[count];
unsigned i;
[_performers getObjects: array];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
if ([[_performers objectAtIndex: i] order] <= order) if (array[i]->order <= order)
{ {
[_performers insertObject: item atIndex: i]; [_performers insertObject: item atIndex: i];
break; break;

View file

@ -28,19 +28,30 @@
#include <Foundation/NSRunLoop.h> #include <Foundation/NSRunLoop.h>
#include <Foundation/NSInvocation.h> #include <Foundation/NSInvocation.h>
@class NSGDate;
static Class NSDate_class;
@implementation NSTimer @implementation NSTimer
+ (void) initialize
{
if (self == [NSTimer class])
{
NSDate_class = [NSGDate class];
}
}
/* This is the designated initializer. */ /* This is the designated initializer. */
- initWithTimeInterval: (NSTimeInterval)seconds - (id) initWithTimeInterval: (NSTimeInterval)seconds
targetOrInvocation: t targetOrInvocation: (id)t
selector: (SEL)sel selector: (SEL)sel
userInfo: info userInfo: info
repeats: (BOOL)f repeats: (BOOL)f
{ {
if (seconds <= 0) if (seconds <= 0)
seconds = 0.01; seconds = 0.001;
_interval = seconds; _interval = seconds;
_date = [[NSDate allocWithZone: [self zone]] _date = [[NSDate_class allocWithZone: [self zone]]
initWithTimeIntervalSinceNow: seconds]; initWithTimeIntervalSinceNow: seconds];
_target = t; _target = t;
_selector = sel; _selector = sel;