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-2011, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
41 with Ada.Unchecked_Conversion;
42 with Ada.Unchecked_Deallocation;
46 with System.Task_Info;
47 with System.Tasking.Debug;
48 with System.Interrupt_Management;
49 with System.OS_Primitives;
50 with System.Stack_Checking.Operations;
51 with System.Multiprocessors;
53 with System.Soft_Links;
54 -- We use System.Soft_Links instead of System.Tasking.Initialization
55 -- because the later is a higher level package that we shouldn't depend on.
56 -- For example when using the restricted run time, it is replaced by
57 -- System.Tasking.Restricted.Stages.
59 package body System.Task_Primitives.Operations is
61 package SSL renames System.Soft_Links;
62 package SC renames System.Stack_Checking.Operations;
64 use System.Tasking.Debug;
67 use System.OS_Interface;
68 use System.Parameters;
69 use System.OS_Primitives;
76 -- The followings are logically constants, but need to be initialized
79 Single_RTS_Lock : aliased RTS_Lock;
80 -- This is a lock to allow only one thread of control in the RTS at
81 -- a time; it is used to execute in mutual exclusion from all other tasks.
82 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
84 Environment_Task_Id : Task_Id;
85 -- A variable to hold Task_Id for the environment task
87 Unblocked_Signal_Mask : aliased sigset_t;
88 -- The set of signals that should be unblocked in all tasks
90 -- The followings are internal configuration constants needed
92 Next_Serial_Number : Task_Serial_Number := 100;
93 -- We start at 100 (reserve some special values for using in error checks)
95 Time_Slice_Val : Integer;
96 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
98 Dispatching_Policy : Character;
99 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
101 -- The following are effectively constants, but they need to be initialized
102 -- by calling a pthread_ function.
104 Mutex_Attr : aliased pthread_mutexattr_t;
105 Cond_Attr : aliased pthread_condattr_t;
107 Foreign_Task_Elaborated : aliased Boolean := True;
108 -- Used to identified fake tasks (i.e., non-Ada Threads)
110 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
111 -- Whether to use an alternate signal stack for stack overflows
113 Abort_Handler_Installed : Boolean := False;
114 -- True if a handler for the abort signal is installed
122 procedure Initialize (Environment_Task : Task_Id);
123 pragma Inline (Initialize);
124 -- Initialize various data needed by this package
126 function Is_Valid_Task return Boolean;
127 pragma Inline (Is_Valid_Task);
128 -- Does executing thread have a TCB?
130 procedure Set (Self_Id : Task_Id);
132 -- Set the self id for the current task
134 function Self return Task_Id;
135 pragma Inline (Self);
136 -- Return a pointer to the Ada Task Control Block of the calling task
140 package body Specific is separate;
141 -- The body of this package is target specific
143 ---------------------------------
144 -- Support for foreign threads --
145 ---------------------------------
147 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
148 -- Allocate and Initialize a new ATCB for the current Thread
150 function Register_Foreign_Thread
151 (Thread : Thread_Id) return Task_Id is separate;
153 -----------------------
154 -- Local Subprograms --
155 -----------------------
157 subtype unsigned_long is Interfaces.C.unsigned_long;
159 procedure Abort_Handler (signo : Signal);
161 function To_pthread_t is new Ada.Unchecked_Conversion
162 (unsigned_long, System.OS_Interface.pthread_t);
168 procedure Abort_Handler (signo : Signal) is
169 pragma Unreferenced (signo);
171 Self_Id : constant Task_Id := Self;
172 Result : Interfaces.C.int;
173 Old_Set : aliased sigset_t;
176 -- It's not safe to raise an exception when using GCC ZCX mechanism.
177 -- Note that we still need to install a signal handler, since in some
178 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
179 -- need to send the Abort signal to a task.
181 if ZCX_By_Default and then GCC_ZCX_Support then
185 if Self_Id.Deferral_Level = 0
186 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
187 and then not Self_Id.Aborting
189 Self_Id.Aborting := True;
191 -- Make sure signals used for RTS internal purpose are unmasked
196 Unblocked_Signal_Mask'Access,
198 pragma Assert (Result = 0);
200 raise Standard'Abort_Signal;
208 procedure Lock_RTS is
210 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
217 procedure Unlock_RTS is
219 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
226 -- The underlying thread system extends the memory (up to 2MB) when needed
228 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
229 pragma Unreferenced (T);
230 pragma Unreferenced (On);
239 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
241 return T.Common.LL.Thread;
248 function Self return Task_Id renames Specific.Self;
250 ---------------------
251 -- Initialize_Lock --
252 ---------------------
254 -- Note: mutexes and cond_variables needed per-task basis are initialized
255 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
256 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
257 -- status change of RTS. Therefore raising Storage_Error in the following
258 -- routines should be able to be handled safely.
260 procedure Initialize_Lock
261 (Prio : System.Any_Priority;
262 L : not null access Lock)
264 pragma Unreferenced (Prio);
266 Result : Interfaces.C.int;
269 Result := pthread_mutex_init (L, Mutex_Attr'Access);
271 pragma Assert (Result = 0 or else Result = ENOMEM);
273 if Result = ENOMEM then
274 raise Storage_Error with "Failed to allocate a lock";
278 procedure Initialize_Lock
279 (L : not null access RTS_Lock;
282 pragma Unreferenced (Level);
284 Result : Interfaces.C.int;
287 Result := pthread_mutex_init (L, Mutex_Attr'Access);
289 pragma Assert (Result = 0 or else Result = ENOMEM);
291 if Result = ENOMEM then
300 procedure Finalize_Lock (L : not null access Lock) is
301 Result : Interfaces.C.int;
303 Result := pthread_mutex_destroy (L);
304 pragma Assert (Result = 0);
307 procedure Finalize_Lock (L : not null access RTS_Lock) is
308 Result : Interfaces.C.int;
310 Result := pthread_mutex_destroy (L);
311 pragma Assert (Result = 0);
319 (L : not null access Lock;
320 Ceiling_Violation : out Boolean)
322 Result : Interfaces.C.int;
324 Result := pthread_mutex_lock (L);
325 Ceiling_Violation := Result = EINVAL;
327 -- Assume the cause of EINVAL is a priority ceiling violation
329 pragma Assert (Result = 0 or else Result = EINVAL);
333 (L : not null access RTS_Lock;
334 Global_Lock : Boolean := False)
336 Result : Interfaces.C.int;
338 if not Single_Lock or else Global_Lock then
339 Result := pthread_mutex_lock (L);
340 pragma Assert (Result = 0);
344 procedure Write_Lock (T : Task_Id) is
345 Result : Interfaces.C.int;
347 if not Single_Lock then
348 Result := pthread_mutex_lock (T.Common.LL.L'Access);
349 pragma Assert (Result = 0);
358 (L : not null access Lock;
359 Ceiling_Violation : out Boolean)
362 Write_Lock (L, Ceiling_Violation);
369 procedure Unlock (L : not null access Lock) is
370 Result : Interfaces.C.int;
372 Result := pthread_mutex_unlock (L);
373 pragma Assert (Result = 0);
377 (L : not null access RTS_Lock;
378 Global_Lock : Boolean := False)
380 Result : Interfaces.C.int;
382 if not Single_Lock or else Global_Lock then
383 Result := pthread_mutex_unlock (L);
384 pragma Assert (Result = 0);
388 procedure Unlock (T : Task_Id) is
389 Result : Interfaces.C.int;
391 if not Single_Lock then
392 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
393 pragma Assert (Result = 0);
401 -- Dynamic priority ceilings are not supported by the underlying system
403 procedure Set_Ceiling
404 (L : not null access Lock;
405 Prio : System.Any_Priority)
407 pragma Unreferenced (L, Prio);
418 Reason : System.Tasking.Task_States)
420 pragma Unreferenced (Reason);
422 Result : Interfaces.C.int;
425 pragma Assert (Self_ID = Self);
429 (cond => Self_ID.Common.LL.CV'Access,
430 mutex => (if Single_Lock
431 then Single_RTS_Lock'Access
432 else Self_ID.Common.LL.L'Access));
434 -- EINTR is not considered a failure
436 pragma Assert (Result = 0 or else Result = EINTR);
443 -- This is for use within the run-time system, so abort is
444 -- assumed to be already deferred, and the caller should be
445 -- holding its own ATCB lock.
447 procedure Timed_Sleep
450 Mode : ST.Delay_Modes;
451 Reason : System.Tasking.Task_States;
452 Timedout : out Boolean;
453 Yielded : out Boolean)
455 pragma Unreferenced (Reason);
457 Base_Time : constant Duration := Monotonic_Clock;
458 Check_Time : Duration := Base_Time;
460 Request : aliased timespec;
461 Result : Interfaces.C.int;
469 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
470 else 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;
479 pthread_cond_timedwait
480 (cond => Self_ID.Common.LL.CV'Access,
481 mutex => (if Single_Lock
482 then Single_RTS_Lock'Access
483 else Self_ID.Common.LL.L'Access),
484 abstime => Request'Access);
486 Check_Time := Monotonic_Clock;
487 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
489 if Result = 0 or else Result = EINTR then
491 -- Somebody may have called Wakeup for us
497 pragma Assert (Result = ETIMEDOUT);
506 -- This is for use in implementing delay statements, so we assume the
507 -- caller is abort-deferred but is holding no locks.
509 procedure Timed_Delay
512 Mode : ST.Delay_Modes)
514 Base_Time : constant Duration := Monotonic_Clock;
515 Check_Time : Duration := Base_Time;
517 Request : aliased timespec;
519 Result : Interfaces.C.int;
520 pragma Warnings (Off, Result);
527 Write_Lock (Self_ID);
531 then Time + Check_Time
532 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
534 if Abs_Time > Check_Time then
535 Request := To_Timespec (Abs_Time);
536 Self_ID.Common.State := Delay_Sleep;
539 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
542 pthread_cond_timedwait
543 (cond => Self_ID.Common.LL.CV'Access,
544 mutex => (if Single_Lock
545 then Single_RTS_Lock'Access
546 else Self_ID.Common.LL.L'Access),
547 abstime => Request'Access);
549 Check_Time := Monotonic_Clock;
550 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
552 pragma Assert (Result = 0 or else
553 Result = ETIMEDOUT or else
557 Self_ID.Common.State := Runnable;
566 Result := sched_yield;
569 ---------------------
570 -- Monotonic_Clock --
571 ---------------------
573 function Monotonic_Clock return Duration is
576 type timeval is array (1 .. 2) of C.long;
578 procedure timeval_to_duration
579 (T : not null access timeval;
580 sec : not null access C.long;
581 usec : not null access C.long);
582 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
584 Micro : constant := 10**6;
585 sec : aliased C.long;
586 usec : aliased C.long;
587 TV : aliased timeval;
590 function gettimeofday
591 (Tv : access timeval;
592 Tz : System.Address := System.Null_Address) return int;
593 pragma Import (C, gettimeofday, "gettimeofday");
596 Result := gettimeofday (TV'Access, System.Null_Address);
597 pragma Assert (Result = 0);
598 timeval_to_duration (TV'Access, sec'Access, usec'Access);
599 return Duration (sec) + Duration (usec) / Micro;
606 function RT_Resolution return Duration is
615 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
616 pragma Unreferenced (Reason);
617 Result : Interfaces.C.int;
619 Result := pthread_cond_signal (T.Common.LL.CV'Access);
620 pragma Assert (Result = 0);
627 procedure Yield (Do_Yield : Boolean := True) is
628 Result : Interfaces.C.int;
629 pragma Unreferenced (Result);
632 Result := sched_yield;
640 procedure Set_Priority
642 Prio : System.Any_Priority;
643 Loss_Of_Inheritance : Boolean := False)
645 pragma Unreferenced (Loss_Of_Inheritance);
647 Result : Interfaces.C.int;
648 Param : aliased struct_sched_param;
650 function Get_Policy (Prio : System.Any_Priority) return Character;
651 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
652 -- Get priority specific dispatching policy
654 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
655 -- Upper case first character of the policy name corresponding to the
656 -- task as set by a Priority_Specific_Dispatching pragma.
659 T.Common.Current_Priority := Prio;
661 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
663 Param.sched_priority := Interfaces.C.int (Prio) + 1;
665 if Dispatching_Policy = 'R'
666 or else Priority_Specific_Policy = 'R'
667 or else Time_Slice_Val > 0
670 pthread_setschedparam
671 (T.Common.LL.Thread, SCHED_RR, Param'Access);
673 elsif Dispatching_Policy = 'F'
674 or else Priority_Specific_Policy = 'F'
675 or else Time_Slice_Val = 0
678 pthread_setschedparam
679 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
682 Param.sched_priority := 0;
684 pthread_setschedparam
686 SCHED_OTHER, Param'Access);
689 pragma Assert (Result = 0 or else Result = EPERM);
696 function Get_Priority (T : Task_Id) return System.Any_Priority is
698 return T.Common.Current_Priority;
705 procedure Enter_Task (Self_ID : Task_Id) is
707 if Self_ID.Common.Task_Info /= null
708 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
710 raise Invalid_CPU_Number;
713 Self_ID.Common.LL.Thread := pthread_self;
714 Self_ID.Common.LL.LWP := lwp_self;
716 Specific.Set (Self_ID);
718 if Use_Alternate_Stack
719 and then Self_ID.Common.Task_Alternate_Stack /= Null_Address
722 Stack : aliased stack_t;
723 Result : Interfaces.C.int;
725 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
726 Stack.ss_size := Alternate_Stack_Size;
728 Result := sigaltstack (Stack'Access, null);
729 pragma Assert (Result = 0);
738 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
740 return new Ada_Task_Control_Block (Entry_Num);
747 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
749 -----------------------------
750 -- Register_Foreign_Thread --
751 -----------------------------
753 function Register_Foreign_Thread return Task_Id is
755 if Is_Valid_Task then
758 return Register_Foreign_Thread (pthread_self);
760 end Register_Foreign_Thread;
766 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
767 Result : Interfaces.C.int;
770 -- Give the task a unique serial number
772 Self_ID.Serial_Number := Next_Serial_Number;
773 Next_Serial_Number := Next_Serial_Number + 1;
774 pragma Assert (Next_Serial_Number /= 0);
776 Self_ID.Common.LL.Thread := To_pthread_t (-1);
778 if not Single_Lock then
779 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
781 pragma Assert (Result = 0 or else Result = ENOMEM);
789 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
791 pragma Assert (Result = 0 or else Result = ENOMEM);
796 if not Single_Lock then
797 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
798 pragma Assert (Result = 0);
809 procedure Create_Task
811 Wrapper : System.Address;
812 Stack_Size : System.Parameters.Size_Type;
813 Priority : System.Any_Priority;
814 Succeeded : out Boolean)
816 Attributes : aliased pthread_attr_t;
817 Adjusted_Stack_Size : Interfaces.C.size_t;
818 Result : Interfaces.C.int;
820 use type System.Multiprocessors.CPU_Range;
823 Adjusted_Stack_Size :=
824 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
826 Result := pthread_attr_init (Attributes'Access);
827 pragma Assert (Result = 0 or else Result = ENOMEM);
835 pthread_attr_setstacksize
836 (Attributes'Access, Adjusted_Stack_Size);
837 pragma Assert (Result = 0);
840 pthread_attr_setdetachstate
841 (Attributes'Access, PTHREAD_CREATE_DETACHED);
842 pragma Assert (Result = 0);
844 -- Set the required attributes for the creation of the thread
846 -- Note: Previously, we called pthread_setaffinity_np (after thread
847 -- creation but before thread activation) to set the affinity but it was
848 -- not behaving as expected. Setting the required attributes for the
849 -- creation of the thread works correctly and it is more appropriate.
851 -- Do nothing if required support not provided by the operating system
853 if pthread_attr_setaffinity_np'Address = System.Null_Address then
856 -- Support is available
858 elsif T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
860 CPU_Set : aliased cpu_set_t := (bits => (others => False));
862 CPU_Set.bits (Integer (T.Common.Base_CPU)) := True;
864 pthread_attr_setaffinity_np
868 pragma Assert (Result = 0);
873 elsif T.Common.Task_Info /= null
874 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
877 pthread_attr_setaffinity_np
880 T.Common.Task_Info.CPU_Affinity'Access);
881 pragma Assert (Result = 0);
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.
889 Result := pthread_create
890 (T.Common.LL.Thread'Access,
892 Thread_Body_Access (Wrapper),
895 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
899 Result := pthread_attr_destroy (Attributes'Access);
900 pragma Assert (Result = 0);
906 Result := pthread_attr_destroy (Attributes'Access);
907 pragma Assert (Result = 0);
909 Set_Priority (T, Priority);
916 procedure Finalize_TCB (T : Task_Id) is
917 Result : Interfaces.C.int;
919 Is_Self : constant Boolean := T = Self;
921 procedure Free is new
922 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
925 if not Single_Lock then
926 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
927 pragma Assert (Result = 0);
930 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
931 pragma Assert (Result = 0);
933 if T.Known_Tasks_Index /= -1 then
934 Known_Tasks (T.Known_Tasks_Index) := null;
936 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
948 procedure Exit_Task is
957 procedure Abort_Task (T : Task_Id) is
958 Result : Interfaces.C.int;
960 if Abort_Handler_Installed then
964 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
965 pragma Assert (Result = 0);
973 procedure Initialize (S : in out Suspension_Object) is
974 Result : Interfaces.C.int;
977 -- Initialize internal state (always to False (RM D.10(6)))
982 -- Initialize internal mutex
984 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
986 pragma Assert (Result = 0 or else Result = ENOMEM);
988 if Result = ENOMEM then
992 -- Initialize internal condition variable
994 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
996 pragma Assert (Result = 0 or else Result = ENOMEM);
999 Result := pthread_mutex_destroy (S.L'Access);
1000 pragma Assert (Result = 0);
1002 if Result = ENOMEM then
1003 raise Storage_Error;
1012 procedure Finalize (S : in out Suspension_Object) is
1013 Result : Interfaces.C.int;
1016 -- Destroy internal mutex
1018 Result := pthread_mutex_destroy (S.L'Access);
1019 pragma Assert (Result = 0);
1021 -- Destroy internal condition variable
1023 Result := pthread_cond_destroy (S.CV'Access);
1024 pragma Assert (Result = 0);
1031 function Current_State (S : Suspension_Object) return Boolean is
1033 -- We do not want to use lock on this read operation. State is marked
1034 -- as Atomic so that we ensure that the value retrieved is correct.
1043 procedure Set_False (S : in out Suspension_Object) is
1044 Result : Interfaces.C.int;
1047 SSL.Abort_Defer.all;
1049 Result := pthread_mutex_lock (S.L'Access);
1050 pragma Assert (Result = 0);
1054 Result := pthread_mutex_unlock (S.L'Access);
1055 pragma Assert (Result = 0);
1057 SSL.Abort_Undefer.all;
1064 procedure Set_True (S : in out Suspension_Object) is
1065 Result : Interfaces.C.int;
1068 SSL.Abort_Defer.all;
1070 Result := pthread_mutex_lock (S.L'Access);
1071 pragma Assert (Result = 0);
1073 -- If there is already a task waiting on this suspension object then
1074 -- we resume it, leaving the state of the suspension object to False,
1075 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1076 -- the state to True.
1082 Result := pthread_cond_signal (S.CV'Access);
1083 pragma Assert (Result = 0);
1089 Result := pthread_mutex_unlock (S.L'Access);
1090 pragma Assert (Result = 0);
1092 SSL.Abort_Undefer.all;
1095 ------------------------
1096 -- Suspend_Until_True --
1097 ------------------------
1099 procedure Suspend_Until_True (S : in out Suspension_Object) is
1100 Result : Interfaces.C.int;
1103 SSL.Abort_Defer.all;
1105 Result := pthread_mutex_lock (S.L'Access);
1106 pragma Assert (Result = 0);
1110 -- Program_Error must be raised upon calling Suspend_Until_True
1111 -- if another task is already waiting on that suspension object
1114 Result := pthread_mutex_unlock (S.L'Access);
1115 pragma Assert (Result = 0);
1117 SSL.Abort_Undefer.all;
1119 raise Program_Error;
1122 -- Suspend the task if the state is False. Otherwise, the task
1123 -- continues its execution, and the state of the suspension object
1124 -- is set to False (ARM D.10 par. 9).
1132 -- Loop in case pthread_cond_wait returns earlier than expected
1133 -- (e.g. in case of EINTR caused by a signal). This should not
1134 -- happen with the current Linux implementation of pthread, but
1135 -- POSIX does not guarantee it so this may change in future.
1137 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1138 pragma Assert (Result = 0 or else Result = EINTR);
1140 exit when not S.Waiting;
1144 Result := pthread_mutex_unlock (S.L'Access);
1145 pragma Assert (Result = 0);
1147 SSL.Abort_Undefer.all;
1149 end Suspend_Until_True;
1157 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1158 pragma Unreferenced (Self_ID);
1163 --------------------
1164 -- Check_No_Locks --
1165 --------------------
1167 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1168 pragma Unreferenced (Self_ID);
1173 ----------------------
1174 -- Environment_Task --
1175 ----------------------
1177 function Environment_Task return Task_Id is
1179 return Environment_Task_Id;
1180 end Environment_Task;
1186 function Suspend_Task
1188 Thread_Self : Thread_Id) return Boolean
1191 if T.Common.LL.Thread /= Thread_Self then
1192 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1202 function Resume_Task
1204 Thread_Self : Thread_Id) return Boolean
1207 if T.Common.LL.Thread /= Thread_Self then
1208 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1214 --------------------
1215 -- Stop_All_Tasks --
1216 --------------------
1218 procedure Stop_All_Tasks is
1227 function Stop_Task (T : ST.Task_Id) return Boolean is
1228 pragma Unreferenced (T);
1237 function Continue_Task (T : ST.Task_Id) return Boolean is
1238 pragma Unreferenced (T);
1247 procedure Initialize (Environment_Task : Task_Id) is
1248 act : aliased struct_sigaction;
1249 old_act : aliased struct_sigaction;
1250 Tmp_Set : aliased sigset_t;
1251 Result : Interfaces.C.int;
1252 -- Whether to use an alternate signal stack for stack overflows
1255 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1256 pragma Import (C, State, "__gnat_get_interrupt_state");
1257 -- Get interrupt state. Defined in a-init.c
1258 -- The input argument is the interrupt number,
1259 -- and the result is one of the following:
1261 Default : constant Character := 's';
1262 -- 'n' this interrupt not set by any Interrupt_State pragma
1263 -- 'u' Interrupt_State pragma set state to User
1264 -- 'r' Interrupt_State pragma set state to Runtime
1265 -- 's' Interrupt_State pragma set state to System (use "default"
1268 use type System.Multiprocessors.CPU_Range;
1271 Environment_Task_Id := Environment_Task;
1273 Interrupt_Management.Initialize;
1275 -- Prepare the set of signals that should be unblocked in all tasks
1277 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1278 pragma Assert (Result = 0);
1280 for J in Interrupt_Management.Interrupt_ID loop
1281 if System.Interrupt_Management.Keep_Unmasked (J) then
1282 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1283 pragma Assert (Result = 0);
1287 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1288 pragma Assert (Result = 0);
1290 Result := pthread_condattr_init (Cond_Attr'Access);
1291 pragma Assert (Result = 0);
1293 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1295 -- Initialize the global RTS lock
1297 Specific.Initialize (Environment_Task);
1299 if Use_Alternate_Stack then
1300 Environment_Task.Common.Task_Alternate_Stack :=
1301 Alternate_Stack'Address;
1304 -- Make environment task known here because it doesn't go through
1305 -- Activate_Tasks, which does it for all other tasks.
1307 Known_Tasks (Known_Tasks'First) := Environment_Task;
1308 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1310 Enter_Task (Environment_Task);
1313 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1316 act.sa_handler := Abort_Handler'Address;
1318 Result := sigemptyset (Tmp_Set'Access);
1319 pragma Assert (Result = 0);
1320 act.sa_mask := Tmp_Set;
1324 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1325 act'Unchecked_Access,
1326 old_act'Unchecked_Access);
1327 pragma Assert (Result = 0);
1328 Abort_Handler_Installed := True;
1331 -- pragma CPU for the environment task
1333 if pthread_setaffinity_np'Address /= System.Null_Address
1334 and then Environment_Task.Common.Base_CPU /=
1335 System.Multiprocessors.Not_A_Specific_CPU
1338 CPU_Set : aliased cpu_set_t := (bits => (others => False));
1340 CPU_Set.bits (Integer (Environment_Task.Common.Base_CPU)) := True;
1342 pthread_setaffinity_np
1343 (Environment_Task.Common.LL.Thread,
1346 pragma Assert (Result = 0);
1351 end System.Task_Primitives.Operations;