X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/0ac6b47e810e6efaebcdbf3075b633e9b4109341..e2488d880e54b1b938409d3870c28ea952c6d51a:/ltimevalstuff.pas diff --git a/ltimevalstuff.pas b/ltimevalstuff.pas old mode 100755 new mode 100644 index bf9767e..7a10c7a --- a/ltimevalstuff.pas +++ b/ltimevalstuff.pas @@ -3,41 +3,53 @@ which is included in the package ----------------------------------------------------------------------------- } +{$ifdef fpc} + {$mode delphi} +{$endif} + unit ltimevalstuff; interface - -{$ifdef win32} - type - ttimeval = record - tv_sec : longint; - tv_usec : longint; - end; -{$else} - {$ifdef ver1_0} - uses linux; - {$else} - uses baseunix,unix; - {$endif} -{$endif} - + +{$ifdef mswindows} + type + ttimeval = record + tv_sec : longint; + tv_usec : longint; + end; +{$else} + {$ifdef ver1_0} + uses linux; + {$else} + uses + {$ifdef linux}linux,{$endif} //for clock_gettime + {$ifdef freebsd}freebsd,{$endif} //for clock_gettime + baseunix,unix,unixutil,sockets; + {$endif} +{$endif} + procedure tv_add(var tv:ttimeval;msec:integer); function tv_compare(const tv1,tv2:ttimeval):boolean; -procedure tv_substract(var tv:ttimeval;const tv2:ttimeval); +procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval); procedure msectotimeval(var tv:ttimeval;msec:integer); - + +{$ifdef unix} +//for internal use by lcore as a replacement for gettimeofday -beware +procedure gettimemonotonic(var tv:ttimeval); +{$endif} + //tv_invalidtimebig will always compare as greater than any valid timeval -//unfortunately unixstuff.inc hasn't worked it's magic yet so we +//unfortunately unixstuff.inc hasn't worked it's magic yet so we //have to ifdef this manually. const - {$ifdef ver1_0} + {$ifdef ver1_0} tv_invalidtimebig : ttimeval = (sec:maxlongint;usec:maxlongint); - {$else} - tv_invalidtimebig : ttimeval = (tv_sec:maxlongint;tv_usec:maxlongint); - {$endif} + {$else} + tv_invalidtimebig : ttimeval = (tv_sec:maxlongint;tv_usec:maxlongint); + {$endif} implementation - -{$i unixstuff.inc} + +{$i unixstuff.inc} {add nn msec to tv} procedure tv_add(var tv:ttimeval;msec:integer); @@ -55,7 +67,7 @@ begin end else result := tv1.tv_sec > tv2.tv_sec; end; -procedure tv_substract(var tv:ttimeval;const tv2:ttimeval); +procedure tv_subtract(var tv:ttimeval;const tv2:ttimeval); begin dec(tv.tv_usec,tv2.tv_usec); if tv.tv_usec < 0 then begin @@ -71,4 +83,24 @@ begin tv.tv_usec := (msec mod 1000)*1000; end; -end. \ No newline at end of file + +{$ifdef unix} +{$ifdef linux}{$define have_clock_gettime}{$endif} +{$ifdef freebsd}{$define have_clock_gettime}{$endif} + +procedure gettimemonotonic(var tv:ttimeval); +var + ts:ttimespec; +begin + {$ifdef have_clock_gettime} + if (clock_gettime(CLOCK_MONOTONIC, @ts) = 0) then begin + tv.tv_sec := ts.tv_sec; + tv.tv_usec := ts.tv_nsec div 1000; + exit; + end; + {$endif} + gettimeofday(tv); +end; +{$endif} + +end.