datetime review part 2

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 26 05:59:58 PST 2010


On Friday 26 November 2010 04:38:47 Michel Fortin wrote:
> On 2010-11-26 03:03:15 -0500, Jonathan M Davis <jmdavisProg at gmx.com> said:
> > Most recent code: http://is.gd/hO85C
> > 
> > I added code for Mac OS X which should use its monotonic clock functions.
> > Unfortunately, as I don't own a Mac, it's completed untested, but in
> > theory, what's there should work, and someone with a Mac can fix it
> > later if need be.
> 
> Just tested it. There's an error in the definition of
> mach_timebase_info (takes a mach_timebase_info_t* argument, but it
> should be mach_timebase_info_t, no pointer) and the
> mach_timebase_info_t alias is missing. Here's a fixed version of the
> definitions:
> 
> 	extern(C)
> 	{
> 		struct mach_timebase_info_data_t
> 		{
> 		    uint numer;
> 		    uint denom;
> 		}
> 		alias mach_timebase_info_data_t* mach_timebase_info_t;
> 
> 		kern_return_t mach_timebase_info(mach_timebase_info_t);
> 
> 		ulong mach_absolute_time();
> 	}
> 
> I tested it with this small program:
> 
> 	extern(C) void sleep(uint sec);
> 
> 	void main()
> 	{
> 	    auto start = TickDuration.currSystemTick;
> 	    sleep(20);
> 	    auto end = TickDuration.currSystemTick;
> 	    writeln(to!string(end-start));
> 	}
> 
> And this is the result (more or less a few thousands on each run):
> 
> 	TickDuration(20000046269)
> 
> So it seems all good.

Thanks! It's hard enough to test for Windows, but doing any Mac testing is 
essentially impossible for me.

> One thing I wonder about... what is the expected behaviour if you put
> the computer to sleep in the middle of the above program? With
> mach_absolute_time, the clock stops counting while the computer is put
> to sleep. Does TickDuration.currSystemTick works like that on all
> systems? If there's a difference in behaviour perhaps it should be
> documented.

Really? That is... not good. Unacceptable in fact. Even if it were acceptable 
when dealing with stop watch and benchmarking code (which is questionable), it 
totally fries Clock.currAppTick(), which tells you how long the application has 
been running. The documentation for QueryPermanceCounter() and clock_gettime() 
give no indication that the clock stops counting when a thread is sleeping.

Do you know if there's another way to get a monotonic clock on the Mac - one 
which isn't affected by sleep? Much as I'd hate to do it, it would be better to 
use gettimeofday() and forgo the monotonic clock then have a monotonic clock 
which is affected by sleep. But I'd _really_ like to have a monotonic clock if I 
can.

- Jonathan M Davis


More information about the Digitalmars-d mailing list