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-2005, 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a POSIX-like version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 -- Note: this file can only be used for POSIX compliant systems that
40 -- implement SCHED_FIFO and Ceiling Locking correctly.
42 -- For configurations where SCHED_FIFO and priority ceiling are not a
43 -- requirement, this file can also be used (e.g AiX threads)
46 -- Turn off polling, we do not want ATC polling to take place during
47 -- tasking operations. It causes infinite loops and other problems.
49 with System
.Tasking
.Debug
;
50 -- used for Known_Tasks
52 with System
.Interrupt_Management
;
53 -- used for Keep_Unmasked
54 -- Abort_Task_Interrupt
57 with System
.OS_Primitives
;
58 -- used for Delay_Modes
60 with System
.Task_Info
;
61 -- used for Task_Info_Type
67 with System
.Parameters
;
70 with Unchecked_Conversion
;
71 with Unchecked_Deallocation
;
73 package body System
.Task_Primitives
.Operations
is
75 use System
.Tasking
.Debug
;
78 use System
.OS_Interface
;
79 use System
.Parameters
;
80 use System
.OS_Primitives
;
86 -- The followings are logically constants, but need to be initialized
89 Single_RTS_Lock
: aliased RTS_Lock
;
90 -- This is a lock to allow only one thread of control in the RTS at
91 -- a time; it is used to execute in mutual exclusion from all other tasks.
92 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
94 ATCB_Key
: aliased pthread_key_t
;
95 -- Key used to find the Ada Task_Id associated with a thread
97 Environment_Task_Id
: Task_Id
;
98 -- A variable to hold Task_Id for the environment task.
100 Locking_Policy
: Character;
101 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
102 -- Value of the pragma Locking_Policy:
103 -- 'C' for Ceiling_Locking
104 -- 'I' for Inherit_Locking
107 Unblocked_Signal_Mask
: aliased sigset_t
;
108 -- The set of signals that should unblocked in all tasks
110 -- The followings are internal configuration constants needed.
112 Next_Serial_Number
: Task_Serial_Number
:= 100;
113 -- We start at 100, to reserve some special values for
114 -- using in error checking.
116 Time_Slice_Val
: Integer;
117 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
119 Dispatching_Policy
: Character;
120 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
122 Foreign_Task_Elaborated
: aliased Boolean := True;
123 -- Used to identified fake tasks (i.e., non-Ada Threads).
131 procedure Initialize
(Environment_Task
: Task_Id
);
132 pragma Inline
(Initialize
);
133 -- Initialize various data needed by this package.
135 function Is_Valid_Task
return Boolean;
136 pragma Inline
(Is_Valid_Task
);
137 -- Does executing thread have a TCB?
139 procedure Set
(Self_Id
: Task_Id
);
141 -- Set the self id for the current task.
143 function Self
return Task_Id
;
144 pragma Inline
(Self
);
145 -- Return a pointer to the Ada Task Control Block of the calling task.
149 package body Specific
is separate;
150 -- The body of this package is target specific.
152 ---------------------------------
153 -- Support for foreign threads --
154 ---------------------------------
156 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
157 -- Allocate and Initialize a new ATCB for the current Thread.
159 function Register_Foreign_Thread
160 (Thread
: Thread_Id
) return Task_Id
is separate;
162 -----------------------
163 -- Local Subprograms --
164 -----------------------
166 procedure Abort_Handler
(Sig
: Signal
);
167 -- Signal handler used to implement asynchronous abort.
168 -- See also comment before body, below.
170 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
176 -- Target-dependent binding of inter-thread Abort signal to
177 -- the raising of the Abort_Signal exception.
179 -- The technical issues and alternatives here are essentially
180 -- the same as for raising exceptions in response to other
181 -- signals (e.g. Storage_Error). See code and comments in
182 -- the package body System.Interrupt_Management.
184 -- Some implementations may not allow an exception to be propagated
185 -- out of a handler, and others might leave the signal or
186 -- interrupt that invoked this handler masked after the exceptional
187 -- return to the application code.
189 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
190 -- On most UNIX systems, this will allow transfer out of a signal handler,
191 -- which is usually the only mechanism available for implementing
192 -- asynchronous handlers of this kind. However, some
193 -- systems do not restore the signal mask on longjmp(), leaving the
194 -- abort signal masked.
196 procedure Abort_Handler
(Sig
: Signal
) is
197 pragma Warnings
(Off
, Sig
);
199 T
: constant Task_Id
:= Self
;
200 Result
: Interfaces
.C
.int
;
201 Old_Set
: aliased sigset_t
;
204 -- It is not safe to raise an exception when using ZCX and the GCC
205 -- exception handling mechanism.
207 if ZCX_By_Default
and then GCC_ZCX_Support
then
211 if T
.Deferral_Level
= 0
212 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
217 -- Make sure signals used for RTS internal purpose are unmasked
219 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
220 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
221 pragma Assert
(Result
= 0);
223 raise Standard
'Abort_Signal;
231 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
232 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
233 Guard_Page_Address
: Address
;
235 Res
: Interfaces
.C
.int
;
238 if Stack_Base_Available
then
240 -- Compute the guard page address
242 Guard_Page_Address
:=
243 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
246 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
248 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
251 pragma Assert
(Res
= 0);
259 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
261 return T
.Common
.LL
.Thread
;
268 function Self
return Task_Id
renames Specific
.Self
;
270 ---------------------
271 -- Initialize_Lock --
272 ---------------------
274 -- Note: mutexes and cond_variables needed per-task basis are
275 -- initialized in Intialize_TCB and the Storage_Error is
276 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
277 -- used in RTS is initialized before any status change of RTS.
278 -- Therefore rasing Storage_Error in the following routines
279 -- should be able to be handled safely.
281 procedure Initialize_Lock
282 (Prio
: System
.Any_Priority
;
285 Attributes
: aliased pthread_mutexattr_t
;
286 Result
: Interfaces
.C
.int
;
289 Result
:= pthread_mutexattr_init
(Attributes
'Access);
290 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
292 if Result
= ENOMEM
then
296 if Locking_Policy
= 'C' then
297 Result
:= pthread_mutexattr_setprotocol
298 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
299 pragma Assert
(Result
= 0);
301 Result
:= pthread_mutexattr_setprioceiling
302 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
303 pragma Assert
(Result
= 0);
305 elsif Locking_Policy
= 'I' then
306 Result
:= pthread_mutexattr_setprotocol
307 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
308 pragma Assert
(Result
= 0);
311 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
312 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
314 if Result
= ENOMEM
then
315 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
319 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
320 pragma Assert
(Result
= 0);
323 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
324 pragma Warnings
(Off
, Level
);
326 Attributes
: aliased pthread_mutexattr_t
;
327 Result
: Interfaces
.C
.int
;
330 Result
:= pthread_mutexattr_init
(Attributes
'Access);
331 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
333 if Result
= ENOMEM
then
337 if Locking_Policy
= 'C' then
338 Result
:= pthread_mutexattr_setprotocol
339 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
340 pragma Assert
(Result
= 0);
342 Result
:= pthread_mutexattr_setprioceiling
343 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
344 pragma Assert
(Result
= 0);
346 elsif Locking_Policy
= 'I' then
347 Result
:= pthread_mutexattr_setprotocol
348 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
349 pragma Assert
(Result
= 0);
352 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
353 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
355 if Result
= ENOMEM
then
356 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
360 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
361 pragma Assert
(Result
= 0);
368 procedure Finalize_Lock
(L
: access Lock
) is
369 Result
: Interfaces
.C
.int
;
372 Result
:= pthread_mutex_destroy
(L
);
373 pragma Assert
(Result
= 0);
376 procedure Finalize_Lock
(L
: access RTS_Lock
) is
377 Result
: Interfaces
.C
.int
;
380 Result
:= pthread_mutex_destroy
(L
);
381 pragma Assert
(Result
= 0);
388 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
389 Result
: Interfaces
.C
.int
;
392 Result
:= pthread_mutex_lock
(L
);
394 -- Assume that the cause of EINVAL is a priority ceiling violation
396 Ceiling_Violation
:= (Result
= EINVAL
);
397 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
401 (L
: access RTS_Lock
;
402 Global_Lock
: Boolean := False)
404 Result
: Interfaces
.C
.int
;
407 if not Single_Lock
or else Global_Lock
then
408 Result
:= pthread_mutex_lock
(L
);
409 pragma Assert
(Result
= 0);
413 procedure Write_Lock
(T
: Task_Id
) is
414 Result
: Interfaces
.C
.int
;
417 if not Single_Lock
then
418 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
419 pragma Assert
(Result
= 0);
427 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
429 Write_Lock
(L
, Ceiling_Violation
);
436 procedure Unlock
(L
: access Lock
) is
437 Result
: Interfaces
.C
.int
;
440 Result
:= pthread_mutex_unlock
(L
);
441 pragma Assert
(Result
= 0);
444 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
445 Result
: Interfaces
.C
.int
;
448 if not Single_Lock
or else Global_Lock
then
449 Result
:= pthread_mutex_unlock
(L
);
450 pragma Assert
(Result
= 0);
454 procedure Unlock
(T
: Task_Id
) is
455 Result
: Interfaces
.C
.int
;
458 if not Single_Lock
then
459 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
460 pragma Assert
(Result
= 0);
470 Reason
: System
.Tasking
.Task_States
)
472 pragma Warnings
(Off
, Reason
);
474 Result
: Interfaces
.C
.int
;
478 Result
:= pthread_cond_wait
479 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
481 Result
:= pthread_cond_wait
482 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
485 -- EINTR is not considered a failure.
487 pragma Assert
(Result
= 0 or else Result
= EINTR
);
494 -- This is for use within the run-time system, so abort is
495 -- assumed to be already deferred, and the caller should be
496 -- holding its own ATCB lock.
498 procedure Timed_Sleep
501 Mode
: ST
.Delay_Modes
;
502 Reason
: Task_States
;
503 Timedout
: out Boolean;
504 Yielded
: out Boolean)
506 pragma Warnings
(Off
, Reason
);
508 Check_Time
: constant Duration := Monotonic_Clock
;
511 Request
: aliased timespec
;
512 Result
: Interfaces
.C
.int
;
518 if Mode
= Relative
then
519 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
521 if Relative_Timed_Wait
then
522 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
526 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
528 if Relative_Timed_Wait
then
529 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
533 if Abs_Time
> Check_Time
then
534 if Relative_Timed_Wait
then
535 Request
:= To_Timespec
(Rel_Time
);
537 Request
:= To_Timespec
(Abs_Time
);
541 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
542 or else Self_ID
.Pending_Priority_Change
;
545 Result
:= pthread_cond_timedwait
546 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
550 Result
:= pthread_cond_timedwait
551 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
555 exit when Abs_Time
<= Monotonic_Clock
;
557 if Result
= 0 or Result
= EINTR
then
559 -- Somebody may have called Wakeup for us
565 pragma Assert
(Result
= ETIMEDOUT
);
574 -- This is for use in implementing delay statements, so
575 -- we assume the caller is abort-deferred but is holding
578 procedure Timed_Delay
581 Mode
: ST
.Delay_Modes
)
583 Check_Time
: constant Duration := Monotonic_Clock
;
586 Request
: aliased timespec
;
587 Result
: Interfaces
.C
.int
;
594 Write_Lock
(Self_ID
);
596 if Mode
= Relative
then
597 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
599 if Relative_Timed_Wait
then
600 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
604 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
606 if Relative_Timed_Wait
then
607 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
611 if Abs_Time
> Check_Time
then
612 if Relative_Timed_Wait
then
613 Request
:= To_Timespec
(Rel_Time
);
615 Request
:= To_Timespec
(Abs_Time
);
618 Self_ID
.Common
.State
:= Delay_Sleep
;
621 if Self_ID
.Pending_Priority_Change
then
622 Self_ID
.Pending_Priority_Change
:= False;
623 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
624 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
627 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
630 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
631 Single_RTS_Lock
'Access, Request
'Access);
633 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
634 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
637 exit when Abs_Time
<= Monotonic_Clock
;
639 pragma Assert
(Result
= 0
640 or else Result
= ETIMEDOUT
641 or else Result
= EINTR
);
644 Self_ID
.Common
.State
:= Runnable
;
653 Result
:= sched_yield
;
656 ---------------------
657 -- Monotonic_Clock --
658 ---------------------
660 function Monotonic_Clock
return Duration is
661 TS
: aliased timespec
;
662 Result
: Interfaces
.C
.int
;
664 Result
:= clock_gettime
665 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
666 pragma Assert
(Result
= 0);
667 return To_Duration
(TS
);
674 function RT_Resolution
return Duration is
683 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
684 pragma Warnings
(Off
, Reason
);
685 Result
: Interfaces
.C
.int
;
687 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
688 pragma Assert
(Result
= 0);
695 procedure Yield
(Do_Yield
: Boolean := True) is
696 Result
: Interfaces
.C
.int
;
697 pragma Unreferenced
(Result
);
700 Result
:= sched_yield
;
708 procedure Set_Priority
710 Prio
: System
.Any_Priority
;
711 Loss_Of_Inheritance
: Boolean := False)
713 pragma Warnings
(Off
, Loss_Of_Inheritance
);
715 Result
: Interfaces
.C
.int
;
716 Param
: aliased struct_sched_param
;
719 T
.Common
.Current_Priority
:= Prio
;
720 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
722 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
723 Result
:= pthread_setschedparam
724 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
726 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
727 Result
:= pthread_setschedparam
728 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
731 Result
:= pthread_setschedparam
732 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
735 pragma Assert
(Result
= 0);
742 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
744 return T
.Common
.Current_Priority
;
751 procedure Enter_Task
(Self_ID
: Task_Id
) is
753 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
754 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
756 Specific
.Set
(Self_ID
);
760 for J
in Known_Tasks
'Range loop
761 if Known_Tasks
(J
) = null then
762 Known_Tasks
(J
) := Self_ID
;
763 Self_ID
.Known_Tasks_Index
:= J
;
775 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
777 return new Ada_Task_Control_Block
(Entry_Num
);
784 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
786 -----------------------------
787 -- Register_Foreign_Thread --
788 -----------------------------
790 function Register_Foreign_Thread
return Task_Id
is
792 if Is_Valid_Task
then
795 return Register_Foreign_Thread
(pthread_self
);
797 end Register_Foreign_Thread
;
803 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
804 Mutex_Attr
: aliased pthread_mutexattr_t
;
805 Result
: Interfaces
.C
.int
;
806 Cond_Attr
: aliased pthread_condattr_t
;
809 -- Give the task a unique serial number.
811 Self_ID
.Serial_Number
:= Next_Serial_Number
;
812 Next_Serial_Number
:= Next_Serial_Number
+ 1;
813 pragma Assert
(Next_Serial_Number
/= 0);
815 if not Single_Lock
then
816 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
817 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
820 if Locking_Policy
= 'C' then
821 Result
:= pthread_mutexattr_setprotocol
822 (Mutex_Attr
'Access, PTHREAD_PRIO_PROTECT
);
823 pragma Assert
(Result
= 0);
825 Result
:= pthread_mutexattr_setprioceiling
827 Interfaces
.C
.int
(System
.Any_Priority
'Last));
828 pragma Assert
(Result
= 0);
830 elsif Locking_Policy
= 'I' then
831 Result
:= pthread_mutexattr_setprotocol
832 (Mutex_Attr
'Access, PTHREAD_PRIO_INHERIT
);
833 pragma Assert
(Result
= 0);
836 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
838 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
846 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
847 pragma Assert
(Result
= 0);
850 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
851 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
854 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
856 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
862 if not Single_Lock
then
863 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
864 pragma Assert
(Result
= 0);
870 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
871 pragma Assert
(Result
= 0);
878 procedure Create_Task
880 Wrapper
: System
.Address
;
881 Stack_Size
: System
.Parameters
.Size_Type
;
882 Priority
: System
.Any_Priority
;
883 Succeeded
: out Boolean)
885 Attributes
: aliased pthread_attr_t
;
886 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
887 Result
: Interfaces
.C
.int
;
889 function Thread_Body_Access
is new
890 Unchecked_Conversion
(System
.Address
, Thread_Body
);
892 use System
.Task_Info
;
895 if Stack_Size
= Unspecified_Size
then
896 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
898 elsif Stack_Size
< Minimum_Stack_Size
then
899 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
902 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
905 if Stack_Base_Available
then
906 -- If Stack Checking is supported then allocate 2 additional pages:
908 -- In the worst case, stack is allocated at something like
909 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
910 -- to be sure the effective stack size is greater than what
913 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
916 Result
:= pthread_attr_init
(Attributes
'Access);
917 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
924 Result
:= pthread_attr_setdetachstate
925 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
926 pragma Assert
(Result
= 0);
928 Result
:= pthread_attr_setstacksize
929 (Attributes
'Access, Adjusted_Stack_Size
);
930 pragma Assert
(Result
= 0);
932 if T
.Common
.Task_Info
/= Default_Scope
then
934 -- We are assuming that Scope_Type has the same values than the
935 -- corresponding C macros
937 Result
:= pthread_attr_setscope
938 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
939 pragma Assert
(Result
= 0);
942 -- Since the initial signal mask of a thread is inherited from the
943 -- creator, and the Environment task has all its signals masked, we
944 -- do not need to manipulate caller's signal mask at this point.
945 -- All tasks in RTS will have All_Tasks_Mask initially.
947 Result
:= pthread_create
948 (T
.Common
.LL
.Thread
'Access,
950 Thread_Body_Access
(Wrapper
),
952 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
954 Succeeded
:= Result
= 0;
956 Result
:= pthread_attr_destroy
(Attributes
'Access);
957 pragma Assert
(Result
= 0);
959 Set_Priority
(T
, Priority
);
966 procedure Finalize_TCB
(T
: Task_Id
) is
967 Result
: Interfaces
.C
.int
;
969 Is_Self
: constant Boolean := T
= Self
;
971 procedure Free
is new
972 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
975 if not Single_Lock
then
976 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
977 pragma Assert
(Result
= 0);
980 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
981 pragma Assert
(Result
= 0);
983 if T
.Known_Tasks_Index
/= -1 then
984 Known_Tasks
(T
.Known_Tasks_Index
) := null;
998 procedure Exit_Task
is
1000 -- Mark this task as unknown, so that if Self is called, it won't
1001 -- return a dangling pointer.
1003 Specific
.Set
(null);
1010 procedure Abort_Task
(T
: Task_Id
) is
1011 Result
: Interfaces
.C
.int
;
1013 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
1014 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1015 pragma Assert
(Result
= 0);
1022 procedure Initialize
(S
: in out Suspension_Object
) is
1023 Mutex_Attr
: aliased pthread_mutexattr_t
;
1024 Cond_Attr
: aliased pthread_condattr_t
;
1025 Result
: Interfaces
.C
.int
;
1027 -- Initialize internal state. It is always initialized to False (ARM
1033 -- Initialize internal mutex
1035 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1036 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1038 if Result
= ENOMEM
then
1039 raise Storage_Error
;
1042 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1043 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1045 if Result
= ENOMEM
then
1046 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1047 pragma Assert
(Result
= 0);
1049 raise Storage_Error
;
1052 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1053 pragma Assert
(Result
= 0);
1055 -- Initialize internal condition variable
1057 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1058 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1061 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1062 pragma Assert
(Result
= 0);
1064 if Result
= ENOMEM
then
1065 raise Storage_Error
;
1069 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1070 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1073 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1074 pragma Assert
(Result
= 0);
1076 if Result
= ENOMEM
then
1077 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1078 pragma Assert
(Result
= 0);
1080 raise Storage_Error
;
1084 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1085 pragma Assert
(Result
= 0);
1092 procedure Finalize
(S
: in out Suspension_Object
) is
1093 Result
: Interfaces
.C
.int
;
1095 -- Destroy internal mutex
1097 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1098 pragma Assert
(Result
= 0);
1100 -- Destroy internal condition variable
1102 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1103 pragma Assert
(Result
= 0);
1110 function Current_State
(S
: Suspension_Object
) return Boolean is
1112 -- We do not want to use lock on this read operation. State is marked
1113 -- as Atomic so that we ensure that the value retrieved is correct.
1122 procedure Set_False
(S
: in out Suspension_Object
) is
1123 Result
: Interfaces
.C
.int
;
1125 Result
:= pthread_mutex_lock
(S
.L
'Access);
1126 pragma Assert
(Result
= 0);
1130 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1131 pragma Assert
(Result
= 0);
1138 procedure Set_True
(S
: in out Suspension_Object
) is
1139 Result
: Interfaces
.C
.int
;
1141 Result
:= pthread_mutex_lock
(S
.L
'Access);
1142 pragma Assert
(Result
= 0);
1144 -- If there is already a task waiting on this suspension object then
1145 -- we resume it, leaving the state of the suspension object to False,
1146 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1147 -- the state to True.
1153 Result
:= pthread_cond_signal
(S
.CV
'Access);
1154 pragma Assert
(Result
= 0);
1159 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1160 pragma Assert
(Result
= 0);
1163 ------------------------
1164 -- Suspend_Until_True --
1165 ------------------------
1167 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1168 Result
: Interfaces
.C
.int
;
1170 Result
:= pthread_mutex_lock
(S
.L
'Access);
1171 pragma Assert
(Result
= 0);
1174 -- Program_Error must be raised upon calling Suspend_Until_True
1175 -- if another task is already waiting on that suspension object
1176 -- (ARM D.10 par. 10).
1178 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1179 pragma Assert
(Result
= 0);
1181 raise Program_Error
;
1183 -- Suspend the task if the state is False. Otherwise, the task
1184 -- continues its execution, and the state of the suspension object
1185 -- is set to False (ARM D.10 par. 9).
1191 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1195 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1196 pragma Assert
(Result
= 0);
1197 end Suspend_Until_True
;
1205 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1206 pragma Warnings
(Off
, Self_ID
);
1211 --------------------
1212 -- Check_No_Locks --
1213 --------------------
1215 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1216 pragma Warnings
(Off
, Self_ID
);
1221 ----------------------
1222 -- Environment_Task --
1223 ----------------------
1225 function Environment_Task
return Task_Id
is
1227 return Environment_Task_Id
;
1228 end Environment_Task
;
1234 procedure Lock_RTS
is
1236 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1243 procedure Unlock_RTS
is
1245 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1252 function Suspend_Task
1254 Thread_Self
: Thread_Id
) return Boolean
1256 pragma Warnings
(Off
, T
);
1257 pragma Warnings
(Off
, Thread_Self
);
1266 function Resume_Task
1268 Thread_Self
: Thread_Id
) return Boolean
1270 pragma Warnings
(Off
, T
);
1271 pragma Warnings
(Off
, Thread_Self
);
1280 procedure Initialize
(Environment_Task
: Task_Id
) is
1281 act
: aliased struct_sigaction
;
1282 old_act
: aliased struct_sigaction
;
1283 Tmp_Set
: aliased sigset_t
;
1284 Result
: Interfaces
.C
.int
;
1287 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1288 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1289 -- Get interrupt state. Defined in a-init.c
1290 -- The input argument is the interrupt number,
1291 -- and the result is one of the following:
1293 Default
: constant Character := 's';
1294 -- 'n' this interrupt not set by any Interrupt_State pragma
1295 -- 'u' Interrupt_State pragma set state to User
1296 -- 'r' Interrupt_State pragma set state to Runtime
1297 -- 's' Interrupt_State pragma set state to System (use "default"
1301 Environment_Task_Id
:= Environment_Task
;
1303 Interrupt_Management
.Initialize
;
1305 -- Prepare the set of signals that should unblocked in all tasks
1307 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1308 pragma Assert
(Result
= 0);
1310 for J
in Interrupt_Management
.Interrupt_ID
loop
1311 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1312 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1313 pragma Assert
(Result
= 0);
1317 -- Initialize the lock used to synchronize chain of all ATCBs.
1319 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1321 Specific
.Initialize
(Environment_Task
);
1323 Enter_Task
(Environment_Task
);
1325 -- Install the abort-signal handler
1327 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
1331 act
.sa_handler
:= Abort_Handler
'Address;
1333 Result
:= sigemptyset
(Tmp_Set
'Access);
1334 pragma Assert
(Result
= 0);
1335 act
.sa_mask
:= Tmp_Set
;
1339 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1340 act
'Unchecked_Access,
1341 old_act
'Unchecked_Access);
1342 pragma Assert
(Result
= 0);
1346 end System
.Task_Primitives
.Operations
;