OS_clock.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
class OS_TIMEVAL
class OS_TIMEVAL is
-- This class defines the two integer components of the underlying
-- operating system clock - as indices of an integer array. This enables
-- using classes to find the time and access the individual components.
-- This version is defined for Posix conformant unix implementations!
-- Reference should be made to Posix specifications for further detail.
-- Version 1.2 Mar 01. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 20 Apr 97 kh Original for portability using Unix
-- 28 Oct 99 kh Revised for new library.
-- 4 Mar 01 kh Re-written for linux-posix conformance
include AREF{INT} ;
const seconds : CARD := 0 ;
const microseconds : CARD := 1 ;
create : SAME is
-- This is the sole feature provided directly in this implementation class - others qre inherited!
-- return new(2)
return SAME::create(2);
end ;
end ; -- OS_TIMEVAL
immutable class OS_TIME < $OPSYS_TIME
immutable class OS_TIME < $OPSYS_TIME is
-- This class implements the operating system standard real-time clock
-- facilities -- producing an OS-independent time object (precision and
-- accuracy may vary!).
-- This version is defined for Posix conformant unix implementations!
-- Reference should be made to Posix specifications for further detail.
-- Version 1.1 Jul 97. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 10 Dec 96 kh Original from distribution library
-- 24 Jul 97 kh Complete revision for independence.
const Base_Year : CARD := 1970 ;
const Base_Wday : WEEKDAYS := WEEKDAYS::Thursday ;
readonly attr days : CARD ; -- whole day count from base date
readonly attr millisecs : CARD ; -- so far this day
const Ticks_per_Millisec : CARD := 1 ;
const Microseconds_per_Millisec : CARD := 1000 ;
const Millisecs_per_Second : CARD := 1000 ;
const Ticks_per_Second : CARD := Millisecs_per_Second * Ticks_per_Millisec ;
const Seconds_per_Min : CARD := 60 ;
const Mins_per_Hour : CARD := 60 ;
const Hours_per_Day : CARD := 24 ;
const Seconds_per_Day : CARD := Seconds_per_Min * Mins_per_Hour *
Hours_per_Day ;
const Ticks_per_Day : CARD := Seconds_per_Day * Ticks_per_Second ;
read_possible : BOOL is
-- This function returns TRUE iff it is possible to obtain time/date
-- information from a system clock, otherwise FALSE.
return true
end ;
set_possible : BOOL is
-- This function returns TRUE iff it is possible to set the time of
-- the system time/date clock, otherwise FALSE.
return OPSYS::is_super_user
end ;
create(
ticks : CARD
) : SAME is
-- This routine takes an OS clock count and, using the known base-date
-- defined by the system, returns the compound object.
loc_days : CARD := ticks / Ticks_per_Day ;
return days(loc_days).millisecs(
(ticks % Ticks_per_Day) / Ticks_per_Millisec)
end ;
create(
day : CARD,
millisecs : CARD
) : SAME is
-- This routine takes an OS clock count and returns the compound object.
return days(day).millisecs(millisecs) ;
end ;
time_stamp : SAME
pre read_possible
post true
is
-- This procedure returns the current date/time as known by the
-- system clock. The date/time returned depends upon the setting of the
-- clock - and may be void if some system problem occurred! Note that in
-- Win32 'unix' lib the gettimeofday function may only be used for fractions
-- of a second since it is the monotonic time clock rather than the
-- (synchronised) time of day clock value available by using the 'time'
-- routine.
ticks : OS_TIMEVAL := OS_TIMEVAL::create ;
success : INT := OS_TIMES::get_timeofday(ticks.array_ptr,void) ;
if success = INT::zero then
secs : CARD := ticks[ticks.seconds].card ;
micros : CARD := ticks[ticks.microseconds].card ;
loc_days : CARD := secs / Seconds_per_Day ;
loc_millis : CARD := (secs % Seconds_per_Day) * Millisecs_per_Second
+ micros / Microseconds_per_Millisec ;
return days(loc_days).millisecs(loc_millis)
else
return void
end
end ;
convert(
fs_time_stamp : OS_FSTIME,
ticks_per_unit : CARD
) : SAME is
-- This routine takes a filing system time stamp together with the
-- number of ticks per filesystem time unit and returns the compound object
-- using the standard clock day/millisec units.
loc_days : CARD := fs_time_stamp.units /
(Ticks_per_Day / ticks_per_unit) ;
remaining_units : CARD := fs_time_stamp.units %
(Ticks_per_Day / ticks_per_unit) ;
loc_millis : CARD := remaining_units *
ticks_per_unit / Ticks_per_Millisec ;
return days(loc_days).millisecs(loc_millis)
end ;
create_fstime(
days : CARD,
millis : CARD,
ticks_per_unit : CARD
) : OS_FSTIME is
-- This routine takes the given day and millisecond count and the number
-- of ticks per file system time unit, converting to a 'file system' time.
loc_units : CARD := (days * (Ticks_per_Day / ticks_per_unit)) +
((millis * Ticks_per_Millisec) / ticks_per_unit) ;
return OS_FSTIME::units(loc_units).sub_units(0)
end ;
set_clock(
new_time : SAME
) : BOOL
pre set_possible
post true
is
-- If the pre-condition is satisfied then this routine sets the time of
-- day clock to the given value, returning true unless some unexpected system
-- problem occurred!
ticks : OS_TIMEVAL := OS_TIMEVAL::create ;
ticks[ticks.seconds] := (new_time.days * Seconds_per_Day +
new_time.millisecs / Millisecs_per_Second).int ;
ticks[ticks.microseconds] := ((new_time.millisecs % Millisecs_per_Second)
* Ticks_per_Millisec).int ;
return (OS_TIMES::set_timeofday(ticks.array_ptr,void) = INT::zero)
end ;
adjust_clock(
offset : INT -- in ticks!
) : BOOL
pre set_possible
post result
is
-- This routine returns true if and only if it has been possible to
-- alter the value of the system clock by the given number of milliseconds,
-- otherwise false -- due to a failure in setting.
ticks : OS_TIMEVAL := OS_TIMEVAL::create ;
ticks[ticks.seconds] := offset / Ticks_per_Second.int ;
ticks[ticks.microseconds] := offset % Ticks_per_Second.int ;
return (OS_TIMES::adjust_time(ticks.array_ptr,void) = INT::zero)
end ;
count : CARD is
-- This routine converts the time-stamp to an Operating System specific
-- count of ticks.
return days * Ticks_per_Day + millisecs * Ticks_per_Millisec
end ;
pause(
secs : CARD
) : CARD is
-- This routine provides the program with a facility to pause execution
-- by a number of seconds. The value returned is either zero if the whole
-- pause time has elapsed - or some positive value indicating that some
-- interrupt has woken the process.
return OS_TIMES::sa_pause(secs)
end ;
end ; -- OS_TIME
immutable class OS_HIRES_TIME
immutable class OS_HIRES_TIME is
-- This class models the second/nanosecond variety of time as known
-- on many Unix systems -- two 'integer' values! Note that this is usually
-- a monotonic timer, not necessarily related to the calendar/time!
-- This version is defined for Posix conformant unix implementations!
-- Reference should be made to Posix specifications for further detail.
-- Version 1.2 Apr 97. Copyright K Hopper,U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 10 Jun 96 kh Original
-- 19 Feb 97 kh Additions for string/char portability.
-- 10 Apr 97 kh Modified for INT to CARD, etc
const Base_Year : CARD := OS_TIME::Base_Year ;
readonly attr day : CARD ; -- days from start of base year.
readonly attr seconds : CARD ; -- seconds since start of day.
readonly attr nanoseconds : CARD ; -- nanoseconds since start of sec.
const Ticks_per_Second : CARD := 1000000 ; -- gives true resolution!
const Millisecs_per_Second : CARD := 1000 ;
const Microsecs_per_Millisec : CARD := 1000 ;
const Nanoseconds_per_Microsec : CARD := 1000 ;
create(
day : CARD,
secs : CARD,
nano : CARD
) : SAME is
-- This routine takes an OS clock count and, using the known base-date
-- defined by the system, returns the compound object. Not available!!!
return day(days).seconds(secs).nanoseconds(nano)
end ;
hires_time_stamp : SAME
pre read_possible
post true
is
-- This procedure returns the current date/time in microseconds as
-- known by the system time of day clock. The date/time returned depends
-- upon the setting of the clock - and may be void if some system problem
-- occurred!
ticks : OS_TIMEVAL := OS_TIMEVAL::create ;
success : INT := OS_TIMES::get_timeofday(ticks.array_ptr,void) ;
if success = 0 then
secs : CARD := ticks[ticks.seconds].card ;
micros : CARD := ticks[ticks.microseconds].card ;
return day(secs / Seconds_per_Day).seconds(
secs % Seconds_per_Day).nanoseconds(
micros * Nanosecs_per_Microsec)
else
return void
end
end ;
time_stamp : OS_TIME
pre ~void(self)
post true
is
-- This routine returns the time stamp which is the nearest
-- approximation to the high resolution value.
if void then
return void
end ;
millis : CARD := seconds * Millisecs_per_Second +
nanoseconds / Microseconds_per_Millisec ;
if (nanoseconds % Microseconds_per_Millisec) >
(Microseconds_per_Millisec / 2) then
millis := millis + 1
end ;
return OS_TIME::create(day,millis)
end ;
adjust(
offset : SAME
) : BOOL
pre OPSYS::is_super_user
post result
is
-- This routine returns true if and only if it has been possible to
-- adjust the time of day clock by the amount given in operating system
-- dependent ticks.
ticks : OS_TIMEVAL := OS_TIMEVAL::create ;
ticks[ticks.seconds] := offset / Ticks_per_Second ;
ticks[ticks.microseconds] := offset % Ticks_per_Second ;
return (adjust_time(ticks.array_ptr,void) = INT::zero)
end ;
end ; -- OS_HIRES_TIME
external C class OS_TIMES is
-- This external class provides the interface to the underlying
-- operating system clock and timer facilities.
-- This version is defined for Posix conformant unix implementations!
-- Reference should be made to Posix specifications for further detail.
-- Version 1.0 Apr 97. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 20 Apr 97 kh Original for portability using Unix
-- The following refer to the high-resolution clock (if any)
-- provided by the operating system. This has a tuple with two
-- cardinal components giving seconds and microseconds.
get_timeofday(
hrclock : REFERENCE,
tzdata : REFERENCE -- should now be null always
) : INT ;
-- This routine sets its arguments to have the current value of the
-- operating system high-resolution clock. This routine returns zero if
-- the operation was successful.
set_timeofday(
hrclock : REFERENCE,
tzdata : REFERENCE -- should now be null always
) : INT ;
-- This routine sets the high resolution clock to have the value
-- of the arguments. This routine returns a negative value if this was
-- not possible, otherwise zero.
adjust_time(
delta : REFERENCE,
old_delta : REFERENCE
) : INT ;
-- This routine adjusts the operatinmg system time of day clock by the
-- given delta. If old_delta is not void then it is given the amount of
-- adjustment not yet completed. This permits time adjustment to be
-- smoothed over a short period.
-- The following refer to the low-resolution clock (if any)
-- provided by the operating system.
os_clock : INT ;
-- This routine returns the clock value in seconds since the base-date
-- defined by the operating system interface library.
os_time(
val : REFERENCE
) : INT ;
-- This routine returns zero if it has successfully set the argument to
-- the clock value in seconds since the base-date defined by the operating
-- system interface library.
-- The following refers to processor usage by self/children.
-- The 'usage' is a tuple of four CARDs (see OS_TIME_USAGE).
p_times(
usage : REFERENCE
) : CARD ;
-- This is a wrapper for the Posix routine called times -- which
-- conflicts with the Sather standard arithmetic routine of that name!
sa_pause(
secs : CARD
) : CARD ;
-- This is the standard Posix sleep operation. It guarrantees that
-- the whole process running will be suspended for at least secs seconds.
end ; -- OS_TIMES