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-2012, 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 Result
: Interfaces
.C
.int
;
297 Result
:= pthread_mutex_init
(L
.WO
'Access, null);
299 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
301 if Result
= ENOMEM
then
302 raise Storage_Error
with "Failed to allocate a lock";
308 procedure Initialize_Lock
309 (L
: not null access RTS_Lock
;
312 pragma Unreferenced
(Level
);
314 Result
: Interfaces
.C
.int
;
317 Result
:= pthread_mutex_init
(L
, null);
319 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
321 if Result
= ENOMEM
then
330 procedure Finalize_Lock
(L
: not null access Lock
) is
331 Result
: Interfaces
.C
.int
;
333 if Locking_Policy
= 'R' then
334 Result
:= pthread_rwlock_destroy
(L
.RW
'Access);
336 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
338 pragma Assert
(Result
= 0);
341 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
342 Result
: Interfaces
.C
.int
;
344 Result
:= pthread_mutex_destroy
(L
);
345 pragma Assert
(Result
= 0);
353 (L
: not null access Lock
;
354 Ceiling_Violation
: out Boolean)
356 Result
: Interfaces
.C
.int
;
358 if Locking_Policy
= 'R' then
359 Result
:= pthread_rwlock_wrlock
(L
.RW
'Access);
361 Result
:= pthread_mutex_lock
(L
.WO
'Access);
364 Ceiling_Violation
:= Result
= EINVAL
;
366 -- Assume the cause of EINVAL is a priority ceiling violation
368 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
372 (L
: not null access RTS_Lock
;
373 Global_Lock
: Boolean := False)
375 Result
: Interfaces
.C
.int
;
377 if not Single_Lock
or else Global_Lock
then
378 Result
:= pthread_mutex_lock
(L
);
379 pragma Assert
(Result
= 0);
383 procedure Write_Lock
(T
: Task_Id
) is
384 Result
: Interfaces
.C
.int
;
386 if not Single_Lock
then
387 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
388 pragma Assert
(Result
= 0);
397 (L
: not null access Lock
;
398 Ceiling_Violation
: out Boolean)
400 Result
: Interfaces
.C
.int
;
402 if Locking_Policy
= 'R' then
403 Result
:= pthread_rwlock_rdlock
(L
.RW
'Access);
405 Result
:= pthread_mutex_lock
(L
.WO
'Access);
408 Ceiling_Violation
:= Result
= EINVAL
;
410 -- Assume the cause of EINVAL is a priority ceiling violation
412 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
419 procedure Unlock
(L
: not null access Lock
) is
420 Result
: Interfaces
.C
.int
;
422 if Locking_Policy
= 'R' then
423 Result
:= pthread_rwlock_unlock
(L
.RW
'Access);
425 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
427 pragma Assert
(Result
= 0);
431 (L
: not null access RTS_Lock
;
432 Global_Lock
: Boolean := False)
434 Result
: Interfaces
.C
.int
;
436 if not Single_Lock
or else Global_Lock
then
437 Result
:= pthread_mutex_unlock
(L
);
438 pragma Assert
(Result
= 0);
442 procedure Unlock
(T
: Task_Id
) is
443 Result
: Interfaces
.C
.int
;
445 if not Single_Lock
then
446 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
447 pragma Assert
(Result
= 0);
455 -- Dynamic priority ceilings are not supported by the underlying system
457 procedure Set_Ceiling
458 (L
: not null access Lock
;
459 Prio
: System
.Any_Priority
)
461 pragma Unreferenced
(L
, Prio
);
472 Reason
: System
.Tasking
.Task_States
)
474 pragma Unreferenced
(Reason
);
476 Result
: Interfaces
.C
.int
;
479 pragma Assert
(Self_ID
= Self
);
483 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
484 mutex
=> (if Single_Lock
485 then Single_RTS_Lock
'Access
486 else Self_ID
.Common
.LL
.L
'Access));
488 -- EINTR is not considered a failure
490 pragma Assert
(Result
= 0 or else Result
= EINTR
);
497 -- This is for use within the run-time system, so abort is
498 -- assumed to be already deferred, and the caller should be
499 -- holding its own ATCB lock.
501 procedure Timed_Sleep
504 Mode
: ST
.Delay_Modes
;
505 Reason
: System
.Tasking
.Task_States
;
506 Timedout
: out Boolean;
507 Yielded
: out Boolean)
509 pragma Unreferenced
(Reason
);
511 Base_Time
: constant Duration := Monotonic_Clock
;
512 Check_Time
: Duration := Base_Time
;
514 Request
: aliased timespec
;
515 Result
: Interfaces
.C
.int
;
523 then Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
524 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
526 if Abs_Time
> Check_Time
then
527 Request
:= To_Timespec
(Abs_Time
);
530 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
533 pthread_cond_timedwait
534 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
535 mutex
=> (if Single_Lock
536 then Single_RTS_Lock
'Access
537 else Self_ID
.Common
.LL
.L
'Access),
538 abstime
=> Request
'Access);
540 Check_Time
:= Monotonic_Clock
;
541 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
543 if Result
= 0 or else Result
= EINTR
then
545 -- Somebody may have called Wakeup for us
551 pragma Assert
(Result
= ETIMEDOUT
);
560 -- This is for use in implementing delay statements, so we assume the
561 -- caller is abort-deferred but is holding no locks.
563 procedure Timed_Delay
566 Mode
: ST
.Delay_Modes
)
568 Base_Time
: constant Duration := Monotonic_Clock
;
569 Check_Time
: Duration := Base_Time
;
571 Request
: aliased timespec
;
573 Result
: Interfaces
.C
.int
;
574 pragma Warnings
(Off
, Result
);
581 Write_Lock
(Self_ID
);
585 then Time
+ Check_Time
586 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
588 if Abs_Time
> Check_Time
then
589 Request
:= To_Timespec
(Abs_Time
);
590 Self_ID
.Common
.State
:= Delay_Sleep
;
593 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
596 pthread_cond_timedwait
597 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
598 mutex
=> (if Single_Lock
599 then Single_RTS_Lock
'Access
600 else Self_ID
.Common
.LL
.L
'Access),
601 abstime
=> Request
'Access);
603 Check_Time
:= Monotonic_Clock
;
604 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
606 pragma Assert
(Result
= 0 or else
607 Result
= ETIMEDOUT
or else
611 Self_ID
.Common
.State
:= Runnable
;
620 Result
:= sched_yield
;
623 ---------------------
624 -- Monotonic_Clock --
625 ---------------------
627 function Monotonic_Clock
return Duration is
630 type timeval
is array (1 .. 2) of C
.long
;
632 procedure timeval_to_duration
633 (T
: not null access timeval
;
634 sec
: not null access C
.long
;
635 usec
: not null access C
.long
);
636 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
638 Micro
: constant := 10**6;
639 sec
: aliased C
.long
;
640 usec
: aliased C
.long
;
641 TV
: aliased timeval
;
644 function gettimeofday
645 (Tv
: access timeval
;
646 Tz
: System
.Address
:= System
.Null_Address
) return int
;
647 pragma Import
(C
, gettimeofday
, "gettimeofday");
650 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
651 pragma Assert
(Result
= 0);
652 timeval_to_duration
(TV
'Access, sec
'Access, usec
'Access);
653 return Duration (sec
) + Duration (usec
) / Micro
;
660 function RT_Resolution
return Duration is
669 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
670 pragma Unreferenced
(Reason
);
671 Result
: Interfaces
.C
.int
;
673 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
674 pragma Assert
(Result
= 0);
681 procedure Yield
(Do_Yield
: Boolean := True) is
682 Result
: Interfaces
.C
.int
;
683 pragma Unreferenced
(Result
);
686 Result
:= sched_yield
;
694 procedure Set_Priority
696 Prio
: System
.Any_Priority
;
697 Loss_Of_Inheritance
: Boolean := False)
699 pragma Unreferenced
(Loss_Of_Inheritance
);
701 Result
: Interfaces
.C
.int
;
702 Param
: aliased struct_sched_param
;
704 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
705 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
706 -- Get priority specific dispatching policy
708 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
709 -- Upper case first character of the policy name corresponding to the
710 -- task as set by a Priority_Specific_Dispatching pragma.
713 T
.Common
.Current_Priority
:= Prio
;
715 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
717 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
) + 1;
719 if Dispatching_Policy
= 'R'
720 or else Priority_Specific_Policy
= 'R'
721 or else Time_Slice_Val
> 0
724 pthread_setschedparam
725 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
727 elsif Dispatching_Policy
= 'F'
728 or else Priority_Specific_Policy
= 'F'
729 or else Time_Slice_Val
= 0
732 pthread_setschedparam
733 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
736 Param
.sched_priority
:= 0;
738 pthread_setschedparam
740 SCHED_OTHER
, Param
'Access);
743 pragma Assert
(Result
= 0 or else Result
= EPERM
);
750 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
752 return T
.Common
.Current_Priority
;
759 procedure Enter_Task
(Self_ID
: Task_Id
) is
761 if Self_ID
.Common
.Task_Info
/= null
762 and then Self_ID
.Common
.Task_Info
.CPU_Affinity
= No_CPU
764 raise Invalid_CPU_Number
;
767 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
768 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
770 if Self_ID
.Common
.Task_Image_Len
> 0 then
772 Task_Name
: String (1 .. Parameters
.Max_Task_Image_Length
+ 1);
776 -- Set thread name to ease debugging
778 Task_Name
(1 .. Self_ID
.Common
.Task_Image_Len
) :=
779 Self_ID
.Common
.Task_Image
(1 .. Self_ID
.Common
.Task_Image_Len
);
780 Task_Name
(Self_ID
.Common
.Task_Image_Len
+ 1) := ASCII
.NUL
;
782 Result
:= prctl
(PR_SET_NAME
, unsigned_long
(Task_Name
'Address));
783 pragma Assert
(Result
= 0);
787 Specific
.Set
(Self_ID
);
789 if Use_Alternate_Stack
790 and then Self_ID
.Common
.Task_Alternate_Stack
/= Null_Address
793 Stack
: aliased stack_t
;
794 Result
: Interfaces
.C
.int
;
796 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
797 Stack
.ss_size
:= Alternate_Stack_Size
;
799 Result
:= sigaltstack
(Stack
'Access, null);
800 pragma Assert
(Result
= 0);
809 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
811 -----------------------------
812 -- Register_Foreign_Thread --
813 -----------------------------
815 function Register_Foreign_Thread
return Task_Id
is
817 if Is_Valid_Task
then
820 return Register_Foreign_Thread
(pthread_self
);
822 end Register_Foreign_Thread
;
828 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
829 Cond_Attr
: aliased pthread_condattr_t
;
830 Result
: Interfaces
.C
.int
;
833 -- Give the task a unique serial number
835 Self_ID
.Serial_Number
:= Next_Serial_Number
;
836 Next_Serial_Number
:= Next_Serial_Number
+ 1;
837 pragma Assert
(Next_Serial_Number
/= 0);
839 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
841 if not Single_Lock
then
843 pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access, null);
844 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
852 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
853 pragma Assert
(Result
= 0);
856 pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
857 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
862 if not Single_Lock
then
863 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
864 pragma Assert
(Result
= 0);
875 procedure Create_Task
877 Wrapper
: System
.Address
;
878 Stack_Size
: System
.Parameters
.Size_Type
;
879 Priority
: System
.Any_Priority
;
880 Succeeded
: out Boolean)
882 Attributes
: aliased pthread_attr_t
;
883 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
884 Result
: Interfaces
.C
.int
;
886 use type System
.Multiprocessors
.CPU_Range
;
889 -- Check whether both Dispatching_Domain and CPU are specified for
890 -- the task, and the CPU value is not contained within the range of
891 -- processors for the domain.
893 if T
.Common
.Domain
/= null
894 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
896 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
897 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
903 Adjusted_Stack_Size
:=
904 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
906 Result
:= pthread_attr_init
(Attributes
'Access);
907 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
915 pthread_attr_setstacksize
(Attributes
'Access, Adjusted_Stack_Size
);
916 pragma Assert
(Result
= 0);
919 pthread_attr_setdetachstate
920 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
921 pragma Assert
(Result
= 0);
923 -- Set the required attributes for the creation of the thread
925 -- Note: Previously, we called pthread_setaffinity_np (after thread
926 -- creation but before thread activation) to set the affinity but it was
927 -- not behaving as expected. Setting the required attributes for the
928 -- creation of the thread works correctly and it is more appropriate.
930 -- Do nothing if required support not provided by the operating system
932 if pthread_attr_setaffinity_np
'Address = System
.Null_Address
then
935 -- Support is available
937 elsif T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
then
939 CPUs
: constant size_t
:=
941 (System
.Multiprocessors
.Number_Of_CPUs
);
942 CPU_Set
: constant cpu_set_t_ptr
:= CPU_ALLOC
(CPUs
);
943 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
946 CPU_ZERO
(Size
, CPU_Set
);
947 System
.OS_Interface
.CPU_SET
948 (int
(T
.Common
.Base_CPU
), Size
, CPU_Set
);
950 pthread_attr_setaffinity_np
(Attributes
'Access, Size
, CPU_Set
);
951 pragma Assert
(Result
= 0);
958 elsif T
.Common
.Task_Info
/= null then
960 pthread_attr_setaffinity_np
963 T
.Common
.Task_Info
.CPU_Affinity
'Access);
964 pragma Assert
(Result
= 0);
966 -- Handle dispatching domains
968 -- To avoid changing CPU affinities when not needed, we set the
969 -- affinity only when assigning to a domain other than the default
970 -- one, or when the default one has been modified.
972 elsif T
.Common
.Domain
/= null and then
973 (T
.Common
.Domain
/= ST
.System_Domain
974 or else T
.Common
.Domain
.all /=
975 (Multiprocessors
.CPU
'First ..
976 Multiprocessors
.Number_Of_CPUs
=> True))
979 CPUs
: constant size_t
:=
981 (System
.Multiprocessors
.Number_Of_CPUs
);
982 CPU_Set
: constant cpu_set_t_ptr
:= CPU_ALLOC
(CPUs
);
983 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
986 CPU_ZERO
(Size
, CPU_Set
);
988 -- Set the affinity to all the processors belonging to the
989 -- dispatching domain.
991 for Proc
in T
.Common
.Domain
'Range loop
992 if T
.Common
.Domain
(Proc
) then
993 System
.OS_Interface
.CPU_SET
(int
(Proc
), Size
, CPU_Set
);
998 pthread_attr_setaffinity_np
(Attributes
'Access, Size
, CPU_Set
);
999 pragma Assert
(Result
= 0);
1005 -- Since the initial signal mask of a thread is inherited from the
1006 -- creator, and the Environment task has all its signals masked, we
1007 -- do not need to manipulate caller's signal mask at this point.
1008 -- All tasks in RTS will have All_Tasks_Mask initially.
1010 -- Note: the use of Unrestricted_Access in the following call is needed
1011 -- because otherwise we have an error of getting a access-to-volatile
1012 -- value which points to a non-volatile object. But in this case it is
1013 -- safe to do this, since we know we have no problems with aliasing and
1014 -- Unrestricted_Access bypasses this check.
1018 (T
.Common
.LL
.Thread
'Unrestricted_Access,
1020 Thread_Body_Access
(Wrapper
),
1024 (Result
= 0 or else Result
= EAGAIN
or else Result
= ENOMEM
);
1028 Result
:= pthread_attr_destroy
(Attributes
'Access);
1029 pragma Assert
(Result
= 0);
1035 Result
:= pthread_attr_destroy
(Attributes
'Access);
1036 pragma Assert
(Result
= 0);
1038 Set_Priority
(T
, Priority
);
1045 procedure Finalize_TCB
(T
: Task_Id
) is
1046 Result
: Interfaces
.C
.int
;
1049 if not Single_Lock
then
1050 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
1051 pragma Assert
(Result
= 0);
1054 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
1055 pragma Assert
(Result
= 0);
1057 if T
.Known_Tasks_Index
/= -1 then
1058 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1061 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
1063 ATCB_Allocation
.Free_ATCB
(T
);
1070 procedure Exit_Task
is
1072 Specific
.Set
(null);
1079 procedure Abort_Task
(T
: Task_Id
) is
1080 Result
: Interfaces
.C
.int
;
1082 if Abort_Handler_Installed
then
1085 (T
.Common
.LL
.Thread
,
1086 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1087 pragma Assert
(Result
= 0);
1095 procedure Initialize
(S
: in out Suspension_Object
) is
1096 Result
: Interfaces
.C
.int
;
1099 -- Initialize internal state (always to False (RM D.10(6)))
1104 -- Initialize internal mutex
1106 Result
:= pthread_mutex_init
(S
.L
'Access, null);
1108 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1110 if Result
= ENOMEM
then
1111 raise Storage_Error
;
1114 -- Initialize internal condition variable
1116 Result
:= pthread_cond_init
(S
.CV
'Access, null);
1118 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1121 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1122 pragma Assert
(Result
= 0);
1124 if Result
= ENOMEM
then
1125 raise Storage_Error
;
1134 procedure Finalize
(S
: in out Suspension_Object
) is
1135 Result
: Interfaces
.C
.int
;
1138 -- Destroy internal mutex
1140 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1141 pragma Assert
(Result
= 0);
1143 -- Destroy internal condition variable
1145 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1146 pragma Assert
(Result
= 0);
1153 function Current_State
(S
: Suspension_Object
) return Boolean is
1155 -- We do not want to use lock on this read operation. State is marked
1156 -- as Atomic so that we ensure that the value retrieved is correct.
1165 procedure Set_False
(S
: in out Suspension_Object
) is
1166 Result
: Interfaces
.C
.int
;
1169 SSL
.Abort_Defer
.all;
1171 Result
:= pthread_mutex_lock
(S
.L
'Access);
1172 pragma Assert
(Result
= 0);
1176 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1177 pragma Assert
(Result
= 0);
1179 SSL
.Abort_Undefer
.all;
1186 procedure Set_True
(S
: in out Suspension_Object
) is
1187 Result
: Interfaces
.C
.int
;
1190 SSL
.Abort_Defer
.all;
1192 Result
:= pthread_mutex_lock
(S
.L
'Access);
1193 pragma Assert
(Result
= 0);
1195 -- If there is already a task waiting on this suspension object then
1196 -- we resume it, leaving the state of the suspension object to False,
1197 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1198 -- the state to True.
1204 Result
:= pthread_cond_signal
(S
.CV
'Access);
1205 pragma Assert
(Result
= 0);
1211 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1212 pragma Assert
(Result
= 0);
1214 SSL
.Abort_Undefer
.all;
1217 ------------------------
1218 -- Suspend_Until_True --
1219 ------------------------
1221 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1222 Result
: Interfaces
.C
.int
;
1225 SSL
.Abort_Defer
.all;
1227 Result
:= pthread_mutex_lock
(S
.L
'Access);
1228 pragma Assert
(Result
= 0);
1232 -- Program_Error must be raised upon calling Suspend_Until_True
1233 -- if another task is already waiting on that suspension object
1236 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1237 pragma Assert
(Result
= 0);
1239 SSL
.Abort_Undefer
.all;
1241 raise Program_Error
;
1244 -- Suspend the task if the state is False. Otherwise, the task
1245 -- continues its execution, and the state of the suspension object
1246 -- is set to False (ARM D.10 par. 9).
1254 -- Loop in case pthread_cond_wait returns earlier than expected
1255 -- (e.g. in case of EINTR caused by a signal). This should not
1256 -- happen with the current Linux implementation of pthread, but
1257 -- POSIX does not guarantee it so this may change in future.
1259 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1260 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1262 exit when not S
.Waiting
;
1266 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1267 pragma Assert
(Result
= 0);
1269 SSL
.Abort_Undefer
.all;
1271 end Suspend_Until_True
;
1279 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1280 pragma Unreferenced
(Self_ID
);
1285 --------------------
1286 -- Check_No_Locks --
1287 --------------------
1289 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1290 pragma Unreferenced
(Self_ID
);
1295 ----------------------
1296 -- Environment_Task --
1297 ----------------------
1299 function Environment_Task
return Task_Id
is
1301 return Environment_Task_Id
;
1302 end Environment_Task
;
1308 function Suspend_Task
1310 Thread_Self
: Thread_Id
) return Boolean
1313 if T
.Common
.LL
.Thread
/= Thread_Self
then
1314 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1324 function Resume_Task
1326 Thread_Self
: Thread_Id
) return Boolean
1329 if T
.Common
.LL
.Thread
/= Thread_Self
then
1330 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1336 --------------------
1337 -- Stop_All_Tasks --
1338 --------------------
1340 procedure Stop_All_Tasks
is
1349 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1350 pragma Unreferenced
(T
);
1359 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1360 pragma Unreferenced
(T
);
1369 procedure Initialize
(Environment_Task
: Task_Id
) is
1370 act
: aliased struct_sigaction
;
1371 old_act
: aliased struct_sigaction
;
1372 Tmp_Set
: aliased sigset_t
;
1373 Result
: Interfaces
.C
.int
;
1374 -- Whether to use an alternate signal stack for stack overflows
1377 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1378 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1379 -- Get interrupt state. Defined in a-init.c
1380 -- The input argument is the interrupt number,
1381 -- and the result is one of the following:
1383 Default
: constant Character := 's';
1384 -- 'n' this interrupt not set by any Interrupt_State pragma
1385 -- 'u' Interrupt_State pragma set state to User
1386 -- 'r' Interrupt_State pragma set state to Runtime
1387 -- 's' Interrupt_State pragma set state to System (use "default"
1390 use type System
.Multiprocessors
.CPU_Range
;
1393 Environment_Task_Id
:= Environment_Task
;
1395 Interrupt_Management
.Initialize
;
1397 -- Prepare the set of signals that should be unblocked in all tasks
1399 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1400 pragma Assert
(Result
= 0);
1402 for J
in Interrupt_Management
.Interrupt_ID
loop
1403 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1404 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1405 pragma Assert
(Result
= 0);
1409 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1411 -- Initialize the global RTS lock
1413 Specific
.Initialize
(Environment_Task
);
1415 if Use_Alternate_Stack
then
1416 Environment_Task
.Common
.Task_Alternate_Stack
:=
1417 Alternate_Stack
'Address;
1420 -- Make environment task known here because it doesn't go through
1421 -- Activate_Tasks, which does it for all other tasks.
1423 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1424 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1426 Enter_Task
(Environment_Task
);
1429 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1432 act
.sa_handler
:= Abort_Handler
'Address;
1434 Result
:= sigemptyset
(Tmp_Set
'Access);
1435 pragma Assert
(Result
= 0);
1436 act
.sa_mask
:= Tmp_Set
;
1440 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1441 act
'Unchecked_Access,
1442 old_act
'Unchecked_Access);
1443 pragma Assert
(Result
= 0);
1444 Abort_Handler_Installed
:= True;
1447 -- pragma CPU and dispatching domains for the environment task
1449 Set_Task_Affinity
(Environment_Task
);
1452 -----------------------
1453 -- Set_Task_Affinity --
1454 -----------------------
1456 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1457 use type System
.Multiprocessors
.CPU_Range
;
1460 -- Do nothing if there is no support for setting affinities or the
1461 -- underlying thread has not yet been created. If the thread has not
1462 -- yet been created then the proper affinity will be set during its
1465 if pthread_setaffinity_np
'Address /= System
.Null_Address
1466 and then T
.Common
.LL
.Thread
/= Null_Thread_Id
1469 CPUs
: constant size_t
:=
1471 (System
.Multiprocessors
.Number_Of_CPUs
);
1472 CPU_Set
: cpu_set_t_ptr
:= null;
1473 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
1475 Result
: Interfaces
.C
.int
;
1478 -- We look at the specific CPU (Base_CPU) first, then at the
1479 -- Task_Info field, and finally at the assigned dispatching
1482 if T
.Common
.Base_CPU
/= Multiprocessors
.Not_A_Specific_CPU
then
1484 -- Set the affinity to an unique CPU
1486 CPU_Set
:= CPU_ALLOC
(CPUs
);
1487 System
.OS_Interface
.CPU_ZERO
(Size
, CPU_Set
);
1488 System
.OS_Interface
.CPU_SET
1489 (int
(T
.Common
.Base_CPU
), Size
, CPU_Set
);
1493 elsif T
.Common
.Task_Info
/= null then
1494 CPU_Set
:= T
.Common
.Task_Info
.CPU_Affinity
'Access;
1496 -- Handle dispatching domains
1498 elsif T
.Common
.Domain
/= null and then
1499 (T
.Common
.Domain
/= ST
.System_Domain
1500 or else T
.Common
.Domain
.all /=
1501 (Multiprocessors
.CPU
'First ..
1502 Multiprocessors
.Number_Of_CPUs
=> True))
1504 -- Set the affinity to all the processors belonging to the
1505 -- dispatching domain. To avoid changing CPU affinities when
1506 -- not needed, we set the affinity only when assigning to a
1507 -- domain other than the default one, or when the default one
1508 -- has been modified.
1510 CPU_Set
:= CPU_ALLOC
(CPUs
);
1511 System
.OS_Interface
.CPU_ZERO
(Size
, CPU_Set
);
1513 for Proc
in T
.Common
.Domain
'Range loop
1514 System
.OS_Interface
.CPU_SET
(int
(Proc
), Size
, CPU_Set
);
1518 -- We set the new affinity if needed. Otherwise, the new task
1519 -- will inherit its creator's CPU affinity mask (according to
1520 -- the documentation of pthread_setaffinity_np), which is
1521 -- consistent with Ada's required semantics.
1523 if CPU_Set
/= null then
1525 pthread_setaffinity_np
(T
.Common
.LL
.Thread
, Size
, CPU_Set
);
1526 pragma Assert
(Result
= 0);
1532 end Set_Task_Affinity
;
1534 end System
.Task_Primitives
.Operations
;