Commits

Peter Hosey  committed d60818d

Update all views in a single CATransaction on the main thread to minimize CA overhead.

  • Participants
  • Parent commits 95939ed
  • Branches core-animation

Comments (0)

Files changed (1)

File CPUUsageMonitor.m

 
 - (void)layOutCellsInTarget:(id)target;
 
-//Main thread.
-- (void)updateCPUUsageButNotViews:(NSTimer *)timer;
-- (void)startViewUpdateThreads;
-//Threaded.
-- (void)threadedLaunchTimer:(NSNumber *)CPUIndexNum;
+- (void)updateCPUUsage:(NSTimer *)timer;
 
 @end
 
 
 	updateTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
 													target:self
-												  selector:@selector(updateCPUUsageButNotViews:)
+												  selector:@selector(updateCPUUsage:)
 												  userInfo:nil
 												   repeats:YES] retain];
 	[updateTimer fire];
 
-	[self performSelector:@selector(startViewUpdateThreads)
-			   withObject:nil
-			   afterDelay:1.];
-
 	if(shouldDrawToWindow)
 		[window orderFront:nil];
 }
 
-- (void)startViewUpdateThreads {
-	//Launch the threaded timers that sweep our CPU usage array looking for views.
-	for(unsigned i = 0U; i < numCPUs; ++i) {
-		[NSThread detachNewThreadSelector:@selector(threadedLaunchTimer:)
-								 toTarget:self
-							   withObject:[NSNumber numberWithUnsignedInt:i]];
-	}
-}	
-
 - (void)applicationWillTerminate:(NSNotification *)notification {
 	//First, wait for our resize animation to finish.
 	if (windowResizeAnimation) {
 
 #pragma mark Timer
 
-- (void)threadedUpdateCPUUsageView:(NSTimer *)timer {
-	unsigned i = [[timer userInfo] unsignedIntValue];
-
-	CPUUsageView *view = [usageViews objectAtIndex:i];
-	CPUUsageView *viewInDockIcon = [dockIconUsageViews objectAtIndex:i];
-
-	[CPUUsageLock lock];
-	[view setCPUUsage:CPUUsage[i]];
-	[viewInDockIcon setCPUUsage:CPUUsage[i]];
-	[CPUUsageLock unlock];
-
-	if(threadsRemainingToDie) {
-		[timer invalidate];
-
-		[deathLock lock];
-		--threadsRemainingToDie;
-		[deathLock unlock];
-	}
-}
-- (void)threadedLaunchTimer:(NSNumber *)CPUIndexNum {
-	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
-	NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
-
-	NSTimer *timer = [NSTimer timerWithTimeInterval:0.5
-											 target:self
-										   selector:@selector(threadedUpdateCPUUsageView:)
-										   userInfo:CPUIndexNum //userInfo
-											repeats:YES];
-	[timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
-
-	[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
-	[runLoop run];
-
-	[pool release];
-}
-
 //Main thread.
-- (void)updateCPUUsageButNotViews:(NSTimer *)timer {
+- (void)updateCPUUsage:(NSTimer *)timer {
 	natural_t numProcessors_nobodyCares = 0U;
 
 	kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numProcessors_nobodyCares, &processorInfo, &numProcessorInfo);
 	if(err == KERN_SUCCESS) {
 		[CPUUsageLock lock];
+		[CATransaction begin];
 
 		for(unsigned i = 0U; i < numCPUs; ++i) {
 			//We only want the last $REFRESH_TIME seconds' worth of data, not all time.
 			}
 
 			CPUUsage[i] = inUse / total;
+
+			CPUUsageView *view = [usageViews objectAtIndex:i];
+			CPUUsageView *viewInDockIcon = [dockIconUsageViews objectAtIndex:i];
+			[view setCPUUsage:CPUUsage[i]];
+			[viewInDockIcon setCPUUsage:CPUUsage[i]];
 		}
 
+		[CATransaction commit];
 		[CPUUsageLock unlock];
 
 		if(lastProcessorInfo) {