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-2009, 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
;
48 with Ada
.Unchecked_Deallocation
;
52 with System
.Tasking
.Debug
;
53 with System
.Interrupt_Management
;
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 SSL
renames System
.Soft_Links
;
67 use System
.Tasking
.Debug
;
70 use System
.OS_Interface
;
71 use System
.Parameters
;
72 use System
.OS_Primitives
;
78 -- The followings are logically constants, but need to be initialized
81 Single_RTS_Lock
: aliased RTS_Lock
;
82 -- This is a lock to allow only one thread of control in the RTS at
83 -- a time; it is used to execute in mutual exclusion from all other tasks.
84 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
86 ATCB_Key
: aliased pthread_key_t
;
87 -- Key used to find the Ada Task_Id associated with a thread
89 Environment_Task_Id
: Task_Id
;
90 -- A variable to hold Task_Id for the environment task
92 Locking_Policy
: Character;
93 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
94 -- Value of the pragma Locking_Policy:
95 -- 'C' for Ceiling_Locking
96 -- 'I' for Inherit_Locking
99 Unblocked_Signal_Mask
: aliased sigset_t
;
100 -- The set of signals that should unblocked in all tasks
102 -- The followings are internal configuration constants needed
104 Next_Serial_Number
: Task_Serial_Number
:= 100;
105 -- We start at 100, to reserve some special values for
106 -- using in error checking.
108 Time_Slice_Val
: Integer;
109 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
111 Dispatching_Policy
: Character;
112 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
114 Foreign_Task_Elaborated
: aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
118 -- Whether to use an alternate signal stack for stack overflows
120 Abort_Handler_Installed
: Boolean := False;
121 -- True if a handler for the abort signal is installed
129 procedure Initialize
(Environment_Task
: Task_Id
);
130 pragma Inline
(Initialize
);
131 -- Initialize various data needed by this package
133 function Is_Valid_Task
return Boolean;
134 pragma Inline
(Is_Valid_Task
);
135 -- Does executing thread have a TCB?
137 procedure Set
(Self_Id
: Task_Id
);
139 -- Set the self id for the current task
141 function Self
return Task_Id
;
142 pragma Inline
(Self
);
143 -- Return a pointer to the Ada Task Control Block of the calling task
147 package body Specific
is separate;
148 -- The body of this package is target specific
150 ---------------------------------
151 -- Support for foreign threads --
152 ---------------------------------
154 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
155 -- Allocate and Initialize a new ATCB for the current Thread
157 function Register_Foreign_Thread
158 (Thread
: Thread_Id
) return Task_Id
is separate;
160 -----------------------
161 -- Local Subprograms --
162 -----------------------
164 procedure Abort_Handler
(Sig
: Signal
);
165 -- Signal handler used to implement asynchronous abort.
166 -- See also comment before body, below.
168 function To_Address
is
169 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
175 -- Target-dependent binding of inter-thread Abort signal to the raising of
176 -- the Abort_Signal exception.
178 -- The technical issues and alternatives here are essentially the
179 -- same as for raising exceptions in response to other signals
180 -- (e.g. Storage_Error). See code and comments in the package body
181 -- System.Interrupt_Management.
183 -- Some implementations may not allow an exception to be propagated out of
184 -- a handler, and others might leave the signal or interrupt that invoked
185 -- this handler masked after the exceptional return to the application
188 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
189 -- most UNIX systems, this will allow transfer out of a signal handler,
190 -- which is usually the only mechanism available for implementing
191 -- asynchronous handlers of this kind. However, some systems do not
192 -- restore the signal mask on longjmp(), leaving the abort signal masked.
194 procedure Abort_Handler
(Sig
: Signal
) is
195 pragma Unreferenced
(Sig
);
197 T
: constant Task_Id
:= Self
;
198 Old_Set
: aliased sigset_t
;
200 Result
: Interfaces
.C
.int
;
201 pragma Warnings
(Off
, Result
);
204 -- It's not safe to raise an exception when using GCC ZCX mechanism.
205 -- Note that we still need to install a signal handler, since in some
206 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
207 -- need to send the Abort signal to a task.
209 if ZCX_By_Default
and then GCC_ZCX_Support
then
213 if T
.Deferral_Level
= 0
214 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
219 -- Make sure signals used for RTS internal purpose are unmasked
221 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
222 Unblocked_Signal_Mask
'Access, Old_Set
'Access);
223 pragma Assert
(Result
= 0);
225 raise Standard
'Abort_Signal;
233 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
234 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
235 Guard_Page_Address
: Address
;
237 Res
: Interfaces
.C
.int
;
240 if Stack_Base_Available
then
242 -- Compute the guard page address
244 Guard_Page_Address
:=
245 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
248 mprotect
(Guard_Page_Address
, Get_Page_Size
,
249 prot
=> (if On
then PROT_ON
else PROT_OFF
));
250 pragma Assert
(Res
= 0);
258 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
260 return T
.Common
.LL
.Thread
;
267 function Self
return Task_Id
renames Specific
.Self
;
269 ---------------------
270 -- Initialize_Lock --
271 ---------------------
273 -- Note: mutexes and cond_variables needed per-task basis are
274 -- initialized in Initialize_TCB and the Storage_Error is
275 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
276 -- used in RTS is initialized before any status change of RTS.
277 -- Therefore raising Storage_Error in the following routines
278 -- should be able to be handled safely.
280 procedure Initialize_Lock
281 (Prio
: System
.Any_Priority
;
282 L
: not null access Lock
)
284 Attributes
: aliased pthread_mutexattr_t
;
285 Result
: Interfaces
.C
.int
;
288 Result
:= pthread_mutexattr_init
(Attributes
'Access);
289 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
291 if Result
= ENOMEM
then
295 if Locking_Policy
= 'C' then
296 Result
:= pthread_mutexattr_setprotocol
297 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
298 pragma Assert
(Result
= 0);
300 Result
:= pthread_mutexattr_setprioceiling
301 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
302 pragma Assert
(Result
= 0);
304 elsif Locking_Policy
= 'I' then
305 Result
:= pthread_mutexattr_setprotocol
306 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
307 pragma Assert
(Result
= 0);
310 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
311 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
313 if Result
= ENOMEM
then
314 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
318 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
319 pragma Assert
(Result
= 0);
322 procedure Initialize_Lock
323 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
325 pragma Unreferenced
(Level
);
327 Attributes
: aliased pthread_mutexattr_t
;
328 Result
: Interfaces
.C
.int
;
331 Result
:= pthread_mutexattr_init
(Attributes
'Access);
332 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
334 if Result
= ENOMEM
then
338 if Locking_Policy
= 'C' then
339 Result
:= pthread_mutexattr_setprotocol
340 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
341 pragma Assert
(Result
= 0);
343 Result
:= pthread_mutexattr_setprioceiling
344 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
345 pragma Assert
(Result
= 0);
347 elsif Locking_Policy
= 'I' then
348 Result
:= pthread_mutexattr_setprotocol
349 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
350 pragma Assert
(Result
= 0);
353 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
354 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
356 if Result
= ENOMEM
then
357 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
361 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
362 pragma Assert
(Result
= 0);
369 procedure Finalize_Lock
(L
: not null access Lock
) is
370 Result
: Interfaces
.C
.int
;
372 Result
:= pthread_mutex_destroy
(L
);
373 pragma Assert
(Result
= 0);
376 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
377 Result
: Interfaces
.C
.int
;
379 Result
:= pthread_mutex_destroy
(L
);
380 pragma Assert
(Result
= 0);
388 (L
: not null access Lock
; Ceiling_Violation
: out Boolean)
390 Result
: Interfaces
.C
.int
;
393 Result
:= pthread_mutex_lock
(L
);
395 -- Assume that the cause of EINVAL is a priority ceiling violation
397 Ceiling_Violation
:= (Result
= EINVAL
);
398 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
402 (L
: not null access RTS_Lock
;
403 Global_Lock
: Boolean := False)
405 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
;
416 if not Single_Lock
then
417 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
418 pragma Assert
(Result
= 0);
427 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
429 Write_Lock
(L
, Ceiling_Violation
);
436 procedure Unlock
(L
: not null access Lock
) is
437 Result
: Interfaces
.C
.int
;
439 Result
:= pthread_mutex_unlock
(L
);
440 pragma Assert
(Result
= 0);
444 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False)
446 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
;
457 if not Single_Lock
then
458 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
459 pragma Assert
(Result
= 0);
467 -- Dynamic priority ceilings are not supported by the underlying system
469 procedure Set_Ceiling
470 (L
: not null access Lock
;
471 Prio
: System
.Any_Priority
)
473 pragma Unreferenced
(L
, Prio
);
484 Reason
: System
.Tasking
.Task_States
)
486 pragma Unreferenced
(Reason
);
488 Result
: Interfaces
.C
.int
;
493 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
494 mutex
=> (if Single_Lock
495 then Single_RTS_Lock
'Access
496 else Self_ID
.Common
.LL
.L
'Access));
498 -- EINTR is not considered a failure
500 pragma Assert
(Result
= 0 or else Result
= EINTR
);
507 -- This is for use within the run-time system, so abort is
508 -- assumed to be already deferred, and the caller should be
509 -- holding its own ATCB lock.
511 procedure Timed_Sleep
514 Mode
: ST
.Delay_Modes
;
515 Reason
: Task_States
;
516 Timedout
: out Boolean;
517 Yielded
: out Boolean)
519 pragma Unreferenced
(Reason
);
521 Base_Time
: constant Duration := Monotonic_Clock
;
522 Check_Time
: Duration := Base_Time
;
525 Request
: aliased timespec
;
526 Result
: Interfaces
.C
.int
;
532 if Mode
= Relative
then
533 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
535 if Relative_Timed_Wait
then
536 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
540 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
542 if Relative_Timed_Wait
then
543 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
547 if Abs_Time
> Check_Time
then
549 To_Timespec
(if Relative_Timed_Wait
then Rel_Time
else Abs_Time
);
552 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
555 pthread_cond_timedwait
556 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
557 mutex
=> (if Single_Lock
558 then Single_RTS_Lock
'Access
559 else Self_ID
.Common
.LL
.L
'Access),
560 abstime
=> Request
'Access);
562 Check_Time
:= Monotonic_Clock
;
563 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
565 if Result
= 0 or Result
= EINTR
then
567 -- Somebody may have called Wakeup for us
573 pragma Assert
(Result
= ETIMEDOUT
);
582 -- This is for use in implementing delay statements, so we assume the
583 -- caller is abort-deferred but is holding no locks.
585 procedure Timed_Delay
588 Mode
: ST
.Delay_Modes
)
590 Base_Time
: constant Duration := Monotonic_Clock
;
591 Check_Time
: Duration := Base_Time
;
594 Request
: aliased timespec
;
596 Result
: Interfaces
.C
.int
;
597 pragma Warnings
(Off
, Result
);
604 Write_Lock
(Self_ID
);
606 if Mode
= Relative
then
607 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
609 if Relative_Timed_Wait
then
610 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
614 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
616 if Relative_Timed_Wait
then
617 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
621 if Abs_Time
> Check_Time
then
623 To_Timespec
(if Relative_Timed_Wait
then Rel_Time
else Abs_Time
);
624 Self_ID
.Common
.State
:= Delay_Sleep
;
627 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
630 pthread_cond_timedwait
631 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
632 mutex
=> (if Single_Lock
633 then Single_RTS_Lock
'Access
634 else Self_ID
.Common
.LL
.L
'Access),
635 abstime
=> Request
'Access);
637 Check_Time
:= Monotonic_Clock
;
638 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
640 pragma Assert
(Result
= 0
641 or else Result
= ETIMEDOUT
642 or else Result
= EINTR
);
645 Self_ID
.Common
.State
:= Runnable
;
654 Result
:= sched_yield
;
657 ---------------------
658 -- Monotonic_Clock --
659 ---------------------
661 function Monotonic_Clock
return Duration is
662 TS
: aliased timespec
;
663 Result
: Interfaces
.C
.int
;
665 Result
:= clock_gettime
666 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
667 pragma Assert
(Result
= 0);
668 return To_Duration
(TS
);
675 function RT_Resolution
return Duration is
684 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
685 pragma Unreferenced
(Reason
);
686 Result
: Interfaces
.C
.int
;
688 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
689 pragma Assert
(Result
= 0);
696 procedure Yield
(Do_Yield
: Boolean := True) is
697 Result
: Interfaces
.C
.int
;
698 pragma Unreferenced
(Result
);
701 Result
:= sched_yield
;
709 procedure Set_Priority
711 Prio
: System
.Any_Priority
;
712 Loss_Of_Inheritance
: Boolean := False)
714 pragma Unreferenced
(Loss_Of_Inheritance
);
716 Result
: Interfaces
.C
.int
;
717 Param
: aliased struct_sched_param
;
719 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
720 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
721 -- Get priority specific dispatching policy
723 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
724 -- Upper case first character of the policy name corresponding to the
725 -- task as set by a Priority_Specific_Dispatching pragma.
728 T
.Common
.Current_Priority
:= Prio
;
729 Param
.sched_priority
:= To_Target_Priority
(Prio
);
731 if Time_Slice_Supported
732 and then (Dispatching_Policy
= 'R'
733 or else Priority_Specific_Policy
= 'R'
734 or else Time_Slice_Val
> 0)
736 Result
:= pthread_setschedparam
737 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
739 elsif Dispatching_Policy
= 'F'
740 or else Priority_Specific_Policy
= 'F'
741 or else Time_Slice_Val
= 0
743 Result
:= pthread_setschedparam
744 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
747 Result
:= pthread_setschedparam
748 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
751 pragma Assert
(Result
= 0);
758 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
760 return T
.Common
.Current_Priority
;
767 procedure Enter_Task
(Self_ID
: Task_Id
) is
769 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
770 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
772 Specific
.Set
(Self_ID
);
774 if Use_Alternate_Stack
then
776 Stack
: aliased stack_t
;
777 Result
: Interfaces
.C
.int
;
779 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
780 Stack
.ss_size
:= Alternate_Stack_Size
;
782 Result
:= sigaltstack
(Stack
'Access, null);
783 pragma Assert
(Result
= 0);
792 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
794 return new Ada_Task_Control_Block
(Entry_Num
);
801 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
803 -----------------------------
804 -- Register_Foreign_Thread --
805 -----------------------------
807 function Register_Foreign_Thread
return Task_Id
is
809 if Is_Valid_Task
then
812 return Register_Foreign_Thread
(pthread_self
);
814 end Register_Foreign_Thread
;
820 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
821 Mutex_Attr
: aliased pthread_mutexattr_t
;
822 Result
: Interfaces
.C
.int
;
823 Cond_Attr
: aliased pthread_condattr_t
;
826 -- Give the task a unique serial number
828 Self_ID
.Serial_Number
:= Next_Serial_Number
;
829 Next_Serial_Number
:= Next_Serial_Number
+ 1;
830 pragma Assert
(Next_Serial_Number
/= 0);
832 if not Single_Lock
then
833 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
834 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
837 if Locking_Policy
= 'C' then
839 pthread_mutexattr_setprotocol
841 PTHREAD_PRIO_PROTECT
);
842 pragma Assert
(Result
= 0);
845 pthread_mutexattr_setprioceiling
847 Interfaces
.C
.int
(System
.Any_Priority
'Last));
848 pragma Assert
(Result
= 0);
850 elsif Locking_Policy
= 'I' then
852 pthread_mutexattr_setprotocol
854 PTHREAD_PRIO_INHERIT
);
855 pragma Assert
(Result
= 0);
860 (Self_ID
.Common
.LL
.L
'Access,
862 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
870 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
871 pragma Assert
(Result
= 0);
874 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
875 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
880 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
881 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
887 if not Single_Lock
then
888 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
889 pragma Assert
(Result
= 0);
895 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
896 pragma Assert
(Result
= 0);
903 procedure Create_Task
905 Wrapper
: System
.Address
;
906 Stack_Size
: System
.Parameters
.Size_Type
;
907 Priority
: System
.Any_Priority
;
908 Succeeded
: out Boolean)
910 Attributes
: aliased pthread_attr_t
;
911 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
912 Page_Size
: constant Interfaces
.C
.size_t
:= Get_Page_Size
;
913 Result
: Interfaces
.C
.int
;
915 function Thread_Body_Access
is new
916 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
918 use System
.Task_Info
;
921 Adjusted_Stack_Size
:=
922 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
924 if Stack_Base_Available
then
926 -- If Stack Checking is supported then allocate 2 additional pages:
928 -- In the worst case, stack is allocated at something like
929 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
930 -- to be sure the effective stack size is greater than what
933 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
936 -- Round stack size as this is required by some OSes (Darwin)
938 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
939 Adjusted_Stack_Size
:=
940 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
942 Result
:= pthread_attr_init
(Attributes
'Access);
943 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
951 pthread_attr_setdetachstate
952 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
953 pragma Assert
(Result
= 0);
956 pthread_attr_setstacksize
957 (Attributes
'Access, Adjusted_Stack_Size
);
958 pragma Assert
(Result
= 0);
960 if T
.Common
.Task_Info
/= Default_Scope
then
961 case T
.Common
.Task_Info
is
962 when System
.Task_Info
.Process_Scope
=>
964 pthread_attr_setscope
965 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
967 when System
.Task_Info
.System_Scope
=>
969 pthread_attr_setscope
970 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
972 when System
.Task_Info
.Default_Scope
=>
976 pragma Assert
(Result
= 0);
979 -- Since the initial signal mask of a thread is inherited from the
980 -- creator, and the Environment task has all its signals masked, we
981 -- do not need to manipulate caller's signal mask at this point.
982 -- All tasks in RTS will have All_Tasks_Mask initially.
984 Result
:= pthread_create
985 (T
.Common
.LL
.Thread
'Access,
987 Thread_Body_Access
(Wrapper
),
989 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
991 Succeeded
:= Result
= 0;
993 Result
:= pthread_attr_destroy
(Attributes
'Access);
994 pragma Assert
(Result
= 0);
997 Set_Priority
(T
, Priority
);
1005 procedure Finalize_TCB
(T
: Task_Id
) is
1006 Result
: Interfaces
.C
.int
;
1008 Is_Self
: constant Boolean := T
= Self
;
1010 procedure Free
is new
1011 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
1014 if not Single_Lock
then
1015 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
1016 pragma Assert
(Result
= 0);
1019 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
1020 pragma Assert
(Result
= 0);
1022 if T
.Known_Tasks_Index
/= -1 then
1023 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1029 Specific
.Set
(null);
1037 procedure Exit_Task
is
1039 -- Mark this task as unknown, so that if Self is called, it won't
1040 -- return a dangling pointer.
1042 Specific
.Set
(null);
1049 procedure Abort_Task
(T
: Task_Id
) is
1050 Result
: Interfaces
.C
.int
;
1052 if Abort_Handler_Installed
then
1055 (T
.Common
.LL
.Thread
,
1056 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1057 pragma Assert
(Result
= 0);
1065 procedure Initialize
(S
: in out Suspension_Object
) is
1066 Mutex_Attr
: aliased pthread_mutexattr_t
;
1067 Cond_Attr
: aliased pthread_condattr_t
;
1068 Result
: Interfaces
.C
.int
;
1071 -- Initialize internal state (always to False (RM D.10 (6)))
1076 -- Initialize internal mutex
1078 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1079 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1081 if Result
= ENOMEM
then
1082 raise Storage_Error
;
1085 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1086 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1088 if Result
= ENOMEM
then
1089 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1090 pragma Assert
(Result
= 0);
1092 raise Storage_Error
;
1095 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1096 pragma Assert
(Result
= 0);
1098 -- Initialize internal condition variable
1100 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1101 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1104 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1105 pragma Assert
(Result
= 0);
1107 if Result
= ENOMEM
then
1108 raise Storage_Error
;
1112 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1113 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1116 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1117 pragma Assert
(Result
= 0);
1119 if Result
= ENOMEM
then
1120 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1121 pragma Assert
(Result
= 0);
1122 raise Storage_Error
;
1126 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1127 pragma Assert
(Result
= 0);
1134 procedure Finalize
(S
: in out Suspension_Object
) is
1135 Result
: Interfaces
.C
.int
;
1138 -- Destroy internal mutex
1140 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1141 pragma Assert
(Result
= 0);
1143 -- Destroy internal condition variable
1145 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1146 pragma Assert
(Result
= 0);
1153 function Current_State
(S
: Suspension_Object
) return Boolean is
1155 -- We do not want to use lock on this read operation. State is marked
1156 -- as Atomic so that we ensure that the value retrieved is correct.
1165 procedure Set_False
(S
: in out Suspension_Object
) is
1166 Result
: Interfaces
.C
.int
;
1169 SSL
.Abort_Defer
.all;
1171 Result
:= pthread_mutex_lock
(S
.L
'Access);
1172 pragma Assert
(Result
= 0);
1176 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1177 pragma Assert
(Result
= 0);
1179 SSL
.Abort_Undefer
.all;
1186 procedure Set_True
(S
: in out Suspension_Object
) is
1187 Result
: Interfaces
.C
.int
;
1190 SSL
.Abort_Defer
.all;
1192 Result
:= pthread_mutex_lock
(S
.L
'Access);
1193 pragma Assert
(Result
= 0);
1195 -- If there is already a task waiting on this suspension object then
1196 -- we resume it, leaving the state of the suspension object to False,
1197 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1198 -- the state to True.
1204 Result
:= pthread_cond_signal
(S
.CV
'Access);
1205 pragma Assert
(Result
= 0);
1211 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1212 pragma Assert
(Result
= 0);
1214 SSL
.Abort_Undefer
.all;
1217 ------------------------
1218 -- Suspend_Until_True --
1219 ------------------------
1221 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1222 Result
: Interfaces
.C
.int
;
1225 SSL
.Abort_Defer
.all;
1227 Result
:= pthread_mutex_lock
(S
.L
'Access);
1228 pragma Assert
(Result
= 0);
1232 -- Program_Error must be raised upon calling Suspend_Until_True
1233 -- if another task is already waiting on that suspension object
1236 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1237 pragma Assert
(Result
= 0);
1239 SSL
.Abort_Undefer
.all;
1241 raise Program_Error
;
1244 -- Suspend the task if the state is False. Otherwise, the task
1245 -- continues its execution, and the state of the suspension object
1246 -- is set to False (ARM D.10 par. 9).
1254 -- Loop in case pthread_cond_wait returns earlier than expected
1255 -- (e.g. in case of EINTR caused by a signal).
1257 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1258 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1260 exit when not S
.Waiting
;
1264 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1265 pragma Assert
(Result
= 0);
1267 SSL
.Abort_Undefer
.all;
1269 end Suspend_Until_True
;
1277 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1278 pragma Unreferenced
(Self_ID
);
1283 --------------------
1284 -- Check_No_Locks --
1285 --------------------
1287 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1288 pragma Unreferenced
(Self_ID
);
1293 ----------------------
1294 -- Environment_Task --
1295 ----------------------
1297 function Environment_Task
return Task_Id
is
1299 return Environment_Task_Id
;
1300 end Environment_Task
;
1306 procedure Lock_RTS
is
1308 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1315 procedure Unlock_RTS
is
1317 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1324 function Suspend_Task
1326 Thread_Self
: Thread_Id
) return Boolean
1328 pragma Unreferenced
(T
, Thread_Self
);
1337 function Resume_Task
1339 Thread_Self
: Thread_Id
) return Boolean
1341 pragma Unreferenced
(T
, Thread_Self
);
1346 --------------------
1347 -- Stop_All_Tasks --
1348 --------------------
1350 procedure Stop_All_Tasks
is
1359 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1360 pragma Unreferenced
(T
);
1369 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1370 pragma Unreferenced
(T
);
1379 procedure Initialize
(Environment_Task
: Task_Id
) is
1380 act
: aliased struct_sigaction
;
1381 old_act
: aliased struct_sigaction
;
1382 Tmp_Set
: aliased sigset_t
;
1383 Result
: Interfaces
.C
.int
;
1386 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1387 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1388 -- Get interrupt state. Defined in a-init.c
1389 -- The input argument is the interrupt number,
1390 -- and the result is one of the following:
1392 Default
: constant Character := 's';
1393 -- 'n' this interrupt not set by any Interrupt_State pragma
1394 -- 'u' Interrupt_State pragma set state to User
1395 -- 'r' Interrupt_State pragma set state to Runtime
1396 -- 's' Interrupt_State pragma set state to System (use "default"
1400 Environment_Task_Id
:= Environment_Task
;
1402 Interrupt_Management
.Initialize
;
1404 -- Prepare the set of signals that should unblocked in all tasks
1406 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1407 pragma Assert
(Result
= 0);
1409 for J
in Interrupt_Management
.Interrupt_ID
loop
1410 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1411 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1412 pragma Assert
(Result
= 0);
1416 -- Initialize the lock used to synchronize chain of all ATCBs
1418 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1420 Specific
.Initialize
(Environment_Task
);
1422 if Use_Alternate_Stack
then
1423 Environment_Task
.Common
.Task_Alternate_Stack
:=
1424 Alternate_Stack
'Address;
1427 -- Make environment task known here because it doesn't go through
1428 -- Activate_Tasks, which does it for all other tasks.
1430 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1431 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1433 Enter_Task
(Environment_Task
);
1436 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1439 act
.sa_handler
:= Abort_Handler
'Address;
1441 Result
:= sigemptyset
(Tmp_Set
'Access);
1442 pragma Assert
(Result
= 0);
1443 act
.sa_mask
:= Tmp_Set
;
1447 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1448 act
'Unchecked_Access,
1449 old_act
'Unchecked_Access);
1450 pragma Assert
(Result
= 0);
1451 Abort_Handler_Installed
:= True;
1455 end System
.Task_Primitives
.Operations
;