'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 22 May 2009 at 2:09:39 pm'!
!classDefinition: #WorldState category: #'Morphic-OldWorlds'!
Object subclass: #WorldState
	instanceVariableNames: 'hands viewBox canvas damageRecorder stepList lastStepTime lastStepMessage lastCycleTime alarms lastAlarmTime remoteServer drawingFailingMorphs waitDelay'
	classVariableNames: 'CanSurrenderToOS DeferredUIMessages DisableDeferredUpdates LastCycleTime MinCycleLapse'
	poolDictionaries: ''
	category: 'Morphic-OldWorlds'!

!Delay methodsFor: 'private' stamp: 'jmv 5/22/2009 13:43'!
setDelay: millisecondCount 
	"Private!! Initialize this delay to signal the given semaphore after the given number of milliseconds."

	delayDuration _ millisecondCount
! !


!WorldState methodsFor: 'update cycle' stamp: 'jmv 5/22/2009 13:48'!
interCyclePause: milliSecs
	"delay enough that the previous cycle plus the amount of delay will equal milliSecs.  If the cycle is already expensive, then no delay occurs.  However, if the system is idly waiting for interaction from the user, the method will delay for a proportionally long time and cause the overall CPU usage of Squeak to be low."

	| currentTime wait |
	waitDelay ifNil: [ waitDelay _ Delay forMilliseconds: 50 ].
	Preferences serverMode
		ifFalse: [
			(lastCycleTime notNil and: [CanSurrenderToOS ~~ false]) ifTrue: [ 
				currentTime _ Time millisecondClockValue.
				wait _ lastCycleTime + milliSecs - currentTime.
		 		(wait > 0 and: [ wait <= milliSecs ] )
				ifTrue: [
					waitDelay setDelay: wait; wait ] ] ]
		ifTrue: [ waitDelay wait ].

	lastCycleTime _  Time millisecondClockValue.
	CanSurrenderToOS _ true.! !

