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 IRIX (pthread library) 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_Conversion;
42 with Ada.Unchecked_Deallocation;
46 with System.Task_Info;
47 with System.Tasking.Debug;
48 with System.Interrupt_Management;
49 with System.OS_Primitives;
52 with System.Soft_Links;
53 -- We use System.Soft_Links instead of System.Tasking.Initialization
54 -- because the later is a higher level package that we shouldn't depend on.
55 -- For example when using the restricted run time, it is replaced by
56 -- System.Tasking.Restricted.Stages.
58 package body System.Task_Primitives.Operations is
60 package SSL renames System.Soft_Links;
63 use System.Tasking.Debug;
65 use System.OS_Interface;
66 use System.OS_Primitives;
67 use System.Parameters;
73 -- The followings are logically constants, but need to be initialized
76 Single_RTS_Lock : aliased RTS_Lock;
77 -- This is a lock to allow only one thread of control in the RTS at
78 -- a time; it is used to execute in mutual exclusion from all other tasks.
79 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
81 ATCB_Key : aliased pthread_key_t;
82 -- Key used to find the Ada Task_Id associated with a thread
84 Environment_Task_Id : Task_Id;
85 -- A variable to hold Task_Id for the environment task
87 Locking_Policy : Character;
88 pragma Import (C, Locking_Policy, "__gl_locking_policy");
90 Time_Slice_Val : Integer;
91 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
93 Dispatching_Policy : Character;
94 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
96 Real_Time_Clock_Id : constant clockid_t := CLOCK_REALTIME;
98 Unblocked_Signal_Mask : aliased sigset_t;
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 function To_Address is
145 new Ada.Unchecked_Conversion (Task_Id, System.Address);
147 procedure Abort_Handler (Sig : Signal);
148 -- Signal handler used to implement asynchronous abort
154 procedure Abort_Handler (Sig : Signal) is
155 pragma Unreferenced (Sig);
157 T : constant Task_Id := Self;
158 Result : Interfaces.C.int;
159 Old_Set : aliased sigset_t;
162 -- It is not safe to raise an exception when using ZCX and the GCC
163 -- exception handling mechanism.
165 if ZCX_By_Default and then GCC_ZCX_Support then
169 if T.Deferral_Level = 0
170 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
172 -- Make sure signals used for RTS internal purpose are unmasked
174 Result := pthread_sigmask
176 Unblocked_Signal_Mask'Access,
178 pragma Assert (Result = 0);
180 raise Standard'Abort_Signal;
188 -- The underlying thread system sets a guard page at the
189 -- bottom of a thread stack, so nothing is needed.
191 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
192 pragma Unreferenced (On);
193 pragma Unreferenced (T);
202 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
204 return T.Common.LL.Thread;
211 function Self return Task_Id renames Specific.Self;
213 ---------------------
214 -- Initialize_Lock --
215 ---------------------
217 -- Note: mutexes and cond_variables needed per-task basis are initialized
218 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
219 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
220 -- status change of RTS. Therefore raising Storage_Error in the following
221 -- routines should be able to be handled safely.
223 procedure Initialize_Lock
224 (Prio : System.Any_Priority;
225 L : not null access Lock)
227 Attributes : aliased pthread_mutexattr_t;
228 Result : Interfaces.C.int;
231 Result := pthread_mutexattr_init (Attributes'Access);
232 pragma Assert (Result = 0 or else Result = ENOMEM);
234 if Result = ENOMEM then
238 if Locking_Policy = 'C' then
240 pthread_mutexattr_setprotocol
241 (Attributes'Access, PTHREAD_PRIO_PROTECT);
242 pragma Assert (Result = 0);
245 pthread_mutexattr_setprioceiling
246 (Attributes'Access, Interfaces.C.int (Prio));
247 pragma Assert (Result = 0);
250 Result := pthread_mutex_init (L, Attributes'Access);
251 pragma Assert (Result = 0 or else Result = ENOMEM);
253 if Result = ENOMEM then
254 Result := pthread_mutexattr_destroy (Attributes'Access);
258 Result := pthread_mutexattr_destroy (Attributes'Access);
259 pragma Assert (Result = 0);
262 procedure Initialize_Lock
263 (L : not null access RTS_Lock;
266 pragma Unreferenced (Level);
268 Attributes : aliased pthread_mutexattr_t;
269 Result : Interfaces.C.int;
272 Result := pthread_mutexattr_init (Attributes'Access);
273 pragma Assert (Result = 0 or else Result = ENOMEM);
275 if Result = ENOMEM then
279 if Locking_Policy = 'C' then
280 Result := pthread_mutexattr_setprotocol
281 (Attributes'Access, PTHREAD_PRIO_PROTECT);
282 pragma Assert (Result = 0);
284 Result := pthread_mutexattr_setprioceiling
285 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
286 pragma Assert (Result = 0);
289 Result := pthread_mutex_init (L, Attributes'Access);
291 pragma Assert (Result = 0 or else Result = ENOMEM);
293 if Result = ENOMEM then
294 Result := pthread_mutexattr_destroy (Attributes'Access);
298 Result := pthread_mutexattr_destroy (Attributes'Access);
299 pragma Assert (Result = 0);
306 procedure Finalize_Lock (L : not null access Lock) is
307 Result : Interfaces.C.int;
309 Result := pthread_mutex_destroy (L);
310 pragma Assert (Result = 0);
313 procedure Finalize_Lock (L : not null access RTS_Lock) is
314 Result : Interfaces.C.int;
316 Result := pthread_mutex_destroy (L);
317 pragma Assert (Result = 0);
325 (L : not null access Lock; Ceiling_Violation : out Boolean)
327 Result : Interfaces.C.int;
330 Result := pthread_mutex_lock (L);
331 Ceiling_Violation := Result = EINVAL;
333 -- Assumes the cause of EINVAL is a priority ceiling violation
335 pragma Assert (Result = 0 or else Result = EINVAL);
339 (L : not null access RTS_Lock;
340 Global_Lock : Boolean := False)
342 Result : Interfaces.C.int;
344 if not Single_Lock or else Global_Lock then
345 Result := pthread_mutex_lock (L);
346 pragma Assert (Result = 0);
350 procedure Write_Lock (T : Task_Id) is
351 Result : Interfaces.C.int;
353 if not Single_Lock then
354 Result := pthread_mutex_lock (T.Common.LL.L'Access);
355 pragma Assert (Result = 0);
364 (L : not null access Lock; Ceiling_Violation : out Boolean) is
366 Write_Lock (L, Ceiling_Violation);
373 procedure Unlock (L : not null access Lock) is
374 Result : Interfaces.C.int;
376 Result := pthread_mutex_unlock (L);
377 pragma Assert (Result = 0);
381 (L : not null access RTS_Lock;
382 Global_Lock : Boolean := False)
384 Result : Interfaces.C.int;
386 if not Single_Lock or else Global_Lock then
387 Result := pthread_mutex_unlock (L);
388 pragma Assert (Result = 0);
392 procedure Unlock (T : Task_Id) is
393 Result : Interfaces.C.int;
395 if not Single_Lock then
396 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
397 pragma Assert (Result = 0);
405 -- Dynamic priority ceilings are not supported by the underlying system
407 procedure Set_Ceiling
408 (L : not null access Lock;
409 Prio : System.Any_Priority)
411 pragma Unreferenced (L, Prio);
421 (Self_ID : ST.Task_Id;
422 Reason : System.Tasking.Task_States)
424 pragma Unreferenced (Reason);
425 Result : Interfaces.C.int;
431 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
435 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
438 -- EINTR is not considered a failure
440 pragma Assert (Result = 0 or else Result = EINTR);
447 procedure Timed_Sleep
450 Mode : ST.Delay_Modes;
451 Reason : Task_States;
452 Timedout : out Boolean;
453 Yielded : out Boolean)
455 pragma Unreferenced (Reason);
457 Base_Time : constant Duration := Monotonic_Clock;
458 Check_Time : Duration := Base_Time;
460 Request : aliased timespec;
461 Result : Interfaces.C.int;
467 if Mode = Relative then
468 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
470 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
473 if Abs_Time > Check_Time then
474 Request := To_Timespec (Abs_Time);
477 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
481 pthread_cond_timedwait
482 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
487 pthread_cond_timedwait
488 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
492 Check_Time := Monotonic_Clock;
493 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
495 if Result = 0 or else errno = EINTR then
507 -- This is for use in implementing delay statements, so we assume
508 -- the caller is abort-deferred but is holding no locks.
510 procedure Timed_Delay
513 Mode : ST.Delay_Modes)
515 Base_Time : constant Duration := Monotonic_Clock;
516 Check_Time : Duration := Base_Time;
518 Request : aliased timespec;
519 Result : Interfaces.C.int;
526 Write_Lock (Self_ID);
528 if Mode = Relative then
529 Abs_Time := Time + Check_Time;
531 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
534 if Abs_Time > Check_Time then
535 Request := To_Timespec (Abs_Time);
536 Self_ID.Common.State := Delay_Sleep;
539 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
542 Result := pthread_cond_timedwait
543 (Self_ID.Common.LL.CV'Access,
544 Single_RTS_Lock'Access,
547 Result := pthread_cond_timedwait
548 (Self_ID.Common.LL.CV'Access,
549 Self_ID.Common.LL.L'Access,
553 Check_Time := Monotonic_Clock;
554 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
556 pragma Assert (Result = 0
557 or else Result = ETIMEDOUT
558 or else Result = EINTR);
561 Self_ID.Common.State := Runnable;
573 ---------------------
574 -- Monotonic_Clock --
575 ---------------------
577 function Monotonic_Clock return Duration is
578 TS : aliased timespec;
579 Result : Interfaces.C.int;
581 Result := clock_gettime (Real_Time_Clock_Id, TS'Unchecked_Access);
582 pragma Assert (Result = 0);
583 return To_Duration (TS);
590 function RT_Resolution return Duration is
592 -- The clock_getres (Real_Time_Clock_Id) function appears to return
593 -- the interrupt resolution of the realtime clock and not the actual
594 -- resolution of reading the clock. Even though this last value is
595 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
596 -- have a microsecond resolution or better.
598 -- ??? We should figure out a method to return the right value on
608 procedure Wakeup (T : ST.Task_Id; Reason : System.Tasking.Task_States) is
609 pragma Unreferenced (Reason);
610 Result : Interfaces.C.int;
612 Result := pthread_cond_signal (T.Common.LL.CV'Access);
613 pragma Assert (Result = 0);
620 procedure Yield (Do_Yield : Boolean := True) is
621 Result : Interfaces.C.int;
622 pragma Unreferenced (Result);
625 Result := sched_yield;
633 procedure Set_Priority
635 Prio : System.Any_Priority;
636 Loss_Of_Inheritance : Boolean := False)
638 pragma Unreferenced (Loss_Of_Inheritance);
640 Result : Interfaces.C.int;
641 Param : aliased struct_sched_param;
642 Sched_Policy : Interfaces.C.int;
644 use type System.Task_Info.Task_Info_Type;
646 function To_Int is new Ada.Unchecked_Conversion
647 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
649 function Get_Policy (Prio : System.Any_Priority) return Character;
650 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
651 -- Get priority specific dispatching policy
653 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
654 -- Upper case first character of the policy name corresponding to the
655 -- task as set by a Priority_Specific_Dispatching pragma.
658 T.Common.Current_Priority := Prio;
659 Param.sched_priority := Interfaces.C.int (Prio);
661 if T.Common.Task_Info /= null then
662 Sched_Policy := To_Int (T.Common.Task_Info.Policy);
664 elsif Dispatching_Policy = 'R'
665 or else Priority_Specific_Policy = 'R'
666 or else Time_Slice_Val > 0
668 Sched_Policy := SCHED_RR;
671 Sched_Policy := SCHED_FIFO;
674 Result := pthread_setschedparam (T.Common.LL.Thread, Sched_Policy,
676 pragma Assert (Result = 0);
683 function Get_Priority (T : Task_Id) return System.Any_Priority is
685 return T.Common.Current_Priority;
692 procedure Enter_Task (Self_ID : Task_Id) is
693 Result : Interfaces.C.int;
695 function To_Int is new Ada.Unchecked_Conversion
696 (System.Task_Info.CPU_Number, Interfaces.C.int);
698 use System.Task_Info;
701 Self_ID.Common.LL.Thread := pthread_self;
702 Specific.Set (Self_ID);
704 if Self_ID.Common.Task_Info /= null
705 and then Self_ID.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
706 and then Self_ID.Common.Task_Info.Runon_CPU /= ANY_CPU
708 Result := pthread_setrunon_np
709 (To_Int (Self_ID.Common.Task_Info.Runon_CPU));
710 pragma Assert (Result = 0);
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 Result : Interfaces.C.int;
748 Cond_Attr : aliased pthread_condattr_t;
751 if not Single_Lock then
752 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
755 Result := pthread_condattr_init (Cond_Attr'Access);
756 pragma Assert (Result = 0 or else Result = ENOMEM);
760 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
761 pragma Assert (Result = 0 or else Result = ENOMEM);
767 if not Single_Lock then
768 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
769 pragma Assert (Result = 0);
775 Result := pthread_condattr_destroy (Cond_Attr'Access);
776 pragma Assert (Result = 0);
783 procedure Create_Task
785 Wrapper : System.Address;
786 Stack_Size : System.Parameters.Size_Type;
787 Priority : System.Any_Priority;
788 Succeeded : out Boolean)
790 use System.Task_Info;
792 Attributes : aliased pthread_attr_t;
793 Sched_Param : aliased struct_sched_param;
794 Result : Interfaces.C.int;
796 function Thread_Body_Access is new
797 Ada.Unchecked_Conversion (System.Address, Thread_Body);
798 function To_Int is new Ada.Unchecked_Conversion
799 (System.Task_Info.Thread_Scheduling_Scope, Interfaces.C.int);
800 function To_Int is new Ada.Unchecked_Conversion
801 (System.Task_Info.Thread_Scheduling_Inheritance, Interfaces.C.int);
802 function To_Int is new Ada.Unchecked_Conversion
803 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
806 Result := pthread_attr_init (Attributes'Access);
807 pragma Assert (Result = 0 or else Result = ENOMEM);
815 pthread_attr_setdetachstate
816 (Attributes'Access, PTHREAD_CREATE_DETACHED);
817 pragma Assert (Result = 0);
820 pthread_attr_setstacksize
821 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
822 pragma Assert (Result = 0);
824 if T.Common.Task_Info /= null then
826 pthread_attr_setscope
827 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
828 pragma Assert (Result = 0);
831 pthread_attr_setinheritsched
832 (Attributes'Access, To_Int (T.Common.Task_Info.Inheritance));
833 pragma Assert (Result = 0);
836 pthread_attr_setschedpolicy
837 (Attributes'Access, To_Int (T.Common.Task_Info.Policy));
838 pragma Assert (Result = 0);
840 Sched_Param.sched_priority :=
841 Interfaces.C.int (T.Common.Task_Info.Priority);
844 pthread_attr_setschedparam
845 (Attributes'Access, Sched_Param'Access);
846 pragma Assert (Result = 0);
849 -- Since the initial signal mask of a thread is inherited from the
850 -- creator, and the Environment task has all its signals masked, we
851 -- do not need to manipulate caller's signal mask at this point.
852 -- All tasks in RTS will have All_Tasks_Mask initially.
856 (T.Common.LL.Thread'Access,
858 Thread_Body_Access (Wrapper),
862 and then T.Common.Task_Info /= null
863 and then T.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
865 -- The pthread_create call may have failed because we asked for a
866 -- system scope pthread and none were available (probably because
867 -- the program was not executed by the superuser). Let's try for
868 -- a process scope pthread instead of raising Tasking_Error.
871 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
872 System.IO.Put ("""");
873 System.IO.Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len));
874 System.IO.Put_Line (""" could not be honored. ");
875 System.IO.Put_Line ("Scope changed to PTHREAD_SCOPE_PROCESS");
877 T.Common.Task_Info.Scope := PTHREAD_SCOPE_PROCESS;
879 pthread_attr_setscope
880 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
881 pragma Assert (Result = 0);
885 (T.Common.LL.Thread'Access,
887 Thread_Body_Access (Wrapper),
891 pragma Assert (Result = 0 or else Result = EAGAIN);
893 Succeeded := Result = 0;
897 -- The following needs significant commenting ???
899 if T.Common.Task_Info /= null then
900 T.Common.Base_Priority := T.Common.Task_Info.Priority;
901 Set_Priority (T, T.Common.Task_Info.Priority);
903 Set_Priority (T, Priority);
907 Result := pthread_attr_destroy (Attributes'Access);
908 pragma Assert (Result = 0);
915 procedure Finalize_TCB (T : Task_Id) is
916 Result : Interfaces.C.int;
918 Is_Self : constant Boolean := T = Self;
920 procedure Free is new
921 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
924 if not Single_Lock then
925 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
926 pragma Assert (Result = 0);
929 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
930 pragma Assert (Result = 0);
932 if T.Known_Tasks_Index /= -1 then
933 Known_Tasks (T.Known_Tasks_Index) := null;
947 procedure Exit_Task is
956 procedure Abort_Task (T : Task_Id) is
957 Result : Interfaces.C.int;
962 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
963 pragma Assert (Result = 0);
970 procedure Initialize (S : in out Suspension_Object) is
971 Mutex_Attr : aliased pthread_mutexattr_t;
972 Cond_Attr : aliased pthread_condattr_t;
973 Result : Interfaces.C.int;
976 -- Initialize internal state (always to False (RM D.10(6))
981 -- Initialize internal mutex
983 Result := pthread_mutexattr_init (Mutex_Attr'Access);
984 pragma Assert (Result = 0 or else Result = ENOMEM);
986 if Result = ENOMEM then
990 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
991 pragma Assert (Result = 0 or else Result = ENOMEM);
993 if Result = ENOMEM then
994 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
995 pragma Assert (Result = 0);
1000 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1001 pragma Assert (Result = 0);
1003 -- Initialize internal condition variable
1005 Result := pthread_condattr_init (Cond_Attr'Access);
1006 pragma Assert (Result = 0 or else Result = ENOMEM);
1009 Result := pthread_mutex_destroy (S.L'Access);
1010 pragma Assert (Result = 0);
1012 if Result = ENOMEM then
1013 raise Storage_Error;
1017 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1018 pragma Assert (Result = 0 or else Result = ENOMEM);
1021 Result := pthread_mutex_destroy (S.L'Access);
1022 pragma Assert (Result = 0);
1024 if Result = ENOMEM then
1025 Result := pthread_condattr_destroy (Cond_Attr'Access);
1026 pragma Assert (Result = 0);
1027 raise Storage_Error;
1031 Result := pthread_condattr_destroy (Cond_Attr'Access);
1032 pragma Assert (Result = 0);
1039 procedure Finalize (S : in out Suspension_Object) is
1040 Result : Interfaces.C.int;
1043 -- Destroy internal mutex
1045 Result := pthread_mutex_destroy (S.L'Access);
1046 pragma Assert (Result = 0);
1048 -- Destroy internal condition variable
1050 Result := pthread_cond_destroy (S.CV'Access);
1051 pragma Assert (Result = 0);
1058 function Current_State (S : Suspension_Object) return Boolean is
1060 -- We do not want to use lock on this read operation. State is marked
1061 -- as Atomic so that we ensure that the value retrieved is correct.
1070 procedure Set_False (S : in out Suspension_Object) is
1071 Result : Interfaces.C.int;
1074 SSL.Abort_Defer.all;
1076 Result := pthread_mutex_lock (S.L'Access);
1077 pragma Assert (Result = 0);
1081 Result := pthread_mutex_unlock (S.L'Access);
1082 pragma Assert (Result = 0);
1084 SSL.Abort_Undefer.all;
1091 procedure Set_True (S : in out Suspension_Object) is
1092 Result : Interfaces.C.int;
1095 SSL.Abort_Defer.all;
1097 Result := pthread_mutex_lock (S.L'Access);
1098 pragma Assert (Result = 0);
1100 -- If there is already a task waiting on this suspension object then
1101 -- we resume it, leaving the state of the suspension object to False,
1102 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1103 -- the state to True.
1109 Result := pthread_cond_signal (S.CV'Access);
1110 pragma Assert (Result = 0);
1116 Result := pthread_mutex_unlock (S.L'Access);
1117 pragma Assert (Result = 0);
1119 SSL.Abort_Undefer.all;
1122 ------------------------
1123 -- Suspend_Until_True --
1124 ------------------------
1126 procedure Suspend_Until_True (S : in out Suspension_Object) is
1127 Result : Interfaces.C.int;
1130 SSL.Abort_Defer.all;
1132 Result := pthread_mutex_lock (S.L'Access);
1133 pragma Assert (Result = 0);
1137 -- Program_Error must be raised upon calling Suspend_Until_True
1138 -- if another task is already waiting on that suspension object
1141 Result := pthread_mutex_unlock (S.L'Access);
1142 pragma Assert (Result = 0);
1144 SSL.Abort_Undefer.all;
1146 raise Program_Error;
1148 -- Suspend the task if the state is False. Otherwise, the task
1149 -- continues its execution, and the state of the suspension object
1150 -- is set to False (ARM D.10 par. 9).
1158 -- Loop in case pthread_cond_wait returns earlier than expected
1159 -- (e.g. in case of EINTR caused by a signal).
1161 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1162 pragma Assert (Result = 0 or else Result = EINTR);
1164 exit when not S.Waiting;
1168 Result := pthread_mutex_unlock (S.L'Access);
1169 pragma Assert (Result = 0);
1171 SSL.Abort_Undefer.all;
1173 end Suspend_Until_True;
1181 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1182 pragma Unreferenced (Self_ID);
1187 --------------------
1188 -- Check_No_Locks --
1189 --------------------
1191 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1192 pragma Unreferenced (Self_ID);
1197 ----------------------
1198 -- Environment_Task --
1199 ----------------------
1201 function Environment_Task return Task_Id is
1203 return Environment_Task_Id;
1204 end Environment_Task;
1210 procedure Lock_RTS is
1212 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1219 procedure Unlock_RTS is
1221 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1228 function Suspend_Task
1230 Thread_Self : Thread_Id) return Boolean
1232 pragma Unreferenced (T);
1233 pragma Unreferenced (Thread_Self);
1242 function Resume_Task
1244 Thread_Self : Thread_Id) return Boolean
1246 pragma Unreferenced (T);
1247 pragma Unreferenced (Thread_Self);
1252 --------------------
1253 -- Stop_All_Tasks --
1254 --------------------
1256 procedure Stop_All_Tasks is
1265 function Stop_Task (T : ST.Task_Id) return Boolean is
1266 pragma Unreferenced (T);
1275 function Continue_Task (T : ST.Task_Id) return Boolean is
1276 pragma Unreferenced (T);
1285 procedure Initialize (Environment_Task : Task_Id) is
1286 act : aliased struct_sigaction;
1287 old_act : aliased struct_sigaction;
1288 Tmp_Set : aliased sigset_t;
1289 Result : Interfaces.C.int;
1292 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1293 pragma Import (C, State, "__gnat_get_interrupt_state");
1294 -- Get interrupt state. Defined in a-init.c. The input argument is
1295 -- the interrupt number, and the result is one of the following:
1297 Default : constant Character := 's';
1298 -- 'n' this interrupt not set by any Interrupt_State pragma
1299 -- 'u' Interrupt_State pragma set state to User
1300 -- 'r' Interrupt_State pragma set state to Runtime
1301 -- 's' Interrupt_State pragma set state to System (use "default"
1305 Environment_Task_Id := Environment_Task;
1307 Interrupt_Management.Initialize;
1309 -- Initialize the lock used to synchronize chain of all ATCBs
1311 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1313 Specific.Initialize (Environment_Task);
1315 -- Make environment task known here because it doesn't go through
1316 -- Activate_Tasks, which does it for all other tasks.
1318 Known_Tasks (Known_Tasks'First) := Environment_Task;
1319 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1321 Enter_Task (Environment_Task);
1323 -- Prepare the set of signals that should unblocked in all tasks
1325 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1326 pragma Assert (Result = 0);
1328 for J in Interrupt_Management.Interrupt_ID loop
1329 if System.Interrupt_Management.Keep_Unmasked (J) then
1330 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1331 pragma Assert (Result = 0);
1335 -- Install the abort-signal handler
1338 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1341 act.sa_handler := Abort_Handler'Address;
1343 Result := sigemptyset (Tmp_Set'Access);
1344 pragma Assert (Result = 0);
1345 act.sa_mask := Tmp_Set;
1349 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1350 act'Unchecked_Access,
1351 old_act'Unchecked_Access);
1352 pragma Assert (Result = 0);
1356 end System.Task_Primitives.Operations;