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-2008, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a IRIX (pthread library) version of this package
36 -- This package contains all the GNULL primitives that interface directly with
40 -- Turn off polling, we do not want ATC polling to take place during tasking
41 -- operations. It causes infinite loops and other problems.
43 with Ada.Unchecked_Conversion;
44 with Ada.Unchecked_Deallocation;
48 with System.Task_Info;
49 with System.Tasking.Debug;
50 with System.Interrupt_Management;
51 with System.OS_Primitives;
54 with System.Soft_Links;
55 -- We use System.Soft_Links instead of System.Tasking.Initialization
56 -- because the later is a higher level package that we shouldn't depend on.
57 -- For example when using the restricted run time, it is replaced by
58 -- System.Tasking.Restricted.Stages.
60 package body System.Task_Primitives.Operations is
62 package SSL renames System.Soft_Links;
65 use System.Tasking.Debug;
67 use System.OS_Interface;
68 use System.OS_Primitives;
69 use System.Parameters;
75 -- The followings are logically constants, but need to be initialized
78 Single_RTS_Lock : aliased RTS_Lock;
79 -- This is a lock to allow only one thread of control in the RTS at
80 -- a time; it is used to execute in mutual exclusion from all other tasks.
81 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
83 ATCB_Key : aliased pthread_key_t;
84 -- Key used to find the Ada Task_Id associated with a thread
86 Environment_Task_Id : Task_Id;
87 -- A variable to hold Task_Id for the environment task
89 Locking_Policy : Character;
90 pragma Import (C, Locking_Policy, "__gl_locking_policy");
92 Time_Slice_Val : Integer;
93 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
95 Dispatching_Policy : Character;
96 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
98 Real_Time_Clock_Id : constant clockid_t := CLOCK_REALTIME;
100 Unblocked_Signal_Mask : aliased sigset_t;
102 Foreign_Task_Elaborated : aliased Boolean := True;
103 -- Used to identified fake tasks (i.e., non-Ada Threads)
111 procedure Initialize (Environment_Task : Task_Id);
112 pragma Inline (Initialize);
113 -- Initialize various data needed by this package
115 function Is_Valid_Task return Boolean;
116 pragma Inline (Is_Valid_Task);
117 -- Does executing thread have a TCB?
119 procedure Set (Self_Id : Task_Id);
121 -- Set the self id for the current task
123 function Self return Task_Id;
124 pragma Inline (Self);
125 -- Return a pointer to the Ada Task Control Block of the calling task
129 package body Specific is separate;
130 -- The body of this package is target specific
132 ---------------------------------
133 -- Support for foreign threads --
134 ---------------------------------
136 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
137 -- Allocate and Initialize a new ATCB for the current Thread
139 function Register_Foreign_Thread
140 (Thread : Thread_Id) return Task_Id is separate;
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 function To_Address is
147 new Ada.Unchecked_Conversion (Task_Id, System.Address);
149 procedure Abort_Handler (Sig : Signal);
150 -- Signal handler used to implement asynchronous abort
156 procedure Abort_Handler (Sig : Signal) is
157 pragma Unreferenced (Sig);
159 T : constant Task_Id := Self;
160 Result : Interfaces.C.int;
161 Old_Set : aliased sigset_t;
164 -- It is not safe to raise an exception when using ZCX and the GCC
165 -- exception handling mechanism.
167 if ZCX_By_Default and then GCC_ZCX_Support then
171 if T.Deferral_Level = 0
172 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
174 -- Make sure signals used for RTS internal purpose are unmasked
176 Result := pthread_sigmask
178 Unblocked_Signal_Mask'Access,
180 pragma Assert (Result = 0);
182 raise Standard'Abort_Signal;
190 -- The underlying thread system sets a guard page at the
191 -- bottom of a thread stack, so nothing is needed.
193 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
194 pragma Unreferenced (On);
195 pragma Unreferenced (T);
204 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
206 return T.Common.LL.Thread;
213 function Self return Task_Id renames Specific.Self;
215 ---------------------
216 -- Initialize_Lock --
217 ---------------------
219 -- Note: mutexes and cond_variables needed per-task basis are initialized
220 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
221 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
222 -- status change of RTS. Therefore rasing Storage_Error in the following
223 -- routines should be able to be handled safely.
225 procedure Initialize_Lock
226 (Prio : System.Any_Priority;
227 L : not null access Lock)
229 Attributes : aliased pthread_mutexattr_t;
230 Result : Interfaces.C.int;
233 Result := pthread_mutexattr_init (Attributes'Access);
234 pragma Assert (Result = 0 or else Result = ENOMEM);
236 if Result = ENOMEM then
240 if Locking_Policy = 'C' then
242 pthread_mutexattr_setprotocol
243 (Attributes'Access, PTHREAD_PRIO_PROTECT);
244 pragma Assert (Result = 0);
247 pthread_mutexattr_setprioceiling
248 (Attributes'Access, Interfaces.C.int (Prio));
249 pragma Assert (Result = 0);
252 Result := pthread_mutex_init (L, Attributes'Access);
253 pragma Assert (Result = 0 or else Result = ENOMEM);
255 if Result = ENOMEM then
256 Result := pthread_mutexattr_destroy (Attributes'Access);
260 Result := pthread_mutexattr_destroy (Attributes'Access);
261 pragma Assert (Result = 0);
264 procedure Initialize_Lock
265 (L : not null access RTS_Lock;
268 pragma Unreferenced (Level);
270 Attributes : aliased pthread_mutexattr_t;
271 Result : Interfaces.C.int;
274 Result := pthread_mutexattr_init (Attributes'Access);
275 pragma Assert (Result = 0 or else Result = ENOMEM);
277 if Result = ENOMEM then
281 if Locking_Policy = 'C' then
282 Result := pthread_mutexattr_setprotocol
283 (Attributes'Access, PTHREAD_PRIO_PROTECT);
284 pragma Assert (Result = 0);
286 Result := pthread_mutexattr_setprioceiling
287 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
288 pragma Assert (Result = 0);
291 Result := pthread_mutex_init (L, Attributes'Access);
293 pragma Assert (Result = 0 or else Result = ENOMEM);
295 if Result = ENOMEM then
296 Result := pthread_mutexattr_destroy (Attributes'Access);
300 Result := pthread_mutexattr_destroy (Attributes'Access);
301 pragma Assert (Result = 0);
308 procedure Finalize_Lock (L : not null access Lock) is
309 Result : Interfaces.C.int;
311 Result := pthread_mutex_destroy (L);
312 pragma Assert (Result = 0);
315 procedure Finalize_Lock (L : not null access RTS_Lock) is
316 Result : Interfaces.C.int;
318 Result := pthread_mutex_destroy (L);
319 pragma Assert (Result = 0);
327 (L : not null access Lock; Ceiling_Violation : out Boolean)
329 Result : Interfaces.C.int;
332 Result := pthread_mutex_lock (L);
333 Ceiling_Violation := Result = EINVAL;
335 -- Assumes the cause of EINVAL is a priority ceiling violation
337 pragma Assert (Result = 0 or else Result = EINVAL);
341 (L : not null access RTS_Lock;
342 Global_Lock : Boolean := False)
344 Result : Interfaces.C.int;
346 if not Single_Lock or else Global_Lock then
347 Result := pthread_mutex_lock (L);
348 pragma Assert (Result = 0);
352 procedure Write_Lock (T : Task_Id) is
353 Result : Interfaces.C.int;
355 if not Single_Lock then
356 Result := pthread_mutex_lock (T.Common.LL.L'Access);
357 pragma Assert (Result = 0);
366 (L : not null access Lock; Ceiling_Violation : out Boolean) is
368 Write_Lock (L, Ceiling_Violation);
375 procedure Unlock (L : not null access Lock) is
376 Result : Interfaces.C.int;
378 Result := pthread_mutex_unlock (L);
379 pragma Assert (Result = 0);
383 (L : not null access RTS_Lock;
384 Global_Lock : Boolean := False)
386 Result : Interfaces.C.int;
388 if not Single_Lock or else Global_Lock then
389 Result := pthread_mutex_unlock (L);
390 pragma Assert (Result = 0);
394 procedure Unlock (T : Task_Id) is
395 Result : Interfaces.C.int;
397 if not Single_Lock then
398 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
399 pragma Assert (Result = 0);
407 -- Dynamic priority ceilings are not supported by the underlying system
409 procedure Set_Ceiling
410 (L : not null access Lock;
411 Prio : System.Any_Priority)
413 pragma Unreferenced (L, Prio);
423 (Self_ID : ST.Task_Id;
424 Reason : System.Tasking.Task_States)
426 pragma Unreferenced (Reason);
427 Result : Interfaces.C.int;
433 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
437 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
440 -- EINTR is not considered a failure
442 pragma Assert (Result = 0 or else Result = EINTR);
449 procedure Timed_Sleep
452 Mode : ST.Delay_Modes;
453 Reason : Task_States;
454 Timedout : out Boolean;
455 Yielded : out Boolean)
457 pragma Unreferenced (Reason);
459 Base_Time : constant Duration := Monotonic_Clock;
460 Check_Time : Duration := Base_Time;
462 Request : aliased timespec;
463 Result : Interfaces.C.int;
469 if Mode = Relative then
470 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
472 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
475 if Abs_Time > Check_Time then
476 Request := To_Timespec (Abs_Time);
479 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
483 pthread_cond_timedwait
484 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
489 pthread_cond_timedwait
490 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
494 Check_Time := Monotonic_Clock;
495 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
497 if Result = 0 or else errno = EINTR then
509 -- This is for use in implementing delay statements, so we assume
510 -- the caller is abort-deferred but is holding no locks.
512 procedure Timed_Delay
515 Mode : ST.Delay_Modes)
517 Base_Time : constant Duration := Monotonic_Clock;
518 Check_Time : Duration := Base_Time;
520 Request : aliased timespec;
521 Result : Interfaces.C.int;
528 Write_Lock (Self_ID);
530 if Mode = Relative then
531 Abs_Time := Time + Check_Time;
533 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
536 if Abs_Time > Check_Time then
537 Request := To_Timespec (Abs_Time);
538 Self_ID.Common.State := Delay_Sleep;
541 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
544 Result := pthread_cond_timedwait
545 (Self_ID.Common.LL.CV'Access,
546 Single_RTS_Lock'Access,
549 Result := pthread_cond_timedwait
550 (Self_ID.Common.LL.CV'Access,
551 Self_ID.Common.LL.L'Access,
555 Check_Time := Monotonic_Clock;
556 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
558 pragma Assert (Result = 0
559 or else Result = ETIMEDOUT
560 or else Result = EINTR);
563 Self_ID.Common.State := Runnable;
575 ---------------------
576 -- Monotonic_Clock --
577 ---------------------
579 function Monotonic_Clock return Duration is
580 TS : aliased timespec;
581 Result : Interfaces.C.int;
583 Result := clock_gettime (Real_Time_Clock_Id, TS'Unchecked_Access);
584 pragma Assert (Result = 0);
585 return To_Duration (TS);
592 function RT_Resolution return Duration is
594 -- The clock_getres (Real_Time_Clock_Id) function appears to return
595 -- the interrupt resolution of the realtime clock and not the actual
596 -- resolution of reading the clock. Even though this last value is
597 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
598 -- have a microsecond resolution or better.
600 -- ??? We should figure out a method to return the right value on
610 procedure Wakeup (T : ST.Task_Id; Reason : System.Tasking.Task_States) is
611 pragma Unreferenced (Reason);
612 Result : Interfaces.C.int;
614 Result := pthread_cond_signal (T.Common.LL.CV'Access);
615 pragma Assert (Result = 0);
622 procedure Yield (Do_Yield : Boolean := True) is
623 Result : Interfaces.C.int;
624 pragma Unreferenced (Result);
627 Result := sched_yield;
635 procedure Set_Priority
637 Prio : System.Any_Priority;
638 Loss_Of_Inheritance : Boolean := False)
640 pragma Unreferenced (Loss_Of_Inheritance);
642 Result : Interfaces.C.int;
643 Param : aliased struct_sched_param;
644 Sched_Policy : Interfaces.C.int;
646 use type System.Task_Info.Task_Info_Type;
648 function To_Int is new Ada.Unchecked_Conversion
649 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
651 function Get_Policy (Prio : System.Any_Priority) return Character;
652 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
653 -- Get priority specific dispatching policy
655 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
656 -- Upper case first character of the policy name corresponding to the
657 -- task as set by a Priority_Specific_Dispatching pragma.
660 T.Common.Current_Priority := Prio;
661 Param.sched_priority := Interfaces.C.int (Prio);
663 if T.Common.Task_Info /= null then
664 Sched_Policy := To_Int (T.Common.Task_Info.Policy);
666 elsif Dispatching_Policy = 'R'
667 or else Priority_Specific_Policy = 'R'
668 or else Time_Slice_Val > 0
670 Sched_Policy := SCHED_RR;
673 Sched_Policy := SCHED_FIFO;
676 Result := pthread_setschedparam (T.Common.LL.Thread, Sched_Policy,
678 pragma Assert (Result = 0);
685 function Get_Priority (T : Task_Id) return System.Any_Priority is
687 return T.Common.Current_Priority;
694 procedure Enter_Task (Self_ID : Task_Id) is
695 Result : Interfaces.C.int;
697 function To_Int is new Ada.Unchecked_Conversion
698 (System.Task_Info.CPU_Number, Interfaces.C.int);
700 use System.Task_Info;
703 Self_ID.Common.LL.Thread := pthread_self;
704 Specific.Set (Self_ID);
706 if Self_ID.Common.Task_Info /= null
707 and then Self_ID.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
708 and then Self_ID.Common.Task_Info.Runon_CPU /= ANY_CPU
710 Result := pthread_setrunon_np
711 (To_Int (Self_ID.Common.Task_Info.Runon_CPU));
712 pragma Assert (Result = 0);
717 for J in Known_Tasks'Range loop
718 if Known_Tasks (J) = null then
719 Known_Tasks (J) := Self_ID;
720 Self_ID.Known_Tasks_Index := J;
732 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
734 return new Ada_Task_Control_Block (Entry_Num);
741 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
743 -----------------------------
744 -- Register_Foreign_Thread --
745 -----------------------------
747 function Register_Foreign_Thread return Task_Id is
749 if Is_Valid_Task then
752 return Register_Foreign_Thread (pthread_self);
754 end Register_Foreign_Thread;
760 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
761 Result : Interfaces.C.int;
762 Cond_Attr : aliased pthread_condattr_t;
765 if not Single_Lock then
766 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
769 Result := pthread_condattr_init (Cond_Attr'Access);
770 pragma Assert (Result = 0 or else Result = ENOMEM);
774 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
775 pragma Assert (Result = 0 or else Result = ENOMEM);
781 if not Single_Lock then
782 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
783 pragma Assert (Result = 0);
789 Result := pthread_condattr_destroy (Cond_Attr'Access);
790 pragma Assert (Result = 0);
797 procedure Create_Task
799 Wrapper : System.Address;
800 Stack_Size : System.Parameters.Size_Type;
801 Priority : System.Any_Priority;
802 Succeeded : out Boolean)
804 use System.Task_Info;
806 Attributes : aliased pthread_attr_t;
807 Sched_Param : aliased struct_sched_param;
808 Result : Interfaces.C.int;
810 function Thread_Body_Access is new
811 Ada.Unchecked_Conversion (System.Address, Thread_Body);
812 function To_Int is new Ada.Unchecked_Conversion
813 (System.Task_Info.Thread_Scheduling_Scope, Interfaces.C.int);
814 function To_Int is new Ada.Unchecked_Conversion
815 (System.Task_Info.Thread_Scheduling_Inheritance, Interfaces.C.int);
816 function To_Int is new Ada.Unchecked_Conversion
817 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
820 Result := pthread_attr_init (Attributes'Access);
821 pragma Assert (Result = 0 or else Result = ENOMEM);
829 pthread_attr_setdetachstate
830 (Attributes'Access, PTHREAD_CREATE_DETACHED);
831 pragma Assert (Result = 0);
834 pthread_attr_setstacksize
835 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
836 pragma Assert (Result = 0);
838 if T.Common.Task_Info /= null then
840 pthread_attr_setscope
841 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
842 pragma Assert (Result = 0);
845 pthread_attr_setinheritsched
846 (Attributes'Access, To_Int (T.Common.Task_Info.Inheritance));
847 pragma Assert (Result = 0);
850 pthread_attr_setschedpolicy
851 (Attributes'Access, To_Int (T.Common.Task_Info.Policy));
852 pragma Assert (Result = 0);
854 Sched_Param.sched_priority :=
855 Interfaces.C.int (T.Common.Task_Info.Priority);
858 pthread_attr_setschedparam
859 (Attributes'Access, Sched_Param'Access);
860 pragma Assert (Result = 0);
863 -- Since the initial signal mask of a thread is inherited from the
864 -- creator, and the Environment task has all its signals masked, we
865 -- do not need to manipulate caller's signal mask at this point.
866 -- All tasks in RTS will have All_Tasks_Mask initially.
870 (T.Common.LL.Thread'Access,
872 Thread_Body_Access (Wrapper),
876 and then T.Common.Task_Info /= null
877 and then T.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
879 -- The pthread_create call may have failed because we asked for a
880 -- system scope pthread and none were available (probably because
881 -- the program was not executed by the superuser). Let's try for
882 -- a process scope pthread instead of raising Tasking_Error.
885 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
886 System.IO.Put ("""");
887 System.IO.Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len));
888 System.IO.Put_Line (""" could not be honored. ");
889 System.IO.Put_Line ("Scope changed to PTHREAD_SCOPE_PROCESS");
891 T.Common.Task_Info.Scope := PTHREAD_SCOPE_PROCESS;
893 pthread_attr_setscope
894 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
895 pragma Assert (Result = 0);
899 (T.Common.LL.Thread'Access,
901 Thread_Body_Access (Wrapper),
905 pragma Assert (Result = 0 or else Result = EAGAIN);
907 Succeeded := Result = 0;
911 -- The following needs significant commenting ???
913 if T.Common.Task_Info /= null then
914 T.Common.Base_Priority := T.Common.Task_Info.Priority;
915 Set_Priority (T, T.Common.Task_Info.Priority);
917 Set_Priority (T, Priority);
921 Result := pthread_attr_destroy (Attributes'Access);
922 pragma Assert (Result = 0);
929 procedure Finalize_TCB (T : Task_Id) is
930 Result : Interfaces.C.int;
932 Is_Self : constant Boolean := T = Self;
934 procedure Free is new
935 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
938 if not Single_Lock then
939 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
940 pragma Assert (Result = 0);
943 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
944 pragma Assert (Result = 0);
946 if T.Known_Tasks_Index /= -1 then
947 Known_Tasks (T.Known_Tasks_Index) := null;
961 procedure Exit_Task is
970 procedure Abort_Task (T : Task_Id) is
971 Result : Interfaces.C.int;
976 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
977 pragma Assert (Result = 0);
984 procedure Initialize (S : in out Suspension_Object) is
985 Mutex_Attr : aliased pthread_mutexattr_t;
986 Cond_Attr : aliased pthread_condattr_t;
987 Result : Interfaces.C.int;
990 -- Initialize internal state (always to False (RM D.10(6))
995 -- Initialize internal mutex
997 Result := pthread_mutexattr_init (Mutex_Attr'Access);
998 pragma Assert (Result = 0 or else Result = ENOMEM);
1000 if Result = ENOMEM then
1001 raise Storage_Error;
1004 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1005 pragma Assert (Result = 0 or else Result = ENOMEM);
1007 if Result = ENOMEM then
1008 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1009 pragma Assert (Result = 0);
1011 raise Storage_Error;
1014 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1015 pragma Assert (Result = 0);
1017 -- Initialize internal condition variable
1019 Result := pthread_condattr_init (Cond_Attr'Access);
1020 pragma Assert (Result = 0 or else Result = ENOMEM);
1023 Result := pthread_mutex_destroy (S.L'Access);
1024 pragma Assert (Result = 0);
1026 if Result = ENOMEM then
1027 raise Storage_Error;
1031 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1032 pragma Assert (Result = 0 or else Result = ENOMEM);
1035 Result := pthread_mutex_destroy (S.L'Access);
1036 pragma Assert (Result = 0);
1038 if Result = ENOMEM then
1039 Result := pthread_condattr_destroy (Cond_Attr'Access);
1040 pragma Assert (Result = 0);
1041 raise Storage_Error;
1045 Result := pthread_condattr_destroy (Cond_Attr'Access);
1046 pragma Assert (Result = 0);
1053 procedure Finalize (S : in out Suspension_Object) is
1054 Result : Interfaces.C.int;
1057 -- Destroy internal mutex
1059 Result := pthread_mutex_destroy (S.L'Access);
1060 pragma Assert (Result = 0);
1062 -- Destroy internal condition variable
1064 Result := pthread_cond_destroy (S.CV'Access);
1065 pragma Assert (Result = 0);
1072 function Current_State (S : Suspension_Object) return Boolean is
1074 -- We do not want to use lock on this read operation. State is marked
1075 -- as Atomic so that we ensure that the value retrieved is correct.
1084 procedure Set_False (S : in out Suspension_Object) is
1085 Result : Interfaces.C.int;
1088 SSL.Abort_Defer.all;
1090 Result := pthread_mutex_lock (S.L'Access);
1091 pragma Assert (Result = 0);
1095 Result := pthread_mutex_unlock (S.L'Access);
1096 pragma Assert (Result = 0);
1098 SSL.Abort_Undefer.all;
1105 procedure Set_True (S : in out Suspension_Object) is
1106 Result : Interfaces.C.int;
1109 SSL.Abort_Defer.all;
1111 Result := pthread_mutex_lock (S.L'Access);
1112 pragma Assert (Result = 0);
1114 -- If there is already a task waiting on this suspension object then
1115 -- we resume it, leaving the state of the suspension object to False,
1116 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1117 -- the state to True.
1123 Result := pthread_cond_signal (S.CV'Access);
1124 pragma Assert (Result = 0);
1130 Result := pthread_mutex_unlock (S.L'Access);
1131 pragma Assert (Result = 0);
1133 SSL.Abort_Undefer.all;
1136 ------------------------
1137 -- Suspend_Until_True --
1138 ------------------------
1140 procedure Suspend_Until_True (S : in out Suspension_Object) is
1141 Result : Interfaces.C.int;
1144 SSL.Abort_Defer.all;
1146 Result := pthread_mutex_lock (S.L'Access);
1147 pragma Assert (Result = 0);
1151 -- Program_Error must be raised upon calling Suspend_Until_True
1152 -- if another task is already waiting on that suspension object
1155 Result := pthread_mutex_unlock (S.L'Access);
1156 pragma Assert (Result = 0);
1158 SSL.Abort_Undefer.all;
1160 raise Program_Error;
1162 -- Suspend the task if the state is False. Otherwise, the task
1163 -- continues its execution, and the state of the suspension object
1164 -- is set to False (ARM D.10 par. 9).
1170 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1173 Result := pthread_mutex_unlock (S.L'Access);
1174 pragma Assert (Result = 0);
1176 SSL.Abort_Undefer.all;
1178 end Suspend_Until_True;
1186 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1187 pragma Unreferenced (Self_ID);
1192 --------------------
1193 -- Check_No_Locks --
1194 --------------------
1196 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1197 pragma Unreferenced (Self_ID);
1202 ----------------------
1203 -- Environment_Task --
1204 ----------------------
1206 function Environment_Task return Task_Id is
1208 return Environment_Task_Id;
1209 end Environment_Task;
1215 procedure Lock_RTS is
1217 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1224 procedure Unlock_RTS is
1226 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1233 function Suspend_Task
1235 Thread_Self : Thread_Id) return Boolean
1237 pragma Unreferenced (T);
1238 pragma Unreferenced (Thread_Self);
1247 function Resume_Task
1249 Thread_Self : Thread_Id) return Boolean
1251 pragma Unreferenced (T);
1252 pragma Unreferenced (Thread_Self);
1257 --------------------
1258 -- Stop_All_Tasks --
1259 --------------------
1261 procedure Stop_All_Tasks is
1270 function Stop_Task (T : ST.Task_Id) return Boolean is
1271 pragma Unreferenced (T);
1280 function Continue_Task (T : ST.Task_Id) return Boolean is
1281 pragma Unreferenced (T);
1290 procedure Initialize (Environment_Task : Task_Id) is
1291 act : aliased struct_sigaction;
1292 old_act : aliased struct_sigaction;
1293 Tmp_Set : aliased sigset_t;
1294 Result : Interfaces.C.int;
1297 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1298 pragma Import (C, State, "__gnat_get_interrupt_state");
1299 -- Get interrupt state. Defined in a-init.c. The input argument is
1300 -- the interrupt number, and the result is one of the following:
1302 Default : constant Character := 's';
1303 -- 'n' this interrupt not set by any Interrupt_State pragma
1304 -- 'u' Interrupt_State pragma set state to User
1305 -- 'r' Interrupt_State pragma set state to Runtime
1306 -- 's' Interrupt_State pragma set state to System (use "default"
1310 Environment_Task_Id := Environment_Task;
1312 Interrupt_Management.Initialize;
1314 -- Initialize the lock used to synchronize chain of all ATCBs
1316 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1318 Specific.Initialize (Environment_Task);
1320 Enter_Task (Environment_Task);
1322 -- Prepare the set of signals that should unblocked in all tasks
1324 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1325 pragma Assert (Result = 0);
1327 for J in Interrupt_Management.Interrupt_ID loop
1328 if System.Interrupt_Management.Keep_Unmasked (J) then
1329 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1330 pragma Assert (Result = 0);
1334 -- Install the abort-signal handler
1337 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1340 act.sa_handler := Abort_Handler'Address;
1342 Result := sigemptyset (Tmp_Set'Access);
1343 pragma Assert (Result = 0);
1344 act.sa_mask := Tmp_Set;
1348 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1349 act'Unchecked_Access,
1350 old_act'Unchecked_Access);
1351 pragma Assert (Result = 0);
1355 end System.Task_Primitives.Operations;