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-2006, 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 Unchecked_Conversion
;
68 with Unchecked_Deallocation
;
70 package body System
.Task_Primitives
.Operations
is
72 use System
.Tasking
.Debug
;
75 use System
.OS_Interface
;
76 use System
.Parameters
;
77 use System
.OS_Primitives
;
83 -- The followings are logically constants, but need to be initialized
86 Single_RTS_Lock
: aliased RTS_Lock
;
87 -- This is a lock to allow only one thread of control in the RTS at
88 -- a time; it is used to execute in mutual exclusion from all other tasks.
89 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
91 ATCB_Key
: aliased pthread_key_t
;
92 -- Key used to find the Ada Task_Id associated with a thread
94 Environment_Task_Id
: Task_Id
;
95 -- A variable to hold Task_Id for the environment task.
97 Locking_Policy
: Character;
98 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
99 -- Value of the pragma Locking_Policy:
100 -- 'C' for Ceiling_Locking
101 -- 'I' for Inherit_Locking
104 Unblocked_Signal_Mask
: aliased sigset_t
;
105 -- The set of signals that should unblocked in all tasks
107 -- The followings are internal configuration constants needed.
109 Next_Serial_Number
: Task_Serial_Number
:= 100;
110 -- We start at 100, to reserve some special values for
111 -- using in error checking.
113 Time_Slice_Val
: Integer;
114 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
116 Dispatching_Policy
: Character;
117 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
119 Foreign_Task_Elaborated
: aliased Boolean := True;
120 -- Used to identified fake tasks (i.e., non-Ada Threads).
128 procedure Initialize
(Environment_Task
: Task_Id
);
129 pragma Inline
(Initialize
);
130 -- Initialize various data needed by this package.
132 function Is_Valid_Task
return Boolean;
133 pragma Inline
(Is_Valid_Task
);
134 -- Does executing thread have a TCB?
136 procedure Set
(Self_Id
: Task_Id
);
138 -- Set the self id for the current task.
140 function Self
return Task_Id
;
141 pragma Inline
(Self
);
142 -- Return a pointer to the Ada Task Control Block of the calling task.
146 package body Specific
is separate;
147 -- The body of this package is target specific.
149 ---------------------------------
150 -- Support for foreign threads --
151 ---------------------------------
153 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
154 -- Allocate and Initialize a new ATCB for the current Thread.
156 function Register_Foreign_Thread
157 (Thread
: Thread_Id
) return Task_Id
is separate;
159 -----------------------
160 -- Local Subprograms --
161 -----------------------
163 procedure Abort_Handler
(Sig
: Signal
);
164 -- Signal handler used to implement asynchronous abort.
165 -- See also comment before body, below.
167 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
173 -- Target-dependent binding of inter-thread Abort signal to
174 -- the raising of the Abort_Signal exception.
176 -- The technical issues and alternatives here are essentially
177 -- the same as for raising exceptions in response to other
178 -- signals (e.g. Storage_Error). See code and comments in
179 -- the package body System.Interrupt_Management.
181 -- Some implementations may not allow an exception to be propagated
182 -- out of a handler, and others might leave the signal or
183 -- interrupt that invoked this handler masked after the exceptional
184 -- return to the application code.
186 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
187 -- On most UNIX systems, this will allow transfer out of a signal handler,
188 -- which is usually the only mechanism available for implementing
189 -- asynchronous handlers of this kind. However, some
190 -- systems do not restore the signal mask on longjmp(), leaving the
191 -- abort signal masked.
193 procedure Abort_Handler
(Sig
: Signal
) is
194 pragma Warnings
(Off
, Sig
);
196 T
: constant Task_Id
:= Self
;
197 Result
: Interfaces
.C
.int
;
198 Old_Set
: aliased sigset_t
;
201 -- It is not safe to raise an exception when using ZCX and the GCC
202 -- exception handling mechanism.
204 if ZCX_By_Default
and then GCC_ZCX_Support
then
208 if T
.Deferral_Level
= 0
209 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
214 -- Make sure signals used for RTS internal purpose are unmasked
216 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
217 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
218 pragma Assert
(Result
= 0);
220 raise Standard
'Abort_Signal;
228 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
229 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
230 Guard_Page_Address
: Address
;
232 Res
: Interfaces
.C
.int
;
235 if Stack_Base_Available
then
237 -- Compute the guard page address
239 Guard_Page_Address
:=
240 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
243 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
245 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
248 pragma Assert
(Res
= 0);
256 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
258 return T
.Common
.LL
.Thread
;
265 function Self
return Task_Id
renames Specific
.Self
;
267 ---------------------
268 -- Initialize_Lock --
269 ---------------------
271 -- Note: mutexes and cond_variables needed per-task basis are
272 -- initialized in Intialize_TCB and the Storage_Error is
273 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
274 -- used in RTS is initialized before any status change of RTS.
275 -- Therefore rasing Storage_Error in the following routines
276 -- should be able to be handled safely.
278 procedure Initialize_Lock
279 (Prio
: System
.Any_Priority
;
282 Attributes
: aliased pthread_mutexattr_t
;
283 Result
: Interfaces
.C
.int
;
286 Result
:= pthread_mutexattr_init
(Attributes
'Access);
287 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
289 if Result
= ENOMEM
then
293 if Locking_Policy
= 'C' then
294 Result
:= pthread_mutexattr_setprotocol
295 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
296 pragma Assert
(Result
= 0);
298 Result
:= pthread_mutexattr_setprioceiling
299 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
300 pragma Assert
(Result
= 0);
302 elsif Locking_Policy
= 'I' then
303 Result
:= pthread_mutexattr_setprotocol
304 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
305 pragma Assert
(Result
= 0);
308 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
309 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
311 if Result
= ENOMEM
then
312 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
316 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
317 pragma Assert
(Result
= 0);
320 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
321 pragma Warnings
(Off
, Level
);
323 Attributes
: aliased pthread_mutexattr_t
;
324 Result
: Interfaces
.C
.int
;
327 Result
:= pthread_mutexattr_init
(Attributes
'Access);
328 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
330 if Result
= ENOMEM
then
334 if Locking_Policy
= 'C' then
335 Result
:= pthread_mutexattr_setprotocol
336 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
337 pragma Assert
(Result
= 0);
339 Result
:= pthread_mutexattr_setprioceiling
340 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
341 pragma Assert
(Result
= 0);
343 elsif Locking_Policy
= 'I' then
344 Result
:= pthread_mutexattr_setprotocol
345 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
346 pragma Assert
(Result
= 0);
349 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
350 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
352 if Result
= ENOMEM
then
353 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
357 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
358 pragma Assert
(Result
= 0);
365 procedure Finalize_Lock
(L
: access Lock
) is
366 Result
: Interfaces
.C
.int
;
369 Result
:= pthread_mutex_destroy
(L
);
370 pragma Assert
(Result
= 0);
373 procedure Finalize_Lock
(L
: access RTS_Lock
) is
374 Result
: Interfaces
.C
.int
;
377 Result
:= pthread_mutex_destroy
(L
);
378 pragma Assert
(Result
= 0);
385 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
386 Result
: Interfaces
.C
.int
;
389 Result
:= pthread_mutex_lock
(L
);
391 -- Assume that the cause of EINVAL is a priority ceiling violation
393 Ceiling_Violation
:= (Result
= EINVAL
);
394 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
398 (L
: access RTS_Lock
;
399 Global_Lock
: Boolean := False)
401 Result
: Interfaces
.C
.int
;
404 if not Single_Lock
or else Global_Lock
then
405 Result
:= pthread_mutex_lock
(L
);
406 pragma Assert
(Result
= 0);
410 procedure Write_Lock
(T
: Task_Id
) is
411 Result
: Interfaces
.C
.int
;
414 if not Single_Lock
then
415 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
416 pragma Assert
(Result
= 0);
424 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
426 Write_Lock
(L
, Ceiling_Violation
);
433 procedure Unlock
(L
: access Lock
) is
434 Result
: Interfaces
.C
.int
;
437 Result
:= pthread_mutex_unlock
(L
);
438 pragma Assert
(Result
= 0);
441 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
442 Result
: Interfaces
.C
.int
;
445 if not Single_Lock
or else Global_Lock
then
446 Result
:= pthread_mutex_unlock
(L
);
447 pragma Assert
(Result
= 0);
451 procedure Unlock
(T
: Task_Id
) is
452 Result
: Interfaces
.C
.int
;
455 if not Single_Lock
then
456 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
457 pragma Assert
(Result
= 0);
467 Reason
: System
.Tasking
.Task_States
)
469 pragma Warnings
(Off
, Reason
);
471 Result
: Interfaces
.C
.int
;
475 Result
:= pthread_cond_wait
476 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
478 Result
:= pthread_cond_wait
479 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
482 -- EINTR is not considered a failure.
484 pragma Assert
(Result
= 0 or else Result
= EINTR
);
491 -- This is for use within the run-time system, so abort is
492 -- assumed to be already deferred, and the caller should be
493 -- holding its own ATCB lock.
495 procedure Timed_Sleep
498 Mode
: ST
.Delay_Modes
;
499 Reason
: Task_States
;
500 Timedout
: out Boolean;
501 Yielded
: out Boolean)
503 pragma Warnings
(Off
, Reason
);
505 Check_Time
: constant Duration := Monotonic_Clock
;
508 Request
: aliased timespec
;
509 Result
: Interfaces
.C
.int
;
515 if Mode
= Relative
then
516 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
518 if Relative_Timed_Wait
then
519 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
523 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
525 if Relative_Timed_Wait
then
526 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
530 if Abs_Time
> Check_Time
then
531 if Relative_Timed_Wait
then
532 Request
:= To_Timespec
(Rel_Time
);
534 Request
:= To_Timespec
(Abs_Time
);
538 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
539 or else Self_ID
.Pending_Priority_Change
;
542 Result
:= pthread_cond_timedwait
543 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
547 Result
:= pthread_cond_timedwait
548 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
552 exit when Abs_Time
<= Monotonic_Clock
;
554 if Result
= 0 or Result
= EINTR
then
556 -- Somebody may have called Wakeup for us
562 pragma Assert
(Result
= ETIMEDOUT
);
571 -- This is for use in implementing delay statements, so
572 -- we assume the caller is abort-deferred but is holding
575 procedure Timed_Delay
578 Mode
: ST
.Delay_Modes
)
580 Check_Time
: constant Duration := Monotonic_Clock
;
583 Request
: aliased timespec
;
584 Result
: Interfaces
.C
.int
;
591 Write_Lock
(Self_ID
);
593 if Mode
= Relative
then
594 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
596 if Relative_Timed_Wait
then
597 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
601 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
603 if Relative_Timed_Wait
then
604 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
608 if Abs_Time
> Check_Time
then
609 if Relative_Timed_Wait
then
610 Request
:= To_Timespec
(Rel_Time
);
612 Request
:= To_Timespec
(Abs_Time
);
615 Self_ID
.Common
.State
:= Delay_Sleep
;
618 if Self_ID
.Pending_Priority_Change
then
619 Self_ID
.Pending_Priority_Change
:= False;
620 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
621 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
624 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
627 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
628 Single_RTS_Lock
'Access, Request
'Access);
630 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
631 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
634 exit when Abs_Time
<= Monotonic_Clock
;
636 pragma Assert
(Result
= 0
637 or else Result
= ETIMEDOUT
638 or else Result
= EINTR
);
641 Self_ID
.Common
.State
:= Runnable
;
650 Result
:= sched_yield
;
653 ---------------------
654 -- Monotonic_Clock --
655 ---------------------
657 function Monotonic_Clock
return Duration is
658 TS
: aliased timespec
;
659 Result
: Interfaces
.C
.int
;
661 Result
:= clock_gettime
662 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
663 pragma Assert
(Result
= 0);
664 return To_Duration
(TS
);
671 function RT_Resolution
return Duration is
680 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
681 pragma Warnings
(Off
, Reason
);
682 Result
: Interfaces
.C
.int
;
684 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
685 pragma Assert
(Result
= 0);
692 procedure Yield
(Do_Yield
: Boolean := True) is
693 Result
: Interfaces
.C
.int
;
694 pragma Unreferenced
(Result
);
697 Result
:= sched_yield
;
705 procedure Set_Priority
707 Prio
: System
.Any_Priority
;
708 Loss_Of_Inheritance
: Boolean := False)
710 pragma Warnings
(Off
, Loss_Of_Inheritance
);
712 Result
: Interfaces
.C
.int
;
713 Param
: aliased struct_sched_param
;
716 T
.Common
.Current_Priority
:= Prio
;
717 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
719 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
720 Result
:= pthread_setschedparam
721 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
723 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
724 Result
:= pthread_setschedparam
725 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
728 Result
:= pthread_setschedparam
729 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
732 pragma Assert
(Result
= 0);
739 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
741 return T
.Common
.Current_Priority
;
748 procedure Enter_Task
(Self_ID
: Task_Id
) is
750 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
751 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
753 Specific
.Set
(Self_ID
);
757 for J
in Known_Tasks
'Range loop
758 if Known_Tasks
(J
) = null then
759 Known_Tasks
(J
) := Self_ID
;
760 Self_ID
.Known_Tasks_Index
:= J
;
772 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
774 return new Ada_Task_Control_Block
(Entry_Num
);
781 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
783 -----------------------------
784 -- Register_Foreign_Thread --
785 -----------------------------
787 function Register_Foreign_Thread
return Task_Id
is
789 if Is_Valid_Task
then
792 return Register_Foreign_Thread
(pthread_self
);
794 end Register_Foreign_Thread
;
800 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
801 Mutex_Attr
: aliased pthread_mutexattr_t
;
802 Result
: Interfaces
.C
.int
;
803 Cond_Attr
: aliased pthread_condattr_t
;
806 -- Give the task a unique serial number.
808 Self_ID
.Serial_Number
:= Next_Serial_Number
;
809 Next_Serial_Number
:= Next_Serial_Number
+ 1;
810 pragma Assert
(Next_Serial_Number
/= 0);
812 if not Single_Lock
then
813 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
814 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
817 if Locking_Policy
= 'C' then
818 Result
:= pthread_mutexattr_setprotocol
819 (Mutex_Attr
'Access, PTHREAD_PRIO_PROTECT
);
820 pragma Assert
(Result
= 0);
822 Result
:= pthread_mutexattr_setprioceiling
824 Interfaces
.C
.int
(System
.Any_Priority
'Last));
825 pragma Assert
(Result
= 0);
827 elsif Locking_Policy
= 'I' then
828 Result
:= pthread_mutexattr_setprotocol
829 (Mutex_Attr
'Access, PTHREAD_PRIO_INHERIT
);
830 pragma Assert
(Result
= 0);
833 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
835 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
843 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
844 pragma Assert
(Result
= 0);
847 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
848 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
851 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
853 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
859 if not Single_Lock
then
860 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
861 pragma Assert
(Result
= 0);
867 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
868 pragma Assert
(Result
= 0);
875 procedure Create_Task
877 Wrapper
: System
.Address
;
878 Stack_Size
: System
.Parameters
.Size_Type
;
879 Priority
: System
.Any_Priority
;
880 Succeeded
: out Boolean)
882 Attributes
: aliased pthread_attr_t
;
883 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
884 Result
: Interfaces
.C
.int
;
886 function Thread_Body_Access
is new
887 Unchecked_Conversion
(System
.Address
, Thread_Body
);
889 use System
.Task_Info
;
892 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
894 if Stack_Base_Available
then
895 -- If Stack Checking is supported then allocate 2 additional pages:
897 -- In the worst case, stack is allocated at something like
898 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
899 -- to be sure the effective stack size is greater than what
902 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
905 Result
:= pthread_attr_init
(Attributes
'Access);
906 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
913 Result
:= pthread_attr_setdetachstate
914 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
915 pragma Assert
(Result
= 0);
917 Result
:= pthread_attr_setstacksize
918 (Attributes
'Access, Adjusted_Stack_Size
);
919 pragma Assert
(Result
= 0);
921 if T
.Common
.Task_Info
/= Default_Scope
then
923 -- We are assuming that Scope_Type has the same values than the
924 -- corresponding C macros
926 Result
:= pthread_attr_setscope
927 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
928 pragma Assert
(Result
= 0);
931 -- Since the initial signal mask of a thread is inherited from the
932 -- creator, and the Environment task has all its signals masked, we
933 -- do not need to manipulate caller's signal mask at this point.
934 -- All tasks in RTS will have All_Tasks_Mask initially.
936 Result
:= pthread_create
937 (T
.Common
.LL
.Thread
'Access,
939 Thread_Body_Access
(Wrapper
),
941 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
943 Succeeded
:= Result
= 0;
945 Result
:= pthread_attr_destroy
(Attributes
'Access);
946 pragma Assert
(Result
= 0);
948 Set_Priority
(T
, Priority
);
955 procedure Finalize_TCB
(T
: Task_Id
) is
956 Result
: Interfaces
.C
.int
;
958 Is_Self
: constant Boolean := T
= Self
;
960 procedure Free
is new
961 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
964 if not Single_Lock
then
965 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
966 pragma Assert
(Result
= 0);
969 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
970 pragma Assert
(Result
= 0);
972 if T
.Known_Tasks_Index
/= -1 then
973 Known_Tasks
(T
.Known_Tasks_Index
) := null;
987 procedure Exit_Task
is
989 -- Mark this task as unknown, so that if Self is called, it won't
990 -- return a dangling pointer.
999 procedure Abort_Task
(T
: Task_Id
) is
1000 Result
: Interfaces
.C
.int
;
1002 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
1003 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1004 pragma Assert
(Result
= 0);
1011 procedure Initialize
(S
: in out Suspension_Object
) is
1012 Mutex_Attr
: aliased pthread_mutexattr_t
;
1013 Cond_Attr
: aliased pthread_condattr_t
;
1014 Result
: Interfaces
.C
.int
;
1016 -- Initialize internal state. It is always initialized to False (ARM
1022 -- Initialize internal mutex
1024 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1025 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1027 if Result
= ENOMEM
then
1028 raise Storage_Error
;
1031 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1032 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1034 if Result
= ENOMEM
then
1035 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1036 pragma Assert
(Result
= 0);
1038 raise Storage_Error
;
1041 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1042 pragma Assert
(Result
= 0);
1044 -- Initialize internal condition variable
1046 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1047 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1050 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1051 pragma Assert
(Result
= 0);
1053 if Result
= ENOMEM
then
1054 raise Storage_Error
;
1058 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1059 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1062 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1063 pragma Assert
(Result
= 0);
1065 if Result
= ENOMEM
then
1066 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1067 pragma Assert
(Result
= 0);
1069 raise Storage_Error
;
1073 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1074 pragma Assert
(Result
= 0);
1081 procedure Finalize
(S
: in out Suspension_Object
) is
1082 Result
: Interfaces
.C
.int
;
1084 -- Destroy internal mutex
1086 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1087 pragma Assert
(Result
= 0);
1089 -- Destroy internal condition variable
1091 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1092 pragma Assert
(Result
= 0);
1099 function Current_State
(S
: Suspension_Object
) return Boolean is
1101 -- We do not want to use lock on this read operation. State is marked
1102 -- as Atomic so that we ensure that the value retrieved is correct.
1111 procedure Set_False
(S
: in out Suspension_Object
) is
1112 Result
: Interfaces
.C
.int
;
1114 Result
:= pthread_mutex_lock
(S
.L
'Access);
1115 pragma Assert
(Result
= 0);
1119 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1120 pragma Assert
(Result
= 0);
1127 procedure Set_True
(S
: in out Suspension_Object
) is
1128 Result
: Interfaces
.C
.int
;
1130 Result
:= pthread_mutex_lock
(S
.L
'Access);
1131 pragma Assert
(Result
= 0);
1133 -- If there is already a task waiting on this suspension object then
1134 -- we resume it, leaving the state of the suspension object to False,
1135 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1136 -- the state to True.
1142 Result
:= pthread_cond_signal
(S
.CV
'Access);
1143 pragma Assert
(Result
= 0);
1148 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1149 pragma Assert
(Result
= 0);
1152 ------------------------
1153 -- Suspend_Until_True --
1154 ------------------------
1156 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1157 Result
: Interfaces
.C
.int
;
1159 Result
:= pthread_mutex_lock
(S
.L
'Access);
1160 pragma Assert
(Result
= 0);
1163 -- Program_Error must be raised upon calling Suspend_Until_True
1164 -- if another task is already waiting on that suspension object
1165 -- (ARM D.10 par. 10).
1167 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1168 pragma Assert
(Result
= 0);
1170 raise Program_Error
;
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1180 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1184 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1185 pragma Assert
(Result
= 0);
1186 end Suspend_Until_True
;
1194 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1195 pragma Warnings
(Off
, Self_ID
);
1200 --------------------
1201 -- Check_No_Locks --
1202 --------------------
1204 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1205 pragma Warnings
(Off
, Self_ID
);
1210 ----------------------
1211 -- Environment_Task --
1212 ----------------------
1214 function Environment_Task
return Task_Id
is
1216 return Environment_Task_Id
;
1217 end Environment_Task
;
1223 procedure Lock_RTS
is
1225 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1232 procedure Unlock_RTS
is
1234 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1241 function Suspend_Task
1243 Thread_Self
: Thread_Id
) return Boolean
1245 pragma Warnings
(Off
, T
);
1246 pragma Warnings
(Off
, Thread_Self
);
1255 function Resume_Task
1257 Thread_Self
: Thread_Id
) return Boolean
1259 pragma Warnings
(Off
, T
);
1260 pragma Warnings
(Off
, Thread_Self
);
1269 procedure Initialize
(Environment_Task
: Task_Id
) is
1270 act
: aliased struct_sigaction
;
1271 old_act
: aliased struct_sigaction
;
1272 Tmp_Set
: aliased sigset_t
;
1273 Result
: Interfaces
.C
.int
;
1276 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1277 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1278 -- Get interrupt state. Defined in a-init.c
1279 -- The input argument is the interrupt number,
1280 -- and the result is one of the following:
1282 Default
: constant Character := 's';
1283 -- 'n' this interrupt not set by any Interrupt_State pragma
1284 -- 'u' Interrupt_State pragma set state to User
1285 -- 'r' Interrupt_State pragma set state to Runtime
1286 -- 's' Interrupt_State pragma set state to System (use "default"
1290 Environment_Task_Id
:= Environment_Task
;
1292 Interrupt_Management
.Initialize
;
1294 -- Prepare the set of signals that should unblocked in all tasks
1296 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1297 pragma Assert
(Result
= 0);
1299 for J
in Interrupt_Management
.Interrupt_ID
loop
1300 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1301 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1302 pragma Assert
(Result
= 0);
1306 -- Initialize the lock used to synchronize chain of all ATCBs.
1308 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1310 Specific
.Initialize
(Environment_Task
);
1312 Enter_Task
(Environment_Task
);
1314 -- Install the abort-signal handler
1316 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
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);
1335 end System
.Task_Primitives
.Operations
;