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_Constants;
47 with System.OS_Primitives;
48 with System.Task_Info;
50 with System.Soft_Links;
51 -- We use System.Soft_Links instead of System.Tasking.Initialization
52 -- because the later is a higher level package that we shouldn't depend on.
53 -- For example when using the restricted run time, it is replaced by
54 -- System.Tasking.Restricted.Stages.
56 package body System.Task_Primitives.Operations is
58 package OSC renames System.OS_Constants;
59 package SSL renames System.Soft_Links;
61 use System.Tasking.Debug;
64 use System.OS_Interface;
65 use System.Parameters;
66 use System.OS_Primitives;
72 -- The followings are logically constants, but need to be initialized
75 Single_RTS_Lock : aliased RTS_Lock;
76 -- This is a lock to allow only one thread of control in the RTS at
77 -- a time; it is used to execute in mutual exclusion from all other tasks.
78 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
80 Environment_Task_Id : Task_Id;
81 -- A variable to hold Task_Id for the environment task
83 Unblocked_Signal_Mask : aliased sigset_t;
84 -- The set of signals that should unblocked in all tasks
86 Time_Slice_Val : Integer;
87 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
89 Locking_Policy : Character;
90 pragma Import (C, Locking_Policy, "__gl_locking_policy");
92 Dispatching_Policy : Character;
93 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
97 Foreign_Task_Elaborated : aliased Boolean := True;
98 -- Used to identified fake tasks (i.e., non-Ada Threads)
100 Abort_Handler_Installed : Boolean := False;
101 -- True if a handler for the abort signal is installed
109 procedure Initialize (Environment_Task : Task_Id);
110 pragma Inline (Initialize);
111 -- Initialize various data needed by this package
113 function Is_Valid_Task return Boolean;
114 pragma Inline (Is_Valid_Task);
115 -- Does executing thread have a TCB?
117 procedure Set (Self_Id : Task_Id);
119 -- Set the self id for the current task
121 function Self return Task_Id;
122 pragma Inline (Self);
123 -- Return a pointer to the Ada Task Control Block of the calling task
127 package body Specific is separate;
128 -- The body of this package is target specific
130 ----------------------------------
131 -- ATCB allocation/deallocation --
132 ----------------------------------
134 package body ATCB_Allocation is separate;
135 -- The body of this package is shared across several targets
137 ---------------------------------
138 -- Support for foreign threads --
139 ---------------------------------
141 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
142 -- Allocate and initialize a new ATCB for the current Thread
144 function Register_Foreign_Thread
145 (Thread : Thread_Id) return Task_Id is separate;
147 -----------------------
148 -- Local Subprograms --
149 -----------------------
151 procedure Abort_Handler (Sig : Signal);
152 -- Signal handler used to implement asynchronous abort
154 function Get_Policy (Prio : System.Any_Priority) return Character;
155 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
156 -- Get priority specific dispatching policy
162 procedure Abort_Handler (Sig : Signal) is
163 pragma Unreferenced (Sig);
165 T : constant Task_Id := Self;
166 Old_Set : aliased sigset_t;
168 Result : Interfaces.C.int;
169 pragma Warnings (Off, Result);
172 -- It's not safe to raise an exception when using GCC ZCX mechanism.
173 -- Note that we still need to install a signal handler, since in some
174 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
175 -- need to send the Abort signal to a task.
177 if ZCX_By_Default then
181 if T.Deferral_Level = 0
182 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
183 and then not T.Aborting
187 -- Make sure signals used for RTS internal purpose are unmasked
192 Unblocked_Signal_Mask'Access,
194 pragma Assert (Result = 0);
196 raise Standard'Abort_Signal;
204 -- The underlying thread system sets a guard page at the bottom of a thread
205 -- stack, so nothing is needed.
207 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
208 pragma Unreferenced (T);
209 pragma Unreferenced (On);
218 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
220 return T.Common.LL.Thread;
227 function Self return Task_Id renames Specific.Self;
229 ---------------------
230 -- Initialize_Lock --
231 ---------------------
233 -- Note: mutexes and cond_variables needed per-task basis are initialized
234 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
235 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
236 -- status change of RTS. Therefore raising Storage_Error in the following
237 -- routines should be able to be handled safely.
239 procedure Initialize_Lock
240 (Prio : System.Any_Priority;
241 L : not null access Lock)
243 Attributes : aliased pthread_mutexattr_t;
244 Result : Interfaces.C.int;
247 Result := pthread_mutexattr_init (Attributes'Access);
248 pragma Assert (Result = 0 or else Result = ENOMEM);
250 if Result = ENOMEM then
254 if Locking_Policy = 'C' then
255 L.Ceiling := Interfaces.C.int (Prio);
258 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
259 pragma Assert (Result = 0 or else Result = ENOMEM);
261 if Result = ENOMEM then
262 Result := pthread_mutexattr_destroy (Attributes'Access);
266 Result := pthread_mutexattr_destroy (Attributes'Access);
267 pragma Assert (Result = 0);
270 procedure Initialize_Lock
271 (L : not null access RTS_Lock;
274 pragma Unreferenced (Level);
276 Attributes : aliased pthread_mutexattr_t;
277 Result : Interfaces.C.int;
280 Result := pthread_mutexattr_init (Attributes'Access);
281 pragma Assert (Result = 0 or else Result = ENOMEM);
283 if Result = ENOMEM then
287 Result := pthread_mutex_init (L, Attributes'Access);
288 pragma Assert (Result = 0 or else Result = ENOMEM);
290 if Result = ENOMEM then
291 Result := pthread_mutexattr_destroy (Attributes'Access);
295 Result := pthread_mutexattr_destroy (Attributes'Access);
296 pragma Assert (Result = 0);
303 procedure Finalize_Lock (L : not null access Lock) is
304 Result : Interfaces.C.int;
306 Result := pthread_mutex_destroy (L.L'Access);
307 pragma Assert (Result = 0);
310 procedure Finalize_Lock (L : not null access RTS_Lock) is
311 Result : Interfaces.C.int;
313 Result := pthread_mutex_destroy (L);
314 pragma Assert (Result = 0);
322 (L : not null access Lock;
323 Ceiling_Violation : out Boolean)
325 Result : Interfaces.C.int;
327 All_Tasks_Link : Task_Id;
328 Current_Prio : System.Any_Priority;
331 -- Perform ceiling checks only when this is the locking policy in use
333 if Locking_Policy = 'C' then
335 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
336 Current_Prio := Get_Priority (Self_ID);
338 -- If there is no other task, no need to check priorities
340 if All_Tasks_Link /= Null_Task
341 and then L.Ceiling < Interfaces.C.int (Current_Prio)
343 Ceiling_Violation := True;
348 Result := pthread_mutex_lock (L.L'Access);
349 pragma Assert (Result = 0);
351 Ceiling_Violation := False;
355 (L : not null access RTS_Lock;
356 Global_Lock : Boolean := False)
358 Result : Interfaces.C.int;
360 if not Single_Lock or else Global_Lock then
361 Result := pthread_mutex_lock (L);
362 pragma Assert (Result = 0);
366 procedure Write_Lock (T : Task_Id) is
367 Result : Interfaces.C.int;
369 if not Single_Lock then
370 Result := pthread_mutex_lock (T.Common.LL.L'Access);
371 pragma Assert (Result = 0);
380 (L : not null access Lock;
381 Ceiling_Violation : out Boolean)
384 Write_Lock (L, Ceiling_Violation);
391 procedure Unlock (L : not null access Lock) is
392 Result : Interfaces.C.int;
394 Result := pthread_mutex_unlock (L.L'Access);
395 pragma Assert (Result = 0);
399 (L : not null access RTS_Lock;
400 Global_Lock : Boolean := False)
402 Result : Interfaces.C.int;
404 if not Single_Lock or else Global_Lock then
405 Result := pthread_mutex_unlock (L);
406 pragma Assert (Result = 0);
410 procedure Unlock (T : Task_Id) is
411 Result : Interfaces.C.int;
413 if not Single_Lock then
414 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
415 pragma Assert (Result = 0);
423 -- Dynamic priority ceilings are not supported by the underlying system
425 procedure Set_Ceiling
426 (L : not null access Lock;
427 Prio : System.Any_Priority)
429 pragma Unreferenced (L, Prio);
440 Reason : System.Tasking.Task_States)
442 pragma Unreferenced (Reason);
444 Result : Interfaces.C.int;
449 (cond => Self_ID.Common.LL.CV'Access,
450 mutex => (if Single_Lock
451 then Single_RTS_Lock'Access
452 else Self_ID.Common.LL.L'Access));
454 -- EINTR is not considered a failure
456 pragma Assert (Result = 0 or else Result = EINTR);
463 -- This is for use within the run-time system, so abort is assumed to be
464 -- already deferred, and the caller should be holding its own ATCB lock.
466 procedure Timed_Sleep
469 Mode : ST.Delay_Modes;
470 Reason : System.Tasking.Task_States;
471 Timedout : out Boolean;
472 Yielded : out Boolean)
474 pragma Unreferenced (Reason);
476 Base_Time : constant Duration := Monotonic_Clock;
477 Check_Time : Duration := Base_Time;
479 Request : aliased timespec;
480 Result : Interfaces.C.int;
488 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
489 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
491 if Abs_Time > Check_Time then
492 Request := To_Timespec (Abs_Time);
495 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
498 pthread_cond_timedwait
499 (cond => Self_ID.Common.LL.CV'Access,
500 mutex => (if Single_Lock
501 then Single_RTS_Lock'Access
502 else Self_ID.Common.LL.L'Access),
503 abstime => Request'Access);
505 Check_Time := Monotonic_Clock;
506 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
508 if Result = 0 or Result = EINTR then
510 -- Somebody may have called Wakeup for us
516 pragma Assert (Result = ETIMEDOUT);
525 -- This is for use in implementing delay statements, so we assume the
526 -- caller is abort-deferred but is holding no locks.
528 procedure Timed_Delay
531 Mode : ST.Delay_Modes)
533 Base_Time : constant Duration := Monotonic_Clock;
534 Check_Time : Duration := Base_Time;
536 Request : aliased timespec;
537 Result : Interfaces.C.int;
544 Write_Lock (Self_ID);
548 then Time + Check_Time
549 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
551 if Abs_Time > Check_Time then
552 Request := To_Timespec (Abs_Time);
553 Self_ID.Common.State := Delay_Sleep;
556 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
559 pthread_cond_timedwait
560 (cond => Self_ID.Common.LL.CV'Access,
561 mutex => (if Single_Lock
562 then Single_RTS_Lock'Access
563 else Self_ID.Common.LL.L'Access),
564 abstime => Request'Access);
566 Check_Time := Monotonic_Clock;
567 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
569 pragma Assert (Result = 0 or else
570 Result = ETIMEDOUT or else
574 Self_ID.Common.State := Runnable;
586 ---------------------
587 -- Monotonic_Clock --
588 ---------------------
590 function Monotonic_Clock return Duration is
591 TS : aliased timespec;
592 Result : Interfaces.C.int;
594 Result := clock_gettime (OSC.CLOCK_RT_Ada, TS'Unchecked_Access);
595 pragma Assert (Result = 0);
596 return To_Duration (TS);
603 function RT_Resolution return Duration is
605 -- Returned value must be an integral multiple of Duration'Small (1 ns)
606 -- The following is the best approximation of 1/1024. The clock on the
607 -- DEC Alpha ticks at 1024 Hz.
609 return 0.000_976_563;
616 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
617 pragma Unreferenced (Reason);
618 Result : Interfaces.C.int;
620 Result := pthread_cond_signal (T.Common.LL.CV'Access);
621 pragma Assert (Result = 0);
628 procedure Yield (Do_Yield : Boolean := True) is
629 Result : Interfaces.C.int;
630 pragma Unreferenced (Result);
633 Result := sched_yield;
641 procedure Set_Priority
643 Prio : System.Any_Priority;
644 Loss_Of_Inheritance : Boolean := False)
646 pragma Unreferenced (Loss_Of_Inheritance);
648 Result : Interfaces.C.int;
649 Param : aliased struct_sched_param;
651 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
652 -- Upper case first character of the policy name corresponding to the
653 -- task as set by a Priority_Specific_Dispatching pragma.
656 T.Common.Current_Priority := Prio;
657 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
659 if Dispatching_Policy = 'R'
660 or else Priority_Specific_Policy = 'R'
661 or else Time_Slice_Val > 0
664 pthread_setschedparam
665 (T.Common.LL.Thread, SCHED_RR, Param'Access);
667 elsif Dispatching_Policy = 'F'
668 or else Priority_Specific_Policy = 'F'
669 or else Time_Slice_Val = 0
672 pthread_setschedparam
673 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
677 pthread_setschedparam
678 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
681 pragma Assert (Result = 0);
688 function Get_Priority (T : Task_Id) return System.Any_Priority is
690 return T.Common.Current_Priority;
697 procedure Enter_Task (Self_ID : Task_Id) is
699 Hide_Unhide_Yellow_Zone (Hide => True);
700 Self_ID.Common.LL.Thread := pthread_self;
702 Specific.Set (Self_ID);
709 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
711 -----------------------------
712 -- Register_Foreign_Thread --
713 -----------------------------
715 function Register_Foreign_Thread return Task_Id is
717 if Is_Valid_Task then
720 return Register_Foreign_Thread (pthread_self);
722 end Register_Foreign_Thread;
728 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
729 Mutex_Attr : aliased pthread_mutexattr_t;
730 Result : Interfaces.C.int;
731 Cond_Attr : aliased pthread_condattr_t;
734 if not Single_Lock then
735 Result := pthread_mutexattr_init (Mutex_Attr'Access);
736 pragma Assert (Result = 0 or else Result = ENOMEM);
741 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
742 pragma Assert (Result = 0 or else Result = ENOMEM);
750 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
751 pragma Assert (Result = 0);
754 Result := pthread_condattr_init (Cond_Attr'Access);
755 pragma Assert (Result = 0 or else Result = ENOMEM);
760 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
761 pragma Assert (Result = 0 or else Result = ENOMEM);
767 if not Single_Lock then
768 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
769 pragma Assert (Result = 0);
775 Result := pthread_condattr_destroy (Cond_Attr'Access);
776 pragma Assert (Result = 0);
783 procedure Create_Task
785 Wrapper : System.Address;
786 Stack_Size : System.Parameters.Size_Type;
787 Priority : System.Any_Priority;
788 Succeeded : out Boolean)
790 Attributes : aliased pthread_attr_t;
791 Adjusted_Stack_Size : Interfaces.C.size_t;
792 Result : Interfaces.C.int;
793 Param : aliased System.OS_Interface.struct_sched_param;
795 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
796 -- Upper case first character of the policy name corresponding to the
797 -- task as set by a Priority_Specific_Dispatching pragma.
799 use System.Task_Info;
802 -- Account for the Yellow Zone (2 pages) and the guard page right above.
803 -- See Hide_Unhide_Yellow_Zone for the rationale.
805 Adjusted_Stack_Size :=
806 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
808 Result := pthread_attr_init (Attributes'Access);
809 pragma Assert (Result = 0 or else Result = ENOMEM);
817 pthread_attr_setdetachstate
818 (Attributes'Access, PTHREAD_CREATE_DETACHED);
819 pragma Assert (Result = 0);
822 pthread_attr_setstacksize
823 (Attributes'Access, Adjusted_Stack_Size);
824 pragma Assert (Result = 0);
826 Param.sched_priority :=
827 Interfaces.C.int (Underlying_Priorities (Priority));
829 pthread_attr_setschedparam
830 (Attributes'Access, Param'Access);
831 pragma Assert (Result = 0);
833 if Dispatching_Policy = 'R'
834 or else Priority_Specific_Policy = 'R'
835 or else Time_Slice_Val > 0
838 pthread_attr_setschedpolicy
839 (Attributes'Access, System.OS_Interface.SCHED_RR);
841 elsif Dispatching_Policy = 'F'
842 or else Priority_Specific_Policy = 'F'
843 or else Time_Slice_Val = 0
846 pthread_attr_setschedpolicy
847 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
851 pthread_attr_setschedpolicy
852 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
855 pragma Assert (Result = 0);
857 -- Set the scheduling parameters explicitly, since this is the only way
858 -- to force the OS to take e.g. the sched policy and scope attributes
862 pthread_attr_setinheritsched
863 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
864 pragma Assert (Result = 0);
866 T.Common.Current_Priority := Priority;
868 if T.Common.Task_Info /= null then
869 case T.Common.Task_Info.Contention_Scope is
870 when System.Task_Info.Process_Scope =>
872 pthread_attr_setscope
873 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
875 when System.Task_Info.System_Scope =>
877 pthread_attr_setscope
878 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
880 when System.Task_Info.Default_Scope =>
884 pragma Assert (Result = 0);
887 -- Since the initial signal mask of a thread is inherited from the
888 -- creator, and the Environment task has all its signals masked, we
889 -- do not need to manipulate caller's signal mask at this point.
890 -- All tasks in RTS will have All_Tasks_Mask initially.
892 -- Note: the use of Unrestricted_Access in the following call is needed
893 -- because otherwise we have an error of getting a access-to-volatile
894 -- value which points to a non-volatile object. But in this case it is
895 -- safe to do this, since we know we have no problems with aliasing and
896 -- Unrestricted_Access bypasses this check.
900 (T.Common.LL.Thread'Unrestricted_Access,
902 Thread_Body_Access (Wrapper),
904 pragma Assert (Result = 0 or else Result = EAGAIN);
906 Succeeded := Result = 0;
908 Result := pthread_attr_destroy (Attributes'Access);
909 pragma Assert (Result = 0);
911 if Succeeded and then T.Common.Task_Info /= null then
913 -- ??? We're using a process-wide function to implement a task
914 -- specific characteristic.
916 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
917 Result := bind_to_cpu (Curpid, 0);
919 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
923 Interfaces.C.unsigned_long (
924 Interfaces.Shift_Left
925 (Interfaces.Unsigned_64'(1),
926 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
927 pragma Assert (Result = 0);
936 procedure Finalize_TCB (T : Task_Id) is
937 Result : Interfaces.C.int;
940 if not Single_Lock then
941 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
942 pragma Assert (Result = 0);
945 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
946 pragma Assert (Result = 0);
948 if T.Known_Tasks_Index /= -1 then
949 Known_Tasks (T.Known_Tasks_Index) := null;
952 ATCB_Allocation.Free_ATCB (T);
959 procedure Exit_Task is
962 Hide_Unhide_Yellow_Zone (Hide => False);
969 procedure Abort_Task (T : Task_Id) is
970 Result : Interfaces.C.int;
972 if Abort_Handler_Installed then
973 Result := pthread_kill (T.Common.LL.Thread,
974 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
975 pragma Assert (Result = 0);
983 procedure Initialize (S : in out Suspension_Object) is
984 Mutex_Attr : aliased pthread_mutexattr_t;
985 Cond_Attr : aliased pthread_condattr_t;
986 Result : Interfaces.C.int;
989 -- Initialize internal state (always to False (RM D.10(6)))
994 -- Initialize internal mutex
996 Result := pthread_mutexattr_init (Mutex_Attr'Access);
997 pragma Assert (Result = 0 or else Result = ENOMEM);
999 if Result = ENOMEM then
1000 raise Storage_Error;
1003 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1004 pragma Assert (Result = 0 or else Result = ENOMEM);
1006 if Result = ENOMEM then
1007 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1008 raise Storage_Error;
1011 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1012 pragma Assert (Result = 0);
1014 -- Initialize internal condition variable
1016 Result := pthread_condattr_init (Cond_Attr'Access);
1017 pragma Assert (Result = 0 or else Result = ENOMEM);
1019 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1021 pragma Assert (Result = 0 or else Result = ENOMEM);
1024 Result := pthread_mutex_destroy (S.L'Access);
1025 pragma Assert (Result = 0);
1027 if Result = ENOMEM then
1028 raise Storage_Error;
1037 procedure Finalize (S : in out Suspension_Object) is
1038 Result : Interfaces.C.int;
1041 -- Destroy internal mutex
1043 Result := pthread_mutex_destroy (S.L'Access);
1044 pragma Assert (Result = 0);
1046 -- Destroy internal condition variable
1048 Result := pthread_cond_destroy (S.CV'Access);
1049 pragma Assert (Result = 0);
1056 function Current_State (S : Suspension_Object) return Boolean is
1058 -- We do not want to use lock on this read operation. State is marked
1059 -- as Atomic so that we ensure that the value retrieved is correct.
1068 procedure Set_False (S : in out Suspension_Object) is
1069 Result : Interfaces.C.int;
1072 SSL.Abort_Defer.all;
1074 Result := pthread_mutex_lock (S.L'Access);
1075 pragma Assert (Result = 0);
1079 Result := pthread_mutex_unlock (S.L'Access);
1080 pragma Assert (Result = 0);
1082 SSL.Abort_Undefer.all;
1089 procedure Set_True (S : in out Suspension_Object) is
1090 Result : Interfaces.C.int;
1093 SSL.Abort_Defer.all;
1095 Result := pthread_mutex_lock (S.L'Access);
1096 pragma Assert (Result = 0);
1098 -- If there is already a task waiting on this suspension object then we
1099 -- resume it, leaving the state of the suspension object to False, as
1100 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1106 Result := pthread_cond_signal (S.CV'Access);
1107 pragma Assert (Result = 0);
1113 Result := pthread_mutex_unlock (S.L'Access);
1114 pragma Assert (Result = 0);
1116 SSL.Abort_Undefer.all;
1119 ------------------------
1120 -- Suspend_Until_True --
1121 ------------------------
1123 procedure Suspend_Until_True (S : in out Suspension_Object) is
1124 Result : Interfaces.C.int;
1127 SSL.Abort_Defer.all;
1129 Result := pthread_mutex_lock (S.L'Access);
1130 pragma Assert (Result = 0);
1134 -- Program_Error must be raised upon calling Suspend_Until_True
1135 -- if another task is already waiting on that suspension object
1138 Result := pthread_mutex_unlock (S.L'Access);
1139 pragma Assert (Result = 0);
1141 SSL.Abort_Undefer.all;
1143 raise Program_Error;
1146 -- Suspend the task if the state is False. Otherwise, the task
1147 -- continues its execution, and the state of the suspension object
1148 -- is set to False (RM D.10(9)).
1156 -- Loop in case pthread_cond_wait returns earlier than expected
1157 -- (e.g. in case of EINTR caused by a signal).
1159 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1160 pragma Assert (Result = 0 or else Result = EINTR);
1162 exit when not S.Waiting;
1166 Result := pthread_mutex_unlock (S.L'Access);
1167 pragma Assert (Result = 0);
1169 SSL.Abort_Undefer.all;
1171 end Suspend_Until_True;
1179 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1180 pragma Unreferenced (Self_ID);
1185 --------------------
1186 -- Check_No_Locks --
1187 --------------------
1189 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1190 pragma Unreferenced (Self_ID);
1195 ----------------------
1196 -- Environment_Task --
1197 ----------------------
1199 function Environment_Task return Task_Id is
1201 return Environment_Task_Id;
1202 end Environment_Task;
1208 procedure Lock_RTS is
1210 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1217 procedure Unlock_RTS is
1219 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1226 function Suspend_Task
1228 Thread_Self : Thread_Id) return Boolean
1230 pragma Unreferenced (T, Thread_Self);
1239 function Resume_Task
1241 Thread_Self : Thread_Id) return Boolean
1243 pragma Unreferenced (T, Thread_Self);
1248 --------------------
1249 -- Stop_All_Tasks --
1250 --------------------
1252 procedure Stop_All_Tasks is
1261 function Stop_Task (T : ST.Task_Id) return Boolean is
1262 pragma Unreferenced (T);
1271 function Continue_Task (T : ST.Task_Id) return Boolean is
1272 pragma Unreferenced (T);
1281 procedure Initialize (Environment_Task : Task_Id) is
1282 act : aliased struct_sigaction;
1283 old_act : aliased struct_sigaction;
1284 Tmp_Set : aliased sigset_t;
1285 Result : Interfaces.C.int;
1288 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1289 pragma Import (C, State, "__gnat_get_interrupt_state");
1290 -- Get interrupt state. Defined in a-init.c. The input argument is
1291 -- the interrupt number, and the result is one of the following:
1293 Default : constant Character := 's';
1294 -- 'n' this interrupt not set by any Interrupt_State pragma
1295 -- 'u' Interrupt_State pragma set state to User
1296 -- 'r' Interrupt_State pragma set state to Runtime
1297 -- 's' Interrupt_State pragma set state to System (use "default"
1301 Environment_Task_Id := Environment_Task;
1303 Interrupt_Management.Initialize;
1305 -- Prepare the set of signals that should unblocked in all tasks
1307 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1308 pragma Assert (Result = 0);
1310 for J in Interrupt_Management.Interrupt_ID loop
1311 if System.Interrupt_Management.Keep_Unmasked (J) then
1312 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1313 pragma Assert (Result = 0);
1319 -- Initialize the lock used to synchronize chain of all ATCBs
1321 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1323 Specific.Initialize (Environment_Task);
1325 -- Make environment task known here because it doesn't go through
1326 -- Activate_Tasks, which does it for all other tasks.
1328 Known_Tasks (Known_Tasks'First) := Environment_Task;
1329 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1331 Enter_Task (Environment_Task);
1334 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1337 act.sa_handler := Abort_Handler'Address;
1339 Result := sigemptyset (Tmp_Set'Access);
1340 pragma Assert (Result = 0);
1341 act.sa_mask := Tmp_Set;
1345 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1346 act'Unchecked_Access,
1347 old_act'Unchecked_Access);
1348 pragma Assert (Result = 0);
1349 Abort_Handler_Installed := True;
1353 -----------------------
1354 -- Set_Task_Affinity --
1355 -----------------------
1357 procedure Set_Task_Affinity (T : ST.Task_Id) is
1358 pragma Unreferenced (T);
1361 -- Setting task affinity is not supported by the underlying system
1364 end Set_Task_Affinity;
1365 end System.Task_Primitives.Operations;