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 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.
41 with Ada.Unchecked_Deallocation;
46 with System.Tasking.Debug;
47 with System.Interrupt_Management;
48 with System.OS_Primitives;
49 with System.Task_Info;
51 with System.Soft_Links;
52 -- We use System.Soft_Links instead of System.Tasking.Initialization
53 -- because the later is a higher level package that we shouldn't depend on.
54 -- For example when using the restricted run time, it is replaced by
55 -- System.Tasking.Restricted.Stages.
57 package body System.Task_Primitives.Operations is
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 ATCB_Key : aliased pthread_key_t;
81 -- Key used to find the Ada Task_Id associated with a thread
83 Environment_Task_Id : Task_Id;
84 -- A variable to hold Task_Id for the environment task
86 Unblocked_Signal_Mask : aliased sigset_t;
87 -- The set of signals that should unblocked in all tasks
89 Time_Slice_Val : Integer;
90 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
92 Locking_Policy : Character;
93 pragma Import (C, Locking_Policy, "__gl_locking_policy");
95 Dispatching_Policy : Character;
96 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
100 Foreign_Task_Elaborated : aliased Boolean := True;
101 -- Used to identified fake tasks (i.e., non-Ada Threads)
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 -- Support for foreign threads --
132 ---------------------------------
134 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
135 -- Allocate and initialize a new ATCB for the current Thread
137 function Register_Foreign_Thread
138 (Thread : Thread_Id) return Task_Id is separate;
140 -----------------------
141 -- Local Subprograms --
142 -----------------------
144 procedure Abort_Handler (Sig : Signal);
145 -- Signal handler used to implement asynchronous abort
147 function Get_Policy (Prio : System.Any_Priority) return Character;
148 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
149 -- Get priority specific dispatching policy
155 procedure Abort_Handler (Sig : Signal) is
156 pragma Unreferenced (Sig);
158 T : constant Task_Id := Self;
159 Old_Set : aliased sigset_t;
161 Result : Interfaces.C.int;
162 pragma Warnings (Off, Result);
165 -- It is not safe to raise an exception when using ZCX and the GCC
166 -- exception handling mechanism.
168 if ZCX_By_Default and then GCC_ZCX_Support then
172 if T.Deferral_Level = 0
173 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
174 and then not T.Aborting
178 -- Make sure signals used for RTS internal purpose are unmasked
183 Unblocked_Signal_Mask'Access,
185 pragma Assert (Result = 0);
187 raise Standard'Abort_Signal;
195 -- The underlying thread system sets a guard page at the bottom of a thread
196 -- stack, so nothing is needed.
198 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
199 pragma Unreferenced (T);
200 pragma Unreferenced (On);
209 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
211 return T.Common.LL.Thread;
218 function Self return Task_Id renames Specific.Self;
220 ---------------------
221 -- Initialize_Lock --
222 ---------------------
224 -- Note: mutexes and cond_variables needed per-task basis are initialized
225 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
226 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
227 -- status change of RTS. Therefore raising Storage_Error in the following
228 -- routines should be able to be handled safely.
230 procedure Initialize_Lock
231 (Prio : System.Any_Priority;
232 L : not null access Lock)
234 Attributes : aliased pthread_mutexattr_t;
235 Result : Interfaces.C.int;
238 Result := pthread_mutexattr_init (Attributes'Access);
239 pragma Assert (Result = 0 or else Result = ENOMEM);
241 if Result = ENOMEM then
245 if Locking_Policy = 'C' then
246 L.Ceiling := Interfaces.C.int (Prio);
249 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
250 pragma Assert (Result = 0 or else Result = ENOMEM);
252 if Result = ENOMEM then
253 Result := pthread_mutexattr_destroy (Attributes'Access);
257 Result := pthread_mutexattr_destroy (Attributes'Access);
258 pragma Assert (Result = 0);
261 procedure Initialize_Lock
262 (L : not null access RTS_Lock;
265 pragma Unreferenced (Level);
267 Attributes : aliased pthread_mutexattr_t;
268 Result : Interfaces.C.int;
271 Result := pthread_mutexattr_init (Attributes'Access);
272 pragma Assert (Result = 0 or else Result = ENOMEM);
274 if Result = ENOMEM then
278 Result := pthread_mutex_init (L, Attributes'Access);
279 pragma Assert (Result = 0 or else Result = ENOMEM);
281 if Result = ENOMEM then
282 Result := pthread_mutexattr_destroy (Attributes'Access);
286 Result := pthread_mutexattr_destroy (Attributes'Access);
287 pragma Assert (Result = 0);
294 procedure Finalize_Lock (L : not null access Lock) is
295 Result : Interfaces.C.int;
297 Result := pthread_mutex_destroy (L.L'Access);
298 pragma Assert (Result = 0);
301 procedure Finalize_Lock (L : not null access RTS_Lock) is
302 Result : Interfaces.C.int;
304 Result := pthread_mutex_destroy (L);
305 pragma Assert (Result = 0);
313 (L : not null access Lock;
314 Ceiling_Violation : out Boolean)
316 Result : Interfaces.C.int;
318 All_Tasks_Link : Task_Id;
319 Current_Prio : System.Any_Priority;
322 -- Perform ceiling checks only when this is the locking policy in use
324 if Locking_Policy = 'C' then
326 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
327 Current_Prio := Get_Priority (Self_ID);
329 -- If there is no other task, no need to check priorities
331 if All_Tasks_Link /= Null_Task
332 and then L.Ceiling < Interfaces.C.int (Current_Prio)
334 Ceiling_Violation := True;
339 Result := pthread_mutex_lock (L.L'Access);
340 pragma Assert (Result = 0);
342 Ceiling_Violation := False;
346 (L : not null access RTS_Lock;
347 Global_Lock : Boolean := False)
349 Result : Interfaces.C.int;
351 if not Single_Lock or else Global_Lock then
352 Result := pthread_mutex_lock (L);
353 pragma Assert (Result = 0);
357 procedure Write_Lock (T : Task_Id) is
358 Result : Interfaces.C.int;
360 if not Single_Lock then
361 Result := pthread_mutex_lock (T.Common.LL.L'Access);
362 pragma Assert (Result = 0);
371 (L : not null access Lock;
372 Ceiling_Violation : out Boolean)
375 Write_Lock (L, Ceiling_Violation);
382 procedure Unlock (L : not null access Lock) is
383 Result : Interfaces.C.int;
385 Result := pthread_mutex_unlock (L.L'Access);
386 pragma Assert (Result = 0);
390 (L : not null access RTS_Lock;
391 Global_Lock : Boolean := False)
393 Result : Interfaces.C.int;
395 if not Single_Lock or else Global_Lock then
396 Result := pthread_mutex_unlock (L);
397 pragma Assert (Result = 0);
401 procedure Unlock (T : Task_Id) is
402 Result : Interfaces.C.int;
404 if not Single_Lock then
405 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
406 pragma Assert (Result = 0);
414 -- Dynamic priority ceilings are not supported by the underlying system
416 procedure Set_Ceiling
417 (L : not null access Lock;
418 Prio : System.Any_Priority)
420 pragma Unreferenced (L, Prio);
431 Reason : System.Tasking.Task_States)
433 pragma Unreferenced (Reason);
435 Result : Interfaces.C.int;
441 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
445 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
448 -- EINTR is not considered a failure
450 pragma Assert (Result = 0 or else Result = EINTR);
457 -- This is for use within the run-time system, so abort is assumed to be
458 -- already deferred, and the caller should be holding its own ATCB lock.
460 procedure Timed_Sleep
463 Mode : ST.Delay_Modes;
464 Reason : System.Tasking.Task_States;
465 Timedout : out Boolean;
466 Yielded : out Boolean)
468 pragma Unreferenced (Reason);
470 Base_Time : constant Duration := Monotonic_Clock;
471 Check_Time : Duration := Base_Time;
473 Request : aliased timespec;
474 Result : Interfaces.C.int;
480 if Mode = Relative then
481 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
483 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
486 if Abs_Time > Check_Time then
487 Request := To_Timespec (Abs_Time);
490 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
494 pthread_cond_timedwait
495 (Self_ID.Common.LL.CV'Access,
496 Single_RTS_Lock'Access,
501 pthread_cond_timedwait
502 (Self_ID.Common.LL.CV'Access,
503 Self_ID.Common.LL.L'Access,
507 Check_Time := Monotonic_Clock;
508 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
510 if Result = 0 or Result = EINTR then
512 -- Somebody may have called Wakeup for us
518 pragma Assert (Result = ETIMEDOUT);
527 -- This is for use in implementing delay statements, so we assume the
528 -- caller is abort-deferred but is holding no locks.
530 procedure Timed_Delay
533 Mode : ST.Delay_Modes)
535 Base_Time : constant Duration := Monotonic_Clock;
536 Check_Time : Duration := Base_Time;
538 Request : aliased timespec;
539 Result : Interfaces.C.int;
546 Write_Lock (Self_ID);
548 if Mode = Relative then
549 Abs_Time := Time + Check_Time;
551 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
554 if Abs_Time > Check_Time then
555 Request := To_Timespec (Abs_Time);
556 Self_ID.Common.State := Delay_Sleep;
559 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
563 pthread_cond_timedwait
564 (Self_ID.Common.LL.CV'Access,
565 Single_RTS_Lock'Access,
569 pthread_cond_timedwait
570 (Self_ID.Common.LL.CV'Access,
571 Self_ID.Common.LL.L'Access,
575 Check_Time := Monotonic_Clock;
576 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
578 pragma Assert (Result = 0 or else
579 Result = ETIMEDOUT or else
583 Self_ID.Common.State := Runnable;
595 ---------------------
596 -- Monotonic_Clock --
597 ---------------------
599 function Monotonic_Clock return Duration is
600 TS : aliased timespec;
601 Result : Interfaces.C.int;
603 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
604 pragma Assert (Result = 0);
605 return To_Duration (TS);
612 function RT_Resolution return Duration is
614 -- Returned value must be an integral multiple of Duration'Small (1 ns)
615 -- The following is the best approximation of 1/1024. The clock on the
616 -- DEC Alpha ticks at 1024 Hz.
618 return 0.000_976_563;
625 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
626 pragma Unreferenced (Reason);
627 Result : Interfaces.C.int;
629 Result := pthread_cond_signal (T.Common.LL.CV'Access);
630 pragma Assert (Result = 0);
637 procedure Yield (Do_Yield : Boolean := True) is
638 Result : Interfaces.C.int;
639 pragma Unreferenced (Result);
642 Result := sched_yield;
650 procedure Set_Priority
652 Prio : System.Any_Priority;
653 Loss_Of_Inheritance : Boolean := False)
655 pragma Unreferenced (Loss_Of_Inheritance);
657 Result : Interfaces.C.int;
658 Param : aliased struct_sched_param;
660 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
661 -- Upper case first character of the policy name corresponding to the
662 -- task as set by a Priority_Specific_Dispatching pragma.
665 T.Common.Current_Priority := Prio;
666 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
668 if Dispatching_Policy = 'R'
669 or else Priority_Specific_Policy = 'R'
670 or else Time_Slice_Val > 0
673 pthread_setschedparam
674 (T.Common.LL.Thread, SCHED_RR, Param'Access);
676 elsif Dispatching_Policy = 'F'
677 or else Priority_Specific_Policy = 'F'
678 or else Time_Slice_Val = 0
681 pthread_setschedparam
682 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
686 pthread_setschedparam
687 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
690 pragma Assert (Result = 0);
697 function Get_Priority (T : Task_Id) return System.Any_Priority is
699 return T.Common.Current_Priority;
706 procedure Enter_Task (Self_ID : Task_Id) is
708 Hide_Unhide_Yellow_Zone (Hide => True);
709 Self_ID.Common.LL.Thread := pthread_self;
710 Specific.Set (Self_ID);
714 for J in Known_Tasks'Range loop
715 if Known_Tasks (J) = null then
716 Known_Tasks (J) := Self_ID;
717 Self_ID.Known_Tasks_Index := J;
729 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
731 return new Ada_Task_Control_Block (Entry_Num);
738 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
740 -----------------------------
741 -- Register_Foreign_Thread --
742 -----------------------------
744 function Register_Foreign_Thread return Task_Id is
746 if Is_Valid_Task then
749 return Register_Foreign_Thread (pthread_self);
751 end Register_Foreign_Thread;
757 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
758 Mutex_Attr : aliased pthread_mutexattr_t;
759 Result : Interfaces.C.int;
760 Cond_Attr : aliased pthread_condattr_t;
763 if not Single_Lock then
764 Result := pthread_mutexattr_init (Mutex_Attr'Access);
765 pragma Assert (Result = 0 or else Result = ENOMEM);
770 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
771 pragma Assert (Result = 0 or else Result = ENOMEM);
779 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
780 pragma Assert (Result = 0);
783 Result := pthread_condattr_init (Cond_Attr'Access);
784 pragma Assert (Result = 0 or else Result = ENOMEM);
789 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
790 pragma Assert (Result = 0 or else Result = ENOMEM);
796 if not Single_Lock then
797 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
798 pragma Assert (Result = 0);
804 Result := pthread_condattr_destroy (Cond_Attr'Access);
805 pragma Assert (Result = 0);
812 procedure Create_Task
814 Wrapper : System.Address;
815 Stack_Size : System.Parameters.Size_Type;
816 Priority : System.Any_Priority;
817 Succeeded : out Boolean)
819 Attributes : aliased pthread_attr_t;
820 Adjusted_Stack_Size : Interfaces.C.size_t;
821 Result : Interfaces.C.int;
822 Param : aliased System.OS_Interface.struct_sched_param;
824 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
825 -- Upper case first character of the policy name corresponding to the
826 -- task as set by a Priority_Specific_Dispatching pragma.
828 use System.Task_Info;
831 -- Account for the Yellow Zone (2 pages) and the guard page right above.
832 -- See Hide_Unhide_Yellow_Zone for the rationale.
834 Adjusted_Stack_Size :=
835 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
837 Result := pthread_attr_init (Attributes'Access);
838 pragma Assert (Result = 0 or else Result = ENOMEM);
846 pthread_attr_setdetachstate
847 (Attributes'Access, PTHREAD_CREATE_DETACHED);
848 pragma Assert (Result = 0);
851 pthread_attr_setstacksize
852 (Attributes'Access, Adjusted_Stack_Size);
853 pragma Assert (Result = 0);
855 Param.sched_priority :=
856 Interfaces.C.int (Underlying_Priorities (Priority));
858 pthread_attr_setschedparam
859 (Attributes'Access, Param'Access);
860 pragma Assert (Result = 0);
862 if Dispatching_Policy = 'R'
863 or else Priority_Specific_Policy = 'R'
864 or else Time_Slice_Val > 0
867 pthread_attr_setschedpolicy
868 (Attributes'Access, System.OS_Interface.SCHED_RR);
870 elsif Dispatching_Policy = 'F'
871 or else Priority_Specific_Policy = 'F'
872 or else Time_Slice_Val = 0
875 pthread_attr_setschedpolicy
876 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
880 pthread_attr_setschedpolicy
881 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
884 pragma Assert (Result = 0);
886 -- Set the scheduling parameters explicitly, since this is the only way
887 -- to force the OS to take e.g. the sched policy and scope attributes
891 pthread_attr_setinheritsched
892 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
893 pragma Assert (Result = 0);
895 T.Common.Current_Priority := Priority;
897 if T.Common.Task_Info /= null then
898 case T.Common.Task_Info.Contention_Scope is
899 when System.Task_Info.Process_Scope =>
901 pthread_attr_setscope
902 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
904 when System.Task_Info.System_Scope =>
906 pthread_attr_setscope
907 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
909 when System.Task_Info.Default_Scope =>
913 pragma Assert (Result = 0);
916 -- Since the initial signal mask of a thread is inherited from the
917 -- creator, and the Environment task has all its signals masked, we
918 -- do not need to manipulate caller's signal mask at this point.
919 -- All tasks in RTS will have All_Tasks_Mask initially.
923 (T.Common.LL.Thread'Access,
925 Thread_Body_Access (Wrapper),
927 pragma Assert (Result = 0 or else Result = EAGAIN);
929 Succeeded := Result = 0;
931 Result := pthread_attr_destroy (Attributes'Access);
932 pragma Assert (Result = 0);
934 if Succeeded and then T.Common.Task_Info /= null then
936 -- ??? We're using a process-wide function to implement a task
937 -- specific characteristic.
939 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
940 Result := bind_to_cpu (Curpid, 0);
942 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
946 Interfaces.C.unsigned_long (
947 Interfaces.Shift_Left
948 (Interfaces.Unsigned_64'(1),
949 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
950 pragma Assert (Result = 0);
959 procedure Finalize_TCB (T : Task_Id) is
960 Result : Interfaces.C.int;
962 Is_Self : constant Boolean := T = Self;
964 procedure Free is new
965 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
968 if not Single_Lock then
969 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
970 pragma Assert (Result = 0);
973 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
974 pragma Assert (Result = 0);
976 if T.Known_Tasks_Index /= -1 then
977 Known_Tasks (T.Known_Tasks_Index) := null;
991 procedure Exit_Task is
994 Hide_Unhide_Yellow_Zone (Hide => False);
1001 procedure Abort_Task (T : Task_Id) is
1002 Result : Interfaces.C.int;
1004 Result := pthread_kill (T.Common.LL.Thread,
1005 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1006 pragma Assert (Result = 0);
1013 procedure Initialize (S : in out Suspension_Object) is
1014 Mutex_Attr : aliased pthread_mutexattr_t;
1015 Cond_Attr : aliased pthread_condattr_t;
1016 Result : Interfaces.C.int;
1019 -- Initialize internal state (always to False (RM D.10(6)))
1024 -- Initialize internal mutex
1026 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1027 pragma Assert (Result = 0 or else Result = ENOMEM);
1029 if Result = ENOMEM then
1030 raise Storage_Error;
1033 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1034 pragma Assert (Result = 0 or else Result = ENOMEM);
1036 if Result = ENOMEM then
1037 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1038 raise Storage_Error;
1041 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1042 pragma Assert (Result = 0);
1044 -- Initialize internal condition variable
1046 Result := pthread_condattr_init (Cond_Attr'Access);
1047 pragma Assert (Result = 0 or else Result = ENOMEM);
1049 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1051 pragma Assert (Result = 0 or else Result = ENOMEM);
1054 Result := pthread_mutex_destroy (S.L'Access);
1055 pragma Assert (Result = 0);
1057 if Result = ENOMEM then
1058 raise Storage_Error;
1067 procedure Finalize (S : in out Suspension_Object) is
1068 Result : Interfaces.C.int;
1071 -- Destroy internal mutex
1073 Result := pthread_mutex_destroy (S.L'Access);
1074 pragma Assert (Result = 0);
1076 -- Destroy internal condition variable
1078 Result := pthread_cond_destroy (S.CV'Access);
1079 pragma Assert (Result = 0);
1086 function Current_State (S : Suspension_Object) return Boolean is
1088 -- We do not want to use lock on this read operation. State is marked
1089 -- as Atomic so that we ensure that the value retrieved is correct.
1098 procedure Set_False (S : in out Suspension_Object) is
1099 Result : Interfaces.C.int;
1102 SSL.Abort_Defer.all;
1104 Result := pthread_mutex_lock (S.L'Access);
1105 pragma Assert (Result = 0);
1109 Result := pthread_mutex_unlock (S.L'Access);
1110 pragma Assert (Result = 0);
1112 SSL.Abort_Undefer.all;
1119 procedure Set_True (S : in out Suspension_Object) is
1120 Result : Interfaces.C.int;
1123 SSL.Abort_Defer.all;
1125 Result := pthread_mutex_lock (S.L'Access);
1126 pragma Assert (Result = 0);
1128 -- If there is already a task waiting on this suspension object then we
1129 -- resume it, leaving the state of the suspension object to False, as
1130 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1136 Result := pthread_cond_signal (S.CV'Access);
1137 pragma Assert (Result = 0);
1143 Result := pthread_mutex_unlock (S.L'Access);
1144 pragma Assert (Result = 0);
1146 SSL.Abort_Undefer.all;
1149 ------------------------
1150 -- Suspend_Until_True --
1151 ------------------------
1153 procedure Suspend_Until_True (S : in out Suspension_Object) is
1154 Result : Interfaces.C.int;
1157 SSL.Abort_Defer.all;
1159 Result := pthread_mutex_lock (S.L'Access);
1160 pragma Assert (Result = 0);
1164 -- Program_Error must be raised upon calling Suspend_Until_True
1165 -- if another task is already waiting on that suspension object
1168 Result := pthread_mutex_unlock (S.L'Access);
1169 pragma Assert (Result = 0);
1171 SSL.Abort_Undefer.all;
1173 raise Program_Error;
1176 -- Suspend the task if the state is False. Otherwise, the task
1177 -- continues its execution, and the state of the suspension object
1178 -- is set to False (RM D.10(9)).
1184 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1187 Result := pthread_mutex_unlock (S.L'Access);
1188 pragma Assert (Result = 0);
1190 SSL.Abort_Undefer.all;
1192 end Suspend_Until_True;
1200 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1201 pragma Unreferenced (Self_ID);
1206 --------------------
1207 -- Check_No_Locks --
1208 --------------------
1210 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1211 pragma Unreferenced (Self_ID);
1216 ----------------------
1217 -- Environment_Task --
1218 ----------------------
1220 function Environment_Task return Task_Id is
1222 return Environment_Task_Id;
1223 end Environment_Task;
1229 procedure Lock_RTS is
1231 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1238 procedure Unlock_RTS is
1240 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1247 function Suspend_Task
1249 Thread_Self : Thread_Id) return Boolean
1251 pragma Unreferenced (T, Thread_Self);
1260 function Resume_Task
1262 Thread_Self : Thread_Id) return Boolean
1264 pragma Unreferenced (T, Thread_Self);
1269 --------------------
1270 -- Stop_All_Tasks --
1271 --------------------
1273 procedure Stop_All_Tasks is
1282 function Stop_Task (T : ST.Task_Id) return Boolean is
1283 pragma Unreferenced (T);
1292 function Continue_Task (T : ST.Task_Id) return Boolean is
1293 pragma Unreferenced (T);
1302 procedure Initialize (Environment_Task : Task_Id) is
1303 act : aliased struct_sigaction;
1304 old_act : aliased struct_sigaction;
1305 Tmp_Set : aliased sigset_t;
1306 Result : Interfaces.C.int;
1309 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1310 pragma Import (C, State, "__gnat_get_interrupt_state");
1311 -- Get interrupt state. Defined in a-init.c. The input argument is
1312 -- the interrupt number, and the result is one of the following:
1314 Default : constant Character := 's';
1315 -- 'n' this interrupt not set by any Interrupt_State pragma
1316 -- 'u' Interrupt_State pragma set state to User
1317 -- 'r' Interrupt_State pragma set state to Runtime
1318 -- 's' Interrupt_State pragma set state to System (use "default"
1322 Environment_Task_Id := Environment_Task;
1324 Interrupt_Management.Initialize;
1326 -- Prepare the set of signals that should unblocked in all tasks
1328 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1329 pragma Assert (Result = 0);
1331 for J in Interrupt_Management.Interrupt_ID loop
1332 if System.Interrupt_Management.Keep_Unmasked (J) then
1333 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1334 pragma Assert (Result = 0);
1340 -- Initialize the lock used to synchronize chain of all ATCBs
1342 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1344 Specific.Initialize (Environment_Task);
1346 Enter_Task (Environment_Task);
1348 -- Install the abort-signal handler
1351 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1354 act.sa_handler := Abort_Handler'Address;
1356 Result := sigemptyset (Tmp_Set'Access);
1357 pragma Assert (Result = 0);
1358 act.sa_mask := Tmp_Set;
1362 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1363 act'Unchecked_Access,
1364 old_act'Unchecked_Access);
1365 pragma Assert (Result = 0);
1369 end System.Task_Primitives.Operations;