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_Deallocation;
45 with System.Task_Info;
46 with System.Tasking.Debug;
47 with System.Interrupt_Management;
48 with System.OS_Primitives;
49 with System.Stack_Checking.Operations;
50 with System.Multiprocessors;
52 with System.Soft_Links;
53 -- We use System.Soft_Links instead of System.Tasking.Initialization
54 -- because the later is a higher level package that we shouldn't depend on.
55 -- For example when using the restricted run time, it is replaced by
56 -- System.Tasking.Restricted.Stages.
58 package body System.Task_Primitives.Operations is
60 package SSL renames System.Soft_Links;
61 package SC renames System.Stack_Checking.Operations;
63 use System.Tasking.Debug;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
75 -- The followings are logically constants, but need to be initialized
78 Single_RTS_Lock : aliased RTS_Lock;
79 -- This is a lock to allow only one thread of control in the RTS at
80 -- a time; it is used to execute in mutual exclusion from all other tasks.
81 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
83 Environment_Task_Id : Task_Id;
84 -- A variable to hold Task_Id for the environment task
86 Unblocked_Signal_Mask : aliased sigset_t;
87 -- The set of signals that should be unblocked in all tasks
89 -- The followings are internal configuration constants needed
91 Next_Serial_Number : Task_Serial_Number := 100;
92 -- We start at 100 (reserve some special values for using in error checks)
94 Time_Slice_Val : Integer;
95 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
97 Dispatching_Policy : Character;
98 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
100 -- The following are effectively constants, but they need to be initialized
101 -- by calling a pthread_ function.
103 Mutex_Attr : aliased pthread_mutexattr_t;
104 Cond_Attr : aliased pthread_condattr_t;
106 Foreign_Task_Elaborated : aliased Boolean := True;
107 -- Used to identified fake tasks (i.e., non-Ada Threads)
109 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
110 -- Whether to use an alternate signal stack for stack overflows
112 Abort_Handler_Installed : Boolean := False;
113 -- True if a handler for the abort signal is installed
115 Null_Thread_Id : constant pthread_t := pthread_t'Last;
116 -- Constant to indicate that the thread identifier has not yet been
125 procedure Initialize (Environment_Task : Task_Id);
126 pragma Inline (Initialize);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task return Boolean;
130 pragma Inline (Is_Valid_Task);
131 -- Does executing thread have a TCB?
133 procedure Set (Self_Id : Task_Id);
135 -- Set the self id for the current task
137 function Self return Task_Id;
138 pragma Inline (Self);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread : Thread_Id) return Task_Id is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 procedure Abort_Handler (signo : Signal);
166 procedure Abort_Handler (signo : Signal) is
167 pragma Unreferenced (signo);
169 Self_Id : constant Task_Id := Self;
170 Result : Interfaces.C.int;
171 Old_Set : aliased sigset_t;
174 -- It's not safe to raise an exception when using GCC ZCX mechanism.
175 -- Note that we still need to install a signal handler, since in some
176 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
177 -- need to send the Abort signal to a task.
179 if ZCX_By_Default then
183 if Self_Id.Deferral_Level = 0
184 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
185 and then not Self_Id.Aborting
187 Self_Id.Aborting := True;
189 -- Make sure signals used for RTS internal purpose are unmasked
194 Unblocked_Signal_Mask'Access,
196 pragma Assert (Result = 0);
198 raise Standard'Abort_Signal;
206 procedure Lock_RTS is
208 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
215 procedure Unlock_RTS is
217 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
224 -- The underlying thread system extends the memory (up to 2MB) when needed
226 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
227 pragma Unreferenced (T);
228 pragma Unreferenced (On);
237 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
239 return T.Common.LL.Thread;
246 function Self return Task_Id renames Specific.Self;
248 ---------------------
249 -- Initialize_Lock --
250 ---------------------
252 -- Note: mutexes and cond_variables needed per-task basis are initialized
253 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
254 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
255 -- status change of RTS. Therefore raising Storage_Error in the following
256 -- routines should be able to be handled safely.
258 procedure Initialize_Lock
259 (Prio : System.Any_Priority;
260 L : not null access Lock)
262 pragma Unreferenced (Prio);
264 Result : Interfaces.C.int;
267 Result := pthread_mutex_init (L, Mutex_Attr'Access);
269 pragma Assert (Result = 0 or else Result = ENOMEM);
271 if Result = ENOMEM then
272 raise Storage_Error with "Failed to allocate a lock";
276 procedure Initialize_Lock
277 (L : not null access RTS_Lock;
280 pragma Unreferenced (Level);
282 Result : Interfaces.C.int;
285 Result := pthread_mutex_init (L, Mutex_Attr'Access);
287 pragma Assert (Result = 0 or else Result = ENOMEM);
289 if Result = ENOMEM then
298 procedure Finalize_Lock (L : not null access Lock) is
299 Result : Interfaces.C.int;
301 Result := pthread_mutex_destroy (L);
302 pragma Assert (Result = 0);
305 procedure Finalize_Lock (L : not null access RTS_Lock) is
306 Result : Interfaces.C.int;
308 Result := pthread_mutex_destroy (L);
309 pragma Assert (Result = 0);
317 (L : not null access Lock;
318 Ceiling_Violation : out Boolean)
320 Result : Interfaces.C.int;
322 Result := pthread_mutex_lock (L);
323 Ceiling_Violation := Result = EINVAL;
325 -- Assume the cause of EINVAL is a priority ceiling violation
327 pragma Assert (Result = 0 or else Result = EINVAL);
331 (L : not null access RTS_Lock;
332 Global_Lock : Boolean := False)
334 Result : Interfaces.C.int;
336 if not Single_Lock or else Global_Lock then
337 Result := pthread_mutex_lock (L);
338 pragma Assert (Result = 0);
342 procedure Write_Lock (T : Task_Id) is
343 Result : Interfaces.C.int;
345 if not Single_Lock then
346 Result := pthread_mutex_lock (T.Common.LL.L'Access);
347 pragma Assert (Result = 0);
356 (L : not null access Lock;
357 Ceiling_Violation : out Boolean)
360 Write_Lock (L, Ceiling_Violation);
367 procedure Unlock (L : not null access Lock) is
368 Result : Interfaces.C.int;
370 Result := pthread_mutex_unlock (L);
371 pragma Assert (Result = 0);
375 (L : not null access RTS_Lock;
376 Global_Lock : Boolean := False)
378 Result : Interfaces.C.int;
380 if not Single_Lock or else Global_Lock then
381 Result := pthread_mutex_unlock (L);
382 pragma Assert (Result = 0);
386 procedure Unlock (T : Task_Id) is
387 Result : Interfaces.C.int;
389 if not Single_Lock then
390 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
391 pragma Assert (Result = 0);
399 -- Dynamic priority ceilings are not supported by the underlying system
401 procedure Set_Ceiling
402 (L : not null access Lock;
403 Prio : System.Any_Priority)
405 pragma Unreferenced (L, Prio);
416 Reason : System.Tasking.Task_States)
418 pragma Unreferenced (Reason);
420 Result : Interfaces.C.int;
423 pragma Assert (Self_ID = Self);
427 (cond => Self_ID.Common.LL.CV'Access,
428 mutex => (if Single_Lock
429 then Single_RTS_Lock'Access
430 else Self_ID.Common.LL.L'Access));
432 -- EINTR is not considered a failure
434 pragma Assert (Result = 0 or else Result = EINTR);
441 -- This is for use within the run-time system, so abort is
442 -- assumed to be already deferred, and the caller should be
443 -- holding its own ATCB lock.
445 procedure Timed_Sleep
448 Mode : ST.Delay_Modes;
449 Reason : System.Tasking.Task_States;
450 Timedout : out Boolean;
451 Yielded : out Boolean)
453 pragma Unreferenced (Reason);
455 Base_Time : constant Duration := Monotonic_Clock;
456 Check_Time : Duration := Base_Time;
458 Request : aliased timespec;
459 Result : Interfaces.C.int;
467 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
468 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
470 if Abs_Time > Check_Time then
471 Request := To_Timespec (Abs_Time);
474 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
477 pthread_cond_timedwait
478 (cond => Self_ID.Common.LL.CV'Access,
479 mutex => (if Single_Lock
480 then Single_RTS_Lock'Access
481 else Self_ID.Common.LL.L'Access),
482 abstime => Request'Access);
484 Check_Time := Monotonic_Clock;
485 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
487 if Result = 0 or else Result = EINTR then
489 -- Somebody may have called Wakeup for us
495 pragma Assert (Result = ETIMEDOUT);
504 -- This is for use in implementing delay statements, so we assume the
505 -- caller is abort-deferred but is holding no locks.
507 procedure Timed_Delay
510 Mode : ST.Delay_Modes)
512 Base_Time : constant Duration := Monotonic_Clock;
513 Check_Time : Duration := Base_Time;
515 Request : aliased timespec;
517 Result : Interfaces.C.int;
518 pragma Warnings (Off, Result);
525 Write_Lock (Self_ID);
529 then Time + Check_Time
530 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
532 if Abs_Time > Check_Time then
533 Request := To_Timespec (Abs_Time);
534 Self_ID.Common.State := Delay_Sleep;
537 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
540 pthread_cond_timedwait
541 (cond => Self_ID.Common.LL.CV'Access,
542 mutex => (if Single_Lock
543 then Single_RTS_Lock'Access
544 else Self_ID.Common.LL.L'Access),
545 abstime => Request'Access);
547 Check_Time := Monotonic_Clock;
548 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
550 pragma Assert (Result = 0 or else
551 Result = ETIMEDOUT or else
555 Self_ID.Common.State := Runnable;
564 Result := sched_yield;
567 ---------------------
568 -- Monotonic_Clock --
569 ---------------------
571 function Monotonic_Clock return Duration is
574 type timeval is array (1 .. 2) of C.long;
576 procedure timeval_to_duration
577 (T : not null access timeval;
578 sec : not null access C.long;
579 usec : not null access C.long);
580 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
582 Micro : constant := 10**6;
583 sec : aliased C.long;
584 usec : aliased C.long;
585 TV : aliased timeval;
588 function gettimeofday
589 (Tv : access timeval;
590 Tz : System.Address := System.Null_Address) return int;
591 pragma Import (C, gettimeofday, "gettimeofday");
594 Result := gettimeofday (TV'Access, System.Null_Address);
595 pragma Assert (Result = 0);
596 timeval_to_duration (TV'Access, sec'Access, usec'Access);
597 return Duration (sec) + Duration (usec) / Micro;
604 function RT_Resolution return Duration is
613 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
614 pragma Unreferenced (Reason);
615 Result : Interfaces.C.int;
617 Result := pthread_cond_signal (T.Common.LL.CV'Access);
618 pragma Assert (Result = 0);
625 procedure Yield (Do_Yield : Boolean := True) is
626 Result : Interfaces.C.int;
627 pragma Unreferenced (Result);
630 Result := sched_yield;
638 procedure Set_Priority
640 Prio : System.Any_Priority;
641 Loss_Of_Inheritance : Boolean := False)
643 pragma Unreferenced (Loss_Of_Inheritance);
645 Result : Interfaces.C.int;
646 Param : aliased struct_sched_param;
648 function Get_Policy (Prio : System.Any_Priority) return Character;
649 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
650 -- Get priority specific dispatching policy
652 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
653 -- Upper case first character of the policy name corresponding to the
654 -- task as set by a Priority_Specific_Dispatching pragma.
657 T.Common.Current_Priority := Prio;
659 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
661 Param.sched_priority := Interfaces.C.int (Prio) + 1;
663 if Dispatching_Policy = 'R'
664 or else Priority_Specific_Policy = 'R'
665 or else Time_Slice_Val > 0
668 pthread_setschedparam
669 (T.Common.LL.Thread, SCHED_RR, Param'Access);
671 elsif Dispatching_Policy = 'F'
672 or else Priority_Specific_Policy = 'F'
673 or else Time_Slice_Val = 0
676 pthread_setschedparam
677 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
680 Param.sched_priority := 0;
682 pthread_setschedparam
684 SCHED_OTHER, Param'Access);
687 pragma Assert (Result = 0 or else Result = EPERM);
694 function Get_Priority (T : Task_Id) return System.Any_Priority is
696 return T.Common.Current_Priority;
703 procedure Enter_Task (Self_ID : Task_Id) is
705 if Self_ID.Common.Task_Info /= null
706 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
708 raise Invalid_CPU_Number;
711 Self_ID.Common.LL.Thread := pthread_self;
712 Self_ID.Common.LL.LWP := lwp_self;
714 Specific.Set (Self_ID);
716 if Use_Alternate_Stack
717 and then Self_ID.Common.Task_Alternate_Stack /= Null_Address
720 Stack : aliased stack_t;
721 Result : Interfaces.C.int;
723 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
724 Stack.ss_size := Alternate_Stack_Size;
726 Result := sigaltstack (Stack'Access, null);
727 pragma Assert (Result = 0);
736 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
738 return new Ada_Task_Control_Block (Entry_Num);
745 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
747 -----------------------------
748 -- Register_Foreign_Thread --
749 -----------------------------
751 function Register_Foreign_Thread return Task_Id is
753 if Is_Valid_Task then
756 return Register_Foreign_Thread (pthread_self);
758 end Register_Foreign_Thread;
764 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
765 Result : Interfaces.C.int;
768 -- Give the task a unique serial number
770 Self_ID.Serial_Number := Next_Serial_Number;
771 Next_Serial_Number := Next_Serial_Number + 1;
772 pragma Assert (Next_Serial_Number /= 0);
774 Self_ID.Common.LL.Thread := Null_Thread_Id;
776 if not Single_Lock then
777 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
779 pragma Assert (Result = 0 or else Result = ENOMEM);
787 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
789 pragma Assert (Result = 0 or else Result = ENOMEM);
794 if not Single_Lock then
795 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
796 pragma Assert (Result = 0);
807 procedure Create_Task
809 Wrapper : System.Address;
810 Stack_Size : System.Parameters.Size_Type;
811 Priority : System.Any_Priority;
812 Succeeded : out Boolean)
814 Attributes : aliased pthread_attr_t;
815 Adjusted_Stack_Size : Interfaces.C.size_t;
816 Result : Interfaces.C.int;
818 use type System.Multiprocessors.CPU_Range;
821 -- Check whether both Dispatching_Domain and CPU are specified for the
822 -- task, and the CPU value is not contained within the range of
823 -- processors for the domain.
825 if T.Common.Domain /= null
826 and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
828 (T.Common.Base_CPU not in T.Common.Domain'Range
829 or else not T.Common.Domain (T.Common.Base_CPU))
835 Adjusted_Stack_Size :=
836 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
838 Result := pthread_attr_init (Attributes'Access);
839 pragma Assert (Result = 0 or else Result = ENOMEM);
847 pthread_attr_setstacksize
848 (Attributes'Access, Adjusted_Stack_Size);
849 pragma Assert (Result = 0);
852 pthread_attr_setdetachstate
853 (Attributes'Access, PTHREAD_CREATE_DETACHED);
854 pragma Assert (Result = 0);
856 -- Set the required attributes for the creation of the thread
858 -- Note: Previously, we called pthread_setaffinity_np (after thread
859 -- creation but before thread activation) to set the affinity but it was
860 -- not behaving as expected. Setting the required attributes for the
861 -- creation of the thread works correctly and it is more appropriate.
863 -- Do nothing if required support not provided by the operating system
865 if pthread_attr_setaffinity_np'Address = System.Null_Address then
868 -- Support is available
870 elsif T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
872 CPU_Set : aliased cpu_set_t := (bits => (others => False));
874 CPU_Set.bits (Integer (T.Common.Base_CPU)) := True;
876 pthread_attr_setaffinity_np
880 pragma Assert (Result = 0);
885 elsif T.Common.Task_Info /= null
886 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
889 pthread_attr_setaffinity_np
892 T.Common.Task_Info.CPU_Affinity'Access);
893 pragma Assert (Result = 0);
895 -- Handle dispatching domains
897 -- To avoid changing CPU affinities when not needed, we set the
898 -- affinity only when assigning to a domain other than the default
899 -- one, or when the default one has been modified.
901 elsif T.Common.Domain /= null and then
902 (T.Common.Domain /= ST.System_Domain
903 or else T.Common.Domain.all /=
904 (Multiprocessors.CPU'First ..
905 Multiprocessors.Number_Of_CPUs => True))
908 CPU_Set : aliased cpu_set_t := (bits => (others => False));
911 -- Set the affinity to all the processors belonging to the
912 -- dispatching domain.
914 for Proc in T.Common.Domain'Range loop
915 CPU_Set.bits (Integer (Proc)) := T.Common.Domain (Proc);
919 pthread_attr_setaffinity_np
923 pragma Assert (Result = 0);
927 -- Since the initial signal mask of a thread is inherited from the
928 -- creator, and the Environment task has all its signals masked, we
929 -- do not need to manipulate caller's signal mask at this point.
930 -- All tasks in RTS will have All_Tasks_Mask initially.
932 Result := pthread_create
933 (T.Common.LL.Thread'Access,
935 Thread_Body_Access (Wrapper),
938 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
942 Result := pthread_attr_destroy (Attributes'Access);
943 pragma Assert (Result = 0);
949 Result := pthread_attr_destroy (Attributes'Access);
950 pragma Assert (Result = 0);
952 Set_Priority (T, Priority);
959 procedure Finalize_TCB (T : Task_Id) is
960 Result : Interfaces.C.int;
962 Is_Self : constant Boolean := T = Self;
964 procedure Free is new
965 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
968 if not Single_Lock then
969 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
970 pragma Assert (Result = 0);
973 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
974 pragma Assert (Result = 0);
976 if T.Known_Tasks_Index /= -1 then
977 Known_Tasks (T.Known_Tasks_Index) := null;
979 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
991 procedure Exit_Task is
1000 procedure Abort_Task (T : Task_Id) is
1001 Result : Interfaces.C.int;
1003 if Abort_Handler_Installed then
1006 (T.Common.LL.Thread,
1007 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1008 pragma Assert (Result = 0);
1016 procedure Initialize (S : in out Suspension_Object) is
1017 Result : Interfaces.C.int;
1020 -- Initialize internal state (always to False (RM D.10(6)))
1025 -- Initialize internal mutex
1027 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1029 pragma Assert (Result = 0 or else Result = ENOMEM);
1031 if Result = ENOMEM then
1032 raise Storage_Error;
1035 -- Initialize internal condition variable
1037 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1039 pragma Assert (Result = 0 or else Result = ENOMEM);
1042 Result := pthread_mutex_destroy (S.L'Access);
1043 pragma Assert (Result = 0);
1045 if Result = ENOMEM then
1046 raise Storage_Error;
1055 procedure Finalize (S : in out Suspension_Object) is
1056 Result : Interfaces.C.int;
1059 -- Destroy internal mutex
1061 Result := pthread_mutex_destroy (S.L'Access);
1062 pragma Assert (Result = 0);
1064 -- Destroy internal condition variable
1066 Result := pthread_cond_destroy (S.CV'Access);
1067 pragma Assert (Result = 0);
1074 function Current_State (S : Suspension_Object) return Boolean is
1076 -- We do not want to use lock on this read operation. State is marked
1077 -- as Atomic so that we ensure that the value retrieved is correct.
1086 procedure Set_False (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);
1097 Result := pthread_mutex_unlock (S.L'Access);
1098 pragma Assert (Result = 0);
1100 SSL.Abort_Undefer.all;
1107 procedure Set_True (S : in out Suspension_Object) is
1108 Result : Interfaces.C.int;
1111 SSL.Abort_Defer.all;
1113 Result := pthread_mutex_lock (S.L'Access);
1114 pragma Assert (Result = 0);
1116 -- If there is already a task waiting on this suspension object then
1117 -- we resume it, leaving the state of the suspension object to False,
1118 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1119 -- the state to True.
1125 Result := pthread_cond_signal (S.CV'Access);
1126 pragma Assert (Result = 0);
1132 Result := pthread_mutex_unlock (S.L'Access);
1133 pragma Assert (Result = 0);
1135 SSL.Abort_Undefer.all;
1138 ------------------------
1139 -- Suspend_Until_True --
1140 ------------------------
1142 procedure Suspend_Until_True (S : in out Suspension_Object) is
1143 Result : Interfaces.C.int;
1146 SSL.Abort_Defer.all;
1148 Result := pthread_mutex_lock (S.L'Access);
1149 pragma Assert (Result = 0);
1153 -- Program_Error must be raised upon calling Suspend_Until_True
1154 -- if another task is already waiting on that suspension object
1157 Result := pthread_mutex_unlock (S.L'Access);
1158 pragma Assert (Result = 0);
1160 SSL.Abort_Undefer.all;
1162 raise Program_Error;
1165 -- Suspend the task if the state is False. Otherwise, the task
1166 -- continues its execution, and the state of the suspension object
1167 -- is set to False (ARM D.10 par. 9).
1175 -- Loop in case pthread_cond_wait returns earlier than expected
1176 -- (e.g. in case of EINTR caused by a signal). This should not
1177 -- happen with the current Linux implementation of pthread, but
1178 -- POSIX does not guarantee it so this may change in future.
1180 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1181 pragma Assert (Result = 0 or else Result = EINTR);
1183 exit when not S.Waiting;
1187 Result := pthread_mutex_unlock (S.L'Access);
1188 pragma Assert (Result = 0);
1190 SSL.Abort_Undefer.all;
1192 end Suspend_Until_True;
1200 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1201 pragma Unreferenced (Self_ID);
1206 --------------------
1207 -- Check_No_Locks --
1208 --------------------
1210 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1211 pragma Unreferenced (Self_ID);
1216 ----------------------
1217 -- Environment_Task --
1218 ----------------------
1220 function Environment_Task return Task_Id is
1222 return Environment_Task_Id;
1223 end Environment_Task;
1229 function Suspend_Task
1231 Thread_Self : Thread_Id) return Boolean
1234 if T.Common.LL.Thread /= Thread_Self then
1235 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1245 function Resume_Task
1247 Thread_Self : Thread_Id) return Boolean
1250 if T.Common.LL.Thread /= Thread_Self then
1251 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1257 --------------------
1258 -- Stop_All_Tasks --
1259 --------------------
1261 procedure Stop_All_Tasks is
1270 function Stop_Task (T : ST.Task_Id) return Boolean is
1271 pragma Unreferenced (T);
1280 function Continue_Task (T : ST.Task_Id) return Boolean is
1281 pragma Unreferenced (T);
1290 procedure Initialize (Environment_Task : Task_Id) is
1291 act : aliased struct_sigaction;
1292 old_act : aliased struct_sigaction;
1293 Tmp_Set : aliased sigset_t;
1294 Result : Interfaces.C.int;
1295 -- Whether to use an alternate signal stack for stack overflows
1298 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1299 pragma Import (C, State, "__gnat_get_interrupt_state");
1300 -- Get interrupt state. Defined in a-init.c
1301 -- The input argument is the interrupt number,
1302 -- and the result is one of the following:
1304 Default : constant Character := 's';
1305 -- 'n' this interrupt not set by any Interrupt_State pragma
1306 -- 'u' Interrupt_State pragma set state to User
1307 -- 'r' Interrupt_State pragma set state to Runtime
1308 -- 's' Interrupt_State pragma set state to System (use "default"
1311 use type System.Multiprocessors.CPU_Range;
1314 Environment_Task_Id := Environment_Task;
1316 Interrupt_Management.Initialize;
1318 -- Prepare the set of signals that should be unblocked in all tasks
1320 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1321 pragma Assert (Result = 0);
1323 for J in Interrupt_Management.Interrupt_ID loop
1324 if System.Interrupt_Management.Keep_Unmasked (J) then
1325 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1326 pragma Assert (Result = 0);
1330 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1331 pragma Assert (Result = 0);
1333 Result := pthread_condattr_init (Cond_Attr'Access);
1334 pragma Assert (Result = 0);
1336 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1338 -- Initialize the global RTS lock
1340 Specific.Initialize (Environment_Task);
1342 if Use_Alternate_Stack then
1343 Environment_Task.Common.Task_Alternate_Stack :=
1344 Alternate_Stack'Address;
1347 -- Make environment task known here because it doesn't go through
1348 -- Activate_Tasks, which does it for all other tasks.
1350 Known_Tasks (Known_Tasks'First) := Environment_Task;
1351 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1353 Enter_Task (Environment_Task);
1356 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1359 act.sa_handler := Abort_Handler'Address;
1361 Result := sigemptyset (Tmp_Set'Access);
1362 pragma Assert (Result = 0);
1363 act.sa_mask := Tmp_Set;
1367 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1368 act'Unchecked_Access,
1369 old_act'Unchecked_Access);
1370 pragma Assert (Result = 0);
1371 Abort_Handler_Installed := True;
1374 -- pragma CPU and dispatching domains for the environment task
1376 Set_Task_Affinity (Environment_Task);
1379 -----------------------
1380 -- Set_Task_Affinity --
1381 -----------------------
1383 procedure Set_Task_Affinity (T : ST.Task_Id) is
1384 use type System.Multiprocessors.CPU_Range;
1387 -- Do nothing if there is no support for setting affinities or the
1388 -- underlying thread has not yet been created. If the thread has not
1389 -- yet been created then the proper affinity will be set during its
1392 if pthread_setaffinity_np'Address /= System.Null_Address
1393 and then T.Common.LL.Thread /= Null_Thread_Id
1396 type cpu_set_t_ptr is access all cpu_set_t;
1398 CPU_Set : cpu_set_t_ptr := null;
1399 Result : Interfaces.C.int;
1402 -- We look at the specific CPU (Base_CPU) first, then at the
1403 -- Task_Info field, and finally at the assigned dispatching
1406 if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1408 -- Set the affinity to an unique CPU
1410 CPU_Set := new cpu_set_t'(bits => (others => False));
1411 CPU_Set.bits (Integer (T.Common.Base_CPU)) := True;
1415 elsif T.Common.Task_Info /= null
1416 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
1418 CPU_Set := T.Common.Task_Info.CPU_Affinity'Access;
1420 -- Handle dispatching domains
1422 elsif T.Common.Domain /= null and then
1423 (T.Common.Domain /= ST.System_Domain
1424 or else T.Common.Domain.all /=
1425 (Multiprocessors.CPU'First ..
1426 Multiprocessors.Number_Of_CPUs => True))
1428 -- Set the affinity to all the processors belonging to the
1429 -- dispatching domain. To avoid changing CPU affinities when
1430 -- not needed, we set the affinity only when assigning to a
1431 -- domain other than the default one, or when the default one
1432 -- has been modified.
1434 CPU_Set := new cpu_set_t'(bits => (others => False));
1436 for Proc in T.Common.Domain'Range loop
1437 CPU_Set.bits (Integer (Proc)) := T.Common.Domain (Proc);
1441 -- We set the new affinity if needed. Otherwise, the new task
1442 -- will inherit its creator's CPU affinity mask (according to
1443 -- the documentation of pthread_setaffinity_np), which is
1444 -- consistent with Ada's required semantics.
1446 if CPU_Set /= null then
1448 pthread_setaffinity_np
1449 (T.Common.LL.Thread, CPU_SETSIZE / 8, CPU_Set);
1450 pragma Assert (Result = 0);
1454 end Set_Task_Affinity;
1456 end System.Task_Primitives.Operations;