1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
36 -- This package contains all the GNULL primitives that interface directly with
40 -- Turn off polling, we do not want ATC polling to take place during tasking
41 -- operations. It causes infinite loops and other problems.
43 with Ada.Unchecked_Conversion;
44 with Ada.Unchecked_Deallocation;
48 with System.Task_Info;
49 with System.Tasking.Debug;
50 with System.Interrupt_Management;
51 with System.OS_Primitives;
52 with System.Stack_Checking.Operations;
54 with System.Soft_Links;
55 -- We use System.Soft_Links instead of System.Tasking.Initialization
56 -- because the later is a higher level package that we shouldn't depend on.
57 -- For example when using the restricted run time, it is replaced by
58 -- System.Tasking.Restricted.Stages.
60 package body System.Task_Primitives.Operations is
62 package SSL renames System.Soft_Links;
63 package SC renames System.Stack_Checking.Operations;
65 use System.Tasking.Debug;
68 use System.OS_Interface;
69 use System.Parameters;
70 use System.OS_Primitives;
73 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
74 -- Whether to use an alternate signal stack for stack overflows
80 -- The followings are logically constants, but need to be initialized
83 Single_RTS_Lock : aliased RTS_Lock;
84 -- This is a lock to allow only one thread of control in the RTS at
85 -- a time; it is used to execute in mutual exclusion from all other tasks.
86 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
88 ATCB_Key : aliased pthread_key_t;
89 -- Key used to find the Ada Task_Id associated with a thread
91 Environment_Task_Id : Task_Id;
92 -- A variable to hold Task_Id for the environment task
94 Unblocked_Signal_Mask : aliased sigset_t;
95 -- The set of signals that should be unblocked in all tasks
97 -- The followings are internal configuration constants needed
99 Next_Serial_Number : Task_Serial_Number := 100;
100 -- We start at 100 (reserve some special values for using in error checks)
102 Time_Slice_Val : Integer;
103 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
105 Dispatching_Policy : Character;
106 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
108 -- The following are effectively constants, but they need to be initialized
109 -- by calling a pthread_ function.
111 Mutex_Attr : aliased pthread_mutexattr_t;
112 Cond_Attr : aliased pthread_condattr_t;
114 Foreign_Task_Elaborated : aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
123 procedure Initialize (Environment_Task : Task_Id);
124 pragma Inline (Initialize);
125 -- Initialize various data needed by this package
127 function Is_Valid_Task return Boolean;
128 pragma Inline (Is_Valid_Task);
129 -- Does executing thread have a TCB?
131 procedure Set (Self_Id : Task_Id);
133 -- Set the self id for the current task
135 function Self return Task_Id;
136 pragma Inline (Self);
137 -- Return a pointer to the Ada Task Control Block of the calling task
141 package body Specific is separate;
142 -- The body of this package is target specific
144 ---------------------------------
145 -- Support for foreign threads --
146 ---------------------------------
148 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
149 -- Allocate and Initialize a new ATCB for the current Thread
151 function Register_Foreign_Thread
152 (Thread : Thread_Id) return Task_Id is separate;
154 -----------------------
155 -- Local Subprograms --
156 -----------------------
158 subtype unsigned_long is Interfaces.C.unsigned_long;
160 procedure Abort_Handler (signo : Signal);
162 function To_pthread_t is new Ada.Unchecked_Conversion
163 (unsigned_long, System.OS_Interface.pthread_t);
169 procedure Abort_Handler (signo : Signal) is
170 pragma Unreferenced (signo);
172 Self_Id : constant Task_Id := Self;
173 Result : Interfaces.C.int;
174 Old_Set : aliased sigset_t;
177 if ZCX_By_Default and then GCC_ZCX_Support then
181 if Self_Id.Deferral_Level = 0
182 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
183 and then not Self_Id.Aborting
185 Self_Id.Aborting := True;
187 -- Make sure signals used for RTS internal purpose are unmasked
192 Unblocked_Signal_Mask'Access,
194 pragma Assert (Result = 0);
196 raise Standard'Abort_Signal;
204 procedure Lock_RTS is
206 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
213 procedure Unlock_RTS is
215 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
222 -- The underlying thread system extends the memory (up to 2MB) when needed
224 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
225 pragma Unreferenced (T);
226 pragma Unreferenced (On);
235 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
237 return T.Common.LL.Thread;
244 function Self return Task_Id renames Specific.Self;
246 ---------------------
247 -- Initialize_Lock --
248 ---------------------
250 -- Note: mutexes and cond_variables needed per-task basis are initialized
251 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
252 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
253 -- status change of RTS. Therefore rasing Storage_Error in the following
254 -- routines should be able to be handled safely.
256 procedure Initialize_Lock
257 (Prio : System.Any_Priority;
258 L : not null access Lock)
260 pragma Unreferenced (Prio);
262 Result : Interfaces.C.int;
265 Result := pthread_mutex_init (L, Mutex_Attr'Access);
267 pragma Assert (Result = 0 or else Result = ENOMEM);
269 if Result = ENOMEM then
270 raise Storage_Error with "Failed to allocate a lock";
274 procedure Initialize_Lock
275 (L : not null access RTS_Lock;
278 pragma Unreferenced (Level);
280 Result : Interfaces.C.int;
283 Result := pthread_mutex_init (L, Mutex_Attr'Access);
285 pragma Assert (Result = 0 or else Result = ENOMEM);
287 if Result = ENOMEM then
296 procedure Finalize_Lock (L : not null access Lock) is
297 Result : Interfaces.C.int;
299 Result := pthread_mutex_destroy (L);
300 pragma Assert (Result = 0);
303 procedure Finalize_Lock (L : not null access RTS_Lock) is
304 Result : Interfaces.C.int;
306 Result := pthread_mutex_destroy (L);
307 pragma Assert (Result = 0);
315 (L : not null access Lock;
316 Ceiling_Violation : out Boolean)
318 Result : Interfaces.C.int;
320 Result := pthread_mutex_lock (L);
321 Ceiling_Violation := Result = EINVAL;
323 -- Assume the cause of EINVAL is a priority ceiling violation
325 pragma Assert (Result = 0 or else Result = EINVAL);
329 (L : not null access RTS_Lock;
330 Global_Lock : Boolean := False)
332 Result : Interfaces.C.int;
334 if not Single_Lock or else Global_Lock then
335 Result := pthread_mutex_lock (L);
336 pragma Assert (Result = 0);
340 procedure Write_Lock (T : Task_Id) is
341 Result : Interfaces.C.int;
343 if not Single_Lock then
344 Result := pthread_mutex_lock (T.Common.LL.L'Access);
345 pragma Assert (Result = 0);
354 (L : not null access Lock;
355 Ceiling_Violation : out Boolean)
358 Write_Lock (L, Ceiling_Violation);
365 procedure Unlock (L : not null access Lock) is
366 Result : Interfaces.C.int;
368 Result := pthread_mutex_unlock (L);
369 pragma Assert (Result = 0);
373 (L : not null access RTS_Lock;
374 Global_Lock : Boolean := False)
376 Result : Interfaces.C.int;
378 if not Single_Lock or else Global_Lock then
379 Result := pthread_mutex_unlock (L);
380 pragma Assert (Result = 0);
384 procedure Unlock (T : Task_Id) is
385 Result : Interfaces.C.int;
387 if not Single_Lock then
388 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
389 pragma Assert (Result = 0);
397 -- Dynamic priority ceilings are not supported by the underlying system
399 procedure Set_Ceiling
400 (L : not null access Lock;
401 Prio : System.Any_Priority)
403 pragma Unreferenced (L, Prio);
414 Reason : System.Tasking.Task_States)
416 pragma Unreferenced (Reason);
418 Result : Interfaces.C.int;
421 pragma Assert (Self_ID = Self);
426 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
430 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
433 -- EINTR is not considered a failure
435 pragma Assert (Result = 0 or else Result = EINTR);
442 -- This is for use within the run-time system, so abort is
443 -- assumed to be already deferred, and the caller should be
444 -- holding its own ATCB lock.
446 procedure Timed_Sleep
449 Mode : ST.Delay_Modes;
450 Reason : System.Tasking.Task_States;
451 Timedout : out Boolean;
452 Yielded : out Boolean)
454 pragma Unreferenced (Reason);
456 Base_Time : constant Duration := Monotonic_Clock;
457 Check_Time : Duration := Base_Time;
459 Request : aliased timespec;
460 Result : Interfaces.C.int;
466 if Mode = Relative then
467 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
469 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
472 if Abs_Time > Check_Time then
473 Request := To_Timespec (Abs_Time);
476 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
480 pthread_cond_timedwait
481 (Self_ID.Common.LL.CV'Access,
482 Single_RTS_Lock'Access,
487 pthread_cond_timedwait
488 (Self_ID.Common.LL.CV'Access,
489 Self_ID.Common.LL.L'Access,
493 Check_Time := Monotonic_Clock;
494 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
496 if Result = 0 or else Result = EINTR then
498 -- Somebody may have called Wakeup for us
504 pragma Assert (Result = ETIMEDOUT);
513 -- This is for use in implementing delay statements, so we assume the
514 -- caller is abort-deferred but is holding no locks.
516 procedure Timed_Delay
519 Mode : ST.Delay_Modes)
521 Base_Time : constant Duration := Monotonic_Clock;
522 Check_Time : Duration := Base_Time;
524 Request : aliased timespec;
526 Result : Interfaces.C.int;
527 pragma Warnings (Off, Result);
534 Write_Lock (Self_ID);
536 if Mode = Relative then
537 Abs_Time := Time + Check_Time;
539 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
542 if Abs_Time > Check_Time then
543 Request := To_Timespec (Abs_Time);
544 Self_ID.Common.State := Delay_Sleep;
547 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
550 Result := pthread_cond_timedwait
551 (Self_ID.Common.LL.CV'Access,
552 Single_RTS_Lock'Access,
555 Result := pthread_cond_timedwait
556 (Self_ID.Common.LL.CV'Access,
557 Self_ID.Common.LL.L'Access,
561 Check_Time := Monotonic_Clock;
562 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
564 pragma Assert (Result = 0 or else
565 Result = ETIMEDOUT or else
569 Self_ID.Common.State := Runnable;
578 Result := sched_yield;
581 ---------------------
582 -- Monotonic_Clock --
583 ---------------------
585 function Monotonic_Clock return Duration is
586 TV : aliased struct_timeval;
587 Result : Interfaces.C.int;
589 Result := gettimeofday (TV'Access, System.Null_Address);
590 pragma Assert (Result = 0);
591 return To_Duration (TV);
598 function RT_Resolution return Duration is
607 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
608 pragma Unreferenced (Reason);
609 Result : Interfaces.C.int;
611 Result := pthread_cond_signal (T.Common.LL.CV'Access);
612 pragma Assert (Result = 0);
619 procedure Yield (Do_Yield : Boolean := True) is
620 Result : Interfaces.C.int;
621 pragma Unreferenced (Result);
624 Result := sched_yield;
632 procedure Set_Priority
634 Prio : System.Any_Priority;
635 Loss_Of_Inheritance : Boolean := False)
637 pragma Unreferenced (Loss_Of_Inheritance);
639 Result : Interfaces.C.int;
640 Param : aliased struct_sched_param;
642 function Get_Policy (Prio : System.Any_Priority) return Character;
643 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
644 -- Get priority specific dispatching policy
646 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
647 -- Upper case first character of the policy name corresponding to the
648 -- task as set by a Priority_Specific_Dispatching pragma.
651 T.Common.Current_Priority := Prio;
653 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
655 Param.sched_priority := Interfaces.C.int (Prio) + 1;
657 if Dispatching_Policy = 'R'
658 or else Priority_Specific_Policy = 'R'
659 or else Time_Slice_Val > 0
662 pthread_setschedparam
663 (T.Common.LL.Thread, SCHED_RR, Param'Access);
665 elsif Dispatching_Policy = 'F'
666 or else Priority_Specific_Policy = 'F'
667 or else Time_Slice_Val = 0
670 pthread_setschedparam
671 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
674 Param.sched_priority := 0;
676 pthread_setschedparam
678 SCHED_OTHER, Param'Access);
681 pragma Assert (Result = 0 or else Result = EPERM);
688 function Get_Priority (T : Task_Id) return System.Any_Priority is
690 return T.Common.Current_Priority;
697 procedure Enter_Task (Self_ID : Task_Id) is
699 if Self_ID.Common.Task_Info /= null
700 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
702 raise Invalid_CPU_Number;
705 Self_ID.Common.LL.Thread := pthread_self;
707 Specific.Set (Self_ID);
711 for J in Known_Tasks'Range loop
712 if Known_Tasks (J) = null then
713 Known_Tasks (J) := Self_ID;
714 Self_ID.Known_Tasks_Index := J;
721 if Use_Alternate_Stack then
723 Stack : aliased stack_t;
724 Result : Interfaces.C.int;
726 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
727 Stack.ss_size := Alternate_Stack_Size;
729 Result := sigaltstack (Stack'Access, null);
730 pragma Assert (Result = 0);
739 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
741 return new Ada_Task_Control_Block (Entry_Num);
748 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
750 -----------------------------
751 -- Register_Foreign_Thread --
752 -----------------------------
754 function Register_Foreign_Thread return Task_Id is
756 if Is_Valid_Task then
759 return Register_Foreign_Thread (pthread_self);
761 end Register_Foreign_Thread;
767 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
768 Result : Interfaces.C.int;
771 -- Give the task a unique serial number
773 Self_ID.Serial_Number := Next_Serial_Number;
774 Next_Serial_Number := Next_Serial_Number + 1;
775 pragma Assert (Next_Serial_Number /= 0);
777 Self_ID.Common.LL.Thread := To_pthread_t (-1);
779 if not Single_Lock then
780 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
782 pragma Assert (Result = 0 or else Result = ENOMEM);
790 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
792 pragma Assert (Result = 0 or else Result = ENOMEM);
797 if not Single_Lock then
798 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
799 pragma Assert (Result = 0);
810 procedure Create_Task
812 Wrapper : System.Address;
813 Stack_Size : System.Parameters.Size_Type;
814 Priority : System.Any_Priority;
815 Succeeded : out Boolean)
817 Attributes : aliased pthread_attr_t;
818 Adjusted_Stack_Size : Interfaces.C.size_t;
819 Result : Interfaces.C.int;
822 Adjusted_Stack_Size :=
823 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
825 Result := pthread_attr_init (Attributes'Access);
826 pragma Assert (Result = 0 or else Result = ENOMEM);
834 pthread_attr_setstacksize
835 (Attributes'Access, Adjusted_Stack_Size);
836 pragma Assert (Result = 0);
839 pthread_attr_setdetachstate
840 (Attributes'Access, PTHREAD_CREATE_DETACHED);
841 pragma Assert (Result = 0);
843 -- Since the initial signal mask of a thread is inherited from the
844 -- creator, and the Environment task has all its signals masked, we
845 -- do not need to manipulate caller's signal mask at this point.
846 -- All tasks in RTS will have All_Tasks_Mask initially.
848 Result := pthread_create
849 (T.Common.LL.Thread'Access,
851 Thread_Body_Access (Wrapper),
854 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
858 Result := pthread_attr_destroy (Attributes'Access);
859 pragma Assert (Result = 0);
867 if T.Common.Task_Info /= null then
868 if T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU then
870 pthread_setaffinity_np
873 T.Common.Task_Info.CPU_Affinity'Access);
874 pragma Assert (Result = 0);
878 Result := pthread_attr_destroy (Attributes'Access);
879 pragma Assert (Result = 0);
881 Set_Priority (T, Priority);
888 procedure Finalize_TCB (T : Task_Id) is
889 Result : Interfaces.C.int;
891 Is_Self : constant Boolean := T = Self;
893 procedure Free is new
894 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
897 if not Single_Lock then
898 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
899 pragma Assert (Result = 0);
902 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
903 pragma Assert (Result = 0);
905 if T.Known_Tasks_Index /= -1 then
906 Known_Tasks (T.Known_Tasks_Index) := null;
908 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
920 procedure Exit_Task is
929 procedure Abort_Task (T : Task_Id) is
930 Result : Interfaces.C.int;
935 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
936 pragma Assert (Result = 0);
943 procedure Initialize (S : in out Suspension_Object) is
944 Result : Interfaces.C.int;
947 -- Initialize internal state (always to False (RM D.10(6)))
952 -- Initialize internal mutex
954 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
956 pragma Assert (Result = 0 or else Result = ENOMEM);
958 if Result = ENOMEM then
962 -- Initialize internal condition variable
964 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
966 pragma Assert (Result = 0 or else Result = ENOMEM);
969 Result := pthread_mutex_destroy (S.L'Access);
970 pragma Assert (Result = 0);
972 if Result = ENOMEM then
982 procedure Finalize (S : in out Suspension_Object) is
983 Result : Interfaces.C.int;
986 -- Destroy internal mutex
988 Result := pthread_mutex_destroy (S.L'Access);
989 pragma Assert (Result = 0);
991 -- Destroy internal condition variable
993 Result := pthread_cond_destroy (S.CV'Access);
994 pragma Assert (Result = 0);
1001 function Current_State (S : Suspension_Object) return Boolean is
1003 -- We do not want to use lock on this read operation. State is marked
1004 -- as Atomic so that we ensure that the value retrieved is correct.
1013 procedure Set_False (S : in out Suspension_Object) is
1014 Result : Interfaces.C.int;
1017 SSL.Abort_Defer.all;
1019 Result := pthread_mutex_lock (S.L'Access);
1020 pragma Assert (Result = 0);
1024 Result := pthread_mutex_unlock (S.L'Access);
1025 pragma Assert (Result = 0);
1027 SSL.Abort_Undefer.all;
1034 procedure Set_True (S : in out Suspension_Object) is
1035 Result : Interfaces.C.int;
1038 SSL.Abort_Defer.all;
1040 Result := pthread_mutex_lock (S.L'Access);
1041 pragma Assert (Result = 0);
1043 -- If there is already a task waiting on this suspension object then
1044 -- we resume it, leaving the state of the suspension object to False,
1045 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1046 -- the state to True.
1052 Result := pthread_cond_signal (S.CV'Access);
1053 pragma Assert (Result = 0);
1059 Result := pthread_mutex_unlock (S.L'Access);
1060 pragma Assert (Result = 0);
1062 SSL.Abort_Undefer.all;
1065 ------------------------
1066 -- Suspend_Until_True --
1067 ------------------------
1069 procedure Suspend_Until_True (S : in out Suspension_Object) is
1070 Result : Interfaces.C.int;
1073 SSL.Abort_Defer.all;
1075 Result := pthread_mutex_lock (S.L'Access);
1076 pragma Assert (Result = 0);
1080 -- Program_Error must be raised upon calling Suspend_Until_True
1081 -- if another task is already waiting on that suspension object
1084 Result := pthread_mutex_unlock (S.L'Access);
1085 pragma Assert (Result = 0);
1087 SSL.Abort_Undefer.all;
1089 raise Program_Error;
1091 -- Suspend the task if the state is False. Otherwise, the task
1092 -- continues its execution, and the state of the suspension object
1093 -- is set to False (ARM D.10 par. 9).
1099 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1102 Result := pthread_mutex_unlock (S.L'Access);
1103 pragma Assert (Result = 0);
1105 SSL.Abort_Undefer.all;
1107 end Suspend_Until_True;
1115 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1116 pragma Unreferenced (Self_ID);
1121 --------------------
1122 -- Check_No_Locks --
1123 --------------------
1125 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1126 pragma Unreferenced (Self_ID);
1131 ----------------------
1132 -- Environment_Task --
1133 ----------------------
1135 function Environment_Task return Task_Id is
1137 return Environment_Task_Id;
1138 end Environment_Task;
1144 function Suspend_Task
1146 Thread_Self : Thread_Id) return Boolean
1149 if T.Common.LL.Thread /= Thread_Self then
1150 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1160 function Resume_Task
1162 Thread_Self : Thread_Id) return Boolean
1165 if T.Common.LL.Thread /= Thread_Self then
1166 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1172 --------------------
1173 -- Stop_All_Tasks --
1174 --------------------
1176 procedure Stop_All_Tasks is
1185 function Stop_Task (T : ST.Task_Id) return Boolean is
1186 pragma Unreferenced (T);
1195 function Continue_Task (T : ST.Task_Id) return Boolean is
1196 pragma Unreferenced (T);
1205 procedure Initialize (Environment_Task : Task_Id) is
1206 act : aliased struct_sigaction;
1207 old_act : aliased struct_sigaction;
1208 Tmp_Set : aliased sigset_t;
1209 Result : Interfaces.C.int;
1210 -- Whether to use an alternate signal stack for stack overflows
1213 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1214 pragma Import (C, State, "__gnat_get_interrupt_state");
1215 -- Get interrupt state. Defined in a-init.c
1216 -- The input argument is the interrupt number,
1217 -- and the result is one of the following:
1219 Default : constant Character := 's';
1220 -- 'n' this interrupt not set by any Interrupt_State pragma
1221 -- 'u' Interrupt_State pragma set state to User
1222 -- 'r' Interrupt_State pragma set state to Runtime
1223 -- 's' Interrupt_State pragma set state to System (use "default"
1227 Environment_Task_Id := Environment_Task;
1229 Interrupt_Management.Initialize;
1231 -- Prepare the set of signals that should be unblocked in all tasks
1233 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1234 pragma Assert (Result = 0);
1236 for J in Interrupt_Management.Interrupt_ID loop
1237 if System.Interrupt_Management.Keep_Unmasked (J) then
1238 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1239 pragma Assert (Result = 0);
1243 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1244 pragma Assert (Result = 0);
1246 Result := pthread_condattr_init (Cond_Attr'Access);
1247 pragma Assert (Result = 0);
1249 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1251 -- Initialize the global RTS lock
1253 Specific.Initialize (Environment_Task);
1255 if Use_Alternate_Stack then
1256 Environment_Task.Common.Task_Alternate_Stack :=
1257 Alternate_Stack'Address;
1260 Enter_Task (Environment_Task);
1262 -- Install the abort-signal handler
1265 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1268 act.sa_handler := Abort_Handler'Address;
1270 Result := sigemptyset (Tmp_Set'Access);
1271 pragma Assert (Result = 0);
1272 act.sa_mask := Tmp_Set;
1276 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1277 act'Unchecked_Access,
1278 old_act'Unchecked_Access);
1279 pragma Assert (Result = 0);
1283 end System.Task_Primitives.Operations;