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)
103 Abort_Handler_Installed : Boolean := False;
104 -- True if a handler for the abort signal is installed
112 procedure Initialize (Environment_Task : Task_Id);
113 pragma Inline (Initialize);
114 -- Initialize various data needed by this package
116 function Is_Valid_Task return Boolean;
117 pragma Inline (Is_Valid_Task);
118 -- Does executing thread have a TCB?
120 procedure Set (Self_Id : Task_Id);
122 -- Set the self id for the current task
124 function Self return Task_Id;
125 pragma Inline (Self);
126 -- Return a pointer to the Ada Task Control Block of the calling task
130 package body Specific is separate;
131 -- The body of this package is target specific
133 ---------------------------------
134 -- Support for foreign threads --
135 ---------------------------------
137 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
138 -- Allocate and initialize a new ATCB for the current Thread
140 function Register_Foreign_Thread
141 (Thread : Thread_Id) return Task_Id is separate;
143 -----------------------
144 -- Local Subprograms --
145 -----------------------
147 procedure Abort_Handler (Sig : Signal);
148 -- Signal handler used to implement asynchronous abort
150 function Get_Policy (Prio : System.Any_Priority) return Character;
151 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
152 -- Get priority specific dispatching policy
158 procedure Abort_Handler (Sig : Signal) is
159 pragma Unreferenced (Sig);
161 T : constant Task_Id := Self;
162 Old_Set : aliased sigset_t;
164 Result : Interfaces.C.int;
165 pragma Warnings (Off, Result);
168 -- It's not safe to raise an exception when using GCC ZCX mechanism.
169 -- Note that we still need to install a signal handler, since in some
170 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
171 -- need to send the Abort signal to a task.
173 if ZCX_By_Default and then GCC_ZCX_Support then
177 if T.Deferral_Level = 0
178 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
179 and then not T.Aborting
183 -- Make sure signals used for RTS internal purpose are unmasked
188 Unblocked_Signal_Mask'Access,
190 pragma Assert (Result = 0);
192 raise Standard'Abort_Signal;
200 -- The underlying thread system sets a guard page at the bottom of a thread
201 -- stack, so nothing is needed.
203 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
204 pragma Unreferenced (T);
205 pragma Unreferenced (On);
214 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
216 return T.Common.LL.Thread;
223 function Self return Task_Id renames Specific.Self;
225 ---------------------
226 -- Initialize_Lock --
227 ---------------------
229 -- Note: mutexes and cond_variables needed per-task basis are initialized
230 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
231 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
232 -- status change of RTS. Therefore raising Storage_Error in the following
233 -- routines should be able to be handled safely.
235 procedure Initialize_Lock
236 (Prio : System.Any_Priority;
237 L : not null access Lock)
239 Attributes : aliased pthread_mutexattr_t;
240 Result : Interfaces.C.int;
243 Result := pthread_mutexattr_init (Attributes'Access);
244 pragma Assert (Result = 0 or else Result = ENOMEM);
246 if Result = ENOMEM then
250 if Locking_Policy = 'C' then
251 L.Ceiling := Interfaces.C.int (Prio);
254 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
255 pragma Assert (Result = 0 or else Result = ENOMEM);
257 if Result = ENOMEM then
258 Result := pthread_mutexattr_destroy (Attributes'Access);
262 Result := pthread_mutexattr_destroy (Attributes'Access);
263 pragma Assert (Result = 0);
266 procedure Initialize_Lock
267 (L : not null access RTS_Lock;
270 pragma Unreferenced (Level);
272 Attributes : aliased pthread_mutexattr_t;
273 Result : Interfaces.C.int;
276 Result := pthread_mutexattr_init (Attributes'Access);
277 pragma Assert (Result = 0 or else Result = ENOMEM);
279 if Result = ENOMEM then
283 Result := pthread_mutex_init (L, Attributes'Access);
284 pragma Assert (Result = 0 or else Result = ENOMEM);
286 if Result = ENOMEM then
287 Result := pthread_mutexattr_destroy (Attributes'Access);
291 Result := pthread_mutexattr_destroy (Attributes'Access);
292 pragma Assert (Result = 0);
299 procedure Finalize_Lock (L : not null access Lock) is
300 Result : Interfaces.C.int;
302 Result := pthread_mutex_destroy (L.L'Access);
303 pragma Assert (Result = 0);
306 procedure Finalize_Lock (L : not null access RTS_Lock) is
307 Result : Interfaces.C.int;
309 Result := pthread_mutex_destroy (L);
310 pragma Assert (Result = 0);
318 (L : not null access Lock;
319 Ceiling_Violation : out Boolean)
321 Result : Interfaces.C.int;
323 All_Tasks_Link : Task_Id;
324 Current_Prio : System.Any_Priority;
327 -- Perform ceiling checks only when this is the locking policy in use
329 if Locking_Policy = 'C' then
331 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
332 Current_Prio := Get_Priority (Self_ID);
334 -- If there is no other task, no need to check priorities
336 if All_Tasks_Link /= Null_Task
337 and then L.Ceiling < Interfaces.C.int (Current_Prio)
339 Ceiling_Violation := True;
344 Result := pthread_mutex_lock (L.L'Access);
345 pragma Assert (Result = 0);
347 Ceiling_Violation := False;
351 (L : not null access RTS_Lock;
352 Global_Lock : Boolean := False)
354 Result : Interfaces.C.int;
356 if not Single_Lock or else Global_Lock then
357 Result := pthread_mutex_lock (L);
358 pragma Assert (Result = 0);
362 procedure Write_Lock (T : Task_Id) is
363 Result : Interfaces.C.int;
365 if not Single_Lock then
366 Result := pthread_mutex_lock (T.Common.LL.L'Access);
367 pragma Assert (Result = 0);
376 (L : not null access Lock;
377 Ceiling_Violation : out Boolean)
380 Write_Lock (L, Ceiling_Violation);
387 procedure Unlock (L : not null access Lock) is
388 Result : Interfaces.C.int;
390 Result := pthread_mutex_unlock (L.L'Access);
391 pragma Assert (Result = 0);
395 (L : not null access RTS_Lock;
396 Global_Lock : Boolean := False)
398 Result : Interfaces.C.int;
400 if not Single_Lock or else Global_Lock then
401 Result := pthread_mutex_unlock (L);
402 pragma Assert (Result = 0);
406 procedure Unlock (T : Task_Id) is
407 Result : Interfaces.C.int;
409 if not Single_Lock then
410 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
411 pragma Assert (Result = 0);
419 -- Dynamic priority ceilings are not supported by the underlying system
421 procedure Set_Ceiling
422 (L : not null access Lock;
423 Prio : System.Any_Priority)
425 pragma Unreferenced (L, Prio);
436 Reason : System.Tasking.Task_States)
438 pragma Unreferenced (Reason);
440 Result : Interfaces.C.int;
445 (cond => Self_ID.Common.LL.CV'Access,
446 mutex => (if Single_Lock
447 then Single_RTS_Lock'Access
448 else 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;
484 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
485 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
487 if Abs_Time > Check_Time then
488 Request := To_Timespec (Abs_Time);
491 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
494 pthread_cond_timedwait
495 (cond => Self_ID.Common.LL.CV'Access,
496 mutex => (if Single_Lock
497 then Single_RTS_Lock'Access
498 else Self_ID.Common.LL.L'Access),
499 abstime => Request'Access);
501 Check_Time := Monotonic_Clock;
502 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
504 if Result = 0 or Result = EINTR then
506 -- Somebody may have called Wakeup for us
512 pragma Assert (Result = ETIMEDOUT);
521 -- This is for use in implementing delay statements, so we assume the
522 -- caller is abort-deferred but is holding no locks.
524 procedure Timed_Delay
527 Mode : ST.Delay_Modes)
529 Base_Time : constant Duration := Monotonic_Clock;
530 Check_Time : Duration := Base_Time;
532 Request : aliased timespec;
533 Result : Interfaces.C.int;
540 Write_Lock (Self_ID);
544 then Time + Check_Time
545 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
547 if Abs_Time > Check_Time then
548 Request := To_Timespec (Abs_Time);
549 Self_ID.Common.State := Delay_Sleep;
552 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
555 pthread_cond_timedwait
556 (cond => Self_ID.Common.LL.CV'Access,
557 mutex => (if Single_Lock
558 then Single_RTS_Lock'Access
559 else Self_ID.Common.LL.L'Access),
560 abstime => Request'Access);
562 Check_Time := Monotonic_Clock;
563 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
565 pragma Assert (Result = 0 or else
566 Result = ETIMEDOUT or else
570 Self_ID.Common.State := Runnable;
582 ---------------------
583 -- Monotonic_Clock --
584 ---------------------
586 function Monotonic_Clock return Duration is
587 TS : aliased timespec;
588 Result : Interfaces.C.int;
590 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
591 pragma Assert (Result = 0);
592 return To_Duration (TS);
599 function RT_Resolution return Duration is
601 -- Returned value must be an integral multiple of Duration'Small (1 ns)
602 -- The following is the best approximation of 1/1024. The clock on the
603 -- DEC Alpha ticks at 1024 Hz.
605 return 0.000_976_563;
612 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
613 pragma Unreferenced (Reason);
614 Result : Interfaces.C.int;
616 Result := pthread_cond_signal (T.Common.LL.CV'Access);
617 pragma Assert (Result = 0);
624 procedure Yield (Do_Yield : Boolean := True) is
625 Result : Interfaces.C.int;
626 pragma Unreferenced (Result);
629 Result := sched_yield;
637 procedure Set_Priority
639 Prio : System.Any_Priority;
640 Loss_Of_Inheritance : Boolean := False)
642 pragma Unreferenced (Loss_Of_Inheritance);
644 Result : Interfaces.C.int;
645 Param : aliased struct_sched_param;
647 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
648 -- Upper case first character of the policy name corresponding to the
649 -- task as set by a Priority_Specific_Dispatching pragma.
652 T.Common.Current_Priority := Prio;
653 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
655 if Dispatching_Policy = 'R'
656 or else Priority_Specific_Policy = 'R'
657 or else Time_Slice_Val > 0
660 pthread_setschedparam
661 (T.Common.LL.Thread, SCHED_RR, Param'Access);
663 elsif Dispatching_Policy = 'F'
664 or else Priority_Specific_Policy = 'F'
665 or else Time_Slice_Val = 0
668 pthread_setschedparam
669 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
673 pthread_setschedparam
674 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
677 pragma Assert (Result = 0);
684 function Get_Priority (T : Task_Id) return System.Any_Priority is
686 return T.Common.Current_Priority;
693 procedure Enter_Task (Self_ID : Task_Id) is
695 Hide_Unhide_Yellow_Zone (Hide => True);
696 Self_ID.Common.LL.Thread := pthread_self;
698 Specific.Set (Self_ID);
705 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
707 return new Ada_Task_Control_Block (Entry_Num);
714 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
716 -----------------------------
717 -- Register_Foreign_Thread --
718 -----------------------------
720 function Register_Foreign_Thread return Task_Id is
722 if Is_Valid_Task then
725 return Register_Foreign_Thread (pthread_self);
727 end Register_Foreign_Thread;
733 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
734 Mutex_Attr : aliased pthread_mutexattr_t;
735 Result : Interfaces.C.int;
736 Cond_Attr : aliased pthread_condattr_t;
739 if not Single_Lock then
740 Result := pthread_mutexattr_init (Mutex_Attr'Access);
741 pragma Assert (Result = 0 or else Result = ENOMEM);
746 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
747 pragma Assert (Result = 0 or else Result = ENOMEM);
755 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
756 pragma Assert (Result = 0);
759 Result := pthread_condattr_init (Cond_Attr'Access);
760 pragma Assert (Result = 0 or else Result = ENOMEM);
765 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
766 pragma Assert (Result = 0 or else Result = ENOMEM);
772 if not Single_Lock then
773 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
774 pragma Assert (Result = 0);
780 Result := pthread_condattr_destroy (Cond_Attr'Access);
781 pragma Assert (Result = 0);
788 procedure Create_Task
790 Wrapper : System.Address;
791 Stack_Size : System.Parameters.Size_Type;
792 Priority : System.Any_Priority;
793 Succeeded : out Boolean)
795 Attributes : aliased pthread_attr_t;
796 Adjusted_Stack_Size : Interfaces.C.size_t;
797 Result : Interfaces.C.int;
798 Param : aliased System.OS_Interface.struct_sched_param;
800 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
801 -- Upper case first character of the policy name corresponding to the
802 -- task as set by a Priority_Specific_Dispatching pragma.
804 use System.Task_Info;
807 -- Account for the Yellow Zone (2 pages) and the guard page right above.
808 -- See Hide_Unhide_Yellow_Zone for the rationale.
810 Adjusted_Stack_Size :=
811 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
813 Result := pthread_attr_init (Attributes'Access);
814 pragma Assert (Result = 0 or else Result = ENOMEM);
822 pthread_attr_setdetachstate
823 (Attributes'Access, PTHREAD_CREATE_DETACHED);
824 pragma Assert (Result = 0);
827 pthread_attr_setstacksize
828 (Attributes'Access, Adjusted_Stack_Size);
829 pragma Assert (Result = 0);
831 Param.sched_priority :=
832 Interfaces.C.int (Underlying_Priorities (Priority));
834 pthread_attr_setschedparam
835 (Attributes'Access, Param'Access);
836 pragma Assert (Result = 0);
838 if Dispatching_Policy = 'R'
839 or else Priority_Specific_Policy = 'R'
840 or else Time_Slice_Val > 0
843 pthread_attr_setschedpolicy
844 (Attributes'Access, System.OS_Interface.SCHED_RR);
846 elsif Dispatching_Policy = 'F'
847 or else Priority_Specific_Policy = 'F'
848 or else Time_Slice_Val = 0
851 pthread_attr_setschedpolicy
852 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
856 pthread_attr_setschedpolicy
857 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
860 pragma Assert (Result = 0);
862 -- Set the scheduling parameters explicitly, since this is the only way
863 -- to force the OS to take e.g. the sched policy and scope attributes
867 pthread_attr_setinheritsched
868 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
869 pragma Assert (Result = 0);
871 T.Common.Current_Priority := Priority;
873 if T.Common.Task_Info /= null then
874 case T.Common.Task_Info.Contention_Scope is
875 when System.Task_Info.Process_Scope =>
877 pthread_attr_setscope
878 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
880 when System.Task_Info.System_Scope =>
882 pthread_attr_setscope
883 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
885 when System.Task_Info.Default_Scope =>
889 pragma Assert (Result = 0);
892 -- Since the initial signal mask of a thread is inherited from the
893 -- creator, and the Environment task has all its signals masked, we
894 -- do not need to manipulate caller's signal mask at this point.
895 -- All tasks in RTS will have All_Tasks_Mask initially.
899 (T.Common.LL.Thread'Access,
901 Thread_Body_Access (Wrapper),
903 pragma Assert (Result = 0 or else Result = EAGAIN);
905 Succeeded := Result = 0;
907 Result := pthread_attr_destroy (Attributes'Access);
908 pragma Assert (Result = 0);
910 if Succeeded and then T.Common.Task_Info /= null then
912 -- ??? We're using a process-wide function to implement a task
913 -- specific characteristic.
915 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
916 Result := bind_to_cpu (Curpid, 0);
918 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
922 Interfaces.C.unsigned_long (
923 Interfaces.Shift_Left
924 (Interfaces.Unsigned_64'(1),
925 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
926 pragma Assert (Result = 0);
935 procedure Finalize_TCB (T : Task_Id) is
936 Result : Interfaces.C.int;
938 Is_Self : constant Boolean := T = Self;
940 procedure Free is new
941 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
944 if not Single_Lock then
945 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
946 pragma Assert (Result = 0);
949 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
950 pragma Assert (Result = 0);
952 if T.Known_Tasks_Index /= -1 then
953 Known_Tasks (T.Known_Tasks_Index) := null;
967 procedure Exit_Task is
970 Hide_Unhide_Yellow_Zone (Hide => False);
977 procedure Abort_Task (T : Task_Id) is
978 Result : Interfaces.C.int;
980 if Abort_Handler_Installed then
981 Result := pthread_kill (T.Common.LL.Thread,
982 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
983 pragma Assert (Result = 0);
991 procedure Initialize (S : in out Suspension_Object) is
992 Mutex_Attr : aliased pthread_mutexattr_t;
993 Cond_Attr : aliased pthread_condattr_t;
994 Result : Interfaces.C.int;
997 -- Initialize internal state (always to False (RM D.10(6)))
1002 -- Initialize internal mutex
1004 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1005 pragma Assert (Result = 0 or else Result = ENOMEM);
1007 if Result = ENOMEM then
1008 raise Storage_Error;
1011 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1012 pragma Assert (Result = 0 or else Result = ENOMEM);
1014 if Result = ENOMEM then
1015 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1016 raise Storage_Error;
1019 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1020 pragma Assert (Result = 0);
1022 -- Initialize internal condition variable
1024 Result := pthread_condattr_init (Cond_Attr'Access);
1025 pragma Assert (Result = 0 or else Result = ENOMEM);
1027 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1029 pragma Assert (Result = 0 or else Result = ENOMEM);
1032 Result := pthread_mutex_destroy (S.L'Access);
1033 pragma Assert (Result = 0);
1035 if Result = ENOMEM then
1036 raise Storage_Error;
1045 procedure Finalize (S : in out Suspension_Object) is
1046 Result : Interfaces.C.int;
1049 -- Destroy internal mutex
1051 Result := pthread_mutex_destroy (S.L'Access);
1052 pragma Assert (Result = 0);
1054 -- Destroy internal condition variable
1056 Result := pthread_cond_destroy (S.CV'Access);
1057 pragma Assert (Result = 0);
1064 function Current_State (S : Suspension_Object) return Boolean is
1066 -- We do not want to use lock on this read operation. State is marked
1067 -- as Atomic so that we ensure that the value retrieved is correct.
1076 procedure Set_False (S : in out Suspension_Object) is
1077 Result : Interfaces.C.int;
1080 SSL.Abort_Defer.all;
1082 Result := pthread_mutex_lock (S.L'Access);
1083 pragma Assert (Result = 0);
1087 Result := pthread_mutex_unlock (S.L'Access);
1088 pragma Assert (Result = 0);
1090 SSL.Abort_Undefer.all;
1097 procedure Set_True (S : in out Suspension_Object) is
1098 Result : Interfaces.C.int;
1101 SSL.Abort_Defer.all;
1103 Result := pthread_mutex_lock (S.L'Access);
1104 pragma Assert (Result = 0);
1106 -- If there is already a task waiting on this suspension object then we
1107 -- resume it, leaving the state of the suspension object to False, as
1108 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1114 Result := pthread_cond_signal (S.CV'Access);
1115 pragma Assert (Result = 0);
1121 Result := pthread_mutex_unlock (S.L'Access);
1122 pragma Assert (Result = 0);
1124 SSL.Abort_Undefer.all;
1127 ------------------------
1128 -- Suspend_Until_True --
1129 ------------------------
1131 procedure Suspend_Until_True (S : in out Suspension_Object) is
1132 Result : Interfaces.C.int;
1135 SSL.Abort_Defer.all;
1137 Result := pthread_mutex_lock (S.L'Access);
1138 pragma Assert (Result = 0);
1142 -- Program_Error must be raised upon calling Suspend_Until_True
1143 -- if another task is already waiting on that suspension object
1146 Result := pthread_mutex_unlock (S.L'Access);
1147 pragma Assert (Result = 0);
1149 SSL.Abort_Undefer.all;
1151 raise Program_Error;
1154 -- Suspend the task if the state is False. Otherwise, the task
1155 -- continues its execution, and the state of the suspension object
1156 -- is set to False (RM D.10(9)).
1164 -- Loop in case pthread_cond_wait returns earlier than expected
1165 -- (e.g. in case of EINTR caused by a signal).
1167 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1168 pragma Assert (Result = 0 or else Result = EINTR);
1170 exit when not S.Waiting;
1174 Result := pthread_mutex_unlock (S.L'Access);
1175 pragma Assert (Result = 0);
1177 SSL.Abort_Undefer.all;
1179 end Suspend_Until_True;
1187 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1188 pragma Unreferenced (Self_ID);
1193 --------------------
1194 -- Check_No_Locks --
1195 --------------------
1197 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1198 pragma Unreferenced (Self_ID);
1203 ----------------------
1204 -- Environment_Task --
1205 ----------------------
1207 function Environment_Task return Task_Id is
1209 return Environment_Task_Id;
1210 end Environment_Task;
1216 procedure Lock_RTS is
1218 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1225 procedure Unlock_RTS is
1227 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1234 function Suspend_Task
1236 Thread_Self : Thread_Id) return Boolean
1238 pragma Unreferenced (T, Thread_Self);
1247 function Resume_Task
1249 Thread_Self : Thread_Id) return Boolean
1251 pragma Unreferenced (T, Thread_Self);
1256 --------------------
1257 -- Stop_All_Tasks --
1258 --------------------
1260 procedure Stop_All_Tasks is
1269 function Stop_Task (T : ST.Task_Id) return Boolean is
1270 pragma Unreferenced (T);
1279 function Continue_Task (T : ST.Task_Id) return Boolean is
1280 pragma Unreferenced (T);
1289 procedure Initialize (Environment_Task : Task_Id) is
1290 act : aliased struct_sigaction;
1291 old_act : aliased struct_sigaction;
1292 Tmp_Set : aliased sigset_t;
1293 Result : Interfaces.C.int;
1296 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1297 pragma Import (C, State, "__gnat_get_interrupt_state");
1298 -- Get interrupt state. Defined in a-init.c. The input argument is
1299 -- the interrupt number, and the result is one of the following:
1301 Default : constant Character := 's';
1302 -- 'n' this interrupt not set by any Interrupt_State pragma
1303 -- 'u' Interrupt_State pragma set state to User
1304 -- 'r' Interrupt_State pragma set state to Runtime
1305 -- 's' Interrupt_State pragma set state to System (use "default"
1309 Environment_Task_Id := Environment_Task;
1311 Interrupt_Management.Initialize;
1313 -- Prepare the set of signals that should unblocked in all tasks
1315 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1316 pragma Assert (Result = 0);
1318 for J in Interrupt_Management.Interrupt_ID loop
1319 if System.Interrupt_Management.Keep_Unmasked (J) then
1320 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1321 pragma Assert (Result = 0);
1327 -- Initialize the lock used to synchronize chain of all ATCBs
1329 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1331 Specific.Initialize (Environment_Task);
1333 -- Make environment task known here because it doesn't go through
1334 -- Activate_Tasks, which does it for all other tasks.
1336 Known_Tasks (Known_Tasks'First) := Environment_Task;
1337 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1339 Enter_Task (Environment_Task);
1342 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1345 act.sa_handler := Abort_Handler'Address;
1347 Result := sigemptyset (Tmp_Set'Access);
1348 pragma Assert (Result = 0);
1349 act.sa_mask := Tmp_Set;
1353 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1354 act'Unchecked_Access,
1355 old_act'Unchecked_Access);
1356 pragma Assert (Result = 0);
1357 Abort_Handler_Installed := True;
1361 end System.Task_Primitives.Operations;