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-2024, 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 the RTEMS version of this package
34 -- This package contains all the GNULL primitives that interface directly with
37 with Ada
.Unchecked_Conversion
;
41 with System
.Tasking
.Debug
;
42 with System
.Interrupt_Management
;
43 with System
.OS_Constants
;
44 with System
.OS_Primitives
;
45 with System
.Task_Info
;
47 with System
.Soft_Links
;
48 -- We use System.Soft_Links instead of System.Tasking.Initialization
49 -- because the later is a higher level package that we shouldn't depend on.
50 -- For example when using the restricted run time, it is replaced by
51 -- System.Tasking.Restricted.Stages.
53 package body System
.Task_Primitives
.Operations
is
55 package OSC
renames System
.OS_Constants
;
56 package SSL
renames System
.Soft_Links
;
58 use System
.Tasking
.Debug
;
61 use System
.OS_Interface
;
62 use System
.Parameters
;
63 use System
.OS_Primitives
;
69 -- The followings are logically constants, but need to be initialized
72 Single_RTS_Lock
: aliased RTS_Lock
;
73 -- This is a lock to allow only one thread of control in the RTS at
74 -- a time; it is used to execute in mutual exclusion from all other tasks.
75 -- Used to protect All_Tasks_List
77 Environment_Task_Id
: Task_Id
;
78 -- A variable to hold Task_Id for the environment task
80 Locking_Policy
: constant Character;
81 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
82 -- Value of the pragma Locking_Policy:
83 -- 'C' for Ceiling_Locking
84 -- 'I' for Inherit_Locking
87 -- The followings are internal configuration constants needed
89 Next_Serial_Number
: Task_Serial_Number
:= 100;
90 -- We start at 100, to reserve some special values for
91 -- using in error checking.
93 Time_Slice_Val
: constant Integer;
94 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
96 Dispatching_Policy
: constant Character;
97 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
99 Foreign_Task_Elaborated
: aliased Boolean := True;
100 -- Used to identified fake tasks (i.e., non-Ada Threads)
102 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
103 -- Whether to use an alternate signal stack for stack overflows
105 Abort_Handler_Installed
: Boolean := False;
106 -- True if a handler for the abort signal is installed
114 procedure Initialize
(Environment_Task
: Task_Id
);
115 pragma Inline
(Initialize
);
116 -- Initialize various data needed by this package
118 function Is_Valid_Task
return Boolean;
119 pragma Inline
(Is_Valid_Task
);
120 -- Does executing thread have a TCB?
122 procedure Set
(Self_Id
: Task_Id
);
124 -- Set the self id for the current task
126 function Self
return Task_Id
;
127 pragma Inline
(Self
);
128 -- Return a pointer to the Ada Task Control Block of the calling task
132 package body Specific
is separate;
133 -- The body of this package is target specific
137 function Monotonic_Clock
return Duration;
138 pragma Inline
(Monotonic_Clock
);
139 -- Returns an absolute time, represented as an offset relative to some
140 -- unspecified starting point, typically system boot time. This clock
141 -- is not affected by discontinuous jumps in the system time.
143 function RT_Resolution
return Duration;
144 pragma Inline
(RT_Resolution
);
145 -- Returns resolution of the underlying clock used to implement RT_Clock
147 procedure Timed_Sleep
148 (Self_ID
: ST
.Task_Id
;
150 Mode
: ST
.Delay_Modes
;
151 Reason
: System
.Tasking
.Task_States
;
152 Timedout
: out Boolean;
153 Yielded
: out Boolean);
154 -- Combination of Sleep (above) and Timed_Delay
156 procedure Timed_Delay
157 (Self_ID
: ST
.Task_Id
;
159 Mode
: ST
.Delay_Modes
);
160 -- Implement the semantics of the delay statement.
161 -- The caller should be abort-deferred and should not hold any locks.
165 package body Monotonic
is separate;
167 ----------------------------------
168 -- ATCB allocation/deallocation --
169 ----------------------------------
171 package body ATCB_Allocation
is separate;
172 -- The body of this package is shared across several targets
174 ---------------------------------
175 -- Support for foreign threads --
176 ---------------------------------
178 function Register_Foreign_Thread
180 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
181 -- Allocate and initialize a new ATCB for the current Thread. The size of
182 -- the secondary stack can be optionally specified.
184 function Register_Foreign_Thread
186 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
187 return Task_Id
is separate;
189 -----------------------
190 -- Local Subprograms --
191 -----------------------
193 procedure Abort_Handler
(Sig
: Signal
);
194 -- Signal handler used to implement asynchronous abort.
195 -- See also comment before body, below.
197 function To_Address
is
198 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
200 function GNAT_pthread_condattr_setup
201 (attr
: access pthread_condattr_t
) return int
;
203 GNAT_pthread_condattr_setup
, "__gnat_pthread_condattr_setup");
209 -- Target-dependent binding of inter-thread Abort signal to the raising of
210 -- the Abort_Signal exception.
212 -- The technical issues and alternatives here are essentially the
213 -- same as for raising exceptions in response to other signals
214 -- (e.g. Storage_Error). See code and comments in the package body
215 -- System.Interrupt_Management.
217 -- Some implementations may not allow an exception to be propagated out of
218 -- a handler, and others might leave the signal or interrupt that invoked
219 -- this handler masked after the exceptional return to the application
222 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
223 -- most UNIX systems, this will allow transfer out of a signal handler,
224 -- which is usually the only mechanism available for implementing
225 -- asynchronous handlers of this kind. However, some systems do not
226 -- restore the signal mask on longjmp(), leaving the abort signal masked.
228 procedure Abort_Handler
(Sig
: Signal
) is
229 pragma Unreferenced
(Sig
);
231 T
: constant Task_Id
:= Self
;
232 Old_Set
: aliased sigset_t
;
233 Unblocked_Mask
: aliased sigset_t
;
234 Result
: Interfaces
.C
.int
;
235 pragma Warnings
(Off
, Result
);
238 -- It's not safe to raise an exception when using GCC ZCX mechanism.
239 -- Note that we still need to install a signal handler, since in some
240 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
241 -- need to send the Abort signal to a task.
243 if ZCX_By_Default
then
247 if T
.Deferral_Level
= 0
248 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
253 -- Make sure signals used for RTS internal purpose are unmasked
255 Result
:= sigemptyset
(Unblocked_Mask
'Access);
256 pragma Assert
(Result
= 0);
259 (Unblocked_Mask
'Access,
260 Signal
(Interrupt_Management
.Abort_Task_Interrupt
));
261 pragma Assert
(Result
= 0);
262 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGBUS
);
263 pragma Assert
(Result
= 0);
264 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGFPE
);
265 pragma Assert
(Result
= 0);
266 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGILL
);
267 pragma Assert
(Result
= 0);
268 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGSEGV
);
269 pragma Assert
(Result
= 0);
274 Unblocked_Mask
'Access,
276 pragma Assert
(Result
= 0);
278 raise Standard
'Abort_Signal;
286 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
287 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
289 Res
: Interfaces
.C
.int
;
292 if Stack_Base_Available
then
294 -- Compute the guard page address
296 Page_Size
:= Address
(Get_Page_Size
);
299 (Stack_Base
- (Stack_Base
mod Page_Size
) + Page_Size
,
301 prot
=> (if On
then PROT_ON
else PROT_OFF
));
302 pragma Assert
(Res
= 0);
310 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
312 return T
.Common
.LL
.Thread
;
319 function Self
return Task_Id
renames Specific
.Self
;
321 ---------------------
322 -- Initialize_Lock --
323 ---------------------
325 -- Note: mutexes and cond_variables needed per-task basis are initialized
326 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
327 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
328 -- status change of RTS. Therefore raising Storage_Error in the following
329 -- routines should be able to be handled safely.
331 procedure Initialize_Lock
332 (Prio
: System
.Any_Priority
;
333 L
: not null access Lock
)
335 Attributes
: aliased pthread_mutexattr_t
;
336 Result
: Interfaces
.C
.int
;
339 Result
:= pthread_mutexattr_init
(Attributes
'Access);
340 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
342 if Result
= ENOMEM
then
346 if Locking_Policy
= 'C' then
347 Result
:= pthread_mutexattr_setprotocol
348 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
349 pragma Assert
(Result
= 0);
351 Result
:= pthread_mutexattr_setprioceiling
352 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
353 pragma Assert
(Result
= 0);
355 elsif Locking_Policy
= 'I' then
356 Result
:= pthread_mutexattr_setprotocol
357 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
358 pragma Assert
(Result
= 0);
361 Result
:= pthread_mutex_init
(L
.WO
'Access, Attributes
'Access);
362 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
364 if Result
= ENOMEM
then
365 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
369 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
370 pragma Assert
(Result
= 0);
373 procedure Initialize_Lock
374 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
376 pragma Unreferenced
(Level
);
378 Attributes
: aliased pthread_mutexattr_t
;
379 Result
: Interfaces
.C
.int
;
382 Result
:= pthread_mutexattr_init
(Attributes
'Access);
383 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
385 if Result
= ENOMEM
then
389 if Locking_Policy
= 'C' then
390 Result
:= pthread_mutexattr_setprotocol
391 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
392 pragma Assert
(Result
= 0);
394 Result
:= pthread_mutexattr_setprioceiling
395 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
396 pragma Assert
(Result
= 0);
398 elsif Locking_Policy
= 'I' then
399 Result
:= pthread_mutexattr_setprotocol
400 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
401 pragma Assert
(Result
= 0);
404 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
405 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
407 if Result
= ENOMEM
then
408 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
412 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
413 pragma Assert
(Result
= 0);
420 procedure Finalize_Lock
(L
: not null access Lock
) is
421 Result
: Interfaces
.C
.int
;
423 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
424 pragma Assert
(Result
= 0);
427 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
428 Result
: Interfaces
.C
.int
;
430 Result
:= pthread_mutex_destroy
(L
);
431 pragma Assert
(Result
= 0);
439 (L
: not null access Lock
; Ceiling_Violation
: out Boolean)
441 Result
: Interfaces
.C
.int
;
444 Result
:= pthread_mutex_lock
(L
.WO
'Access);
446 -- The cause of EINVAL is a priority ceiling violation
448 Ceiling_Violation
:= Result
= EINVAL
;
449 pragma Assert
(Result
= 0 or else Ceiling_Violation
);
452 procedure Write_Lock
(L
: not null access RTS_Lock
) is
453 Result
: Interfaces
.C
.int
;
455 Result
:= pthread_mutex_lock
(L
);
456 pragma Assert
(Result
= 0);
459 procedure Write_Lock
(T
: Task_Id
) is
460 Result
: Interfaces
.C
.int
;
462 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
463 pragma Assert
(Result
= 0);
471 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
473 Write_Lock
(L
, Ceiling_Violation
);
480 procedure Unlock
(L
: not null access Lock
) is
481 Result
: Interfaces
.C
.int
;
483 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
484 pragma Assert
(Result
= 0);
487 procedure Unlock
(L
: not null access RTS_Lock
) is
488 Result
: Interfaces
.C
.int
;
490 Result
:= pthread_mutex_unlock
(L
);
491 pragma Assert
(Result
= 0);
494 procedure Unlock
(T
: Task_Id
) is
495 Result
: Interfaces
.C
.int
;
497 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
498 pragma Assert
(Result
= 0);
505 -- Dynamic priority ceilings are not supported by the underlying system
507 procedure Set_Ceiling
508 (L
: not null access Lock
;
509 Prio
: System
.Any_Priority
)
511 pragma Unreferenced
(L
, Prio
);
522 Reason
: System
.Tasking
.Task_States
)
524 pragma Unreferenced
(Reason
);
526 Result
: Interfaces
.C
.int
;
531 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
532 mutex
=> Self_ID
.Common
.LL
.L
'Access);
534 -- EINTR is not considered a failure
536 pragma Assert
(Result
= 0 or else Result
= EINTR
);
543 -- This is for use within the run-time system, so abort is
544 -- assumed to be already deferred, and the caller should be
545 -- holding its own ATCB lock.
547 procedure Timed_Sleep
550 Mode
: ST
.Delay_Modes
;
551 Reason
: Task_States
;
552 Timedout
: out Boolean;
553 Yielded
: out Boolean) renames Monotonic
.Timed_Sleep
;
559 -- This is for use in implementing delay statements, so we assume the
560 -- caller is abort-deferred but is holding no locks.
562 procedure Timed_Delay
565 Mode
: ST
.Delay_Modes
) renames Monotonic
.Timed_Delay
;
567 ---------------------
568 -- Monotonic_Clock --
569 ---------------------
571 function Monotonic_Clock
return Duration renames Monotonic
.Monotonic_Clock
;
577 function RT_Resolution
return Duration renames Monotonic
.RT_Resolution
;
583 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
584 pragma Unreferenced
(Reason
);
585 Result
: Interfaces
.C
.int
;
587 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
588 pragma Assert
(Result
= 0);
595 procedure Yield
(Do_Yield
: Boolean := True) is
596 Result
: Interfaces
.C
.int
;
597 pragma Unreferenced
(Result
);
600 Result
:= sched_yield
;
608 procedure Set_Priority
610 Prio
: System
.Any_Priority
;
611 Loss_Of_Inheritance
: Boolean := False)
613 pragma Unreferenced
(Loss_Of_Inheritance
);
615 Result
: Interfaces
.C
.int
;
616 Param
: aliased struct_sched_param
;
618 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
619 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
620 -- Get priority specific dispatching policy
622 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
623 -- Upper case first character of the policy name corresponding to the
624 -- task as set by a Priority_Specific_Dispatching pragma.
627 T
.Common
.Current_Priority
:= Prio
;
628 Param
.sched_priority
:= To_Target_Priority
(Prio
);
630 if Time_Slice_Supported
631 and then (Dispatching_Policy
= 'R'
632 or else Priority_Specific_Policy
= 'R'
633 or else Time_Slice_Val
> 0)
635 Result
:= pthread_setschedparam
636 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
638 elsif Dispatching_Policy
= 'F'
639 or else Priority_Specific_Policy
= 'F'
640 or else Time_Slice_Val
= 0
642 Result
:= pthread_setschedparam
643 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
646 Result
:= pthread_setschedparam
647 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
650 pragma Assert
(Result
= 0);
657 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
659 return T
.Common
.Current_Priority
;
666 procedure Enter_Task
(Self_ID
: Task_Id
) is
668 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
669 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
671 Specific
.Set
(Self_ID
);
673 if Use_Alternate_Stack
then
675 Stack
: aliased stack_t
;
676 Result
: Interfaces
.C
.int
;
678 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
679 Stack
.ss_size
:= Alternate_Stack_Size
;
681 Result
:= sigaltstack
(Stack
'Access, null);
682 pragma Assert
(Result
= 0);
691 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
693 -----------------------------
694 -- Register_Foreign_Thread --
695 -----------------------------
697 function Register_Foreign_Thread
return Task_Id
is
699 if Is_Valid_Task
then
702 return Register_Foreign_Thread
(pthread_self
);
704 end Register_Foreign_Thread
;
710 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
711 Mutex_Attr
: aliased pthread_mutexattr_t
;
712 Result
: Interfaces
.C
.int
;
713 Cond_Attr
: aliased pthread_condattr_t
;
716 -- Give the task a unique serial number
718 Self_ID
.Serial_Number
:= Next_Serial_Number
;
719 Next_Serial_Number
:= Next_Serial_Number
+ 1;
720 pragma Assert
(Next_Serial_Number
/= 0);
722 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
723 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
726 if Locking_Policy
= 'C' then
728 pthread_mutexattr_setprotocol
730 PTHREAD_PRIO_PROTECT
);
731 pragma Assert
(Result
= 0);
734 pthread_mutexattr_setprioceiling
736 Interfaces
.C
.int
(System
.Any_Priority
'Last));
737 pragma Assert
(Result
= 0);
739 elsif Locking_Policy
= 'I' then
741 pthread_mutexattr_setprotocol
743 PTHREAD_PRIO_INHERIT
);
744 pragma Assert
(Result
= 0);
749 (Self_ID
.Common
.LL
.L
'Access,
751 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
759 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
760 pragma Assert
(Result
= 0);
762 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
763 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
766 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
767 pragma Assert
(Result
= 0);
771 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
772 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
778 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
779 pragma Assert
(Result
= 0);
783 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
784 pragma Assert
(Result
= 0);
791 procedure Create_Task
793 Wrapper
: System
.Address
;
794 Stack_Size
: System
.Parameters
.Size_Type
;
795 Priority
: System
.Any_Priority
;
796 Succeeded
: out Boolean)
798 Attributes
: aliased pthread_attr_t
;
799 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
800 Page_Size
: constant Interfaces
.C
.size_t
:=
801 Interfaces
.C
.size_t
(Get_Page_Size
);
802 Result
: Interfaces
.C
.int
;
804 function Thread_Body_Access
is new
805 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
807 use System
.Task_Info
;
810 Adjusted_Stack_Size
:=
811 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
813 if Stack_Base_Available
then
815 -- If Stack Checking is supported then allocate 2 additional pages:
817 -- In the worst case, stack is allocated at something like
818 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
819 -- to be sure the effective stack size is greater than what
822 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
825 -- Round stack size as this is required by some OSes (Darwin)
827 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
828 Adjusted_Stack_Size
:=
829 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
831 Result
:= pthread_attr_init
(Attributes
'Access);
832 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
840 pthread_attr_setdetachstate
841 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
842 pragma Assert
(Result
= 0);
845 pthread_attr_setstacksize
846 (Attributes
'Access, Adjusted_Stack_Size
);
847 pragma Assert
(Result
= 0);
849 if T
.Common
.Task_Info
/= Default_Scope
then
850 case T
.Common
.Task_Info
is
851 when System
.Task_Info
.Process_Scope
=>
853 pthread_attr_setscope
854 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
856 when System
.Task_Info
.System_Scope
=>
858 pthread_attr_setscope
859 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
861 when System
.Task_Info
.Default_Scope
=>
865 pragma Assert
(Result
= 0);
868 -- Since the initial signal mask of a thread is inherited from the
869 -- creator, and the Environment task has all its signals masked, we
870 -- do not need to manipulate caller's signal mask at this point.
871 -- All tasks in RTS will have All_Tasks_Mask initially.
873 -- Note: the use of Unrestricted_Access in the following call is needed
874 -- because otherwise we have an error of getting a access-to-volatile
875 -- value which points to a non-volatile object. But in this case it is
876 -- safe to do this, since we know we have no problems with aliasing and
877 -- Unrestricted_Access bypasses this check.
879 Result
:= pthread_create
880 (T
.Common
.LL
.Thread
'Unrestricted_Access,
882 Thread_Body_Access
(Wrapper
),
884 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
886 Succeeded
:= Result
= 0;
888 Result
:= pthread_attr_destroy
(Attributes
'Access);
889 pragma Assert
(Result
= 0);
892 Set_Priority
(T
, Priority
);
900 procedure Finalize_TCB
(T
: Task_Id
) is
901 Result
: Interfaces
.C
.int
;
904 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
905 pragma Assert
(Result
= 0);
907 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
908 pragma Assert
(Result
= 0);
910 if T
.Known_Tasks_Index
/= -1 then
911 Known_Tasks
(T
.Known_Tasks_Index
) := null;
914 ATCB_Allocation
.Free_ATCB
(T
);
921 procedure Exit_Task
is
923 -- Mark this task as unknown, so that if Self is called, it won't
924 -- return a dangling pointer.
933 procedure Abort_Task
(T
: Task_Id
) is
934 Result
: Interfaces
.C
.int
;
936 if Abort_Handler_Installed
then
940 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
941 pragma Assert
(Result
= 0);
949 procedure Initialize
(S
: in out Suspension_Object
) is
950 Mutex_Attr
: aliased pthread_mutexattr_t
;
951 Cond_Attr
: aliased pthread_condattr_t
;
952 Result
: Interfaces
.C
.int
;
955 -- Initialize internal state (always to False (RM D.10 (6)))
960 -- Initialize internal mutex
962 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
963 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
965 if Result
= ENOMEM
then
969 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
970 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
972 if Result
= ENOMEM
then
973 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
974 pragma Assert
(Result
= 0);
979 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
980 pragma Assert
(Result
= 0);
982 -- Initialize internal condition variable
984 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
985 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
988 Result
:= pthread_mutex_destroy
(S
.L
'Access);
989 pragma Assert
(Result
= 0);
991 -- Storage_Error is propagated as intended if the allocation of the
992 -- underlying OS entities fails.
997 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
998 pragma Assert
(Result
= 0);
1001 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1002 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1005 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1006 pragma Assert
(Result
= 0);
1008 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1009 pragma Assert
(Result
= 0);
1011 -- Storage_Error is propagated as intended if the allocation of the
1012 -- underlying OS entities fails.
1014 raise Storage_Error
;
1017 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1018 pragma Assert
(Result
= 0);
1025 procedure Finalize
(S
: in out Suspension_Object
) is
1026 Result
: Interfaces
.C
.int
;
1029 -- Destroy internal mutex
1031 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1032 pragma Assert
(Result
= 0);
1034 -- Destroy internal condition variable
1036 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1037 pragma Assert
(Result
= 0);
1044 function Current_State
(S
: Suspension_Object
) return Boolean is
1046 -- We do not want to use lock on this read operation. State is marked
1047 -- as Atomic so that we ensure that the value retrieved is correct.
1056 procedure Set_False
(S
: in out Suspension_Object
) is
1057 Result
: Interfaces
.C
.int
;
1060 SSL
.Abort_Defer
.all;
1062 Result
:= pthread_mutex_lock
(S
.L
'Access);
1063 pragma Assert
(Result
= 0);
1067 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1068 pragma Assert
(Result
= 0);
1070 SSL
.Abort_Undefer
.all;
1077 procedure Set_True
(S
: in out Suspension_Object
) is
1078 Result
: Interfaces
.C
.int
;
1081 SSL
.Abort_Defer
.all;
1083 Result
:= pthread_mutex_lock
(S
.L
'Access);
1084 pragma Assert
(Result
= 0);
1086 -- If there is already a task waiting on this suspension object then
1087 -- we resume it, leaving the state of the suspension object to False,
1088 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1089 -- the state to True.
1095 Result
:= pthread_cond_signal
(S
.CV
'Access);
1096 pragma Assert
(Result
= 0);
1102 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1103 pragma Assert
(Result
= 0);
1105 SSL
.Abort_Undefer
.all;
1108 ------------------------
1109 -- Suspend_Until_True --
1110 ------------------------
1112 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1113 Result
: Interfaces
.C
.int
;
1116 SSL
.Abort_Defer
.all;
1118 Result
:= pthread_mutex_lock
(S
.L
'Access);
1119 pragma Assert
(Result
= 0);
1123 -- Program_Error must be raised upon calling Suspend_Until_True
1124 -- if another task is already waiting on that suspension object
1127 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1128 pragma Assert
(Result
= 0);
1130 SSL
.Abort_Undefer
.all;
1132 raise Program_Error
;
1135 -- Suspend the task if the state is False. Otherwise, the task
1136 -- continues its execution, and the state of the suspension object
1137 -- is set to False (ARM D.10 par. 9).
1145 -- Loop in case pthread_cond_wait returns earlier than expected
1146 -- (e.g. in case of EINTR caused by a signal).
1148 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1149 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1151 exit when not S
.Waiting
;
1155 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1156 pragma Assert
(Result
= 0);
1158 SSL
.Abort_Undefer
.all;
1160 end Suspend_Until_True
;
1168 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1169 pragma Unreferenced
(Self_ID
);
1174 --------------------
1175 -- Check_No_Locks --
1176 --------------------
1178 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1179 pragma Unreferenced
(Self_ID
);
1184 ----------------------
1185 -- Environment_Task --
1186 ----------------------
1188 function Environment_Task
return Task_Id
is
1190 return Environment_Task_Id
;
1191 end Environment_Task
;
1197 procedure Lock_RTS
is
1199 Write_Lock
(Single_RTS_Lock
'Access);
1206 procedure Unlock_RTS
is
1208 Unlock
(Single_RTS_Lock
'Access);
1215 function Suspend_Task
1217 Thread_Self
: Thread_Id
) return Boolean
1219 pragma Unreferenced
(T
, Thread_Self
);
1228 function Resume_Task
1230 Thread_Self
: Thread_Id
) return Boolean
1232 pragma Unreferenced
(T
, Thread_Self
);
1237 --------------------
1238 -- Stop_All_Tasks --
1239 --------------------
1241 procedure Stop_All_Tasks
is
1250 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1251 pragma Unreferenced
(T
);
1260 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1261 pragma Unreferenced
(T
);
1270 procedure Initialize
(Environment_Task
: Task_Id
) is
1271 act
: aliased struct_sigaction
;
1272 old_act
: aliased struct_sigaction
;
1273 Tmp_Set
: aliased sigset_t
;
1274 Result
: Interfaces
.C
.int
;
1277 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1278 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1279 -- Get interrupt state. Defined in a-init.c
1280 -- The input argument is the interrupt number,
1281 -- and the result is one of the following:
1283 Default
: constant Character := 's';
1284 -- 'n' this interrupt not set by any Interrupt_State pragma
1285 -- 'u' Interrupt_State pragma set state to User
1286 -- 'r' Interrupt_State pragma set state to Runtime
1287 -- 's' Interrupt_State pragma set state to System (use "default"
1291 Environment_Task_Id
:= Environment_Task
;
1293 Interrupt_Management
.Initialize
;
1295 -- Initialize the lock used to synchronize chain of all ATCBs
1297 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1299 Specific
.Initialize
(Environment_Task
);
1301 if Use_Alternate_Stack
then
1302 Environment_Task
.Common
.Task_Alternate_Stack
:=
1303 Alternate_Stack
'Address;
1306 -- Make environment task known here because it doesn't go through
1307 -- Activate_Tasks, which does it for all other tasks.
1309 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1310 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1312 Enter_Task
(Environment_Task
);
1315 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1318 act
.sa_handler
:= Abort_Handler
'Address;
1320 Result
:= sigemptyset
(Tmp_Set
'Access);
1321 pragma Assert
(Result
= 0);
1322 act
.sa_mask
:= Tmp_Set
;
1326 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1327 act
'Unchecked_Access,
1328 old_act
'Unchecked_Access);
1329 pragma Assert
(Result
= 0);
1330 Abort_Handler_Installed
:= True;
1334 -----------------------
1335 -- Set_Task_Affinity --
1336 -----------------------
1338 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1339 pragma Unreferenced
(T
);
1342 -- Setting task affinity is not supported by the underlying system
1345 end Set_Task_Affinity
;
1347 end System
.Task_Primitives
.Operations
;