OSDN Git Service

Update FSF address
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taprop-tru64.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
5 --     S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S    --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --         Copyright (C) 1992-2005, 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 DEC Unix 4.0d version of this package
35
36 --  This package contains all the GNULL primitives that interface directly
37 --  with the underlying OS.
38
39 pragma Polling (Off);
40 --  Turn off polling, we do not want ATC polling to take place during
41 --  tasking operations. It causes infinite loops and other problems.
42
43 with System.Tasking.Debug;
44 --  used for Known_Tasks
45
46 with System.Task_Info;
47 --  used for Task_Info_Type
48
49 with Interfaces;
50 --  used for Shift_Left
51
52 with Interfaces.C;
53 --  used for int
54 --           size_t
55
56 with System.Interrupt_Management;
57 --  used for Keep_Unmasked
58 --           Abort_Task_Interrupt
59 --           Interrupt_ID
60
61 with System.Parameters;
62 --  used for Size_Type
63
64 with System.Tasking;
65 --  used for Ada_Task_Control_Block
66 --           Task_Id
67 --           ATCB components and types
68
69 with System.Soft_Links;
70 --  used for Defer/Undefer_Abort
71
72 --  Note that we do not use System.Tasking.Initialization directly since
73 --  this is a higher level package that we shouldn't depend on. For example
74 --  when using the restricted run time, it is replaced by
75 --  System.Tasking.Restricted.Stages.
76
77 with System.OS_Primitives;
78 --  used for Delay_Modes
79
80 with Unchecked_Deallocation;
81
82 package body System.Task_Primitives.Operations is
83
84    use System.Tasking.Debug;
85    use System.Tasking;
86    use Interfaces.C;
87    use System.OS_Interface;
88    use System.Parameters;
89    use System.OS_Primitives;
90
91    package SSL renames System.Soft_Links;
92
93    ----------------
94    -- Local Data --
95    ----------------
96
97    --  The followings are logically constants, but need to be initialized
98    --  at run time.
99
100    Single_RTS_Lock : aliased RTS_Lock;
101    --  This is a lock to allow only one thread of control in the RTS at
102    --  a time; it is used to execute in mutual exclusion from all other tasks.
103    --  Used mainly in Single_Lock mode, but also to protect All_Tasks_List
104
105    ATCB_Key : aliased pthread_key_t;
106    --  Key used to find the Ada Task_Id associated with a thread
107
108    Environment_Task_Id : Task_Id;
109    --  A variable to hold Task_Id for the environment task
110
111    Unblocked_Signal_Mask : aliased sigset_t;
112    --  The set of signals that should unblocked in all tasks
113
114    Time_Slice_Val : Integer;
115    pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
116
117    Locking_Policy : Character;
118    pragma Import (C, Locking_Policy, "__gl_locking_policy");
119
120    Dispatching_Policy : Character;
121    pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
122
123    FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
124    --  Indicates whether FIFO_Within_Priorities is set
125
126    Curpid : pid_t;
127
128    Foreign_Task_Elaborated : aliased Boolean := True;
129    --  Used to identified fake tasks (i.e., non-Ada Threads)
130
131    --------------------
132    -- Local Packages --
133    --------------------
134
135    package Specific is
136
137       procedure Initialize (Environment_Task : Task_Id);
138       pragma Inline (Initialize);
139       --  Initialize various data needed by this package
140
141       function Is_Valid_Task return Boolean;
142       pragma Inline (Is_Valid_Task);
143       --  Does executing thread have a TCB?
144
145       procedure Set (Self_Id : Task_Id);
146       pragma Inline (Set);
147       --  Set the self id for the current task
148
149       function Self return Task_Id;
150       pragma Inline (Self);
151       --  Return a pointer to the Ada Task Control Block of the calling task
152
153    end Specific;
154
155    package body Specific is separate;
156    --  The body of this package is target specific
157
158    ---------------------------------
159    -- Support for foreign threads --
160    ---------------------------------
161
162    function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
163    --  Allocate and initialize a new ATCB for the current Thread
164
165    function Register_Foreign_Thread
166      (Thread : Thread_Id) return Task_Id is separate;
167
168    -----------------------
169    -- Local Subprograms --
170    -----------------------
171
172    procedure Abort_Handler (Sig : Signal);
173    --  Signal handler used to implement asynchronous abort
174
175    -------------------
176    -- Abort_Handler --
177    -------------------
178
179    procedure Abort_Handler (Sig : Signal) is
180       pragma Unreferenced (Sig);
181
182       T       : constant Task_Id := Self;
183       Result  : Interfaces.C.int;
184       Old_Set : aliased sigset_t;
185
186    begin
187       --  It is not safe to raise an exception when using ZCX and the GCC
188       --  exception handling mechanism.
189
190       if ZCX_By_Default and then GCC_ZCX_Support then
191          return;
192       end if;
193
194       if T.Deferral_Level = 0
195         and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
196         not T.Aborting
197       then
198          T.Aborting := True;
199
200          --  Make sure signals used for RTS internal purpose are unmasked
201
202          Result := pthread_sigmask (SIG_UNBLOCK,
203            Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
204          pragma Assert (Result = 0);
205
206          raise Standard'Abort_Signal;
207       end if;
208    end Abort_Handler;
209
210    ------------------
211    -- Stack_Guard  --
212    ------------------
213
214    --  The underlying thread system sets a guard page at the
215    --  bottom of a thread stack, so nothing is needed.
216
217    procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
218       pragma Unreferenced (T);
219       pragma Unreferenced (On);
220    begin
221       null;
222    end Stack_Guard;
223
224    --------------------
225    -- Get_Thread_Id  --
226    --------------------
227
228    function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
229    begin
230       return T.Common.LL.Thread;
231    end Get_Thread_Id;
232
233    ----------
234    -- Self --
235    ----------
236
237    function Self return Task_Id renames Specific.Self;
238
239    ---------------------
240    -- Initialize_Lock --
241    ---------------------
242
243    --  Note: mutexes and cond_variables needed per-task basis are
244    --        initialized in Initialize_TCB and the Storage_Error is
245    --        handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
246    --        used in RTS is initialized before any status change of RTS.
247    --        Therefore rasing Storage_Error in the following routines
248    --        should be able to be handled safely.
249
250    procedure Initialize_Lock
251      (Prio : System.Any_Priority;
252       L    : access Lock)
253    is
254       Attributes : aliased pthread_mutexattr_t;
255       Result     : Interfaces.C.int;
256
257    begin
258       Result := pthread_mutexattr_init (Attributes'Access);
259       pragma Assert (Result = 0 or else Result = ENOMEM);
260
261       if Result = ENOMEM then
262          raise Storage_Error;
263       end if;
264
265       if Locking_Policy = 'C' then
266          L.Ceiling := Interfaces.C.int (Prio);
267       end if;
268
269       Result := pthread_mutex_init (L.L'Access, Attributes'Access);
270       pragma Assert (Result = 0 or else Result = ENOMEM);
271
272       if Result = ENOMEM then
273          Result := pthread_mutexattr_destroy (Attributes'Access);
274          raise Storage_Error;
275       end if;
276
277       Result := pthread_mutexattr_destroy (Attributes'Access);
278       pragma Assert (Result = 0);
279    end Initialize_Lock;
280
281    procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
282       pragma Unreferenced (Level);
283
284       Attributes : aliased pthread_mutexattr_t;
285       Result     : Interfaces.C.int;
286
287    begin
288       Result := pthread_mutexattr_init (Attributes'Access);
289       pragma Assert (Result = 0 or else Result = ENOMEM);
290
291       if Result = ENOMEM then
292          raise Storage_Error;
293       end if;
294
295       Result := pthread_mutex_init (L, Attributes'Access);
296       pragma Assert (Result = 0 or else Result = ENOMEM);
297
298       if Result = ENOMEM then
299          Result := pthread_mutexattr_destroy (Attributes'Access);
300          raise Storage_Error;
301       end if;
302
303       Result := pthread_mutexattr_destroy (Attributes'Access);
304       pragma Assert (Result = 0);
305    end Initialize_Lock;
306
307    -------------------
308    -- Finalize_Lock --
309    -------------------
310
311    procedure Finalize_Lock (L : access Lock) is
312       Result : Interfaces.C.int;
313    begin
314       Result := pthread_mutex_destroy (L.L'Access);
315       pragma Assert (Result = 0);
316    end Finalize_Lock;
317
318    procedure Finalize_Lock (L : access RTS_Lock) is
319       Result : Interfaces.C.int;
320    begin
321       Result := pthread_mutex_destroy (L);
322       pragma Assert (Result = 0);
323    end Finalize_Lock;
324
325    ----------------
326    -- Write_Lock --
327    ----------------
328
329    procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
330       Result         : Interfaces.C.int;
331       Self_ID        : Task_Id;
332       All_Tasks_Link : Task_Id;
333       Current_Prio   : System.Any_Priority;
334
335    begin
336       --  Perform ceiling checks only when this is the locking policy in use
337
338       if Locking_Policy = 'C' then
339          Self_ID := Self;
340          All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
341          Current_Prio := Get_Priority (Self_ID);
342
343          --  If there is no other task, no need to check priorities
344
345          if All_Tasks_Link /= Null_Task
346            and then L.Ceiling < Interfaces.C.int (Current_Prio)
347          then
348             Ceiling_Violation := True;
349             return;
350          end if;
351       end if;
352
353       Result := pthread_mutex_lock (L.L'Access);
354       pragma Assert (Result = 0);
355
356       Ceiling_Violation := False;
357    end Write_Lock;
358
359    procedure Write_Lock
360      (L : access RTS_Lock; Global_Lock : Boolean := False)
361    is
362       Result : Interfaces.C.int;
363    begin
364       if not Single_Lock or else Global_Lock then
365          Result := pthread_mutex_lock (L);
366          pragma Assert (Result = 0);
367       end if;
368    end Write_Lock;
369
370    procedure Write_Lock (T : Task_Id) is
371       Result : Interfaces.C.int;
372    begin
373       if not Single_Lock then
374          Result := pthread_mutex_lock (T.Common.LL.L'Access);
375          pragma Assert (Result = 0);
376       end if;
377    end Write_Lock;
378
379    ---------------
380    -- Read_Lock --
381    ---------------
382
383    procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
384    begin
385       Write_Lock (L, Ceiling_Violation);
386    end Read_Lock;
387
388    ------------
389    -- Unlock --
390    ------------
391
392    procedure Unlock (L : access Lock) is
393       Result : Interfaces.C.int;
394    begin
395       Result := pthread_mutex_unlock (L.L'Access);
396       pragma Assert (Result = 0);
397    end Unlock;
398
399    procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
400       Result : Interfaces.C.int;
401    begin
402       if not Single_Lock or else Global_Lock then
403          Result := pthread_mutex_unlock (L);
404          pragma Assert (Result = 0);
405       end if;
406    end Unlock;
407
408    procedure Unlock (T : Task_Id) is
409       Result : Interfaces.C.int;
410    begin
411       if not Single_Lock then
412          Result := pthread_mutex_unlock (T.Common.LL.L'Access);
413          pragma Assert (Result = 0);
414       end if;
415    end Unlock;
416
417    -----------
418    -- Sleep --
419    -----------
420
421    procedure Sleep
422      (Self_ID : Task_Id;
423       Reason  : System.Tasking.Task_States)
424    is
425       pragma Unreferenced (Reason);
426
427       Result : Interfaces.C.int;
428
429    begin
430       if Single_Lock then
431          Result := pthread_cond_wait
432                      (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
433       else
434          Result := pthread_cond_wait
435                      (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
436       end if;
437
438       --  EINTR is not considered a failure
439
440       pragma Assert (Result = 0 or else Result = EINTR);
441    end Sleep;
442
443    -----------------
444    -- Timed_Sleep --
445    -----------------
446
447    --  This is for use within the run-time system, so abort is
448    --  assumed to be already deferred, and the caller should be
449    --  holding its own ATCB lock.
450
451    procedure Timed_Sleep
452      (Self_ID  : Task_Id;
453       Time     : Duration;
454       Mode     : ST.Delay_Modes;
455       Reason   : System.Tasking.Task_States;
456       Timedout : out Boolean;
457       Yielded  : out Boolean)
458    is
459       pragma Unreferenced (Reason);
460
461       Check_Time : constant Duration := Monotonic_Clock;
462       Abs_Time   : Duration;
463       Request    : aliased timespec;
464       Result     : Interfaces.C.int;
465
466    begin
467       Timedout := True;
468       Yielded := False;
469
470       if Mode = Relative then
471          Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
472       else
473          Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
474       end if;
475
476       if Abs_Time > Check_Time then
477          Request := To_Timespec (Abs_Time);
478
479          loop
480             exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
481               or else Self_ID.Pending_Priority_Change;
482
483             if Single_Lock then
484                Result := pthread_cond_timedwait
485                            (Self_ID.Common.LL.CV'Access,
486                             Single_RTS_Lock'Access,
487                             Request'Access);
488
489             else
490                Result := pthread_cond_timedwait
491                            (Self_ID.Common.LL.CV'Access,
492                             Self_ID.Common.LL.L'Access,
493                             Request'Access);
494             end if;
495
496             exit when Abs_Time <= Monotonic_Clock;
497
498             if Result = 0 or Result = EINTR then
499
500                --  Somebody may have called Wakeup for us
501
502                Timedout := False;
503                exit;
504             end if;
505
506             pragma Assert (Result = ETIMEDOUT);
507          end loop;
508       end if;
509    end Timed_Sleep;
510
511    -----------------
512    -- Timed_Delay --
513    -----------------
514
515    --  This is for use in implementing delay statements, so
516    --  we assume the caller is abort-deferred but is holding
517    --  no locks.
518
519    procedure Timed_Delay
520      (Self_ID  : Task_Id;
521       Time     : Duration;
522       Mode     : ST.Delay_Modes)
523    is
524       Check_Time : constant Duration := Monotonic_Clock;
525       Abs_Time   : Duration;
526       Request    : aliased timespec;
527       Result     : Interfaces.C.int;
528
529    begin
530       --  Only the little window between deferring abort and
531       --  locking Self_ID is the reason we need to
532       --  check for pending abort and priority change below! :(
533
534       SSL.Abort_Defer.all;
535
536       if Single_Lock then
537          Lock_RTS;
538       end if;
539
540       Write_Lock (Self_ID);
541
542       if Mode = Relative then
543          Abs_Time := Time + Check_Time;
544       else
545          Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
546       end if;
547
548       if Abs_Time > Check_Time then
549          Request := To_Timespec (Abs_Time);
550          Self_ID.Common.State := Delay_Sleep;
551
552          loop
553             if Self_ID.Pending_Priority_Change then
554                Self_ID.Pending_Priority_Change := False;
555                Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
556                Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
557             end if;
558
559             exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
560
561             if Single_Lock then
562                Result := pthread_cond_timedwait
563                            (Self_ID.Common.LL.CV'Access,
564                             Single_RTS_Lock'Access,
565                             Request'Access);
566             else
567                Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
568                  Self_ID.Common.LL.L'Access, Request'Access);
569             end if;
570
571             exit when Abs_Time <= Monotonic_Clock;
572
573             pragma Assert (Result = 0 or else
574               Result = ETIMEDOUT or else
575               Result = EINTR);
576          end loop;
577
578          Self_ID.Common.State := Runnable;
579       end if;
580
581       Unlock (Self_ID);
582
583       if Single_Lock then
584          Unlock_RTS;
585       end if;
586
587       Yield;
588       SSL.Abort_Undefer.all;
589    end Timed_Delay;
590
591    ---------------------
592    -- Monotonic_Clock --
593    ---------------------
594
595    function Monotonic_Clock return Duration is
596       TS     : aliased timespec;
597       Result : Interfaces.C.int;
598    begin
599       Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
600       pragma Assert (Result = 0);
601       return To_Duration (TS);
602    end Monotonic_Clock;
603
604    -------------------
605    -- RT_Resolution --
606    -------------------
607
608    function RT_Resolution return Duration is
609    begin
610       --  Returned value must be an integral multiple of Duration'Small (1 ns)
611       --  The following is the best approximation of 1/1024. The clock on the
612       --  DEC Alpha ticks at 1024 Hz.
613
614       return 0.000_976_563;
615    end RT_Resolution;
616
617    ------------
618    -- Wakeup --
619    ------------
620
621    procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
622       pragma Unreferenced (Reason);
623       Result : Interfaces.C.int;
624    begin
625       Result := pthread_cond_signal (T.Common.LL.CV'Access);
626       pragma Assert (Result = 0);
627    end Wakeup;
628
629    -----------
630    -- Yield --
631    -----------
632
633    procedure Yield (Do_Yield : Boolean := True) is
634       Result : Interfaces.C.int;
635       pragma Unreferenced (Result);
636    begin
637       if Do_Yield then
638          Result := sched_yield;
639       end if;
640    end Yield;
641
642    ------------------
643    -- Set_Priority --
644    ------------------
645
646    procedure Set_Priority
647      (T                   : Task_Id;
648       Prio                : System.Any_Priority;
649       Loss_Of_Inheritance : Boolean := False)
650    is
651       pragma Unreferenced (Loss_Of_Inheritance);
652
653       Result : Interfaces.C.int;
654       Param  : aliased struct_sched_param;
655
656    begin
657       T.Common.Current_Priority := Prio;
658       Param.sched_priority  := Interfaces.C.int (Underlying_Priorities (Prio));
659
660       if Time_Slice_Val > 0 then
661          Result := pthread_setschedparam
662                      (T.Common.LL.Thread, SCHED_RR, Param'Access);
663
664       elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
665          Result := pthread_setschedparam
666                      (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
667
668       else
669          Result := pthread_setschedparam
670                      (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
671       end if;
672
673       pragma Assert (Result = 0);
674    end Set_Priority;
675
676    ------------------
677    -- Get_Priority --
678    ------------------
679
680    function Get_Priority (T : Task_Id) return System.Any_Priority is
681    begin
682       return T.Common.Current_Priority;
683    end Get_Priority;
684
685    ----------------
686    -- Enter_Task --
687    ----------------
688
689    procedure Enter_Task (Self_ID : Task_Id) is
690    begin
691       Hide_Yellow_Zone;
692       Self_ID.Common.LL.Thread := pthread_self;
693       Specific.Set (Self_ID);
694
695       Lock_RTS;
696
697       for J in Known_Tasks'Range loop
698          if Known_Tasks (J) = null then
699             Known_Tasks (J) := Self_ID;
700             Self_ID.Known_Tasks_Index := J;
701             exit;
702          end if;
703       end loop;
704
705       Unlock_RTS;
706    end Enter_Task;
707
708    --------------
709    -- New_ATCB --
710    --------------
711
712    function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
713    begin
714       return new Ada_Task_Control_Block (Entry_Num);
715    end New_ATCB;
716
717    -------------------
718    -- Is_Valid_Task --
719    -------------------
720
721    function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
722
723    -----------------------------
724    -- Register_Foreign_Thread --
725    -----------------------------
726
727    function Register_Foreign_Thread return Task_Id is
728    begin
729       if Is_Valid_Task then
730          return Self;
731       else
732          return Register_Foreign_Thread (pthread_self);
733       end if;
734    end Register_Foreign_Thread;
735
736    --------------------
737    -- Initialize_TCB --
738    --------------------
739
740    procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
741       Mutex_Attr : aliased pthread_mutexattr_t;
742       Result     : Interfaces.C.int;
743       Cond_Attr  : aliased pthread_condattr_t;
744
745    begin
746       if not Single_Lock then
747          Result := pthread_mutexattr_init (Mutex_Attr'Access);
748          pragma Assert (Result = 0 or else Result = ENOMEM);
749
750          if Result = 0 then
751             Result := pthread_mutex_init
752                         (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
753             pragma Assert (Result = 0 or else Result = ENOMEM);
754          end if;
755
756          if Result /= 0 then
757             Succeeded := False;
758             return;
759          end if;
760
761          Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
762          pragma Assert (Result = 0);
763       end if;
764
765       Result := pthread_condattr_init (Cond_Attr'Access);
766       pragma Assert (Result = 0 or else Result = ENOMEM);
767
768       if Result = 0 then
769          Result := pthread_cond_init
770                      (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
771          pragma Assert (Result = 0 or else Result = ENOMEM);
772       end if;
773
774       if Result = 0 then
775          Succeeded := True;
776       else
777          if not Single_Lock then
778             Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
779             pragma Assert (Result = 0);
780          end if;
781
782          Succeeded := False;
783       end if;
784
785       Result := pthread_condattr_destroy (Cond_Attr'Access);
786       pragma Assert (Result = 0);
787    end Initialize_TCB;
788
789    -----------------
790    -- Create_Task --
791    -----------------
792
793    procedure Create_Task
794      (T          : Task_Id;
795       Wrapper    : System.Address;
796       Stack_Size : System.Parameters.Size_Type;
797       Priority   : System.Any_Priority;
798       Succeeded  : out Boolean)
799    is
800       Attributes          : aliased pthread_attr_t;
801       Adjusted_Stack_Size : Interfaces.C.size_t;
802       Result              : Interfaces.C.int;
803       Param               : aliased System.OS_Interface.struct_sched_param;
804
805       use System.Task_Info;
806
807    begin
808       if Stack_Size = Unspecified_Size then
809          Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
810
811       elsif Stack_Size < Minimum_Stack_Size then
812          Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
813
814       else
815          Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
816       end if;
817
818       --  Account for the Yellow Zone (2 pages) and the guard page
819       --  right above. See Hide_Yellow_Zone for the rationale.
820
821       Adjusted_Stack_Size := Adjusted_Stack_Size + 3 * Get_Page_Size;
822
823       Result := pthread_attr_init (Attributes'Access);
824       pragma Assert (Result = 0 or else Result = ENOMEM);
825
826       if Result /= 0 then
827          Succeeded := False;
828          return;
829       end if;
830
831       Result := pthread_attr_setdetachstate
832                   (Attributes'Access, PTHREAD_CREATE_DETACHED);
833       pragma Assert (Result = 0);
834
835       Result := pthread_attr_setstacksize
836                   (Attributes'Access, Adjusted_Stack_Size);
837       pragma Assert (Result = 0);
838
839       Param.sched_priority :=
840         Interfaces.C.int (Underlying_Priorities (Priority));
841       Result := pthread_attr_setschedparam
842                   (Attributes'Access, Param'Access);
843       pragma Assert (Result = 0);
844
845       if Time_Slice_Val > 0 then
846          Result := pthread_attr_setschedpolicy
847                      (Attributes'Access, System.OS_Interface.SCHED_RR);
848
849       elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
850          Result := pthread_attr_setschedpolicy
851                      (Attributes'Access, System.OS_Interface.SCHED_FIFO);
852
853       else
854          Result := pthread_attr_setschedpolicy
855                      (Attributes'Access, System.OS_Interface.SCHED_OTHER);
856       end if;
857
858       pragma Assert (Result = 0);
859
860       --  Set the scheduling parameters explicitly, since this is the
861       --  only way to force the OS to take e.g. the sched policy and scope
862       --  attributes into account.
863
864       Result := pthread_attr_setinheritsched
865                   (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
866       pragma Assert (Result = 0);
867
868       T.Common.Current_Priority := Priority;
869
870       if T.Common.Task_Info /= null then
871          case T.Common.Task_Info.Contention_Scope is
872             when System.Task_Info.Process_Scope =>
873                Result := pthread_attr_setscope
874                            (Attributes'Access, PTHREAD_SCOPE_PROCESS);
875
876             when System.Task_Info.System_Scope =>
877                Result := pthread_attr_setscope
878                            (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
879
880             when System.Task_Info.Default_Scope =>
881                Result := 0;
882          end case;
883
884          pragma Assert (Result = 0);
885       end if;
886
887       --  Since the initial signal mask of a thread is inherited from the
888       --  creator, and the Environment task has all its signals masked, we
889       --  do not need to manipulate caller's signal mask at this point.
890       --  All tasks in RTS will have All_Tasks_Mask initially.
891
892       Result := pthread_create
893                   (T.Common.LL.Thread'Access,
894                    Attributes'Access,
895                    Thread_Body_Access (Wrapper),
896                    To_Address (T));
897       pragma Assert (Result = 0 or else Result = EAGAIN);
898
899       Succeeded := Result = 0;
900
901       Result := pthread_attr_destroy (Attributes'Access);
902       pragma Assert (Result = 0);
903
904       if T.Common.Task_Info /= null then
905          --  ??? We're using a process-wide function to implement a task
906          --  specific characteristic.
907
908          if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
909             Result := bind_to_cpu (Curpid, 0);
910          elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
911             Result := bind_to_cpu
912               (Curpid,
913                Interfaces.C.unsigned_long (
914                  Interfaces.Shift_Left
915                    (Interfaces.Unsigned_64'(1),
916                     T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
917             pragma Assert (Result = 0);
918          end if;
919       end if;
920    end Create_Task;
921
922    ------------------
923    -- Finalize_TCB --
924    ------------------
925
926    procedure Finalize_TCB (T : Task_Id) is
927       Result  : Interfaces.C.int;
928       Tmp     : Task_Id := T;
929       Is_Self : constant Boolean := T = Self;
930
931       procedure Free is new
932         Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
933
934    begin
935       if not Single_Lock then
936          Result := pthread_mutex_destroy (T.Common.LL.L'Access);
937          pragma Assert (Result = 0);
938       end if;
939
940       Result := pthread_cond_destroy (T.Common.LL.CV'Access);
941       pragma Assert (Result = 0);
942
943       if T.Known_Tasks_Index /= -1 then
944          Known_Tasks (T.Known_Tasks_Index) := null;
945       end if;
946
947       Free (Tmp);
948
949       if Is_Self then
950          Specific.Set (null);
951       end if;
952    end Finalize_TCB;
953
954    ---------------
955    -- Exit_Task --
956    ---------------
957
958    procedure Exit_Task is
959    begin
960       Specific.Set (null);
961    end Exit_Task;
962
963    ----------------
964    -- Abort_Task --
965    ----------------
966
967    procedure Abort_Task (T : Task_Id) is
968       Result : Interfaces.C.int;
969    begin
970       Result := pthread_kill (T.Common.LL.Thread,
971         Signal (System.Interrupt_Management.Abort_Task_Interrupt));
972       pragma Assert (Result = 0);
973    end Abort_Task;
974
975    ----------------
976    -- Initialize --
977    ----------------
978
979    procedure Initialize (S : in out Suspension_Object) is
980       Mutex_Attr : aliased pthread_mutexattr_t;
981       Cond_Attr  : aliased pthread_condattr_t;
982       Result     : Interfaces.C.int;
983    begin
984       --  Initialize internal state. It is always initialized to False (ARM
985       --  D.10 par. 6).
986
987       S.State := False;
988       S.Waiting := False;
989
990       --  Initialize internal mutex
991
992       Result := pthread_mutexattr_init (Mutex_Attr'Access);
993       pragma Assert (Result = 0 or else Result = ENOMEM);
994
995       if Result = ENOMEM then
996          raise Storage_Error;
997       end if;
998
999       Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1000       pragma Assert (Result = 0 or else Result = ENOMEM);
1001
1002       if Result = ENOMEM then
1003          Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1004          raise Storage_Error;
1005       end if;
1006
1007       Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1008       pragma Assert (Result = 0);
1009
1010       --  Initialize internal condition variable
1011
1012       Result := pthread_condattr_init (Cond_Attr'Access);
1013       pragma Assert (Result = 0 or else Result = ENOMEM);
1014
1015       Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1016
1017       pragma Assert (Result = 0 or else Result = ENOMEM);
1018
1019       if Result /= 0 then
1020          Result := pthread_mutex_destroy (S.L'Access);
1021          pragma Assert (Result = 0);
1022
1023          if Result = ENOMEM then
1024             raise Storage_Error;
1025          end if;
1026       end if;
1027    end Initialize;
1028
1029    --------------
1030    -- Finalize --
1031    --------------
1032
1033    procedure Finalize (S : in out Suspension_Object) is
1034       Result  : Interfaces.C.int;
1035    begin
1036       --  Destroy internal mutex
1037
1038       Result := pthread_mutex_destroy (S.L'Access);
1039       pragma Assert (Result = 0);
1040
1041       --  Destroy internal condition variable
1042
1043       Result := pthread_cond_destroy (S.CV'Access);
1044       pragma Assert (Result = 0);
1045    end Finalize;
1046
1047    -------------------
1048    -- Current_State --
1049    -------------------
1050
1051    function Current_State (S : Suspension_Object) return Boolean is
1052    begin
1053       --  We do not want to use lock on this read operation. State is marked
1054       --  as Atomic so that we ensure that the value retrieved is correct.
1055
1056       return S.State;
1057    end Current_State;
1058
1059    ---------------
1060    -- Set_False --
1061    ---------------
1062
1063    procedure Set_False (S : in out Suspension_Object) is
1064       Result  : Interfaces.C.int;
1065    begin
1066       Result := pthread_mutex_lock (S.L'Access);
1067       pragma Assert (Result = 0);
1068
1069       S.State := False;
1070
1071       Result := pthread_mutex_unlock (S.L'Access);
1072       pragma Assert (Result = 0);
1073    end Set_False;
1074
1075    --------------
1076    -- Set_True --
1077    --------------
1078
1079    procedure Set_True (S : in out Suspension_Object) is
1080       Result : Interfaces.C.int;
1081    begin
1082       Result := pthread_mutex_lock (S.L'Access);
1083       pragma Assert (Result = 0);
1084
1085       --  If there is already a task waiting on this suspension object then
1086       --  we resume it, leaving the state of the suspension object to False,
1087       --  as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1088       --  the state to True.
1089
1090       if S.Waiting then
1091          S.Waiting := False;
1092          S.State := False;
1093
1094          Result := pthread_cond_signal (S.CV'Access);
1095          pragma Assert (Result = 0);
1096       else
1097          S.State := True;
1098       end if;
1099
1100       Result := pthread_mutex_unlock (S.L'Access);
1101       pragma Assert (Result = 0);
1102    end Set_True;
1103
1104    ------------------------
1105    -- Suspend_Until_True --
1106    ------------------------
1107
1108    procedure Suspend_Until_True (S : in out Suspension_Object) is
1109       Result : Interfaces.C.int;
1110    begin
1111       Result := pthread_mutex_lock (S.L'Access);
1112       pragma Assert (Result = 0);
1113
1114       if S.Waiting then
1115          --  Program_Error must be raised upon calling Suspend_Until_True
1116          --  if another task is already waiting on that suspension object
1117          --  (ARM D.10 par. 10).
1118
1119          Result := pthread_mutex_unlock (S.L'Access);
1120          pragma Assert (Result = 0);
1121
1122          raise Program_Error;
1123       else
1124          --  Suspend the task if the state is False. Otherwise, the task
1125          --  continues its execution, and the state of the suspension object
1126          --  is set to False (ARM D.10 par. 9).
1127
1128          if S.State then
1129             S.State := False;
1130          else
1131             S.Waiting := True;
1132             Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1133          end if;
1134       end if;
1135
1136       Result := pthread_mutex_unlock (S.L'Access);
1137       pragma Assert (Result = 0);
1138    end Suspend_Until_True;
1139
1140    ----------------
1141    -- Check_Exit --
1142    ----------------
1143
1144    --  Dummy version
1145
1146    function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1147       pragma Unreferenced (Self_ID);
1148    begin
1149       return True;
1150    end Check_Exit;
1151
1152    --------------------
1153    -- Check_No_Locks --
1154    --------------------
1155
1156    function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1157       pragma Unreferenced (Self_ID);
1158    begin
1159       return True;
1160    end Check_No_Locks;
1161
1162    ----------------------
1163    -- Environment_Task --
1164    ----------------------
1165
1166    function Environment_Task return Task_Id is
1167    begin
1168       return Environment_Task_Id;
1169    end Environment_Task;
1170
1171    --------------
1172    -- Lock_RTS --
1173    --------------
1174
1175    procedure Lock_RTS is
1176    begin
1177       Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1178    end Lock_RTS;
1179
1180    ----------------
1181    -- Unlock_RTS --
1182    ----------------
1183
1184    procedure Unlock_RTS is
1185    begin
1186       Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1187    end Unlock_RTS;
1188
1189    ------------------
1190    -- Suspend_Task --
1191    ------------------
1192
1193    function Suspend_Task
1194      (T           : ST.Task_Id;
1195       Thread_Self : Thread_Id) return Boolean
1196    is
1197       pragma Warnings (Off, T);
1198       pragma Warnings (Off, Thread_Self);
1199    begin
1200       return False;
1201    end Suspend_Task;
1202
1203    -----------------
1204    -- Resume_Task --
1205    -----------------
1206
1207    function Resume_Task
1208      (T           : ST.Task_Id;
1209       Thread_Self : Thread_Id) return Boolean
1210    is
1211       pragma Warnings (Off, T);
1212       pragma Warnings (Off, Thread_Self);
1213    begin
1214       return False;
1215    end Resume_Task;
1216
1217    ----------------
1218    -- Initialize --
1219    ----------------
1220
1221    procedure Initialize (Environment_Task : Task_Id) is
1222       act     : aliased struct_sigaction;
1223       old_act : aliased struct_sigaction;
1224       Tmp_Set : aliased sigset_t;
1225       Result  : Interfaces.C.int;
1226
1227       function State
1228         (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1229       pragma Import (C, State, "__gnat_get_interrupt_state");
1230       --  Get interrupt state. Defined in a-init.c. The input argument is
1231       --  the interrupt number, and the result is one of the following:
1232
1233       Default : constant Character := 's';
1234       --    'n'   this interrupt not set by any Interrupt_State pragma
1235       --    'u'   Interrupt_State pragma set state to User
1236       --    'r'   Interrupt_State pragma set state to Runtime
1237       --    's'   Interrupt_State pragma set state to System (use "default"
1238       --           system handler)
1239
1240    begin
1241       Environment_Task_Id := Environment_Task;
1242
1243       --  Initialize the lock used to synchronize chain of all ATCBs
1244
1245       Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1246
1247       Specific.Initialize (Environment_Task);
1248
1249       Enter_Task (Environment_Task);
1250
1251       --  Install the abort-signal handler
1252
1253       if State (System.Interrupt_Management.Abort_Task_Interrupt)
1254         /= Default
1255       then
1256          act.sa_flags := 0;
1257          act.sa_handler := Abort_Handler'Address;
1258
1259          Result := sigemptyset (Tmp_Set'Access);
1260          pragma Assert (Result = 0);
1261          act.sa_mask := Tmp_Set;
1262
1263          Result :=
1264            sigaction
1265            (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1266             act'Unchecked_Access,
1267             old_act'Unchecked_Access);
1268          pragma Assert (Result = 0);
1269       end if;
1270    end Initialize;
1271
1272 begin
1273    declare
1274       Result : Interfaces.C.int;
1275    begin
1276       --  Prepare the set of signals that should unblocked in all tasks
1277
1278       Result := sigemptyset (Unblocked_Signal_Mask'Access);
1279       pragma Assert (Result = 0);
1280
1281       for J in Interrupt_Management.Interrupt_ID loop
1282          if System.Interrupt_Management.Keep_Unmasked (J) then
1283             Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1284             pragma Assert (Result = 0);
1285          end if;
1286       end loop;
1287    end;
1288
1289    Curpid := getpid;
1290 end System.Task_Primitives.Operations;