/[lcore]/trunk/btime.pas
ViewVC logotype

Diff of /trunk/btime.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 156 by beware, Sun Feb 23 19:27:13 2020 UTC revision 157 by beware, Fri Aug 13 03:52:55 2021 UTC
# Line 121  Line 121 
121      {$ifdef VER1_0}      {$ifdef VER1_0}
122        linux,        linux,
123      {$else}      {$else}
124          {$ifdef linux}linux,{$endif} //for clock_gettime
125          {$ifdef freebsd}freebsd,{$endif} //for clock_gettime
126        baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions}        baseunix,unix,unixutil,sockets, {unixutil and sockets needed by unixstuff.inc on some compiler versions}
127      {$endif}      {$endif}
     {$ifdef linux}  
       dl,  
     {$endif}  
128    {$else}    {$else}
129      windows,unitsettc,mmsystem,      windows,unitsettc,mmsystem,
130    {$endif}    {$endif}
# Line 181  Line 180 
180    gettimeofday(tv);    gettimeofday(tv);
181    sec := tv.tv_sec;    sec := tv.tv_sec;
182    {$ifndef cpu64}    {$ifndef cpu64}
183    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
184    {$endif}    {$endif}
185    result := sec+(tv.tv_usec/1000000);    result := sec+(tv.tv_usec/1000000);
186  end;  end;
187    
188  {$ifdef linux}  {$ifdef linux}{$define have_clock_gettime}{$endif}
189    {$define monotimefloat_implemented}  {$ifdef freebsd}{$define have_clock_gettime}{$endif}
   const  
     CLOCK_MONOTONIC = 1;  
   type  
     ptimeval = ^ttimeval;  
     tclock_gettime = function(clk_id: integer; tp: ptimeval): integer; cdecl;  
190    
191    var  {$ifdef have_clock_gettime}
192      librt_handle:pointer;    {$define monotimefloat_implemented}
     librt_inited:boolean;  
     clock_gettime: tclock_gettime;  
193    
194    function monotimefloat:float;    function monotimefloat:float;
195    var    var
196      ts: ttimeval;      ts: ttimespec;
197    begin    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  
198        if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin        if clock_gettime(CLOCK_MONOTONIC, @ts) = 0 then begin
199          //note this really returns nanoseconds          //note this really returns nanoseconds
200          result := ts.tv_sec + ts.tv_usec / 1000000000.0;        result := ts.tv_sec + ts.tv_nsec / 1000000000.0;
201          exit;          exit;
202        end;        end;
     end;  
203      //fallback      //fallback
204      result := unixtimefloat;      result := unixtimefloat;
205    end;    end;
206    
207    
208  {$endif} {linux}  {$endif}
209    
210  {$ifdef darwin} {mac OS X}  {$ifdef darwin} {mac OS X}
211  {$define monotimefloat_implemented}  {$define monotimefloat_implemented}

Legend:
Removed from v.156  
changed lines
  Added in v.157

No admin address has been configured">No admin address has been configured
ViewVC Help
Powered by ViewVC 1.1.26