* doc/install.texi (*-*-aix): Update explanation of XLC bootstrap.
[official-gcc.git] / gcc / ada / g-socthi-vxworks.adb
bloba35e429fbb209d06850a6b6b5c47c624f847659d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . S O C K E T S . T H I N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2009, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This package provides a target dependent thin interface to the sockets
35 -- layer for use by the GNAT.Sockets package (g-socket.ads). This package
36 -- should not be directly with'ed by an applications program.
38 -- This version is for VxWorks
40 with GNAT.OS_Lib; use GNAT.OS_Lib;
41 with GNAT.Task_Lock;
43 with Interfaces.C; use Interfaces.C;
45 package body GNAT.Sockets.Thin is
47 Non_Blocking_Sockets : aliased Fd_Set;
48 -- When this package is initialized with Process_Blocking_IO set
49 -- to True, sockets are set in non-blocking mode to avoid blocking
50 -- the whole process when a thread wants to perform a blocking IO
51 -- operation. But the user can also set a socket in non-blocking
52 -- mode by purpose. In order to make a difference between these
53 -- two situations, we track the origin of non-blocking mode in
54 -- Non_Blocking_Sockets. If S is in Non_Blocking_Sockets, it has
55 -- been set in non-blocking mode by the user.
57 Quantum : constant Duration := 0.2;
58 -- When SOSC.Thread_Blocking_IO is False, we set sockets in
59 -- non-blocking mode and we spend a period of time Quantum between
60 -- two attempts on a blocking operation.
62 Unknown_System_Error : constant C.Strings.chars_ptr :=
63 C.Strings.New_String ("Unknown system error");
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 -- All these require comments ???
71 function Syscall_Accept
72 (S : C.int;
73 Addr : System.Address;
74 Addrlen : not null access C.int) return C.int;
75 pragma Import (C, Syscall_Accept, "accept");
77 function Syscall_Connect
78 (S : C.int;
79 Name : System.Address;
80 Namelen : C.int) return C.int;
81 pragma Import (C, Syscall_Connect, "connect");
83 function Syscall_Ioctl
84 (S : C.int;
85 Req : C.int;
86 Arg : access C.int) return C.int;
87 pragma Import (C, Syscall_Ioctl, "ioctl");
89 function Syscall_Recv
90 (S : C.int;
91 Msg : System.Address;
92 Len : C.int;
93 Flags : C.int) return C.int;
94 pragma Import (C, Syscall_Recv, "recv");
96 function Syscall_Recvfrom
97 (S : C.int;
98 Msg : System.Address;
99 Len : C.int;
100 Flags : C.int;
101 From : System.Address;
102 Fromlen : not null access C.int) return C.int;
103 pragma Import (C, Syscall_Recvfrom, "recvfrom");
105 function Syscall_Recvmsg
106 (S : C.int;
107 Msg : System.Address;
108 Flags : C.int) return C.int;
109 pragma Import (C, Syscall_Recvmsg, "recvmsg");
111 function Syscall_Sendmsg
112 (S : C.int;
113 Msg : System.Address;
114 Flags : C.int) return C.int;
115 pragma Import (C, Syscall_Sendmsg, "sendmsg");
117 function Syscall_Sendto
118 (S : C.int;
119 Msg : System.Address;
120 Len : C.int;
121 Flags : C.int;
122 To : System.Address;
123 Tolen : C.int) return C.int;
124 pragma Import (C, Syscall_Sendto, "sendto");
126 function Syscall_Socket
127 (Domain : C.int;
128 Typ : C.int;
129 Protocol : C.int) return C.int;
130 pragma Import (C, Syscall_Socket, "socket");
132 function Non_Blocking_Socket (S : C.int) return Boolean;
133 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
135 --------------
136 -- C_Accept --
137 --------------
139 function C_Accept
140 (S : C.int;
141 Addr : System.Address;
142 Addrlen : not null access C.int) return C.int
144 R : C.int;
145 Val : aliased C.int := 1;
147 Res : C.int;
148 pragma Unreferenced (Res);
150 begin
151 loop
152 R := Syscall_Accept (S, Addr, Addrlen);
153 exit when SOSC.Thread_Blocking_IO
154 or else R /= Failure
155 or else Non_Blocking_Socket (S)
156 or else Errno /= SOSC.EWOULDBLOCK;
157 delay Quantum;
158 end loop;
160 if not SOSC.Thread_Blocking_IO
161 and then R /= Failure
162 then
163 -- A socket inherits the properties of its server especially
164 -- the FIONBIO flag. Do not use C_Ioctl as this subprogram
165 -- tracks sockets set in non-blocking mode by user.
167 Set_Non_Blocking_Socket (R, Non_Blocking_Socket (S));
168 Res := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
169 -- Is it OK to ignore result ???
170 end if;
172 return R;
173 end C_Accept;
175 ---------------
176 -- C_Connect --
177 ---------------
179 function C_Connect
180 (S : C.int;
181 Name : System.Address;
182 Namelen : C.int) return C.int
184 Res : C.int;
186 begin
187 Res := Syscall_Connect (S, Name, Namelen);
189 if SOSC.Thread_Blocking_IO
190 or else Res /= Failure
191 or else Non_Blocking_Socket (S)
192 or else Errno /= SOSC.EINPROGRESS
193 then
194 return Res;
195 end if;
197 declare
198 WSet : aliased Fd_Set;
199 Now : aliased Timeval;
200 begin
201 Reset_Socket_Set (WSet'Access);
202 loop
203 Insert_Socket_In_Set (WSet'Access, S);
204 Now := Immediat;
205 Res := C_Select
206 (S + 1,
207 No_Fd_Set_Access,
208 WSet'Access,
209 No_Fd_Set_Access,
210 Now'Unchecked_Access);
212 exit when Res > 0;
214 if Res = Failure then
215 return Res;
216 end if;
218 delay Quantum;
219 end loop;
220 end;
222 Res := Syscall_Connect (S, Name, Namelen);
224 if Res = Failure
225 and then Errno = SOSC.EISCONN
226 then
227 return Thin_Common.Success;
228 else
229 return Res;
230 end if;
231 end C_Connect;
233 -------------
234 -- C_Ioctl --
235 -------------
237 function C_Ioctl
238 (S : C.int;
239 Req : C.int;
240 Arg : access C.int) return C.int
242 begin
243 if not SOSC.Thread_Blocking_IO
244 and then Req = SOSC.FIONBIO
245 then
246 if Arg.all /= 0 then
247 Set_Non_Blocking_Socket (S, True);
248 end if;
249 end if;
251 return Syscall_Ioctl (S, Req, Arg);
252 end C_Ioctl;
254 ------------
255 -- C_Recv --
256 ------------
258 function C_Recv
259 (S : C.int;
260 Msg : System.Address;
261 Len : C.int;
262 Flags : C.int) return C.int
264 Res : C.int;
266 begin
267 loop
268 Res := Syscall_Recv (S, Msg, Len, Flags);
269 exit when SOSC.Thread_Blocking_IO
270 or else Res /= Failure
271 or else Non_Blocking_Socket (S)
272 or else Errno /= SOSC.EWOULDBLOCK;
273 delay Quantum;
274 end loop;
276 return Res;
277 end C_Recv;
279 ----------------
280 -- C_Recvfrom --
281 ----------------
283 function C_Recvfrom
284 (S : C.int;
285 Msg : System.Address;
286 Len : C.int;
287 Flags : C.int;
288 From : System.Address;
289 Fromlen : not null access C.int) return C.int
291 Res : C.int;
293 begin
294 loop
295 Res := Syscall_Recvfrom (S, Msg, Len, Flags, From, Fromlen);
296 exit when SOSC.Thread_Blocking_IO
297 or else Res /= Failure
298 or else Non_Blocking_Socket (S)
299 or else Errno /= SOSC.EWOULDBLOCK;
300 delay Quantum;
301 end loop;
303 return Res;
304 end C_Recvfrom;
306 ---------------
307 -- C_Recvmsg --
308 ---------------
310 function C_Recvmsg
311 (S : C.int;
312 Msg : System.Address;
313 Flags : C.int) return ssize_t
315 Res : C.int;
317 begin
318 loop
319 Res := Syscall_Recvmsg (S, Msg, Flags);
320 exit when SOSC.Thread_Blocking_IO
321 or else Res /= Failure
322 or else Non_Blocking_Socket (S)
323 or else Errno /= SOSC.EWOULDBLOCK;
324 delay Quantum;
325 end loop;
327 return ssize_t (Res);
328 end C_Recvmsg;
330 ---------------
331 -- C_Sendmsg --
332 ---------------
334 function C_Sendmsg
335 (S : C.int;
336 Msg : System.Address;
337 Flags : C.int) return ssize_t
339 Res : C.int;
341 begin
342 loop
343 Res := Syscall_Sendmsg (S, Msg, Flags);
344 exit when SOSC.Thread_Blocking_IO
345 or else Res /= Failure
346 or else Non_Blocking_Socket (S)
347 or else Errno /= SOSC.EWOULDBLOCK;
348 delay Quantum;
349 end loop;
351 return ssize_t (Res);
352 end C_Sendmsg;
354 --------------
355 -- C_Sendto --
356 --------------
358 function C_Sendto
359 (S : C.int;
360 Msg : System.Address;
361 Len : C.int;
362 Flags : C.int;
363 To : System.Address;
364 Tolen : C.int) return C.int
366 Res : C.int;
368 begin
369 loop
370 Res := Syscall_Sendto (S, Msg, Len, Flags, To, Tolen);
371 exit when SOSC.Thread_Blocking_IO
372 or else Res /= Failure
373 or else Non_Blocking_Socket (S)
374 or else Errno /= SOSC.EWOULDBLOCK;
375 delay Quantum;
376 end loop;
378 return Res;
379 end C_Sendto;
381 --------------
382 -- C_Socket --
383 --------------
385 function C_Socket
386 (Domain : C.int;
387 Typ : C.int;
388 Protocol : C.int) return C.int
390 R : C.int;
391 Val : aliased C.int := 1;
393 Res : C.int;
394 pragma Unreferenced (Res);
396 begin
397 R := Syscall_Socket (Domain, Typ, Protocol);
399 if not SOSC.Thread_Blocking_IO
400 and then R /= Failure
401 then
402 -- Do not use C_Ioctl as this subprogram tracks sockets set
403 -- in non-blocking mode by user.
405 Res := Syscall_Ioctl (R, SOSC.FIONBIO, Val'Access);
406 -- Is it OK to ignore result ???
407 Set_Non_Blocking_Socket (R, False);
408 end if;
410 return R;
411 end C_Socket;
413 --------------
414 -- Finalize --
415 --------------
417 procedure Finalize is
418 begin
419 null;
420 end Finalize;
422 -------------------------
423 -- Host_Error_Messages --
424 -------------------------
426 package body Host_Error_Messages is separate;
428 ----------------
429 -- Initialize --
430 ----------------
432 procedure Initialize is
433 begin
434 Reset_Socket_Set (Non_Blocking_Sockets'Access);
435 end Initialize;
437 -------------------------
438 -- Non_Blocking_Socket --
439 -------------------------
441 function Non_Blocking_Socket (S : C.int) return Boolean is
442 R : Boolean;
443 begin
444 Task_Lock.Lock;
445 R := (Is_Socket_In_Set (Non_Blocking_Sockets'Access, S) /= 0);
446 Task_Lock.Unlock;
447 return R;
448 end Non_Blocking_Socket;
450 -----------------------------
451 -- Set_Non_Blocking_Socket --
452 -----------------------------
454 procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean) is
455 begin
456 Task_Lock.Lock;
457 if V then
458 Insert_Socket_In_Set (Non_Blocking_Sockets'Access, S);
459 else
460 Remove_Socket_From_Set (Non_Blocking_Sockets'Access, S);
461 end if;
463 Task_Lock.Unlock;
464 end Set_Non_Blocking_Socket;
466 --------------------
467 -- Signalling_Fds --
468 --------------------
470 package body Signalling_Fds is separate;
472 --------------------------
473 -- Socket_Error_Message --
474 --------------------------
476 function Socket_Error_Message
477 (Errno : Integer) return C.Strings.chars_ptr
479 use type Interfaces.C.Strings.chars_ptr;
481 C_Msg : C.Strings.chars_ptr;
483 begin
484 C_Msg := C_Strerror (C.int (Errno));
486 if C_Msg = C.Strings.Null_Ptr then
487 return Unknown_System_Error;
489 else
490 return C_Msg;
491 end if;
492 end Socket_Error_Message;
494 end GNAT.Sockets.Thin;