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-2008, 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 Tru64 version of this package
36 -- This package contains all the GNULL primitives that interface directly with
40 -- Turn off polling, we do not want ATC polling to take place during tasking
41 -- operations. It causes infinite loops and other problems.
43 with Ada.Unchecked_Deallocation;
48 with System.Tasking.Debug;
49 with System.Interrupt_Management;
50 with System.OS_Primitives;
51 with System.Task_Info;
53 with System.Soft_Links;
54 -- We use System.Soft_Links instead of System.Tasking.Initialization
55 -- because the later is a higher level package that we shouldn't depend on.
56 -- For example when using the restricted run time, it is replaced by
57 -- System.Tasking.Restricted.Stages.
59 package body System.Task_Primitives.Operations is
61 package SSL renames System.Soft_Links;
63 use System.Tasking.Debug;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
74 -- The followings are logically constants, but need to be initialized
77 Single_RTS_Lock : aliased RTS_Lock;
78 -- This is a lock to allow only one thread of control in the RTS at
79 -- a time; it is used to execute in mutual exclusion from all other tasks.
80 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
82 ATCB_Key : aliased pthread_key_t;
83 -- Key used to find the Ada Task_Id associated with a thread
85 Environment_Task_Id : Task_Id;
86 -- A variable to hold Task_Id for the environment task
88 Unblocked_Signal_Mask : aliased sigset_t;
89 -- The set of signals that should unblocked in all tasks
91 Time_Slice_Val : Integer;
92 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
94 Locking_Policy : Character;
95 pragma Import (C, Locking_Policy, "__gl_locking_policy");
97 Dispatching_Policy : Character;
98 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
102 Foreign_Task_Elaborated : aliased Boolean := True;
103 -- Used to identified fake tasks (i.e., non-Ada Threads)
111 procedure Initialize (Environment_Task : Task_Id);
112 pragma Inline (Initialize);
113 -- Initialize various data needed by this package
115 function Is_Valid_Task return Boolean;
116 pragma Inline (Is_Valid_Task);
117 -- Does executing thread have a TCB?
119 procedure Set (Self_Id : Task_Id);
121 -- Set the self id for the current task
123 function Self return Task_Id;
124 pragma Inline (Self);
125 -- Return a pointer to the Ada Task Control Block of the calling task
129 package body Specific is separate;
130 -- The body of this package is target specific
132 ---------------------------------
133 -- Support for foreign threads --
134 ---------------------------------
136 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
137 -- Allocate and initialize a new ATCB for the current Thread
139 function Register_Foreign_Thread
140 (Thread : Thread_Id) return Task_Id is separate;
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 procedure Abort_Handler (Sig : Signal);
147 -- Signal handler used to implement asynchronous abort
149 function Get_Policy (Prio : System.Any_Priority) return Character;
150 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
151 -- Get priority specific dispatching policy
157 procedure Abort_Handler (Sig : Signal) is
158 pragma Unreferenced (Sig);
160 T : constant Task_Id := Self;
161 Old_Set : aliased sigset_t;
163 Result : Interfaces.C.int;
164 pragma Warnings (Off, Result);
167 -- It is not safe to raise an exception when using ZCX and the GCC
168 -- exception handling mechanism.
170 if ZCX_By_Default and then GCC_ZCX_Support then
174 if T.Deferral_Level = 0
175 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
176 and then not T.Aborting
180 -- Make sure signals used for RTS internal purpose are unmasked
185 Unblocked_Signal_Mask'Access,
187 pragma Assert (Result = 0);
189 raise Standard'Abort_Signal;
197 -- The underlying thread system sets a guard page at the bottom of a thread
198 -- stack, so nothing is needed.
200 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
201 pragma Unreferenced (T);
202 pragma Unreferenced (On);
211 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
213 return T.Common.LL.Thread;
220 function Self return Task_Id renames Specific.Self;
222 ---------------------
223 -- Initialize_Lock --
224 ---------------------
226 -- Note: mutexes and cond_variables needed per-task basis are initialized
227 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
228 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
229 -- status change of RTS. Therefore rasing Storage_Error in the following
230 -- routines should be able to be handled safely.
232 procedure Initialize_Lock
233 (Prio : System.Any_Priority;
234 L : not null access Lock)
236 Attributes : aliased pthread_mutexattr_t;
237 Result : Interfaces.C.int;
240 Result := pthread_mutexattr_init (Attributes'Access);
241 pragma Assert (Result = 0 or else Result = ENOMEM);
243 if Result = ENOMEM then
247 if Locking_Policy = 'C' then
248 L.Ceiling := Interfaces.C.int (Prio);
251 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
252 pragma Assert (Result = 0 or else Result = ENOMEM);
254 if Result = ENOMEM then
255 Result := pthread_mutexattr_destroy (Attributes'Access);
259 Result := pthread_mutexattr_destroy (Attributes'Access);
260 pragma Assert (Result = 0);
263 procedure Initialize_Lock
264 (L : not null access RTS_Lock;
267 pragma Unreferenced (Level);
269 Attributes : aliased pthread_mutexattr_t;
270 Result : Interfaces.C.int;
273 Result := pthread_mutexattr_init (Attributes'Access);
274 pragma Assert (Result = 0 or else Result = ENOMEM);
276 if Result = ENOMEM then
280 Result := pthread_mutex_init (L, Attributes'Access);
281 pragma Assert (Result = 0 or else Result = ENOMEM);
283 if Result = ENOMEM then
284 Result := pthread_mutexattr_destroy (Attributes'Access);
288 Result := pthread_mutexattr_destroy (Attributes'Access);
289 pragma Assert (Result = 0);
296 procedure Finalize_Lock (L : not null access Lock) is
297 Result : Interfaces.C.int;
299 Result := pthread_mutex_destroy (L.L'Access);
300 pragma Assert (Result = 0);
303 procedure Finalize_Lock (L : not null access RTS_Lock) is
304 Result : Interfaces.C.int;
306 Result := pthread_mutex_destroy (L);
307 pragma Assert (Result = 0);
315 (L : not null access Lock;
316 Ceiling_Violation : out Boolean)
318 Result : Interfaces.C.int;
320 All_Tasks_Link : Task_Id;
321 Current_Prio : System.Any_Priority;
324 -- Perform ceiling checks only when this is the locking policy in use
326 if Locking_Policy = 'C' then
328 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
329 Current_Prio := Get_Priority (Self_ID);
331 -- If there is no other task, no need to check priorities
333 if All_Tasks_Link /= Null_Task
334 and then L.Ceiling < Interfaces.C.int (Current_Prio)
336 Ceiling_Violation := True;
341 Result := pthread_mutex_lock (L.L'Access);
342 pragma Assert (Result = 0);
344 Ceiling_Violation := False;
348 (L : not null access RTS_Lock;
349 Global_Lock : Boolean := False)
351 Result : Interfaces.C.int;
353 if not Single_Lock or else Global_Lock then
354 Result := pthread_mutex_lock (L);
355 pragma Assert (Result = 0);
359 procedure Write_Lock (T : Task_Id) is
360 Result : Interfaces.C.int;
362 if not Single_Lock then
363 Result := pthread_mutex_lock (T.Common.LL.L'Access);
364 pragma Assert (Result = 0);
373 (L : not null access Lock;
374 Ceiling_Violation : out Boolean)
377 Write_Lock (L, Ceiling_Violation);
384 procedure Unlock (L : not null access Lock) is
385 Result : Interfaces.C.int;
387 Result := pthread_mutex_unlock (L.L'Access);
388 pragma Assert (Result = 0);
392 (L : not null access RTS_Lock;
393 Global_Lock : Boolean := False)
395 Result : Interfaces.C.int;
397 if not Single_Lock or else Global_Lock then
398 Result := pthread_mutex_unlock (L);
399 pragma Assert (Result = 0);
403 procedure Unlock (T : Task_Id) is
404 Result : Interfaces.C.int;
406 if not Single_Lock then
407 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
408 pragma Assert (Result = 0);
416 -- Dynamic priority ceilings are not supported by the underlying system
418 procedure Set_Ceiling
419 (L : not null access Lock;
420 Prio : System.Any_Priority)
422 pragma Unreferenced (L, Prio);
433 Reason : System.Tasking.Task_States)
435 pragma Unreferenced (Reason);
437 Result : Interfaces.C.int;
443 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
447 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
450 -- EINTR is not considered a failure
452 pragma Assert (Result = 0 or else Result = EINTR);
459 -- This is for use within the run-time system, so abort is assumed to be
460 -- already deferred, and the caller should be holding its own ATCB lock.
462 procedure Timed_Sleep
465 Mode : ST.Delay_Modes;
466 Reason : System.Tasking.Task_States;
467 Timedout : out Boolean;
468 Yielded : out Boolean)
470 pragma Unreferenced (Reason);
472 Base_Time : constant Duration := Monotonic_Clock;
473 Check_Time : Duration := Base_Time;
475 Request : aliased timespec;
476 Result : Interfaces.C.int;
482 if Mode = Relative then
483 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
485 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
488 if Abs_Time > Check_Time then
489 Request := To_Timespec (Abs_Time);
492 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
496 pthread_cond_timedwait
497 (Self_ID.Common.LL.CV'Access,
498 Single_RTS_Lock'Access,
503 pthread_cond_timedwait
504 (Self_ID.Common.LL.CV'Access,
505 Self_ID.Common.LL.L'Access,
509 Check_Time := Monotonic_Clock;
510 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
512 if Result = 0 or Result = EINTR then
514 -- Somebody may have called Wakeup for us
520 pragma Assert (Result = ETIMEDOUT);
529 -- This is for use in implementing delay statements, so we assume the
530 -- caller is abort-deferred but is holding no locks.
532 procedure Timed_Delay
535 Mode : ST.Delay_Modes)
537 Base_Time : constant Duration := Monotonic_Clock;
538 Check_Time : Duration := Base_Time;
540 Request : aliased timespec;
541 Result : Interfaces.C.int;
548 Write_Lock (Self_ID);
550 if Mode = Relative then
551 Abs_Time := Time + Check_Time;
553 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
556 if Abs_Time > Check_Time then
557 Request := To_Timespec (Abs_Time);
558 Self_ID.Common.State := Delay_Sleep;
561 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
565 pthread_cond_timedwait
566 (Self_ID.Common.LL.CV'Access,
567 Single_RTS_Lock'Access,
571 pthread_cond_timedwait
572 (Self_ID.Common.LL.CV'Access,
573 Self_ID.Common.LL.L'Access,
577 Check_Time := Monotonic_Clock;
578 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
580 pragma Assert (Result = 0 or else
581 Result = ETIMEDOUT or else
585 Self_ID.Common.State := Runnable;
597 ---------------------
598 -- Monotonic_Clock --
599 ---------------------
601 function Monotonic_Clock return Duration is
602 TS : aliased timespec;
603 Result : Interfaces.C.int;
605 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
606 pragma Assert (Result = 0);
607 return To_Duration (TS);
614 function RT_Resolution return Duration is
616 -- Returned value must be an integral multiple of Duration'Small (1 ns)
617 -- The following is the best approximation of 1/1024. The clock on the
618 -- DEC Alpha ticks at 1024 Hz.
620 return 0.000_976_563;
627 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
628 pragma Unreferenced (Reason);
629 Result : Interfaces.C.int;
631 Result := pthread_cond_signal (T.Common.LL.CV'Access);
632 pragma Assert (Result = 0);
639 procedure Yield (Do_Yield : Boolean := True) is
640 Result : Interfaces.C.int;
641 pragma Unreferenced (Result);
644 Result := sched_yield;
652 procedure Set_Priority
654 Prio : System.Any_Priority;
655 Loss_Of_Inheritance : Boolean := False)
657 pragma Unreferenced (Loss_Of_Inheritance);
659 Result : Interfaces.C.int;
660 Param : aliased struct_sched_param;
662 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
663 -- Upper case first character of the policy name corresponding to the
664 -- task as set by a Priority_Specific_Dispatching pragma.
667 T.Common.Current_Priority := Prio;
668 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
670 if Dispatching_Policy = 'R'
671 or else Priority_Specific_Policy = 'R'
672 or else Time_Slice_Val > 0
675 pthread_setschedparam
676 (T.Common.LL.Thread, SCHED_RR, Param'Access);
678 elsif Dispatching_Policy = 'F'
679 or else Priority_Specific_Policy = 'F'
680 or else Time_Slice_Val = 0
683 pthread_setschedparam
684 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
688 pthread_setschedparam
689 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
692 pragma Assert (Result = 0);
699 function Get_Priority (T : Task_Id) return System.Any_Priority is
701 return T.Common.Current_Priority;
708 procedure Enter_Task (Self_ID : Task_Id) is
710 Hide_Unhide_Yellow_Zone (Hide => True);
711 Self_ID.Common.LL.Thread := pthread_self;
712 Specific.Set (Self_ID);
716 for J in Known_Tasks'Range loop
717 if Known_Tasks (J) = null then
718 Known_Tasks (J) := Self_ID;
719 Self_ID.Known_Tasks_Index := J;
731 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
733 return new Ada_Task_Control_Block (Entry_Num);
740 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
742 -----------------------------
743 -- Register_Foreign_Thread --
744 -----------------------------
746 function Register_Foreign_Thread return Task_Id is
748 if Is_Valid_Task then
751 return Register_Foreign_Thread (pthread_self);
753 end Register_Foreign_Thread;
759 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
760 Mutex_Attr : aliased pthread_mutexattr_t;
761 Result : Interfaces.C.int;
762 Cond_Attr : aliased pthread_condattr_t;
765 if not Single_Lock then
766 Result := pthread_mutexattr_init (Mutex_Attr'Access);
767 pragma Assert (Result = 0 or else Result = ENOMEM);
772 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
773 pragma Assert (Result = 0 or else Result = ENOMEM);
781 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
782 pragma Assert (Result = 0);
785 Result := pthread_condattr_init (Cond_Attr'Access);
786 pragma Assert (Result = 0 or else Result = ENOMEM);
791 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
792 pragma Assert (Result = 0 or else Result = ENOMEM);
798 if not Single_Lock then
799 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
800 pragma Assert (Result = 0);
806 Result := pthread_condattr_destroy (Cond_Attr'Access);
807 pragma Assert (Result = 0);
814 procedure Create_Task
816 Wrapper : System.Address;
817 Stack_Size : System.Parameters.Size_Type;
818 Priority : System.Any_Priority;
819 Succeeded : out Boolean)
821 Attributes : aliased pthread_attr_t;
822 Adjusted_Stack_Size : Interfaces.C.size_t;
823 Result : Interfaces.C.int;
824 Param : aliased System.OS_Interface.struct_sched_param;
826 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
827 -- Upper case first character of the policy name corresponding to the
828 -- task as set by a Priority_Specific_Dispatching pragma.
830 use System.Task_Info;
833 -- Account for the Yellow Zone (2 pages) and the guard page right above.
834 -- See Hide_Unhide_Yellow_Zone for the rationale.
836 Adjusted_Stack_Size :=
837 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
839 Result := pthread_attr_init (Attributes'Access);
840 pragma Assert (Result = 0 or else Result = ENOMEM);
848 pthread_attr_setdetachstate
849 (Attributes'Access, PTHREAD_CREATE_DETACHED);
850 pragma Assert (Result = 0);
853 pthread_attr_setstacksize
854 (Attributes'Access, Adjusted_Stack_Size);
855 pragma Assert (Result = 0);
857 Param.sched_priority :=
858 Interfaces.C.int (Underlying_Priorities (Priority));
860 pthread_attr_setschedparam
861 (Attributes'Access, Param'Access);
862 pragma Assert (Result = 0);
864 if Dispatching_Policy = 'R'
865 or else Priority_Specific_Policy = 'R'
866 or else Time_Slice_Val > 0
869 pthread_attr_setschedpolicy
870 (Attributes'Access, System.OS_Interface.SCHED_RR);
872 elsif Dispatching_Policy = 'F'
873 or else Priority_Specific_Policy = 'F'
874 or else Time_Slice_Val = 0
877 pthread_attr_setschedpolicy
878 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
882 pthread_attr_setschedpolicy
883 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
886 pragma Assert (Result = 0);
888 -- Set the scheduling parameters explicitly, since this is the only way
889 -- to force the OS to take e.g. the sched policy and scope attributes
893 pthread_attr_setinheritsched
894 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
895 pragma Assert (Result = 0);
897 T.Common.Current_Priority := Priority;
899 if T.Common.Task_Info /= null then
900 case T.Common.Task_Info.Contention_Scope is
901 when System.Task_Info.Process_Scope =>
903 pthread_attr_setscope
904 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
906 when System.Task_Info.System_Scope =>
908 pthread_attr_setscope
909 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
911 when System.Task_Info.Default_Scope =>
915 pragma Assert (Result = 0);
918 -- Since the initial signal mask of a thread is inherited from the
919 -- creator, and the Environment task has all its signals masked, we
920 -- do not need to manipulate caller's signal mask at this point.
921 -- All tasks in RTS will have All_Tasks_Mask initially.
925 (T.Common.LL.Thread'Access,
927 Thread_Body_Access (Wrapper),
929 pragma Assert (Result = 0 or else Result = EAGAIN);
931 Succeeded := Result = 0;
933 Result := pthread_attr_destroy (Attributes'Access);
934 pragma Assert (Result = 0);
936 if Succeeded and then T.Common.Task_Info /= null then
938 -- ??? We're using a process-wide function to implement a task
939 -- specific characteristic.
941 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
942 Result := bind_to_cpu (Curpid, 0);
944 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
948 Interfaces.C.unsigned_long (
949 Interfaces.Shift_Left
950 (Interfaces.Unsigned_64'(1),
951 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
952 pragma Assert (Result = 0);
961 procedure Finalize_TCB (T : Task_Id) is
962 Result : Interfaces.C.int;
964 Is_Self : constant Boolean := T = Self;
966 procedure Free is new
967 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
970 if not Single_Lock then
971 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
972 pragma Assert (Result = 0);
975 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
976 pragma Assert (Result = 0);
978 if T.Known_Tasks_Index /= -1 then
979 Known_Tasks (T.Known_Tasks_Index) := null;
993 procedure Exit_Task is
996 Hide_Unhide_Yellow_Zone (Hide => False);
1003 procedure Abort_Task (T : Task_Id) is
1004 Result : Interfaces.C.int;
1006 Result := pthread_kill (T.Common.LL.Thread,
1007 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1008 pragma Assert (Result = 0);
1015 procedure Initialize (S : in out Suspension_Object) is
1016 Mutex_Attr : aliased pthread_mutexattr_t;
1017 Cond_Attr : aliased pthread_condattr_t;
1018 Result : Interfaces.C.int;
1021 -- Initialize internal state (always to False (RM D.10(6)))
1026 -- Initialize internal mutex
1028 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1029 pragma Assert (Result = 0 or else Result = ENOMEM);
1031 if Result = ENOMEM then
1032 raise Storage_Error;
1035 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1036 pragma Assert (Result = 0 or else Result = ENOMEM);
1038 if Result = ENOMEM then
1039 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1040 raise Storage_Error;
1043 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1044 pragma Assert (Result = 0);
1046 -- Initialize internal condition variable
1048 Result := pthread_condattr_init (Cond_Attr'Access);
1049 pragma Assert (Result = 0 or else Result = ENOMEM);
1051 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1053 pragma Assert (Result = 0 or else Result = ENOMEM);
1056 Result := pthread_mutex_destroy (S.L'Access);
1057 pragma Assert (Result = 0);
1059 if Result = ENOMEM then
1060 raise Storage_Error;
1069 procedure Finalize (S : in out Suspension_Object) is
1070 Result : Interfaces.C.int;
1073 -- Destroy internal mutex
1075 Result := pthread_mutex_destroy (S.L'Access);
1076 pragma Assert (Result = 0);
1078 -- Destroy internal condition variable
1080 Result := pthread_cond_destroy (S.CV'Access);
1081 pragma Assert (Result = 0);
1088 function Current_State (S : Suspension_Object) return Boolean is
1090 -- We do not want to use lock on this read operation. State is marked
1091 -- as Atomic so that we ensure that the value retrieved is correct.
1100 procedure Set_False (S : in out Suspension_Object) is
1101 Result : Interfaces.C.int;
1104 SSL.Abort_Defer.all;
1106 Result := pthread_mutex_lock (S.L'Access);
1107 pragma Assert (Result = 0);
1111 Result := pthread_mutex_unlock (S.L'Access);
1112 pragma Assert (Result = 0);
1114 SSL.Abort_Undefer.all;
1121 procedure Set_True (S : in out Suspension_Object) is
1122 Result : Interfaces.C.int;
1125 SSL.Abort_Defer.all;
1127 Result := pthread_mutex_lock (S.L'Access);
1128 pragma Assert (Result = 0);
1130 -- If there is already a task waiting on this suspension object then we
1131 -- resume it, leaving the state of the suspension object to False, as
1132 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1138 Result := pthread_cond_signal (S.CV'Access);
1139 pragma Assert (Result = 0);
1145 Result := pthread_mutex_unlock (S.L'Access);
1146 pragma Assert (Result = 0);
1148 SSL.Abort_Undefer.all;
1151 ------------------------
1152 -- Suspend_Until_True --
1153 ------------------------
1155 procedure Suspend_Until_True (S : in out Suspension_Object) is
1156 Result : Interfaces.C.int;
1159 SSL.Abort_Defer.all;
1161 Result := pthread_mutex_lock (S.L'Access);
1162 pragma Assert (Result = 0);
1166 -- Program_Error must be raised upon calling Suspend_Until_True
1167 -- if another task is already waiting on that suspension object
1170 Result := pthread_mutex_unlock (S.L'Access);
1171 pragma Assert (Result = 0);
1173 SSL.Abort_Undefer.all;
1175 raise Program_Error;
1178 -- Suspend the task if the state is False. Otherwise, the task
1179 -- continues its execution, and the state of the suspension object
1180 -- is set to False (RM D.10(9)).
1186 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1189 Result := pthread_mutex_unlock (S.L'Access);
1190 pragma Assert (Result = 0);
1192 SSL.Abort_Undefer.all;
1194 end Suspend_Until_True;
1202 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1203 pragma Unreferenced (Self_ID);
1208 --------------------
1209 -- Check_No_Locks --
1210 --------------------
1212 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1213 pragma Unreferenced (Self_ID);
1218 ----------------------
1219 -- Environment_Task --
1220 ----------------------
1222 function Environment_Task return Task_Id is
1224 return Environment_Task_Id;
1225 end Environment_Task;
1231 procedure Lock_RTS is
1233 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1240 procedure Unlock_RTS is
1242 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1249 function Suspend_Task
1251 Thread_Self : Thread_Id) return Boolean
1253 pragma Unreferenced (T, Thread_Self);
1262 function Resume_Task
1264 Thread_Self : Thread_Id) return Boolean
1266 pragma Unreferenced (T, Thread_Self);
1271 --------------------
1272 -- Stop_All_Tasks --
1273 --------------------
1275 procedure Stop_All_Tasks is
1284 function Stop_Task (T : ST.Task_Id) return Boolean is
1285 pragma Unreferenced (T);
1294 function Continue_Task (T : ST.Task_Id) return Boolean is
1295 pragma Unreferenced (T);
1304 procedure Initialize (Environment_Task : Task_Id) is
1305 act : aliased struct_sigaction;
1306 old_act : aliased struct_sigaction;
1307 Tmp_Set : aliased sigset_t;
1308 Result : Interfaces.C.int;
1311 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1312 pragma Import (C, State, "__gnat_get_interrupt_state");
1313 -- Get interrupt state. Defined in a-init.c. The input argument is
1314 -- the interrupt number, and the result is one of the following:
1316 Default : constant Character := 's';
1317 -- 'n' this interrupt not set by any Interrupt_State pragma
1318 -- 'u' Interrupt_State pragma set state to User
1319 -- 'r' Interrupt_State pragma set state to Runtime
1320 -- 's' Interrupt_State pragma set state to System (use "default"
1324 Environment_Task_Id := Environment_Task;
1326 Interrupt_Management.Initialize;
1328 -- Prepare the set of signals that should unblocked in all tasks
1330 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1331 pragma Assert (Result = 0);
1333 for J in Interrupt_Management.Interrupt_ID loop
1334 if System.Interrupt_Management.Keep_Unmasked (J) then
1335 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1336 pragma Assert (Result = 0);
1342 -- Initialize the lock used to synchronize chain of all ATCBs
1344 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1346 Specific.Initialize (Environment_Task);
1348 Enter_Task (Environment_Task);
1350 -- Install the abort-signal handler
1353 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1356 act.sa_handler := Abort_Handler'Address;
1358 Result := sigemptyset (Tmp_Set'Access);
1359 pragma Assert (Result = 0);
1360 act.sa_mask := Tmp_Set;
1364 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1365 act'Unchecked_Access,
1366 old_act'Unchecked_Access);
1367 pragma Assert (Result = 0);
1371 end System.Task_Primitives.Operations;