| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . S O C K E T S . P O L L . W A I T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT 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 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 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 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Wait implementation on top of posix select call |
| 33 |
|
| 34 |
with GNAT.Sockets.Poll.G_Wait; |
| 35 |
|
| 36 |
separate (GNAT.Sockets.Poll) |
| 37 |
|
| 38 |
procedure Wait |
| 39 |
(Fds : in out Set; Timeout : Interfaces.C.int; Result : out Integer) |
| 40 |
is |
| 41 |
use Interfaces; |
| 42 |
|
| 43 |
function Get_Max_FD return FD_Type; |
| 44 |
-- Check is Max_FD is actual and correct it if necessary |
| 45 |
|
| 46 |
type FD_Set_Type is array (0 .. Get_Max_FD / C.long'Size) of C.long |
| 47 |
with Convention => C; |
| 48 |
|
| 49 |
procedure Reset_Socket_Set (Set : in out FD_Set_Type); |
| 50 |
-- Use own FD_ZERO routine because FD_Set_Type size depend on Fds.Max_FD |
| 51 |
|
| 52 |
procedure Insert_Socket_In_Set (Set : in out FD_Set_Type; FD : FD_Type) |
| 53 |
with Import, Convention => C, |
| 54 |
External_Name => "__gnat_insert_socket_in_set"; |
| 55 |
|
| 56 |
function Is_Socket_In_Set (Set : FD_Set_Type; FD : FD_Type) return C.int |
| 57 |
with Import, Convention => C, |
| 58 |
External_Name => "__gnat_is_socket_in_set"; |
| 59 |
|
| 60 |
procedure Reset_Socket_Set (Set : in out FD_Set_Type) is |
| 61 |
begin |
| 62 |
Set := (others => 0); |
| 63 |
end Reset_Socket_Set; |
| 64 |
|
| 65 |
procedure Poll is new G_Wait |
| 66 |
(FD_Set_Type, Reset_Socket_Set, Insert_Socket_In_Set, Is_Socket_In_Set); |
| 67 |
|
| 68 |
---------------- |
| 69 |
-- Get_Max_FD -- |
| 70 |
---------------- |
| 71 |
|
| 72 |
function Get_Max_FD return FD_Type is |
| 73 |
begin |
| 74 |
if not Fds.Max_OK then |
| 75 |
Fds.Max_FD := Fds.Fds (Fds.Fds'First).Socket; |
| 76 |
|
| 77 |
for J in Fds.Fds'First + 1 .. Fds.Length loop |
| 78 |
if Fds.Max_FD < Fds.Fds (J).Socket then |
| 79 |
Fds.Max_FD := Fds.Fds (J).Socket; |
| 80 |
end if; |
| 81 |
end loop; |
| 82 |
|
| 83 |
Fds.Max_OK := True; |
| 84 |
end if; |
| 85 |
|
| 86 |
return Fds.Max_FD; |
| 87 |
end Get_Max_FD; |
| 88 |
|
| 89 |
begin |
| 90 |
Poll (Fds, Timeout, Result); |
| 91 |
end Wait; |