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-2018, 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)
44 -- Turn off polling, we do not want ATC polling to take place during tasking
45 -- operations. It causes infinite loops and other problems.
47 with Ada
.Unchecked_Conversion
;
51 with System
.Tasking
.Debug
;
52 with System
.Interrupt_Management
;
53 with System
.OS_Constants
;
54 with System
.OS_Primitives
;
55 with System
.Task_Info
;
57 with System
.Soft_Links
;
58 -- We use System.Soft_Links instead of System.Tasking.Initialization
59 -- because the later is a higher level package that we shouldn't depend on.
60 -- For example when using the restricted run time, it is replaced by
61 -- System.Tasking.Restricted.Stages.
63 package body System
.Task_Primitives
.Operations
is
65 package OSC
renames System
.OS_Constants
;
66 package SSL
renames System
.Soft_Links
;
68 use System
.Tasking
.Debug
;
71 use System
.OS_Interface
;
72 use System
.Parameters
;
73 use System
.OS_Primitives
;
79 -- The followings are logically constants, but need to be initialized
82 Single_RTS_Lock
: aliased RTS_Lock
;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 Environment_Task_Id
: Task_Id
;
88 -- A variable to hold Task_Id for the environment task
90 Locking_Policy
: Character;
91 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
92 -- Value of the pragma Locking_Policy:
93 -- 'C' for Ceiling_Locking
94 -- 'I' for Inherit_Locking
97 Unblocked_Signal_Mask
: aliased sigset_t
;
98 -- The set of signals that should unblocked in all tasks
100 -- The followings are internal configuration constants needed
102 Next_Serial_Number
: Task_Serial_Number
:= 100;
103 -- We start at 100, to reserve some special values for
104 -- using in error checking.
106 Time_Slice_Val
: Integer;
107 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
109 Dispatching_Policy
: Character;
110 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
112 Foreign_Task_Elaborated
: aliased Boolean := True;
113 -- Used to identified fake tasks (i.e., non-Ada Threads)
115 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
116 -- Whether to use an alternate signal stack for stack overflows
118 Abort_Handler_Installed
: Boolean := False;
119 -- True if a handler for the abort signal is installed
127 procedure Initialize
(Environment_Task
: Task_Id
);
128 pragma Inline
(Initialize
);
129 -- Initialize various data needed by this package
131 function Is_Valid_Task
return Boolean;
132 pragma Inline
(Is_Valid_Task
);
133 -- Does executing thread have a TCB?
135 procedure Set
(Self_Id
: Task_Id
);
137 -- Set the self id for the current task
139 function Self
return Task_Id
;
140 pragma Inline
(Self
);
141 -- Return a pointer to the Ada Task Control Block of the calling task
145 package body Specific
is separate;
146 -- The body of this package is target specific
150 function Monotonic_Clock
return Duration;
151 pragma Inline
(Monotonic_Clock
);
152 -- Returns an absolute time, represented as an offset relative to some
153 -- unspecified starting point, typically system boot time. This clock
154 -- is not affected by discontinuous jumps in the system time.
156 function RT_Resolution
return Duration;
157 pragma Inline
(RT_Resolution
);
158 -- Returns resolution of the underlying clock used to implement RT_Clock
160 procedure Timed_Sleep
161 (Self_ID
: ST
.Task_Id
;
163 Mode
: ST
.Delay_Modes
;
164 Reason
: System
.Tasking
.Task_States
;
165 Timedout
: out Boolean;
166 Yielded
: out Boolean);
167 -- Combination of Sleep (above) and Timed_Delay
169 procedure Timed_Delay
170 (Self_ID
: ST
.Task_Id
;
172 Mode
: ST
.Delay_Modes
);
173 -- Implement the semantics of the delay statement.
174 -- The caller should be abort-deferred and should not hold any locks.
178 package body Monotonic
is separate;
180 ----------------------------------
181 -- ATCB allocation/deallocation --
182 ----------------------------------
184 package body ATCB_Allocation
is separate;
185 -- The body of this package is shared across several targets
187 ---------------------------------
188 -- Support for foreign threads --
189 ---------------------------------
191 function Register_Foreign_Thread
193 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
194 -- Allocate and initialize a new ATCB for the current Thread. The size of
195 -- the secondary stack can be optionally specified.
197 function Register_Foreign_Thread
199 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
200 return Task_Id
is separate;
202 -----------------------
203 -- Local Subprograms --
204 -----------------------
206 procedure Abort_Handler
(Sig
: Signal
);
207 -- Signal handler used to implement asynchronous abort.
208 -- See also comment before body, below.
210 function To_Address
is
211 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
213 function GNAT_pthread_condattr_setup
214 (attr
: access pthread_condattr_t
) return int
;
216 GNAT_pthread_condattr_setup
, "__gnat_pthread_condattr_setup");
222 -- Target-dependent binding of inter-thread Abort signal to the raising of
223 -- the Abort_Signal exception.
225 -- The technical issues and alternatives here are essentially the
226 -- same as for raising exceptions in response to other signals
227 -- (e.g. Storage_Error). See code and comments in the package body
228 -- System.Interrupt_Management.
230 -- Some implementations may not allow an exception to be propagated out of
231 -- a handler, and others might leave the signal or interrupt that invoked
232 -- this handler masked after the exceptional return to the application
235 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
236 -- most UNIX systems, this will allow transfer out of a signal handler,
237 -- which is usually the only mechanism available for implementing
238 -- asynchronous handlers of this kind. However, some systems do not
239 -- restore the signal mask on longjmp(), leaving the abort signal masked.
241 procedure Abort_Handler
(Sig
: Signal
) is
242 pragma Unreferenced
(Sig
);
244 T
: constant Task_Id
:= Self
;
245 Old_Set
: aliased sigset_t
;
247 Result
: Interfaces
.C
.int
;
248 pragma Warnings
(Off
, Result
);
251 -- It's not safe to raise an exception when using GCC ZCX mechanism.
252 -- Note that we still need to install a signal handler, since in some
253 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
254 -- need to send the Abort signal to a task.
256 if ZCX_By_Default
then
260 if T
.Deferral_Level
= 0
261 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
266 -- Make sure signals used for RTS internal purpose are unmasked
268 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
269 Unblocked_Signal_Mask
'Access, Old_Set
'Access);
270 pragma Assert
(Result
= 0);
272 raise Standard
'Abort_Signal;
280 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
281 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
283 Res
: Interfaces
.C
.int
;
286 if Stack_Base_Available
then
288 -- Compute the guard page address
290 Page_Size
:= Address
(Get_Page_Size
);
293 (Stack_Base
- (Stack_Base
mod Page_Size
) + Page_Size
,
295 prot
=> (if On
then PROT_ON
else PROT_OFF
));
296 pragma Assert
(Res
= 0);
304 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
306 return T
.Common
.LL
.Thread
;
313 function Self
return Task_Id
renames Specific
.Self
;
315 ---------------------
316 -- Initialize_Lock --
317 ---------------------
319 -- Note: mutexes and cond_variables needed per-task basis are initialized
320 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
321 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
322 -- status change of RTS. Therefore raising Storage_Error in the following
323 -- routines should be able to be handled safely.
325 procedure Initialize_Lock
326 (Prio
: System
.Any_Priority
;
327 L
: not null access Lock
)
329 Attributes
: aliased pthread_mutexattr_t
;
330 Result
: Interfaces
.C
.int
;
333 Result
:= pthread_mutexattr_init
(Attributes
'Access);
334 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
336 if Result
= ENOMEM
then
340 if Locking_Policy
= 'C' then
341 Result
:= pthread_mutexattr_setprotocol
342 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
343 pragma Assert
(Result
= 0);
345 Result
:= pthread_mutexattr_setprioceiling
346 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
347 pragma Assert
(Result
= 0);
349 elsif Locking_Policy
= 'I' then
350 Result
:= pthread_mutexattr_setprotocol
351 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
352 pragma Assert
(Result
= 0);
355 Result
:= pthread_mutex_init
(L
.WO
'Access, Attributes
'Access);
356 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
358 if Result
= ENOMEM
then
359 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
363 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
364 pragma Assert
(Result
= 0);
367 procedure Initialize_Lock
368 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
370 pragma Unreferenced
(Level
);
372 Attributes
: aliased pthread_mutexattr_t
;
373 Result
: Interfaces
.C
.int
;
376 Result
:= pthread_mutexattr_init
(Attributes
'Access);
377 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
379 if Result
= ENOMEM
then
383 if Locking_Policy
= 'C' then
384 Result
:= pthread_mutexattr_setprotocol
385 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
386 pragma Assert
(Result
= 0);
388 Result
:= pthread_mutexattr_setprioceiling
389 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
390 pragma Assert
(Result
= 0);
392 elsif Locking_Policy
= 'I' then
393 Result
:= pthread_mutexattr_setprotocol
394 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
395 pragma Assert
(Result
= 0);
398 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
399 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
401 if Result
= ENOMEM
then
402 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
406 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
407 pragma Assert
(Result
= 0);
414 procedure Finalize_Lock
(L
: not null access Lock
) is
415 Result
: Interfaces
.C
.int
;
417 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
418 pragma Assert
(Result
= 0);
421 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
422 Result
: Interfaces
.C
.int
;
424 Result
:= pthread_mutex_destroy
(L
);
425 pragma Assert
(Result
= 0);
433 (L
: not null access Lock
; Ceiling_Violation
: out Boolean)
435 Result
: Interfaces
.C
.int
;
438 Result
:= pthread_mutex_lock
(L
.WO
'Access);
440 -- The cause of EINVAL is a priority ceiling violation
442 Ceiling_Violation
:= Result
= EINVAL
;
443 pragma Assert
(Result
= 0 or else Ceiling_Violation
);
447 (L
: not null access RTS_Lock
;
448 Global_Lock
: Boolean := False)
450 Result
: Interfaces
.C
.int
;
452 if not Single_Lock
or else Global_Lock
then
453 Result
:= pthread_mutex_lock
(L
);
454 pragma Assert
(Result
= 0);
458 procedure Write_Lock
(T
: Task_Id
) is
459 Result
: Interfaces
.C
.int
;
461 if not Single_Lock
then
462 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
463 pragma Assert
(Result
= 0);
472 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
474 Write_Lock
(L
, Ceiling_Violation
);
481 procedure Unlock
(L
: not null access Lock
) is
482 Result
: Interfaces
.C
.int
;
484 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
485 pragma Assert
(Result
= 0);
489 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False)
491 Result
: Interfaces
.C
.int
;
493 if not Single_Lock
or else Global_Lock
then
494 Result
:= pthread_mutex_unlock
(L
);
495 pragma Assert
(Result
= 0);
499 procedure Unlock
(T
: Task_Id
) is
500 Result
: Interfaces
.C
.int
;
502 if not Single_Lock
then
503 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
504 pragma Assert
(Result
= 0);
512 -- Dynamic priority ceilings are not supported by the underlying system
514 procedure Set_Ceiling
515 (L
: not null access Lock
;
516 Prio
: System
.Any_Priority
)
518 pragma Unreferenced
(L
, Prio
);
529 Reason
: System
.Tasking
.Task_States
)
531 pragma Unreferenced
(Reason
);
533 Result
: Interfaces
.C
.int
;
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));
543 -- EINTR is not considered a failure
545 pragma Assert
(Result
= 0 or else Result
= EINTR
);
552 -- This is for use within the run-time system, so abort is
553 -- assumed to be already deferred, and the caller should be
554 -- holding its own ATCB lock.
556 procedure Timed_Sleep
559 Mode
: ST
.Delay_Modes
;
560 Reason
: Task_States
;
561 Timedout
: out Boolean;
562 Yielded
: out Boolean) renames Monotonic
.Timed_Sleep
;
568 -- This is for use in implementing delay statements, so we assume the
569 -- caller is abort-deferred but is holding no locks.
571 procedure Timed_Delay
574 Mode
: ST
.Delay_Modes
) renames Monotonic
.Timed_Delay
;
576 ---------------------
577 -- Monotonic_Clock --
578 ---------------------
580 function Monotonic_Clock
return Duration renames Monotonic
.Monotonic_Clock
;
586 function RT_Resolution
return Duration renames Monotonic
.RT_Resolution
;
592 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
593 pragma Unreferenced
(Reason
);
594 Result
: Interfaces
.C
.int
;
596 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
597 pragma Assert
(Result
= 0);
604 procedure Yield
(Do_Yield
: Boolean := True) is
605 Result
: Interfaces
.C
.int
;
606 pragma Unreferenced
(Result
);
609 Result
:= sched_yield
;
617 procedure Set_Priority
619 Prio
: System
.Any_Priority
;
620 Loss_Of_Inheritance
: Boolean := False)
622 pragma Unreferenced
(Loss_Of_Inheritance
);
624 Result
: Interfaces
.C
.int
;
625 Param
: aliased struct_sched_param
;
627 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
628 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
629 -- Get priority specific dispatching policy
631 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
632 -- Upper case first character of the policy name corresponding to the
633 -- task as set by a Priority_Specific_Dispatching pragma.
636 T
.Common
.Current_Priority
:= Prio
;
637 Param
.sched_priority
:= To_Target_Priority
(Prio
);
639 if Time_Slice_Supported
640 and then (Dispatching_Policy
= 'R'
641 or else Priority_Specific_Policy
= 'R'
642 or else Time_Slice_Val
> 0)
644 Result
:= pthread_setschedparam
645 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
647 elsif Dispatching_Policy
= 'F'
648 or else Priority_Specific_Policy
= 'F'
649 or else Time_Slice_Val
= 0
651 Result
:= pthread_setschedparam
652 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
655 Result
:= pthread_setschedparam
656 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
659 pragma Assert
(Result
= 0);
666 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
668 return T
.Common
.Current_Priority
;
675 procedure Enter_Task
(Self_ID
: Task_Id
) is
677 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
678 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
680 Specific
.Set
(Self_ID
);
682 if Use_Alternate_Stack
then
684 Stack
: aliased stack_t
;
685 Result
: Interfaces
.C
.int
;
687 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
688 Stack
.ss_size
:= Alternate_Stack_Size
;
690 Result
:= sigaltstack
(Stack
'Access, null);
691 pragma Assert
(Result
= 0);
700 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
702 -----------------------------
703 -- Register_Foreign_Thread --
704 -----------------------------
706 function Register_Foreign_Thread
return Task_Id
is
708 if Is_Valid_Task
then
711 return Register_Foreign_Thread
(pthread_self
);
713 end Register_Foreign_Thread
;
719 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
720 Mutex_Attr
: aliased pthread_mutexattr_t
;
721 Result
: Interfaces
.C
.int
;
722 Cond_Attr
: aliased pthread_condattr_t
;
725 -- Give the task a unique serial number
727 Self_ID
.Serial_Number
:= Next_Serial_Number
;
728 Next_Serial_Number
:= Next_Serial_Number
+ 1;
729 pragma Assert
(Next_Serial_Number
/= 0);
731 if not Single_Lock
then
732 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
733 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
736 if Locking_Policy
= 'C' then
738 pthread_mutexattr_setprotocol
740 PTHREAD_PRIO_PROTECT
);
741 pragma Assert
(Result
= 0);
744 pthread_mutexattr_setprioceiling
746 Interfaces
.C
.int
(System
.Any_Priority
'Last));
747 pragma Assert
(Result
= 0);
749 elsif Locking_Policy
= 'I' then
751 pthread_mutexattr_setprotocol
753 PTHREAD_PRIO_INHERIT
);
754 pragma Assert
(Result
= 0);
759 (Self_ID
.Common
.LL
.L
'Access,
761 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
769 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
770 pragma Assert
(Result
= 0);
773 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
774 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
777 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
778 pragma Assert
(Result
= 0);
782 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
783 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
789 if not Single_Lock
then
790 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
791 pragma Assert
(Result
= 0);
797 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
798 pragma Assert
(Result
= 0);
805 procedure Create_Task
807 Wrapper
: System
.Address
;
808 Stack_Size
: System
.Parameters
.Size_Type
;
809 Priority
: System
.Any_Priority
;
810 Succeeded
: out Boolean)
812 Attributes
: aliased pthread_attr_t
;
813 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
814 Page_Size
: constant Interfaces
.C
.size_t
:=
815 Interfaces
.C
.size_t
(Get_Page_Size
);
816 Result
: Interfaces
.C
.int
;
818 function Thread_Body_Access
is new
819 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
821 use System
.Task_Info
;
824 Adjusted_Stack_Size
:=
825 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
827 if Stack_Base_Available
then
829 -- If Stack Checking is supported then allocate 2 additional pages:
831 -- In the worst case, stack is allocated at something like
832 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
833 -- to be sure the effective stack size is greater than what
836 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
839 -- Round stack size as this is required by some OSes (Darwin)
841 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
842 Adjusted_Stack_Size
:=
843 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
845 Result
:= pthread_attr_init
(Attributes
'Access);
846 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
854 pthread_attr_setdetachstate
855 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
856 pragma Assert
(Result
= 0);
859 pthread_attr_setstacksize
860 (Attributes
'Access, Adjusted_Stack_Size
);
861 pragma Assert
(Result
= 0);
863 if T
.Common
.Task_Info
/= Default_Scope
then
864 case T
.Common
.Task_Info
is
865 when System
.Task_Info
.Process_Scope
=>
867 pthread_attr_setscope
868 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
870 when System
.Task_Info
.System_Scope
=>
872 pthread_attr_setscope
873 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
875 when System
.Task_Info
.Default_Scope
=>
879 pragma Assert
(Result
= 0);
882 -- Since the initial signal mask of a thread is inherited from the
883 -- creator, and the Environment task has all its signals masked, we
884 -- do not need to manipulate caller's signal mask at this point.
885 -- All tasks in RTS will have All_Tasks_Mask initially.
887 -- Note: the use of Unrestricted_Access in the following call is needed
888 -- because otherwise we have an error of getting a access-to-volatile
889 -- value which points to a non-volatile object. But in this case it is
890 -- safe to do this, since we know we have no problems with aliasing and
891 -- Unrestricted_Access bypasses this check.
893 Result
:= pthread_create
894 (T
.Common
.LL
.Thread
'Unrestricted_Access,
896 Thread_Body_Access
(Wrapper
),
898 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
900 Succeeded
:= Result
= 0;
902 Result
:= pthread_attr_destroy
(Attributes
'Access);
903 pragma Assert
(Result
= 0);
906 Set_Priority
(T
, Priority
);
914 procedure Finalize_TCB
(T
: Task_Id
) is
915 Result
: Interfaces
.C
.int
;
918 if not Single_Lock
then
919 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
920 pragma Assert
(Result
= 0);
923 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
924 pragma Assert
(Result
= 0);
926 if T
.Known_Tasks_Index
/= -1 then
927 Known_Tasks
(T
.Known_Tasks_Index
) := null;
930 ATCB_Allocation
.Free_ATCB
(T
);
937 procedure Exit_Task
is
939 -- Mark this task as unknown, so that if Self is called, it won't
940 -- return a dangling pointer.
949 procedure Abort_Task
(T
: Task_Id
) is
950 Result
: Interfaces
.C
.int
;
952 if Abort_Handler_Installed
then
956 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
957 pragma Assert
(Result
= 0);
965 procedure Initialize
(S
: in out Suspension_Object
) is
966 Mutex_Attr
: aliased pthread_mutexattr_t
;
967 Cond_Attr
: aliased pthread_condattr_t
;
968 Result
: Interfaces
.C
.int
;
971 -- Initialize internal state (always to False (RM D.10 (6)))
976 -- Initialize internal mutex
978 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
979 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
981 if Result
= ENOMEM
then
985 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
986 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
988 if Result
= ENOMEM
then
989 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
990 pragma Assert
(Result
= 0);
995 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
996 pragma Assert
(Result
= 0);
998 -- Initialize internal condition variable
1000 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1001 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1004 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1005 pragma Assert
(Result
= 0);
1007 -- Storage_Error is propagated as intended if the allocation of the
1008 -- underlying OS entities fails.
1010 raise Storage_Error
;
1013 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
1014 pragma Assert
(Result
= 0);
1017 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1018 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1021 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1022 pragma Assert
(Result
= 0);
1024 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1025 pragma Assert
(Result
= 0);
1027 -- Storage_Error is propagated as intended if the allocation of the
1028 -- underlying OS entities fails.
1030 raise Storage_Error
;
1033 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1034 pragma Assert
(Result
= 0);
1041 procedure Finalize
(S
: in out Suspension_Object
) is
1042 Result
: Interfaces
.C
.int
;
1045 -- Destroy internal mutex
1047 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1048 pragma Assert
(Result
= 0);
1050 -- Destroy internal condition variable
1052 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1053 pragma Assert
(Result
= 0);
1060 function Current_State
(S
: Suspension_Object
) return Boolean is
1062 -- We do not want to use lock on this read operation. State is marked
1063 -- as Atomic so that we ensure that the value retrieved is correct.
1072 procedure Set_False
(S
: in out Suspension_Object
) is
1073 Result
: Interfaces
.C
.int
;
1076 SSL
.Abort_Defer
.all;
1078 Result
:= pthread_mutex_lock
(S
.L
'Access);
1079 pragma Assert
(Result
= 0);
1083 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1084 pragma Assert
(Result
= 0);
1086 SSL
.Abort_Undefer
.all;
1093 procedure Set_True
(S
: in out Suspension_Object
) is
1094 Result
: Interfaces
.C
.int
;
1097 SSL
.Abort_Defer
.all;
1099 Result
:= pthread_mutex_lock
(S
.L
'Access);
1100 pragma Assert
(Result
= 0);
1102 -- If there is already a task waiting on this suspension object then
1103 -- we resume it, leaving the state of the suspension object to False,
1104 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1105 -- the state to True.
1111 Result
:= pthread_cond_signal
(S
.CV
'Access);
1112 pragma Assert
(Result
= 0);
1118 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1119 pragma Assert
(Result
= 0);
1121 SSL
.Abort_Undefer
.all;
1124 ------------------------
1125 -- Suspend_Until_True --
1126 ------------------------
1128 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1129 Result
: Interfaces
.C
.int
;
1132 SSL
.Abort_Defer
.all;
1134 Result
:= pthread_mutex_lock
(S
.L
'Access);
1135 pragma Assert
(Result
= 0);
1139 -- Program_Error must be raised upon calling Suspend_Until_True
1140 -- if another task is already waiting on that suspension object
1143 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1144 pragma Assert
(Result
= 0);
1146 SSL
.Abort_Undefer
.all;
1148 raise Program_Error
;
1151 -- Suspend the task if the state is False. Otherwise, the task
1152 -- continues its execution, and the state of the suspension object
1153 -- is set to False (ARM D.10 par. 9).
1161 -- Loop in case pthread_cond_wait returns earlier than expected
1162 -- (e.g. in case of EINTR caused by a signal).
1164 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1165 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1167 exit when not S
.Waiting
;
1171 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1172 pragma Assert
(Result
= 0);
1174 SSL
.Abort_Undefer
.all;
1176 end Suspend_Until_True
;
1184 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1185 pragma Unreferenced
(Self_ID
);
1190 --------------------
1191 -- Check_No_Locks --
1192 --------------------
1194 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1195 pragma Unreferenced
(Self_ID
);
1200 ----------------------
1201 -- Environment_Task --
1202 ----------------------
1204 function Environment_Task
return Task_Id
is
1206 return Environment_Task_Id
;
1207 end Environment_Task
;
1213 procedure Lock_RTS
is
1215 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1222 procedure Unlock_RTS
is
1224 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1231 function Suspend_Task
1233 Thread_Self
: Thread_Id
) return Boolean
1235 pragma Unreferenced
(T
, Thread_Self
);
1244 function Resume_Task
1246 Thread_Self
: Thread_Id
) return Boolean
1248 pragma Unreferenced
(T
, Thread_Self
);
1253 --------------------
1254 -- Stop_All_Tasks --
1255 --------------------
1257 procedure Stop_All_Tasks
is
1266 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1267 pragma Unreferenced
(T
);
1276 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1277 pragma Unreferenced
(T
);
1286 procedure Initialize
(Environment_Task
: Task_Id
) is
1287 act
: aliased struct_sigaction
;
1288 old_act
: aliased struct_sigaction
;
1289 Tmp_Set
: aliased sigset_t
;
1290 Result
: Interfaces
.C
.int
;
1293 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1294 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1295 -- Get interrupt state. Defined in a-init.c
1296 -- The input argument is the interrupt number,
1297 -- and the result is one of the following:
1299 Default
: constant Character := 's';
1300 -- 'n' this interrupt not set by any Interrupt_State pragma
1301 -- 'u' Interrupt_State pragma set state to User
1302 -- 'r' Interrupt_State pragma set state to Runtime
1303 -- 's' Interrupt_State pragma set state to System (use "default"
1307 Environment_Task_Id
:= Environment_Task
;
1309 Interrupt_Management
.Initialize
;
1311 -- Prepare the set of signals that should unblocked in all tasks
1313 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1314 pragma Assert
(Result
= 0);
1316 for J
in Interrupt_Management
.Interrupt_ID
loop
1317 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1318 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1319 pragma Assert
(Result
= 0);
1323 -- Initialize the lock used to synchronize chain of all ATCBs
1325 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1327 Specific
.Initialize
(Environment_Task
);
1329 if Use_Alternate_Stack
then
1330 Environment_Task
.Common
.Task_Alternate_Stack
:=
1331 Alternate_Stack
'Address;
1334 -- Make environment task known here because it doesn't go through
1335 -- Activate_Tasks, which does it for all other tasks.
1337 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1338 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1340 Enter_Task
(Environment_Task
);
1343 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1346 act
.sa_handler
:= Abort_Handler
'Address;
1348 Result
:= sigemptyset
(Tmp_Set
'Access);
1349 pragma Assert
(Result
= 0);
1350 act
.sa_mask
:= Tmp_Set
;
1354 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1355 act
'Unchecked_Access,
1356 old_act
'Unchecked_Access);
1357 pragma Assert
(Result
= 0);
1358 Abort_Handler_Installed
:= True;
1362 -----------------------
1363 -- Set_Task_Affinity --
1364 -----------------------
1366 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1367 pragma Unreferenced
(T
);
1370 -- Setting task affinity is not supported by the underlying system
1373 end Set_Task_Affinity
;
1375 end System
.Task_Primitives
.Operations
;