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-2007, 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
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
47 with System.Task_Info;
49 with System.Tasking.Debug;
50 -- used for Known_Tasks
52 with System.Interrupt_Management;
53 -- used for Keep_Unmasked
54 -- Abort_Task_Interrupt
57 with System.OS_Primitives;
58 -- used for Delay_Modes
63 with System.Soft_Links;
64 -- used for Abort_Defer/Undefer
66 -- We use System.Soft_Links instead of System.Tasking.Initialization
67 -- because the later is a higher level package that we shouldn't depend on.
68 -- For example when using the restricted run time, it is replaced by
69 -- System.Tasking.Restricted.Stages.
71 with Ada.Unchecked_Conversion;
72 with Ada.Unchecked_Deallocation;
74 package body System.Task_Primitives.Operations is
76 package SSL renames System.Soft_Links;
79 use System.Tasking.Debug;
81 use System.OS_Interface;
82 use System.OS_Primitives;
83 use System.Parameters;
89 -- The followings are logically constants, but need to be initialized
92 Single_RTS_Lock : aliased RTS_Lock;
93 -- This is a lock to allow only one thread of control in the RTS at
94 -- a time; it is used to execute in mutual exclusion from all other tasks.
95 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
97 ATCB_Key : aliased pthread_key_t;
98 -- Key used to find the Ada Task_Id associated with a thread
100 Environment_Task_Id : Task_Id;
101 -- A variable to hold Task_Id for the environment task
103 Locking_Policy : Character;
104 pragma Import (C, Locking_Policy, "__gl_locking_policy");
106 Time_Slice_Val : Integer;
107 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
109 Dispatching_Policy : Character;
110 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
112 Real_Time_Clock_Id : constant clockid_t := CLOCK_REALTIME;
114 Unblocked_Signal_Mask : aliased sigset_t;
116 Foreign_Task_Elaborated : aliased Boolean := True;
117 -- Used to identified fake tasks (i.e., non-Ada Threads)
125 procedure Initialize (Environment_Task : Task_Id);
126 pragma Inline (Initialize);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task return Boolean;
130 pragma Inline (Is_Valid_Task);
131 -- Does executing thread have a TCB?
133 procedure Set (Self_Id : Task_Id);
135 -- Set the self id for the current task
137 function Self return Task_Id;
138 pragma Inline (Self);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread : Thread_Id) return Task_Id is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 function To_Address is
161 new Ada.Unchecked_Conversion (Task_Id, System.Address);
163 procedure Abort_Handler (Sig : Signal);
164 -- Signal handler used to implement asynchronous abort
170 procedure Abort_Handler (Sig : Signal) is
171 pragma Unreferenced (Sig);
173 T : constant Task_Id := Self;
174 Result : Interfaces.C.int;
175 Old_Set : aliased sigset_t;
178 -- It is not safe to raise an exception when using ZCX and the GCC
179 -- exception handling mechanism.
181 if ZCX_By_Default and then GCC_ZCX_Support then
185 if T.Deferral_Level = 0
186 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
188 -- Make sure signals used for RTS internal purpose are unmasked
190 Result := pthread_sigmask
192 Unblocked_Signal_Mask'Unchecked_Access,
193 Old_Set'Unchecked_Access);
194 pragma Assert (Result = 0);
196 raise Standard'Abort_Signal;
204 -- The underlying thread system sets a guard page at the
205 -- bottom of a thread stack, so nothing is needed.
207 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
208 pragma Unreferenced (On);
209 pragma Unreferenced (T);
218 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
220 return T.Common.LL.Thread;
227 function Self return Task_Id renames Specific.Self;
229 ---------------------
230 -- Initialize_Lock --
231 ---------------------
233 -- Note: mutexes and cond_variables needed per-task basis are initialized
234 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
235 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
236 -- status change of RTS. Therefore rasing Storage_Error in the following
237 -- routines should be able to be handled safely.
239 procedure Initialize_Lock
240 (Prio : System.Any_Priority;
241 L : not null access Lock)
243 Attributes : aliased pthread_mutexattr_t;
244 Result : Interfaces.C.int;
247 Result := pthread_mutexattr_init (Attributes'Access);
248 pragma Assert (Result = 0 or else Result = ENOMEM);
250 if Result = ENOMEM then
254 if Locking_Policy = 'C' then
256 pthread_mutexattr_setprotocol
257 (Attributes'Access, PTHREAD_PRIO_PROTECT);
258 pragma Assert (Result = 0);
261 pthread_mutexattr_setprioceiling
262 (Attributes'Access, Interfaces.C.int (Prio));
263 pragma Assert (Result = 0);
266 Result := pthread_mutex_init (L, Attributes'Access);
267 pragma Assert (Result = 0 or else Result = ENOMEM);
269 if Result = ENOMEM then
270 Result := pthread_mutexattr_destroy (Attributes'Access);
274 Result := pthread_mutexattr_destroy (Attributes'Access);
275 pragma Assert (Result = 0);
278 procedure Initialize_Lock
279 (L : not null access RTS_Lock;
282 pragma Unreferenced (Level);
284 Attributes : aliased pthread_mutexattr_t;
285 Result : Interfaces.C.int;
288 Result := pthread_mutexattr_init (Attributes'Access);
289 pragma Assert (Result = 0 or else Result = ENOMEM);
291 if Result = ENOMEM then
295 if Locking_Policy = 'C' then
296 Result := pthread_mutexattr_setprotocol
297 (Attributes'Access, PTHREAD_PRIO_PROTECT);
298 pragma Assert (Result = 0);
300 Result := pthread_mutexattr_setprioceiling
301 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
302 pragma Assert (Result = 0);
305 Result := pthread_mutex_init (L, Attributes'Access);
307 pragma Assert (Result = 0 or else Result = ENOMEM);
309 if Result = ENOMEM then
310 Result := pthread_mutexattr_destroy (Attributes'Access);
314 Result := pthread_mutexattr_destroy (Attributes'Access);
315 pragma Assert (Result = 0);
322 procedure Finalize_Lock (L : not null access Lock) is
323 Result : Interfaces.C.int;
325 Result := pthread_mutex_destroy (L);
326 pragma Assert (Result = 0);
329 procedure Finalize_Lock (L : not null access RTS_Lock) is
330 Result : Interfaces.C.int;
332 Result := pthread_mutex_destroy (L);
333 pragma Assert (Result = 0);
341 (L : not null access Lock; Ceiling_Violation : out Boolean)
343 Result : Interfaces.C.int;
346 Result := pthread_mutex_lock (L);
347 Ceiling_Violation := Result = EINVAL;
349 -- Assumes the cause of EINVAL is a priority ceiling violation
351 pragma Assert (Result = 0 or else Result = EINVAL);
355 (L : not null access RTS_Lock;
356 Global_Lock : Boolean := False)
358 Result : Interfaces.C.int;
360 if not Single_Lock or else Global_Lock then
361 Result := pthread_mutex_lock (L);
362 pragma Assert (Result = 0);
366 procedure Write_Lock (T : Task_Id) is
367 Result : Interfaces.C.int;
369 if not Single_Lock then
370 Result := pthread_mutex_lock (T.Common.LL.L'Access);
371 pragma Assert (Result = 0);
380 (L : not null access Lock; Ceiling_Violation : out Boolean) is
382 Write_Lock (L, Ceiling_Violation);
389 procedure Unlock (L : not null access Lock) is
390 Result : Interfaces.C.int;
392 Result := pthread_mutex_unlock (L);
393 pragma Assert (Result = 0);
397 (L : not null access RTS_Lock;
398 Global_Lock : Boolean := False)
400 Result : Interfaces.C.int;
402 if not Single_Lock or else Global_Lock then
403 Result := pthread_mutex_unlock (L);
404 pragma Assert (Result = 0);
408 procedure Unlock (T : Task_Id) is
409 Result : Interfaces.C.int;
411 if not Single_Lock then
412 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
413 pragma Assert (Result = 0);
421 -- Dynamic priority ceilings are not supported by the underlying system
423 procedure Set_Ceiling
424 (L : not null access Lock;
425 Prio : System.Any_Priority)
427 pragma Unreferenced (L, Prio);
437 (Self_ID : ST.Task_Id;
438 Reason : System.Tasking.Task_States)
440 pragma Unreferenced (Reason);
441 Result : Interfaces.C.int;
447 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
451 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
454 -- EINTR is not considered a failure
456 pragma Assert (Result = 0 or else Result = EINTR);
463 procedure Timed_Sleep
466 Mode : ST.Delay_Modes;
467 Reason : Task_States;
468 Timedout : out Boolean;
469 Yielded : out Boolean)
471 pragma Unreferenced (Reason);
473 Base_Time : constant Duration := Monotonic_Clock;
474 Check_Time : Duration := Base_Time;
476 Request : aliased timespec;
477 Result : Interfaces.C.int;
483 if Mode = Relative then
484 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
486 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
489 if Abs_Time > Check_Time then
490 Request := To_Timespec (Abs_Time);
493 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
497 pthread_cond_timedwait
498 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
503 pthread_cond_timedwait
504 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
508 Check_Time := Monotonic_Clock;
509 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
511 if Result = 0 or else errno = EINTR then
523 -- This is for use in implementing delay statements, so we assume
524 -- the caller is abort-deferred but is holding no locks.
526 procedure Timed_Delay
529 Mode : ST.Delay_Modes)
531 Base_Time : constant Duration := Monotonic_Clock;
532 Check_Time : Duration := Base_Time;
534 Request : aliased timespec;
535 Result : Interfaces.C.int;
542 Write_Lock (Self_ID);
544 if Mode = Relative then
545 Abs_Time := Time + Check_Time;
547 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
550 if Abs_Time > Check_Time then
551 Request := To_Timespec (Abs_Time);
552 Self_ID.Common.State := Delay_Sleep;
555 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
558 Result := pthread_cond_timedwait
559 (Self_ID.Common.LL.CV'Access,
560 Single_RTS_Lock'Access,
563 Result := pthread_cond_timedwait
564 (Self_ID.Common.LL.CV'Access,
565 Self_ID.Common.LL.L'Access,
569 Check_Time := Monotonic_Clock;
570 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
572 pragma Assert (Result = 0
573 or else Result = ETIMEDOUT
574 or else Result = EINTR);
577 Self_ID.Common.State := Runnable;
589 ---------------------
590 -- Monotonic_Clock --
591 ---------------------
593 function Monotonic_Clock return Duration is
594 TS : aliased timespec;
595 Result : Interfaces.C.int;
597 Result := clock_gettime (Real_Time_Clock_Id, TS'Unchecked_Access);
598 pragma Assert (Result = 0);
599 return To_Duration (TS);
606 function RT_Resolution return Duration is
608 -- The clock_getres (Real_Time_Clock_Id) function appears to return
609 -- the interrupt resolution of the realtime clock and not the actual
610 -- resolution of reading the clock. Even though this last value is
611 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
612 -- have a microsecond resolution or better.
614 -- ??? We should figure out a method to return the right value on
624 procedure Wakeup (T : ST.Task_Id; Reason : System.Tasking.Task_States) is
625 pragma Unreferenced (Reason);
626 Result : Interfaces.C.int;
628 Result := pthread_cond_signal (T.Common.LL.CV'Access);
629 pragma Assert (Result = 0);
636 procedure Yield (Do_Yield : Boolean := True) is
637 Result : Interfaces.C.int;
638 pragma Unreferenced (Result);
641 Result := sched_yield;
649 procedure Set_Priority
651 Prio : System.Any_Priority;
652 Loss_Of_Inheritance : Boolean := False)
654 pragma Unreferenced (Loss_Of_Inheritance);
656 Result : Interfaces.C.int;
657 Param : aliased struct_sched_param;
658 Sched_Policy : Interfaces.C.int;
660 use type System.Task_Info.Task_Info_Type;
662 function To_Int is new Ada.Unchecked_Conversion
663 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
665 function Get_Policy (Prio : System.Any_Priority) return Character;
666 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
667 -- Get priority specific dispatching policy
669 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
670 -- Upper case first character of the policy name corresponding to the
671 -- task as set by a Priority_Specific_Dispatching pragma.
674 T.Common.Current_Priority := Prio;
675 Param.sched_priority := Interfaces.C.int (Prio);
677 if T.Common.Task_Info /= null then
678 Sched_Policy := To_Int (T.Common.Task_Info.Policy);
680 elsif Dispatching_Policy = 'R'
681 or else Priority_Specific_Policy = 'R'
682 or else Time_Slice_Val > 0
684 Sched_Policy := SCHED_RR;
687 Sched_Policy := SCHED_FIFO;
690 Result := pthread_setschedparam (T.Common.LL.Thread, Sched_Policy,
692 pragma Assert (Result = 0);
699 function Get_Priority (T : Task_Id) return System.Any_Priority is
701 return T.Common.Current_Priority;
708 procedure Enter_Task (Self_ID : Task_Id) is
709 Result : Interfaces.C.int;
711 function To_Int is new Ada.Unchecked_Conversion
712 (System.Task_Info.CPU_Number, Interfaces.C.int);
714 use System.Task_Info;
717 Self_ID.Common.LL.Thread := pthread_self;
718 Specific.Set (Self_ID);
720 if Self_ID.Common.Task_Info /= null
721 and then Self_ID.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
722 and then Self_ID.Common.Task_Info.Runon_CPU /= ANY_CPU
724 Result := pthread_setrunon_np
725 (To_Int (Self_ID.Common.Task_Info.Runon_CPU));
726 pragma Assert (Result = 0);
731 for J in Known_Tasks'Range loop
732 if Known_Tasks (J) = null then
733 Known_Tasks (J) := Self_ID;
734 Self_ID.Known_Tasks_Index := J;
746 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
748 return new Ada_Task_Control_Block (Entry_Num);
755 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
757 -----------------------------
758 -- Register_Foreign_Thread --
759 -----------------------------
761 function Register_Foreign_Thread return Task_Id is
763 if Is_Valid_Task then
766 return Register_Foreign_Thread (pthread_self);
768 end Register_Foreign_Thread;
774 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
775 Result : Interfaces.C.int;
776 Cond_Attr : aliased pthread_condattr_t;
779 if not Single_Lock then
780 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
783 Result := pthread_condattr_init (Cond_Attr'Access);
784 pragma Assert (Result = 0 or else Result = ENOMEM);
788 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
789 pragma Assert (Result = 0 or else Result = ENOMEM);
795 if not Single_Lock then
796 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
797 pragma Assert (Result = 0);
803 Result := pthread_condattr_destroy (Cond_Attr'Access);
804 pragma Assert (Result = 0);
811 procedure Create_Task
813 Wrapper : System.Address;
814 Stack_Size : System.Parameters.Size_Type;
815 Priority : System.Any_Priority;
816 Succeeded : out Boolean)
818 use System.Task_Info;
820 Attributes : aliased pthread_attr_t;
821 Sched_Param : aliased struct_sched_param;
822 Result : Interfaces.C.int;
824 function Thread_Body_Access is new
825 Ada.Unchecked_Conversion (System.Address, Thread_Body);
826 function To_Int is new Ada.Unchecked_Conversion
827 (System.Task_Info.Thread_Scheduling_Scope, Interfaces.C.int);
828 function To_Int is new Ada.Unchecked_Conversion
829 (System.Task_Info.Thread_Scheduling_Inheritance, Interfaces.C.int);
830 function To_Int is new Ada.Unchecked_Conversion
831 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
834 Result := pthread_attr_init (Attributes'Access);
835 pragma Assert (Result = 0 or else Result = ENOMEM);
843 pthread_attr_setdetachstate
844 (Attributes'Access, PTHREAD_CREATE_DETACHED);
845 pragma Assert (Result = 0);
848 pthread_attr_setstacksize
849 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
850 pragma Assert (Result = 0);
852 if T.Common.Task_Info /= null then
854 pthread_attr_setscope
855 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
856 pragma Assert (Result = 0);
859 pthread_attr_setinheritsched
860 (Attributes'Access, To_Int (T.Common.Task_Info.Inheritance));
861 pragma Assert (Result = 0);
864 pthread_attr_setschedpolicy
865 (Attributes'Access, To_Int (T.Common.Task_Info.Policy));
866 pragma Assert (Result = 0);
868 Sched_Param.sched_priority :=
869 Interfaces.C.int (T.Common.Task_Info.Priority);
872 pthread_attr_setschedparam
873 (Attributes'Access, Sched_Param'Access);
874 pragma Assert (Result = 0);
877 -- Since the initial signal mask of a thread is inherited from the
878 -- creator, and the Environment task has all its signals masked, we
879 -- do not need to manipulate caller's signal mask at this point.
880 -- All tasks in RTS will have All_Tasks_Mask initially.
884 (T.Common.LL.Thread'Access,
886 Thread_Body_Access (Wrapper),
890 and then T.Common.Task_Info /= null
891 and then T.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
893 -- The pthread_create call may have failed because we asked for a
894 -- system scope pthread and none were available (probably because
895 -- the program was not executed by the superuser). Let's try for
896 -- a process scope pthread instead of raising Tasking_Error.
899 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
900 System.IO.Put ("""");
901 System.IO.Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len));
902 System.IO.Put_Line (""" could not be honored. ");
903 System.IO.Put_Line ("Scope changed to PTHREAD_SCOPE_PROCESS");
905 T.Common.Task_Info.Scope := PTHREAD_SCOPE_PROCESS;
907 pthread_attr_setscope
908 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
909 pragma Assert (Result = 0);
913 (T.Common.LL.Thread'Access,
915 Thread_Body_Access (Wrapper),
919 pragma Assert (Result = 0 or else Result = EAGAIN);
921 Succeeded := Result = 0;
923 -- The following needs significant commenting ???
925 if T.Common.Task_Info /= null then
926 T.Common.Base_Priority := T.Common.Task_Info.Priority;
927 Set_Priority (T, T.Common.Task_Info.Priority);
929 Set_Priority (T, Priority);
932 Result := pthread_attr_destroy (Attributes'Access);
933 pragma Assert (Result = 0);
940 procedure Finalize_TCB (T : Task_Id) is
941 Result : Interfaces.C.int;
943 Is_Self : constant Boolean := T = Self;
945 procedure Free is new
946 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
949 if not Single_Lock then
950 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
951 pragma Assert (Result = 0);
954 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
955 pragma Assert (Result = 0);
957 if T.Known_Tasks_Index /= -1 then
958 Known_Tasks (T.Known_Tasks_Index) := null;
972 procedure Exit_Task is
981 procedure Abort_Task (T : Task_Id) is
982 Result : Interfaces.C.int;
987 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
988 pragma Assert (Result = 0);
995 procedure Initialize (S : in out Suspension_Object) is
996 Mutex_Attr : aliased pthread_mutexattr_t;
997 Cond_Attr : aliased pthread_condattr_t;
998 Result : Interfaces.C.int;
1001 -- Initialize internal state (always to False (RM D.10(6))
1006 -- Initialize internal mutex
1008 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1009 pragma Assert (Result = 0 or else Result = ENOMEM);
1011 if Result = ENOMEM then
1012 raise Storage_Error;
1015 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1016 pragma Assert (Result = 0 or else Result = ENOMEM);
1018 if Result = ENOMEM then
1019 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1020 pragma Assert (Result = 0);
1022 raise Storage_Error;
1025 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1026 pragma Assert (Result = 0);
1028 -- Initialize internal condition variable
1030 Result := pthread_condattr_init (Cond_Attr'Access);
1031 pragma Assert (Result = 0 or else Result = ENOMEM);
1034 Result := pthread_mutex_destroy (S.L'Access);
1035 pragma Assert (Result = 0);
1037 if Result = ENOMEM then
1038 raise Storage_Error;
1042 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1043 pragma Assert (Result = 0 or else Result = ENOMEM);
1046 Result := pthread_mutex_destroy (S.L'Access);
1047 pragma Assert (Result = 0);
1049 if Result = ENOMEM then
1050 Result := pthread_condattr_destroy (Cond_Attr'Access);
1051 pragma Assert (Result = 0);
1052 raise Storage_Error;
1056 Result := pthread_condattr_destroy (Cond_Attr'Access);
1057 pragma Assert (Result = 0);
1064 procedure Finalize (S : in out Suspension_Object) is
1065 Result : Interfaces.C.int;
1068 -- Destroy internal mutex
1070 Result := pthread_mutex_destroy (S.L'Access);
1071 pragma Assert (Result = 0);
1073 -- Destroy internal condition variable
1075 Result := pthread_cond_destroy (S.CV'Access);
1076 pragma Assert (Result = 0);
1083 function Current_State (S : Suspension_Object) return Boolean is
1085 -- We do not want to use lock on this read operation. State is marked
1086 -- as Atomic so that we ensure that the value retrieved is correct.
1095 procedure Set_False (S : in out Suspension_Object) is
1096 Result : Interfaces.C.int;
1099 SSL.Abort_Defer.all;
1101 Result := pthread_mutex_lock (S.L'Access);
1102 pragma Assert (Result = 0);
1106 Result := pthread_mutex_unlock (S.L'Access);
1107 pragma Assert (Result = 0);
1109 SSL.Abort_Undefer.all;
1116 procedure Set_True (S : in out Suspension_Object) is
1117 Result : Interfaces.C.int;
1120 SSL.Abort_Defer.all;
1122 Result := pthread_mutex_lock (S.L'Access);
1123 pragma Assert (Result = 0);
1125 -- If there is already a task waiting on this suspension object then
1126 -- we resume it, leaving the state of the suspension object to False,
1127 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1128 -- the state to True.
1134 Result := pthread_cond_signal (S.CV'Access);
1135 pragma Assert (Result = 0);
1141 Result := pthread_mutex_unlock (S.L'Access);
1142 pragma Assert (Result = 0);
1144 SSL.Abort_Undefer.all;
1147 ------------------------
1148 -- Suspend_Until_True --
1149 ------------------------
1151 procedure Suspend_Until_True (S : in out Suspension_Object) is
1152 Result : Interfaces.C.int;
1155 SSL.Abort_Defer.all;
1157 Result := pthread_mutex_lock (S.L'Access);
1158 pragma Assert (Result = 0);
1162 -- Program_Error must be raised upon calling Suspend_Until_True
1163 -- if another task is already waiting on that suspension object
1166 Result := pthread_mutex_unlock (S.L'Access);
1167 pragma Assert (Result = 0);
1169 SSL.Abort_Undefer.all;
1171 raise Program_Error;
1173 -- Suspend the task if the state is False. Otherwise, the task
1174 -- continues its execution, and the state of the suspension object
1175 -- is set to False (ARM D.10 par. 9).
1181 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1184 Result := pthread_mutex_unlock (S.L'Access);
1185 pragma Assert (Result = 0);
1187 SSL.Abort_Undefer.all;
1189 end Suspend_Until_True;
1197 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1198 pragma Unreferenced (Self_ID);
1203 --------------------
1204 -- Check_No_Locks --
1205 --------------------
1207 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1208 pragma Unreferenced (Self_ID);
1213 ----------------------
1214 -- Environment_Task --
1215 ----------------------
1217 function Environment_Task return Task_Id is
1219 return Environment_Task_Id;
1220 end Environment_Task;
1226 procedure Lock_RTS is
1228 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1235 procedure Unlock_RTS is
1237 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1244 function Suspend_Task
1246 Thread_Self : Thread_Id) return Boolean
1248 pragma Unreferenced (T);
1249 pragma Unreferenced (Thread_Self);
1258 function Resume_Task
1260 Thread_Self : Thread_Id) return Boolean
1262 pragma Unreferenced (T);
1263 pragma Unreferenced (Thread_Self);
1268 --------------------
1269 -- Stop_All_Tasks --
1270 --------------------
1272 procedure Stop_All_Tasks is
1281 function Stop_Task (T : ST.Task_Id) return Boolean is
1282 pragma Unreferenced (T);
1291 function Continue_Task (T : ST.Task_Id) return Boolean is
1292 pragma Unreferenced (T);
1301 procedure Initialize (Environment_Task : Task_Id) is
1302 act : aliased struct_sigaction;
1303 old_act : aliased struct_sigaction;
1304 Tmp_Set : aliased sigset_t;
1305 Result : Interfaces.C.int;
1308 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1309 pragma Import (C, State, "__gnat_get_interrupt_state");
1310 -- Get interrupt state. Defined in a-init.c. The input argument is
1311 -- the interrupt number, and the result is one of the following:
1313 Default : constant Character := 's';
1314 -- 'n' this interrupt not set by any Interrupt_State pragma
1315 -- 'u' Interrupt_State pragma set state to User
1316 -- 'r' Interrupt_State pragma set state to Runtime
1317 -- 's' Interrupt_State pragma set state to System (use "default"
1321 Environment_Task_Id := Environment_Task;
1323 Interrupt_Management.Initialize;
1325 -- Initialize the lock used to synchronize chain of all ATCBs
1327 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1329 Specific.Initialize (Environment_Task);
1331 Enter_Task (Environment_Task);
1333 -- Prepare the set of signals that should unblocked in all tasks
1335 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1336 pragma Assert (Result = 0);
1338 for J in Interrupt_Management.Interrupt_ID loop
1339 if System.Interrupt_Management.Keep_Unmasked (J) then
1340 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1341 pragma Assert (Result = 0);
1345 -- Install the abort-signal handler
1348 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1351 act.sa_handler := Abort_Handler'Address;
1353 Result := sigemptyset (Tmp_Set'Access);
1354 pragma Assert (Result = 0);
1355 act.sa_mask := Tmp_Set;
1359 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1360 act'Unchecked_Access,
1361 old_act'Unchecked_Access);
1362 pragma Assert (Result = 0);
1366 end System.Task_Primitives.Operations;