1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2004, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
.Task_Info
;
53 -- used for Task_Info_Type
59 with System
.Interrupt_Management
;
60 -- used for Keep_Unmasked
61 -- Abort_Task_Interrupt
64 with System
.Interrupt_Management
.Operations
;
65 -- used for Set_Interrupt_Mask
67 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
69 with System
.Parameters
;
73 -- used for Ada_Task_Control_Block
76 with System
.Soft_Links
;
77 -- used for Defer/Undefer_Abort
79 -- Note that we do not use System.Tasking.Initialization directly since
80 -- this is a higher level package that we shouldn't depend on. For example
81 -- when using the restricted run time, it is replaced by
82 -- System.Tasking.Restricted.Stages.
84 with System
.OS_Primitives
;
85 -- used for Delay_Modes
87 with Unchecked_Conversion
;
88 with Unchecked_Deallocation
;
90 package body System
.Task_Primitives
.Operations
is
92 use System
.Tasking
.Debug
;
95 use System
.OS_Interface
;
96 use System
.Parameters
;
97 use System
.OS_Primitives
;
99 package SSL
renames System
.Soft_Links
;
105 -- The followings are logically constants, but need to be initialized
108 Single_RTS_Lock
: aliased RTS_Lock
;
109 -- This is a lock to allow only one thread of control in the RTS at
110 -- a time; it is used to execute in mutual exclusion from all other tasks.
111 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
113 ATCB_Key
: aliased pthread_key_t
;
114 -- Key used to find the Ada Task_Id associated with a thread
116 Environment_Task_Id
: Task_Id
;
117 -- A variable to hold Task_Id for the environment task.
119 Locking_Policy
: Character;
120 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
121 -- Value of the pragma Locking_Policy:
122 -- 'C' for Ceiling_Locking
123 -- 'I' for Inherit_Locking
126 Unblocked_Signal_Mask
: aliased sigset_t
;
127 -- The set of signals that should unblocked in all tasks
129 -- The followings are internal configuration constants needed.
131 Next_Serial_Number
: Task_Serial_Number
:= 100;
132 -- We start at 100, to reserve some special values for
133 -- using in error checking.
135 Time_Slice_Val
: Integer;
136 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
138 Dispatching_Policy
: Character;
139 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
141 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
142 -- Indicates whether FIFO_Within_Priorities is set.
144 Foreign_Task_Elaborated
: aliased Boolean := True;
145 -- Used to identified fake tasks (i.e., non-Ada Threads).
153 procedure Initialize
(Environment_Task
: Task_Id
);
154 pragma Inline
(Initialize
);
155 -- Initialize various data needed by this package.
157 function Is_Valid_Task
return Boolean;
158 pragma Inline
(Is_Valid_Task
);
159 -- Does executing thread have a TCB?
161 procedure Set
(Self_Id
: Task_Id
);
163 -- Set the self id for the current task.
165 function Self
return Task_Id
;
166 pragma Inline
(Self
);
167 -- Return a pointer to the Ada Task Control Block of the calling task.
171 package body Specific
is separate;
172 -- The body of this package is target specific.
174 ---------------------------------
175 -- Support for foreign threads --
176 ---------------------------------
178 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
179 -- Allocate and Initialize a new ATCB for the current Thread.
181 function Register_Foreign_Thread
182 (Thread
: Thread_Id
) return Task_Id
is separate;
184 -----------------------
185 -- Local Subprograms --
186 -----------------------
188 procedure Abort_Handler
(Sig
: Signal
);
189 -- Signal handler used to implement asynchronous abort.
190 -- See also comment before body, below.
192 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
198 -- Target-dependent binding of inter-thread Abort signal to
199 -- the raising of the Abort_Signal exception.
201 -- The technical issues and alternatives here are essentially
202 -- the same as for raising exceptions in response to other
203 -- signals (e.g. Storage_Error). See code and comments in
204 -- the package body System.Interrupt_Management.
206 -- Some implementations may not allow an exception to be propagated
207 -- out of a handler, and others might leave the signal or
208 -- interrupt that invoked this handler masked after the exceptional
209 -- return to the application code.
211 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
212 -- On most UNIX systems, this will allow transfer out of a signal handler,
213 -- which is usually the only mechanism available for implementing
214 -- asynchronous handlers of this kind. However, some
215 -- systems do not restore the signal mask on longjmp(), leaving the
216 -- abort signal masked.
218 procedure Abort_Handler
(Sig
: Signal
) is
219 pragma Warnings
(Off
, Sig
);
221 T
: constant Task_Id
:= Self
;
222 Result
: Interfaces
.C
.int
;
223 Old_Set
: aliased sigset_t
;
226 -- It is not safe to raise an exception when using ZCX and the GCC
227 -- exception handling mechanism.
229 if ZCX_By_Default
and then GCC_ZCX_Support
then
233 if T
.Deferral_Level
= 0
234 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
239 -- Make sure signals used for RTS internal purpose are unmasked
241 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
242 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
243 pragma Assert
(Result
= 0);
245 raise Standard
'Abort_Signal;
253 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
254 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
255 Guard_Page_Address
: Address
;
257 Res
: Interfaces
.C
.int
;
260 if Stack_Base_Available
then
262 -- Compute the guard page address
264 Guard_Page_Address
:=
265 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
268 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
270 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
273 pragma Assert
(Res
= 0);
281 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
283 return T
.Common
.LL
.Thread
;
290 function Self
return Task_Id
renames Specific
.Self
;
292 ---------------------
293 -- Initialize_Lock --
294 ---------------------
296 -- Note: mutexes and cond_variables needed per-task basis are
297 -- initialized in Intialize_TCB and the Storage_Error is
298 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
299 -- used in RTS is initialized before any status change of RTS.
300 -- Therefore rasing Storage_Error in the following routines
301 -- should be able to be handled safely.
303 procedure Initialize_Lock
304 (Prio
: System
.Any_Priority
;
307 Attributes
: aliased pthread_mutexattr_t
;
308 Result
: Interfaces
.C
.int
;
311 Result
:= pthread_mutexattr_init
(Attributes
'Access);
312 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
314 if Result
= ENOMEM
then
318 if Locking_Policy
= 'C' then
319 Result
:= pthread_mutexattr_setprotocol
320 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
321 pragma Assert
(Result
= 0);
323 Result
:= pthread_mutexattr_setprioceiling
324 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
325 pragma Assert
(Result
= 0);
327 elsif Locking_Policy
= 'I' then
328 Result
:= pthread_mutexattr_setprotocol
329 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
330 pragma Assert
(Result
= 0);
333 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
334 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
336 if Result
= ENOMEM
then
340 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
341 pragma Assert
(Result
= 0);
344 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
345 pragma Warnings
(Off
, Level
);
347 Attributes
: aliased pthread_mutexattr_t
;
348 Result
: Interfaces
.C
.int
;
351 Result
:= pthread_mutexattr_init
(Attributes
'Access);
352 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
354 if Result
= ENOMEM
then
358 if Locking_Policy
= 'C' then
359 Result
:= pthread_mutexattr_setprotocol
360 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
361 pragma Assert
(Result
= 0);
363 Result
:= pthread_mutexattr_setprioceiling
364 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
365 pragma Assert
(Result
= 0);
367 elsif Locking_Policy
= 'I' then
368 Result
:= pthread_mutexattr_setprotocol
369 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
370 pragma Assert
(Result
= 0);
373 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
374 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
376 if Result
= ENOMEM
then
377 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
381 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
382 pragma Assert
(Result
= 0);
389 procedure Finalize_Lock
(L
: access Lock
) is
390 Result
: Interfaces
.C
.int
;
393 Result
:= pthread_mutex_destroy
(L
);
394 pragma Assert
(Result
= 0);
397 procedure Finalize_Lock
(L
: access RTS_Lock
) is
398 Result
: Interfaces
.C
.int
;
401 Result
:= pthread_mutex_destroy
(L
);
402 pragma Assert
(Result
= 0);
409 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
410 Result
: Interfaces
.C
.int
;
413 Result
:= pthread_mutex_lock
(L
);
415 -- Assume that the cause of EINVAL is a priority ceiling violation
417 Ceiling_Violation
:= (Result
= EINVAL
);
418 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
422 (L
: access RTS_Lock
;
423 Global_Lock
: Boolean := False)
425 Result
: Interfaces
.C
.int
;
428 if not Single_Lock
or else Global_Lock
then
429 Result
:= pthread_mutex_lock
(L
);
430 pragma Assert
(Result
= 0);
434 procedure Write_Lock
(T
: Task_Id
) is
435 Result
: Interfaces
.C
.int
;
438 if not Single_Lock
then
439 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
440 pragma Assert
(Result
= 0);
448 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
450 Write_Lock
(L
, Ceiling_Violation
);
457 procedure Unlock
(L
: access Lock
) is
458 Result
: Interfaces
.C
.int
;
461 Result
:= pthread_mutex_unlock
(L
);
462 pragma Assert
(Result
= 0);
465 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
466 Result
: Interfaces
.C
.int
;
469 if not Single_Lock
or else Global_Lock
then
470 Result
:= pthread_mutex_unlock
(L
);
471 pragma Assert
(Result
= 0);
475 procedure Unlock
(T
: Task_Id
) is
476 Result
: Interfaces
.C
.int
;
479 if not Single_Lock
then
480 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
481 pragma Assert
(Result
= 0);
491 Reason
: System
.Tasking
.Task_States
)
493 pragma Warnings
(Off
, Reason
);
495 Result
: Interfaces
.C
.int
;
499 Result
:= pthread_cond_wait
500 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
502 Result
:= pthread_cond_wait
503 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
506 -- EINTR is not considered a failure.
508 pragma Assert
(Result
= 0 or else Result
= EINTR
);
515 -- This is for use within the run-time system, so abort is
516 -- assumed to be already deferred, and the caller should be
517 -- holding its own ATCB lock.
519 procedure Timed_Sleep
522 Mode
: ST
.Delay_Modes
;
523 Reason
: Task_States
;
524 Timedout
: out Boolean;
525 Yielded
: out Boolean)
527 pragma Warnings
(Off
, Reason
);
529 Check_Time
: constant Duration := Monotonic_Clock
;
532 Request
: aliased timespec
;
533 Result
: Interfaces
.C
.int
;
539 if Mode
= Relative
then
540 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
542 if Relative_Timed_Wait
then
543 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
547 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
549 if Relative_Timed_Wait
then
550 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
554 if Abs_Time
> Check_Time
then
555 if Relative_Timed_Wait
then
556 Request
:= To_Timespec
(Rel_Time
);
558 Request
:= To_Timespec
(Abs_Time
);
562 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
563 or else Self_ID
.Pending_Priority_Change
;
566 Result
:= pthread_cond_timedwait
567 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
571 Result
:= pthread_cond_timedwait
572 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
576 exit when Abs_Time
<= Monotonic_Clock
;
578 if Result
= 0 or Result
= EINTR
then
580 -- Somebody may have called Wakeup for us
586 pragma Assert
(Result
= ETIMEDOUT
);
595 -- This is for use in implementing delay statements, so
596 -- we assume the caller is abort-deferred but is holding
599 procedure Timed_Delay
602 Mode
: ST
.Delay_Modes
)
604 Check_Time
: constant Duration := Monotonic_Clock
;
607 Request
: aliased timespec
;
608 Result
: Interfaces
.C
.int
;
611 -- Only the little window between deferring abort and
612 -- locking Self_ID is the reason we need to
613 -- check for pending abort and priority change below! :(
621 Write_Lock
(Self_ID
);
623 if Mode
= Relative
then
624 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
626 if Relative_Timed_Wait
then
627 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
631 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
633 if Relative_Timed_Wait
then
634 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
638 if Abs_Time
> Check_Time
then
639 if Relative_Timed_Wait
then
640 Request
:= To_Timespec
(Rel_Time
);
642 Request
:= To_Timespec
(Abs_Time
);
645 Self_ID
.Common
.State
:= Delay_Sleep
;
648 if Self_ID
.Pending_Priority_Change
then
649 Self_ID
.Pending_Priority_Change
:= False;
650 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
651 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
654 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
657 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
658 Single_RTS_Lock
'Access, Request
'Access);
660 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
661 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
664 exit when Abs_Time
<= Monotonic_Clock
;
666 pragma Assert
(Result
= 0
667 or else Result
= ETIMEDOUT
668 or else Result
= EINTR
);
671 Self_ID
.Common
.State
:= Runnable
;
680 Result
:= sched_yield
;
681 SSL
.Abort_Undefer
.all;
684 ---------------------
685 -- Monotonic_Clock --
686 ---------------------
688 function Monotonic_Clock
return Duration is
689 TS
: aliased timespec
;
690 Result
: Interfaces
.C
.int
;
692 Result
:= clock_gettime
693 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
694 pragma Assert
(Result
= 0);
695 return To_Duration
(TS
);
702 function RT_Resolution
return Duration is
711 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
712 pragma Warnings
(Off
, Reason
);
713 Result
: Interfaces
.C
.int
;
715 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
716 pragma Assert
(Result
= 0);
723 procedure Yield
(Do_Yield
: Boolean := True) is
724 Result
: Interfaces
.C
.int
;
725 pragma Unreferenced
(Result
);
728 Result
:= sched_yield
;
736 procedure Set_Priority
738 Prio
: System
.Any_Priority
;
739 Loss_Of_Inheritance
: Boolean := False)
741 pragma Warnings
(Off
, Loss_Of_Inheritance
);
743 Result
: Interfaces
.C
.int
;
744 Param
: aliased struct_sched_param
;
747 T
.Common
.Current_Priority
:= Prio
;
748 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
750 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
751 Result
:= pthread_setschedparam
752 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
754 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
755 Result
:= pthread_setschedparam
756 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
759 Result
:= pthread_setschedparam
760 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
763 pragma Assert
(Result
= 0);
770 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
772 return T
.Common
.Current_Priority
;
779 procedure Enter_Task
(Self_ID
: Task_Id
) is
781 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
782 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
784 Specific
.Set
(Self_ID
);
788 for J
in Known_Tasks
'Range loop
789 if Known_Tasks
(J
) = null then
790 Known_Tasks
(J
) := Self_ID
;
791 Self_ID
.Known_Tasks_Index
:= J
;
803 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
805 return new Ada_Task_Control_Block
(Entry_Num
);
812 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
814 -----------------------------
815 -- Register_Foreign_Thread --
816 -----------------------------
818 function Register_Foreign_Thread
return Task_Id
is
820 if Is_Valid_Task
then
823 return Register_Foreign_Thread
(pthread_self
);
825 end Register_Foreign_Thread
;
831 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
832 Mutex_Attr
: aliased pthread_mutexattr_t
;
833 Result
: Interfaces
.C
.int
;
834 Cond_Attr
: aliased pthread_condattr_t
;
837 -- Give the task a unique serial number.
839 Self_ID
.Serial_Number
:= Next_Serial_Number
;
840 Next_Serial_Number
:= Next_Serial_Number
+ 1;
841 pragma Assert
(Next_Serial_Number
/= 0);
843 if not Single_Lock
then
844 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
845 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
848 if Locking_Policy
= 'C' then
849 Result
:= pthread_mutexattr_setprotocol
850 (Mutex_Attr
'Access, PTHREAD_PRIO_PROTECT
);
851 pragma Assert
(Result
= 0);
853 Result
:= pthread_mutexattr_setprioceiling
855 Interfaces
.C
.int
(System
.Any_Priority
'Last));
856 pragma Assert
(Result
= 0);
858 elsif Locking_Policy
= 'I' then
859 Result
:= pthread_mutexattr_setprotocol
860 (Mutex_Attr
'Access, PTHREAD_PRIO_INHERIT
);
861 pragma Assert
(Result
= 0);
864 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
866 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
874 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
875 pragma Assert
(Result
= 0);
878 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
879 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
882 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
884 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
890 if not Single_Lock
then
891 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
892 pragma Assert
(Result
= 0);
898 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
899 pragma Assert
(Result
= 0);
906 procedure Create_Task
908 Wrapper
: System
.Address
;
909 Stack_Size
: System
.Parameters
.Size_Type
;
910 Priority
: System
.Any_Priority
;
911 Succeeded
: out Boolean)
913 Attributes
: aliased pthread_attr_t
;
914 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
915 Result
: Interfaces
.C
.int
;
917 function Thread_Body_Access
is new
918 Unchecked_Conversion
(System
.Address
, Thread_Body
);
920 use System
.Task_Info
;
923 if Stack_Size
= Unspecified_Size
then
924 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
926 elsif Stack_Size
< Minimum_Stack_Size
then
927 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
930 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
933 if Stack_Base_Available
then
934 -- If Stack Checking is supported then allocate 2 additional pages:
936 -- In the worst case, stack is allocated at something like
937 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
938 -- to be sure the effective stack size is greater than what
941 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
944 Result
:= pthread_attr_init
(Attributes
'Access);
945 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
952 Result
:= pthread_attr_setdetachstate
953 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
954 pragma Assert
(Result
= 0);
956 Result
:= pthread_attr_setstacksize
957 (Attributes
'Access, Adjusted_Stack_Size
);
958 pragma Assert
(Result
= 0);
960 if T
.Common
.Task_Info
/= Default_Scope
then
962 -- We are assuming that Scope_Type has the same values than the
963 -- corresponding C macros
965 Result
:= pthread_attr_setscope
966 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
967 pragma Assert
(Result
= 0);
970 -- Since the initial signal mask of a thread is inherited from the
971 -- creator, and the Environment task has all its signals masked, we
972 -- do not need to manipulate caller's signal mask at this point.
973 -- All tasks in RTS will have All_Tasks_Mask initially.
975 Result
:= pthread_create
976 (T
.Common
.LL
.Thread
'Access,
978 Thread_Body_Access
(Wrapper
),
980 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
982 Succeeded
:= Result
= 0;
984 Result
:= pthread_attr_destroy
(Attributes
'Access);
985 pragma Assert
(Result
= 0);
987 Set_Priority
(T
, Priority
);
994 procedure Finalize_TCB
(T
: Task_Id
) is
995 Result
: Interfaces
.C
.int
;
997 Is_Self
: constant Boolean := T
= Self
;
999 procedure Free
is new
1000 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
1003 if not Single_Lock
then
1004 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
1005 pragma Assert
(Result
= 0);
1008 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
1009 pragma Assert
(Result
= 0);
1011 if T
.Known_Tasks_Index
/= -1 then
1012 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1018 Specific
.Set
(null);
1026 procedure Exit_Task
is
1028 -- Mark this task as unknown, so that if Self is called, it won't
1029 -- return a dangling pointer.
1031 Specific
.Set
(null);
1038 procedure Abort_Task
(T
: Task_Id
) is
1039 Result
: Interfaces
.C
.int
;
1042 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
1043 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1044 pragma Assert
(Result
= 0);
1053 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1054 pragma Warnings
(Off
, Self_ID
);
1059 --------------------
1060 -- Check_No_Locks --
1061 --------------------
1063 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1064 pragma Warnings
(Off
, Self_ID
);
1069 ----------------------
1070 -- Environment_Task --
1071 ----------------------
1073 function Environment_Task
return Task_Id
is
1075 return Environment_Task_Id
;
1076 end Environment_Task
;
1082 procedure Lock_RTS
is
1084 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1091 procedure Unlock_RTS
is
1093 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1100 function Suspend_Task
1102 Thread_Self
: Thread_Id
) return Boolean
1104 pragma Warnings
(Off
, T
);
1105 pragma Warnings
(Off
, Thread_Self
);
1114 function Resume_Task
1116 Thread_Self
: Thread_Id
) return Boolean
1118 pragma Warnings
(Off
, T
);
1119 pragma Warnings
(Off
, Thread_Self
);
1128 procedure Initialize
(Environment_Task
: Task_Id
) is
1129 act
: aliased struct_sigaction
;
1130 old_act
: aliased struct_sigaction
;
1131 Tmp_Set
: aliased sigset_t
;
1132 Result
: Interfaces
.C
.int
;
1135 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1136 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1137 -- Get interrupt state. Defined in a-init.c
1138 -- The input argument is the interrupt number,
1139 -- and the result is one of the following:
1141 Default
: constant Character := 's';
1142 -- 'n' this interrupt not set by any Interrupt_State pragma
1143 -- 'u' Interrupt_State pragma set state to User
1144 -- 'r' Interrupt_State pragma set state to Runtime
1145 -- 's' Interrupt_State pragma set state to System (use "default"
1149 Environment_Task_Id
:= Environment_Task
;
1151 -- Initialize the lock used to synchronize chain of all ATCBs.
1153 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1155 Specific
.Initialize
(Environment_Task
);
1157 Enter_Task
(Environment_Task
);
1159 -- Install the abort-signal handler
1161 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
1165 act
.sa_handler
:= Abort_Handler
'Address;
1167 Result
:= sigemptyset
(Tmp_Set
'Access);
1168 pragma Assert
(Result
= 0);
1169 act
.sa_mask
:= Tmp_Set
;
1173 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1174 act
'Unchecked_Access,
1175 old_act
'Unchecked_Access);
1176 pragma Assert
(Result
= 0);
1182 Result
: Interfaces
.C
.int
;
1184 -- Mask Environment task for all signals. The original mask of the
1185 -- Environment task will be recovered by Interrupt_Server task
1186 -- during the elaboration of s-interr.adb.
1188 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1189 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1191 -- Prepare the set of signals that should unblocked in all tasks
1193 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1194 pragma Assert
(Result
= 0);
1196 for J
in Interrupt_Management
.Interrupt_ID
loop
1197 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1198 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1199 pragma Assert
(Result
= 0);
1203 end System
.Task_Primitives
.Operations
;