Skip to content
Browse files

NSTimer to handle yield loop while there are active coroutines

  • Loading branch information...
1 parent c3cd93a commit d7730b2720db432092c9221c61445bec8499707a @stevedekorte committed Aug 31, 2011
View
1,238 ...xcodeproj/project.xcworkspace/xcuserdata/steve.xcuserdatad/UserInterfaceState.xcuserstate
725 additions, 513 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 ActorKit/ActorKit-Info.plist
@@ -23,7 +23,7 @@
<key>CFBundleVersion</key>
<string>1</string>
<key>NSHumanReadableCopyright</key>
- <string>Copyright © 2011 Steve Dekorte. All rights reserved.</string>
+ <string>Copyright © 2011 Steve Dekorte. BSD licensed.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
View
7 ActorKit/ActorKit.h
@@ -3,9 +3,10 @@
// ActorKit
//
// Created by Steve Dekorte on 20110830.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
-#import "Actor.h"
+#import "Coroutine.h"
#import "Future.h"
-#import "Coroutine.h"
+#import "NSObject+Actor.h"
+#import "NSURLConnection+Coroutine.h"
View
4 ActorKit/Coroutine.h
@@ -3,9 +3,9 @@
// CoroutineKit
//
// Created by Steve Dekorte on 20110830.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
-// I recommend using the Actor object and not this object directly
+// I recommend using the NSObject Actor category instead of this Class directly
#import <Foundation/Foundation.h>
#import "Coro.h"
View
52 ActorKit/Coroutine.m
@@ -3,23 +3,60 @@
// CoroutineKit
//
// Created by Steve Dekorte on 20110830.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
#import "Coroutine.h"
-static Coroutine *mainCoroutine = nil;
-static Coroutine *currentCoroutine = nil;
-static NSMutableArray *scheduledCoroutines = nil;
-
@implementation Coroutine
+static Coroutine *mainCoroutine = nil;
+static Coroutine *currentCoroutine = nil;
+
@synthesize target;
@synthesize action;
@synthesize hasStarted;
@synthesize next;
@synthesize previous;
@synthesize waitingOnFuture;
+static long activeCoroutineCount = 0;
+static NSTimer *activeCoroutineTimer = nil;
+
++ (void)incrementActiveCoroutineCount
+{
+ activeCoroutineCount ++;
+
+ if(!activeCoroutineTimer)
+ {
+ activeCoroutineTimer = [NSTimer timerWithTimeInterval:1.0/30.0
+ target:mainCoroutine
+ selector:@selector(timer:)
+ userInfo:nil
+ repeats:YES];
+ [activeCoroutineTimer retain];
+ }
+
+ if([activeCoroutineTimer isValid])
+ {
+ [activeCoroutineTimer fire];
+ }
+}
+
++ (void)decrementActiveCoroutineCount
+{
+ activeCoroutineCount --;
+
+ if(activeCoroutineCount == 0 && [activeCoroutineTimer isValid])
+ {
+ [activeCoroutineTimer invalidate];
+ }
+}
+
+- (void)timer:userInfo
+{
+ [mainCoroutine yield];
+}
+
- (Coro *)coro
{
return coro;
@@ -39,7 +76,6 @@ + (Coroutine *)mainCoroutine
[mainCoroutine setNext:mainCoroutine];
[mainCoroutine setPrevious:mainCoroutine];
currentCoroutine = mainCoroutine;
- scheduledCoroutines = [[NSMutableArray alloc] init];
}
return mainCoroutine;
@@ -54,12 +90,14 @@ - (id)init
[Coroutine mainCoroutine];
coro = Coro_new();
}
-
+
+ [[self class] incrementActiveCoroutineCount];
return self;
}
- (void)dealloc
{
+ [[self class] decrementActiveCoroutineCount];
Coro_free(coro);
[super dealloc];
}
View
2 ActorKit/Future.h
@@ -3,7 +3,7 @@
// ActorKit
//
// Created by Steve Dekorte on 20110830.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
View
2 ActorKit/Future.m
@@ -3,7 +3,7 @@
// ActorKit
//
// Created by Steve Dekorte on 20110830.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
#import "Future.h"
View
2 ActorKit/NSObject+Actor.h
@@ -3,7 +3,7 @@
// ActorKit
//
// Created by Steve Dekorte on 20110831.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
#import "Coroutine.h"
View
26 ActorKit/NSObject+Actor.m
@@ -3,14 +3,17 @@
// ActorKit
//
// Created by Steve Dekorte on 20110831.
-// Copyright 2011 Steve Dekorte. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
#import "NSObject+Actor.h"
#import <objc/runtime.h>
+
@implementation NSObject (NSObject_Actor)
+static long activeActorCount = 0;
+
- (void)setFirstFuture:(Future *)aFuture
{
objc_setAssociatedObject(self, "firstFuture", aFuture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@@ -21,21 +24,21 @@ - (Future *)firstFuture
return (Future *)objc_getAssociatedObject(self, "firstFuture");
}
-- (void)setCoroutine:(Coroutine *)aCoroutine
+- (void)setActorCoroutine:(Coroutine *)aCoroutine
{
- objc_setAssociatedObject(self, "coroutine", aCoroutine, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(self, "actorCoroutine", aCoroutine, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-- (Coroutine *)coroutine
+- (Coroutine *)actorCoroutine
{
- Coroutine *c = (Coroutine *)objc_getAssociatedObject(self, "coroutine");
+ Coroutine *c = (Coroutine *)objc_getAssociatedObject(self, "actorCoroutine");
if(!c)
{
c = [[[Coroutine alloc] init] autorelease];
[c setTarget:self];
[c setAction:@selector(actorRunLoop)];
- [self setCoroutine:c];
+ [self setActorCoroutine:c];
}
return c;
@@ -64,7 +67,7 @@ - (Future *)futurePerformSelector:(SEL)selector withObject:anObject
[future setSelector:selector];
[future setArgument:anObject];
[[self firstFuture] append:future];
- [[self coroutine] scheduleLast];
+ [[self actorCoroutine] scheduleLast];
return future;
}
@@ -73,14 +76,17 @@ - (void)actorRunLoop
{
while(YES) // coroutines never return, they are only unscheduled
{
- while ([self firstFuture])
+ activeActorCount ++;
+
+ while([self firstFuture])
{
[[self firstFuture] send]; // exceptions are caught within the send method
[self setFirstFuture:[[self firstFuture] nextFuture]];
- [[self coroutine] yield];
+ [[self actorCoroutine] yield];
}
- [[self coroutine] unschedule];
+ activeActorCount --;
+ [[self actorCoroutine] unschedule];
}
}
View
2 NSURLConnection+Coroutine.h
@@ -3,7 +3,7 @@
// ActorKit
//
// Created by Steve Dekorte on 20110831.
-// Copyright 2011 __MyCompanyName__. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
View
8 NSURLConnection+Coroutine.m
@@ -3,7 +3,7 @@
// ActorKit
//
// Created by Steve Dekorte on 20110831.
-// Copyright 2011 __MyCompanyName__. All rights reserved.
+// Copyright 2011 Steve Dekorte. BSD licensed.
//
#import "NSURLConnection+Coroutine.h"
@@ -31,9 +31,9 @@ + (NSDictionary *)sendRequest:(NSURLRequest *)request
[coroutine unschedule]; // pauses coroutine until completionHandler resumes it
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setObject:theResponse forKey:@"response"];
- [dict setObject:theData forKey:@"data"];
- [dict setObject:theError forKey:@"error"];
+ [dict setObject:theResponse forKey:@"response"];
+ [dict setObject:theData forKey:@"data"];
+ [dict setObject:theError forKey:@"error"];
return dict;
}

0 comments on commit d7730b2

Please sign in to comment.
Something went wrong with that request. Please try again.