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;
875 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
876 System.OS_Interface.CPU_SET
877 (int (T.Common.Base_CPU), CPU_Set'Access);
879 pthread_attr_setaffinity_np
883 pragma Assert (Result = 0);
888 elsif T.Common.Task_Info /= null
889 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
892 pthread_attr_setaffinity_np
895 T.Common.Task_Info.CPU_Affinity'Access);
896 pragma Assert (Result = 0);
898 -- Handle dispatching domains
900 -- To avoid changing CPU affinities when not needed, we set the
901 -- affinity only when assigning to a domain other than the default
902 -- one, or when the default one has been modified.
904 elsif T.Common.Domain /= null and then
905 (T.Common.Domain /= ST.System_Domain
906 or else T.Common.Domain.all /=
907 (Multiprocessors.CPU'First ..
908 Multiprocessors.Number_Of_CPUs => True))
911 CPU_Set : aliased cpu_set_t;
914 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
916 -- Set the affinity to all the processors belonging to the
917 -- dispatching domain.
919 for Proc in T.Common.Domain'Range loop
920 if T.Common.Domain (Proc) then
921 System.OS_Interface.CPU_SET (int (Proc), CPU_Set'Access);
926 pthread_attr_setaffinity_np
930 pragma Assert (Result = 0);
934 -- Since the initial signal mask of a thread is inherited from the
935 -- creator, and the Environment task has all its signals masked, we
936 -- do not need to manipulate caller's signal mask at this point.
937 -- All tasks in RTS will have All_Tasks_Mask initially.
939 Result := pthread_create
940 (T.Common.LL.Thread'Access,
942 Thread_Body_Access (Wrapper),
945 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
949 Result := pthread_attr_destroy (Attributes'Access);
950 pragma Assert (Result = 0);
956 Result := pthread_attr_destroy (Attributes'Access);
957 pragma Assert (Result = 0);
959 Set_Priority (T, Priority);
966 procedure Finalize_TCB (T : Task_Id) is
967 Result : Interfaces.C.int;
969 Is_Self : constant Boolean := T = Self;
971 procedure Free is new
972 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
975 if not Single_Lock then
976 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
977 pragma Assert (Result = 0);
980 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
981 pragma Assert (Result = 0);
983 if T.Known_Tasks_Index /= -1 then
984 Known_Tasks (T.Known_Tasks_Index) := null;
986 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
998 procedure Exit_Task is
1000 Specific.Set (null);
1007 procedure Abort_Task (T : Task_Id) is
1008 Result : Interfaces.C.int;
1010 if Abort_Handler_Installed then
1013 (T.Common.LL.Thread,
1014 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1015 pragma Assert (Result = 0);
1023 procedure Initialize (S : in out Suspension_Object) is
1024 Result : Interfaces.C.int;
1027 -- Initialize internal state (always to False (RM D.10(6)))
1032 -- Initialize internal mutex
1034 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1036 pragma Assert (Result = 0 or else Result = ENOMEM);
1038 if Result = ENOMEM then
1039 raise Storage_Error;
1042 -- Initialize internal condition variable
1044 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1046 pragma Assert (Result = 0 or else Result = ENOMEM);
1049 Result := pthread_mutex_destroy (S.L'Access);
1050 pragma Assert (Result = 0);
1052 if Result = ENOMEM then
1053 raise Storage_Error;
1062 procedure Finalize (S : in out Suspension_Object) is
1063 Result : Interfaces.C.int;
1066 -- Destroy internal mutex
1068 Result := pthread_mutex_destroy (S.L'Access);
1069 pragma Assert (Result = 0);
1071 -- Destroy internal condition variable
1073 Result := pthread_cond_destroy (S.CV'Access);
1074 pragma Assert (Result = 0);
1081 function Current_State (S : Suspension_Object) return Boolean is
1083 -- We do not want to use lock on this read operation. State is marked
1084 -- as Atomic so that we ensure that the value retrieved is correct.
1093 procedure Set_False (S : in out Suspension_Object) is
1094 Result : Interfaces.C.int;
1097 SSL.Abort_Defer.all;
1099 Result := pthread_mutex_lock (S.L'Access);
1100 pragma Assert (Result = 0);
1104 Result := pthread_mutex_unlock (S.L'Access);
1105 pragma Assert (Result = 0);
1107 SSL.Abort_Undefer.all;
1114 procedure Set_True (S : in out Suspension_Object) is
1115 Result : Interfaces.C.int;
1118 SSL.Abort_Defer.all;
1120 Result := pthread_mutex_lock (S.L'Access);
1121 pragma Assert (Result = 0);
1123 -- If there is already a task waiting on this suspension object then
1124 -- we resume it, leaving the state of the suspension object to False,
1125 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1126 -- the state to True.
1132 Result := pthread_cond_signal (S.CV'Access);
1133 pragma Assert (Result = 0);
1139 Result := pthread_mutex_unlock (S.L'Access);
1140 pragma Assert (Result = 0);
1142 SSL.Abort_Undefer.all;
1145 ------------------------
1146 -- Suspend_Until_True --
1147 ------------------------
1149 procedure Suspend_Until_True (S : in out Suspension_Object) is
1150 Result : Interfaces.C.int;
1153 SSL.Abort_Defer.all;
1155 Result := pthread_mutex_lock (S.L'Access);
1156 pragma Assert (Result = 0);
1160 -- Program_Error must be raised upon calling Suspend_Until_True
1161 -- if another task is already waiting on that suspension object
1164 Result := pthread_mutex_unlock (S.L'Access);
1165 pragma Assert (Result = 0);
1167 SSL.Abort_Undefer.all;
1169 raise Program_Error;
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1182 -- Loop in case pthread_cond_wait returns earlier than expected
1183 -- (e.g. in case of EINTR caused by a signal). This should not
1184 -- happen with the current Linux implementation of pthread, but
1185 -- POSIX does not guarantee it so this may change in future.
1187 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1188 pragma Assert (Result = 0 or else Result = EINTR);
1190 exit when not S.Waiting;
1194 Result := pthread_mutex_unlock (S.L'Access);
1195 pragma Assert (Result = 0);
1197 SSL.Abort_Undefer.all;
1199 end Suspend_Until_True;
1207 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1208 pragma Unreferenced (Self_ID);
1213 --------------------
1214 -- Check_No_Locks --
1215 --------------------
1217 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1218 pragma Unreferenced (Self_ID);
1223 ----------------------
1224 -- Environment_Task --
1225 ----------------------
1227 function Environment_Task return Task_Id is
1229 return Environment_Task_Id;
1230 end Environment_Task;
1236 function Suspend_Task
1238 Thread_Self : Thread_Id) return Boolean
1241 if T.Common.LL.Thread /= Thread_Self then
1242 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1252 function Resume_Task
1254 Thread_Self : Thread_Id) return Boolean
1257 if T.Common.LL.Thread /= Thread_Self then
1258 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1264 --------------------
1265 -- Stop_All_Tasks --
1266 --------------------
1268 procedure Stop_All_Tasks is
1277 function Stop_Task (T : ST.Task_Id) return Boolean is
1278 pragma Unreferenced (T);
1287 function Continue_Task (T : ST.Task_Id) return Boolean is
1288 pragma Unreferenced (T);
1297 procedure Initialize (Environment_Task : Task_Id) is
1298 act : aliased struct_sigaction;
1299 old_act : aliased struct_sigaction;
1300 Tmp_Set : aliased sigset_t;
1301 Result : Interfaces.C.int;
1302 -- Whether to use an alternate signal stack for stack overflows
1305 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1306 pragma Import (C, State, "__gnat_get_interrupt_state");
1307 -- Get interrupt state. Defined in a-init.c
1308 -- The input argument is the interrupt number,
1309 -- and the result is one of the following:
1311 Default : constant Character := 's';
1312 -- 'n' this interrupt not set by any Interrupt_State pragma
1313 -- 'u' Interrupt_State pragma set state to User
1314 -- 'r' Interrupt_State pragma set state to Runtime
1315 -- 's' Interrupt_State pragma set state to System (use "default"
1318 use type System.Multiprocessors.CPU_Range;
1321 Environment_Task_Id := Environment_Task;
1323 Interrupt_Management.Initialize;
1325 -- Prepare the set of signals that should be unblocked in all tasks
1327 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1328 pragma Assert (Result = 0);
1330 for J in Interrupt_Management.Interrupt_ID loop
1331 if System.Interrupt_Management.Keep_Unmasked (J) then
1332 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1333 pragma Assert (Result = 0);
1337 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1338 pragma Assert (Result = 0);
1340 Result := pthread_condattr_init (Cond_Attr'Access);
1341 pragma Assert (Result = 0);
1343 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1345 -- Initialize the global RTS lock
1347 Specific.Initialize (Environment_Task);
1349 if Use_Alternate_Stack then
1350 Environment_Task.Common.Task_Alternate_Stack :=
1351 Alternate_Stack'Address;
1354 -- Make environment task known here because it doesn't go through
1355 -- Activate_Tasks, which does it for all other tasks.
1357 Known_Tasks (Known_Tasks'First) := Environment_Task;
1358 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1360 Enter_Task (Environment_Task);
1363 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1366 act.sa_handler := Abort_Handler'Address;
1368 Result := sigemptyset (Tmp_Set'Access);
1369 pragma Assert (Result = 0);
1370 act.sa_mask := Tmp_Set;
1374 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1375 act'Unchecked_Access,
1376 old_act'Unchecked_Access);
1377 pragma Assert (Result = 0);
1378 Abort_Handler_Installed := True;
1381 -- pragma CPU and dispatching domains for the environment task
1383 Set_Task_Affinity (Environment_Task);
1386 -----------------------
1387 -- Set_Task_Affinity --
1388 -----------------------
1390 procedure Set_Task_Affinity (T : ST.Task_Id) is
1391 use type System.Multiprocessors.CPU_Range;
1394 -- Do nothing if there is no support for setting affinities or the
1395 -- underlying thread has not yet been created. If the thread has not
1396 -- yet been created then the proper affinity will be set during its
1399 if pthread_setaffinity_np'Address /= System.Null_Address
1400 and then T.Common.LL.Thread /= Null_Thread_Id
1403 type cpu_set_t_ptr is access all cpu_set_t;
1404 CPU_Set : aliased cpu_set_t;
1405 CPU_Set_Ptr : cpu_set_t_ptr := null;
1407 Result : Interfaces.C.int;
1410 -- We look at the specific CPU (Base_CPU) first, then at the
1411 -- Task_Info field, and finally at the assigned dispatching
1414 if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1416 -- Set the affinity to an unique CPU
1417 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
1418 System.OS_Interface.CPU_SET
1419 (int (T.Common.Base_CPU), CPU_Set'Access);
1420 CPU_Set_Ptr := CPU_Set'Access;
1424 elsif T.Common.Task_Info /= null
1425 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
1427 CPU_Set_Ptr := T.Common.Task_Info.CPU_Affinity'Access;
1429 -- Handle dispatching domains
1431 elsif T.Common.Domain /= null and then
1432 (T.Common.Domain /= ST.System_Domain
1433 or else T.Common.Domain.all /=
1434 (Multiprocessors.CPU'First ..
1435 Multiprocessors.Number_Of_CPUs => True))
1437 -- Set the affinity to all the processors belonging to the
1438 -- dispatching domain. To avoid changing CPU affinities when
1439 -- not needed, we set the affinity only when assigning to a
1440 -- domain other than the default one, or when the default one
1441 -- has been modified.
1443 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
1445 for Proc in T.Common.Domain'Range loop
1446 System.OS_Interface.CPU_SET (int (Proc), CPU_Set'Access);
1449 CPU_Set_Ptr := CPU_Set'Access;
1452 -- We set the new affinity if needed. Otherwise, the new task
1453 -- will inherit its creator's CPU affinity mask (according to
1454 -- the documentation of pthread_setaffinity_np), which is
1455 -- consistent with Ada's required semantics.
1457 if CPU_Set_Ptr /= null then
1459 pthread_setaffinity_np
1460 (T.Common.LL.Thread, CPU_SETSIZE / 8, CPU_Set_Ptr);
1461 pragma Assert (Result = 0);
1465 end Set_Task_Affinity;
1467 end System.Task_Primitives.Operations;