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-2007, 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 DEC Unix 4.0d version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.Interrupt_Management;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
51 with System.OS_Primitives;
52 -- used for Delay_Modes
54 with System.Task_Info;
55 -- used for Task_Info_Type
58 -- used for Shift_Left
64 with System.Soft_Links;
65 -- used for Abort_Defer/Undefer
67 -- We use System.Soft_Links instead of System.Tasking.Initialization
68 -- because the later is a higher level package that we shouldn't depend on.
69 -- For example when using the restricted run time, it is replaced by
70 -- System.Tasking.Restricted.Stages.
72 with Ada.Unchecked_Deallocation;
74 package body System.Task_Primitives.Operations is
76 package SSL renames System.Soft_Links;
78 use System.Tasking.Debug;
81 use System.OS_Interface;
82 use System.Parameters;
83 use System.OS_Primitives;
89 -- The followings are logically constants, but need to be initialized
92 Single_RTS_Lock : aliased RTS_Lock;
93 -- This is a lock to allow only one thread of control in the RTS at
94 -- a time; it is used to execute in mutual exclusion from all other tasks.
95 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
97 ATCB_Key : aliased pthread_key_t;
98 -- Key used to find the Ada Task_Id associated with a thread
100 Environment_Task_Id : Task_Id;
101 -- A variable to hold Task_Id for the environment task
103 Unblocked_Signal_Mask : aliased sigset_t;
104 -- The set of signals that should unblocked in all tasks
106 Time_Slice_Val : Integer;
107 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
109 Locking_Policy : Character;
110 pragma Import (C, Locking_Policy, "__gl_locking_policy");
112 Dispatching_Policy : Character;
113 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
117 Foreign_Task_Elaborated : aliased Boolean := True;
118 -- Used to identified fake tasks (i.e., non-Ada Threads)
126 procedure Initialize (Environment_Task : Task_Id);
127 pragma Inline (Initialize);
128 -- Initialize various data needed by this package
130 function Is_Valid_Task return Boolean;
131 pragma Inline (Is_Valid_Task);
132 -- Does executing thread have a TCB?
134 procedure Set (Self_Id : Task_Id);
136 -- Set the self id for the current task
138 function Self return Task_Id;
139 pragma Inline (Self);
140 -- Return a pointer to the Ada Task Control Block of the calling task
144 package body Specific is separate;
145 -- The body of this package is target specific
147 ---------------------------------
148 -- Support for foreign threads --
149 ---------------------------------
151 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
152 -- Allocate and initialize a new ATCB for the current Thread
154 function Register_Foreign_Thread
155 (Thread : Thread_Id) return Task_Id is separate;
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 procedure Abort_Handler (Sig : Signal);
162 -- Signal handler used to implement asynchronous abort
164 function Get_Policy (Prio : System.Any_Priority) return Character;
165 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
166 -- Get priority specific dispatching policy
172 procedure Abort_Handler (Sig : Signal) is
173 pragma Unreferenced (Sig);
175 T : constant Task_Id := Self;
176 Result : Interfaces.C.int;
177 Old_Set : aliased sigset_t;
180 -- It is not safe to raise an exception when using ZCX and the GCC
181 -- exception handling mechanism.
183 if ZCX_By_Default and then GCC_ZCX_Support then
187 if T.Deferral_Level = 0
188 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
189 and then not T.Aborting
193 -- Make sure signals used for RTS internal purpose are unmasked
198 Unblocked_Signal_Mask'Unchecked_Access,
199 Old_Set'Unchecked_Access);
200 pragma Assert (Result = 0);
202 raise Standard'Abort_Signal;
210 -- The underlying thread system sets a guard page at the bottom of a thread
211 -- stack, so nothing is needed.
213 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
214 pragma Unreferenced (T);
215 pragma Unreferenced (On);
224 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
226 return T.Common.LL.Thread;
233 function Self return Task_Id renames Specific.Self;
235 ---------------------
236 -- Initialize_Lock --
237 ---------------------
239 -- Note: mutexes and cond_variables needed per-task basis are initialized
240 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
241 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
242 -- status change of RTS. Therefore rasing Storage_Error in the following
243 -- routines should be able to be handled safely.
245 procedure Initialize_Lock
246 (Prio : System.Any_Priority;
247 L : not null access Lock)
249 Attributes : aliased pthread_mutexattr_t;
250 Result : Interfaces.C.int;
253 Result := pthread_mutexattr_init (Attributes'Access);
254 pragma Assert (Result = 0 or else Result = ENOMEM);
256 if Result = ENOMEM then
260 if Locking_Policy = 'C' then
261 L.Ceiling := Interfaces.C.int (Prio);
264 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
265 pragma Assert (Result = 0 or else Result = ENOMEM);
267 if Result = ENOMEM then
268 Result := pthread_mutexattr_destroy (Attributes'Access);
272 Result := pthread_mutexattr_destroy (Attributes'Access);
273 pragma Assert (Result = 0);
276 procedure Initialize_Lock
277 (L : not null access RTS_Lock;
280 pragma Unreferenced (Level);
282 Attributes : aliased pthread_mutexattr_t;
283 Result : Interfaces.C.int;
286 Result := pthread_mutexattr_init (Attributes'Access);
287 pragma Assert (Result = 0 or else Result = ENOMEM);
289 if Result = ENOMEM then
293 Result := pthread_mutex_init (L, Attributes'Access);
294 pragma Assert (Result = 0 or else Result = ENOMEM);
296 if Result = ENOMEM then
297 Result := pthread_mutexattr_destroy (Attributes'Access);
301 Result := pthread_mutexattr_destroy (Attributes'Access);
302 pragma Assert (Result = 0);
309 procedure Finalize_Lock (L : not null access Lock) is
310 Result : Interfaces.C.int;
312 Result := pthread_mutex_destroy (L.L'Access);
313 pragma Assert (Result = 0);
316 procedure Finalize_Lock (L : not null access RTS_Lock) is
317 Result : Interfaces.C.int;
319 Result := pthread_mutex_destroy (L);
320 pragma Assert (Result = 0);
328 (L : not null access Lock;
329 Ceiling_Violation : out Boolean)
331 Result : Interfaces.C.int;
333 All_Tasks_Link : Task_Id;
334 Current_Prio : System.Any_Priority;
337 -- Perform ceiling checks only when this is the locking policy in use
339 if Locking_Policy = 'C' then
341 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
342 Current_Prio := Get_Priority (Self_ID);
344 -- If there is no other task, no need to check priorities
346 if All_Tasks_Link /= Null_Task
347 and then L.Ceiling < Interfaces.C.int (Current_Prio)
349 Ceiling_Violation := True;
354 Result := pthread_mutex_lock (L.L'Access);
355 pragma Assert (Result = 0);
357 Ceiling_Violation := False;
361 (L : not null access RTS_Lock;
362 Global_Lock : Boolean := False)
364 Result : Interfaces.C.int;
366 if not Single_Lock or else Global_Lock then
367 Result := pthread_mutex_lock (L);
368 pragma Assert (Result = 0);
372 procedure Write_Lock (T : Task_Id) is
373 Result : Interfaces.C.int;
375 if not Single_Lock then
376 Result := pthread_mutex_lock (T.Common.LL.L'Access);
377 pragma Assert (Result = 0);
386 (L : not null access Lock;
387 Ceiling_Violation : out Boolean)
390 Write_Lock (L, Ceiling_Violation);
397 procedure Unlock (L : not null access Lock) is
398 Result : Interfaces.C.int;
400 Result := pthread_mutex_unlock (L.L'Access);
401 pragma Assert (Result = 0);
405 (L : not null access RTS_Lock;
406 Global_Lock : Boolean := False)
408 Result : Interfaces.C.int;
410 if not Single_Lock or else Global_Lock then
411 Result := pthread_mutex_unlock (L);
412 pragma Assert (Result = 0);
416 procedure Unlock (T : Task_Id) is
417 Result : Interfaces.C.int;
419 if not Single_Lock then
420 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
421 pragma Assert (Result = 0);
429 -- Dynamic priority ceilings are not supported by the underlying system
431 procedure Set_Ceiling
432 (L : not null access Lock;
433 Prio : System.Any_Priority)
435 pragma Unreferenced (L, Prio);
446 Reason : System.Tasking.Task_States)
448 pragma Unreferenced (Reason);
450 Result : Interfaces.C.int;
456 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
460 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
463 -- EINTR is not considered a failure
465 pragma Assert (Result = 0 or else Result = EINTR);
472 -- This is for use within the run-time system, so abort is assumed to be
473 -- already deferred, and the caller should be holding its own ATCB lock.
475 procedure Timed_Sleep
478 Mode : ST.Delay_Modes;
479 Reason : System.Tasking.Task_States;
480 Timedout : out Boolean;
481 Yielded : out Boolean)
483 pragma Unreferenced (Reason);
485 Base_Time : constant Duration := Monotonic_Clock;
486 Check_Time : Duration := Base_Time;
488 Request : aliased timespec;
489 Result : Interfaces.C.int;
495 if Mode = Relative then
496 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
498 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
501 if Abs_Time > Check_Time then
502 Request := To_Timespec (Abs_Time);
505 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
509 pthread_cond_timedwait
510 (Self_ID.Common.LL.CV'Access,
511 Single_RTS_Lock'Access,
516 pthread_cond_timedwait
517 (Self_ID.Common.LL.CV'Access,
518 Self_ID.Common.LL.L'Access,
522 Check_Time := Monotonic_Clock;
523 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
525 if Result = 0 or Result = EINTR then
527 -- Somebody may have called Wakeup for us
533 pragma Assert (Result = ETIMEDOUT);
542 -- This is for use in implementing delay statements, so we assume the
543 -- caller is abort-deferred but is holding no locks.
545 procedure Timed_Delay
548 Mode : ST.Delay_Modes)
550 Base_Time : constant Duration := Monotonic_Clock;
551 Check_Time : Duration := Base_Time;
553 Request : aliased timespec;
554 Result : Interfaces.C.int;
561 Write_Lock (Self_ID);
563 if Mode = Relative then
564 Abs_Time := Time + Check_Time;
566 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
569 if Abs_Time > Check_Time then
570 Request := To_Timespec (Abs_Time);
571 Self_ID.Common.State := Delay_Sleep;
574 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
578 pthread_cond_timedwait
579 (Self_ID.Common.LL.CV'Access,
580 Single_RTS_Lock'Access,
584 pthread_cond_timedwait
585 (Self_ID.Common.LL.CV'Access,
586 Self_ID.Common.LL.L'Access,
590 Check_Time := Monotonic_Clock;
591 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
593 pragma Assert (Result = 0 or else
594 Result = ETIMEDOUT or else
598 Self_ID.Common.State := Runnable;
610 ---------------------
611 -- Monotonic_Clock --
612 ---------------------
614 function Monotonic_Clock return Duration is
615 TS : aliased timespec;
616 Result : Interfaces.C.int;
618 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
619 pragma Assert (Result = 0);
620 return To_Duration (TS);
627 function RT_Resolution return Duration is
629 -- Returned value must be an integral multiple of Duration'Small (1 ns)
630 -- The following is the best approximation of 1/1024. The clock on the
631 -- DEC Alpha ticks at 1024 Hz.
633 return 0.000_976_563;
640 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
641 pragma Unreferenced (Reason);
642 Result : Interfaces.C.int;
644 Result := pthread_cond_signal (T.Common.LL.CV'Access);
645 pragma Assert (Result = 0);
652 procedure Yield (Do_Yield : Boolean := True) is
653 Result : Interfaces.C.int;
654 pragma Unreferenced (Result);
657 Result := sched_yield;
665 procedure Set_Priority
667 Prio : System.Any_Priority;
668 Loss_Of_Inheritance : Boolean := False)
670 pragma Unreferenced (Loss_Of_Inheritance);
672 Result : Interfaces.C.int;
673 Param : aliased struct_sched_param;
675 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
676 -- Upper case first character of the policy name corresponding to the
677 -- task as set by a Priority_Specific_Dispatching pragma.
680 T.Common.Current_Priority := Prio;
681 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
683 if Dispatching_Policy = 'R'
684 or else Priority_Specific_Policy = 'R'
685 or else Time_Slice_Val > 0
688 pthread_setschedparam
689 (T.Common.LL.Thread, SCHED_RR, Param'Access);
691 elsif Dispatching_Policy = 'F'
692 or else Priority_Specific_Policy = 'F'
693 or else Time_Slice_Val = 0
696 pthread_setschedparam
697 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
701 pthread_setschedparam
702 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
705 pragma Assert (Result = 0);
712 function Get_Priority (T : Task_Id) return System.Any_Priority is
714 return T.Common.Current_Priority;
721 procedure Enter_Task (Self_ID : Task_Id) is
724 Self_ID.Common.LL.Thread := pthread_self;
725 Specific.Set (Self_ID);
729 for J in Known_Tasks'Range loop
730 if Known_Tasks (J) = null then
731 Known_Tasks (J) := Self_ID;
732 Self_ID.Known_Tasks_Index := J;
744 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
746 return new Ada_Task_Control_Block (Entry_Num);
753 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
755 -----------------------------
756 -- Register_Foreign_Thread --
757 -----------------------------
759 function Register_Foreign_Thread return Task_Id is
761 if Is_Valid_Task then
764 return Register_Foreign_Thread (pthread_self);
766 end Register_Foreign_Thread;
772 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
773 Mutex_Attr : aliased pthread_mutexattr_t;
774 Result : Interfaces.C.int;
775 Cond_Attr : aliased pthread_condattr_t;
778 if not Single_Lock then
779 Result := pthread_mutexattr_init (Mutex_Attr'Access);
780 pragma Assert (Result = 0 or else Result = ENOMEM);
785 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
786 pragma Assert (Result = 0 or else Result = ENOMEM);
794 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
795 pragma Assert (Result = 0);
798 Result := pthread_condattr_init (Cond_Attr'Access);
799 pragma Assert (Result = 0 or else Result = ENOMEM);
804 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
805 pragma Assert (Result = 0 or else Result = ENOMEM);
811 if not Single_Lock then
812 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
813 pragma Assert (Result = 0);
819 Result := pthread_condattr_destroy (Cond_Attr'Access);
820 pragma Assert (Result = 0);
827 procedure Create_Task
829 Wrapper : System.Address;
830 Stack_Size : System.Parameters.Size_Type;
831 Priority : System.Any_Priority;
832 Succeeded : out Boolean)
834 Attributes : aliased pthread_attr_t;
835 Adjusted_Stack_Size : Interfaces.C.size_t;
836 Result : Interfaces.C.int;
837 Param : aliased System.OS_Interface.struct_sched_param;
839 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
840 -- Upper case first character of the policy name corresponding to the
841 -- task as set by a Priority_Specific_Dispatching pragma.
843 use System.Task_Info;
846 -- Account for the Yellow Zone (2 pages) and the guard page
847 -- right above. See Hide_Yellow_Zone for the rationale.
849 Adjusted_Stack_Size :=
850 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
852 Result := pthread_attr_init (Attributes'Access);
853 pragma Assert (Result = 0 or else Result = ENOMEM);
861 pthread_attr_setdetachstate
862 (Attributes'Access, PTHREAD_CREATE_DETACHED);
863 pragma Assert (Result = 0);
866 pthread_attr_setstacksize
867 (Attributes'Access, Adjusted_Stack_Size);
868 pragma Assert (Result = 0);
870 Param.sched_priority :=
871 Interfaces.C.int (Underlying_Priorities (Priority));
873 pthread_attr_setschedparam
874 (Attributes'Access, Param'Access);
875 pragma Assert (Result = 0);
877 if Dispatching_Policy = 'R'
878 or else Priority_Specific_Policy = 'R'
879 or else Time_Slice_Val > 0
882 pthread_attr_setschedpolicy
883 (Attributes'Access, System.OS_Interface.SCHED_RR);
885 elsif Dispatching_Policy = 'F'
886 or else Priority_Specific_Policy = 'F'
887 or else Time_Slice_Val = 0
890 pthread_attr_setschedpolicy
891 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
895 pthread_attr_setschedpolicy
896 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
899 pragma Assert (Result = 0);
901 -- Set the scheduling parameters explicitly, since this is the only way
902 -- to force the OS to take e.g. the sched policy and scope attributes
906 pthread_attr_setinheritsched
907 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
908 pragma Assert (Result = 0);
910 T.Common.Current_Priority := Priority;
912 if T.Common.Task_Info /= null then
913 case T.Common.Task_Info.Contention_Scope is
914 when System.Task_Info.Process_Scope =>
916 pthread_attr_setscope
917 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
919 when System.Task_Info.System_Scope =>
921 pthread_attr_setscope
922 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
924 when System.Task_Info.Default_Scope =>
928 pragma Assert (Result = 0);
931 -- Since the initial signal mask of a thread is inherited from the
932 -- creator, and the Environment task has all its signals masked, we
933 -- do not need to manipulate caller's signal mask at this point.
934 -- All tasks in RTS will have All_Tasks_Mask initially.
938 (T.Common.LL.Thread'Access,
940 Thread_Body_Access (Wrapper),
942 pragma Assert (Result = 0 or else Result = EAGAIN);
944 Succeeded := Result = 0;
946 Result := pthread_attr_destroy (Attributes'Access);
947 pragma Assert (Result = 0);
949 if T.Common.Task_Info /= null then
951 -- ??? We're using a process-wide function to implement a task
952 -- specific characteristic.
954 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
955 Result := bind_to_cpu (Curpid, 0);
957 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
961 Interfaces.C.unsigned_long (
962 Interfaces.Shift_Left
963 (Interfaces.Unsigned_64'(1),
964 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
965 pragma Assert (Result = 0);
974 procedure Finalize_TCB (T : Task_Id) is
975 Result : Interfaces.C.int;
977 Is_Self : constant Boolean := T = Self;
979 procedure Free is new
980 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
983 if not Single_Lock then
984 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
985 pragma Assert (Result = 0);
988 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
989 pragma Assert (Result = 0);
991 if T.Known_Tasks_Index /= -1 then
992 Known_Tasks (T.Known_Tasks_Index) := null;
1006 procedure Exit_Task is
1008 Specific.Set (null);
1015 procedure Abort_Task (T : Task_Id) is
1016 Result : Interfaces.C.int;
1018 Result := pthread_kill (T.Common.LL.Thread,
1019 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1020 pragma Assert (Result = 0);
1027 procedure Initialize (S : in out Suspension_Object) is
1028 Mutex_Attr : aliased pthread_mutexattr_t;
1029 Cond_Attr : aliased pthread_condattr_t;
1030 Result : Interfaces.C.int;
1033 -- Initialize internal state (always to False (RM D.10(6)))
1038 -- Initialize internal mutex
1040 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1041 pragma Assert (Result = 0 or else Result = ENOMEM);
1043 if Result = ENOMEM then
1044 raise Storage_Error;
1047 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1048 pragma Assert (Result = 0 or else Result = ENOMEM);
1050 if Result = ENOMEM then
1051 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1052 raise Storage_Error;
1055 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1056 pragma Assert (Result = 0);
1058 -- Initialize internal condition variable
1060 Result := pthread_condattr_init (Cond_Attr'Access);
1061 pragma Assert (Result = 0 or else Result = ENOMEM);
1063 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1065 pragma Assert (Result = 0 or else Result = ENOMEM);
1068 Result := pthread_mutex_destroy (S.L'Access);
1069 pragma Assert (Result = 0);
1071 if Result = ENOMEM then
1072 raise Storage_Error;
1081 procedure Finalize (S : in out Suspension_Object) is
1082 Result : Interfaces.C.int;
1085 -- Destroy internal mutex
1087 Result := pthread_mutex_destroy (S.L'Access);
1088 pragma Assert (Result = 0);
1090 -- Destroy internal condition variable
1092 Result := pthread_cond_destroy (S.CV'Access);
1093 pragma Assert (Result = 0);
1100 function Current_State (S : Suspension_Object) return Boolean is
1102 -- We do not want to use lock on this read operation. State is marked
1103 -- as Atomic so that we ensure that the value retrieved is correct.
1112 procedure Set_False (S : in out Suspension_Object) is
1113 Result : Interfaces.C.int;
1116 SSL.Abort_Defer.all;
1118 Result := pthread_mutex_lock (S.L'Access);
1119 pragma Assert (Result = 0);
1123 Result := pthread_mutex_unlock (S.L'Access);
1124 pragma Assert (Result = 0);
1126 SSL.Abort_Undefer.all;
1133 procedure Set_True (S : in out Suspension_Object) is
1134 Result : Interfaces.C.int;
1137 SSL.Abort_Defer.all;
1139 Result := pthread_mutex_lock (S.L'Access);
1140 pragma Assert (Result = 0);
1142 -- If there is already a task waiting on this suspension object then we
1143 -- resume it, leaving the state of the suspension object to False, as
1144 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1150 Result := pthread_cond_signal (S.CV'Access);
1151 pragma Assert (Result = 0);
1157 Result := pthread_mutex_unlock (S.L'Access);
1158 pragma Assert (Result = 0);
1160 SSL.Abort_Undefer.all;
1163 ------------------------
1164 -- Suspend_Until_True --
1165 ------------------------
1167 procedure Suspend_Until_True (S : in out Suspension_Object) is
1168 Result : Interfaces.C.int;
1171 SSL.Abort_Defer.all;
1173 Result := pthread_mutex_lock (S.L'Access);
1174 pragma Assert (Result = 0);
1178 -- Program_Error must be raised upon calling Suspend_Until_True
1179 -- if another task is already waiting on that suspension object
1182 Result := pthread_mutex_unlock (S.L'Access);
1183 pragma Assert (Result = 0);
1185 SSL.Abort_Undefer.all;
1187 raise Program_Error;
1190 -- Suspend the task if the state is False. Otherwise, the task
1191 -- continues its execution, and the state of the suspension object
1192 -- is set to False (RM D.10(9)).
1198 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1201 Result := pthread_mutex_unlock (S.L'Access);
1202 pragma Assert (Result = 0);
1204 SSL.Abort_Undefer.all;
1206 end Suspend_Until_True;
1214 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1215 pragma Unreferenced (Self_ID);
1220 --------------------
1221 -- Check_No_Locks --
1222 --------------------
1224 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1225 pragma Unreferenced (Self_ID);
1230 ----------------------
1231 -- Environment_Task --
1232 ----------------------
1234 function Environment_Task return Task_Id is
1236 return Environment_Task_Id;
1237 end Environment_Task;
1243 procedure Lock_RTS is
1245 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1252 procedure Unlock_RTS is
1254 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1261 function Suspend_Task
1263 Thread_Self : Thread_Id) return Boolean
1265 pragma Unreferenced (T, Thread_Self);
1274 function Resume_Task
1276 Thread_Self : Thread_Id) return Boolean
1278 pragma Unreferenced (T, Thread_Self);
1287 procedure Initialize (Environment_Task : Task_Id) is
1288 act : aliased struct_sigaction;
1289 old_act : aliased struct_sigaction;
1290 Tmp_Set : aliased sigset_t;
1291 Result : Interfaces.C.int;
1294 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1295 pragma Import (C, State, "__gnat_get_interrupt_state");
1296 -- Get interrupt state. Defined in a-init.c. The input argument is
1297 -- the interrupt number, and the result is one of the following:
1299 Default : constant Character := 's';
1300 -- 'n' this interrupt not set by any Interrupt_State pragma
1301 -- 'u' Interrupt_State pragma set state to User
1302 -- 'r' Interrupt_State pragma set state to Runtime
1303 -- 's' Interrupt_State pragma set state to System (use "default"
1307 Environment_Task_Id := Environment_Task;
1309 Interrupt_Management.Initialize;
1311 -- Prepare the set of signals that should unblocked in all tasks
1313 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1314 pragma Assert (Result = 0);
1316 for J in Interrupt_Management.Interrupt_ID loop
1317 if System.Interrupt_Management.Keep_Unmasked (J) then
1318 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1319 pragma Assert (Result = 0);
1325 -- Initialize the lock used to synchronize chain of all ATCBs
1327 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1329 Specific.Initialize (Environment_Task);
1331 Enter_Task (Environment_Task);
1333 -- Install the abort-signal handler
1336 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1339 act.sa_handler := Abort_Handler'Address;
1341 Result := sigemptyset (Tmp_Set'Access);
1342 pragma Assert (Result = 0);
1343 act.sa_mask := Tmp_Set;
1347 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1348 act'Unchecked_Access,
1349 old_act'Unchecked_Access);
1350 pragma Assert (Result = 0);
1354 end System.Task_Primitives.Operations;