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 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);
883 -- Handle dispatching domains
885 elsif T.Common.Domain /= null then
887 CPU_Set : aliased cpu_set_t := (bits => (others => False));
890 -- Set the affinity to all the processors belonging to the
891 -- dispatching domain.
893 for Proc in T.Common.Domain'Range loop
894 CPU_Set.bits (Integer (Proc)) := T.Common.Domain (Proc);
898 pthread_attr_setaffinity_np
902 pragma Assert (Result = 0);
906 -- Since the initial signal mask of a thread is inherited from the
907 -- creator, and the Environment task has all its signals masked, we
908 -- do not need to manipulate caller's signal mask at this point.
909 -- All tasks in RTS will have All_Tasks_Mask initially.
911 Result := pthread_create
912 (T.Common.LL.Thread'Access,
914 Thread_Body_Access (Wrapper),
917 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
921 Result := pthread_attr_destroy (Attributes'Access);
922 pragma Assert (Result = 0);
928 Result := pthread_attr_destroy (Attributes'Access);
929 pragma Assert (Result = 0);
931 Set_Priority (T, Priority);
938 procedure Finalize_TCB (T : Task_Id) is
939 Result : Interfaces.C.int;
941 Is_Self : constant Boolean := T = Self;
943 procedure Free is new
944 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
947 if not Single_Lock then
948 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
949 pragma Assert (Result = 0);
952 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
953 pragma Assert (Result = 0);
955 if T.Known_Tasks_Index /= -1 then
956 Known_Tasks (T.Known_Tasks_Index) := null;
958 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
970 procedure Exit_Task is
979 procedure Abort_Task (T : Task_Id) is
980 Result : Interfaces.C.int;
982 if Abort_Handler_Installed then
986 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
987 pragma Assert (Result = 0);
995 procedure Initialize (S : in out Suspension_Object) is
996 Result : Interfaces.C.int;
999 -- Initialize internal state (always to False (RM D.10(6)))
1004 -- Initialize internal mutex
1006 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1008 pragma Assert (Result = 0 or else Result = ENOMEM);
1010 if Result = ENOMEM then
1011 raise Storage_Error;
1014 -- Initialize internal condition variable
1016 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1018 pragma Assert (Result = 0 or else Result = ENOMEM);
1021 Result := pthread_mutex_destroy (S.L'Access);
1022 pragma Assert (Result = 0);
1024 if Result = ENOMEM then
1025 raise Storage_Error;
1034 procedure Finalize (S : in out Suspension_Object) is
1035 Result : Interfaces.C.int;
1038 -- Destroy internal mutex
1040 Result := pthread_mutex_destroy (S.L'Access);
1041 pragma Assert (Result = 0);
1043 -- Destroy internal condition variable
1045 Result := pthread_cond_destroy (S.CV'Access);
1046 pragma Assert (Result = 0);
1053 function Current_State (S : Suspension_Object) return Boolean is
1055 -- We do not want to use lock on this read operation. State is marked
1056 -- as Atomic so that we ensure that the value retrieved is correct.
1065 procedure Set_False (S : in out Suspension_Object) is
1066 Result : Interfaces.C.int;
1069 SSL.Abort_Defer.all;
1071 Result := pthread_mutex_lock (S.L'Access);
1072 pragma Assert (Result = 0);
1076 Result := pthread_mutex_unlock (S.L'Access);
1077 pragma Assert (Result = 0);
1079 SSL.Abort_Undefer.all;
1086 procedure Set_True (S : in out Suspension_Object) is
1087 Result : Interfaces.C.int;
1090 SSL.Abort_Defer.all;
1092 Result := pthread_mutex_lock (S.L'Access);
1093 pragma Assert (Result = 0);
1095 -- If there is already a task waiting on this suspension object then
1096 -- we resume it, leaving the state of the suspension object to False,
1097 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1098 -- the state to True.
1104 Result := pthread_cond_signal (S.CV'Access);
1105 pragma Assert (Result = 0);
1111 Result := pthread_mutex_unlock (S.L'Access);
1112 pragma Assert (Result = 0);
1114 SSL.Abort_Undefer.all;
1117 ------------------------
1118 -- Suspend_Until_True --
1119 ------------------------
1121 procedure Suspend_Until_True (S : in out Suspension_Object) is
1122 Result : Interfaces.C.int;
1125 SSL.Abort_Defer.all;
1127 Result := pthread_mutex_lock (S.L'Access);
1128 pragma Assert (Result = 0);
1132 -- Program_Error must be raised upon calling Suspend_Until_True
1133 -- if another task is already waiting on that suspension object
1136 Result := pthread_mutex_unlock (S.L'Access);
1137 pragma Assert (Result = 0);
1139 SSL.Abort_Undefer.all;
1141 raise Program_Error;
1144 -- Suspend the task if the state is False. Otherwise, the task
1145 -- continues its execution, and the state of the suspension object
1146 -- is set to False (ARM D.10 par. 9).
1154 -- Loop in case pthread_cond_wait returns earlier than expected
1155 -- (e.g. in case of EINTR caused by a signal). This should not
1156 -- happen with the current Linux implementation of pthread, but
1157 -- POSIX does not guarantee it so this may change in future.
1159 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1160 pragma Assert (Result = 0 or else Result = EINTR);
1162 exit when not S.Waiting;
1166 Result := pthread_mutex_unlock (S.L'Access);
1167 pragma Assert (Result = 0);
1169 SSL.Abort_Undefer.all;
1171 end Suspend_Until_True;
1179 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1180 pragma Unreferenced (Self_ID);
1185 --------------------
1186 -- Check_No_Locks --
1187 --------------------
1189 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1190 pragma Unreferenced (Self_ID);
1195 ----------------------
1196 -- Environment_Task --
1197 ----------------------
1199 function Environment_Task return Task_Id is
1201 return Environment_Task_Id;
1202 end Environment_Task;
1208 function Suspend_Task
1210 Thread_Self : Thread_Id) return Boolean
1213 if T.Common.LL.Thread /= Thread_Self then
1214 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1224 function Resume_Task
1226 Thread_Self : Thread_Id) return Boolean
1229 if T.Common.LL.Thread /= Thread_Self then
1230 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1236 --------------------
1237 -- Stop_All_Tasks --
1238 --------------------
1240 procedure Stop_All_Tasks is
1249 function Stop_Task (T : ST.Task_Id) return Boolean is
1250 pragma Unreferenced (T);
1259 function Continue_Task (T : ST.Task_Id) return Boolean is
1260 pragma Unreferenced (T);
1269 procedure Initialize (Environment_Task : Task_Id) is
1270 act : aliased struct_sigaction;
1271 old_act : aliased struct_sigaction;
1272 Tmp_Set : aliased sigset_t;
1273 Result : Interfaces.C.int;
1274 -- Whether to use an alternate signal stack for stack overflows
1277 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1278 pragma Import (C, State, "__gnat_get_interrupt_state");
1279 -- Get interrupt state. Defined in a-init.c
1280 -- The input argument is the interrupt number,
1281 -- and the result is one of the following:
1283 Default : constant Character := 's';
1284 -- 'n' this interrupt not set by any Interrupt_State pragma
1285 -- 'u' Interrupt_State pragma set state to User
1286 -- 'r' Interrupt_State pragma set state to Runtime
1287 -- 's' Interrupt_State pragma set state to System (use "default"
1290 use type System.Multiprocessors.CPU_Range;
1293 Environment_Task_Id := Environment_Task;
1295 Interrupt_Management.Initialize;
1297 -- Prepare the set of signals that should be unblocked in all tasks
1299 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1300 pragma Assert (Result = 0);
1302 for J in Interrupt_Management.Interrupt_ID loop
1303 if System.Interrupt_Management.Keep_Unmasked (J) then
1304 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1305 pragma Assert (Result = 0);
1309 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1310 pragma Assert (Result = 0);
1312 Result := pthread_condattr_init (Cond_Attr'Access);
1313 pragma Assert (Result = 0);
1315 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1317 -- Initialize the global RTS lock
1319 Specific.Initialize (Environment_Task);
1321 if Use_Alternate_Stack then
1322 Environment_Task.Common.Task_Alternate_Stack :=
1323 Alternate_Stack'Address;
1326 -- Make environment task known here because it doesn't go through
1327 -- Activate_Tasks, which does it for all other tasks.
1329 Known_Tasks (Known_Tasks'First) := Environment_Task;
1330 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1332 Enter_Task (Environment_Task);
1335 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1338 act.sa_handler := Abort_Handler'Address;
1340 Result := sigemptyset (Tmp_Set'Access);
1341 pragma Assert (Result = 0);
1342 act.sa_mask := Tmp_Set;
1346 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1347 act'Unchecked_Access,
1348 old_act'Unchecked_Access);
1349 pragma Assert (Result = 0);
1350 Abort_Handler_Installed := True;
1353 -- pragma CPU and dispatching domains for the environment task
1355 Set_Task_Affinity (Environment_Task);
1358 -----------------------
1359 -- Set_Task_Affinity --
1360 -----------------------
1362 procedure Set_Task_Affinity (T : ST.Task_Id) is
1363 use type System.Multiprocessors.CPU_Range;
1366 if pthread_setaffinity_np'Address /= System.Null_Address then
1368 type cpu_set_t_ptr is access all cpu_set_t;
1370 CPU_Set : cpu_set_t_ptr := null;
1371 Result : Interfaces.C.int;
1374 -- We look at the specific CPU (Base_CPU) first, then at the
1375 -- Task_Info field, and finally at the assigned dispatching
1378 if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1380 -- Set the affinity to an unique CPU
1382 CPU_Set := new cpu_set_t'(bits => (others => False));
1383 CPU_Set.bits (Integer (T.Common.Base_CPU)) := True;
1387 elsif T.Common.Task_Info /= null
1388 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
1390 CPU_Set := T.Common.Task_Info.CPU_Affinity'Access;
1392 -- Handle dispatching domains
1394 elsif T.Common.Domain /= null and then
1395 (T.Common.Domain /= ST.System_Domain
1396 or else T.Common.Domain.all /=
1397 (Multiprocessors.CPU'First ..
1398 Multiprocessors.Number_Of_CPUs => True))
1400 -- Set the affinity to all the processors belonging to the
1401 -- dispatching domain. To avoid changing CPU affinities when
1402 -- not needed, we set the affinity only when assigning to a
1403 -- domain other than the default one, or when the default one
1404 -- has been modified.
1406 CPU_Set := new cpu_set_t'(bits => (others => False));
1408 for Proc in T.Common.Domain'Range loop
1409 CPU_Set.bits (Integer (Proc)) := T.Common.Domain (Proc);
1413 -- We set the new affinity if needed. Otherwise, the new task
1414 -- will inherit its creator's CPU affinity mask (according to
1415 -- the documentation of pthread_setaffinity_np), which is
1416 -- consistent with Ada's required semantics.
1418 if CPU_Set /= null then
1420 pthread_setaffinity_np
1421 (T.Common.LL.Thread, CPU_SETSIZE / 8, CPU_Set);
1422 pragma Assert (Result = 0);
1426 end Set_Task_Affinity;
1428 end System.Task_Primitives.Operations;