1 ------------------------------------------------------------------------------
3 -- GNAT 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-2023, 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 POSIX-like version of this package
34 -- This package contains all the GNULL primitives that interface directly with
37 -- Note: this file can only be used for POSIX compliant systems that implement
38 -- SCHED_FIFO and Ceiling Locking correctly.
40 -- For configurations where SCHED_FIFO and priority ceiling are not a
41 -- requirement, this file can also be used (e.g AiX threads)
43 with Ada
.Unchecked_Conversion
;
47 with System
.Tasking
.Debug
;
48 with System
.Interrupt_Management
;
49 with System
.OS_Constants
;
50 with System
.OS_Primitives
;
51 with System
.Task_Info
;
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 OSC
renames System
.OS_Constants
;
62 package SSL
renames System
.Soft_Links
;
64 use System
.Tasking
.Debug
;
67 use System
.OS_Interface
;
68 use System
.Parameters
;
69 use System
.OS_Primitives
;
75 -- The followings are logically constants, but need to be initialized
78 Single_RTS_Lock
: aliased RTS_Lock
;
79 -- This is a lock to allow only one thread of control in the RTS at
80 -- a time; it is used to execute in mutual exclusion from all other tasks.
81 -- Used to protect All_Tasks_List
83 Environment_Task_Id
: Task_Id
;
84 -- A variable to hold Task_Id for the environment task
86 Locking_Policy
: constant Character;
87 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
88 -- Value of the pragma Locking_Policy:
89 -- 'C' for Ceiling_Locking
90 -- 'I' for Inherit_Locking
93 Unblocked_Signal_Mask
: aliased sigset_t
;
94 -- The set of signals that should unblocked in all tasks
96 -- The followings are internal configuration constants needed
98 Next_Serial_Number
: Task_Serial_Number
:= 100;
99 -- We start at 100, to reserve some special values for
100 -- using in error checking.
102 Time_Slice_Val
: constant Integer;
103 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
105 Dispatching_Policy
: constant Character;
106 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
108 Foreign_Task_Elaborated
: aliased Boolean := True;
109 -- Used to identified fake tasks (i.e., non-Ada Threads)
111 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
112 -- Whether to use an alternate signal stack for stack overflows
114 Abort_Handler_Installed
: Boolean := False;
115 -- True if a handler for the abort signal is installed
123 procedure Initialize
(Environment_Task
: Task_Id
);
124 pragma Inline
(Initialize
);
125 -- Initialize various data needed by this package
127 function Is_Valid_Task
return Boolean;
128 pragma Inline
(Is_Valid_Task
);
129 -- Does executing thread have a TCB?
131 procedure Set
(Self_Id
: Task_Id
);
133 -- Set the self id for the current task
135 function Self
return Task_Id
;
136 pragma Inline
(Self
);
137 -- Return a pointer to the Ada Task Control Block of the calling task
141 package body Specific
is separate;
142 -- The body of this package is target specific
146 function Monotonic_Clock
return Duration;
147 pragma Inline
(Monotonic_Clock
);
148 -- Returns an absolute time, represented as an offset relative to some
149 -- unspecified starting point, typically system boot time. This clock
150 -- is not affected by discontinuous jumps in the system time.
152 function RT_Resolution
return Duration;
153 pragma Inline
(RT_Resolution
);
154 -- Returns resolution of the underlying clock used to implement RT_Clock
156 procedure Timed_Sleep
157 (Self_ID
: ST
.Task_Id
;
159 Mode
: ST
.Delay_Modes
;
160 Reason
: System
.Tasking
.Task_States
;
161 Timedout
: out Boolean;
162 Yielded
: out Boolean);
163 -- Combination of Sleep (above) and Timed_Delay
165 procedure Timed_Delay
166 (Self_ID
: ST
.Task_Id
;
168 Mode
: ST
.Delay_Modes
);
169 -- Implement the semantics of the delay statement.
170 -- The caller should be abort-deferred and should not hold any locks.
174 package body Monotonic
is separate;
176 ----------------------------------
177 -- ATCB allocation/deallocation --
178 ----------------------------------
180 package body ATCB_Allocation
is separate;
181 -- The body of this package is shared across several targets
183 ---------------------------------
184 -- Support for foreign threads --
185 ---------------------------------
187 function Register_Foreign_Thread
189 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
190 -- Allocate and initialize a new ATCB for the current Thread. The size of
191 -- the secondary stack can be optionally specified.
193 function Register_Foreign_Thread
195 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
196 return Task_Id
is separate;
198 -----------------------
199 -- Local Subprograms --
200 -----------------------
202 procedure Abort_Handler
(Sig
: Signal
);
203 -- Signal handler used to implement asynchronous abort.
204 -- See also comment before body, below.
206 function To_Address
is
207 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
209 function GNAT_pthread_condattr_setup
210 (attr
: access pthread_condattr_t
) return int
;
212 GNAT_pthread_condattr_setup
, "__gnat_pthread_condattr_setup");
218 -- Target-dependent binding of inter-thread Abort signal to the raising of
219 -- the Abort_Signal exception.
221 -- The technical issues and alternatives here are essentially the
222 -- same as for raising exceptions in response to other signals
223 -- (e.g. Storage_Error). See code and comments in the package body
224 -- System.Interrupt_Management.
226 -- Some implementations may not allow an exception to be propagated out of
227 -- a handler, and others might leave the signal or interrupt that invoked
228 -- this handler masked after the exceptional return to the application
231 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
232 -- most UNIX systems, this will allow transfer out of a signal handler,
233 -- which is usually the only mechanism available for implementing
234 -- asynchronous handlers of this kind. However, some systems do not
235 -- restore the signal mask on longjmp(), leaving the abort signal masked.
237 procedure Abort_Handler
(Sig
: Signal
) is
238 pragma Unreferenced
(Sig
);
240 T
: constant Task_Id
:= Self
;
241 Old_Set
: aliased sigset_t
;
243 Result
: Interfaces
.C
.int
;
244 pragma Warnings
(Off
, Result
);
247 -- It's not safe to raise an exception when using GCC ZCX mechanism.
248 -- Note that we still need to install a signal handler, since in some
249 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
250 -- need to send the Abort signal to a task.
252 if ZCX_By_Default
then
256 if T
.Deferral_Level
= 0
257 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
262 -- Make sure signals used for RTS internal purpose are unmasked
264 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
265 Unblocked_Signal_Mask
'Access, Old_Set
'Access);
266 pragma Assert
(Result
= 0);
268 raise Standard
'Abort_Signal;
276 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
277 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
279 Res
: Interfaces
.C
.int
;
282 if Stack_Base_Available
then
284 -- Compute the guard page address
286 Page_Size
:= Address
(Get_Page_Size
);
289 (Stack_Base
- (Stack_Base
mod Page_Size
) + Page_Size
,
291 prot
=> (if On
then PROT_ON
else PROT_OFF
));
292 pragma Assert
(Res
= 0);
300 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
302 return T
.Common
.LL
.Thread
;
309 function Self
return Task_Id
renames Specific
.Self
;
311 ---------------------
312 -- Initialize_Lock --
313 ---------------------
315 -- Note: mutexes and cond_variables needed per-task basis are initialized
316 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
317 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
318 -- status change of RTS. Therefore raising Storage_Error in the following
319 -- routines should be able to be handled safely.
321 procedure Initialize_Lock
322 (Prio
: System
.Any_Priority
;
323 L
: not null access Lock
)
325 Attributes
: aliased pthread_mutexattr_t
;
326 Result
: Interfaces
.C
.int
;
329 Result
:= pthread_mutexattr_init
(Attributes
'Access);
330 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
332 if Result
= ENOMEM
then
336 if Locking_Policy
= 'C' then
337 Result
:= pthread_mutexattr_setprotocol
338 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
339 pragma Assert
(Result
= 0);
341 Result
:= pthread_mutexattr_setprioceiling
342 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
343 pragma Assert
(Result
= 0);
345 elsif Locking_Policy
= 'I' then
346 Result
:= pthread_mutexattr_setprotocol
347 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
348 pragma Assert
(Result
= 0);
351 Result
:= pthread_mutex_init
(L
.WO
'Access, Attributes
'Access);
352 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
354 if Result
= ENOMEM
then
355 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
359 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
360 pragma Assert
(Result
= 0);
363 procedure Initialize_Lock
364 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
366 pragma Unreferenced
(Level
);
368 Attributes
: aliased pthread_mutexattr_t
;
369 Result
: Interfaces
.C
.int
;
372 Result
:= pthread_mutexattr_init
(Attributes
'Access);
373 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
375 if Result
= ENOMEM
then
379 if Locking_Policy
= 'C' then
380 Result
:= pthread_mutexattr_setprotocol
381 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
382 pragma Assert
(Result
= 0);
384 Result
:= pthread_mutexattr_setprioceiling
385 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
386 pragma Assert
(Result
= 0);
388 elsif Locking_Policy
= 'I' then
389 Result
:= pthread_mutexattr_setprotocol
390 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
391 pragma Assert
(Result
= 0);
394 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
395 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
397 if Result
= ENOMEM
then
398 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
402 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
403 pragma Assert
(Result
= 0);
410 procedure Finalize_Lock
(L
: not null access Lock
) is
411 Result
: Interfaces
.C
.int
;
413 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
414 pragma Assert
(Result
= 0);
417 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
418 Result
: Interfaces
.C
.int
;
420 Result
:= pthread_mutex_destroy
(L
);
421 pragma Assert
(Result
= 0);
429 (L
: not null access Lock
; Ceiling_Violation
: out Boolean)
431 Result
: Interfaces
.C
.int
;
434 Result
:= pthread_mutex_lock
(L
.WO
'Access);
436 -- The cause of EINVAL is a priority ceiling violation
438 Ceiling_Violation
:= Result
= EINVAL
;
439 pragma Assert
(Result
= 0 or else Ceiling_Violation
);
442 procedure Write_Lock
(L
: not null access RTS_Lock
) is
443 Result
: Interfaces
.C
.int
;
445 Result
:= pthread_mutex_lock
(L
);
446 pragma Assert
(Result
= 0);
449 procedure Write_Lock
(T
: Task_Id
) is
450 Result
: Interfaces
.C
.int
;
452 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
453 pragma Assert
(Result
= 0);
461 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
463 Write_Lock
(L
, Ceiling_Violation
);
470 procedure Unlock
(L
: not null access Lock
) is
471 Result
: Interfaces
.C
.int
;
473 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
474 pragma Assert
(Result
= 0);
477 procedure Unlock
(L
: not null access RTS_Lock
) is
478 Result
: Interfaces
.C
.int
;
480 Result
:= pthread_mutex_unlock
(L
);
481 pragma Assert
(Result
= 0);
484 procedure Unlock
(T
: Task_Id
) is
485 Result
: Interfaces
.C
.int
;
487 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
488 pragma Assert
(Result
= 0);
495 -- Dynamic priority ceilings are not supported by the underlying system
497 procedure Set_Ceiling
498 (L
: not null access Lock
;
499 Prio
: System
.Any_Priority
)
501 pragma Unreferenced
(L
, Prio
);
512 Reason
: System
.Tasking
.Task_States
)
514 pragma Unreferenced
(Reason
);
516 Result
: Interfaces
.C
.int
;
521 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
522 mutex
=> Self_ID
.Common
.LL
.L
'Access);
524 -- EINTR is not considered a failure
526 pragma Assert
(Result
= 0 or else Result
= EINTR
);
533 -- This is for use within the run-time system, so abort is
534 -- assumed to be already deferred, and the caller should be
535 -- holding its own ATCB lock.
537 procedure Timed_Sleep
540 Mode
: ST
.Delay_Modes
;
541 Reason
: Task_States
;
542 Timedout
: out Boolean;
543 Yielded
: out Boolean) renames Monotonic
.Timed_Sleep
;
549 -- This is for use in implementing delay statements, so we assume the
550 -- caller is abort-deferred but is holding no locks.
552 procedure Timed_Delay
555 Mode
: ST
.Delay_Modes
) renames Monotonic
.Timed_Delay
;
557 ---------------------
558 -- Monotonic_Clock --
559 ---------------------
561 function Monotonic_Clock
return Duration renames Monotonic
.Monotonic_Clock
;
567 function RT_Resolution
return Duration renames Monotonic
.RT_Resolution
;
573 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
574 pragma Unreferenced
(Reason
);
575 Result
: Interfaces
.C
.int
;
577 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
578 pragma Assert
(Result
= 0);
585 procedure Yield
(Do_Yield
: Boolean := True) is
586 Result
: Interfaces
.C
.int
;
587 pragma Unreferenced
(Result
);
590 Result
:= sched_yield
;
598 procedure Set_Priority
600 Prio
: System
.Any_Priority
;
601 Loss_Of_Inheritance
: Boolean := False)
603 pragma Unreferenced
(Loss_Of_Inheritance
);
605 Result
: Interfaces
.C
.int
;
606 Param
: aliased struct_sched_param
;
608 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
609 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
610 -- Get priority specific dispatching policy
612 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
613 -- Upper case first character of the policy name corresponding to the
614 -- task as set by a Priority_Specific_Dispatching pragma.
617 T
.Common
.Current_Priority
:= Prio
;
618 Param
.sched_priority
:= To_Target_Priority
(Prio
);
620 if Time_Slice_Supported
621 and then (Dispatching_Policy
= 'R'
622 or else Priority_Specific_Policy
= 'R'
623 or else Time_Slice_Val
> 0)
625 Result
:= pthread_setschedparam
626 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
628 elsif Dispatching_Policy
= 'F'
629 or else Priority_Specific_Policy
= 'F'
630 or else Time_Slice_Val
= 0
632 Result
:= pthread_setschedparam
633 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
636 Result
:= pthread_setschedparam
637 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
640 pragma Assert
(Result
= 0);
647 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
649 return T
.Common
.Current_Priority
;
656 procedure Enter_Task
(Self_ID
: Task_Id
) is
658 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
659 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
661 Specific
.Set
(Self_ID
);
663 if Use_Alternate_Stack
then
665 Stack
: aliased stack_t
;
666 Result
: Interfaces
.C
.int
;
668 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
669 Stack
.ss_size
:= Alternate_Stack_Size
;
671 Result
:= sigaltstack
(Stack
'Access, null);
672 pragma Assert
(Result
= 0);
681 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
683 -----------------------------
684 -- Register_Foreign_Thread --
685 -----------------------------
687 function Register_Foreign_Thread
return Task_Id
is
689 if Is_Valid_Task
then
692 return Register_Foreign_Thread
(pthread_self
);
694 end Register_Foreign_Thread
;
700 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
701 Mutex_Attr
: aliased pthread_mutexattr_t
;
702 Result
: Interfaces
.C
.int
;
703 Cond_Attr
: aliased pthread_condattr_t
;
706 -- Give the task a unique serial number
708 Self_ID
.Serial_Number
:= Next_Serial_Number
;
709 Next_Serial_Number
:= Next_Serial_Number
+ 1;
710 pragma Assert
(Next_Serial_Number
/= 0);
712 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
713 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
716 if Locking_Policy
= 'C' then
718 pthread_mutexattr_setprotocol
720 PTHREAD_PRIO_PROTECT
);
721 pragma Assert
(Result
= 0);
724 pthread_mutexattr_setprioceiling
726 Interfaces
.C
.int
(System
.Any_Priority
'Last));
727 pragma Assert
(Result
= 0);
729 elsif Locking_Policy
= 'I' then
731 pthread_mutexattr_setprotocol
733 PTHREAD_PRIO_INHERIT
);
734 pragma Assert
(Result
= 0);
739 (Self_ID
.Common
.LL
.L
'Access,
741 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
749 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
750 pragma Assert
(Result
= 0);
752 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
753 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
756 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
757 pragma Assert
(Result
= 0);
761 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
762 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
768 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
769 pragma Assert
(Result
= 0);
773 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
774 pragma Assert
(Result
= 0);
781 procedure Create_Task
783 Wrapper
: System
.Address
;
784 Stack_Size
: System
.Parameters
.Size_Type
;
785 Priority
: System
.Any_Priority
;
786 Succeeded
: out Boolean)
788 Attributes
: aliased pthread_attr_t
;
789 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
790 Page_Size
: constant Interfaces
.C
.size_t
:=
791 Interfaces
.C
.size_t
(Get_Page_Size
);
792 Result
: Interfaces
.C
.int
;
794 function Thread_Body_Access
is new
795 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
797 use System
.Task_Info
;
800 Adjusted_Stack_Size
:=
801 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
803 if Stack_Base_Available
then
805 -- If Stack Checking is supported then allocate 2 additional pages:
807 -- In the worst case, stack is allocated at something like
808 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
809 -- to be sure the effective stack size is greater than what
812 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
815 -- Round stack size as this is required by some OSes (Darwin)
817 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
818 Adjusted_Stack_Size
:=
819 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
821 Result
:= pthread_attr_init
(Attributes
'Access);
822 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
830 pthread_attr_setdetachstate
831 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
832 pragma Assert
(Result
= 0);
835 pthread_attr_setstacksize
836 (Attributes
'Access, Adjusted_Stack_Size
);
837 pragma Assert
(Result
= 0);
839 if T
.Common
.Task_Info
/= Default_Scope
then
840 case T
.Common
.Task_Info
is
841 when System
.Task_Info
.Process_Scope
=>
843 pthread_attr_setscope
844 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
846 when System
.Task_Info
.System_Scope
=>
848 pthread_attr_setscope
849 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
851 when System
.Task_Info
.Default_Scope
=>
855 pragma Assert
(Result
= 0);
858 -- Since the initial signal mask of a thread is inherited from the
859 -- creator, and the Environment task has all its signals masked, we
860 -- do not need to manipulate caller's signal mask at this point.
861 -- All tasks in RTS will have All_Tasks_Mask initially.
863 -- Note: the use of Unrestricted_Access in the following call is needed
864 -- because otherwise we have an error of getting a access-to-volatile
865 -- value which points to a non-volatile object. But in this case it is
866 -- safe to do this, since we know we have no problems with aliasing and
867 -- Unrestricted_Access bypasses this check.
869 Result
:= pthread_create
870 (T
.Common
.LL
.Thread
'Unrestricted_Access,
872 Thread_Body_Access
(Wrapper
),
874 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
876 Succeeded
:= Result
= 0;
878 Result
:= pthread_attr_destroy
(Attributes
'Access);
879 pragma Assert
(Result
= 0);
882 Set_Priority
(T
, Priority
);
890 procedure Finalize_TCB
(T
: Task_Id
) is
891 Result
: Interfaces
.C
.int
;
894 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
895 pragma Assert
(Result
= 0);
897 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
898 pragma Assert
(Result
= 0);
900 if T
.Known_Tasks_Index
/= -1 then
901 Known_Tasks
(T
.Known_Tasks_Index
) := null;
904 ATCB_Allocation
.Free_ATCB
(T
);
911 procedure Exit_Task
is
913 -- Mark this task as unknown, so that if Self is called, it won't
914 -- return a dangling pointer.
923 procedure Abort_Task
(T
: Task_Id
) is
924 Result
: Interfaces
.C
.int
;
926 if Abort_Handler_Installed
then
930 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
931 pragma Assert
(Result
= 0);
939 procedure Initialize
(S
: in out Suspension_Object
) is
940 Mutex_Attr
: aliased pthread_mutexattr_t
;
941 Cond_Attr
: aliased pthread_condattr_t
;
942 Result
: Interfaces
.C
.int
;
945 -- Initialize internal state (always to False (RM D.10 (6)))
950 -- Initialize internal mutex
952 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
953 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
955 if Result
= ENOMEM
then
959 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
960 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
962 if Result
= ENOMEM
then
963 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
964 pragma Assert
(Result
= 0);
969 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
970 pragma Assert
(Result
= 0);
972 -- Initialize internal condition variable
974 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
975 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
978 Result
:= pthread_mutex_destroy
(S
.L
'Access);
979 pragma Assert
(Result
= 0);
981 -- Storage_Error is propagated as intended if the allocation of the
982 -- underlying OS entities fails.
987 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
988 pragma Assert
(Result
= 0);
991 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
992 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
995 Result
:= pthread_mutex_destroy
(S
.L
'Access);
996 pragma Assert
(Result
= 0);
998 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
999 pragma Assert
(Result
= 0);
1001 -- Storage_Error is propagated as intended if the allocation of the
1002 -- underlying OS entities fails.
1004 raise Storage_Error
;
1007 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1008 pragma Assert
(Result
= 0);
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 (RM D.10(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).
1138 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1139 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1141 exit when not S
.Waiting
;
1145 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1146 pragma Assert
(Result
= 0);
1148 SSL
.Abort_Undefer
.all;
1150 end Suspend_Until_True
;
1158 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1159 pragma Unreferenced
(Self_ID
);
1164 --------------------
1165 -- Check_No_Locks --
1166 --------------------
1168 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1169 pragma Unreferenced
(Self_ID
);
1174 ----------------------
1175 -- Environment_Task --
1176 ----------------------
1178 function Environment_Task
return Task_Id
is
1180 return Environment_Task_Id
;
1181 end Environment_Task
;
1187 procedure Lock_RTS
is
1189 Write_Lock
(Single_RTS_Lock
'Access);
1196 procedure Unlock_RTS
is
1198 Unlock
(Single_RTS_Lock
'Access);
1205 function Suspend_Task
1207 Thread_Self
: Thread_Id
) return Boolean
1209 pragma Unreferenced
(T
, Thread_Self
);
1218 function Resume_Task
1220 Thread_Self
: Thread_Id
) return Boolean
1222 pragma Unreferenced
(T
, Thread_Self
);
1227 --------------------
1228 -- Stop_All_Tasks --
1229 --------------------
1231 procedure Stop_All_Tasks
is
1240 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1241 pragma Unreferenced
(T
);
1250 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1251 pragma Unreferenced
(T
);
1260 procedure Initialize
(Environment_Task
: Task_Id
) is
1261 act
: aliased struct_sigaction
;
1262 old_act
: aliased struct_sigaction
;
1263 Tmp_Set
: aliased sigset_t
;
1264 Result
: Interfaces
.C
.int
;
1267 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1268 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1269 -- Get interrupt state. Defined in a-init.c
1270 -- The input argument is the interrupt number,
1271 -- and the result is one of the following:
1273 Default
: constant Character := 's';
1274 -- 'n' this interrupt not set by any Interrupt_State pragma
1275 -- 'u' Interrupt_State pragma set state to User
1276 -- 'r' Interrupt_State pragma set state to Runtime
1277 -- 's' Interrupt_State pragma set state to System (use "default"
1281 Environment_Task_Id
:= Environment_Task
;
1283 Interrupt_Management
.Initialize
;
1285 -- Prepare the set of signals that should unblocked in all tasks
1287 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1288 pragma Assert
(Result
= 0);
1290 for J
in Interrupt_Management
.Interrupt_ID
loop
1291 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1292 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1293 pragma Assert
(Result
= 0);
1297 -- Initialize the lock used to synchronize chain of all ATCBs
1299 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1301 Specific
.Initialize
(Environment_Task
);
1303 if Use_Alternate_Stack
then
1304 Environment_Task
.Common
.Task_Alternate_Stack
:=
1305 Alternate_Stack
'Address;
1308 -- Make environment task known here because it doesn't go through
1309 -- Activate_Tasks, which does it for all other tasks.
1311 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1312 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1314 Enter_Task
(Environment_Task
);
1317 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1320 act
.sa_handler
:= Abort_Handler
'Address;
1322 Result
:= sigemptyset
(Tmp_Set
'Access);
1323 pragma Assert
(Result
= 0);
1324 act
.sa_mask
:= Tmp_Set
;
1328 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1329 act
'Unchecked_Access,
1330 old_act
'Unchecked_Access);
1331 pragma Assert
(Result
= 0);
1332 Abort_Handler_Installed
:= True;
1336 -----------------------
1337 -- Set_Task_Affinity --
1338 -----------------------
1340 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1341 pragma Unreferenced
(T
);
1344 -- Setting task affinity is not supported by the underlying system
1347 end Set_Task_Affinity
;
1349 end System
.Task_Primitives
.Operations
;