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-2011, 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 Tru64 version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 with System
.Interrupt_Management
;
46 with System
.OS_Primitives
;
47 with System
.Task_Info
;
49 with System
.Soft_Links
;
50 -- We use System.Soft_Links instead of System.Tasking.Initialization
51 -- because the later is a higher level package that we shouldn't depend on.
52 -- For example when using the restricted run time, it is replaced by
53 -- System.Tasking.Restricted.Stages.
55 package body System
.Task_Primitives
.Operations
is
57 package SSL
renames System
.Soft_Links
;
59 use System
.Tasking
.Debug
;
62 use System
.OS_Interface
;
63 use System
.Parameters
;
64 use System
.OS_Primitives
;
70 -- The followings are logically constants, but need to be initialized
73 Single_RTS_Lock
: aliased RTS_Lock
;
74 -- This is a lock to allow only one thread of control in the RTS at
75 -- a time; it is used to execute in mutual exclusion from all other tasks.
76 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
78 Environment_Task_Id
: Task_Id
;
79 -- A variable to hold Task_Id for the environment task
81 Unblocked_Signal_Mask
: aliased sigset_t
;
82 -- The set of signals that should unblocked in all tasks
84 Time_Slice_Val
: Integer;
85 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
87 Locking_Policy
: Character;
88 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
90 Dispatching_Policy
: Character;
91 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
95 Foreign_Task_Elaborated
: aliased Boolean := True;
96 -- Used to identified fake tasks (i.e., non-Ada Threads)
98 Abort_Handler_Installed
: Boolean := False;
99 -- True if a handler for the abort signal is installed
107 procedure Initialize
(Environment_Task
: Task_Id
);
108 pragma Inline
(Initialize
);
109 -- Initialize various data needed by this package
111 function Is_Valid_Task
return Boolean;
112 pragma Inline
(Is_Valid_Task
);
113 -- Does executing thread have a TCB?
115 procedure Set
(Self_Id
: Task_Id
);
117 -- Set the self id for the current task
119 function Self
return Task_Id
;
120 pragma Inline
(Self
);
121 -- Return a pointer to the Ada Task Control Block of the calling task
125 package body Specific
is separate;
126 -- The body of this package is target specific
128 ----------------------------------
129 -- ATCB allocation/deallocation --
130 ----------------------------------
132 package body ATCB_Allocation
is separate;
133 -- The body of this package is shared across several targets
135 ---------------------------------
136 -- Support for foreign threads --
137 ---------------------------------
139 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
140 -- Allocate and initialize a new ATCB for the current Thread
142 function Register_Foreign_Thread
143 (Thread
: Thread_Id
) return Task_Id
is separate;
145 -----------------------
146 -- Local Subprograms --
147 -----------------------
149 procedure Abort_Handler
(Sig
: Signal
);
150 -- Signal handler used to implement asynchronous abort
152 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
153 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
154 -- Get priority specific dispatching policy
160 procedure Abort_Handler
(Sig
: Signal
) is
161 pragma Unreferenced
(Sig
);
163 T
: constant Task_Id
:= Self
;
164 Old_Set
: aliased sigset_t
;
166 Result
: Interfaces
.C
.int
;
167 pragma Warnings
(Off
, Result
);
170 -- It's not safe to raise an exception when using GCC ZCX mechanism.
171 -- Note that we still need to install a signal handler, since in some
172 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
173 -- need to send the Abort signal to a task.
175 if ZCX_By_Default
then
179 if T
.Deferral_Level
= 0
180 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
181 and then not T
.Aborting
185 -- Make sure signals used for RTS internal purpose are unmasked
190 Unblocked_Signal_Mask
'Access,
192 pragma Assert
(Result
= 0);
194 raise Standard
'Abort_Signal;
202 -- The underlying thread system sets a guard page at the bottom of a thread
203 -- stack, so nothing is needed.
205 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
206 pragma Unreferenced
(T
);
207 pragma Unreferenced
(On
);
216 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
218 return T
.Common
.LL
.Thread
;
225 function Self
return Task_Id
renames Specific
.Self
;
227 ---------------------
228 -- Initialize_Lock --
229 ---------------------
231 -- Note: mutexes and cond_variables needed per-task basis are initialized
232 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
233 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
234 -- status change of RTS. Therefore raising Storage_Error in the following
235 -- routines should be able to be handled safely.
237 procedure Initialize_Lock
238 (Prio
: System
.Any_Priority
;
239 L
: not null access Lock
)
241 Attributes
: aliased pthread_mutexattr_t
;
242 Result
: Interfaces
.C
.int
;
245 Result
:= pthread_mutexattr_init
(Attributes
'Access);
246 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
248 if Result
= ENOMEM
then
252 if Locking_Policy
= 'C' then
253 L
.Ceiling
:= Interfaces
.C
.int
(Prio
);
256 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
257 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
259 if Result
= ENOMEM
then
260 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
264 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
265 pragma Assert
(Result
= 0);
268 procedure Initialize_Lock
269 (L
: not null access RTS_Lock
;
272 pragma Unreferenced
(Level
);
274 Attributes
: aliased pthread_mutexattr_t
;
275 Result
: Interfaces
.C
.int
;
278 Result
:= pthread_mutexattr_init
(Attributes
'Access);
279 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
281 if Result
= ENOMEM
then
285 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
286 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
288 if Result
= ENOMEM
then
289 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
293 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
294 pragma Assert
(Result
= 0);
301 procedure Finalize_Lock
(L
: not null access Lock
) is
302 Result
: Interfaces
.C
.int
;
304 Result
:= pthread_mutex_destroy
(L
.L
'Access);
305 pragma Assert
(Result
= 0);
308 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
309 Result
: Interfaces
.C
.int
;
311 Result
:= pthread_mutex_destroy
(L
);
312 pragma Assert
(Result
= 0);
320 (L
: not null access Lock
;
321 Ceiling_Violation
: out Boolean)
323 Result
: Interfaces
.C
.int
;
325 All_Tasks_Link
: Task_Id
;
326 Current_Prio
: System
.Any_Priority
;
329 -- Perform ceiling checks only when this is the locking policy in use
331 if Locking_Policy
= 'C' then
333 All_Tasks_Link
:= Self_ID
.Common
.All_Tasks_Link
;
334 Current_Prio
:= Get_Priority
(Self_ID
);
336 -- If there is no other task, no need to check priorities
338 if All_Tasks_Link
/= Null_Task
339 and then L
.Ceiling
< Interfaces
.C
.int
(Current_Prio
)
341 Ceiling_Violation
:= True;
346 Result
:= pthread_mutex_lock
(L
.L
'Access);
347 pragma Assert
(Result
= 0);
349 Ceiling_Violation
:= False;
353 (L
: not null access RTS_Lock
;
354 Global_Lock
: Boolean := False)
356 Result
: Interfaces
.C
.int
;
358 if not Single_Lock
or else Global_Lock
then
359 Result
:= pthread_mutex_lock
(L
);
360 pragma Assert
(Result
= 0);
364 procedure Write_Lock
(T
: Task_Id
) is
365 Result
: Interfaces
.C
.int
;
367 if not Single_Lock
then
368 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
369 pragma Assert
(Result
= 0);
378 (L
: not null access Lock
;
379 Ceiling_Violation
: out Boolean)
382 Write_Lock
(L
, Ceiling_Violation
);
389 procedure Unlock
(L
: not null access Lock
) is
390 Result
: Interfaces
.C
.int
;
392 Result
:= pthread_mutex_unlock
(L
.L
'Access);
393 pragma Assert
(Result
= 0);
397 (L
: not null access RTS_Lock
;
398 Global_Lock
: Boolean := False)
400 Result
: Interfaces
.C
.int
;
402 if not Single_Lock
or else Global_Lock
then
403 Result
:= pthread_mutex_unlock
(L
);
404 pragma Assert
(Result
= 0);
408 procedure Unlock
(T
: Task_Id
) is
409 Result
: Interfaces
.C
.int
;
411 if not Single_Lock
then
412 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
413 pragma Assert
(Result
= 0);
421 -- Dynamic priority ceilings are not supported by the underlying system
423 procedure Set_Ceiling
424 (L
: not null access Lock
;
425 Prio
: System
.Any_Priority
)
427 pragma Unreferenced
(L
, Prio
);
438 Reason
: System
.Tasking
.Task_States
)
440 pragma Unreferenced
(Reason
);
442 Result
: Interfaces
.C
.int
;
447 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
448 mutex
=> (if Single_Lock
449 then Single_RTS_Lock
'Access
450 else Self_ID
.Common
.LL
.L
'Access));
452 -- EINTR is not considered a failure
454 pragma Assert
(Result
= 0 or else Result
= EINTR
);
461 -- This is for use within the run-time system, so abort is assumed to be
462 -- already deferred, and the caller should be holding its own ATCB lock.
464 procedure Timed_Sleep
467 Mode
: ST
.Delay_Modes
;
468 Reason
: System
.Tasking
.Task_States
;
469 Timedout
: out Boolean;
470 Yielded
: out Boolean)
472 pragma Unreferenced
(Reason
);
474 Base_Time
: constant Duration := Monotonic_Clock
;
475 Check_Time
: Duration := Base_Time
;
477 Request
: aliased timespec
;
478 Result
: Interfaces
.C
.int
;
486 then Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
487 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
489 if Abs_Time
> Check_Time
then
490 Request
:= To_Timespec
(Abs_Time
);
493 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
496 pthread_cond_timedwait
497 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
498 mutex
=> (if Single_Lock
499 then Single_RTS_Lock
'Access
500 else Self_ID
.Common
.LL
.L
'Access),
501 abstime
=> Request
'Access);
503 Check_Time
:= Monotonic_Clock
;
504 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
506 if Result
= 0 or Result
= EINTR
then
508 -- Somebody may have called Wakeup for us
514 pragma Assert
(Result
= ETIMEDOUT
);
523 -- This is for use in implementing delay statements, so we assume the
524 -- caller is abort-deferred but is holding no locks.
526 procedure Timed_Delay
529 Mode
: ST
.Delay_Modes
)
531 Base_Time
: constant Duration := Monotonic_Clock
;
532 Check_Time
: Duration := Base_Time
;
534 Request
: aliased timespec
;
535 Result
: Interfaces
.C
.int
;
542 Write_Lock
(Self_ID
);
546 then Time
+ Check_Time
547 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
549 if Abs_Time
> Check_Time
then
550 Request
:= To_Timespec
(Abs_Time
);
551 Self_ID
.Common
.State
:= Delay_Sleep
;
554 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
557 pthread_cond_timedwait
558 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
559 mutex
=> (if Single_Lock
560 then Single_RTS_Lock
'Access
561 else Self_ID
.Common
.LL
.L
'Access),
562 abstime
=> Request
'Access);
564 Check_Time
:= Monotonic_Clock
;
565 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
567 pragma Assert
(Result
= 0 or else
568 Result
= ETIMEDOUT
or else
572 Self_ID
.Common
.State
:= Runnable
;
584 ---------------------
585 -- Monotonic_Clock --
586 ---------------------
588 function Monotonic_Clock
return Duration is
589 TS
: aliased timespec
;
590 Result
: Interfaces
.C
.int
;
592 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
593 pragma Assert
(Result
= 0);
594 return To_Duration
(TS
);
601 function RT_Resolution
return Duration is
603 -- Returned value must be an integral multiple of Duration'Small (1 ns)
604 -- The following is the best approximation of 1/1024. The clock on the
605 -- DEC Alpha ticks at 1024 Hz.
607 return 0.000_976_563
;
614 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
615 pragma Unreferenced
(Reason
);
616 Result
: Interfaces
.C
.int
;
618 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
619 pragma Assert
(Result
= 0);
626 procedure Yield
(Do_Yield
: Boolean := True) is
627 Result
: Interfaces
.C
.int
;
628 pragma Unreferenced
(Result
);
631 Result
:= sched_yield
;
639 procedure Set_Priority
641 Prio
: System
.Any_Priority
;
642 Loss_Of_Inheritance
: Boolean := False)
644 pragma Unreferenced
(Loss_Of_Inheritance
);
646 Result
: Interfaces
.C
.int
;
647 Param
: aliased struct_sched_param
;
649 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
650 -- Upper case first character of the policy name corresponding to the
651 -- task as set by a Priority_Specific_Dispatching pragma.
654 T
.Common
.Current_Priority
:= Prio
;
655 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
657 if Dispatching_Policy
= 'R'
658 or else Priority_Specific_Policy
= 'R'
659 or else Time_Slice_Val
> 0
662 pthread_setschedparam
663 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
665 elsif Dispatching_Policy
= 'F'
666 or else Priority_Specific_Policy
= 'F'
667 or else Time_Slice_Val
= 0
670 pthread_setschedparam
671 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
675 pthread_setschedparam
676 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
679 pragma Assert
(Result
= 0);
686 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
688 return T
.Common
.Current_Priority
;
695 procedure Enter_Task
(Self_ID
: Task_Id
) is
697 Hide_Unhide_Yellow_Zone
(Hide
=> True);
698 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
700 Specific
.Set
(Self_ID
);
707 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
709 -----------------------------
710 -- Register_Foreign_Thread --
711 -----------------------------
713 function Register_Foreign_Thread
return Task_Id
is
715 if Is_Valid_Task
then
718 return Register_Foreign_Thread
(pthread_self
);
720 end Register_Foreign_Thread
;
726 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
727 Mutex_Attr
: aliased pthread_mutexattr_t
;
728 Result
: Interfaces
.C
.int
;
729 Cond_Attr
: aliased pthread_condattr_t
;
732 if not Single_Lock
then
733 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
734 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
739 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
740 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
748 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
749 pragma Assert
(Result
= 0);
752 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
753 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
758 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
759 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
765 if not Single_Lock
then
766 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
767 pragma Assert
(Result
= 0);
773 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
774 pragma Assert
(Result
= 0);
781 procedure Create_Task
783 Wrapper
: System
.Address
;
784 Stack_Size
: System
.Parameters
.Size_Type
;
785 Priority
: System
.Any_Priority
;
786 Succeeded
: out Boolean)
788 Attributes
: aliased pthread_attr_t
;
789 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
790 Result
: Interfaces
.C
.int
;
791 Param
: aliased System
.OS_Interface
.struct_sched_param
;
793 Priority_Specific_Policy
: constant Character := Get_Policy
(Priority
);
794 -- Upper case first character of the policy name corresponding to the
795 -- task as set by a Priority_Specific_Dispatching pragma.
797 use System
.Task_Info
;
800 -- Account for the Yellow Zone (2 pages) and the guard page right above.
801 -- See Hide_Unhide_Yellow_Zone for the rationale.
803 Adjusted_Stack_Size
:=
804 Interfaces
.C
.size_t
(Stack_Size
) + 3 * Get_Page_Size
;
806 Result
:= pthread_attr_init
(Attributes
'Access);
807 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
815 pthread_attr_setdetachstate
816 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
817 pragma Assert
(Result
= 0);
820 pthread_attr_setstacksize
821 (Attributes
'Access, Adjusted_Stack_Size
);
822 pragma Assert
(Result
= 0);
824 Param
.sched_priority
:=
825 Interfaces
.C
.int
(Underlying_Priorities
(Priority
));
827 pthread_attr_setschedparam
828 (Attributes
'Access, Param
'Access);
829 pragma Assert
(Result
= 0);
831 if Dispatching_Policy
= 'R'
832 or else Priority_Specific_Policy
= 'R'
833 or else Time_Slice_Val
> 0
836 pthread_attr_setschedpolicy
837 (Attributes
'Access, System
.OS_Interface
.SCHED_RR
);
839 elsif Dispatching_Policy
= 'F'
840 or else Priority_Specific_Policy
= 'F'
841 or else Time_Slice_Val
= 0
844 pthread_attr_setschedpolicy
845 (Attributes
'Access, System
.OS_Interface
.SCHED_FIFO
);
849 pthread_attr_setschedpolicy
850 (Attributes
'Access, System
.OS_Interface
.SCHED_OTHER
);
853 pragma Assert
(Result
= 0);
855 -- Set the scheduling parameters explicitly, since this is the only way
856 -- to force the OS to take e.g. the sched policy and scope attributes
860 pthread_attr_setinheritsched
861 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
862 pragma Assert
(Result
= 0);
864 T
.Common
.Current_Priority
:= Priority
;
866 if T
.Common
.Task_Info
/= null then
867 case T
.Common
.Task_Info
.Contention_Scope
is
868 when System
.Task_Info
.Process_Scope
=>
870 pthread_attr_setscope
871 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
873 when System
.Task_Info
.System_Scope
=>
875 pthread_attr_setscope
876 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
878 when System
.Task_Info
.Default_Scope
=>
882 pragma Assert
(Result
= 0);
885 -- Since the initial signal mask of a thread is inherited from the
886 -- creator, and the Environment task has all its signals masked, we
887 -- do not need to manipulate caller's signal mask at this point.
888 -- All tasks in RTS will have All_Tasks_Mask initially.
892 (T
.Common
.LL
.Thread
'Access,
894 Thread_Body_Access
(Wrapper
),
896 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
898 Succeeded
:= Result
= 0;
900 Result
:= pthread_attr_destroy
(Attributes
'Access);
901 pragma Assert
(Result
= 0);
903 if Succeeded
and then T
.Common
.Task_Info
/= null then
905 -- ??? We're using a process-wide function to implement a task
906 -- specific characteristic.
908 if T
.Common
.Task_Info
.Bind_To_Cpu_Number
= 0 then
909 Result
:= bind_to_cpu
(Curpid
, 0);
911 elsif T
.Common
.Task_Info
.Bind_To_Cpu_Number
> 0 then
915 Interfaces
.C
.unsigned_long
(
916 Interfaces
.Shift_Left
917 (Interfaces
.Unsigned_64
'(1),
918 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
919 pragma Assert (Result = 0);
928 procedure Finalize_TCB (T : Task_Id) is
929 Result : Interfaces.C.int;
932 if not Single_Lock then
933 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
934 pragma Assert (Result = 0);
937 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
938 pragma Assert (Result = 0);
940 if T.Known_Tasks_Index /= -1 then
941 Known_Tasks (T.Known_Tasks_Index) := null;
944 ATCB_Allocation.Free_ATCB (T);
951 procedure Exit_Task is
954 Hide_Unhide_Yellow_Zone (Hide => False);
961 procedure Abort_Task (T : Task_Id) is
962 Result : Interfaces.C.int;
964 if Abort_Handler_Installed then
965 Result := pthread_kill (T.Common.LL.Thread,
966 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
967 pragma Assert (Result = 0);
975 procedure Initialize (S : in out Suspension_Object) is
976 Mutex_Attr : aliased pthread_mutexattr_t;
977 Cond_Attr : aliased pthread_condattr_t;
978 Result : Interfaces.C.int;
981 -- Initialize internal state (always to False (RM D.10(6)))
986 -- Initialize internal mutex
988 Result := pthread_mutexattr_init (Mutex_Attr'Access);
989 pragma Assert (Result = 0 or else Result = ENOMEM);
991 if Result = ENOMEM then
995 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
996 pragma Assert (Result = 0 or else Result = ENOMEM);
998 if Result = ENOMEM then
999 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1000 raise Storage_Error;
1003 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1004 pragma Assert (Result = 0);
1006 -- Initialize internal condition variable
1008 Result := pthread_condattr_init (Cond_Attr'Access);
1009 pragma Assert (Result = 0 or else Result = ENOMEM);
1011 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1013 pragma Assert (Result = 0 or else Result = ENOMEM);
1016 Result := pthread_mutex_destroy (S.L'Access);
1017 pragma Assert (Result = 0);
1019 if Result = ENOMEM then
1020 raise Storage_Error;
1029 procedure Finalize (S : in out Suspension_Object) is
1030 Result : Interfaces.C.int;
1033 -- Destroy internal mutex
1035 Result := pthread_mutex_destroy (S.L'Access);
1036 pragma Assert (Result = 0);
1038 -- Destroy internal condition variable
1040 Result := pthread_cond_destroy (S.CV'Access);
1041 pragma Assert (Result = 0);
1048 function Current_State (S : Suspension_Object) return Boolean is
1050 -- We do not want to use lock on this read operation. State is marked
1051 -- as Atomic so that we ensure that the value retrieved is correct.
1060 procedure Set_False (S : in out Suspension_Object) is
1061 Result : Interfaces.C.int;
1064 SSL.Abort_Defer.all;
1066 Result := pthread_mutex_lock (S.L'Access);
1067 pragma Assert (Result = 0);
1071 Result := pthread_mutex_unlock (S.L'Access);
1072 pragma Assert (Result = 0);
1074 SSL.Abort_Undefer.all;
1081 procedure Set_True (S : in out Suspension_Object) is
1082 Result : Interfaces.C.int;
1085 SSL.Abort_Defer.all;
1087 Result := pthread_mutex_lock (S.L'Access);
1088 pragma Assert (Result = 0);
1090 -- If there is already a task waiting on this suspension object then we
1091 -- resume it, leaving the state of the suspension object to False, as
1092 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1098 Result := pthread_cond_signal (S.CV'Access);
1099 pragma Assert (Result = 0);
1105 Result := pthread_mutex_unlock (S.L'Access);
1106 pragma Assert (Result = 0);
1108 SSL.Abort_Undefer.all;
1111 ------------------------
1112 -- Suspend_Until_True --
1113 ------------------------
1115 procedure Suspend_Until_True (S : in out Suspension_Object) is
1116 Result : Interfaces.C.int;
1119 SSL.Abort_Defer.all;
1121 Result := pthread_mutex_lock (S.L'Access);
1122 pragma Assert (Result = 0);
1126 -- Program_Error must be raised upon calling Suspend_Until_True
1127 -- if another task is already waiting on that suspension object
1130 Result := pthread_mutex_unlock (S.L'Access);
1131 pragma Assert (Result = 0);
1133 SSL.Abort_Undefer.all;
1135 raise Program_Error;
1138 -- Suspend the task if the state is False. Otherwise, the task
1139 -- continues its execution, and the state of the suspension object
1140 -- is set to False (RM D.10(9)).
1148 -- Loop in case pthread_cond_wait returns earlier than expected
1149 -- (e.g. in case of EINTR caused by a signal).
1151 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1152 pragma Assert (Result = 0 or else Result = EINTR);
1154 exit when not S.Waiting;
1158 Result := pthread_mutex_unlock (S.L'Access);
1159 pragma Assert (Result = 0);
1161 SSL.Abort_Undefer.all;
1163 end Suspend_Until_True;
1171 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1172 pragma Unreferenced (Self_ID);
1177 --------------------
1178 -- Check_No_Locks --
1179 --------------------
1181 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1182 pragma Unreferenced (Self_ID);
1187 ----------------------
1188 -- Environment_Task --
1189 ----------------------
1191 function Environment_Task return Task_Id is
1193 return Environment_Task_Id;
1194 end Environment_Task;
1200 procedure Lock_RTS is
1202 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1209 procedure Unlock_RTS is
1211 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1218 function Suspend_Task
1220 Thread_Self : Thread_Id) return Boolean
1222 pragma Unreferenced (T, Thread_Self);
1231 function Resume_Task
1233 Thread_Self : Thread_Id) return Boolean
1235 pragma Unreferenced (T, Thread_Self);
1240 --------------------
1241 -- Stop_All_Tasks --
1242 --------------------
1244 procedure Stop_All_Tasks is
1253 function Stop_Task (T : ST.Task_Id) return Boolean is
1254 pragma Unreferenced (T);
1263 function Continue_Task (T : ST.Task_Id) return Boolean is
1264 pragma Unreferenced (T);
1273 procedure Initialize (Environment_Task : Task_Id) is
1274 act : aliased struct_sigaction;
1275 old_act : aliased struct_sigaction;
1276 Tmp_Set : aliased sigset_t;
1277 Result : Interfaces.C.int;
1280 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1281 pragma Import (C, State, "__gnat_get_interrupt_state");
1282 -- Get interrupt state. Defined in a-init.c. The input argument is
1283 -- the interrupt number, and the result is one of the following:
1285 Default : constant Character := 's
';
1286 -- 'n
' this interrupt not set by any Interrupt_State pragma
1287 -- 'u
' Interrupt_State pragma set state to User
1288 -- 'r
' Interrupt_State pragma set state to Runtime
1289 -- 's
' Interrupt_State pragma set state to System (use "default"
1293 Environment_Task_Id := Environment_Task;
1295 Interrupt_Management.Initialize;
1297 -- Prepare the set of signals that should unblocked in all tasks
1299 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1300 pragma Assert (Result = 0);
1302 for J in Interrupt_Management.Interrupt_ID loop
1303 if System.Interrupt_Management.Keep_Unmasked (J) then
1304 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1305 pragma Assert (Result = 0);
1311 -- Initialize the lock used to synchronize chain of all ATCBs
1313 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1315 Specific.Initialize (Environment_Task);
1317 -- Make environment task known here because it doesn't go through
1318 -- Activate_Tasks, which does it for all other tasks.
1320 Known_Tasks (Known_Tasks'First) := Environment_Task;
1321 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1323 Enter_Task (Environment_Task);
1326 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1329 act.sa_handler := Abort_Handler'Address;
1331 Result := sigemptyset (Tmp_Set'Access);
1332 pragma Assert (Result = 0);
1333 act.sa_mask := Tmp_Set;
1337 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1338 act'Unchecked_Access,
1339 old_act'Unchecked_Access);
1340 pragma Assert (Result = 0);
1341 Abort_Handler_Installed := True;
1345 -----------------------
1346 -- Set_Task_Affinity --
1347 -----------------------
1349 procedure Set_Task_Affinity (T : ST.Task_Id) is
1350 pragma Unreferenced (T);
1353 -- Setting task affinity is not supported by the underlying system
1356 end Set_Task_Affinity;
1357 end System.Task_Primitives.Operations;