X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/ba45339b0cda696438a3b1553c829445fd894641..e2488d880e54b1b938409d3870c28ea952c6d51a:/btime.pas diff --git a/btime.pas b/btime.pas index a0bfc5e..8826a4d 100644 --- a/btime.pas +++ b/btime.pas @@ -121,11 +121,10 @@ uses {$ifdef VER1_0} linux, {$else} + {$ifdef linux}linux,{$endif} //for clock_gettime + {$ifdef freebsd}freebsd,{$endif} //for clock_gettime baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions} {$endif} - {$ifdef linux} - dl, - {$endif} {$else} windows,unitsettc,mmsystem, {$endif} @@ -181,49 +180,32 @@ begin gettimeofday(tv); sec := tv.tv_sec; {$ifndef cpu64} - if (sec < 0) then inc(sec,$100000000); //tv_sec is 32 bits + if (sec < -1) then inc(sec,$100000000); //tv_sec is 32 bits. allow -1 for invalid result {$endif} result := sec+(tv.tv_usec/1000000); end; -{$ifdef linux} - {$define monotimefloat_implemented} - const - CLOCK_MONOTONIC = 1; - type - ptimeval = ^ttimeval; - tclock_gettime = function(clk_id: integer; tp: ptimeval): integer; cdecl; +{$ifdef linux}{$define have_clock_gettime}{$endif} +{$ifdef freebsd}{$define have_clock_gettime}{$endif} - var - librt_handle:pointer; - librt_inited:boolean; - clock_gettime: tclock_gettime; +{$ifdef have_clock_gettime} + {$define monotimefloat_implemented} function monotimefloat:float; var - ts: ttimeval; + ts: ttimespec; begin - if not librt_inited then begin - librt_inited := true; - clock_gettime := nil; - librt_handle := dlopen('librt.so', RTLD_LAZY); - if assigned(librt_handle) then begin - clock_gettime := dlsym(librt_handle, 'clock_gettime'); - end; - end; - if assigned(clock_gettime) then begin - if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin - //note this really returns nanoseconds - result := ts.tv_sec + ts.tv_usec / 1000000000.0; - exit; - end; + if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin + //note this really returns nanoseconds + result := ts.tv_sec + ts.tv_nsec / 1000000000.0; + exit; end; //fallback result := unixtimefloat; end; -{$endif} {linux} +{$endif} {$ifdef darwin} {mac OS X} {$define monotimefloat_implemented} @@ -273,7 +255,7 @@ begin gettimeofday(tv); sec := tv.tv_sec; {$ifndef cpu64} - if (sec < 0) then inc(sec,$100000000); //tv_sec is 32 bits + if (sec < -1) then inc(sec,$100000000); //tv_sec is 32 bits. allow -1 for invalid result {$endif} result := sec; end;