OSDN Git Service

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