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;
711 Specific.Set (Self_ID);
718 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
720 return new Ada_Task_Control_Block (Entry_Num);
727 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
729 -----------------------------
730 -- Register_Foreign_Thread --
731 -----------------------------
733 function Register_Foreign_Thread return Task_Id is
735 if Is_Valid_Task then
738 return Register_Foreign_Thread (pthread_self);
740 end Register_Foreign_Thread;
746 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
747 Mutex_Attr : aliased pthread_mutexattr_t;
748 Result : Interfaces.C.int;
749 Cond_Attr : aliased pthread_condattr_t;
752 if not Single_Lock then
753 Result := pthread_mutexattr_init (Mutex_Attr'Access);
754 pragma Assert (Result = 0 or else Result = ENOMEM);
759 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
760 pragma Assert (Result = 0 or else Result = ENOMEM);
768 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
769 pragma Assert (Result = 0);
772 Result := pthread_condattr_init (Cond_Attr'Access);
773 pragma Assert (Result = 0 or else Result = ENOMEM);
778 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
779 pragma Assert (Result = 0 or else Result = ENOMEM);
785 if not Single_Lock then
786 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
787 pragma Assert (Result = 0);
793 Result := pthread_condattr_destroy (Cond_Attr'Access);
794 pragma Assert (Result = 0);
801 procedure Create_Task
803 Wrapper : System.Address;
804 Stack_Size : System.Parameters.Size_Type;
805 Priority : System.Any_Priority;
806 Succeeded : out Boolean)
808 Attributes : aliased pthread_attr_t;
809 Adjusted_Stack_Size : Interfaces.C.size_t;
810 Result : Interfaces.C.int;
811 Param : aliased System.OS_Interface.struct_sched_param;
813 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
814 -- Upper case first character of the policy name corresponding to the
815 -- task as set by a Priority_Specific_Dispatching pragma.
817 use System.Task_Info;
820 -- Account for the Yellow Zone (2 pages) and the guard page right above.
821 -- See Hide_Unhide_Yellow_Zone for the rationale.
823 Adjusted_Stack_Size :=
824 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
826 Result := pthread_attr_init (Attributes'Access);
827 pragma Assert (Result = 0 or else Result = ENOMEM);
835 pthread_attr_setdetachstate
836 (Attributes'Access, PTHREAD_CREATE_DETACHED);
837 pragma Assert (Result = 0);
840 pthread_attr_setstacksize
841 (Attributes'Access, Adjusted_Stack_Size);
842 pragma Assert (Result = 0);
844 Param.sched_priority :=
845 Interfaces.C.int (Underlying_Priorities (Priority));
847 pthread_attr_setschedparam
848 (Attributes'Access, Param'Access);
849 pragma Assert (Result = 0);
851 if Dispatching_Policy = 'R'
852 or else Priority_Specific_Policy = 'R'
853 or else Time_Slice_Val > 0
856 pthread_attr_setschedpolicy
857 (Attributes'Access, System.OS_Interface.SCHED_RR);
859 elsif Dispatching_Policy = 'F'
860 or else Priority_Specific_Policy = 'F'
861 or else Time_Slice_Val = 0
864 pthread_attr_setschedpolicy
865 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
869 pthread_attr_setschedpolicy
870 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
873 pragma Assert (Result = 0);
875 -- Set the scheduling parameters explicitly, since this is the only way
876 -- to force the OS to take e.g. the sched policy and scope attributes
880 pthread_attr_setinheritsched
881 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
882 pragma Assert (Result = 0);
884 T.Common.Current_Priority := Priority;
886 if T.Common.Task_Info /= null then
887 case T.Common.Task_Info.Contention_Scope is
888 when System.Task_Info.Process_Scope =>
890 pthread_attr_setscope
891 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
893 when System.Task_Info.System_Scope =>
895 pthread_attr_setscope
896 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
898 when System.Task_Info.Default_Scope =>
902 pragma Assert (Result = 0);
905 -- Since the initial signal mask of a thread is inherited from the
906 -- creator, and the Environment task has all its signals masked, we
907 -- do not need to manipulate caller's signal mask at this point.
908 -- All tasks in RTS will have All_Tasks_Mask initially.
912 (T.Common.LL.Thread'Access,
914 Thread_Body_Access (Wrapper),
916 pragma Assert (Result = 0 or else Result = EAGAIN);
918 Succeeded := Result = 0;
920 Result := pthread_attr_destroy (Attributes'Access);
921 pragma Assert (Result = 0);
923 if Succeeded and then T.Common.Task_Info /= null then
925 -- ??? We're using a process-wide function to implement a task
926 -- specific characteristic.
928 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
929 Result := bind_to_cpu (Curpid, 0);
931 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
935 Interfaces.C.unsigned_long (
936 Interfaces.Shift_Left
937 (Interfaces.Unsigned_64'(1),
938 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
939 pragma Assert (Result = 0);
948 procedure Finalize_TCB (T : Task_Id) is
949 Result : Interfaces.C.int;
951 Is_Self : constant Boolean := T = Self;
953 procedure Free is new
954 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
957 if not Single_Lock then
958 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
959 pragma Assert (Result = 0);
962 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
963 pragma Assert (Result = 0);
965 if T.Known_Tasks_Index /= -1 then
966 Known_Tasks (T.Known_Tasks_Index) := null;
980 procedure Exit_Task is
983 Hide_Unhide_Yellow_Zone (Hide => False);
990 procedure Abort_Task (T : Task_Id) is
991 Result : Interfaces.C.int;
993 Result := pthread_kill (T.Common.LL.Thread,
994 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
995 pragma Assert (Result = 0);
1002 procedure Initialize (S : in out Suspension_Object) is
1003 Mutex_Attr : aliased pthread_mutexattr_t;
1004 Cond_Attr : aliased pthread_condattr_t;
1005 Result : Interfaces.C.int;
1008 -- Initialize internal state (always to False (RM D.10(6)))
1013 -- Initialize internal mutex
1015 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1016 pragma Assert (Result = 0 or else Result = ENOMEM);
1018 if Result = ENOMEM then
1019 raise Storage_Error;
1022 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1023 pragma Assert (Result = 0 or else Result = ENOMEM);
1025 if Result = ENOMEM then
1026 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1027 raise Storage_Error;
1030 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1031 pragma Assert (Result = 0);
1033 -- Initialize internal condition variable
1035 Result := pthread_condattr_init (Cond_Attr'Access);
1036 pragma Assert (Result = 0 or else Result = ENOMEM);
1038 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1040 pragma Assert (Result = 0 or else Result = ENOMEM);
1043 Result := pthread_mutex_destroy (S.L'Access);
1044 pragma Assert (Result = 0);
1046 if Result = ENOMEM then
1047 raise Storage_Error;
1056 procedure Finalize (S : in out Suspension_Object) is
1057 Result : Interfaces.C.int;
1060 -- Destroy internal mutex
1062 Result := pthread_mutex_destroy (S.L'Access);
1063 pragma Assert (Result = 0);
1065 -- Destroy internal condition variable
1067 Result := pthread_cond_destroy (S.CV'Access);
1068 pragma Assert (Result = 0);
1075 function Current_State (S : Suspension_Object) return Boolean is
1077 -- We do not want to use lock on this read operation. State is marked
1078 -- as Atomic so that we ensure that the value retrieved is correct.
1087 procedure Set_False (S : in out Suspension_Object) is
1088 Result : Interfaces.C.int;
1091 SSL.Abort_Defer.all;
1093 Result := pthread_mutex_lock (S.L'Access);
1094 pragma Assert (Result = 0);
1098 Result := pthread_mutex_unlock (S.L'Access);
1099 pragma Assert (Result = 0);
1101 SSL.Abort_Undefer.all;
1108 procedure Set_True (S : in out Suspension_Object) is
1109 Result : Interfaces.C.int;
1112 SSL.Abort_Defer.all;
1114 Result := pthread_mutex_lock (S.L'Access);
1115 pragma Assert (Result = 0);
1117 -- If there is already a task waiting on this suspension object then we
1118 -- resume it, leaving the state of the suspension object to False, as
1119 -- specified in (RM D.10(9)). Otherwise, leave the state set to True.
1125 Result := pthread_cond_signal (S.CV'Access);
1126 pragma Assert (Result = 0);
1132 Result := pthread_mutex_unlock (S.L'Access);
1133 pragma Assert (Result = 0);
1135 SSL.Abort_Undefer.all;
1138 ------------------------
1139 -- Suspend_Until_True --
1140 ------------------------
1142 procedure Suspend_Until_True (S : in out Suspension_Object) is
1143 Result : Interfaces.C.int;
1146 SSL.Abort_Defer.all;
1148 Result := pthread_mutex_lock (S.L'Access);
1149 pragma Assert (Result = 0);
1153 -- Program_Error must be raised upon calling Suspend_Until_True
1154 -- if another task is already waiting on that suspension object
1157 Result := pthread_mutex_unlock (S.L'Access);
1158 pragma Assert (Result = 0);
1160 SSL.Abort_Undefer.all;
1162 raise Program_Error;
1165 -- Suspend the task if the state is False. Otherwise, the task
1166 -- continues its execution, and the state of the suspension object
1167 -- is set to False (RM D.10(9)).
1175 -- loop in case pthread_cond_wait returns earlier than
1176 -- expected (e.g. in case of EINTR caused by a signal).
1178 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1179 pragma Assert (Result = 0 or else Result = EINTR);
1181 exit when not S.Waiting;
1185 Result := pthread_mutex_unlock (S.L'Access);
1186 pragma Assert (Result = 0);
1188 SSL.Abort_Undefer.all;
1190 end Suspend_Until_True;
1198 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1199 pragma Unreferenced (Self_ID);
1204 --------------------
1205 -- Check_No_Locks --
1206 --------------------
1208 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1209 pragma Unreferenced (Self_ID);
1214 ----------------------
1215 -- Environment_Task --
1216 ----------------------
1218 function Environment_Task return Task_Id is
1220 return Environment_Task_Id;
1221 end Environment_Task;
1227 procedure Lock_RTS is
1229 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1236 procedure Unlock_RTS is
1238 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1245 function Suspend_Task
1247 Thread_Self : Thread_Id) return Boolean
1249 pragma Unreferenced (T, Thread_Self);
1258 function Resume_Task
1260 Thread_Self : Thread_Id) return Boolean
1262 pragma Unreferenced (T, Thread_Self);
1267 --------------------
1268 -- Stop_All_Tasks --
1269 --------------------
1271 procedure Stop_All_Tasks is
1280 function Stop_Task (T : ST.Task_Id) return Boolean is
1281 pragma Unreferenced (T);
1290 function Continue_Task (T : ST.Task_Id) return Boolean is
1291 pragma Unreferenced (T);
1300 procedure Initialize (Environment_Task : Task_Id) is
1301 act : aliased struct_sigaction;
1302 old_act : aliased struct_sigaction;
1303 Tmp_Set : aliased sigset_t;
1304 Result : Interfaces.C.int;
1307 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1308 pragma Import (C, State, "__gnat_get_interrupt_state");
1309 -- Get interrupt state. Defined in a-init.c. The input argument is
1310 -- the interrupt number, and the result is one of the following:
1312 Default : constant Character := 's';
1313 -- 'n' this interrupt not set by any Interrupt_State pragma
1314 -- 'u' Interrupt_State pragma set state to User
1315 -- 'r' Interrupt_State pragma set state to Runtime
1316 -- 's' Interrupt_State pragma set state to System (use "default"
1320 Environment_Task_Id := Environment_Task;
1322 Interrupt_Management.Initialize;
1324 -- Prepare the set of signals that should unblocked in all tasks
1326 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1327 pragma Assert (Result = 0);
1329 for J in Interrupt_Management.Interrupt_ID loop
1330 if System.Interrupt_Management.Keep_Unmasked (J) then
1331 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1332 pragma Assert (Result = 0);
1338 -- Initialize the lock used to synchronize chain of all ATCBs
1340 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1342 Specific.Initialize (Environment_Task);
1344 -- Make environment task known here because it doesn't go through
1345 -- Activate_Tasks, which does it for all other tasks.
1347 Known_Tasks (Known_Tasks'First) := Environment_Task;
1348 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1350 Enter_Task (Environment_Task);
1352 -- Install the abort-signal handler
1355 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1358 act.sa_handler := Abort_Handler'Address;
1360 Result := sigemptyset (Tmp_Set'Access);
1361 pragma Assert (Result = 0);
1362 act.sa_mask := Tmp_Set;
1366 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1367 act'Unchecked_Access,
1368 old_act'Unchecked_Access);
1369 pragma Assert (Result = 0);
1373 end System.Task_Primitives.Operations;