OSDN Git Service

2008-04-08 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osinte-lynxos-3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
5 --                   S Y S T E M . O S _ I N T E R F A C E                  --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --          Copyright (C) 1999-2007, 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 2,  or (at your option) any later ver- --
14 -- sion. GNARL 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 GNARL; 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 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This is a LynxOS (Native) version of this package
35
36 pragma Polling (Off);
37 --  Turn off polling, we do not want ATC polling to take place during
38 --  tasking operations. It causes infinite loops and other problems.
39
40 package body System.OS_Interface is
41
42    use Interfaces.C;
43
44    -------------------
45    -- clock_gettime --
46    -------------------
47
48    function clock_gettime
49      (clock_id : clockid_t;
50       tp       : access timespec)
51       return     int
52    is
53       function clock_gettime_base
54         (clock_id : clockid_t;
55          tp       : access timespec)
56          return     int;
57       pragma Import (C, clock_gettime_base, "clock_gettime");
58
59    begin
60       if clock_gettime_base (clock_id, tp) /= 0 then
61          return errno;
62       end if;
63
64       return 0;
65    end clock_gettime;
66
67    -----------------
68    -- To_Duration --
69    -----------------
70
71    function To_Duration (TS : timespec) return Duration is
72    begin
73       return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9;
74    end To_Duration;
75
76    ------------------------
77    -- To_Target_Priority --
78    ------------------------
79
80    function To_Target_Priority
81      (Prio : System.Any_Priority) return Interfaces.C.int
82    is
83    begin
84       return Interfaces.C.int (Prio);
85    end To_Target_Priority;
86
87    -----------------
88    -- To_Timespec --
89    -----------------
90
91    function To_Timespec (D : Duration) return timespec is
92       S : time_t;
93       F : Duration;
94
95    begin
96       S := time_t (Long_Long_Integer (D));
97       F := D - Duration (S);
98
99       --  If F has negative value due to a round-up, adjust for positive F
100       --  value.
101
102       if F < 0.0 then
103          S := S - 1;
104          F := F + 1.0;
105       end if;
106
107       return timespec'(tv_sec => S,
108         tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
109    end To_Timespec;
110
111    -------------------------
112    -- POSIX.1c  Section 3 --
113    -------------------------
114
115    function sigwait
116      (set :  access sigset_t;
117       sig :  access Signal)
118       return int
119    is
120       function sigwait_base
121         (set   : access sigset_t;
122          value : System.Address)
123          return  Signal;
124       pragma Import (C, sigwait_base, "sigwait");
125
126    begin
127       sig.all := sigwait_base (set, Null_Address);
128
129       if sig.all = -1 then
130          return errno;
131       end if;
132
133       return 0;
134    end sigwait;
135
136    --------------------------
137    -- POSIX.1c  Section 11 --
138    --------------------------
139
140    --  For all the following functions, LynxOS threads has the POSIX Draft 4
141    --  begavior; it sets errno but the standard Posix requires it to be
142    --  returned.
143
144    function pthread_mutexattr_init
145      (attr : access pthread_mutexattr_t)
146       return int
147    is
148       function pthread_mutexattr_create
149         (attr : access pthread_mutexattr_t)
150          return int;
151       pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
152
153    begin
154       if pthread_mutexattr_create (attr) /= 0 then
155          return errno;
156       end if;
157
158       return 0;
159    end pthread_mutexattr_init;
160
161    function pthread_mutexattr_destroy
162      (attr : access pthread_mutexattr_t)
163       return int
164    is
165       function pthread_mutexattr_delete
166         (attr : access pthread_mutexattr_t)
167          return int;
168       pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
169
170    begin
171       if pthread_mutexattr_delete (attr) /= 0 then
172          return errno;
173       end if;
174
175       return 0;
176    end pthread_mutexattr_destroy;
177
178    function pthread_mutex_init
179      (mutex : access pthread_mutex_t;
180       attr  : access pthread_mutexattr_t)
181       return  int
182    is
183       function pthread_mutex_init_base
184         (mutex : access pthread_mutex_t;
185          attr  : pthread_mutexattr_t)
186          return  int;
187       pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
188
189    begin
190       if pthread_mutex_init_base (mutex, attr.all) /= 0 then
191          return errno;
192       end if;
193
194       return 0;
195    end pthread_mutex_init;
196
197    function pthread_mutex_destroy
198      (mutex : access pthread_mutex_t)
199       return  int
200    is
201       function pthread_mutex_destroy_base
202         (mutex : access pthread_mutex_t)
203          return  int;
204       pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
205
206    begin
207       if pthread_mutex_destroy_base (mutex) /= 0 then
208          return errno;
209       end if;
210
211       return 0;
212    end pthread_mutex_destroy;
213
214    function pthread_mutex_lock
215      (mutex : access pthread_mutex_t)
216       return  int
217    is
218       function pthread_mutex_lock_base
219         (mutex : access pthread_mutex_t)
220          return  int;
221       pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
222
223    begin
224       if pthread_mutex_lock_base (mutex) /= 0 then
225          return errno;
226       end if;
227
228       return 0;
229    end pthread_mutex_lock;
230
231    function pthread_mutex_unlock
232      (mutex : access pthread_mutex_t)
233       return  int
234    is
235       function pthread_mutex_unlock_base
236         (mutex : access pthread_mutex_t)
237          return  int;
238       pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
239
240    begin
241       if pthread_mutex_unlock_base (mutex) /= 0 then
242          return errno;
243       end if;
244
245       return 0;
246    end pthread_mutex_unlock;
247
248    function pthread_condattr_init
249      (attr : access pthread_condattr_t)
250       return int
251    is
252       function pthread_condattr_create
253         (attr : access pthread_condattr_t)
254          return int;
255       pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
256
257    begin
258       if pthread_condattr_create (attr) /= 0 then
259          return errno;
260       end if;
261
262       return 0;
263    end pthread_condattr_init;
264
265    function pthread_condattr_destroy
266      (attr : access pthread_condattr_t)
267       return int
268    is
269       function pthread_condattr_delete
270         (attr : access pthread_condattr_t)
271          return int;
272       pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
273
274    begin
275       if pthread_condattr_delete (attr) /= 0 then
276          return errno;
277       end if;
278
279       return 0;
280    end pthread_condattr_destroy;
281
282    function pthread_cond_init
283      (cond : access pthread_cond_t;
284       attr : access pthread_condattr_t)
285       return int
286    is
287       function pthread_cond_init_base
288         (cond : access pthread_cond_t;
289          attr : pthread_condattr_t)
290          return int;
291       pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
292
293    begin
294       if pthread_cond_init_base (cond, attr.all) /= 0 then
295          return errno;
296       end if;
297
298       return 0;
299    end pthread_cond_init;
300
301    function pthread_cond_destroy
302      (cond : access pthread_cond_t)
303       return int
304    is
305       function pthread_cond_destroy_base
306         (cond : access pthread_cond_t)
307          return int;
308       pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
309
310    begin
311       if pthread_cond_destroy_base (cond) /= 0 then
312          return errno;
313       end if;
314
315       return 0;
316    end pthread_cond_destroy;
317
318    function pthread_cond_signal
319      (cond : access pthread_cond_t)
320       return int
321    is
322       function pthread_cond_signal_base
323         (cond : access pthread_cond_t)
324          return int;
325       pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
326
327    begin
328       if pthread_cond_signal_base (cond) /= 0 then
329          return errno;
330       end if;
331
332       return 0;
333    end pthread_cond_signal;
334
335    function pthread_cond_wait
336      (cond  : access pthread_cond_t;
337       mutex : access pthread_mutex_t)
338       return  int
339    is
340       function pthread_cond_wait_base
341         (cond  : access pthread_cond_t;
342          mutex : access pthread_mutex_t)
343          return  int;
344       pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
345
346    begin
347       if pthread_cond_wait_base (cond, mutex) /= 0 then
348          return errno;
349       end if;
350
351       return 0;
352    end pthread_cond_wait;
353
354    function pthread_cond_timedwait
355      (cond    : access pthread_cond_t;
356       mutex   : access pthread_mutex_t;
357       reltime : access timespec) return int
358    is
359       function pthread_cond_timedwait_base
360         (cond    : access pthread_cond_t;
361          mutex   : access pthread_mutex_t;
362          reltime : access timespec) return int;
363       pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
364
365    begin
366       if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
367          if errno = EAGAIN then
368             return ETIMEDOUT;
369          end if;
370
371          return errno;
372       end if;
373
374       return 0;
375    end pthread_cond_timedwait;
376
377    --------------------------
378    -- POSIX.1c  Section 13 --
379    --------------------------
380
381    function pthread_setschedparam
382      (thread : pthread_t;
383       policy : int;
384       param  : access struct_sched_param)
385       return   int
386    is
387       function pthread_setscheduler
388         (thread : pthread_t;
389          policy : int;
390          prio   : int)
391          return   int;
392       pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
393
394    begin
395       if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
396          return errno;
397       end if;
398
399       return 0;
400    end pthread_setschedparam;
401
402    function pthread_mutexattr_setprotocol
403      (attr     : access pthread_mutexattr_t;
404       protocol : int)
405       return     int
406    is
407       pragma Unreferenced (attr, protocol);
408    begin
409       return 0;
410    end pthread_mutexattr_setprotocol;
411
412    function pthread_mutexattr_setprioceiling
413      (attr        : access pthread_mutexattr_t;
414       prioceiling : int)
415       return        int
416    is
417       pragma Unreferenced (attr, prioceiling);
418    begin
419       return 0;
420    end pthread_mutexattr_setprioceiling;
421
422    function pthread_attr_setscope
423      (attr            : access pthread_attr_t;
424       contentionscope : int)
425       return            int
426    is
427       pragma Unreferenced (attr, contentionscope);
428    begin
429       return 0;
430    end pthread_attr_setscope;
431
432    function sched_yield return int is
433       procedure pthread_yield;
434       pragma Import (C, pthread_yield, "pthread_yield");
435
436    begin
437       pthread_yield;
438       return 0;
439    end sched_yield;
440
441    -----------------------------
442    --  P1003.1c - Section 16  --
443    -----------------------------
444
445    function pthread_attr_setdetachstate
446      (attr        : access pthread_attr_t;
447       detachstate : int)
448       return        int
449    is
450       pragma Unreferenced (attr, detachstate);
451    begin
452       return 0;
453    end pthread_attr_setdetachstate;
454
455    function pthread_create
456      (thread        : access pthread_t;
457       attributes    : access pthread_attr_t;
458       start_routine : Thread_Body;
459       arg           : System.Address)
460       return          int
461    is
462       --  The LynxOS pthread_create doesn't seems to work.
463       --  Workaround : We're using st_new instead.
464       --
465       --   function pthread_create_base
466       --     (thread        : access pthread_t;
467       --      attributes    : pthread_attr_t;
468       --      start_routine : Thread_Body;
469       --      arg           : System.Address)
470       --      return          int;
471       --   pragma Import (C, pthread_create_base, "pthread_create");
472
473       St : aliased st_t := attributes.st;
474
475       function st_new
476         (start_routine : Thread_Body;
477          arg           : System.Address;
478          attributes    : access st_t;
479          thread        : access pthread_t)
480          return          int;
481       pragma Import (C, st_new, "st_new");
482
483    begin
484       --  Following code would be used if above commented function worked
485
486       --   if pthread_create_base
487       --        (thread, attributes.all, start_routine, arg) /= 0 then
488
489       if st_new (start_routine, arg, St'Access, thread) /= 0 then
490          return errno;
491       end if;
492
493       return 0;
494    end pthread_create;
495
496    function pthread_detach (thread : pthread_t) return int is
497       aliased_thread : aliased pthread_t := thread;
498
499       function pthread_detach_base (thread : access pthread_t) return int;
500       pragma Import (C, pthread_detach_base, "pthread_detach");
501
502    begin
503       if pthread_detach_base (aliased_thread'Access) /= 0 then
504          return errno;
505       end if;
506
507       return 0;
508    end pthread_detach;
509
510    --------------------------
511    -- POSIX.1c  Section 17 --
512    --------------------------
513
514    function pthread_setspecific
515      (key   : pthread_key_t;
516       value : System.Address)
517       return  int
518    is
519       function pthread_setspecific_base
520         (key   : pthread_key_t;
521          value : System.Address)
522          return  int;
523       pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
524
525    begin
526       if pthread_setspecific_base (key, value) /= 0 then
527          return errno;
528       end if;
529
530       return 0;
531    end pthread_setspecific;
532
533    function pthread_getspecific (key : pthread_key_t) return System.Address is
534       procedure pthread_getspecific_base
535         (key   : pthread_key_t;
536          value : access System.Address);
537       pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
538
539       value : aliased System.Address := System.Null_Address;
540
541    begin
542       pthread_getspecific_base (key, value'Unchecked_Access);
543       return value;
544    end pthread_getspecific;
545
546    function Get_Stack_Base (thread : pthread_t) return Address is
547       pragma Warnings (Off, thread);
548
549    begin
550       return Null_Address;
551    end Get_Stack_Base;
552
553    function pthread_key_create
554      (key        : access pthread_key_t;
555       destructor : destructor_pointer)
556       return       int
557    is
558       function pthread_keycreate
559         (key        : access pthread_key_t;
560          destructor : destructor_pointer)
561          return       int;
562       pragma Import (C, pthread_keycreate, "pthread_keycreate");
563
564    begin
565       if pthread_keycreate (key, destructor) /= 0 then
566          return errno;
567       end if;
568
569       return 0;
570    end pthread_key_create;
571
572    procedure pthread_init is
573    begin
574       null;
575    end pthread_init;
576
577 end System.OS_Interface;