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.
43 with System.Task_Info;
44 with System.Tasking.Debug;
45 with System.Interrupt_Management;
46 with System.OS_Primitives;
47 with System.Stack_Checking.Operations;
48 with System.Multiprocessors;
50 with System.Soft_Links;
51 -- We use System.Soft_Links instead of System.Tasking.Initialization
52 -- because the later is a higher level package that we shouldn't depend on.
53 -- For example when using the restricted run time, it is replaced by
54 -- System.Tasking.Restricted.Stages.
56 package body System.Task_Primitives.Operations is
58 package SSL renames System.Soft_Links;
59 package SC renames System.Stack_Checking.Operations;
61 use System.Tasking.Debug;
64 use System.OS_Interface;
65 use System.Parameters;
66 use System.OS_Primitives;
73 -- The followings are logically constants, but need to be initialized
76 Single_RTS_Lock : aliased RTS_Lock;
77 -- This is a lock to allow only one thread of control in the RTS at
78 -- a time; it is used to execute in mutual exclusion from all other tasks.
79 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
81 Environment_Task_Id : Task_Id;
82 -- A variable to hold Task_Id for the environment task
84 Unblocked_Signal_Mask : aliased sigset_t;
85 -- The set of signals that should be unblocked in all tasks
87 -- The followings are internal configuration constants needed
89 Next_Serial_Number : Task_Serial_Number := 100;
90 -- We start at 100 (reserve some special values for using in error checks)
92 Time_Slice_Val : Integer;
93 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
95 Dispatching_Policy : Character;
96 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
98 Locking_Policy : Character;
99 pragma Import (C, Locking_Policy, "__gl_locking_policy");
101 Foreign_Task_Elaborated : aliased Boolean := True;
102 -- Used to identified fake tasks (i.e., non-Ada Threads)
104 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
105 -- Whether to use an alternate signal stack for stack overflows
107 Abort_Handler_Installed : Boolean := False;
108 -- True if a handler for the abort signal is installed
110 Null_Thread_Id : constant pthread_t := pthread_t'Last;
111 -- Constant to indicate that the thread identifier has not yet been
120 procedure Initialize (Environment_Task : Task_Id);
121 pragma Inline (Initialize);
122 -- Initialize various data needed by this package
124 function Is_Valid_Task return Boolean;
125 pragma Inline (Is_Valid_Task);
126 -- Does executing thread have a TCB?
128 procedure Set (Self_Id : Task_Id);
130 -- Set the self id for the current task
132 function Self return Task_Id;
133 pragma Inline (Self);
134 -- Return a pointer to the Ada Task Control Block of the calling task
138 package body Specific is separate;
139 -- The body of this package is target specific
141 ----------------------------------
142 -- ATCB allocation/deallocation --
143 ----------------------------------
145 package body ATCB_Allocation is separate;
146 -- The body of this package is shared across several targets
148 ---------------------------------
149 -- Support for foreign threads --
150 ---------------------------------
152 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
153 -- Allocate and Initialize a new ATCB for the current Thread
155 function Register_Foreign_Thread
156 (Thread : Thread_Id) return Task_Id is separate;
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 procedure Abort_Handler (signo : Signal);
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);
267 if Locking_Policy = 'R' then
269 RWlock_Attr : aliased pthread_rwlockattr_t;
270 Result : Interfaces.C.int;
273 -- Set the rwlock to prefer writer to avoid writers starvation
275 Result := pthread_rwlockattr_init (RWlock_Attr'Access);
276 pragma Assert (Result = 0);
278 Result := pthread_rwlockattr_setkind_np
280 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
281 pragma Assert (Result = 0);
283 Result := pthread_rwlock_init (L.RW'Access, RWlock_Attr'Access);
285 pragma Assert (Result = 0 or else Result = ENOMEM);
287 if Result = ENOMEM then
288 raise Storage_Error with "Failed to allocate a lock";
294 Mutex_Attr : aliased pthread_mutexattr_t;
295 Result : Interfaces.C.int;
298 Result := pthread_mutexattr_init (Mutex_Attr'Access);
299 pragma Assert (Result = 0);
301 Result := pthread_mutex_init (L.WO'Access, Mutex_Attr'Access);
303 pragma Assert (Result = 0 or else Result = ENOMEM);
305 if Result = ENOMEM then
306 raise Storage_Error with "Failed to allocate a lock";
312 procedure Initialize_Lock
313 (L : not null access RTS_Lock;
316 pragma Unreferenced (Level);
318 Mutex_Attr : aliased pthread_mutexattr_t;
319 Result : Interfaces.C.int;
322 Result := pthread_mutexattr_init (Mutex_Attr'Access);
323 pragma Assert (Result = 0);
325 Result := pthread_mutex_init (L, Mutex_Attr'Access);
327 pragma Assert (Result = 0 or else Result = ENOMEM);
329 if Result = ENOMEM then
338 procedure Finalize_Lock (L : not null access Lock) is
339 Result : Interfaces.C.int;
341 if Locking_Policy = 'R' then
342 Result := pthread_rwlock_destroy (L.RW'Access);
344 Result := pthread_mutex_destroy (L.WO'Access);
346 pragma Assert (Result = 0);
349 procedure Finalize_Lock (L : not null access RTS_Lock) is
350 Result : Interfaces.C.int;
352 Result := pthread_mutex_destroy (L);
353 pragma Assert (Result = 0);
361 (L : not null access Lock;
362 Ceiling_Violation : out Boolean)
364 Result : Interfaces.C.int;
366 if Locking_Policy = 'R' then
367 Result := pthread_rwlock_wrlock (L.RW'Access);
369 Result := pthread_mutex_lock (L.WO'Access);
372 Ceiling_Violation := Result = EINVAL;
374 -- Assume the cause of EINVAL is a priority ceiling violation
376 pragma Assert (Result = 0 or else Result = EINVAL);
380 (L : not null access RTS_Lock;
381 Global_Lock : Boolean := False)
383 Result : Interfaces.C.int;
385 if not Single_Lock or else Global_Lock then
386 Result := pthread_mutex_lock (L);
387 pragma Assert (Result = 0);
391 procedure Write_Lock (T : Task_Id) is
392 Result : Interfaces.C.int;
394 if not Single_Lock then
395 Result := pthread_mutex_lock (T.Common.LL.L'Access);
396 pragma Assert (Result = 0);
405 (L : not null access Lock;
406 Ceiling_Violation : out Boolean)
408 Result : Interfaces.C.int;
410 if Locking_Policy = 'R' then
411 Result := pthread_rwlock_rdlock (L.RW'Access);
413 Result := pthread_mutex_lock (L.WO'Access);
416 Ceiling_Violation := Result = EINVAL;
418 -- Assume the cause of EINVAL is a priority ceiling violation
420 pragma Assert (Result = 0 or else Result = EINVAL);
427 procedure Unlock (L : not null access Lock) is
428 Result : Interfaces.C.int;
430 if Locking_Policy = 'R' then
431 Result := pthread_rwlock_unlock (L.RW'Access);
433 Result := pthread_mutex_unlock (L.WO'Access);
435 pragma Assert (Result = 0);
439 (L : not null access RTS_Lock;
440 Global_Lock : Boolean := False)
442 Result : Interfaces.C.int;
444 if not Single_Lock or else Global_Lock then
445 Result := pthread_mutex_unlock (L);
446 pragma Assert (Result = 0);
450 procedure Unlock (T : Task_Id) is
451 Result : Interfaces.C.int;
453 if not Single_Lock then
454 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
455 pragma Assert (Result = 0);
463 -- Dynamic priority ceilings are not supported by the underlying system
465 procedure Set_Ceiling
466 (L : not null access Lock;
467 Prio : System.Any_Priority)
469 pragma Unreferenced (L, Prio);
480 Reason : System.Tasking.Task_States)
482 pragma Unreferenced (Reason);
484 Result : Interfaces.C.int;
487 pragma Assert (Self_ID = Self);
491 (cond => Self_ID.Common.LL.CV'Access,
492 mutex => (if Single_Lock
493 then Single_RTS_Lock'Access
494 else Self_ID.Common.LL.L'Access));
496 -- EINTR is not considered a failure
498 pragma Assert (Result = 0 or else Result = EINTR);
505 -- This is for use within the run-time system, so abort is
506 -- assumed to be already deferred, and the caller should be
507 -- holding its own ATCB lock.
509 procedure Timed_Sleep
512 Mode : ST.Delay_Modes;
513 Reason : System.Tasking.Task_States;
514 Timedout : out Boolean;
515 Yielded : out Boolean)
517 pragma Unreferenced (Reason);
519 Base_Time : constant Duration := Monotonic_Clock;
520 Check_Time : Duration := Base_Time;
522 Request : aliased timespec;
523 Result : Interfaces.C.int;
531 then Duration'Min (Time, Max_Sensible_Delay) + 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);
538 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
541 pthread_cond_timedwait
542 (cond => Self_ID.Common.LL.CV'Access,
543 mutex => (if Single_Lock
544 then Single_RTS_Lock'Access
545 else Self_ID.Common.LL.L'Access),
546 abstime => Request'Access);
548 Check_Time := Monotonic_Clock;
549 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
551 if Result = 0 or else Result = EINTR then
553 -- Somebody may have called Wakeup for us
559 pragma Assert (Result = ETIMEDOUT);
568 -- This is for use in implementing delay statements, so we assume the
569 -- caller is abort-deferred but is holding no locks.
571 procedure Timed_Delay
574 Mode : ST.Delay_Modes)
576 Base_Time : constant Duration := Monotonic_Clock;
577 Check_Time : Duration := Base_Time;
579 Request : aliased timespec;
581 Result : Interfaces.C.int;
582 pragma Warnings (Off, Result);
589 Write_Lock (Self_ID);
593 then Time + Check_Time
594 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
596 if Abs_Time > Check_Time then
597 Request := To_Timespec (Abs_Time);
598 Self_ID.Common.State := Delay_Sleep;
601 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
604 pthread_cond_timedwait
605 (cond => Self_ID.Common.LL.CV'Access,
606 mutex => (if Single_Lock
607 then Single_RTS_Lock'Access
608 else Self_ID.Common.LL.L'Access),
609 abstime => Request'Access);
611 Check_Time := Monotonic_Clock;
612 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
614 pragma Assert (Result = 0 or else
615 Result = ETIMEDOUT or else
619 Self_ID.Common.State := Runnable;
628 Result := sched_yield;
631 ---------------------
632 -- Monotonic_Clock --
633 ---------------------
635 function Monotonic_Clock return Duration is
638 type timeval is array (1 .. 2) of C.long;
640 procedure timeval_to_duration
641 (T : not null access timeval;
642 sec : not null access C.long;
643 usec : not null access C.long);
644 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
646 Micro : constant := 10**6;
647 sec : aliased C.long;
648 usec : aliased C.long;
649 TV : aliased timeval;
652 function gettimeofday
653 (Tv : access timeval;
654 Tz : System.Address := System.Null_Address) return int;
655 pragma Import (C, gettimeofday, "gettimeofday");
658 Result := gettimeofday (TV'Access, System.Null_Address);
659 pragma Assert (Result = 0);
660 timeval_to_duration (TV'Access, sec'Access, usec'Access);
661 return Duration (sec) + Duration (usec) / Micro;
668 function RT_Resolution return Duration is
677 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
678 pragma Unreferenced (Reason);
679 Result : Interfaces.C.int;
681 Result := pthread_cond_signal (T.Common.LL.CV'Access);
682 pragma Assert (Result = 0);
689 procedure Yield (Do_Yield : Boolean := True) is
690 Result : Interfaces.C.int;
691 pragma Unreferenced (Result);
694 Result := sched_yield;
702 procedure Set_Priority
704 Prio : System.Any_Priority;
705 Loss_Of_Inheritance : Boolean := False)
707 pragma Unreferenced (Loss_Of_Inheritance);
709 Result : Interfaces.C.int;
710 Param : aliased struct_sched_param;
712 function Get_Policy (Prio : System.Any_Priority) return Character;
713 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
714 -- Get priority specific dispatching policy
716 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
717 -- Upper case first character of the policy name corresponding to the
718 -- task as set by a Priority_Specific_Dispatching pragma.
721 T.Common.Current_Priority := Prio;
723 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
725 Param.sched_priority := Interfaces.C.int (Prio) + 1;
727 if Dispatching_Policy = 'R'
728 or else Priority_Specific_Policy = 'R'
729 or else Time_Slice_Val > 0
732 pthread_setschedparam
733 (T.Common.LL.Thread, SCHED_RR, Param'Access);
735 elsif Dispatching_Policy = 'F'
736 or else Priority_Specific_Policy = 'F'
737 or else Time_Slice_Val = 0
740 pthread_setschedparam
741 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
744 Param.sched_priority := 0;
746 pthread_setschedparam
748 SCHED_OTHER, Param'Access);
751 pragma Assert (Result = 0 or else Result = EPERM);
758 function Get_Priority (T : Task_Id) return System.Any_Priority is
760 return T.Common.Current_Priority;
767 procedure Enter_Task (Self_ID : Task_Id) is
769 if Self_ID.Common.Task_Info /= null
770 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
772 raise Invalid_CPU_Number;
775 Self_ID.Common.LL.Thread := pthread_self;
776 Self_ID.Common.LL.LWP := lwp_self;
778 Specific.Set (Self_ID);
780 if Use_Alternate_Stack
781 and then Self_ID.Common.Task_Alternate_Stack /= Null_Address
784 Stack : aliased stack_t;
785 Result : Interfaces.C.int;
787 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
788 Stack.ss_size := Alternate_Stack_Size;
790 Result := sigaltstack (Stack'Access, null);
791 pragma Assert (Result = 0);
800 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
802 -----------------------------
803 -- Register_Foreign_Thread --
804 -----------------------------
806 function Register_Foreign_Thread return Task_Id is
808 if Is_Valid_Task then
811 return Register_Foreign_Thread (pthread_self);
813 end Register_Foreign_Thread;
819 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
820 Mutex_Attr : aliased pthread_mutexattr_t;
821 Cond_Attr : aliased pthread_condattr_t;
822 Result : Interfaces.C.int;
825 -- Give the task a unique serial number
827 Self_ID.Serial_Number := Next_Serial_Number;
828 Next_Serial_Number := Next_Serial_Number + 1;
829 pragma Assert (Next_Serial_Number /= 0);
831 Self_ID.Common.LL.Thread := Null_Thread_Id;
833 if not Single_Lock then
834 Result := pthread_mutexattr_init (Mutex_Attr'Access);
835 pragma Assert (Result = 0);
838 pthread_mutex_init (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
839 pragma Assert (Result = 0 or else Result = ENOMEM);
847 Result := pthread_condattr_init (Cond_Attr'Access);
848 pragma Assert (Result = 0);
851 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
852 pragma Assert (Result = 0 or else Result = ENOMEM);
857 if not Single_Lock then
858 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
859 pragma Assert (Result = 0);
870 procedure Create_Task
872 Wrapper : System.Address;
873 Stack_Size : System.Parameters.Size_Type;
874 Priority : System.Any_Priority;
875 Succeeded : out Boolean)
877 Attributes : aliased pthread_attr_t;
878 Adjusted_Stack_Size : Interfaces.C.size_t;
879 Result : Interfaces.C.int;
881 use type System.Multiprocessors.CPU_Range;
884 -- Check whether both Dispatching_Domain and CPU are specified for
885 -- the task, and the CPU value is not contained within the range of
886 -- processors for the domain.
888 if T.Common.Domain /= null
889 and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
891 (T.Common.Base_CPU not in T.Common.Domain'Range
892 or else not T.Common.Domain (T.Common.Base_CPU))
898 Adjusted_Stack_Size :=
899 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
901 Result := pthread_attr_init (Attributes'Access);
902 pragma Assert (Result = 0 or else Result = ENOMEM);
910 pthread_attr_setstacksize (Attributes'Access, Adjusted_Stack_Size);
911 pragma Assert (Result = 0);
914 pthread_attr_setdetachstate
915 (Attributes'Access, PTHREAD_CREATE_DETACHED);
916 pragma Assert (Result = 0);
918 -- Set the required attributes for the creation of the thread
920 -- Note: Previously, we called pthread_setaffinity_np (after thread
921 -- creation but before thread activation) to set the affinity but it was
922 -- not behaving as expected. Setting the required attributes for the
923 -- creation of the thread works correctly and it is more appropriate.
925 -- Do nothing if required support not provided by the operating system
927 if pthread_attr_setaffinity_np'Address = System.Null_Address then
930 -- Support is available
932 elsif T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
934 CPUs : constant size_t :=
936 (System.Multiprocessors.Number_Of_CPUs);
937 CPU_Set : constant cpu_set_t_ptr := CPU_ALLOC (CPUs);
938 Size : constant size_t := CPU_ALLOC_SIZE (CPUs);
941 CPU_ZERO (Size, CPU_Set);
942 System.OS_Interface.CPU_SET
943 (int (T.Common.Base_CPU), Size, CPU_Set);
945 pthread_attr_setaffinity_np (Attributes'Access, Size, CPU_Set);
946 pragma Assert (Result = 0);
953 elsif T.Common.Task_Info /= null then
955 pthread_attr_setaffinity_np
958 T.Common.Task_Info.CPU_Affinity'Access);
959 pragma Assert (Result = 0);
961 -- Handle dispatching domains
963 -- To avoid changing CPU affinities when not needed, we set the
964 -- affinity only when assigning to a domain other than the default
965 -- one, or when the default one has been modified.
967 elsif T.Common.Domain /= null and then
968 (T.Common.Domain /= ST.System_Domain
969 or else T.Common.Domain.all /=
970 (Multiprocessors.CPU'First ..
971 Multiprocessors.Number_Of_CPUs => True))
974 CPUs : constant size_t :=
976 (System.Multiprocessors.Number_Of_CPUs);
977 CPU_Set : constant cpu_set_t_ptr := CPU_ALLOC (CPUs);
978 Size : constant size_t := CPU_ALLOC_SIZE (CPUs);
981 CPU_ZERO (Size, CPU_Set);
983 -- Set the affinity to all the processors belonging to the
984 -- dispatching domain.
986 for Proc in T.Common.Domain'Range loop
987 if T.Common.Domain (Proc) then
988 System.OS_Interface.CPU_SET (int (Proc), Size, CPU_Set);
993 pthread_attr_setaffinity_np (Attributes'Access, Size, CPU_Set);
994 pragma Assert (Result = 0);
1000 -- Since the initial signal mask of a thread is inherited from the
1001 -- creator, and the Environment task has all its signals masked, we
1002 -- do not need to manipulate caller's signal mask at this point.
1003 -- All tasks in RTS will have All_Tasks_Mask initially.
1005 Result := pthread_create
1006 (T.Common.LL.Thread'Access,
1008 Thread_Body_Access (Wrapper),
1012 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
1016 Result := pthread_attr_destroy (Attributes'Access);
1017 pragma Assert (Result = 0);
1023 Result := pthread_attr_destroy (Attributes'Access);
1024 pragma Assert (Result = 0);
1026 Set_Priority (T, Priority);
1033 procedure Finalize_TCB (T : Task_Id) is
1034 Result : Interfaces.C.int;
1037 if not Single_Lock then
1038 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1039 pragma Assert (Result = 0);
1042 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1043 pragma Assert (Result = 0);
1045 if T.Known_Tasks_Index /= -1 then
1046 Known_Tasks (T.Known_Tasks_Index) := null;
1049 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
1051 ATCB_Allocation.Free_ATCB (T);
1058 procedure Exit_Task is
1060 Specific.Set (null);
1067 procedure Abort_Task (T : Task_Id) is
1068 Result : Interfaces.C.int;
1070 if Abort_Handler_Installed then
1073 (T.Common.LL.Thread,
1074 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1075 pragma Assert (Result = 0);
1083 procedure Initialize (S : in out Suspension_Object) is
1084 Mutex_Attr : aliased pthread_mutexattr_t;
1085 Cond_Attr : aliased pthread_condattr_t;
1086 Result : Interfaces.C.int;
1089 -- Initialize internal state (always to False (RM D.10(6)))
1094 -- Initialize internal mutex
1096 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1097 pragma Assert (Result = 0);
1099 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1101 pragma Assert (Result = 0 or else Result = ENOMEM);
1103 if Result = ENOMEM then
1104 raise Storage_Error;
1107 -- Initialize internal condition variable
1109 Result := pthread_condattr_init (Cond_Attr'Access);
1110 pragma Assert (Result = 0);
1112 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1114 pragma Assert (Result = 0 or else Result = ENOMEM);
1117 Result := pthread_mutex_destroy (S.L'Access);
1118 pragma Assert (Result = 0);
1120 if Result = ENOMEM then
1121 raise Storage_Error;
1130 procedure Finalize (S : in out Suspension_Object) is
1131 Result : Interfaces.C.int;
1134 -- Destroy internal mutex
1136 Result := pthread_mutex_destroy (S.L'Access);
1137 pragma Assert (Result = 0);
1139 -- Destroy internal condition variable
1141 Result := pthread_cond_destroy (S.CV'Access);
1142 pragma Assert (Result = 0);
1149 function Current_State (S : Suspension_Object) return Boolean is
1151 -- We do not want to use lock on this read operation. State is marked
1152 -- as Atomic so that we ensure that the value retrieved is correct.
1161 procedure Set_False (S : in out Suspension_Object) is
1162 Result : Interfaces.C.int;
1165 SSL.Abort_Defer.all;
1167 Result := pthread_mutex_lock (S.L'Access);
1168 pragma Assert (Result = 0);
1172 Result := pthread_mutex_unlock (S.L'Access);
1173 pragma Assert (Result = 0);
1175 SSL.Abort_Undefer.all;
1182 procedure Set_True (S : in out Suspension_Object) is
1183 Result : Interfaces.C.int;
1186 SSL.Abort_Defer.all;
1188 Result := pthread_mutex_lock (S.L'Access);
1189 pragma Assert (Result = 0);
1191 -- If there is already a task waiting on this suspension object then
1192 -- we resume it, leaving the state of the suspension object to False,
1193 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1194 -- the state to True.
1200 Result := pthread_cond_signal (S.CV'Access);
1201 pragma Assert (Result = 0);
1207 Result := pthread_mutex_unlock (S.L'Access);
1208 pragma Assert (Result = 0);
1210 SSL.Abort_Undefer.all;
1213 ------------------------
1214 -- Suspend_Until_True --
1215 ------------------------
1217 procedure Suspend_Until_True (S : in out Suspension_Object) is
1218 Result : Interfaces.C.int;
1221 SSL.Abort_Defer.all;
1223 Result := pthread_mutex_lock (S.L'Access);
1224 pragma Assert (Result = 0);
1228 -- Program_Error must be raised upon calling Suspend_Until_True
1229 -- if another task is already waiting on that suspension object
1232 Result := pthread_mutex_unlock (S.L'Access);
1233 pragma Assert (Result = 0);
1235 SSL.Abort_Undefer.all;
1237 raise Program_Error;
1240 -- Suspend the task if the state is False. Otherwise, the task
1241 -- continues its execution, and the state of the suspension object
1242 -- is set to False (ARM D.10 par. 9).
1250 -- Loop in case pthread_cond_wait returns earlier than expected
1251 -- (e.g. in case of EINTR caused by a signal). This should not
1252 -- happen with the current Linux implementation of pthread, but
1253 -- POSIX does not guarantee it so this may change in future.
1255 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1256 pragma Assert (Result = 0 or else Result = EINTR);
1258 exit when not S.Waiting;
1262 Result := pthread_mutex_unlock (S.L'Access);
1263 pragma Assert (Result = 0);
1265 SSL.Abort_Undefer.all;
1267 end Suspend_Until_True;
1275 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1276 pragma Unreferenced (Self_ID);
1281 --------------------
1282 -- Check_No_Locks --
1283 --------------------
1285 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1286 pragma Unreferenced (Self_ID);
1291 ----------------------
1292 -- Environment_Task --
1293 ----------------------
1295 function Environment_Task return Task_Id is
1297 return Environment_Task_Id;
1298 end Environment_Task;
1304 function Suspend_Task
1306 Thread_Self : Thread_Id) return Boolean
1309 if T.Common.LL.Thread /= Thread_Self then
1310 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1320 function Resume_Task
1322 Thread_Self : Thread_Id) return Boolean
1325 if T.Common.LL.Thread /= Thread_Self then
1326 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1332 --------------------
1333 -- Stop_All_Tasks --
1334 --------------------
1336 procedure Stop_All_Tasks is
1345 function Stop_Task (T : ST.Task_Id) return Boolean is
1346 pragma Unreferenced (T);
1355 function Continue_Task (T : ST.Task_Id) return Boolean is
1356 pragma Unreferenced (T);
1365 procedure Initialize (Environment_Task : Task_Id) is
1366 act : aliased struct_sigaction;
1367 old_act : aliased struct_sigaction;
1368 Tmp_Set : aliased sigset_t;
1369 Result : Interfaces.C.int;
1370 -- Whether to use an alternate signal stack for stack overflows
1373 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1374 pragma Import (C, State, "__gnat_get_interrupt_state");
1375 -- Get interrupt state. Defined in a-init.c
1376 -- The input argument is the interrupt number,
1377 -- and the result is one of the following:
1379 Default : constant Character := 's';
1380 -- 'n' this interrupt not set by any Interrupt_State pragma
1381 -- 'u' Interrupt_State pragma set state to User
1382 -- 'r' Interrupt_State pragma set state to Runtime
1383 -- 's' Interrupt_State pragma set state to System (use "default"
1386 use type System.Multiprocessors.CPU_Range;
1389 Environment_Task_Id := Environment_Task;
1391 Interrupt_Management.Initialize;
1393 -- Prepare the set of signals that should be unblocked in all tasks
1395 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1396 pragma Assert (Result = 0);
1398 for J in Interrupt_Management.Interrupt_ID loop
1399 if System.Interrupt_Management.Keep_Unmasked (J) then
1400 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1401 pragma Assert (Result = 0);
1405 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1407 -- Initialize the global RTS lock
1409 Specific.Initialize (Environment_Task);
1411 if Use_Alternate_Stack then
1412 Environment_Task.Common.Task_Alternate_Stack :=
1413 Alternate_Stack'Address;
1416 -- Make environment task known here because it doesn't go through
1417 -- Activate_Tasks, which does it for all other tasks.
1419 Known_Tasks (Known_Tasks'First) := Environment_Task;
1420 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1422 Enter_Task (Environment_Task);
1425 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1428 act.sa_handler := Abort_Handler'Address;
1430 Result := sigemptyset (Tmp_Set'Access);
1431 pragma Assert (Result = 0);
1432 act.sa_mask := Tmp_Set;
1436 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1437 act'Unchecked_Access,
1438 old_act'Unchecked_Access);
1439 pragma Assert (Result = 0);
1440 Abort_Handler_Installed := True;
1443 -- pragma CPU and dispatching domains for the environment task
1445 Set_Task_Affinity (Environment_Task);
1448 -----------------------
1449 -- Set_Task_Affinity --
1450 -----------------------
1452 procedure Set_Task_Affinity (T : ST.Task_Id) is
1453 use type System.Multiprocessors.CPU_Range;
1456 -- Do nothing if there is no support for setting affinities or the
1457 -- underlying thread has not yet been created. If the thread has not
1458 -- yet been created then the proper affinity will be set during its
1461 if pthread_setaffinity_np'Address /= System.Null_Address
1462 and then T.Common.LL.Thread /= Null_Thread_Id
1465 CPUs : constant size_t :=
1467 (System.Multiprocessors.Number_Of_CPUs);
1468 CPU_Set : cpu_set_t_ptr := null;
1469 Size : constant size_t := CPU_ALLOC_SIZE (CPUs);
1471 Result : Interfaces.C.int;
1474 -- We look at the specific CPU (Base_CPU) first, then at the
1475 -- Task_Info field, and finally at the assigned dispatching
1478 if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1480 -- Set the affinity to an unique CPU
1482 CPU_Set := CPU_ALLOC (CPUs);
1483 System.OS_Interface.CPU_ZERO (Size, CPU_Set);
1484 System.OS_Interface.CPU_SET
1485 (int (T.Common.Base_CPU), Size, CPU_Set);
1489 elsif T.Common.Task_Info /= null then
1490 CPU_Set := T.Common.Task_Info.CPU_Affinity'Access;
1492 -- Handle dispatching domains
1494 elsif T.Common.Domain /= null and then
1495 (T.Common.Domain /= ST.System_Domain
1496 or else T.Common.Domain.all /=
1497 (Multiprocessors.CPU'First ..
1498 Multiprocessors.Number_Of_CPUs => True))
1500 -- Set the affinity to all the processors belonging to the
1501 -- dispatching domain. To avoid changing CPU affinities when
1502 -- not needed, we set the affinity only when assigning to a
1503 -- domain other than the default one, or when the default one
1504 -- has been modified.
1506 CPU_Set := CPU_ALLOC (CPUs);
1507 System.OS_Interface.CPU_ZERO (Size, CPU_Set);
1509 for Proc in T.Common.Domain'Range loop
1510 System.OS_Interface.CPU_SET (int (Proc), Size, CPU_Set);
1514 -- We set the new affinity if needed. Otherwise, the new task
1515 -- will inherit its creator's CPU affinity mask (according to
1516 -- the documentation of pthread_setaffinity_np), which is
1517 -- consistent with Ada's required semantics.
1519 if CPU_Set /= null then
1521 pthread_setaffinity_np (T.Common.LL.Thread, Size, CPU_Set);
1522 pragma Assert (Result = 0);
1528 end Set_Task_Affinity;
1530 end System.Task_Primitives.Operations;