1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a POSIX-like version of this package
36 -- This package contains all the GNULL primitives that interface directly with
39 -- Note: this file can only be used for POSIX compliant systems that implement
40 -- SCHED_FIFO and Ceiling Locking correctly.
42 -- For configurations where SCHED_FIFO and priority ceiling are not a
43 -- requirement, this file can also be used (e.g AiX threads)
46 -- Turn off polling, we do not want ATC polling to take place during tasking
47 -- operations. It causes infinite loops and other problems.
49 with Ada.Unchecked_Conversion;
50 with Ada.Unchecked_Deallocation;
54 with System.Tasking.Debug;
55 with System.Interrupt_Management;
56 with System.OS_Primitives;
57 with System.Task_Info;
59 with System.Soft_Links;
60 -- We use System.Soft_Links instead of System.Tasking.Initialization
61 -- because the later is a higher level package that we shouldn't depend on.
62 -- For example when using the restricted run time, it is replaced by
63 -- System.Tasking.Restricted.Stages.
65 package body System.Task_Primitives.Operations is
67 package SSL renames System.Soft_Links;
69 use System.Tasking.Debug;
72 use System.OS_Interface;
73 use System.Parameters;
74 use System.OS_Primitives;
76 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
77 -- Whether to use an alternate signal stack for stack overflows
83 -- The followings are logically constants, but need to be initialized
86 Single_RTS_Lock : aliased RTS_Lock;
87 -- This is a lock to allow only one thread of control in the RTS at
88 -- a time; it is used to execute in mutual exclusion from all other tasks.
89 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
91 ATCB_Key : aliased pthread_key_t;
92 -- Key used to find the Ada Task_Id associated with a thread
94 Environment_Task_Id : Task_Id;
95 -- A variable to hold Task_Id for the environment task
97 Locking_Policy : Character;
98 pragma Import (C, Locking_Policy, "__gl_locking_policy");
99 -- Value of the pragma Locking_Policy:
100 -- 'C' for Ceiling_Locking
101 -- 'I' for Inherit_Locking
104 Unblocked_Signal_Mask : aliased sigset_t;
105 -- The set of signals that should unblocked in all tasks
107 -- The followings are internal configuration constants needed
109 Next_Serial_Number : Task_Serial_Number := 100;
110 -- We start at 100, to reserve some special values for
111 -- using in error checking.
113 Time_Slice_Val : Integer;
114 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
116 Dispatching_Policy : Character;
117 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
119 Foreign_Task_Elaborated : aliased Boolean := True;
120 -- Used to identified fake tasks (i.e., non-Ada Threads)
128 procedure Initialize (Environment_Task : Task_Id);
129 pragma Inline (Initialize);
130 -- Initialize various data needed by this package
132 function Is_Valid_Task return Boolean;
133 pragma Inline (Is_Valid_Task);
134 -- Does executing thread have a TCB?
136 procedure Set (Self_Id : Task_Id);
138 -- Set the self id for the current task
140 function Self return Task_Id;
141 pragma Inline (Self);
142 -- Return a pointer to the Ada Task Control Block of the calling task
146 package body Specific is separate;
147 -- The body of this package is target specific
149 ---------------------------------
150 -- Support for foreign threads --
151 ---------------------------------
153 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
154 -- Allocate and Initialize a new ATCB for the current Thread
156 function Register_Foreign_Thread
157 (Thread : Thread_Id) return Task_Id is separate;
159 -----------------------
160 -- Local Subprograms --
161 -----------------------
163 procedure Abort_Handler (Sig : Signal);
164 -- Signal handler used to implement asynchronous abort.
165 -- See also comment before body, below.
167 function To_Address is
168 new Ada.Unchecked_Conversion (Task_Id, System.Address);
174 -- Target-dependent binding of inter-thread Abort signal to the raising of
175 -- the Abort_Signal exception.
177 -- The technical issues and alternatives here are essentially the
178 -- same as for raising exceptions in response to other signals
179 -- (e.g. Storage_Error). See code and comments in the package body
180 -- System.Interrupt_Management.
182 -- Some implementations may not allow an exception to be propagated out of
183 -- a handler, and others might leave the signal or interrupt that invoked
184 -- this handler masked after the exceptional return to the application
187 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
188 -- most UNIX systems, this will allow transfer out of a signal handler,
189 -- which is usually the only mechanism available for implementing
190 -- asynchronous handlers of this kind. However, some systems do not
191 -- restore the signal mask on longjmp(), leaving the abort signal masked.
193 procedure Abort_Handler (Sig : Signal) is
194 pragma Unreferenced (Sig);
196 T : constant Task_Id := Self;
197 Old_Set : aliased sigset_t;
199 Result : Interfaces.C.int;
200 pragma Warnings (Off, Result);
203 -- It is not safe to raise an exception when using ZCX and the GCC
204 -- exception handling mechanism.
206 if ZCX_By_Default and then GCC_ZCX_Support then
210 if T.Deferral_Level = 0
211 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
216 -- Make sure signals used for RTS internal purpose are unmasked
218 Result := pthread_sigmask (SIG_UNBLOCK,
219 Unblocked_Signal_Mask'Access, Old_Set'Access);
220 pragma Assert (Result = 0);
222 raise Standard'Abort_Signal;
230 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
231 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
232 Guard_Page_Address : Address;
234 Res : Interfaces.C.int;
237 if Stack_Base_Available then
239 -- Compute the guard page address
241 Guard_Page_Address :=
242 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
245 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
247 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
250 pragma Assert (Res = 0);
258 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
260 return T.Common.LL.Thread;
267 function Self return Task_Id renames Specific.Self;
269 ---------------------
270 -- Initialize_Lock --
271 ---------------------
273 -- Note: mutexes and cond_variables needed per-task basis are
274 -- initialized in Intialize_TCB and the Storage_Error is
275 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
276 -- used in RTS is initialized before any status change of RTS.
277 -- Therefore rasing Storage_Error in the following routines
278 -- should be able to be handled safely.
280 procedure Initialize_Lock
281 (Prio : System.Any_Priority;
282 L : not null access Lock)
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 (Prio));
302 pragma Assert (Result = 0);
304 elsif Locking_Policy = 'I' then
305 Result := pthread_mutexattr_setprotocol
306 (Attributes'Access, PTHREAD_PRIO_INHERIT);
307 pragma Assert (Result = 0);
310 Result := pthread_mutex_init (L, Attributes'Access);
311 pragma Assert (Result = 0 or else Result = ENOMEM);
313 if Result = ENOMEM then
314 Result := pthread_mutexattr_destroy (Attributes'Access);
318 Result := pthread_mutexattr_destroy (Attributes'Access);
319 pragma Assert (Result = 0);
322 procedure Initialize_Lock
323 (L : not null access RTS_Lock; Level : Lock_Level)
325 pragma Unreferenced (Level);
327 Attributes : aliased pthread_mutexattr_t;
328 Result : Interfaces.C.int;
331 Result := pthread_mutexattr_init (Attributes'Access);
332 pragma Assert (Result = 0 or else Result = ENOMEM);
334 if Result = ENOMEM then
338 if Locking_Policy = 'C' then
339 Result := pthread_mutexattr_setprotocol
340 (Attributes'Access, PTHREAD_PRIO_PROTECT);
341 pragma Assert (Result = 0);
343 Result := pthread_mutexattr_setprioceiling
344 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
345 pragma Assert (Result = 0);
347 elsif Locking_Policy = 'I' then
348 Result := pthread_mutexattr_setprotocol
349 (Attributes'Access, PTHREAD_PRIO_INHERIT);
350 pragma Assert (Result = 0);
353 Result := pthread_mutex_init (L, Attributes'Access);
354 pragma Assert (Result = 0 or else Result = ENOMEM);
356 if Result = ENOMEM then
357 Result := pthread_mutexattr_destroy (Attributes'Access);
361 Result := pthread_mutexattr_destroy (Attributes'Access);
362 pragma Assert (Result = 0);
369 procedure Finalize_Lock (L : not null access Lock) is
370 Result : Interfaces.C.int;
372 Result := pthread_mutex_destroy (L);
373 pragma Assert (Result = 0);
376 procedure Finalize_Lock (L : not null access RTS_Lock) is
377 Result : Interfaces.C.int;
379 Result := pthread_mutex_destroy (L);
380 pragma Assert (Result = 0);
388 (L : not null access Lock; Ceiling_Violation : out Boolean)
390 Result : Interfaces.C.int;
393 Result := pthread_mutex_lock (L);
395 -- Assume that the cause of EINVAL is a priority ceiling violation
397 Ceiling_Violation := (Result = EINVAL);
398 pragma Assert (Result = 0 or else Result = EINVAL);
402 (L : not null access RTS_Lock;
403 Global_Lock : Boolean := False)
405 Result : Interfaces.C.int;
407 if not Single_Lock or else Global_Lock then
408 Result := pthread_mutex_lock (L);
409 pragma Assert (Result = 0);
413 procedure Write_Lock (T : Task_Id) is
414 Result : Interfaces.C.int;
416 if not Single_Lock then
417 Result := pthread_mutex_lock (T.Common.LL.L'Access);
418 pragma Assert (Result = 0);
427 (L : not null access Lock; Ceiling_Violation : out Boolean) is
429 Write_Lock (L, Ceiling_Violation);
436 procedure Unlock (L : not null access Lock) is
437 Result : Interfaces.C.int;
439 Result := pthread_mutex_unlock (L);
440 pragma Assert (Result = 0);
444 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
446 Result : Interfaces.C.int;
448 if not Single_Lock or else Global_Lock then
449 Result := pthread_mutex_unlock (L);
450 pragma Assert (Result = 0);
454 procedure Unlock (T : Task_Id) is
455 Result : Interfaces.C.int;
457 if not Single_Lock then
458 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
459 pragma Assert (Result = 0);
467 -- Dynamic priority ceilings are not supported by the underlying system
469 procedure Set_Ceiling
470 (L : not null access Lock;
471 Prio : System.Any_Priority)
473 pragma Unreferenced (L, Prio);
484 Reason : System.Tasking.Task_States)
486 pragma Unreferenced (Reason);
488 Result : Interfaces.C.int;
494 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
498 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
501 -- EINTR is not considered a failure
503 pragma Assert (Result = 0 or else Result = EINTR);
510 -- This is for use within the run-time system, so abort is
511 -- assumed to be already deferred, and the caller should be
512 -- holding its own ATCB lock.
514 procedure Timed_Sleep
517 Mode : ST.Delay_Modes;
518 Reason : Task_States;
519 Timedout : out Boolean;
520 Yielded : out Boolean)
522 pragma Unreferenced (Reason);
524 Base_Time : constant Duration := Monotonic_Clock;
525 Check_Time : Duration := Base_Time;
528 Request : aliased timespec;
529 Result : Interfaces.C.int;
535 if Mode = Relative then
536 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
538 if Relative_Timed_Wait then
539 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
543 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
545 if Relative_Timed_Wait then
546 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
550 if Abs_Time > Check_Time then
551 if Relative_Timed_Wait then
552 Request := To_Timespec (Rel_Time);
554 Request := To_Timespec (Abs_Time);
558 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
562 pthread_cond_timedwait
563 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
568 pthread_cond_timedwait
569 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
573 Check_Time := Monotonic_Clock;
574 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
576 if Result = 0 or Result = EINTR then
578 -- Somebody may have called Wakeup for us
584 pragma Assert (Result = ETIMEDOUT);
593 -- This is for use in implementing delay statements, so we assume the
594 -- caller is abort-deferred but is holding no locks.
596 procedure Timed_Delay
599 Mode : ST.Delay_Modes)
601 Base_Time : constant Duration := Monotonic_Clock;
602 Check_Time : Duration := Base_Time;
605 Request : aliased timespec;
607 Result : Interfaces.C.int;
608 pragma Warnings (Off, Result);
615 Write_Lock (Self_ID);
617 if Mode = Relative then
618 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
620 if Relative_Timed_Wait then
621 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
625 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
627 if Relative_Timed_Wait then
628 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
632 if Abs_Time > Check_Time then
633 if Relative_Timed_Wait then
634 Request := To_Timespec (Rel_Time);
636 Request := To_Timespec (Abs_Time);
639 Self_ID.Common.State := Delay_Sleep;
642 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
645 Result := pthread_cond_timedwait
646 (Self_ID.Common.LL.CV'Access,
647 Single_RTS_Lock'Access,
650 Result := pthread_cond_timedwait
651 (Self_ID.Common.LL.CV'Access,
652 Self_ID.Common.LL.L'Access,
656 Check_Time := Monotonic_Clock;
657 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
659 pragma Assert (Result = 0
660 or else Result = ETIMEDOUT
661 or else Result = EINTR);
664 Self_ID.Common.State := Runnable;
673 Result := sched_yield;
676 ---------------------
677 -- Monotonic_Clock --
678 ---------------------
680 function Monotonic_Clock return Duration is
681 TS : aliased timespec;
682 Result : Interfaces.C.int;
684 Result := clock_gettime
685 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
686 pragma Assert (Result = 0);
687 return To_Duration (TS);
694 function RT_Resolution return Duration is
703 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
704 pragma Unreferenced (Reason);
705 Result : Interfaces.C.int;
707 Result := pthread_cond_signal (T.Common.LL.CV'Access);
708 pragma Assert (Result = 0);
715 procedure Yield (Do_Yield : Boolean := True) is
716 Result : Interfaces.C.int;
717 pragma Unreferenced (Result);
720 Result := sched_yield;
728 procedure Set_Priority
730 Prio : System.Any_Priority;
731 Loss_Of_Inheritance : Boolean := False)
733 pragma Unreferenced (Loss_Of_Inheritance);
735 Result : Interfaces.C.int;
736 Param : aliased struct_sched_param;
738 function Get_Policy (Prio : System.Any_Priority) return Character;
739 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
740 -- Get priority specific dispatching policy
742 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
743 -- Upper case first character of the policy name corresponding to the
744 -- task as set by a Priority_Specific_Dispatching pragma.
747 T.Common.Current_Priority := Prio;
748 Param.sched_priority := To_Target_Priority (Prio);
750 if Time_Slice_Supported
751 and then (Dispatching_Policy = 'R'
752 or else Priority_Specific_Policy = 'R'
753 or else Time_Slice_Val > 0)
755 Result := pthread_setschedparam
756 (T.Common.LL.Thread, SCHED_RR, Param'Access);
758 elsif Dispatching_Policy = 'F'
759 or else Priority_Specific_Policy = 'F'
760 or else Time_Slice_Val = 0
762 Result := pthread_setschedparam
763 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
766 Result := pthread_setschedparam
767 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
770 pragma Assert (Result = 0);
777 function Get_Priority (T : Task_Id) return System.Any_Priority is
779 return T.Common.Current_Priority;
786 procedure Enter_Task (Self_ID : Task_Id) is
788 Self_ID.Common.LL.Thread := pthread_self;
789 Self_ID.Common.LL.LWP := lwp_self;
791 Specific.Set (Self_ID);
795 for J in Known_Tasks'Range loop
796 if Known_Tasks (J) = null then
797 Known_Tasks (J) := Self_ID;
798 Self_ID.Known_Tasks_Index := J;
805 if Use_Alternate_Stack then
807 Stack : aliased stack_t;
808 Result : Interfaces.C.int;
810 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
811 Stack.ss_size := Alternate_Stack_Size;
813 Result := sigaltstack (Stack'Access, null);
814 pragma Assert (Result = 0);
823 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
825 return new Ada_Task_Control_Block (Entry_Num);
832 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
834 -----------------------------
835 -- Register_Foreign_Thread --
836 -----------------------------
838 function Register_Foreign_Thread return Task_Id is
840 if Is_Valid_Task then
843 return Register_Foreign_Thread (pthread_self);
845 end Register_Foreign_Thread;
851 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
852 Mutex_Attr : aliased pthread_mutexattr_t;
853 Result : Interfaces.C.int;
854 Cond_Attr : aliased pthread_condattr_t;
857 -- Give the task a unique serial number
859 Self_ID.Serial_Number := Next_Serial_Number;
860 Next_Serial_Number := Next_Serial_Number + 1;
861 pragma Assert (Next_Serial_Number /= 0);
863 if not Single_Lock then
864 Result := pthread_mutexattr_init (Mutex_Attr'Access);
865 pragma Assert (Result = 0 or else Result = ENOMEM);
868 if Locking_Policy = 'C' then
870 pthread_mutexattr_setprotocol
872 PTHREAD_PRIO_PROTECT);
873 pragma Assert (Result = 0);
876 pthread_mutexattr_setprioceiling
878 Interfaces.C.int (System.Any_Priority'Last));
879 pragma Assert (Result = 0);
881 elsif Locking_Policy = 'I' then
883 pthread_mutexattr_setprotocol
885 PTHREAD_PRIO_INHERIT);
886 pragma Assert (Result = 0);
891 (Self_ID.Common.LL.L'Access,
893 pragma Assert (Result = 0 or else Result = ENOMEM);
901 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
902 pragma Assert (Result = 0);
905 Result := pthread_condattr_init (Cond_Attr'Access);
906 pragma Assert (Result = 0 or else Result = ENOMEM);
911 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
912 pragma Assert (Result = 0 or else Result = ENOMEM);
918 if not Single_Lock then
919 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
920 pragma Assert (Result = 0);
926 Result := pthread_condattr_destroy (Cond_Attr'Access);
927 pragma Assert (Result = 0);
934 procedure Create_Task
936 Wrapper : System.Address;
937 Stack_Size : System.Parameters.Size_Type;
938 Priority : System.Any_Priority;
939 Succeeded : out Boolean)
941 Attributes : aliased pthread_attr_t;
942 Adjusted_Stack_Size : Interfaces.C.size_t;
943 Result : Interfaces.C.int;
945 function Thread_Body_Access is new
946 Ada.Unchecked_Conversion (System.Address, Thread_Body);
948 use System.Task_Info;
951 Adjusted_Stack_Size :=
952 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
954 if Stack_Base_Available then
956 -- If Stack Checking is supported then allocate 2 additional pages:
958 -- In the worst case, stack is allocated at something like
959 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
960 -- to be sure the effective stack size is greater than what
963 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
966 Result := pthread_attr_init (Attributes'Access);
967 pragma Assert (Result = 0 or else Result = ENOMEM);
975 pthread_attr_setdetachstate
976 (Attributes'Access, PTHREAD_CREATE_DETACHED);
977 pragma Assert (Result = 0);
980 pthread_attr_setstacksize
981 (Attributes'Access, Adjusted_Stack_Size);
982 pragma Assert (Result = 0);
984 if T.Common.Task_Info /= Default_Scope then
985 case T.Common.Task_Info is
986 when System.Task_Info.Process_Scope =>
988 pthread_attr_setscope
989 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
991 when System.Task_Info.System_Scope =>
993 pthread_attr_setscope
994 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
996 when System.Task_Info.Default_Scope =>
1000 pragma Assert (Result = 0);
1003 -- Since the initial signal mask of a thread is inherited from the
1004 -- creator, and the Environment task has all its signals masked, we
1005 -- do not need to manipulate caller's signal mask at this point.
1006 -- All tasks in RTS will have All_Tasks_Mask initially.
1008 Result := pthread_create
1009 (T.Common.LL.Thread'Access,
1011 Thread_Body_Access (Wrapper),
1013 pragma Assert (Result = 0 or else Result = EAGAIN);
1015 Succeeded := Result = 0;
1017 Result := pthread_attr_destroy (Attributes'Access);
1018 pragma Assert (Result = 0);
1021 Set_Priority (T, Priority);
1029 procedure Finalize_TCB (T : Task_Id) is
1030 Result : Interfaces.C.int;
1032 Is_Self : constant Boolean := T = Self;
1034 procedure Free is new
1035 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1038 if not Single_Lock then
1039 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1040 pragma Assert (Result = 0);
1043 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1044 pragma Assert (Result = 0);
1046 if T.Known_Tasks_Index /= -1 then
1047 Known_Tasks (T.Known_Tasks_Index) := null;
1053 Specific.Set (null);
1061 procedure Exit_Task is
1063 -- Mark this task as unknown, so that if Self is called, it won't
1064 -- return a dangling pointer.
1066 Specific.Set (null);
1073 procedure Abort_Task (T : Task_Id) is
1074 Result : Interfaces.C.int;
1078 (T.Common.LL.Thread,
1079 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1080 pragma Assert (Result = 0);
1087 procedure Initialize (S : in out Suspension_Object) is
1088 Mutex_Attr : aliased pthread_mutexattr_t;
1089 Cond_Attr : aliased pthread_condattr_t;
1090 Result : Interfaces.C.int;
1093 -- Initialize internal state (always to False (RM D.10 (6)))
1098 -- Initialize internal mutex
1100 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1101 pragma Assert (Result = 0 or else Result = ENOMEM);
1103 if Result = ENOMEM then
1104 raise Storage_Error;
1107 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1108 pragma Assert (Result = 0 or else Result = ENOMEM);
1110 if Result = ENOMEM then
1111 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1112 pragma Assert (Result = 0);
1114 raise Storage_Error;
1117 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1118 pragma Assert (Result = 0);
1120 -- Initialize internal condition variable
1122 Result := pthread_condattr_init (Cond_Attr'Access);
1123 pragma Assert (Result = 0 or else Result = ENOMEM);
1126 Result := pthread_mutex_destroy (S.L'Access);
1127 pragma Assert (Result = 0);
1129 if Result = ENOMEM then
1130 raise Storage_Error;
1134 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1135 pragma Assert (Result = 0 or else Result = ENOMEM);
1138 Result := pthread_mutex_destroy (S.L'Access);
1139 pragma Assert (Result = 0);
1141 if Result = ENOMEM then
1142 Result := pthread_condattr_destroy (Cond_Attr'Access);
1143 pragma Assert (Result = 0);
1144 raise Storage_Error;
1148 Result := pthread_condattr_destroy (Cond_Attr'Access);
1149 pragma Assert (Result = 0);
1156 procedure Finalize (S : in out Suspension_Object) is
1157 Result : Interfaces.C.int;
1160 -- Destroy internal mutex
1162 Result := pthread_mutex_destroy (S.L'Access);
1163 pragma Assert (Result = 0);
1165 -- Destroy internal condition variable
1167 Result := pthread_cond_destroy (S.CV'Access);
1168 pragma Assert (Result = 0);
1175 function Current_State (S : Suspension_Object) return Boolean is
1177 -- We do not want to use lock on this read operation. State is marked
1178 -- as Atomic so that we ensure that the value retrieved is correct.
1187 procedure Set_False (S : in out Suspension_Object) is
1188 Result : Interfaces.C.int;
1191 SSL.Abort_Defer.all;
1193 Result := pthread_mutex_lock (S.L'Access);
1194 pragma Assert (Result = 0);
1198 Result := pthread_mutex_unlock (S.L'Access);
1199 pragma Assert (Result = 0);
1201 SSL.Abort_Undefer.all;
1208 procedure Set_True (S : in out Suspension_Object) is
1209 Result : Interfaces.C.int;
1212 SSL.Abort_Defer.all;
1214 Result := pthread_mutex_lock (S.L'Access);
1215 pragma Assert (Result = 0);
1217 -- If there is already a task waiting on this suspension object then
1218 -- we resume it, leaving the state of the suspension object to False,
1219 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1220 -- the state to True.
1226 Result := pthread_cond_signal (S.CV'Access);
1227 pragma Assert (Result = 0);
1233 Result := pthread_mutex_unlock (S.L'Access);
1234 pragma Assert (Result = 0);
1236 SSL.Abort_Undefer.all;
1239 ------------------------
1240 -- Suspend_Until_True --
1241 ------------------------
1243 procedure Suspend_Until_True (S : in out Suspension_Object) is
1244 Result : Interfaces.C.int;
1247 SSL.Abort_Defer.all;
1249 Result := pthread_mutex_lock (S.L'Access);
1250 pragma Assert (Result = 0);
1254 -- Program_Error must be raised upon calling Suspend_Until_True
1255 -- if another task is already waiting on that suspension object
1258 Result := pthread_mutex_unlock (S.L'Access);
1259 pragma Assert (Result = 0);
1261 SSL.Abort_Undefer.all;
1263 raise Program_Error;
1266 -- Suspend the task if the state is False. Otherwise, the task
1267 -- continues its execution, and the state of the suspension object
1268 -- is set to False (ARM D.10 par. 9).
1274 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1277 Result := pthread_mutex_unlock (S.L'Access);
1278 pragma Assert (Result = 0);
1280 SSL.Abort_Undefer.all;
1282 end Suspend_Until_True;
1290 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1291 pragma Unreferenced (Self_ID);
1296 --------------------
1297 -- Check_No_Locks --
1298 --------------------
1300 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1301 pragma Unreferenced (Self_ID);
1306 ----------------------
1307 -- Environment_Task --
1308 ----------------------
1310 function Environment_Task return Task_Id is
1312 return Environment_Task_Id;
1313 end Environment_Task;
1319 procedure Lock_RTS is
1321 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1328 procedure Unlock_RTS is
1330 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1337 function Suspend_Task
1339 Thread_Self : Thread_Id) return Boolean
1341 pragma Unreferenced (T, Thread_Self);
1350 function Resume_Task
1352 Thread_Self : Thread_Id) return Boolean
1354 pragma Unreferenced (T, Thread_Self);
1359 --------------------
1360 -- Stop_All_Tasks --
1361 --------------------
1363 procedure Stop_All_Tasks is
1372 function Stop_Task (T : ST.Task_Id) return Boolean is
1373 pragma Unreferenced (T);
1382 function Continue_Task (T : ST.Task_Id) return Boolean is
1383 pragma Unreferenced (T);
1392 procedure Initialize (Environment_Task : Task_Id) is
1393 act : aliased struct_sigaction;
1394 old_act : aliased struct_sigaction;
1395 Tmp_Set : aliased sigset_t;
1396 Result : Interfaces.C.int;
1399 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1400 pragma Import (C, State, "__gnat_get_interrupt_state");
1401 -- Get interrupt state. Defined in a-init.c
1402 -- The input argument is the interrupt number,
1403 -- and the result is one of the following:
1405 Default : constant Character := 's';
1406 -- 'n' this interrupt not set by any Interrupt_State pragma
1407 -- 'u' Interrupt_State pragma set state to User
1408 -- 'r' Interrupt_State pragma set state to Runtime
1409 -- 's' Interrupt_State pragma set state to System (use "default"
1413 Environment_Task_Id := Environment_Task;
1415 Interrupt_Management.Initialize;
1417 -- Prepare the set of signals that should unblocked in all tasks
1419 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1420 pragma Assert (Result = 0);
1422 for J in Interrupt_Management.Interrupt_ID loop
1423 if System.Interrupt_Management.Keep_Unmasked (J) then
1424 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1425 pragma Assert (Result = 0);
1429 -- Initialize the lock used to synchronize chain of all ATCBs
1431 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1433 Specific.Initialize (Environment_Task);
1435 if Use_Alternate_Stack then
1436 Environment_Task.Common.Task_Alternate_Stack :=
1437 Alternate_Stack'Address;
1440 Enter_Task (Environment_Task);
1442 -- Install the abort-signal handler
1445 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1448 act.sa_handler := Abort_Handler'Address;
1450 Result := sigemptyset (Tmp_Set'Access);
1451 pragma Assert (Result = 0);
1452 act.sa_mask := Tmp_Set;
1456 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1457 act'Unchecked_Access,
1458 old_act'Unchecked_Access);
1459 pragma Assert (Result = 0);
1463 end System.Task_Primitives.Operations;