[Mono-list] Bug in Environment.TickCount
Jeffrey Stedfast
fejj at novell.com
Fri Jul 20 01:27:48 EDT 2007
n/m, that's not quite right... CLOCKS_PER_SEC is incorrect...
try this:
#include <sys/times.h>
#include <inttypes.h>
#include <limits.h>
#include <unistd.h>
static uint32_t initial_ticks;
static uint32_t ticks_per_msec;
static uint32_t ticks_per_sec;
static void
TickCountInit (void)
{
ticks_per_sec = sysconf (_SC_CLK_TCK);
ticks_per_msec = ticks_per_sec > 1000 ? ticks_per_sec / 1000 : 1;
#if defined (__linux__) /* FIXME: && kernel > 2.6.0 */
initial_ticks = (UINT_MAX - 300) * ticks_per_sec;
#else
initial_ticks = 0;
#endif
}
static uint32_t
GetTickCount (void)
{
struct tms tms;
clock_t ticks;
if ((ticks = times (&tms)) == (clock_t) -1)
return 0;
ticks -= initial_ticks;
return (uint32_t) ticks / ticks_per_msec;
}
this gives 1/100th of a second accuracy +/-, so not quite millisecond
accuracy but... eh.
Jeff
On Fri, 2007-07-20 at 00:23 -0400, Jeffrey Stedfast wrote:
> from what I can tell reading the man page... you want to do this:
>
> guint32 GetTickCount (void)
> {
> struct tms tms;
> clock_t ticks;
>
> if ((ticks = times (&tms)) == -1)
> return 0;
>
> #ifdef LINUX_KERNEL_2_6_0_OR_LATER
> ticks -= (UINT_MAX - 300) * CLOCKS_PER_SEC;
> #endif
>
> #define MSEC_PER_SEC 1000
> #define CLOCKS_PER_MSEC (CLOCKS_PER_SEC / MSEC_PER_SEC)
>
> return (guint32) (ticks / CLOCKS_PER_MSEC);
> }
>
> On Thu, 2007-07-19 at 22:38 -0300, Thiago M. Sayão wrote:
> > Hello!
> >
> > Enrironment.TickCount is supposed to return the miliseconds since the
> > boot. For my surprise it was returning a negative number which should
> > only happen on the 47th day of uptime.
> >
> > I did i fix (attached) but it doesn't seem 100% correct and i am not
> > sure if it would work after some days.
> >
> > Since i'm coding a msn library and msn p2p protocol uses GetTickCount(),
> > it's messing things for me!
> >
> > Can anyone take a look?
> >
> > Thanks!
> >
> > _______________________________________________
> > Mono-list maillist - Mono-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
More information about the Mono-list
mailing list