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.
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 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 -- 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's not safe to raise an exception when using GCC ZCX mechanism.
166 -- Note that we still need to install a signal handler, since in some
167 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
168 -- need to send the Abort signal to a task.
170 if ZCX_By_Default then
174 if T.Deferral_Level = 0
175 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
176 and then not T.Aborting
180 -- Make sure signals used for RTS internal purpose are unmasked
185 Unblocked_Signal_Mask'Access,
187 pragma Assert (Result = 0);
189 raise Standard'Abort_Signal;
197 -- The underlying thread system sets a guard page at the bottom of a thread
198 -- stack, so nothing is needed.
200 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
201 pragma Unreferenced (T);
202 pragma Unreferenced (On);
211 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
213 return T.Common.LL.Thread;
220 function Self return Task_Id renames Specific.Self;
222 ---------------------
223 -- Initialize_Lock --
224 ---------------------
226 -- Note: mutexes and cond_variables needed per-task basis are initialized
227 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
228 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
229 -- status change of RTS. Therefore raising Storage_Error in the following
230 -- routines should be able to be handled safely.
232 procedure Initialize_Lock
233 (Prio : System.Any_Priority;
234 L : not null access Lock)
236 Attributes : aliased pthread_mutexattr_t;
237 Result : Interfaces.C.int;
240 Result := pthread_mutexattr_init (Attributes'Access);
241 pragma Assert (Result = 0 or else Result = ENOMEM);
243 if Result = ENOMEM then
247 if Locking_Policy = 'C' then
248 L.Ceiling := Interfaces.C.int (Prio);
251 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
252 pragma Assert (Result = 0 or else Result = ENOMEM);
254 if Result = ENOMEM then
255 Result := pthread_mutexattr_destroy (Attributes'Access);
259 Result := pthread_mutexattr_destroy (Attributes'Access);
260 pragma Assert (Result = 0);
263 procedure Initialize_Lock
264 (L : not null access RTS_Lock;
267 pragma Unreferenced (Level);
269 Attributes : aliased pthread_mutexattr_t;
270 Result : Interfaces.C.int;
273 Result := pthread_mutexattr_init (Attributes'Access);
274 pragma Assert (Result = 0 or else Result = ENOMEM);
276 if Result = ENOMEM then
280 Result := pthread_mutex_init (L, Attributes'Access);
281 pragma Assert (Result = 0 or else Result = ENOMEM);
283 if Result = ENOMEM then
284 Result := pthread_mutexattr_destroy (Attributes'Access);
288 Result := pthread_mutexattr_destroy (Attributes'Access);
289 pragma Assert (Result = 0);
296 procedure Finalize_Lock (L : not null access Lock) is
297 Result : Interfaces.C.int;
299 Result := pthread_mutex_destroy (L.L'Access);
300 pragma Assert (Result = 0);
303 procedure Finalize_Lock (L : not null access RTS_Lock) is
304 Result : Interfaces.C.int;
306 Result := pthread_mutex_destroy (L);
307 pragma Assert (Result = 0);
315 (L : not null access Lock;
316 Ceiling_Violation : out Boolean)
318 Result : Interfaces.C.int;
320 All_Tasks_Link : Task_Id;
321 Current_Prio : System.Any_Priority;
324 -- Perform ceiling checks only when this is the locking policy in use
326 if Locking_Policy = 'C' then
328 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
329 Current_Prio := Get_Priority (Self_ID);
331 -- If there is no other task, no need to check priorities
333 if All_Tasks_Link /= Null_Task
334 and then L.Ceiling < Interfaces.C.int (Current_Prio)
336 Ceiling_Violation := True;
341 Result := pthread_mutex_lock (L.L'Access);
342 pragma Assert (Result = 0);
344 Ceiling_Violation := False;
348 (L : not null access RTS_Lock;
349 Global_Lock : Boolean := False)
351 Result : Interfaces.C.int;
353 if not Single_Lock or else Global_Lock then
354 Result := pthread_mutex_lock (L);
355 pragma Assert (Result = 0);
359 procedure Write_Lock (T : Task_Id) is
360 Result : Interfaces.C.int;
362 if not Single_Lock then
363 Result := pthread_mutex_lock (T.Common.LL.L'Access);
364 pragma Assert (Result = 0);
373 (L : not null access Lock;
374 Ceiling_Violation : out Boolean)
377 Write_Lock (L, Ceiling_Violation);
384 procedure Unlock (L : not null access Lock) is
385 Result : Interfaces.C.int;
387 Result := pthread_mutex_unlock (L.L'Access);
388 pragma Assert (Result = 0);
392 (L : not null access RTS_Lock;
393 Global_Lock : Boolean := False)
395 Result : Interfaces.C.int;
397 if not Single_Lock or else Global_Lock then
398 Result := pthread_mutex_unlock (L);
399 pragma Assert (Result = 0);
403 procedure Unlock (T : Task_Id) is
404 Result : Interfaces.C.int;
406 if not Single_Lock then
407 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
408 pragma Assert (Result = 0);
416 -- Dynamic priority ceilings are not supported by the underlying system
418 procedure Set_Ceiling
419 (L : not null access Lock;
420 Prio : System.Any_Priority)
422 pragma Unreferenced (L, Prio);
433 Reason : System.Tasking.Task_States)
435 pragma Unreferenced (Reason);
437 Result : Interfaces.C.int;
442 (cond => Self_ID.Common.LL.CV'Access,
443 mutex => (if Single_Lock
444 then Single_RTS_Lock'Access
445 else Self_ID.Common.LL.L'Access));
447 -- EINTR is not considered a failure
449 pragma Assert (Result = 0 or else Result = EINTR);
456 -- This is for use within the run-time system, so abort is assumed to be
457 -- already deferred, and the caller should be holding its own ATCB lock.
459 procedure Timed_Sleep
462 Mode : ST.Delay_Modes;
463 Reason : System.Tasking.Task_States;
464 Timedout : out Boolean;
465 Yielded : out Boolean)
467 pragma Unreferenced (Reason);
469 Base_Time : constant Duration := Monotonic_Clock;
470 Check_Time : Duration := Base_Time;
472 Request : aliased timespec;
473 Result : Interfaces.C.int;
481 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
482 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
484 if Abs_Time > Check_Time then
485 Request := To_Timespec (Abs_Time);
488 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
491 pthread_cond_timedwait
492 (cond => Self_ID.Common.LL.CV'Access,
493 mutex => (if Single_Lock
494 then Single_RTS_Lock'Access
495 else Self_ID.Common.LL.L'Access),
496 abstime => Request'Access);
498 Check_Time := Monotonic_Clock;
499 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
501 if Result = 0 or Result = EINTR then
503 -- Somebody may have called Wakeup for us
509 pragma Assert (Result = ETIMEDOUT);
518 -- This is for use in implementing delay statements, so we assume the
519 -- caller is abort-deferred but is holding no locks.
521 procedure Timed_Delay
524 Mode : ST.Delay_Modes)
526 Base_Time : constant Duration := Monotonic_Clock;
527 Check_Time : Duration := Base_Time;
529 Request : aliased timespec;
530 Result : Interfaces.C.int;
537 Write_Lock (Self_ID);
541 then Time + Check_Time
542 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
544 if Abs_Time > Check_Time then
545 Request := To_Timespec (Abs_Time);
546 Self_ID.Common.State := Delay_Sleep;
549 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
552 pthread_cond_timedwait
553 (cond => Self_ID.Common.LL.CV'Access,
554 mutex => (if Single_Lock
555 then Single_RTS_Lock'Access
556 else Self_ID.Common.LL.L'Access),
557 abstime => Request'Access);
559 Check_Time := Monotonic_Clock;
560 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
562 pragma Assert (Result = 0 or else
563 Result = ETIMEDOUT or else
567 Self_ID.Common.State := Runnable;
579 ---------------------
580 -- Monotonic_Clock --
581 ---------------------
583 function Monotonic_Clock return Duration is
584 TS : aliased timespec;
585 Result : Interfaces.C.int;
587 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
588 pragma Assert (Result = 0);
589 return To_Duration (TS);
596 function RT_Resolution return Duration is
598 -- Returned value must be an integral multiple of Duration'Small (1 ns)
599 -- The following is the best approximation of 1/1024. The clock on the
600 -- DEC Alpha ticks at 1024 Hz.
602 return 0.000_976_563;
609 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
610 pragma Unreferenced (Reason);
611 Result : Interfaces.C.int;
613 Result := pthread_cond_signal (T.Common.LL.CV'Access);
614 pragma Assert (Result = 0);
621 procedure Yield (Do_Yield : Boolean := True) is
622 Result : Interfaces.C.int;
623 pragma Unreferenced (Result);
626 Result := sched_yield;
634 procedure Set_Priority
636 Prio : System.Any_Priority;
637 Loss_Of_Inheritance : Boolean := False)
639 pragma Unreferenced (Loss_Of_Inheritance);
641 Result : Interfaces.C.int;
642 Param : aliased struct_sched_param;
644 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
645 -- Upper case first character of the policy name corresponding to the
646 -- task as set by a Priority_Specific_Dispatching pragma.
649 T.Common.Current_Priority := Prio;
650 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
652 if Dispatching_Policy = 'R'
653 or else Priority_Specific_Policy = 'R'
654 or else Time_Slice_Val > 0
657 pthread_setschedparam
658 (T.Common.LL.Thread, SCHED_RR, Param'Access);
660 elsif Dispatching_Policy = 'F'
661 or else Priority_Specific_Policy = 'F'
662 or else Time_Slice_Val = 0
665 pthread_setschedparam
666 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
670 pthread_setschedparam
671 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
674 pragma Assert (Result = 0);
681 function Get_Priority (T : Task_Id) return System.Any_Priority is
683 return T.Common.Current_Priority;
690 procedure Enter_Task (Self_ID : Task_Id) is
692 Hide_Unhide_Yellow_Zone (Hide => True);
693 Self_ID.Common.LL.Thread := pthread_self;
695 Specific.Set (Self_ID);
702 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
704 return new Ada_Task_Control_Block (Entry_Num);
711 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
713 -----------------------------
714 -- Register_Foreign_Thread --
715 -----------------------------
717 function Register_Foreign_Thread return Task_Id is
719 if Is_Valid_Task then
722 return Register_Foreign_Thread (pthread_self);
724 end Register_Foreign_Thread;
730 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
731 Mutex_Attr : aliased pthread_mutexattr_t;
732 Result : Interfaces.C.int;
733 Cond_Attr : aliased pthread_condattr_t;
736 if not Single_Lock then
737 Result := pthread_mutexattr_init (Mutex_Attr'Access);
738 pragma Assert (Result = 0 or else Result = ENOMEM);
743 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
744 pragma Assert (Result = 0 or else Result = ENOMEM);
752 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
753 pragma Assert (Result = 0);
756 Result := pthread_condattr_init (Cond_Attr'Access);
757 pragma Assert (Result = 0 or else Result = ENOMEM);
762 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
763 pragma Assert (Result = 0 or else Result = ENOMEM);
769 if not Single_Lock then
770 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
771 pragma Assert (Result = 0);
777 Result := pthread_condattr_destroy (Cond_Attr'Access);
778 pragma Assert (Result = 0);
785 procedure Create_Task
787 Wrapper : System.Address;
788 Stack_Size : System.Parameters.Size_Type;
789 Priority : System.Any_Priority;
790 Succeeded : out Boolean)
792 Attributes : aliased pthread_attr_t;
793 Adjusted_Stack_Size : Interfaces.C.size_t;
794 Result : Interfaces.C.int;
795 Param : aliased System.OS_Interface.struct_sched_param;
797 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
798 -- Upper case first character of the policy name corresponding to the
799 -- task as set by a Priority_Specific_Dispatching pragma.
801 use System.Task_Info;
804 -- Account for the Yellow Zone (2 pages) and the guard page right above.
805 -- See Hide_Unhide_Yellow_Zone for the rationale.
807 Adjusted_Stack_Size :=
808 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
810 Result := pthread_attr_init (Attributes'Access);
811 pragma Assert (Result = 0 or else Result = ENOMEM);
819 pthread_attr_setdetachstate
820 (Attributes'Access, PTHREAD_CREATE_DETACHED);
821 pragma Assert (Result = 0);
824 pthread_attr_setstacksize
825 (Attributes'Access, Adjusted_Stack_Size);
826 pragma Assert (Result = 0);
828 Param.sched_priority :=
829 Interfaces.C.int (Underlying_Priorities (Priority));
831 pthread_attr_setschedparam
832 (Attributes'Access, Param'Access);
833 pragma Assert (Result = 0);
835 if Dispatching_Policy = 'R'
836 or else Priority_Specific_Policy = 'R'
837 or else Time_Slice_Val > 0
840 pthread_attr_setschedpolicy
841 (Attributes'Access, System.OS_Interface.SCHED_RR);
843 elsif Dispatching_Policy = 'F'
844 or else Priority_Specific_Policy = 'F'
845 or else Time_Slice_Val = 0
848 pthread_attr_setschedpolicy
849 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
853 pthread_attr_setschedpolicy
854 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
857 pragma Assert (Result = 0);
859 -- Set the scheduling parameters explicitly, since this is the only way
860 -- to force the OS to take e.g. the sched policy and scope attributes
864 pthread_attr_setinheritsched
865 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
866 pragma Assert (Result = 0);
868 T.Common.Current_Priority := Priority;
870 if T.Common.Task_Info /= null then
871 case T.Common.Task_Info.Contention_Scope is
872 when System.Task_Info.Process_Scope =>
874 pthread_attr_setscope
875 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
877 when System.Task_Info.System_Scope =>
879 pthread_attr_setscope
880 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
882 when System.Task_Info.Default_Scope =>
886 pragma Assert (Result = 0);
889 -- Since the initial signal mask of a thread is inherited from the
890 -- creator, and the Environment task has all its signals masked, we
891 -- do not need to manipulate caller's signal mask at this point.
892 -- All tasks in RTS will have All_Tasks_Mask initially.
896 (T.Common.LL.Thread'Access,
898 Thread_Body_Access (Wrapper),
900 pragma Assert (Result = 0 or else Result = EAGAIN);
902 Succeeded := Result = 0;
904 Result := pthread_attr_destroy (Attributes'Access);
905 pragma Assert (Result = 0);
907 if Succeeded and then T.Common.Task_Info /= null then
909 -- ??? We're using a process-wide function to implement a task
910 -- specific characteristic.
912 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
913 Result := bind_to_cpu (Curpid, 0);
915 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
919 Interfaces.C.unsigned_long (
920 Interfaces.Shift_Left
921 (Interfaces.Unsigned_64'(1),
922 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
923 pragma Assert (Result = 0);
932 procedure Finalize_TCB (T : Task_Id) is
933 Result : Interfaces.C.int;
935 Is_Self : constant Boolean := T = Self;
937 procedure Free is new
938 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
941 if not Single_Lock then
942 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
943 pragma Assert (Result = 0);
946 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
947 pragma Assert (Result = 0);
949 if T.Known_Tasks_Index /= -1 then
950 Known_Tasks (T.Known_Tasks_Index) := null;
964 procedure Exit_Task is
967 Hide_Unhide_Yellow_Zone (Hide => False);
974 procedure Abort_Task (T : Task_Id) is
975 Result : Interfaces.C.int;
977 if Abort_Handler_Installed then
978 Result := pthread_kill (T.Common.LL.Thread,
979 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
980 pragma Assert (Result = 0);
988 procedure Initialize (S : in out Suspension_Object) is
989 Mutex_Attr : aliased pthread_mutexattr_t;
990 Cond_Attr : aliased pthread_condattr_t;
991 Result : Interfaces.C.int;
994 -- Initialize internal state (always to False (RM D.10(6)))
999 -- Initialize internal mutex
1001 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1002 pragma Assert (Result = 0 or else Result = ENOMEM);
1004 if Result = ENOMEM then
1005 raise Storage_Error;
1008 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1009 pragma Assert (Result = 0 or else Result = ENOMEM);
1011 if Result = ENOMEM then
1012 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1013 raise Storage_Error;
1016 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1017 pragma Assert (Result = 0);
1019 -- Initialize internal condition variable
1021 Result := pthread_condattr_init (Cond_Attr'Access);
1022 pragma Assert (Result = 0 or else Result = ENOMEM);
1024 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1026 pragma Assert (Result = 0 or else Result = ENOMEM);
1029 Result := pthread_mutex_destroy (S.L'Access);
1030 pragma Assert (Result = 0);
1032 if Result = ENOMEM then
1033 raise Storage_Error;
1042 procedure Finalize (S : in out Suspension_Object) is
1043 Result : Interfaces.C.int;
1046 -- Destroy internal mutex
1048 Result := pthread_mutex_destroy (S.L'Access);
1049 pragma Assert (Result = 0);
1051 -- Destroy internal condition variable
1053 Result := pthread_cond_destroy (S.CV'Access);
1054 pragma Assert (Result = 0);
1061 function Current_State (S : Suspension_Object) return Boolean is
1063 -- We do not want to use lock on this read operation. State is marked
1064 -- as Atomic so that we ensure that the value retrieved is correct.
1073 procedure Set_False (S : in out Suspension_Object) is
1074 Result : Interfaces.C.int;
1077 SSL.Abort_Defer.all;
1079 Result := pthread_mutex_lock (S.L'Access);
1080 pragma Assert (Result = 0);
1084 Result := pthread_mutex_unlock (S.L'Access);
1085 pragma Assert (Result = 0);
1087 SSL.Abort_Undefer.all;
1094 procedure Set_True (S : in out Suspension_Object) is
1095 Result : Interfaces.C.int;
1098 SSL.Abort_Defer.all;
1100 Result := pthread_mutex_lock (S.L'Access);
1101 pragma Assert (Result = 0);
1103 -- If there is already a task waiting on this suspension object then we
1104 -- resume it, leaving the state of the suspension object to False, as
1105 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1111 Result := pthread_cond_signal (S.CV'Access);
1112 pragma Assert (Result = 0);
1118 Result := pthread_mutex_unlock (S.L'Access);
1119 pragma Assert (Result = 0);
1121 SSL.Abort_Undefer.all;
1124 ------------------------
1125 -- Suspend_Until_True --
1126 ------------------------
1128 procedure Suspend_Until_True (S : in out Suspension_Object) is
1129 Result : Interfaces.C.int;
1132 SSL.Abort_Defer.all;
1134 Result := pthread_mutex_lock (S.L'Access);
1135 pragma Assert (Result = 0);
1139 -- Program_Error must be raised upon calling Suspend_Until_True
1140 -- if another task is already waiting on that suspension object
1143 Result := pthread_mutex_unlock (S.L'Access);
1144 pragma Assert (Result = 0);
1146 SSL.Abort_Undefer.all;
1148 raise Program_Error;
1151 -- Suspend the task if the state is False. Otherwise, the task
1152 -- continues its execution, and the state of the suspension object
1153 -- is set to False (RM D.10(9)).
1161 -- Loop in case pthread_cond_wait returns earlier than expected
1162 -- (e.g. in case of EINTR caused by a signal).
1164 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1165 pragma Assert (Result = 0 or else Result = EINTR);
1167 exit when not S.Waiting;
1171 Result := pthread_mutex_unlock (S.L'Access);
1172 pragma Assert (Result = 0);
1174 SSL.Abort_Undefer.all;
1176 end Suspend_Until_True;
1184 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1185 pragma Unreferenced (Self_ID);
1190 --------------------
1191 -- Check_No_Locks --
1192 --------------------
1194 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1195 pragma Unreferenced (Self_ID);
1200 ----------------------
1201 -- Environment_Task --
1202 ----------------------
1204 function Environment_Task return Task_Id is
1206 return Environment_Task_Id;
1207 end Environment_Task;
1213 procedure Lock_RTS is
1215 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1222 procedure Unlock_RTS is
1224 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1231 function Suspend_Task
1233 Thread_Self : Thread_Id) return Boolean
1235 pragma Unreferenced (T, Thread_Self);
1244 function Resume_Task
1246 Thread_Self : Thread_Id) return Boolean
1248 pragma Unreferenced (T, Thread_Self);
1253 --------------------
1254 -- Stop_All_Tasks --
1255 --------------------
1257 procedure Stop_All_Tasks is
1266 function Stop_Task (T : ST.Task_Id) return Boolean is
1267 pragma Unreferenced (T);
1276 function Continue_Task (T : ST.Task_Id) return Boolean is
1277 pragma Unreferenced (T);
1286 procedure Initialize (Environment_Task : Task_Id) is
1287 act : aliased struct_sigaction;
1288 old_act : aliased struct_sigaction;
1289 Tmp_Set : aliased sigset_t;
1290 Result : Interfaces.C.int;
1293 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1294 pragma Import (C, State, "__gnat_get_interrupt_state");
1295 -- Get interrupt state. Defined in a-init.c. The input argument is
1296 -- the interrupt number, and the result is one of the following:
1298 Default : constant Character := 's';
1299 -- 'n' this interrupt not set by any Interrupt_State pragma
1300 -- 'u' Interrupt_State pragma set state to User
1301 -- 'r' Interrupt_State pragma set state to Runtime
1302 -- 's' Interrupt_State pragma set state to System (use "default"
1306 Environment_Task_Id := Environment_Task;
1308 Interrupt_Management.Initialize;
1310 -- Prepare the set of signals that should unblocked in all tasks
1312 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1313 pragma Assert (Result = 0);
1315 for J in Interrupt_Management.Interrupt_ID loop
1316 if System.Interrupt_Management.Keep_Unmasked (J) then
1317 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1318 pragma Assert (Result = 0);
1324 -- Initialize the lock used to synchronize chain of all ATCBs
1326 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1328 Specific.Initialize (Environment_Task);
1330 -- Make environment task known here because it doesn't go through
1331 -- Activate_Tasks, which does it for all other tasks.
1333 Known_Tasks (Known_Tasks'First) := Environment_Task;
1334 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1336 Enter_Task (Environment_Task);
1339 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1342 act.sa_handler := Abort_Handler'Address;
1344 Result := sigemptyset (Tmp_Set'Access);
1345 pragma Assert (Result = 0);
1346 act.sa_mask := Tmp_Set;
1350 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1351 act'Unchecked_Access,
1352 old_act'Unchecked_Access);
1353 pragma Assert (Result = 0);
1354 Abort_Handler_Installed := True;
1358 -----------------------
1359 -- Set_Task_Affinity --
1360 -----------------------
1362 procedure Set_Task_Affinity (T : ST.Task_Id) is
1363 pragma Unreferenced (T);
1366 -- Setting task affinity is not supported by the underlying system
1369 end Set_Task_Affinity;
1370 end System.Task_Primitives.Operations;