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-2010, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
41 with Ada
.Unchecked_Conversion
;
42 with Ada
.Unchecked_Deallocation
;
46 with System
.Task_Info
;
47 with System
.Tasking
.Debug
;
48 with System
.Interrupt_Management
;
49 with System
.OS_Primitives
;
50 with System
.Stack_Checking
.Operations
;
51 with System
.Multiprocessors
;
53 with System
.Soft_Links
;
54 -- We use System.Soft_Links instead of System.Tasking.Initialization
55 -- because the later is a higher level package that we shouldn't depend on.
56 -- For example when using the restricted run time, it is replaced by
57 -- System.Tasking.Restricted.Stages.
59 package body System
.Task_Primitives
.Operations
is
61 package SSL
renames System
.Soft_Links
;
62 package SC
renames System
.Stack_Checking
.Operations
;
64 use System
.Tasking
.Debug
;
67 use System
.OS_Interface
;
68 use System
.Parameters
;
69 use System
.OS_Primitives
;
76 -- The followings are logically constants, but need to be initialized
79 Single_RTS_Lock
: aliased RTS_Lock
;
80 -- This is a lock to allow only one thread of control in the RTS at
81 -- a time; it is used to execute in mutual exclusion from all other tasks.
82 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
84 ATCB_Key
: aliased pthread_key_t
;
85 -- Key used to find the Ada Task_Id associated with a thread
87 Environment_Task_Id
: Task_Id
;
88 -- A variable to hold Task_Id for the environment task
90 Unblocked_Signal_Mask
: aliased sigset_t
;
91 -- The set of signals that should be unblocked in all tasks
93 -- The followings are internal configuration constants needed
95 Next_Serial_Number
: Task_Serial_Number
:= 100;
96 -- We start at 100 (reserve some special values for using in error checks)
98 Time_Slice_Val
: Integer;
99 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
101 Dispatching_Policy
: Character;
102 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
104 -- The following are effectively constants, but they need to be initialized
105 -- by calling a pthread_ function.
107 Mutex_Attr
: aliased pthread_mutexattr_t
;
108 Cond_Attr
: aliased pthread_condattr_t
;
110 Foreign_Task_Elaborated
: aliased Boolean := True;
111 -- Used to identified fake tasks (i.e., non-Ada Threads)
113 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
114 -- Whether to use an alternate signal stack for stack overflows
116 Abort_Handler_Installed
: Boolean := False;
117 -- True if a handler for the abort signal is installed
125 procedure Initialize
(Environment_Task
: Task_Id
);
126 pragma Inline
(Initialize
);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task
return Boolean;
130 pragma Inline
(Is_Valid_Task
);
131 -- Does executing thread have a TCB?
133 procedure Set
(Self_Id
: Task_Id
);
135 -- Set the self id for the current task
137 function Self
return Task_Id
;
138 pragma Inline
(Self
);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific
is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread
: Thread_Id
) return Task_Id
is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
162 procedure Abort_Handler
(signo
: Signal
);
164 function To_pthread_t
is new Ada
.Unchecked_Conversion
165 (unsigned_long
, System
.OS_Interface
.pthread_t
);
171 procedure Abort_Handler
(signo
: Signal
) is
172 pragma Unreferenced
(signo
);
174 Self_Id
: constant Task_Id
:= Self
;
175 Result
: Interfaces
.C
.int
;
176 Old_Set
: aliased sigset_t
;
179 -- It's not safe to raise an exception when using GCC ZCX mechanism.
180 -- Note that we still need to install a signal handler, since in some
181 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
182 -- need to send the Abort signal to a task.
184 if ZCX_By_Default
and then GCC_ZCX_Support
then
188 if Self_Id
.Deferral_Level
= 0
189 and then Self_Id
.Pending_ATC_Level
< Self_Id
.ATC_Nesting_Level
190 and then not Self_Id
.Aborting
192 Self_Id
.Aborting
:= True;
194 -- Make sure signals used for RTS internal purpose are unmasked
199 Unblocked_Signal_Mask
'Access,
201 pragma Assert
(Result
= 0);
203 raise Standard
'Abort_Signal;
211 procedure Lock_RTS
is
213 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
220 procedure Unlock_RTS
is
222 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
229 -- The underlying thread system extends the memory (up to 2MB) when needed
231 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
232 pragma Unreferenced
(T
);
233 pragma Unreferenced
(On
);
242 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
244 return T
.Common
.LL
.Thread
;
251 function Self
return Task_Id
renames Specific
.Self
;
253 ---------------------
254 -- Initialize_Lock --
255 ---------------------
257 -- Note: mutexes and cond_variables needed per-task basis are initialized
258 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
259 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
260 -- status change of RTS. Therefore raising Storage_Error in the following
261 -- routines should be able to be handled safely.
263 procedure Initialize_Lock
264 (Prio
: System
.Any_Priority
;
265 L
: not null access Lock
)
267 pragma Unreferenced
(Prio
);
269 Result
: Interfaces
.C
.int
;
272 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
274 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
276 if Result
= ENOMEM
then
277 raise Storage_Error
with "Failed to allocate a lock";
281 procedure Initialize_Lock
282 (L
: not null access RTS_Lock
;
285 pragma Unreferenced
(Level
);
287 Result
: Interfaces
.C
.int
;
290 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
292 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
294 if Result
= ENOMEM
then
303 procedure Finalize_Lock
(L
: not null access Lock
) is
304 Result
: Interfaces
.C
.int
;
306 Result
:= pthread_mutex_destroy
(L
);
307 pragma Assert
(Result
= 0);
310 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
311 Result
: Interfaces
.C
.int
;
313 Result
:= pthread_mutex_destroy
(L
);
314 pragma Assert
(Result
= 0);
322 (L
: not null access Lock
;
323 Ceiling_Violation
: out Boolean)
325 Result
: Interfaces
.C
.int
;
327 Result
:= pthread_mutex_lock
(L
);
328 Ceiling_Violation
:= Result
= EINVAL
;
330 -- Assume the cause of EINVAL is a priority ceiling violation
332 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
336 (L
: not null access RTS_Lock
;
337 Global_Lock
: Boolean := False)
339 Result
: Interfaces
.C
.int
;
341 if not Single_Lock
or else Global_Lock
then
342 Result
:= pthread_mutex_lock
(L
);
343 pragma Assert
(Result
= 0);
347 procedure Write_Lock
(T
: Task_Id
) is
348 Result
: Interfaces
.C
.int
;
350 if not Single_Lock
then
351 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
352 pragma Assert
(Result
= 0);
361 (L
: not null access Lock
;
362 Ceiling_Violation
: out Boolean)
365 Write_Lock
(L
, Ceiling_Violation
);
372 procedure Unlock
(L
: not null access Lock
) is
373 Result
: Interfaces
.C
.int
;
375 Result
:= pthread_mutex_unlock
(L
);
376 pragma Assert
(Result
= 0);
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_unlock
(L
);
387 pragma Assert
(Result
= 0);
391 procedure Unlock
(T
: Task_Id
) is
392 Result
: Interfaces
.C
.int
;
394 if not Single_Lock
then
395 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
396 pragma Assert
(Result
= 0);
404 -- Dynamic priority ceilings are not supported by the underlying system
406 procedure Set_Ceiling
407 (L
: not null access Lock
;
408 Prio
: System
.Any_Priority
)
410 pragma Unreferenced
(L
, Prio
);
421 Reason
: System
.Tasking
.Task_States
)
423 pragma Unreferenced
(Reason
);
425 Result
: Interfaces
.C
.int
;
428 pragma Assert
(Self_ID
= Self
);
432 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
433 mutex
=> (if Single_Lock
434 then Single_RTS_Lock
'Access
435 else Self_ID
.Common
.LL
.L
'Access));
437 -- EINTR is not considered a failure
439 pragma Assert
(Result
= 0 or else Result
= EINTR
);
446 -- This is for use within the run-time system, so abort is
447 -- assumed to be already deferred, and the caller should be
448 -- holding its own ATCB lock.
450 procedure Timed_Sleep
453 Mode
: ST
.Delay_Modes
;
454 Reason
: System
.Tasking
.Task_States
;
455 Timedout
: out Boolean;
456 Yielded
: out Boolean)
458 pragma Unreferenced
(Reason
);
460 Base_Time
: constant Duration := Monotonic_Clock
;
461 Check_Time
: Duration := Base_Time
;
463 Request
: aliased timespec
;
464 Result
: Interfaces
.C
.int
;
472 then Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
473 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
475 if Abs_Time
> Check_Time
then
476 Request
:= To_Timespec
(Abs_Time
);
479 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
482 pthread_cond_timedwait
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),
487 abstime
=> Request
'Access);
489 Check_Time
:= Monotonic_Clock
;
490 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
492 if Result
= 0 or else Result
= EINTR
then
494 -- Somebody may have called Wakeup for us
500 pragma Assert
(Result
= ETIMEDOUT
);
509 -- This is for use in implementing delay statements, so we assume the
510 -- caller is abort-deferred but is holding no locks.
512 procedure Timed_Delay
515 Mode
: ST
.Delay_Modes
)
517 Base_Time
: constant Duration := Monotonic_Clock
;
518 Check_Time
: Duration := Base_Time
;
520 Request
: aliased timespec
;
522 Result
: Interfaces
.C
.int
;
523 pragma Warnings
(Off
, Result
);
530 Write_Lock
(Self_ID
);
534 then Time
+ Check_Time
535 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
537 if Abs_Time
> Check_Time
then
538 Request
:= To_Timespec
(Abs_Time
);
539 Self_ID
.Common
.State
:= Delay_Sleep
;
542 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
545 pthread_cond_timedwait
546 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
547 mutex
=> (if Single_Lock
548 then Single_RTS_Lock
'Access
549 else Self_ID
.Common
.LL
.L
'Access),
550 abstime
=> Request
'Access);
552 Check_Time
:= Monotonic_Clock
;
553 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
555 pragma Assert
(Result
= 0 or else
556 Result
= ETIMEDOUT
or else
560 Self_ID
.Common
.State
:= Runnable
;
569 Result
:= sched_yield
;
572 ---------------------
573 -- Monotonic_Clock --
574 ---------------------
576 function Monotonic_Clock
return Duration is
579 type timeval
is array (1 .. 2) of C
.long
;
581 procedure timeval_to_duration
582 (T
: not null access timeval
;
583 sec
: not null access C
.long
;
584 usec
: not null access C
.long
);
585 pragma Import
(C
, timeval_to_duration
, "__gnat_timeval_to_duration");
587 Micro
: constant := 10**6;
588 sec
: aliased C
.long
;
589 usec
: aliased C
.long
;
590 TV
: aliased timeval
;
593 function gettimeofday
594 (Tv
: access timeval
;
595 Tz
: System
.Address
:= System
.Null_Address
) return int
;
596 pragma Import
(C
, gettimeofday
, "gettimeofday");
599 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
600 pragma Assert
(Result
= 0);
601 timeval_to_duration
(TV
'Access, sec
'Access, usec
'Access);
602 return Duration (sec
) + Duration (usec
) / Micro
;
609 function RT_Resolution
return Duration is
618 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
619 pragma Unreferenced
(Reason
);
620 Result
: Interfaces
.C
.int
;
622 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
623 pragma Assert
(Result
= 0);
630 procedure Yield
(Do_Yield
: Boolean := True) is
631 Result
: Interfaces
.C
.int
;
632 pragma Unreferenced
(Result
);
635 Result
:= sched_yield
;
643 procedure Set_Priority
645 Prio
: System
.Any_Priority
;
646 Loss_Of_Inheritance
: Boolean := False)
648 pragma Unreferenced
(Loss_Of_Inheritance
);
650 Result
: Interfaces
.C
.int
;
651 Param
: aliased struct_sched_param
;
653 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
654 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
655 -- Get priority specific dispatching policy
657 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
658 -- Upper case first character of the policy name corresponding to the
659 -- task as set by a Priority_Specific_Dispatching pragma.
662 T
.Common
.Current_Priority
:= Prio
;
664 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
666 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
) + 1;
668 if Dispatching_Policy
= 'R'
669 or else Priority_Specific_Policy
= 'R'
670 or else Time_Slice_Val
> 0
673 pthread_setschedparam
674 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
676 elsif Dispatching_Policy
= 'F'
677 or else Priority_Specific_Policy
= 'F'
678 or else Time_Slice_Val
= 0
681 pthread_setschedparam
682 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
685 Param
.sched_priority
:= 0;
687 pthread_setschedparam
689 SCHED_OTHER
, Param
'Access);
692 pragma Assert
(Result
= 0 or else Result
= EPERM
);
699 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
701 return T
.Common
.Current_Priority
;
708 procedure Enter_Task
(Self_ID
: Task_Id
) is
710 if Self_ID
.Common
.Task_Info
/= null
711 and then Self_ID
.Common
.Task_Info
.CPU_Affinity
= No_CPU
713 raise Invalid_CPU_Number
;
716 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
717 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
719 Specific
.Set
(Self_ID
);
721 if Use_Alternate_Stack
722 and then Self_ID
.Common
.Task_Alternate_Stack
/= Null_Address
725 Stack
: aliased stack_t
;
726 Result
: Interfaces
.C
.int
;
728 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
729 Stack
.ss_size
:= Alternate_Stack_Size
;
731 Result
:= sigaltstack
(Stack
'Access, null);
732 pragma Assert
(Result
= 0);
741 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
743 return new Ada_Task_Control_Block
(Entry_Num
);
750 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
752 -----------------------------
753 -- Register_Foreign_Thread --
754 -----------------------------
756 function Register_Foreign_Thread
return Task_Id
is
758 if Is_Valid_Task
then
761 return Register_Foreign_Thread
(pthread_self
);
763 end Register_Foreign_Thread
;
769 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
770 Result
: Interfaces
.C
.int
;
773 -- Give the task a unique serial number
775 Self_ID
.Serial_Number
:= Next_Serial_Number
;
776 Next_Serial_Number
:= Next_Serial_Number
+ 1;
777 pragma Assert
(Next_Serial_Number
/= 0);
779 Self_ID
.Common
.LL
.Thread
:= To_pthread_t
(-1);
781 if not Single_Lock
then
782 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
784 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
792 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
794 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
799 if not Single_Lock
then
800 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
801 pragma Assert
(Result
= 0);
812 procedure Create_Task
814 Wrapper
: System
.Address
;
815 Stack_Size
: System
.Parameters
.Size_Type
;
816 Priority
: System
.Any_Priority
;
817 Succeeded
: out Boolean)
819 Attributes
: aliased pthread_attr_t
;
820 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
821 Result
: Interfaces
.C
.int
;
823 use type System
.Multiprocessors
.CPU_Range
;
826 Adjusted_Stack_Size
:=
827 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
829 Result
:= pthread_attr_init
(Attributes
'Access);
830 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
838 pthread_attr_setstacksize
839 (Attributes
'Access, Adjusted_Stack_Size
);
840 pragma Assert
(Result
= 0);
843 pthread_attr_setdetachstate
844 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
845 pragma Assert
(Result
= 0);
847 -- Set the required attributes for the creation of the thread
849 -- Note: Previously, we called pthread_setaffinity_np (after thread
850 -- creation but before thread activation) to set the affinity but it was
851 -- not behaving as expected. Setting the required attributes for the
852 -- creation of the thread works correctly and it is more appropriate.
854 -- Do nothing if required support not provided by the operating system
856 if pthread_attr_setaffinity_np
'Address = System
.Null_Address
then
859 -- Support is available
861 elsif T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
then
863 CPU_Set
: aliased cpu_set_t
:= (bits
=> (others => False));
865 CPU_Set
.bits
(Integer (T
.Common
.Base_CPU
)) := True;
867 pthread_attr_setaffinity_np
871 pragma Assert
(Result
= 0);
876 elsif T
.Common
.Task_Info
/= null
877 and then T
.Common
.Task_Info
.CPU_Affinity
/= Task_Info
.Any_CPU
880 pthread_attr_setaffinity_np
883 T
.Common
.Task_Info
.CPU_Affinity
'Access);
884 pragma Assert
(Result
= 0);
887 -- Since the initial signal mask of a thread is inherited from the
888 -- creator, and the Environment task has all its signals masked, we
889 -- do not need to manipulate caller's signal mask at this point.
890 -- All tasks in RTS will have All_Tasks_Mask initially.
892 Result
:= pthread_create
893 (T
.Common
.LL
.Thread
'Access,
895 Thread_Body_Access
(Wrapper
),
898 (Result
= 0 or else Result
= EAGAIN
or else Result
= ENOMEM
);
902 Result
:= pthread_attr_destroy
(Attributes
'Access);
903 pragma Assert
(Result
= 0);
909 Result
:= pthread_attr_destroy
(Attributes
'Access);
910 pragma Assert
(Result
= 0);
912 Set_Priority
(T
, Priority
);
919 procedure Finalize_TCB
(T
: Task_Id
) is
920 Result
: Interfaces
.C
.int
;
922 Is_Self
: constant Boolean := T
= Self
;
924 procedure Free
is new
925 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
928 if not Single_Lock
then
929 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
930 pragma Assert
(Result
= 0);
933 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
934 pragma Assert
(Result
= 0);
936 if T
.Known_Tasks_Index
/= -1 then
937 Known_Tasks
(T
.Known_Tasks_Index
) := null;
939 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
951 procedure Exit_Task
is
960 procedure Abort_Task
(T
: Task_Id
) is
961 Result
: Interfaces
.C
.int
;
963 if Abort_Handler_Installed
then
967 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
968 pragma Assert
(Result
= 0);
976 procedure Initialize
(S
: in out Suspension_Object
) is
977 Result
: Interfaces
.C
.int
;
980 -- Initialize internal state (always to False (RM D.10(6)))
985 -- Initialize internal mutex
987 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
989 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
991 if Result
= ENOMEM
then
995 -- Initialize internal condition variable
997 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
999 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1002 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1003 pragma Assert
(Result
= 0);
1005 if Result
= ENOMEM
then
1006 raise Storage_Error
;
1015 procedure Finalize
(S
: in out Suspension_Object
) is
1016 Result
: Interfaces
.C
.int
;
1019 -- Destroy internal mutex
1021 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1022 pragma Assert
(Result
= 0);
1024 -- Destroy internal condition variable
1026 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1027 pragma Assert
(Result
= 0);
1034 function Current_State
(S
: Suspension_Object
) return Boolean is
1036 -- We do not want to use lock on this read operation. State is marked
1037 -- as Atomic so that we ensure that the value retrieved is correct.
1046 procedure Set_False
(S
: in out Suspension_Object
) is
1047 Result
: Interfaces
.C
.int
;
1050 SSL
.Abort_Defer
.all;
1052 Result
:= pthread_mutex_lock
(S
.L
'Access);
1053 pragma Assert
(Result
= 0);
1057 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1058 pragma Assert
(Result
= 0);
1060 SSL
.Abort_Undefer
.all;
1067 procedure Set_True
(S
: in out Suspension_Object
) is
1068 Result
: Interfaces
.C
.int
;
1071 SSL
.Abort_Defer
.all;
1073 Result
:= pthread_mutex_lock
(S
.L
'Access);
1074 pragma Assert
(Result
= 0);
1076 -- If there is already a task waiting on this suspension object then
1077 -- we resume it, leaving the state of the suspension object to False,
1078 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1079 -- the state to True.
1085 Result
:= pthread_cond_signal
(S
.CV
'Access);
1086 pragma Assert
(Result
= 0);
1092 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1093 pragma Assert
(Result
= 0);
1095 SSL
.Abort_Undefer
.all;
1098 ------------------------
1099 -- Suspend_Until_True --
1100 ------------------------
1102 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1103 Result
: Interfaces
.C
.int
;
1106 SSL
.Abort_Defer
.all;
1108 Result
:= pthread_mutex_lock
(S
.L
'Access);
1109 pragma Assert
(Result
= 0);
1113 -- Program_Error must be raised upon calling Suspend_Until_True
1114 -- if another task is already waiting on that suspension object
1117 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1118 pragma Assert
(Result
= 0);
1120 SSL
.Abort_Undefer
.all;
1122 raise Program_Error
;
1125 -- Suspend the task if the state is False. Otherwise, the task
1126 -- continues its execution, and the state of the suspension object
1127 -- is set to False (ARM D.10 par. 9).
1135 -- Loop in case pthread_cond_wait returns earlier than expected
1136 -- (e.g. in case of EINTR caused by a signal). This should not
1137 -- happen with the current Linux implementation of pthread, but
1138 -- POSIX does not guarantee it so this may change in future.
1140 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1141 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1143 exit when not S
.Waiting
;
1147 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1148 pragma Assert
(Result
= 0);
1150 SSL
.Abort_Undefer
.all;
1152 end Suspend_Until_True
;
1160 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1161 pragma Unreferenced
(Self_ID
);
1166 --------------------
1167 -- Check_No_Locks --
1168 --------------------
1170 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1171 pragma Unreferenced
(Self_ID
);
1176 ----------------------
1177 -- Environment_Task --
1178 ----------------------
1180 function Environment_Task
return Task_Id
is
1182 return Environment_Task_Id
;
1183 end Environment_Task
;
1189 function Suspend_Task
1191 Thread_Self
: Thread_Id
) return Boolean
1194 if T
.Common
.LL
.Thread
/= Thread_Self
then
1195 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1205 function Resume_Task
1207 Thread_Self
: Thread_Id
) return Boolean
1210 if T
.Common
.LL
.Thread
/= Thread_Self
then
1211 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1217 --------------------
1218 -- Stop_All_Tasks --
1219 --------------------
1221 procedure Stop_All_Tasks
is
1230 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1231 pragma Unreferenced
(T
);
1240 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1241 pragma Unreferenced
(T
);
1250 procedure Initialize
(Environment_Task
: Task_Id
) is
1251 act
: aliased struct_sigaction
;
1252 old_act
: aliased struct_sigaction
;
1253 Tmp_Set
: aliased sigset_t
;
1254 Result
: Interfaces
.C
.int
;
1255 -- Whether to use an alternate signal stack for stack overflows
1258 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1259 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1260 -- Get interrupt state. Defined in a-init.c
1261 -- The input argument is the interrupt number,
1262 -- and the result is one of the following:
1264 Default
: constant Character := 's';
1265 -- 'n' this interrupt not set by any Interrupt_State pragma
1266 -- 'u' Interrupt_State pragma set state to User
1267 -- 'r' Interrupt_State pragma set state to Runtime
1268 -- 's' Interrupt_State pragma set state to System (use "default"
1271 use type System
.Multiprocessors
.CPU_Range
;
1274 Environment_Task_Id
:= Environment_Task
;
1276 Interrupt_Management
.Initialize
;
1278 -- Prepare the set of signals that should be unblocked in all tasks
1280 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1281 pragma Assert
(Result
= 0);
1283 for J
in Interrupt_Management
.Interrupt_ID
loop
1284 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1285 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1286 pragma Assert
(Result
= 0);
1290 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1291 pragma Assert
(Result
= 0);
1293 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1294 pragma Assert
(Result
= 0);
1296 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1298 -- Initialize the global RTS lock
1300 Specific
.Initialize
(Environment_Task
);
1302 if Use_Alternate_Stack
then
1303 Environment_Task
.Common
.Task_Alternate_Stack
:=
1304 Alternate_Stack
'Address;
1307 -- Make environment task known here because it doesn't go through
1308 -- Activate_Tasks, which does it for all other tasks.
1310 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1311 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1313 Enter_Task
(Environment_Task
);
1316 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1319 act
.sa_handler
:= Abort_Handler
'Address;
1321 Result
:= sigemptyset
(Tmp_Set
'Access);
1322 pragma Assert
(Result
= 0);
1323 act
.sa_mask
:= Tmp_Set
;
1327 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1328 act
'Unchecked_Access,
1329 old_act
'Unchecked_Access);
1330 pragma Assert
(Result
= 0);
1331 Abort_Handler_Installed
:= True;
1334 -- pragma CPU for the environment task
1336 if pthread_setaffinity_np
'Address /= System
.Null_Address
1337 and then Environment_Task
.Common
.Base_CPU
/=
1338 System
.Multiprocessors
.Not_A_Specific_CPU
1341 CPU_Set
: aliased cpu_set_t
:= (bits
=> (others => False));
1343 CPU_Set
.bits
(Integer (Environment_Task
.Common
.Base_CPU
)) := True;
1345 pthread_setaffinity_np
1346 (Environment_Task
.Common
.LL
.Thread
,
1349 pragma Assert
(Result
= 0);
1354 end System
.Task_Primitives
.Operations
;