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)
103 Abort_Handler_Installed : Boolean := False;
104 -- True if a handler for the abort signal is installed
112 procedure Initialize (Environment_Task : Task_Id);
113 pragma Inline (Initialize);
114 -- Initialize various data needed by this package
116 function Is_Valid_Task return Boolean;
117 pragma Inline (Is_Valid_Task);
118 -- Does executing thread have a TCB?
120 procedure Set (Self_Id : Task_Id);
122 -- Set the self id for the current task
124 function Self return Task_Id;
125 pragma Inline (Self);
126 -- Return a pointer to the Ada Task Control Block of the calling task
130 package body Specific is separate;
131 -- The body of this package is target specific
133 ---------------------------------
134 -- Support for foreign threads --
135 ---------------------------------
137 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
138 -- Allocate and Initialize a new ATCB for the current Thread
140 function Register_Foreign_Thread
141 (Thread : Thread_Id) return Task_Id is separate;
143 -----------------------
144 -- Local Subprograms --
145 -----------------------
147 function To_Address is
148 new Ada.Unchecked_Conversion (Task_Id, System.Address);
150 procedure Abort_Handler (Sig : Signal);
151 -- Signal handler used to implement asynchronous abort
157 procedure Abort_Handler (Sig : Signal) is
158 pragma Unreferenced (Sig);
160 T : constant Task_Id := Self;
161 Result : Interfaces.C.int;
162 Old_Set : aliased sigset_t;
165 -- It's not safe to raise an exception when using GCC ZCX mechanism.
166 -- Note that we still need to install a signal handler, since in some
167 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
168 -- need to send the Abort signal to a task.
170 if ZCX_By_Default and then GCC_ZCX_Support then
174 if T.Deferral_Level = 0
175 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
177 -- Make sure signals used for RTS internal purpose are unmasked
179 Result := pthread_sigmask
181 Unblocked_Signal_Mask'Access,
183 pragma Assert (Result = 0);
185 raise Standard'Abort_Signal;
193 -- The underlying thread system sets a guard page at the
194 -- bottom of a thread stack, so nothing is needed.
196 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
197 pragma Unreferenced (On);
198 pragma Unreferenced (T);
207 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
209 return T.Common.LL.Thread;
216 function Self return Task_Id renames Specific.Self;
218 ---------------------
219 -- Initialize_Lock --
220 ---------------------
222 -- Note: mutexes and cond_variables needed per-task basis are initialized
223 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
224 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
225 -- status change of RTS. Therefore raising Storage_Error in the following
226 -- routines should be able to be handled safely.
228 procedure Initialize_Lock
229 (Prio : System.Any_Priority;
230 L : not null access Lock)
232 Attributes : aliased pthread_mutexattr_t;
233 Result : Interfaces.C.int;
236 Result := pthread_mutexattr_init (Attributes'Access);
237 pragma Assert (Result = 0 or else Result = ENOMEM);
239 if Result = ENOMEM then
243 if Locking_Policy = 'C' then
245 pthread_mutexattr_setprotocol
246 (Attributes'Access, PTHREAD_PRIO_PROTECT);
247 pragma Assert (Result = 0);
250 pthread_mutexattr_setprioceiling
251 (Attributes'Access, Interfaces.C.int (Prio));
252 pragma Assert (Result = 0);
255 Result := pthread_mutex_init (L, Attributes'Access);
256 pragma Assert (Result = 0 or else Result = ENOMEM);
258 if Result = ENOMEM then
259 Result := pthread_mutexattr_destroy (Attributes'Access);
263 Result := pthread_mutexattr_destroy (Attributes'Access);
264 pragma Assert (Result = 0);
267 procedure Initialize_Lock
268 (L : not null access RTS_Lock;
271 pragma Unreferenced (Level);
273 Attributes : aliased pthread_mutexattr_t;
274 Result : Interfaces.C.int;
277 Result := pthread_mutexattr_init (Attributes'Access);
278 pragma Assert (Result = 0 or else Result = ENOMEM);
280 if Result = ENOMEM then
284 if Locking_Policy = 'C' then
285 Result := pthread_mutexattr_setprotocol
286 (Attributes'Access, PTHREAD_PRIO_PROTECT);
287 pragma Assert (Result = 0);
289 Result := pthread_mutexattr_setprioceiling
290 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
291 pragma Assert (Result = 0);
294 Result := pthread_mutex_init (L, Attributes'Access);
296 pragma Assert (Result = 0 or else Result = ENOMEM);
298 if Result = ENOMEM then
299 Result := pthread_mutexattr_destroy (Attributes'Access);
303 Result := pthread_mutexattr_destroy (Attributes'Access);
304 pragma Assert (Result = 0);
311 procedure Finalize_Lock (L : not null access Lock) is
312 Result : Interfaces.C.int;
314 Result := pthread_mutex_destroy (L);
315 pragma Assert (Result = 0);
318 procedure Finalize_Lock (L : not null access RTS_Lock) is
319 Result : Interfaces.C.int;
321 Result := pthread_mutex_destroy (L);
322 pragma Assert (Result = 0);
330 (L : not null access Lock; Ceiling_Violation : out Boolean)
332 Result : Interfaces.C.int;
335 Result := pthread_mutex_lock (L);
336 Ceiling_Violation := Result = EINVAL;
338 -- Assumes the cause of EINVAL is a priority ceiling violation
340 pragma Assert (Result = 0 or else Result = EINVAL);
344 (L : not null access RTS_Lock;
345 Global_Lock : Boolean := False)
347 Result : Interfaces.C.int;
349 if not Single_Lock or else Global_Lock then
350 Result := pthread_mutex_lock (L);
351 pragma Assert (Result = 0);
355 procedure Write_Lock (T : Task_Id) is
356 Result : Interfaces.C.int;
358 if not Single_Lock then
359 Result := pthread_mutex_lock (T.Common.LL.L'Access);
360 pragma Assert (Result = 0);
369 (L : not null access Lock; Ceiling_Violation : out Boolean) is
371 Write_Lock (L, Ceiling_Violation);
378 procedure Unlock (L : not null access Lock) is
379 Result : Interfaces.C.int;
381 Result := pthread_mutex_unlock (L);
382 pragma Assert (Result = 0);
386 (L : not null access RTS_Lock;
387 Global_Lock : Boolean := False)
389 Result : Interfaces.C.int;
391 if not Single_Lock or else Global_Lock then
392 Result := pthread_mutex_unlock (L);
393 pragma Assert (Result = 0);
397 procedure Unlock (T : Task_Id) is
398 Result : Interfaces.C.int;
400 if not Single_Lock then
401 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
402 pragma Assert (Result = 0);
410 -- Dynamic priority ceilings are not supported by the underlying system
412 procedure Set_Ceiling
413 (L : not null access Lock;
414 Prio : System.Any_Priority)
416 pragma Unreferenced (L, Prio);
426 (Self_ID : ST.Task_Id;
427 Reason : System.Tasking.Task_States)
429 pragma Unreferenced (Reason);
430 Result : Interfaces.C.int;
435 (cond => Self_ID.Common.LL.CV'Access,
436 mutex => (if Single_Lock
437 then Single_RTS_Lock'Access
438 else 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;
471 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
472 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
474 if Abs_Time > Check_Time then
475 Request := To_Timespec (Abs_Time);
478 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
481 pthread_cond_timedwait
482 (cond => Self_ID.Common.LL.CV'Access,
483 mutex => (if Single_Lock
484 then Single_RTS_Lock'Access
485 else Self_ID.Common.LL.L'Access),
486 abstime => Request'Access);
488 Check_Time := Monotonic_Clock;
489 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
491 if Result = 0 or else errno = EINTR then
503 -- This is for use in implementing delay statements, so we assume
504 -- the caller is abort-deferred but is holding no locks.
506 procedure Timed_Delay
509 Mode : ST.Delay_Modes)
511 Base_Time : constant Duration := Monotonic_Clock;
512 Check_Time : Duration := Base_Time;
514 Request : aliased timespec;
515 Result : Interfaces.C.int;
522 Write_Lock (Self_ID);
526 then Time + Check_Time
527 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
529 if Abs_Time > Check_Time then
530 Request := To_Timespec (Abs_Time);
531 Self_ID.Common.State := Delay_Sleep;
534 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
537 pthread_cond_timedwait
538 (cond => Self_ID.Common.LL.CV'Access,
539 mutex => (if Single_Lock
540 then Single_RTS_Lock'Access
541 else Self_ID.Common.LL.L'Access),
542 abstime => Request'Access);
544 Check_Time := Monotonic_Clock;
545 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
547 pragma Assert (Result = 0
548 or else Result = ETIMEDOUT
549 or else Result = EINTR);
552 Self_ID.Common.State := Runnable;
564 ---------------------
565 -- Monotonic_Clock --
566 ---------------------
568 function Monotonic_Clock return Duration is
569 TS : aliased timespec;
570 Result : Interfaces.C.int;
572 Result := clock_gettime (Real_Time_Clock_Id, TS'Unchecked_Access);
573 pragma Assert (Result = 0);
574 return To_Duration (TS);
581 function RT_Resolution return Duration is
583 -- The clock_getres (Real_Time_Clock_Id) function appears to return
584 -- the interrupt resolution of the realtime clock and not the actual
585 -- resolution of reading the clock. Even though this last value is
586 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
587 -- have a microsecond resolution or better.
589 -- ??? We should figure out a method to return the right value on
599 procedure Wakeup (T : ST.Task_Id; Reason : System.Tasking.Task_States) is
600 pragma Unreferenced (Reason);
601 Result : Interfaces.C.int;
603 Result := pthread_cond_signal (T.Common.LL.CV'Access);
604 pragma Assert (Result = 0);
611 procedure Yield (Do_Yield : Boolean := True) is
612 Result : Interfaces.C.int;
613 pragma Unreferenced (Result);
616 Result := sched_yield;
624 procedure Set_Priority
626 Prio : System.Any_Priority;
627 Loss_Of_Inheritance : Boolean := False)
629 pragma Unreferenced (Loss_Of_Inheritance);
631 Result : Interfaces.C.int;
632 Param : aliased struct_sched_param;
633 Sched_Policy : Interfaces.C.int;
635 use type System.Task_Info.Task_Info_Type;
637 function To_Int is new Ada.Unchecked_Conversion
638 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
640 function Get_Policy (Prio : System.Any_Priority) return Character;
641 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
642 -- Get priority specific dispatching policy
644 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
645 -- Upper case first character of the policy name corresponding to the
646 -- task as set by a Priority_Specific_Dispatching pragma.
649 T.Common.Current_Priority := Prio;
650 Param.sched_priority := Interfaces.C.int (Prio);
652 if T.Common.Task_Info /= null then
653 Sched_Policy := To_Int (T.Common.Task_Info.Policy);
655 elsif Dispatching_Policy = 'R'
656 or else Priority_Specific_Policy = 'R'
657 or else Time_Slice_Val > 0
659 Sched_Policy := SCHED_RR;
662 Sched_Policy := SCHED_FIFO;
665 Result := pthread_setschedparam (T.Common.LL.Thread, Sched_Policy,
667 pragma Assert (Result = 0);
674 function Get_Priority (T : Task_Id) return System.Any_Priority is
676 return T.Common.Current_Priority;
683 procedure Enter_Task (Self_ID : Task_Id) is
684 Result : Interfaces.C.int;
686 function To_Int is new Ada.Unchecked_Conversion
687 (System.Task_Info.CPU_Number, Interfaces.C.int);
689 use System.Task_Info;
692 Self_ID.Common.LL.Thread := pthread_self;
693 Specific.Set (Self_ID);
695 if Self_ID.Common.Task_Info /= null
696 and then Self_ID.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
697 and then Self_ID.Common.Task_Info.Runon_CPU /= ANY_CPU
699 Result := pthread_setrunon_np
700 (To_Int (Self_ID.Common.Task_Info.Runon_CPU));
701 pragma Assert (Result = 0);
709 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
711 return new Ada_Task_Control_Block (Entry_Num);
718 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
720 -----------------------------
721 -- Register_Foreign_Thread --
722 -----------------------------
724 function Register_Foreign_Thread return Task_Id is
726 if Is_Valid_Task then
729 return Register_Foreign_Thread (pthread_self);
731 end Register_Foreign_Thread;
737 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
738 Result : Interfaces.C.int;
739 Cond_Attr : aliased pthread_condattr_t;
742 if not Single_Lock then
743 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
746 Result := pthread_condattr_init (Cond_Attr'Access);
747 pragma Assert (Result = 0 or else Result = ENOMEM);
751 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
752 pragma Assert (Result = 0 or else Result = ENOMEM);
758 if not Single_Lock then
759 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
760 pragma Assert (Result = 0);
766 Result := pthread_condattr_destroy (Cond_Attr'Access);
767 pragma Assert (Result = 0);
774 procedure Create_Task
776 Wrapper : System.Address;
777 Stack_Size : System.Parameters.Size_Type;
778 Priority : System.Any_Priority;
779 Succeeded : out Boolean)
781 use System.Task_Info;
783 Attributes : aliased pthread_attr_t;
784 Sched_Param : aliased struct_sched_param;
785 Result : Interfaces.C.int;
787 function Thread_Body_Access is new
788 Ada.Unchecked_Conversion (System.Address, Thread_Body);
789 function To_Int is new Ada.Unchecked_Conversion
790 (System.Task_Info.Thread_Scheduling_Scope, Interfaces.C.int);
791 function To_Int is new Ada.Unchecked_Conversion
792 (System.Task_Info.Thread_Scheduling_Inheritance, Interfaces.C.int);
793 function To_Int is new Ada.Unchecked_Conversion
794 (System.Task_Info.Thread_Scheduling_Policy, Interfaces.C.int);
797 Result := pthread_attr_init (Attributes'Access);
798 pragma Assert (Result = 0 or else Result = ENOMEM);
806 pthread_attr_setdetachstate
807 (Attributes'Access, PTHREAD_CREATE_DETACHED);
808 pragma Assert (Result = 0);
811 pthread_attr_setstacksize
812 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
813 pragma Assert (Result = 0);
815 if T.Common.Task_Info /= null then
817 pthread_attr_setscope
818 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
819 pragma Assert (Result = 0);
822 pthread_attr_setinheritsched
823 (Attributes'Access, To_Int (T.Common.Task_Info.Inheritance));
824 pragma Assert (Result = 0);
827 pthread_attr_setschedpolicy
828 (Attributes'Access, To_Int (T.Common.Task_Info.Policy));
829 pragma Assert (Result = 0);
831 Sched_Param.sched_priority :=
832 Interfaces.C.int (T.Common.Task_Info.Priority);
835 pthread_attr_setschedparam
836 (Attributes'Access, Sched_Param'Access);
837 pragma Assert (Result = 0);
840 -- Since the initial signal mask of a thread is inherited from the
841 -- creator, and the Environment task has all its signals masked, we
842 -- do not need to manipulate caller's signal mask at this point.
843 -- All tasks in RTS will have All_Tasks_Mask initially.
847 (T.Common.LL.Thread'Access,
849 Thread_Body_Access (Wrapper),
853 and then T.Common.Task_Info /= null
854 and then T.Common.Task_Info.Scope = PTHREAD_SCOPE_SYSTEM
856 -- The pthread_create call may have failed because we asked for a
857 -- system scope pthread and none were available (probably because
858 -- the program was not executed by the superuser). Let's try for
859 -- a process scope pthread instead of raising Tasking_Error.
862 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
863 System.IO.Put ("""");
864 System.IO.Put (T.Common.Task_Image (1 .. T.Common.Task_Image_Len));
865 System.IO.Put_Line (""" could not be honored. ");
866 System.IO.Put_Line ("Scope changed to PTHREAD_SCOPE_PROCESS");
868 T.Common.Task_Info.Scope := PTHREAD_SCOPE_PROCESS;
870 pthread_attr_setscope
871 (Attributes'Access, To_Int (T.Common.Task_Info.Scope));
872 pragma Assert (Result = 0);
876 (T.Common.LL.Thread'Access,
878 Thread_Body_Access (Wrapper),
882 pragma Assert (Result = 0 or else Result = EAGAIN);
884 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);
898 Result := pthread_attr_destroy (Attributes'Access);
899 pragma Assert (Result = 0);
906 procedure Finalize_TCB (T : Task_Id) is
907 Result : Interfaces.C.int;
909 Is_Self : constant Boolean := T = Self;
911 procedure Free is new
912 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
915 if not Single_Lock then
916 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
917 pragma Assert (Result = 0);
920 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
921 pragma Assert (Result = 0);
923 if T.Known_Tasks_Index /= -1 then
924 Known_Tasks (T.Known_Tasks_Index) := null;
938 procedure Exit_Task is
947 procedure Abort_Task (T : Task_Id) is
948 Result : Interfaces.C.int;
950 if Abort_Handler_Installed then
954 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
955 pragma Assert (Result = 0);
963 procedure Initialize (S : in out Suspension_Object) is
964 Mutex_Attr : aliased pthread_mutexattr_t;
965 Cond_Attr : aliased pthread_condattr_t;
966 Result : Interfaces.C.int;
969 -- Initialize internal state (always to False (RM D.10(6))
974 -- Initialize internal mutex
976 Result := pthread_mutexattr_init (Mutex_Attr'Access);
977 pragma Assert (Result = 0 or else Result = ENOMEM);
979 if Result = ENOMEM then
983 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
984 pragma Assert (Result = 0 or else Result = ENOMEM);
986 if Result = ENOMEM then
987 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
988 pragma Assert (Result = 0);
993 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
994 pragma Assert (Result = 0);
996 -- Initialize internal condition variable
998 Result := pthread_condattr_init (Cond_Attr'Access);
999 pragma Assert (Result = 0 or else Result = ENOMEM);
1002 Result := pthread_mutex_destroy (S.L'Access);
1003 pragma Assert (Result = 0);
1005 if Result = ENOMEM then
1006 raise Storage_Error;
1010 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1011 pragma Assert (Result = 0 or else Result = ENOMEM);
1014 Result := pthread_mutex_destroy (S.L'Access);
1015 pragma Assert (Result = 0);
1017 if Result = ENOMEM then
1018 Result := pthread_condattr_destroy (Cond_Attr'Access);
1019 pragma Assert (Result = 0);
1020 raise Storage_Error;
1024 Result := pthread_condattr_destroy (Cond_Attr'Access);
1025 pragma Assert (Result = 0);
1032 procedure Finalize (S : in out Suspension_Object) is
1033 Result : Interfaces.C.int;
1036 -- Destroy internal mutex
1038 Result := pthread_mutex_destroy (S.L'Access);
1039 pragma Assert (Result = 0);
1041 -- Destroy internal condition variable
1043 Result := pthread_cond_destroy (S.CV'Access);
1044 pragma Assert (Result = 0);
1051 function Current_State (S : Suspension_Object) return Boolean is
1053 -- We do not want to use lock on this read operation. State is marked
1054 -- as Atomic so that we ensure that the value retrieved is correct.
1063 procedure Set_False (S : in out Suspension_Object) is
1064 Result : Interfaces.C.int;
1067 SSL.Abort_Defer.all;
1069 Result := pthread_mutex_lock (S.L'Access);
1070 pragma Assert (Result = 0);
1074 Result := pthread_mutex_unlock (S.L'Access);
1075 pragma Assert (Result = 0);
1077 SSL.Abort_Undefer.all;
1084 procedure Set_True (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);
1093 -- If there is already a task waiting on this suspension object then
1094 -- we resume it, leaving the state of the suspension object to False,
1095 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1096 -- the state to True.
1102 Result := pthread_cond_signal (S.CV'Access);
1103 pragma Assert (Result = 0);
1109 Result := pthread_mutex_unlock (S.L'Access);
1110 pragma Assert (Result = 0);
1112 SSL.Abort_Undefer.all;
1115 ------------------------
1116 -- Suspend_Until_True --
1117 ------------------------
1119 procedure Suspend_Until_True (S : in out Suspension_Object) is
1120 Result : Interfaces.C.int;
1123 SSL.Abort_Defer.all;
1125 Result := pthread_mutex_lock (S.L'Access);
1126 pragma Assert (Result = 0);
1130 -- Program_Error must be raised upon calling Suspend_Until_True
1131 -- if another task is already waiting on that suspension object
1134 Result := pthread_mutex_unlock (S.L'Access);
1135 pragma Assert (Result = 0);
1137 SSL.Abort_Undefer.all;
1139 raise Program_Error;
1141 -- Suspend the task if the state is False. Otherwise, the task
1142 -- continues its execution, and the state of the suspension object
1143 -- is set to False (ARM D.10 par. 9).
1151 -- Loop in case pthread_cond_wait returns earlier than expected
1152 -- (e.g. in case of EINTR caused by a signal).
1154 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1155 pragma Assert (Result = 0 or else Result = EINTR);
1157 exit when not S.Waiting;
1161 Result := pthread_mutex_unlock (S.L'Access);
1162 pragma Assert (Result = 0);
1164 SSL.Abort_Undefer.all;
1166 end Suspend_Until_True;
1174 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1175 pragma Unreferenced (Self_ID);
1180 --------------------
1181 -- Check_No_Locks --
1182 --------------------
1184 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1185 pragma Unreferenced (Self_ID);
1190 ----------------------
1191 -- Environment_Task --
1192 ----------------------
1194 function Environment_Task return Task_Id is
1196 return Environment_Task_Id;
1197 end Environment_Task;
1203 procedure Lock_RTS is
1205 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1212 procedure Unlock_RTS is
1214 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1221 function Suspend_Task
1223 Thread_Self : Thread_Id) return Boolean
1225 pragma Unreferenced (T);
1226 pragma Unreferenced (Thread_Self);
1235 function Resume_Task
1237 Thread_Self : Thread_Id) return Boolean
1239 pragma Unreferenced (T);
1240 pragma Unreferenced (Thread_Self);
1245 --------------------
1246 -- Stop_All_Tasks --
1247 --------------------
1249 procedure Stop_All_Tasks is
1258 function Stop_Task (T : ST.Task_Id) return Boolean is
1259 pragma Unreferenced (T);
1268 function Continue_Task (T : ST.Task_Id) return Boolean is
1269 pragma Unreferenced (T);
1278 procedure Initialize (Environment_Task : Task_Id) is
1279 act : aliased struct_sigaction;
1280 old_act : aliased struct_sigaction;
1281 Tmp_Set : aliased sigset_t;
1282 Result : Interfaces.C.int;
1285 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1286 pragma Import (C, State, "__gnat_get_interrupt_state");
1287 -- Get interrupt state. Defined in a-init.c. The input argument is
1288 -- the interrupt number, and the result is one of the following:
1290 Default : constant Character := 's';
1291 -- 'n' this interrupt not set by any Interrupt_State pragma
1292 -- 'u' Interrupt_State pragma set state to User
1293 -- 'r' Interrupt_State pragma set state to Runtime
1294 -- 's' Interrupt_State pragma set state to System (use "default"
1298 Environment_Task_Id := Environment_Task;
1300 Interrupt_Management.Initialize;
1302 -- Initialize the lock used to synchronize chain of all ATCBs
1304 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1306 Specific.Initialize (Environment_Task);
1308 -- Make environment task known here because it doesn't go through
1309 -- Activate_Tasks, which does it for all other tasks.
1311 Known_Tasks (Known_Tasks'First) := Environment_Task;
1312 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1314 Enter_Task (Environment_Task);
1316 -- Prepare the set of signals that should unblocked in all tasks
1318 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1319 pragma Assert (Result = 0);
1321 for J in Interrupt_Management.Interrupt_ID loop
1322 if System.Interrupt_Management.Keep_Unmasked (J) then
1323 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1324 pragma Assert (Result = 0);
1329 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1332 act.sa_handler := Abort_Handler'Address;
1334 Result := sigemptyset (Tmp_Set'Access);
1335 pragma Assert (Result = 0);
1336 act.sa_mask := Tmp_Set;
1340 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1341 act'Unchecked_Access,
1342 old_act'Unchecked_Access);
1343 pragma Assert (Result = 0);
1344 Abort_Handler_Installed := True;
1348 end System.Task_Primitives.Operations;