diff --git a/Headers/gnustep/base/NSRunLoop.h b/Headers/gnustep/base/NSRunLoop.h new file mode 100644 index 000000000..3aced1246 --- /dev/null +++ b/Headers/gnustep/base/NSRunLoop.h @@ -0,0 +1,56 @@ +/* Interface for NSRunLoop for GNUStep + Copyright (C) 1996 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Created: March 1996 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef __NSRunLoop_h_OBJECTS_INCLUDE +#define __NSRunLoop_h_OBJECTS_INCLUDE + +#include + +@class NSString; + +@interface NSRunLoop : NSObject +@end + +/* Put this in a category to avoid unimportant errors due to behaviors. */ +@interface NSRunLoop (GNUstep) + +/* Getting this thread's current run loop */ ++ (NSRunLoop*) currentRunLoop; +- (NSString*) currentMode; +- (NSDate*) limitDateForMode: (NSString*)mode + +/* Adding timers. */ +- (void) addTimer: (NSTimer*)timer forMode: (NSString*)mode; + +/* Running a run loop. */ +- (void) acceptInputForMode: (NSString*)mode beforeDate: (NSDate*)limit_date; +- (void) run; +- (BOOL) runMode: (NSString*)mode beforeDate: (NSDate*)limit_date; +- (void) runUntilDate: (NSDate*)limit_date; + +@end + +extern NSString *NSDefaultRunLoopMode; +extern NSString *NSConnectionReplyMode; + +#endif /*__NSRunLoop_h_OBJECTS_INCLUDE */ diff --git a/Source/NotificationQueue.m b/Source/NotificationQueue.m new file mode 100644 index 000000000..a0c5733f2 --- /dev/null +++ b/Source/NotificationQueue.m @@ -0,0 +1,133 @@ +/* Implementation of object for queuing Notification objects + Copyright (C) 1996 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Created: March 1996 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* The implementation for NotificationDispatcher. */ + +#include +#include +#include +#include +#include +#include +#include + +@implementation NotificationQueue + +/* This is the default initialzer. */ +- initWithNotificationDistributor: nd +{ + [super init]; + _notification_distributor = nd; + _asap_queue = [Queue new]; + _idle_queue = [Queue new]; + return self; +} + +- init +{ + return [self initWithNotificationDistributor: + [NotificationDistributor defaultInstance]]; +} + +- (void) dequeueNotificationsMatching: notification + coalesceMask: (unsigned)mask +{ + /* Remove from the queues notifications that match NOTIFICATION, according + the the MASK. */ + void remove_matching_from_queue (id q) + { + int i; + for (i = [q count] - 1; i >= 0; i--) + { + id n = [q objectAtIndex: i]; + /* If the aspects that need to match do match... */ + if (( !(mask & NSNotificationCoalescingOnName) + || [[notification name] isEqual: [n name]]) + && + ( !(mask & NSNotificationCoalescingOnSender) + || [[notification object] isEqual: [n object]])) + /* ...then remove it. */ + [q removeObjectAtIndex: i]; + } + } + remove_matching_from_queue (_asap_queue); + remove_matching_from_queue (_idle_queue); +} + +- (void) enqueueNotification: notification + postingStyle: (NSPostingStyle)style + coalesceMask: (unsigned)mask + forModes: (id )modes +{ + [self dequeueNotificationsMatching: notification + coalesceMask: mask]; + + switch (style) + { + case NSPostIdle: + [_idle_queue enqueueObject: notification]; + break; + case NSPostASAP: + [_asap_queue enqueueObject: notification]; + break; + case NSPostNow: + if ([modes containsObject: [RunLoop mode]]) + [_notification_distributor postNotification: notification]; + else + /* xxx This is the correct behavior? */ + [_asap_queue enqueueObject: notification]; + break; + default: + [self error: "Bad posting style."]; + } +} + +- (void) enqueueNotification: notification + postingStyle: (NSPostingStyle)style +{ + [self enqueueNotification: notification + postingStyle: style + coalesceMask: (NSNotificationCoalescingOnName + | NSNotificationCoalescingOnSender) + modes: nil]; +} + + +- (void) postNotificationsWithASAPStyle +{ + id n; + int i, c = [_asap_queue count]; + + for (i = 0; i < c; i++) + [_notification_dispatcher postNotification: + [_idle_queue objectAtIndex: i]]; + [_asap_queue empty]; +} + +- (void) postNotificationWithIdleStyle +{ + id n = [_idle_queue dequeueObject]; + [_notification_dispatcher postNotification: n]; +} + +@end diff --git a/Testing/nstimer.m b/Testing/nstimer.m new file mode 100644 index 000000000..aa1693f7d --- /dev/null +++ b/Testing/nstimer.m @@ -0,0 +1,57 @@ +#include +#include +#include + +@interface TestDouble : NSObject ++ (double) testDouble; +- (double) testDoubleInstance; +@end +@implementation TestDouble ++ (double) testDouble +{ + return 12345678912345.0; +} +- (double) testDoubleInstance +{ + return 92345678912345.0; +} +@end + +double test_double () +{ + return 92345678912345.0; +} + +void say_count () +{ + static int count = 0; + printf ("Timer fired %d times\n", ++count); +} + +int main() +{ + volatile double foo, bar; + id inv = [[VoidFunctionInvocation alloc] initWithVoidFunction: say_count]; + id o; + id d; + + foo = [TestDouble testDouble]; + printf ("TestDouble is %f\n", foo); + foo = [TestDouble testDouble]; + printf ("TestDouble 2 is %f\n", foo); + o = [[TestDouble alloc] init]; + bar = [o testDoubleInstance]; + printf ("testDouble is %f\n", bar); + + foo = test_double (); + printf ("test_double is %f\n", foo); + + d = [NSDate date]; + printf ("time interval since now %f\n", [d timeIntervalSinceNow]); + + [NSTimer scheduledTimerWithTimeInterval: 3.0 + invocation: inv + repeats: YES]; + [RunLoop run]; + exit (0); +}