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-2006, 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 Unchecked_Conversion;
72 with 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 new Unchecked_Conversion (Task_Id, System.Address);
162 procedure Abort_Handler (Sig : Signal);
163 -- Signal handler used to implement asynchronous abort
169 procedure Abort_Handler (Sig : Signal) is
170 pragma Unreferenced (Sig);
172 T : constant Task_Id := Self;
173 Result : Interfaces.C.int;
174 Old_Set : aliased sigset_t;
177 -- It is not safe to raise an exception when using ZCX and the GCC
178 -- exception handling mechanism.
180 if ZCX_By_Default and then GCC_ZCX_Support then
184 if T.Deferral_Level = 0
185 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
187 -- Make sure signals used for RTS internal purpose are unmasked
189 Result := pthread_sigmask
191 Unblocked_Signal_Mask'Unchecked_Access,
192 Old_Set'Unchecked_Access);
193 pragma Assert (Result = 0);
195 raise Standard'Abort_Signal;
203 -- The underlying thread system sets a guard page at the
204 -- bottom of a thread stack, so nothing is needed.
206 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
207 pragma Unreferenced (On);
208 pragma Unreferenced (T);
217 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
219 return T.Common.LL.Thread;
226 function Self return Task_Id renames Specific.Self;
228 ---------------------
229 -- Initialize_Lock --
230 ---------------------
232 -- Note: mutexes and cond_variables needed per-task basis are
233 -- initialized in Initialize_TCB and the Storage_Error is
234 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
235 -- used in RTS is initialized before any status change of RTS.
236 -- Therefore rasing Storage_Error in the following routines
237 -- 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
255 Result := pthread_mutexattr_setprotocol
256 (Attributes'Access, PTHREAD_PRIO_PROTECT);
257 pragma Assert (Result = 0);
259 Result := pthread_mutexattr_setprioceiling
260 (Attributes'Access, Interfaces.C.int (Prio));
261 pragma Assert (Result = 0);
264 Result := pthread_mutex_init (L, Attributes'Access);
265 pragma Assert (Result = 0 or else Result = ENOMEM);
267 if Result = ENOMEM then
268 Result := pthread_mutexattr_destroy (Attributes'Access);
272 Result := pthread_mutexattr_destroy (Attributes'Access);
273 pragma Assert (Result = 0);
276 procedure Initialize_Lock
277 (L : not null access RTS_Lock; Level : Lock_Level)
279 pragma Unreferenced (Level);
281 Attributes : aliased pthread_mutexattr_t;
282 Result : Interfaces.C.int;
285 Result := pthread_mutexattr_init (Attributes'Access);
286 pragma Assert (Result = 0 or else Result = ENOMEM);
288 if Result = ENOMEM then
292 if Locking_Policy = 'C' then
293 Result := pthread_mutexattr_setprotocol
294 (Attributes'Access, PTHREAD_PRIO_PROTECT);
295 pragma Assert (Result = 0);
297 Result := pthread_mutexattr_setprioceiling
298 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
299 pragma Assert (Result = 0);
302 Result := pthread_mutex_init (L, Attributes'Access);
304 pragma Assert (Result = 0 or else Result = ENOMEM);
306 if Result = ENOMEM then
307 Result := pthread_mutexattr_destroy (Attributes'Access);
311 Result := pthread_mutexattr_destroy (Attributes'Access);
312 pragma Assert (Result = 0);
319 procedure Finalize_Lock (L : not null access Lock) is
320 Result : Interfaces.C.int;
322 Result := pthread_mutex_destroy (L);
323 pragma Assert (Result = 0);
326 procedure Finalize_Lock (L : not null access RTS_Lock) is
327 Result : Interfaces.C.int;
329 Result := pthread_mutex_destroy (L);
330 pragma Assert (Result = 0);
338 (L : not null access Lock; Ceiling_Violation : out Boolean)
340 Result : Interfaces.C.int;
342 Result := pthread_mutex_lock (L);
343 Ceiling_Violation := Result = EINVAL;
345 -- Assumes the cause of EINVAL is a priority ceiling violation
347 pragma Assert (Result = 0 or else Result = EINVAL);
351 (L : not null access RTS_Lock;
352 Global_Lock : Boolean := False)
354 Result : Interfaces.C.int;
356 if not Single_Lock or else Global_Lock then
357 Result := pthread_mutex_lock (L);
358 pragma Assert (Result = 0);
362 procedure Write_Lock (T : Task_Id) is
363 Result : Interfaces.C.int;
365 if not Single_Lock then
366 Result := pthread_mutex_lock (T.Common.LL.L'Access);
367 pragma Assert (Result = 0);
376 (L : not null access Lock; Ceiling_Violation : out Boolean) is
378 Write_Lock (L, Ceiling_Violation);
385 procedure Unlock (L : not null access Lock) is
386 Result : Interfaces.C.int;
388 Result := pthread_mutex_unlock (L);
389 pragma Assert (Result = 0);
393 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
395 Result : Interfaces.C.int;
398 if not Single_Lock or else Global_Lock then
399 Result := pthread_mutex_unlock (L);
400 pragma Assert (Result = 0);
404 procedure Unlock (T : Task_Id) is
405 Result : Interfaces.C.int;
408 if not Single_Lock then
409 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
410 pragma Assert (Result = 0);
419 (Self_ID : ST.Task_Id;
420 Reason : System.Tasking.Task_States)
422 pragma Unreferenced (Reason);
424 Result : Interfaces.C.int;
428 Result := pthread_cond_wait
429 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
431 Result := pthread_cond_wait
432 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
435 -- EINTR is not considered a failure
437 pragma Assert (Result = 0 or else Result = EINTR);
444 procedure Timed_Sleep
447 Mode : ST.Delay_Modes;
448 Reason : Task_States;
449 Timedout : out Boolean;
450 Yielded : out Boolean)
452 pragma Unreferenced (Reason);
454 Check_Time : constant Duration := Monotonic_Clock;
456 Request : aliased timespec;
457 Result : Interfaces.C.int;
463 if Mode = Relative then
464 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
466 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
469 if Abs_Time > Check_Time then
470 Request := To_Timespec (Abs_Time);
473 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
474 or else Self_ID.Pending_Priority_Change;
477 Result := pthread_cond_timedwait
478 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
482 Result := pthread_cond_timedwait
483 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
487 exit when Abs_Time <= Monotonic_Clock;
489 if Result = 0 or else errno = EINTR then
501 -- This is for use in implementing delay statements, so we assume
502 -- the caller is abort-deferred but is holding no locks.
504 procedure Timed_Delay
507 Mode : ST.Delay_Modes)
509 Check_Time : constant Duration := Monotonic_Clock;
511 Request : aliased timespec;
512 Result : Interfaces.C.int;
519 Write_Lock (Self_ID);
521 if Mode = Relative then
522 Abs_Time := Time + Check_Time;
524 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
527 if Abs_Time > Check_Time then
528 Request := To_Timespec (Abs_Time);
529 Self_ID.Common.State := Delay_Sleep;
532 if Self_ID.Pending_Priority_Change then
533 Self_ID.Pending_Priority_Change := False;
534 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
535 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
538 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
540 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
541 Self_ID.Common.LL.L'Access, Request'Access);
542 exit when Abs_Time <= Monotonic_Clock;
544 pragma Assert (Result = 0
545 or else Result = ETIMEDOUT
546 or else Result = EINTR);
549 Self_ID.Common.State := Runnable;
561 ---------------------
562 -- Monotonic_Clock --
563 ---------------------
565 function Monotonic_Clock return Duration is
566 TS : aliased timespec;
567 Result : Interfaces.C.int;
569 Result := clock_gettime (Real_Time_Clock_Id, TS'Unchecked_Access);
570 pragma Assert (Result = 0);
571 return To_Duration (TS);
578 function RT_Resolution return Duration is
580 -- The clock_getres (Real_Time_Clock_Id) function appears to return
581 -- the interrupt resolution of the realtime clock and not the actual
582 -- resolution of reading the clock. Even though this last value is
583 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
584 -- have a microsecond resolution or better.
586 -- ??? We should figure out a method to return the right value on
596 procedure Wakeup (T : ST.Task_Id; Reason : System.Tasking.Task_States) is
597 pragma Unreferenced (Reason);
598 Result : Interfaces.C.int;
600 Result := pthread_cond_signal (T.Common.LL.CV'Access);
601 pragma Assert (Result = 0);
608 procedure Yield (Do_Yield : Boolean := True) is
609 Result : Interfaces.C.int;
610 pragma Unreferenced (Result);
613 Result := sched_yield;
621 procedure Set_Priority
623 Prio : System.Any_Priority;
624 Loss_Of_Inheritance : Boolean := False)
626 pragma Unreferenced (Loss_Of_Inheritance);
628 Result : Interfaces.C.int;
629 Param : aliased struct_sched_param;
630 Sched_Policy : Interfaces.C.int;
632 use type System.Task_Info.Task_Info_Type;
634 function To_Int is new Unchecked_Conversion
635 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
637 function Get_Policy (Prio : System.Any_Priority) return Character;
638 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
639 -- Get priority specific dispatching policy
641 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
642 -- Upper case first character of the policy name corresponding to the
643 -- task as set by a Priority_Specific_Dispatching pragma.
646 T.Common.Current_Priority := Prio;
647 Param.sched_priority := Interfaces.C.int (Prio);
649 if T.Common.Task_Info /= null then
650 Sched_Policy := To_Int (T.Common.Task_Info.Policy);
652 elsif Dispatching_Policy = 'R'
653 or else Priority_Specific_Policy = 'R'
654 or else Time_Slice_Val > 0
656 Sched_Policy := SCHED_RR;
659 Sched_Policy := SCHED_FIFO;
662 Result := pthread_setschedparam (T.Common.LL.Thread, Sched_Policy,
664 pragma Assert (Result = 0);
671 function Get_Priority (T : Task_Id) return System.Any_Priority is
673 return T.Common.Current_Priority;
680 procedure Enter_Task (Self_ID : Task_Id) is
681 Result : Interfaces.C.int;
683 function To_Int is new Unchecked_Conversion
684 (System.Task_Info.CPU_Number, Interfaces.C.int);
686 use System.Task_Info;
689 Self_ID.Common.LL.Thread := pthread_self;
690 Specific.Set (Self_ID);
692 if Self_ID.Common.Task_Info /= null
693 and then Self_ID.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
694 and then Self_ID.Common.Task_Info.Runon_CPU /= ANY_CPU
696 Result := pthread_setrunon_np
697 (To_Int (Self_ID.Common.Task_Info.Runon_CPU));
698 pragma Assert (Result = 0);
703 for J in Known_Tasks'Range loop
704 if Known_Tasks (J) = null then
705 Known_Tasks (J) := Self_ID;
706 Self_ID.Known_Tasks_Index := J;
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);
759 Result := pthread_cond_init (Self_ID.Common.LL.CV'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 Unchecked_Conversion (System.Address, Thread_Body);
799 function To_Int is new Unchecked_Conversion
800 (System.Task_Info.Thread_Scheduling_Scope, Interfaces.C.int);
801 function To_Int is new Unchecked_Conversion
802 (System.Task_Info.Thread_Scheduling_Inheritance, Interfaces.C.int);
803 function To_Int is new Unchecked_Conversion
804 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
807 Result := pthread_attr_init (Attributes'Access);
808 pragma Assert (Result = 0 or else Result = ENOMEM);
815 Result := pthread_attr_setdetachstate
816 (Attributes'Access, PTHREAD_CREATE_DETACHED);
817 pragma Assert (Result = 0);
819 Result := pthread_attr_setstacksize
820 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
821 pragma Assert (Result = 0);
823 if T.Common.Task_Info /= null then
824 Result := pthread_attr_setscope
825 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
826 pragma Assert (Result = 0);
828 Result := pthread_attr_setinheritsched
829 (Attributes'Access, To_Int (T.Common.Task_Info.Inheritance));
830 pragma Assert (Result = 0);
832 Result := pthread_attr_setschedpolicy
833 (Attributes'Access, To_Int (T.Common.Task_Info.Policy));
834 pragma Assert (Result = 0);
836 Sched_Param.sched_priority :=
837 Interfaces.C.int (T.Common.Task_Info.Priority);
839 Result := pthread_attr_setschedparam
840 (Attributes'Access, Sched_Param'Access);
841 pragma Assert (Result = 0);
844 -- Since the initial signal mask of a thread is inherited from the
845 -- creator, and the Environment task has all its signals masked, we
846 -- do not need to manipulate caller's signal mask at this point.
847 -- All tasks in RTS will have All_Tasks_Mask initially.
849 Result := pthread_create
850 (T.Common.LL.Thread'Access,
852 Thread_Body_Access (Wrapper),
856 and then T.Common.Task_Info /= null
857 and then T.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
859 -- The pthread_create call may have failed because we
860 -- asked for a system scope pthread and none were
861 -- available (probably because the program was not executed
862 -- by the superuser). Let's try for a process scope pthread
863 -- instead of raising Tasking_Error.
866 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
867 System.IO.Put ("""");
868 System.IO.Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len));
869 System.IO.Put_Line (""" could not be honored. ");
870 System.IO.Put_Line ("Scope changed to PTHREAD_SCOPE_PROCESS");
872 T.Common.Task_Info.Scope := PTHREAD_SCOPE_PROCESS;
873 Result := pthread_attr_setscope
874 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
875 pragma Assert (Result = 0);
877 Result := pthread_create
878 (T.Common.LL.Thread'Access,
880 Thread_Body_Access (Wrapper),
884 pragma Assert (Result = 0 or else Result = EAGAIN);
886 Succeeded := Result = 0;
888 -- The following needs significant commenting ???
890 if T.Common.Task_Info /= null then
891 T.Common.Base_Priority := T.Common.Task_Info.Priority;
892 Set_Priority (T, T.Common.Task_Info.Priority);
894 Set_Priority (T, Priority);
897 Result := pthread_attr_destroy (Attributes'Access);
898 pragma Assert (Result = 0);
905 procedure Finalize_TCB (T : Task_Id) is
906 Result : Interfaces.C.int;
908 Is_Self : constant Boolean := T = Self;
910 procedure Free is new
911 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
914 if not Single_Lock then
915 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
916 pragma Assert (Result = 0);
919 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
920 pragma Assert (Result = 0);
922 if T.Known_Tasks_Index /= -1 then
923 Known_Tasks (T.Known_Tasks_Index) := null;
937 procedure Exit_Task is
946 procedure Abort_Task (T : Task_Id) is
947 Result : Interfaces.C.int;
949 Result := pthread_kill (T.Common.LL.Thread,
950 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
951 pragma Assert (Result = 0);
958 procedure Initialize (S : in out Suspension_Object) is
959 Mutex_Attr : aliased pthread_mutexattr_t;
960 Cond_Attr : aliased pthread_condattr_t;
961 Result : Interfaces.C.int;
963 -- Initialize internal state. It is always initialized to False (ARM
969 -- Initialize internal mutex
971 Result := pthread_mutexattr_init (Mutex_Attr'Access);
972 pragma Assert (Result = 0 or else Result = ENOMEM);
974 if Result = ENOMEM then
978 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
979 pragma Assert (Result = 0 or else Result = ENOMEM);
981 if Result = ENOMEM then
982 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
983 pragma Assert (Result = 0);
988 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
989 pragma Assert (Result = 0);
991 -- Initialize internal condition variable
993 Result := pthread_condattr_init (Cond_Attr'Access);
994 pragma Assert (Result = 0 or else Result = ENOMEM);
997 Result := pthread_mutex_destroy (S.L'Access);
998 pragma Assert (Result = 0);
1000 if Result = ENOMEM then
1001 raise Storage_Error;
1005 Result := pthread_cond_init (S.CV'Access, 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 Result := pthread_condattr_destroy (Cond_Attr'Access);
1014 pragma Assert (Result = 0);
1016 raise Storage_Error;
1020 Result := pthread_condattr_destroy (Cond_Attr'Access);
1021 pragma Assert (Result = 0);
1028 procedure Finalize (S : in out Suspension_Object) is
1029 Result : Interfaces.C.int;
1031 -- Destroy internal mutex
1033 Result := pthread_mutex_destroy (S.L'Access);
1034 pragma Assert (Result = 0);
1036 -- Destroy internal condition variable
1038 Result := pthread_cond_destroy (S.CV'Access);
1039 pragma Assert (Result = 0);
1046 function Current_State (S : Suspension_Object) return Boolean is
1048 -- We do not want to use lock on this read operation. State is marked
1049 -- as Atomic so that we ensure that the value retrieved is correct.
1058 procedure Set_False (S : in out Suspension_Object) is
1059 Result : Interfaces.C.int;
1061 SSL.Abort_Defer.all;
1063 Result := pthread_mutex_lock (S.L'Access);
1064 pragma Assert (Result = 0);
1068 Result := pthread_mutex_unlock (S.L'Access);
1069 pragma Assert (Result = 0);
1071 SSL.Abort_Undefer.all;
1078 procedure Set_True (S : in out Suspension_Object) is
1079 Result : Interfaces.C.int;
1081 SSL.Abort_Defer.all;
1083 Result := pthread_mutex_lock (S.L'Access);
1084 pragma Assert (Result = 0);
1086 -- If there is already a task waiting on this suspension object then
1087 -- we resume it, leaving the state of the suspension object to False,
1088 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1089 -- the state to True.
1095 Result := pthread_cond_signal (S.CV'Access);
1096 pragma Assert (Result = 0);
1101 Result := pthread_mutex_unlock (S.L'Access);
1102 pragma Assert (Result = 0);
1104 SSL.Abort_Undefer.all;
1107 ------------------------
1108 -- Suspend_Until_True --
1109 ------------------------
1111 procedure Suspend_Until_True (S : in out Suspension_Object) is
1112 Result : Interfaces.C.int;
1114 SSL.Abort_Defer.all;
1116 Result := pthread_mutex_lock (S.L'Access);
1117 pragma Assert (Result = 0);
1120 -- Program_Error must be raised upon calling Suspend_Until_True
1121 -- if another task is already waiting on that suspension object
1122 -- (ARM D.10 par. 10).
1124 Result := pthread_mutex_unlock (S.L'Access);
1125 pragma Assert (Result = 0);
1127 SSL.Abort_Undefer.all;
1129 raise Program_Error;
1131 -- Suspend the task if the state is False. Otherwise, the task
1132 -- continues its execution, and the state of the suspension object
1133 -- is set to False (ARM D.10 par. 9).
1139 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1142 Result := pthread_mutex_unlock (S.L'Access);
1143 pragma Assert (Result = 0);
1145 SSL.Abort_Undefer.all;
1147 end Suspend_Until_True;
1155 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1156 pragma Unreferenced (Self_ID);
1161 --------------------
1162 -- Check_No_Locks --
1163 --------------------
1165 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1166 pragma Unreferenced (Self_ID);
1171 ----------------------
1172 -- Environment_Task --
1173 ----------------------
1175 function Environment_Task return Task_Id is
1177 return Environment_Task_Id;
1178 end Environment_Task;
1184 procedure Lock_RTS is
1186 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1193 procedure Unlock_RTS is
1195 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1202 function Suspend_Task
1204 Thread_Self : Thread_Id) return Boolean
1206 pragma Unreferenced (T);
1207 pragma Unreferenced (Thread_Self);
1216 function Resume_Task
1218 Thread_Self : Thread_Id) return Boolean
1220 pragma Unreferenced (T);
1221 pragma Unreferenced (Thread_Self);
1230 procedure Initialize (Environment_Task : Task_Id) is
1231 act : aliased struct_sigaction;
1232 old_act : aliased struct_sigaction;
1233 Tmp_Set : aliased sigset_t;
1234 Result : Interfaces.C.int;
1237 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1238 pragma Import (C, State, "__gnat_get_interrupt_state");
1239 -- Get interrupt state. Defined in a-init.c. The input argument is
1240 -- the interrupt number, and the result is one of the following:
1242 Default : constant Character := 's';
1243 -- 'n' this interrupt not set by any Interrupt_State pragma
1244 -- 'u' Interrupt_State pragma set state to User
1245 -- 'r' Interrupt_State pragma set state to Runtime
1246 -- 's' Interrupt_State pragma set state to System (use "default"
1250 Environment_Task_Id := Environment_Task;
1252 Interrupt_Management.Initialize;
1254 -- Initialize the lock used to synchronize chain of all ATCBs
1256 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1258 Specific.Initialize (Environment_Task);
1260 Enter_Task (Environment_Task);
1262 -- Prepare the set of signals that should unblocked in all tasks
1264 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1265 pragma Assert (Result = 0);
1267 for J in Interrupt_Management.Interrupt_ID loop
1268 if System.Interrupt_Management.Keep_Unmasked (J) then
1269 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1270 pragma Assert (Result = 0);
1274 -- Install the abort-signal handler
1276 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1280 act.sa_handler := Abort_Handler'Address;
1282 Result := sigemptyset (Tmp_Set'Access);
1283 pragma Assert (Result = 0);
1284 act.sa_mask := Tmp_Set;
1288 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1289 act'Unchecked_Access,
1290 old_act'Unchecked_Access);
1291 pragma Assert (Result = 0);
1295 end System.Task_Primitives.Operations;