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-2014, 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.
42 with Interfaces
.C
.Extensions
;
44 with System
.Task_Info
;
45 with System
.Tasking
.Debug
;
46 with System
.Interrupt_Management
;
47 with System
.OS_Constants
;
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 OSC
renames System
.OS_Constants
;
61 package SSL
renames System
.Soft_Links
;
62 package SC
renames System
.Stack_Checking
.Operations
;
64 use System
.Tasking
.Debug
;
67 use Interfaces
.C
.Extensions
;
68 use System
.OS_Interface
;
69 use System
.Parameters
;
70 use System
.OS_Primitives
;
77 -- The followings are logically constants, but need to be initialized
80 Single_RTS_Lock
: aliased RTS_Lock
;
81 -- This is a lock to allow only one thread of control in the RTS at
82 -- a time; it is used to execute in mutual exclusion from all other tasks.
83 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
85 Environment_Task_Id
: Task_Id
;
86 -- A variable to hold Task_Id for the environment task
88 Unblocked_Signal_Mask
: aliased sigset_t
;
89 -- The set of signals that should be unblocked in all tasks
91 -- The followings are internal configuration constants needed
93 Next_Serial_Number
: Task_Serial_Number
:= 100;
94 -- We start at 100 (reserve some special values for using in error checks)
96 Time_Slice_Val
: Integer;
97 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
99 Dispatching_Policy
: Character;
100 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
102 Locking_Policy
: Character;
103 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
105 Foreign_Task_Elaborated
: aliased Boolean := True;
106 -- Used to identified fake tasks (i.e., non-Ada Threads)
108 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
109 -- Whether to use an alternate signal stack for stack overflows
111 Abort_Handler_Installed
: Boolean := False;
112 -- True if a handler for the abort signal is installed
114 Null_Thread_Id
: constant pthread_t
:= pthread_t
'Last;
115 -- Constant to indicate that the thread identifier has not yet been
124 procedure Initialize
(Environment_Task
: Task_Id
);
125 pragma Inline
(Initialize
);
126 -- Initialize various data needed by this package
128 function Is_Valid_Task
return Boolean;
129 pragma Inline
(Is_Valid_Task
);
130 -- Does executing thread have a TCB?
132 procedure Set
(Self_Id
: Task_Id
);
134 -- Set the self id for the current task
136 function Self
return Task_Id
;
137 pragma Inline
(Self
);
138 -- Return a pointer to the Ada Task Control Block of the calling task
142 package body Specific
is separate;
143 -- The body of this package is target specific
145 ----------------------------------
146 -- ATCB allocation/deallocation --
147 ----------------------------------
149 package body ATCB_Allocation
is separate;
150 -- The body of this package is shared across several targets
152 ---------------------------------
153 -- Support for foreign threads --
154 ---------------------------------
156 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
157 -- Allocate and Initialize a new ATCB for the current Thread
159 function Register_Foreign_Thread
160 (Thread
: Thread_Id
) return Task_Id
is separate;
162 -----------------------
163 -- Local Subprograms --
164 -----------------------
166 procedure Abort_Handler
(signo
: Signal
);
172 procedure Abort_Handler
(signo
: Signal
) is
173 pragma Unreferenced
(signo
);
175 Self_Id
: constant Task_Id
:= Self
;
176 Result
: Interfaces
.C
.int
;
177 Old_Set
: aliased sigset_t
;
180 -- It's not safe to raise an exception when using GCC ZCX mechanism.
181 -- Note that we still need to install a signal handler, since in some
182 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
183 -- need to send the Abort signal to a task.
185 if ZCX_By_Default
then
189 if Self_Id
.Deferral_Level
= 0
190 and then Self_Id
.Pending_ATC_Level
< Self_Id
.ATC_Nesting_Level
191 and then not Self_Id
.Aborting
193 Self_Id
.Aborting
:= True;
195 -- Make sure signals used for RTS internal purpose are unmasked
200 Unblocked_Signal_Mask
'Access,
202 pragma Assert
(Result
= 0);
204 raise Standard
'Abort_Signal;
212 procedure Lock_RTS
is
214 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
221 procedure Unlock_RTS
is
223 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
230 -- The underlying thread system extends the memory (up to 2MB) when needed
232 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
233 pragma Unreferenced
(T
);
234 pragma Unreferenced
(On
);
243 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
245 return T
.Common
.LL
.Thread
;
252 function Self
return Task_Id
renames Specific
.Self
;
254 ---------------------
255 -- Initialize_Lock --
256 ---------------------
258 -- Note: mutexes and cond_variables needed per-task basis are initialized
259 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
260 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
261 -- status change of RTS. Therefore raising Storage_Error in the following
262 -- routines should be able to be handled safely.
264 procedure Initialize_Lock
265 (Prio
: System
.Any_Priority
;
266 L
: not null access Lock
)
268 pragma Unreferenced
(Prio
);
271 if Locking_Policy
= 'R' then
273 RWlock_Attr
: aliased pthread_rwlockattr_t
;
274 Result
: Interfaces
.C
.int
;
277 -- Set the rwlock to prefer writer to avoid writers starvation
279 Result
:= pthread_rwlockattr_init
(RWlock_Attr
'Access);
280 pragma Assert
(Result
= 0);
282 Result
:= pthread_rwlockattr_setkind_np
284 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
);
285 pragma Assert
(Result
= 0);
287 Result
:= pthread_rwlock_init
(L
.RW
'Access, RWlock_Attr
'Access);
289 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
291 if Result
= ENOMEM
then
292 raise Storage_Error
with "Failed to allocate a lock";
298 Result
: Interfaces
.C
.int
;
301 Result
:= pthread_mutex_init
(L
.WO
'Access, null);
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 Result
: Interfaces
.C
.int
;
321 Result
:= pthread_mutex_init
(L
, null);
323 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
325 if Result
= ENOMEM
then
334 procedure Finalize_Lock
(L
: not null access Lock
) is
335 Result
: Interfaces
.C
.int
;
337 if Locking_Policy
= 'R' then
338 Result
:= pthread_rwlock_destroy
(L
.RW
'Access);
340 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
342 pragma Assert
(Result
= 0);
345 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
346 Result
: Interfaces
.C
.int
;
348 Result
:= pthread_mutex_destroy
(L
);
349 pragma Assert
(Result
= 0);
357 (L
: not null access Lock
;
358 Ceiling_Violation
: out Boolean)
360 Result
: Interfaces
.C
.int
;
362 if Locking_Policy
= 'R' then
363 Result
:= pthread_rwlock_wrlock
(L
.RW
'Access);
365 Result
:= pthread_mutex_lock
(L
.WO
'Access);
368 Ceiling_Violation
:= Result
= EINVAL
;
370 -- Assume the cause of EINVAL is a priority ceiling violation
372 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
376 (L
: not null access RTS_Lock
;
377 Global_Lock
: Boolean := False)
379 Result
: Interfaces
.C
.int
;
381 if not Single_Lock
or else Global_Lock
then
382 Result
:= pthread_mutex_lock
(L
);
383 pragma Assert
(Result
= 0);
387 procedure Write_Lock
(T
: Task_Id
) is
388 Result
: Interfaces
.C
.int
;
390 if not Single_Lock
then
391 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
392 pragma Assert
(Result
= 0);
401 (L
: not null access Lock
;
402 Ceiling_Violation
: out Boolean)
404 Result
: Interfaces
.C
.int
;
406 if Locking_Policy
= 'R' then
407 Result
:= pthread_rwlock_rdlock
(L
.RW
'Access);
409 Result
:= pthread_mutex_lock
(L
.WO
'Access);
412 Ceiling_Violation
:= Result
= EINVAL
;
414 -- Assume the cause of EINVAL is a priority ceiling violation
416 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
423 procedure Unlock
(L
: not null access Lock
) is
424 Result
: Interfaces
.C
.int
;
426 if Locking_Policy
= 'R' then
427 Result
:= pthread_rwlock_unlock
(L
.RW
'Access);
429 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
431 pragma Assert
(Result
= 0);
435 (L
: not null access RTS_Lock
;
436 Global_Lock
: Boolean := False)
438 Result
: Interfaces
.C
.int
;
440 if not Single_Lock
or else Global_Lock
then
441 Result
:= pthread_mutex_unlock
(L
);
442 pragma Assert
(Result
= 0);
446 procedure Unlock
(T
: Task_Id
) is
447 Result
: Interfaces
.C
.int
;
449 if not Single_Lock
then
450 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
451 pragma Assert
(Result
= 0);
459 -- Dynamic priority ceilings are not supported by the underlying system
461 procedure Set_Ceiling
462 (L
: not null access Lock
;
463 Prio
: System
.Any_Priority
)
465 pragma Unreferenced
(L
, Prio
);
476 Reason
: System
.Tasking
.Task_States
)
478 pragma Unreferenced
(Reason
);
480 Result
: Interfaces
.C
.int
;
483 pragma Assert
(Self_ID
= Self
);
487 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
488 mutex
=> (if Single_Lock
489 then Single_RTS_Lock
'Access
490 else Self_ID
.Common
.LL
.L
'Access));
492 -- EINTR is not considered a failure
494 pragma Assert
(Result
= 0 or else Result
= EINTR
);
501 -- This is for use within the run-time system, so abort is
502 -- assumed to be already deferred, and the caller should be
503 -- holding its own ATCB lock.
505 procedure Timed_Sleep
508 Mode
: ST
.Delay_Modes
;
509 Reason
: System
.Tasking
.Task_States
;
510 Timedout
: out Boolean;
511 Yielded
: out Boolean)
513 pragma Unreferenced
(Reason
);
515 Base_Time
: constant Duration := Monotonic_Clock
;
516 Check_Time
: Duration := Base_Time
;
518 Request
: aliased timespec
;
519 Result
: Interfaces
.C
.int
;
527 then Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
528 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
530 if Abs_Time
> Check_Time
then
531 Request
:= To_Timespec
(Abs_Time
);
534 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
537 pthread_cond_timedwait
538 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
539 mutex
=> (if Single_Lock
540 then Single_RTS_Lock
'Access
541 else Self_ID
.Common
.LL
.L
'Access),
542 abstime
=> Request
'Access);
544 Check_Time
:= Monotonic_Clock
;
545 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
547 if Result
= 0 or else Result
= EINTR
then
549 -- Somebody may have called Wakeup for us
555 pragma Assert
(Result
= ETIMEDOUT
);
564 -- This is for use in implementing delay statements, so we assume the
565 -- caller is abort-deferred but is holding no locks.
567 procedure Timed_Delay
570 Mode
: ST
.Delay_Modes
)
572 Base_Time
: constant Duration := Monotonic_Clock
;
573 Check_Time
: Duration := Base_Time
;
575 Request
: aliased timespec
;
577 Result
: Interfaces
.C
.int
;
578 pragma Warnings
(Off
, Result
);
585 Write_Lock
(Self_ID
);
589 then Time
+ Check_Time
590 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
592 if Abs_Time
> Check_Time
then
593 Request
:= To_Timespec
(Abs_Time
);
594 Self_ID
.Common
.State
:= Delay_Sleep
;
597 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
600 pthread_cond_timedwait
601 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
602 mutex
=> (if Single_Lock
603 then Single_RTS_Lock
'Access
604 else Self_ID
.Common
.LL
.L
'Access),
605 abstime
=> Request
'Access);
607 Check_Time
:= Monotonic_Clock
;
608 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
610 pragma Assert
(Result
= 0 or else
611 Result
= ETIMEDOUT
or else
615 Self_ID
.Common
.State
:= Runnable
;
624 Result
:= sched_yield
;
627 ---------------------
628 -- Monotonic_Clock --
629 ---------------------
631 function Monotonic_Clock
return Duration is
634 procedure timeval_to_duration
635 (T
: not null access timeval
;
636 sec
: not null access C
.Extensions
.long_long
;
637 usec
: not null access C
.long
);
638 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
640 Micro
: constant := 10**6;
641 sec
: aliased C
.Extensions
.long_long
;
642 usec
: aliased C
.long
;
643 TV
: aliased timeval
;
646 function gettimeofday
647 (Tv
: access timeval
;
648 Tz
: System
.Address
:= System
.Null_Address
) return int
;
649 pragma Import
(C
, gettimeofday
, "gettimeofday");
652 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
653 pragma Assert
(Result
= 0);
654 timeval_to_duration
(TV
'Access, sec
'Access, usec
'Access);
655 return Duration (sec
) + Duration (usec
) / Micro
;
662 function RT_Resolution
return Duration is
663 TS
: aliased timespec
;
667 Result
:= clock_getres
(OSC
.CLOCK_REALTIME
, TS
'Unchecked_Access);
668 pragma Assert
(Result
= 0);
670 return To_Duration
(TS
);
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 if Self_ID
.Common
.Task_Image_Len
> 0 then
780 Task_Name
: String (1 .. Parameters
.Max_Task_Image_Length
+ 1);
784 -- Set thread name to ease debugging
786 Task_Name
(1 .. Self_ID
.Common
.Task_Image_Len
) :=
787 Self_ID
.Common
.Task_Image
(1 .. Self_ID
.Common
.Task_Image_Len
);
788 Task_Name
(Self_ID
.Common
.Task_Image_Len
+ 1) := ASCII
.NUL
;
790 Result
:= prctl
(PR_SET_NAME
, unsigned_long
(Task_Name
'Address));
791 pragma Assert
(Result
= 0);
795 Specific
.Set
(Self_ID
);
797 if Use_Alternate_Stack
798 and then Self_ID
.Common
.Task_Alternate_Stack
/= Null_Address
801 Stack
: aliased stack_t
;
802 Result
: Interfaces
.C
.int
;
804 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
805 Stack
.ss_size
:= Alternate_Stack_Size
;
807 Result
:= sigaltstack
(Stack
'Access, null);
808 pragma Assert
(Result
= 0);
817 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
819 -----------------------------
820 -- Register_Foreign_Thread --
821 -----------------------------
823 function Register_Foreign_Thread
return Task_Id
is
825 if Is_Valid_Task
then
828 return Register_Foreign_Thread
(pthread_self
);
830 end Register_Foreign_Thread
;
836 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
837 Cond_Attr
: aliased pthread_condattr_t
;
838 Result
: Interfaces
.C
.int
;
841 -- Give the task a unique serial number
843 Self_ID
.Serial_Number
:= Next_Serial_Number
;
844 Next_Serial_Number
:= Next_Serial_Number
+ 1;
845 pragma Assert
(Next_Serial_Number
/= 0);
847 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
849 if not Single_Lock
then
851 pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access, null);
852 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
860 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
861 pragma Assert
(Result
= 0);
864 pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
865 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
870 if not Single_Lock
then
871 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
872 pragma Assert
(Result
= 0);
883 procedure Create_Task
885 Wrapper
: System
.Address
;
886 Stack_Size
: System
.Parameters
.Size_Type
;
887 Priority
: System
.Any_Priority
;
888 Succeeded
: out Boolean)
890 Attributes
: aliased pthread_attr_t
;
891 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
892 Result
: Interfaces
.C
.int
;
894 use type System
.Multiprocessors
.CPU_Range
;
897 -- Check whether both Dispatching_Domain and CPU are specified for
898 -- the task, and the CPU value is not contained within the range of
899 -- processors for the domain.
901 if T
.Common
.Domain
/= null
902 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
904 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
905 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
911 Adjusted_Stack_Size
:=
912 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
914 Result
:= pthread_attr_init
(Attributes
'Access);
915 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
923 pthread_attr_setstacksize
(Attributes
'Access, Adjusted_Stack_Size
);
924 pragma Assert
(Result
= 0);
927 pthread_attr_setdetachstate
928 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
929 pragma Assert
(Result
= 0);
931 -- Set the required attributes for the creation of the thread
933 -- Note: Previously, we called pthread_setaffinity_np (after thread
934 -- creation but before thread activation) to set the affinity but it was
935 -- not behaving as expected. Setting the required attributes for the
936 -- creation of the thread works correctly and it is more appropriate.
938 -- Do nothing if required support not provided by the operating system
940 if pthread_attr_setaffinity_np
'Address = System
.Null_Address
then
943 -- Support is available
945 elsif T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
then
947 CPUs
: constant size_t
:=
949 (System
.Multiprocessors
.Number_Of_CPUs
);
950 CPU_Set
: constant cpu_set_t_ptr
:= CPU_ALLOC
(CPUs
);
951 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
954 CPU_ZERO
(Size
, CPU_Set
);
955 System
.OS_Interface
.CPU_SET
956 (int
(T
.Common
.Base_CPU
), Size
, CPU_Set
);
958 pthread_attr_setaffinity_np
(Attributes
'Access, Size
, CPU_Set
);
959 pragma Assert
(Result
= 0);
966 elsif T
.Common
.Task_Info
/= null then
968 pthread_attr_setaffinity_np
971 T
.Common
.Task_Info
.CPU_Affinity
'Access);
972 pragma Assert
(Result
= 0);
974 -- Handle dispatching domains
976 -- To avoid changing CPU affinities when not needed, we set the
977 -- affinity only when assigning to a domain other than the default
978 -- one, or when the default one has been modified.
980 elsif T
.Common
.Domain
/= null and then
981 (T
.Common
.Domain
/= ST
.System_Domain
982 or else T
.Common
.Domain
.all /=
983 (Multiprocessors
.CPU
'First ..
984 Multiprocessors
.Number_Of_CPUs
=> True))
987 CPUs
: constant size_t
:=
989 (System
.Multiprocessors
.Number_Of_CPUs
);
990 CPU_Set
: constant cpu_set_t_ptr
:= CPU_ALLOC
(CPUs
);
991 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
994 CPU_ZERO
(Size
, CPU_Set
);
996 -- Set the affinity to all the processors belonging to the
997 -- dispatching domain.
999 for Proc
in T
.Common
.Domain
'Range loop
1000 if T
.Common
.Domain
(Proc
) then
1001 System
.OS_Interface
.CPU_SET
(int
(Proc
), Size
, CPU_Set
);
1006 pthread_attr_setaffinity_np
(Attributes
'Access, Size
, CPU_Set
);
1007 pragma Assert
(Result
= 0);
1013 -- Since the initial signal mask of a thread is inherited from the
1014 -- creator, and the Environment task has all its signals masked, we
1015 -- do not need to manipulate caller's signal mask at this point.
1016 -- All tasks in RTS will have All_Tasks_Mask initially.
1018 -- Note: the use of Unrestricted_Access in the following call is needed
1019 -- because otherwise we have an error of getting a access-to-volatile
1020 -- value which points to a non-volatile object. But in this case it is
1021 -- safe to do this, since we know we have no problems with aliasing and
1022 -- Unrestricted_Access bypasses this check.
1026 (T
.Common
.LL
.Thread
'Unrestricted_Access,
1028 Thread_Body_Access
(Wrapper
),
1032 (Result
= 0 or else Result
= EAGAIN
or else Result
= ENOMEM
);
1036 Result
:= pthread_attr_destroy
(Attributes
'Access);
1037 pragma Assert
(Result
= 0);
1043 Result
:= pthread_attr_destroy
(Attributes
'Access);
1044 pragma Assert
(Result
= 0);
1046 Set_Priority
(T
, Priority
);
1053 procedure Finalize_TCB
(T
: Task_Id
) is
1054 Result
: Interfaces
.C
.int
;
1057 if not Single_Lock
then
1058 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
1059 pragma Assert
(Result
= 0);
1062 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
1063 pragma Assert
(Result
= 0);
1065 if T
.Known_Tasks_Index
/= -1 then
1066 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1069 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
1071 ATCB_Allocation
.Free_ATCB
(T
);
1078 procedure Exit_Task
is
1080 Specific
.Set
(null);
1087 procedure Abort_Task
(T
: Task_Id
) is
1088 Result
: Interfaces
.C
.int
;
1090 ESRCH
: constant := 3; -- No such process
1091 -- It can happen that T has already vanished, in which case pthread_kill
1092 -- returns ESRCH, so we don't consider that to be an error.
1095 if Abort_Handler_Installed
then
1098 (T
.Common
.LL
.Thread
,
1099 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1100 pragma Assert
(Result
= 0 or else Result
= ESRCH
);
1108 procedure Initialize
(S
: in out Suspension_Object
) is
1109 Result
: Interfaces
.C
.int
;
1112 -- Initialize internal state (always to False (RM D.10(6)))
1117 -- Initialize internal mutex
1119 Result
:= pthread_mutex_init
(S
.L
'Access, null);
1121 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1123 if Result
= ENOMEM
then
1124 raise Storage_Error
;
1127 -- Initialize internal condition variable
1129 Result
:= pthread_cond_init
(S
.CV
'Access, null);
1131 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1134 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1135 pragma Assert
(Result
= 0);
1137 if Result
= ENOMEM
then
1138 raise Storage_Error
;
1147 procedure Finalize
(S
: in out Suspension_Object
) is
1148 Result
: Interfaces
.C
.int
;
1151 -- Destroy internal mutex
1153 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1154 pragma Assert
(Result
= 0);
1156 -- Destroy internal condition variable
1158 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1159 pragma Assert
(Result
= 0);
1166 function Current_State
(S
: Suspension_Object
) return Boolean is
1168 -- We do not want to use lock on this read operation. State is marked
1169 -- as Atomic so that we ensure that the value retrieved is correct.
1178 procedure Set_False
(S
: in out Suspension_Object
) is
1179 Result
: Interfaces
.C
.int
;
1182 SSL
.Abort_Defer
.all;
1184 Result
:= pthread_mutex_lock
(S
.L
'Access);
1185 pragma Assert
(Result
= 0);
1189 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1190 pragma Assert
(Result
= 0);
1192 SSL
.Abort_Undefer
.all;
1199 procedure Set_True
(S
: in out Suspension_Object
) is
1200 Result
: Interfaces
.C
.int
;
1203 SSL
.Abort_Defer
.all;
1205 Result
:= pthread_mutex_lock
(S
.L
'Access);
1206 pragma Assert
(Result
= 0);
1208 -- If there is already a task waiting on this suspension object then
1209 -- we resume it, leaving the state of the suspension object to False,
1210 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1211 -- the state to True.
1217 Result
:= pthread_cond_signal
(S
.CV
'Access);
1218 pragma Assert
(Result
= 0);
1224 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1225 pragma Assert
(Result
= 0);
1227 SSL
.Abort_Undefer
.all;
1230 ------------------------
1231 -- Suspend_Until_True --
1232 ------------------------
1234 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1235 Result
: Interfaces
.C
.int
;
1238 SSL
.Abort_Defer
.all;
1240 Result
:= pthread_mutex_lock
(S
.L
'Access);
1241 pragma Assert
(Result
= 0);
1245 -- Program_Error must be raised upon calling Suspend_Until_True
1246 -- if another task is already waiting on that suspension object
1249 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1250 pragma Assert
(Result
= 0);
1252 SSL
.Abort_Undefer
.all;
1254 raise Program_Error
;
1257 -- Suspend the task if the state is False. Otherwise, the task
1258 -- continues its execution, and the state of the suspension object
1259 -- is set to False (ARM D.10 par. 9).
1267 -- Loop in case pthread_cond_wait returns earlier than expected
1268 -- (e.g. in case of EINTR caused by a signal). This should not
1269 -- happen with the current Linux implementation of pthread, but
1270 -- POSIX does not guarantee it so this may change in future.
1272 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1273 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1275 exit when not S
.Waiting
;
1279 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1280 pragma Assert
(Result
= 0);
1282 SSL
.Abort_Undefer
.all;
1284 end Suspend_Until_True
;
1292 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1293 pragma Unreferenced
(Self_ID
);
1298 --------------------
1299 -- Check_No_Locks --
1300 --------------------
1302 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1303 pragma Unreferenced
(Self_ID
);
1308 ----------------------
1309 -- Environment_Task --
1310 ----------------------
1312 function Environment_Task
return Task_Id
is
1314 return Environment_Task_Id
;
1315 end Environment_Task
;
1321 function Suspend_Task
1323 Thread_Self
: Thread_Id
) return Boolean
1326 if T
.Common
.LL
.Thread
/= Thread_Self
then
1327 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1337 function Resume_Task
1339 Thread_Self
: Thread_Id
) return Boolean
1342 if T
.Common
.LL
.Thread
/= Thread_Self
then
1343 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1349 --------------------
1350 -- Stop_All_Tasks --
1351 --------------------
1353 procedure Stop_All_Tasks
is
1362 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1363 pragma Unreferenced
(T
);
1372 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1373 pragma Unreferenced
(T
);
1382 procedure Initialize
(Environment_Task
: Task_Id
) is
1383 act
: aliased struct_sigaction
;
1384 old_act
: aliased struct_sigaction
;
1385 Tmp_Set
: aliased sigset_t
;
1386 Result
: Interfaces
.C
.int
;
1387 -- Whether to use an alternate signal stack for stack overflows
1390 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1391 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1392 -- Get interrupt state. Defined in a-init.c
1393 -- The input argument is the interrupt number,
1394 -- and the result is one of the following:
1396 Default
: constant Character := 's';
1397 -- 'n' this interrupt not set by any Interrupt_State pragma
1398 -- 'u' Interrupt_State pragma set state to User
1399 -- 'r' Interrupt_State pragma set state to Runtime
1400 -- 's' Interrupt_State pragma set state to System (use "default"
1403 use type System
.Multiprocessors
.CPU_Range
;
1406 Environment_Task_Id
:= Environment_Task
;
1408 Interrupt_Management
.Initialize
;
1410 -- Prepare the set of signals that should be unblocked in all tasks
1412 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1413 pragma Assert
(Result
= 0);
1415 for J
in Interrupt_Management
.Interrupt_ID
loop
1416 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1417 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1418 pragma Assert
(Result
= 0);
1422 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1424 -- Initialize the global RTS lock
1426 Specific
.Initialize
(Environment_Task
);
1428 if Use_Alternate_Stack
then
1429 Environment_Task
.Common
.Task_Alternate_Stack
:=
1430 Alternate_Stack
'Address;
1433 -- Make environment task known here because it doesn't go through
1434 -- Activate_Tasks, which does it for all other tasks.
1436 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1437 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1439 Enter_Task
(Environment_Task
);
1442 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1445 act
.sa_handler
:= Abort_Handler
'Address;
1447 Result
:= sigemptyset
(Tmp_Set
'Access);
1448 pragma Assert
(Result
= 0);
1449 act
.sa_mask
:= Tmp_Set
;
1453 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1454 act
'Unchecked_Access,
1455 old_act
'Unchecked_Access);
1456 pragma Assert
(Result
= 0);
1457 Abort_Handler_Installed
:= True;
1460 -- pragma CPU and dispatching domains for the environment task
1462 Set_Task_Affinity
(Environment_Task
);
1465 -----------------------
1466 -- Set_Task_Affinity --
1467 -----------------------
1469 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1470 use type System
.Multiprocessors
.CPU_Range
;
1473 -- Do nothing if there is no support for setting affinities or the
1474 -- underlying thread has not yet been created. If the thread has not
1475 -- yet been created then the proper affinity will be set during its
1478 if pthread_setaffinity_np
'Address /= System
.Null_Address
1479 and then T
.Common
.LL
.Thread
/= Null_Thread_Id
1482 CPUs
: constant size_t
:=
1484 (System
.Multiprocessors
.Number_Of_CPUs
);
1485 CPU_Set
: cpu_set_t_ptr
:= null;
1486 Size
: constant size_t
:= CPU_ALLOC_SIZE
(CPUs
);
1488 Result
: Interfaces
.C
.int
;
1491 -- We look at the specific CPU (Base_CPU) first, then at the
1492 -- Task_Info field, and finally at the assigned dispatching
1495 if T
.Common
.Base_CPU
/= Multiprocessors
.Not_A_Specific_CPU
then
1497 -- Set the affinity to an unique CPU
1499 CPU_Set
:= CPU_ALLOC
(CPUs
);
1500 System
.OS_Interface
.CPU_ZERO
(Size
, CPU_Set
);
1501 System
.OS_Interface
.CPU_SET
1502 (int
(T
.Common
.Base_CPU
), Size
, CPU_Set
);
1506 elsif T
.Common
.Task_Info
/= null then
1507 CPU_Set
:= T
.Common
.Task_Info
.CPU_Affinity
'Access;
1509 -- Handle dispatching domains
1511 elsif T
.Common
.Domain
/= null and then
1512 (T
.Common
.Domain
/= ST
.System_Domain
1513 or else T
.Common
.Domain
.all /=
1514 (Multiprocessors
.CPU
'First ..
1515 Multiprocessors
.Number_Of_CPUs
=> True))
1517 -- Set the affinity to all the processors belonging to the
1518 -- dispatching domain. To avoid changing CPU affinities when
1519 -- not needed, we set the affinity only when assigning to a
1520 -- domain other than the default one, or when the default one
1521 -- has been modified.
1523 CPU_Set
:= CPU_ALLOC
(CPUs
);
1524 System
.OS_Interface
.CPU_ZERO
(Size
, CPU_Set
);
1526 for Proc
in T
.Common
.Domain
'Range loop
1527 if T
.Common
.Domain
(Proc
) then
1528 System
.OS_Interface
.CPU_SET
(int
(Proc
), Size
, CPU_Set
);
1533 -- We set the new affinity if needed. Otherwise, the new task
1534 -- will inherit its creator's CPU affinity mask (according to
1535 -- the documentation of pthread_setaffinity_np), which is
1536 -- consistent with Ada's required semantics.
1538 if CPU_Set
/= null then
1540 pthread_setaffinity_np
(T
.Common
.LL
.Thread
, Size
, CPU_Set
);
1541 pragma Assert
(Result
= 0);
1547 end Set_Task_Affinity
;
1549 end System
.Task_Primitives
.Operations
;