home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-osprim.adb < prev    next >
Text File  |  2000-07-19  |  6KB  |  171 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                  S Y S T E M . O S _ P R I M I T I V E S                 --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.5 $                             --
  10. --                                                                          --
  11. --             Copyright (C) 1998 Free Software Foundation, Inc.            --
  12. --                                                                          --
  13. -- GNARL is free software; you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNARL; see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNARL was developed by the GNARL team at Florida State University. It is --
  32. -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
  33. -- State University (http://www.gnat.com).                                  --
  34. --                                                                          --
  35. ------------------------------------------------------------------------------
  36.  
  37. --  This is the OS/2 version of this package
  38.  
  39. with Interfaces.C;                      use Interfaces.C;
  40. with Interfaces.OS2Lib;                 use Interfaces.OS2Lib;
  41. with Interfaces.OS2Lib.Synchronization; use Interfaces.OS2Lib.Synchronization;
  42.  
  43. package body System.OS_Primitives is
  44.  
  45.    ----------------
  46.    -- Local Data --
  47.    ----------------
  48.  
  49.    Epoch_Offset    : Duration;       --  See Set_Epoch_Offset
  50.    Max_Tick_Count  : QWORD := 0.0;
  51.    --  This is needed to compensate for small glitches in the
  52.    --  hardware clock or the way it is read by the OS
  53.  
  54.    -----------------------
  55.    -- Local Subprograms --
  56.    -----------------------
  57.  
  58.    procedure Set_Epoch_Offset;
  59.    --  Initializes the Epoch_1970_Offset to the offset of the System_Clock
  60.    --  relative to the Unix epoch (Jan 1, 1970), such that
  61.    --     Clock = System_Clock + Epoch_1970_Offset
  62.  
  63.    function System_Clock return Duration;
  64.    pragma Inline (System_Clock);
  65.    --  Function returning value of system clock with system-dependent timebase.
  66.    --  For OS/2 the system clock returns the elapsed time since system boot.
  67.    --  The clock resolution is approximately 838 ns.
  68.  
  69.    ------------------
  70.    -- System_Clock --
  71.    ------------------
  72.  
  73.    function System_Clock return Duration is
  74.  
  75.       --  Implement conversion from tick count to Duration
  76.       --  using fixed point arithmetic. The frequency of
  77.       --  the Intel 8254 timer chip is 18.2 * 2**16 Hz.
  78.  
  79.       Tick_Duration : constant := 1.0 / (18.2 * 2**16);
  80.       Tick_Count    : aliased QWORD;
  81.  
  82.    begin
  83.       Must_Not_Fail (DosTmrQueryTime (Tick_Count'Access));
  84.       --  Read nr of clock ticks since boot time
  85.  
  86.       Max_Tick_Count := QWORD'Max (Tick_Count, Max_Tick_Count);
  87.  
  88.       return Max_Tick_Count * Tick_Duration;
  89.    end System_Clock;
  90.  
  91.    -----------
  92.    -- Clock --
  93.    -----------
  94.  
  95.    function Clock return Duration is
  96.    begin
  97.       return System_Clock + Epoch_Offset;
  98.    end Clock;
  99.  
  100.    ----------------------
  101.    -- Set_Epoch_Offset --
  102.    ----------------------
  103.  
  104.    procedure Set_Epoch_Offset is
  105.  
  106.       --  Interface to Unix C style gettimeofday
  107.  
  108.       type timeval is record
  109.          tv_sec  : long;
  110.          tv_usec : long;
  111.       end record;
  112.  
  113.       procedure gettimeofday
  114.         (time : access timeval;
  115.          zone : System.Address := System.Address'Null_Parameter);
  116.       pragma Import (C, gettimeofday);
  117.  
  118.       Time_Of_Day       : aliased timeval;
  119.       Micro_To_Nano     : constant := 1.0E3;
  120.       Sec_To_Nano       : constant := 1.0E9;
  121.       Nano_To_Sec       : constant := 1.0 / 1.0E9;
  122.       Nanos_Since_Epoch : QWORD;
  123.  
  124.    begin
  125.       gettimeofday (Time_Of_Day'Access);
  126.       Nanos_Since_Epoch := QWORD (Time_Of_Day.tv_sec) * Sec_To_Nano
  127.         + QWORD (Time_Of_Day.tv_usec) * Micro_To_Nano;
  128.  
  129.       Epoch_Offset :=
  130.          Duration'(Nanos_Since_Epoch / Sec_To_Nano) - System_Clock;
  131.  
  132.    end Set_Epoch_Offset;
  133.  
  134.    -----------------
  135.    -- Timed_Delay --
  136.    -----------------
  137.  
  138.    procedure Timed_Delay
  139.      (Time : Duration;
  140.       Mode : Integer)
  141.    is
  142.       Rel_Time   : Duration;
  143.       Abs_Time   : Duration;
  144.       Check_Time : Duration := Clock;
  145.  
  146.    begin
  147.       if Mode = Relative then
  148.          Rel_Time := Time;
  149.          Abs_Time := Time + Check_Time;
  150.       else
  151.          Rel_Time := Time - Check_Time;
  152.          Abs_Time := Time;
  153.       end if;
  154.  
  155.       if Rel_Time > 0.0 then
  156.          loop
  157.             Must_Not_Fail (DosSleep (ULONG (Rel_Time * 1000.0)));
  158.  
  159.             Check_Time := Clock;
  160.  
  161.             exit when Abs_Time <= Check_Time;
  162.  
  163.             Rel_Time := Abs_Time - Check_Time;
  164.          end loop;
  165.       end if;
  166.    end Timed_Delay;
  167.  
  168. begin
  169.    Set_Epoch_Offset;
  170. end System.OS_Primitives;
  171.