File : s-osprim-unix.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                 GNAT 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 --          Copyright (C) 1998-2015, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNARL is free software; you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  22 -- You should have received a copy of the GNU General Public License and    --
  23 -- a copy of the GCC Runtime Library Exception along with this program;     --
  24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  25 -- <http://www.gnu.org/licenses/>.                                          --
  26 --                                                                          --
  27 -- GNARL was developed by the GNARL team at Florida State University.       --
  28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 --  This version uses gettimeofday and select
  33 --  This file is suitable for OpenNT, Dec Unix and SCO UnixWare.
  34 
  35 package body System.OS_Primitives is
  36 
  37    --  ??? These definitions are duplicated from System.OS_Interface
  38    --  because we don't want to depend on any package. Consider removing
  39    --  these declarations in System.OS_Interface and move these ones in
  40    --  the spec.
  41 
  42    type struct_timeval is record
  43       tv_sec  : Integer;
  44       tv_usec : Integer;
  45    end record;
  46    pragma Convention (C, struct_timeval);
  47 
  48    procedure gettimeofday
  49      (tv : not null access struct_timeval;
  50       tz : Address := Null_Address);
  51    pragma Import (C, gettimeofday, "gettimeofday");
  52 
  53    procedure C_select
  54      (n         : Integer := 0;
  55       readfds,
  56       writefds,
  57       exceptfds : Address := Null_Address;
  58       timeout   : not null access struct_timeval);
  59    pragma Import (C, C_select, "select");
  60 
  61    -----------
  62    -- Clock --
  63    -----------
  64 
  65    function Clock return Duration is
  66       TV : aliased struct_timeval;
  67 
  68    begin
  69       gettimeofday (TV'Access);
  70       return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
  71    end Clock;
  72 
  73    -----------------
  74    -- Timed_Delay --
  75    -----------------
  76 
  77    procedure Timed_Delay
  78      (Time : Duration;
  79       Mode : Integer)
  80    is
  81       Rel_Time   : Duration;
  82       Abs_Time   : Duration;
  83       Base_Time  : constant Duration := Clock;
  84       Check_Time : Duration := Base_Time;
  85       timeval    : aliased struct_timeval;
  86 
  87    begin
  88       if Mode = Relative then
  89          Rel_Time := Time;
  90          Abs_Time := Time + Check_Time;
  91       else
  92          Rel_Time := Time - Check_Time;
  93          Abs_Time := Time;
  94       end if;
  95 
  96       if Rel_Time > 0.0 then
  97          loop
  98             timeval.tv_sec := Integer (Rel_Time);
  99 
 100             if Duration (timeval.tv_sec) > Rel_Time then
 101                timeval.tv_sec := timeval.tv_sec - 1;
 102             end if;
 103 
 104             timeval.tv_usec :=
 105               Integer ((Rel_Time - Duration (timeval.tv_sec)) * 10#1#E6);
 106 
 107             C_select (timeout => timeval'Unchecked_Access);
 108             Check_Time := Clock;
 109 
 110             exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
 111 
 112             Rel_Time := Abs_Time - Check_Time;
 113          end loop;
 114       end if;
 115    end Timed_Delay;
 116 
 117    ----------------
 118    -- Initialize --
 119    ----------------
 120 
 121    procedure Initialize is
 122    begin
 123       null;
 124    end Initialize;
 125 
 126 end System.OS_Primitives;