1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2010, 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 GNU/Linux (GNU/LinuxThreads) 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;
50 with System.Stack_Checking.Operations;
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;
61 package SC renames System.Stack_Checking.Operations;
63 use System.Tasking.Debug;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
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 Unblocked_Signal_Mask : aliased sigset_t;
90 -- The set of signals that should be unblocked in all tasks
92 -- The followings are internal configuration constants needed
94 Next_Serial_Number : Task_Serial_Number := 100;
95 -- We start at 100 (reserve some special values for using in error checks)
97 Time_Slice_Val : Integer;
98 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
100 Dispatching_Policy : Character;
101 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
103 -- The following are effectively constants, but they need to be initialized
104 -- by calling a pthread_ function.
106 Mutex_Attr : aliased pthread_mutexattr_t;
107 Cond_Attr : aliased pthread_condattr_t;
109 Foreign_Task_Elaborated : aliased Boolean := True;
110 -- Used to identified fake tasks (i.e., non-Ada Threads)
112 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
113 -- Whether to use an alternate signal stack for stack overflows
115 Abort_Handler_Installed : Boolean := False;
116 -- True if a handler for the abort signal is installed
124 procedure Initialize (Environment_Task : Task_Id);
125 pragma Inline (Initialize);
126 -- Initialize various data needed by this package
128 function Is_Valid_Task return Boolean;
129 pragma Inline (Is_Valid_Task);
130 -- Does executing thread have a TCB?
132 procedure Set (Self_Id : Task_Id);
134 -- Set the self id for the current task
136 function Self return Task_Id;
137 pragma Inline (Self);
138 -- Return a pointer to the Ada Task Control Block of the calling task
142 package body Specific is separate;
143 -- The body of this package is target specific
145 ---------------------------------
146 -- Support for foreign threads --
147 ---------------------------------
149 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
150 -- Allocate and Initialize a new ATCB for the current Thread
152 function Register_Foreign_Thread
153 (Thread : Thread_Id) return Task_Id is separate;
155 -----------------------
156 -- Local Subprograms --
157 -----------------------
159 subtype unsigned_long is Interfaces.C.unsigned_long;
161 procedure Abort_Handler (signo : Signal);
163 function To_pthread_t is new Ada.Unchecked_Conversion
164 (unsigned_long, System.OS_Interface.pthread_t);
170 procedure Abort_Handler (signo : Signal) is
171 pragma Unreferenced (signo);
173 Self_Id : constant Task_Id := Self;
174 Result : Interfaces.C.int;
175 Old_Set : aliased sigset_t;
178 -- It's not safe to raise an exception when using GCC ZCX mechanism.
179 -- Note that we still need to install a signal handler, since in some
180 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
181 -- need to send the Abort signal to a task.
183 if ZCX_By_Default and then GCC_ZCX_Support then
187 if Self_Id.Deferral_Level = 0
188 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
189 and then not Self_Id.Aborting
191 Self_Id.Aborting := True;
193 -- Make sure signals used for RTS internal purpose are unmasked
198 Unblocked_Signal_Mask'Access,
200 pragma Assert (Result = 0);
202 raise Standard'Abort_Signal;
210 procedure Lock_RTS is
212 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
219 procedure Unlock_RTS is
221 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
228 -- The underlying thread system extends the memory (up to 2MB) when needed
230 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
231 pragma Unreferenced (T);
232 pragma Unreferenced (On);
241 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
243 return T.Common.LL.Thread;
250 function Self return Task_Id renames Specific.Self;
252 ---------------------
253 -- Initialize_Lock --
254 ---------------------
256 -- Note: mutexes and cond_variables needed per-task basis are initialized
257 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
258 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
259 -- status change of RTS. Therefore raising Storage_Error in the following
260 -- routines should be able to be handled safely.
262 procedure Initialize_Lock
263 (Prio : System.Any_Priority;
264 L : not null access Lock)
266 pragma Unreferenced (Prio);
268 Result : Interfaces.C.int;
271 Result := pthread_mutex_init (L, Mutex_Attr'Access);
273 pragma Assert (Result = 0 or else Result = ENOMEM);
275 if Result = ENOMEM then
276 raise Storage_Error with "Failed to allocate a lock";
280 procedure Initialize_Lock
281 (L : not null access RTS_Lock;
284 pragma Unreferenced (Level);
286 Result : Interfaces.C.int;
289 Result := pthread_mutex_init (L, Mutex_Attr'Access);
291 pragma Assert (Result = 0 or else Result = ENOMEM);
293 if Result = ENOMEM then
302 procedure Finalize_Lock (L : not null access Lock) is
303 Result : Interfaces.C.int;
305 Result := pthread_mutex_destroy (L);
306 pragma Assert (Result = 0);
309 procedure Finalize_Lock (L : not null access RTS_Lock) is
310 Result : Interfaces.C.int;
312 Result := pthread_mutex_destroy (L);
313 pragma Assert (Result = 0);
321 (L : not null access Lock;
322 Ceiling_Violation : out Boolean)
324 Result : Interfaces.C.int;
326 Result := pthread_mutex_lock (L);
327 Ceiling_Violation := Result = EINVAL;
329 -- Assume the cause of EINVAL is a priority ceiling violation
331 pragma Assert (Result = 0 or else Result = EINVAL);
335 (L : not null access RTS_Lock;
336 Global_Lock : Boolean := False)
338 Result : Interfaces.C.int;
340 if not Single_Lock or else Global_Lock then
341 Result := pthread_mutex_lock (L);
342 pragma Assert (Result = 0);
346 procedure Write_Lock (T : Task_Id) is
347 Result : Interfaces.C.int;
349 if not Single_Lock then
350 Result := pthread_mutex_lock (T.Common.LL.L'Access);
351 pragma Assert (Result = 0);
360 (L : not null access Lock;
361 Ceiling_Violation : out Boolean)
364 Write_Lock (L, Ceiling_Violation);
371 procedure Unlock (L : not null access Lock) is
372 Result : Interfaces.C.int;
374 Result := pthread_mutex_unlock (L);
375 pragma Assert (Result = 0);
379 (L : not null access RTS_Lock;
380 Global_Lock : Boolean := False)
382 Result : Interfaces.C.int;
384 if not Single_Lock or else Global_Lock then
385 Result := pthread_mutex_unlock (L);
386 pragma Assert (Result = 0);
390 procedure Unlock (T : Task_Id) is
391 Result : Interfaces.C.int;
393 if not Single_Lock then
394 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
395 pragma Assert (Result = 0);
403 -- Dynamic priority ceilings are not supported by the underlying system
405 procedure Set_Ceiling
406 (L : not null access Lock;
407 Prio : System.Any_Priority)
409 pragma Unreferenced (L, Prio);
420 Reason : System.Tasking.Task_States)
422 pragma Unreferenced (Reason);
424 Result : Interfaces.C.int;
427 pragma Assert (Self_ID = Self);
431 (cond => Self_ID.Common.LL.CV'Access,
432 mutex => (if Single_Lock
433 then Single_RTS_Lock'Access
434 else Self_ID.Common.LL.L'Access));
436 -- EINTR is not considered a failure
438 pragma Assert (Result = 0 or else Result = EINTR);
445 -- This is for use within the run-time system, so abort is
446 -- assumed to be already deferred, and the caller should be
447 -- holding its own ATCB lock.
449 procedure Timed_Sleep
452 Mode : ST.Delay_Modes;
453 Reason : System.Tasking.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 Result = EINTR then
493 -- Somebody may have called Wakeup for us
499 pragma Assert (Result = ETIMEDOUT);
508 -- This is for use in implementing delay statements, so we assume the
509 -- caller is abort-deferred but is holding no locks.
511 procedure Timed_Delay
514 Mode : ST.Delay_Modes)
516 Base_Time : constant Duration := Monotonic_Clock;
517 Check_Time : Duration := Base_Time;
519 Request : aliased timespec;
521 Result : Interfaces.C.int;
522 pragma Warnings (Off, Result);
529 Write_Lock (Self_ID);
533 then Time + Check_Time
534 else 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 pthread_cond_timedwait
545 (cond => Self_ID.Common.LL.CV'Access,
546 mutex => (if Single_Lock
547 then Single_RTS_Lock'Access
548 else Self_ID.Common.LL.L'Access),
549 abstime => Request'Access);
551 Check_Time := Monotonic_Clock;
552 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
554 pragma Assert (Result = 0 or else
555 Result = ETIMEDOUT or else
559 Self_ID.Common.State := Runnable;
568 Result := sched_yield;
571 ---------------------
572 -- Monotonic_Clock --
573 ---------------------
575 function Monotonic_Clock return Duration is
578 type timeval is array (1 .. 2) of C.long;
580 procedure timeval_to_duration
581 (T : not null access timeval;
582 sec : not null access C.long;
583 usec : not null access C.long);
584 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
586 Micro : constant := 10**6;
587 sec : aliased C.long;
588 usec : aliased C.long;
589 TV : aliased timeval;
592 function gettimeofday
593 (Tv : access timeval;
594 Tz : System.Address := System.Null_Address) return int;
595 pragma Import (C, gettimeofday, "gettimeofday");
598 Result := gettimeofday (TV'Access, System.Null_Address);
599 pragma Assert (Result = 0);
600 timeval_to_duration (TV'Access, sec'Access, usec'Access);
601 return Duration (sec) + Duration (usec) / Micro;
608 function RT_Resolution return Duration is
617 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
618 pragma Unreferenced (Reason);
619 Result : Interfaces.C.int;
621 Result := pthread_cond_signal (T.Common.LL.CV'Access);
622 pragma Assert (Result = 0);
629 procedure Yield (Do_Yield : Boolean := True) is
630 Result : Interfaces.C.int;
631 pragma Unreferenced (Result);
634 Result := sched_yield;
642 procedure Set_Priority
644 Prio : System.Any_Priority;
645 Loss_Of_Inheritance : Boolean := False)
647 pragma Unreferenced (Loss_Of_Inheritance);
649 Result : Interfaces.C.int;
650 Param : aliased struct_sched_param;
652 function Get_Policy (Prio : System.Any_Priority) return Character;
653 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
654 -- Get priority specific dispatching policy
656 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
657 -- Upper case first character of the policy name corresponding to the
658 -- task as set by a Priority_Specific_Dispatching pragma.
661 T.Common.Current_Priority := Prio;
663 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
665 Param.sched_priority := Interfaces.C.int (Prio) + 1;
667 if Dispatching_Policy = 'R'
668 or else Priority_Specific_Policy = 'R'
669 or else Time_Slice_Val > 0
672 pthread_setschedparam
673 (T.Common.LL.Thread, SCHED_RR, Param'Access);
675 elsif Dispatching_Policy = 'F'
676 or else Priority_Specific_Policy = 'F'
677 or else Time_Slice_Val = 0
680 pthread_setschedparam
681 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
684 Param.sched_priority := 0;
686 pthread_setschedparam
688 SCHED_OTHER, Param'Access);
691 pragma Assert (Result = 0 or else Result = EPERM);
698 function Get_Priority (T : Task_Id) return System.Any_Priority is
700 return T.Common.Current_Priority;
707 procedure Enter_Task (Self_ID : Task_Id) is
709 if Self_ID.Common.Task_Info /= null
710 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
712 raise Invalid_CPU_Number;
715 Self_ID.Common.LL.Thread := pthread_self;
716 Self_ID.Common.LL.LWP := lwp_self;
718 Specific.Set (Self_ID);
720 if Use_Alternate_Stack
721 and then Self_ID.Common.Task_Alternate_Stack /= Null_Address
724 Stack : aliased stack_t;
725 Result : Interfaces.C.int;
727 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
728 Stack.ss_size := Alternate_Stack_Size;
730 Result := sigaltstack (Stack'Access, null);
731 pragma Assert (Result = 0);
740 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
742 return new Ada_Task_Control_Block (Entry_Num);
749 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
751 -----------------------------
752 -- Register_Foreign_Thread --
753 -----------------------------
755 function Register_Foreign_Thread return Task_Id is
757 if Is_Valid_Task then
760 return Register_Foreign_Thread (pthread_self);
762 end Register_Foreign_Thread;
768 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
769 Result : Interfaces.C.int;
772 -- Give the task a unique serial number
774 Self_ID.Serial_Number := Next_Serial_Number;
775 Next_Serial_Number := Next_Serial_Number + 1;
776 pragma Assert (Next_Serial_Number /= 0);
778 Self_ID.Common.LL.Thread := To_pthread_t (-1);
780 if not Single_Lock then
781 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
783 pragma Assert (Result = 0 or else Result = ENOMEM);
791 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
793 pragma Assert (Result = 0 or else Result = ENOMEM);
798 if not Single_Lock then
799 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
800 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 Attributes : aliased pthread_attr_t;
819 Adjusted_Stack_Size : Interfaces.C.size_t;
820 Result : Interfaces.C.int;
823 Adjusted_Stack_Size :=
824 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
826 Result := pthread_attr_init (Attributes'Access);
827 pragma Assert (Result = 0 or else Result = ENOMEM);
835 pthread_attr_setstacksize
836 (Attributes'Access, Adjusted_Stack_Size);
837 pragma Assert (Result = 0);
840 pthread_attr_setdetachstate
841 (Attributes'Access, PTHREAD_CREATE_DETACHED);
842 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),
855 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
859 Result := pthread_attr_destroy (Attributes'Access);
860 pragma Assert (Result = 0);
868 if T.Common.Task_Info /= null then
869 if T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU then
871 pthread_setaffinity_np
874 T.Common.Task_Info.CPU_Affinity'Access);
875 pragma Assert (Result = 0);
879 Result := pthread_attr_destroy (Attributes'Access);
880 pragma Assert (Result = 0);
882 Set_Priority (T, Priority);
889 procedure Finalize_TCB (T : Task_Id) is
890 Result : Interfaces.C.int;
892 Is_Self : constant Boolean := T = Self;
894 procedure Free is new
895 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
898 if not Single_Lock then
899 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
900 pragma Assert (Result = 0);
903 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
904 pragma Assert (Result = 0);
906 if T.Known_Tasks_Index /= -1 then
907 Known_Tasks (T.Known_Tasks_Index) := null;
909 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
921 procedure Exit_Task is
930 procedure Abort_Task (T : Task_Id) is
931 Result : Interfaces.C.int;
933 if Abort_Handler_Installed then
937 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
938 pragma Assert (Result = 0);
946 procedure Initialize (S : in out Suspension_Object) is
947 Result : Interfaces.C.int;
950 -- Initialize internal state (always to False (RM D.10(6)))
955 -- Initialize internal mutex
957 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
959 pragma Assert (Result = 0 or else Result = ENOMEM);
961 if Result = ENOMEM then
965 -- Initialize internal condition variable
967 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
969 pragma Assert (Result = 0 or else Result = ENOMEM);
972 Result := pthread_mutex_destroy (S.L'Access);
973 pragma Assert (Result = 0);
975 if Result = ENOMEM then
985 procedure Finalize (S : in out Suspension_Object) is
986 Result : Interfaces.C.int;
989 -- Destroy internal mutex
991 Result := pthread_mutex_destroy (S.L'Access);
992 pragma Assert (Result = 0);
994 -- Destroy internal condition variable
996 Result := pthread_cond_destroy (S.CV'Access);
997 pragma Assert (Result = 0);
1004 function Current_State (S : Suspension_Object) return Boolean is
1006 -- We do not want to use lock on this read operation. State is marked
1007 -- as Atomic so that we ensure that the value retrieved is correct.
1016 procedure Set_False (S : in out Suspension_Object) is
1017 Result : Interfaces.C.int;
1020 SSL.Abort_Defer.all;
1022 Result := pthread_mutex_lock (S.L'Access);
1023 pragma Assert (Result = 0);
1027 Result := pthread_mutex_unlock (S.L'Access);
1028 pragma Assert (Result = 0);
1030 SSL.Abort_Undefer.all;
1037 procedure Set_True (S : in out Suspension_Object) is
1038 Result : Interfaces.C.int;
1041 SSL.Abort_Defer.all;
1043 Result := pthread_mutex_lock (S.L'Access);
1044 pragma Assert (Result = 0);
1046 -- If there is already a task waiting on this suspension object then
1047 -- we resume it, leaving the state of the suspension object to False,
1048 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1049 -- the state to True.
1055 Result := pthread_cond_signal (S.CV'Access);
1056 pragma Assert (Result = 0);
1062 Result := pthread_mutex_unlock (S.L'Access);
1063 pragma Assert (Result = 0);
1065 SSL.Abort_Undefer.all;
1068 ------------------------
1069 -- Suspend_Until_True --
1070 ------------------------
1072 procedure Suspend_Until_True (S : in out Suspension_Object) is
1073 Result : Interfaces.C.int;
1076 SSL.Abort_Defer.all;
1078 Result := pthread_mutex_lock (S.L'Access);
1079 pragma Assert (Result = 0);
1083 -- Program_Error must be raised upon calling Suspend_Until_True
1084 -- if another task is already waiting on that suspension object
1087 Result := pthread_mutex_unlock (S.L'Access);
1088 pragma Assert (Result = 0);
1090 SSL.Abort_Undefer.all;
1092 raise Program_Error;
1095 -- Suspend the task if the state is False. Otherwise, the task
1096 -- continues its execution, and the state of the suspension object
1097 -- is set to False (ARM D.10 par. 9).
1105 -- Loop in case pthread_cond_wait returns earlier than expected
1106 -- (e.g. in case of EINTR caused by a signal). This should not
1107 -- happen with the current Linux implementation of pthread, but
1108 -- POSIX does not guarantee it so this may change in future.
1110 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1111 pragma Assert (Result = 0 or else Result = EINTR);
1113 exit when not S.Waiting;
1117 Result := pthread_mutex_unlock (S.L'Access);
1118 pragma Assert (Result = 0);
1120 SSL.Abort_Undefer.all;
1122 end Suspend_Until_True;
1130 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1131 pragma Unreferenced (Self_ID);
1136 --------------------
1137 -- Check_No_Locks --
1138 --------------------
1140 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1141 pragma Unreferenced (Self_ID);
1146 ----------------------
1147 -- Environment_Task --
1148 ----------------------
1150 function Environment_Task return Task_Id is
1152 return Environment_Task_Id;
1153 end Environment_Task;
1159 function Suspend_Task
1161 Thread_Self : Thread_Id) return Boolean
1164 if T.Common.LL.Thread /= Thread_Self then
1165 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1175 function Resume_Task
1177 Thread_Self : Thread_Id) return Boolean
1180 if T.Common.LL.Thread /= Thread_Self then
1181 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1187 --------------------
1188 -- Stop_All_Tasks --
1189 --------------------
1191 procedure Stop_All_Tasks is
1200 function Stop_Task (T : ST.Task_Id) return Boolean is
1201 pragma Unreferenced (T);
1210 function Continue_Task (T : ST.Task_Id) return Boolean is
1211 pragma Unreferenced (T);
1220 procedure Initialize (Environment_Task : Task_Id) is
1221 act : aliased struct_sigaction;
1222 old_act : aliased struct_sigaction;
1223 Tmp_Set : aliased sigset_t;
1224 Result : Interfaces.C.int;
1225 -- Whether to use an alternate signal stack for stack overflows
1228 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1229 pragma Import (C, State, "__gnat_get_interrupt_state");
1230 -- Get interrupt state. Defined in a-init.c
1231 -- The input argument is the interrupt number,
1232 -- and the result is one of the following:
1234 Default : constant Character := 's';
1235 -- 'n' this interrupt not set by any Interrupt_State pragma
1236 -- 'u' Interrupt_State pragma set state to User
1237 -- 'r' Interrupt_State pragma set state to Runtime
1238 -- 's' Interrupt_State pragma set state to System (use "default"
1242 Environment_Task_Id := Environment_Task;
1244 Interrupt_Management.Initialize;
1246 -- Prepare the set of signals that should be unblocked in all tasks
1248 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1249 pragma Assert (Result = 0);
1251 for J in Interrupt_Management.Interrupt_ID loop
1252 if System.Interrupt_Management.Keep_Unmasked (J) then
1253 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1254 pragma Assert (Result = 0);
1258 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1259 pragma Assert (Result = 0);
1261 Result := pthread_condattr_init (Cond_Attr'Access);
1262 pragma Assert (Result = 0);
1264 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1266 -- Initialize the global RTS lock
1268 Specific.Initialize (Environment_Task);
1270 if Use_Alternate_Stack then
1271 Environment_Task.Common.Task_Alternate_Stack :=
1272 Alternate_Stack'Address;
1275 -- Make environment task known here because it doesn't go through
1276 -- Activate_Tasks, which does it for all other tasks.
1278 Known_Tasks (Known_Tasks'First) := Environment_Task;
1279 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1281 Enter_Task (Environment_Task);
1284 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1287 act.sa_handler := Abort_Handler'Address;
1289 Result := sigemptyset (Tmp_Set'Access);
1290 pragma Assert (Result = 0);
1291 act.sa_mask := Tmp_Set;
1295 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1296 act'Unchecked_Access,
1297 old_act'Unchecked_Access);
1298 pragma Assert (Result = 0);
1299 Abort_Handler_Installed := True;
1303 end System.Task_Primitives.Operations;