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 POSIX-like version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 -- Note: this file can only be used for POSIX compliant systems that
40 -- implement 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
47 -- tasking operations. It causes infinite loops and other problems.
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
60 with System.Task_Info;
61 -- used for Task_Info_Type
67 with System.Soft_Links;
68 -- used for Abort_Defer/Undefer
70 -- We use System.Soft_Links instead of System.Tasking.Initialization
71 -- because the later is a higher level package that we shouldn't depend on.
72 -- For example when using the restricted run time, it is replaced by
73 -- System.Tasking.Restricted.Stages.
75 with Unchecked_Conversion;
76 with Unchecked_Deallocation;
78 package body System.Task_Primitives.Operations is
80 package SSL renames System.Soft_Links;
82 use System.Tasking.Debug;
85 use System.OS_Interface;
86 use System.Parameters;
87 use System.OS_Primitives;
93 -- The followings are logically constants, but need to be initialized
96 Single_RTS_Lock : aliased RTS_Lock;
97 -- This is a lock to allow only one thread of control in the RTS at
98 -- a time; it is used to execute in mutual exclusion from all other tasks.
99 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
101 ATCB_Key : aliased pthread_key_t;
102 -- Key used to find the Ada Task_Id associated with a thread
104 Environment_Task_Id : Task_Id;
105 -- A variable to hold Task_Id for the environment task.
107 Locking_Policy : Character;
108 pragma Import (C, Locking_Policy, "__gl_locking_policy");
109 -- Value of the pragma Locking_Policy:
110 -- 'C' for Ceiling_Locking
111 -- 'I' for Inherit_Locking
114 Unblocked_Signal_Mask : aliased sigset_t;
115 -- The set of signals that should unblocked in all tasks
117 -- The followings are internal configuration constants needed.
119 Next_Serial_Number : Task_Serial_Number := 100;
120 -- We start at 100, to reserve some special values for
121 -- using in error checking.
123 Time_Slice_Val : Integer;
124 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
126 Dispatching_Policy : Character;
127 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
129 Foreign_Task_Elaborated : aliased Boolean := True;
130 -- Used to identified fake tasks (i.e., non-Ada Threads).
138 procedure Initialize (Environment_Task : Task_Id);
139 pragma Inline (Initialize);
140 -- Initialize various data needed by this package.
142 function Is_Valid_Task return Boolean;
143 pragma Inline (Is_Valid_Task);
144 -- Does executing thread have a TCB?
146 procedure Set (Self_Id : Task_Id);
148 -- Set the self id for the current task.
150 function Self return Task_Id;
151 pragma Inline (Self);
152 -- Return a pointer to the Ada Task Control Block of the calling task.
156 package body Specific is separate;
157 -- The body of this package is target specific.
159 ---------------------------------
160 -- Support for foreign threads --
161 ---------------------------------
163 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
164 -- Allocate and Initialize a new ATCB for the current Thread.
166 function Register_Foreign_Thread
167 (Thread : Thread_Id) return Task_Id is separate;
169 -----------------------
170 -- Local Subprograms --
171 -----------------------
173 procedure Abort_Handler (Sig : Signal);
174 -- Signal handler used to implement asynchronous abort.
175 -- See also comment before body, below.
177 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
183 -- Target-dependent binding of inter-thread Abort signal to
184 -- the raising of the Abort_Signal exception.
186 -- The technical issues and alternatives here are essentially
187 -- the same as for raising exceptions in response to other
188 -- signals (e.g. Storage_Error). See code and comments in
189 -- the package body System.Interrupt_Management.
191 -- Some implementations may not allow an exception to be propagated
192 -- out of a handler, and others might leave the signal or
193 -- interrupt that invoked this handler masked after the exceptional
194 -- return to the application code.
196 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
197 -- On most UNIX systems, this will allow transfer out of a signal handler,
198 -- which is usually the only mechanism available for implementing
199 -- asynchronous handlers of this kind. However, some
200 -- systems do not restore the signal mask on longjmp(), leaving the
201 -- abort signal masked.
203 procedure Abort_Handler (Sig : Signal) is
204 pragma Warnings (Off, Sig);
206 T : constant Task_Id := Self;
207 Result : Interfaces.C.int;
208 Old_Set : aliased sigset_t;
211 -- It is not safe to raise an exception when using ZCX and the GCC
212 -- exception handling mechanism.
214 if ZCX_By_Default and then GCC_ZCX_Support then
218 if T.Deferral_Level = 0
219 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
224 -- Make sure signals used for RTS internal purpose are unmasked
226 Result := pthread_sigmask (SIG_UNBLOCK,
227 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
228 pragma Assert (Result = 0);
230 raise Standard'Abort_Signal;
238 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
239 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
240 Guard_Page_Address : Address;
242 Res : Interfaces.C.int;
245 if Stack_Base_Available then
247 -- Compute the guard page address
249 Guard_Page_Address :=
250 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
253 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
255 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
258 pragma Assert (Res = 0);
266 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
268 return T.Common.LL.Thread;
275 function Self return Task_Id renames Specific.Self;
277 ---------------------
278 -- Initialize_Lock --
279 ---------------------
281 -- Note: mutexes and cond_variables needed per-task basis are
282 -- initialized in Intialize_TCB and the Storage_Error is
283 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
284 -- used in RTS is initialized before any status change of RTS.
285 -- Therefore rasing Storage_Error in the following routines
286 -- should be able to be handled safely.
288 procedure Initialize_Lock
289 (Prio : System.Any_Priority;
292 Attributes : aliased pthread_mutexattr_t;
293 Result : Interfaces.C.int;
296 Result := pthread_mutexattr_init (Attributes'Access);
297 pragma Assert (Result = 0 or else Result = ENOMEM);
299 if Result = ENOMEM then
303 if Locking_Policy = 'C' then
304 Result := pthread_mutexattr_setprotocol
305 (Attributes'Access, PTHREAD_PRIO_PROTECT);
306 pragma Assert (Result = 0);
308 Result := pthread_mutexattr_setprioceiling
309 (Attributes'Access, Interfaces.C.int (Prio));
310 pragma Assert (Result = 0);
312 elsif Locking_Policy = 'I' then
313 Result := pthread_mutexattr_setprotocol
314 (Attributes'Access, PTHREAD_PRIO_INHERIT);
315 pragma Assert (Result = 0);
318 Result := pthread_mutex_init (L, Attributes'Access);
319 pragma Assert (Result = 0 or else Result = ENOMEM);
321 if Result = ENOMEM then
322 Result := pthread_mutexattr_destroy (Attributes'Access);
326 Result := pthread_mutexattr_destroy (Attributes'Access);
327 pragma Assert (Result = 0);
330 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
331 pragma Warnings (Off, Level);
333 Attributes : aliased pthread_mutexattr_t;
334 Result : Interfaces.C.int;
337 Result := pthread_mutexattr_init (Attributes'Access);
338 pragma Assert (Result = 0 or else Result = ENOMEM);
340 if Result = ENOMEM then
344 if Locking_Policy = 'C' then
345 Result := pthread_mutexattr_setprotocol
346 (Attributes'Access, PTHREAD_PRIO_PROTECT);
347 pragma Assert (Result = 0);
349 Result := pthread_mutexattr_setprioceiling
350 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
351 pragma Assert (Result = 0);
353 elsif Locking_Policy = 'I' then
354 Result := pthread_mutexattr_setprotocol
355 (Attributes'Access, PTHREAD_PRIO_INHERIT);
356 pragma Assert (Result = 0);
359 Result := pthread_mutex_init (L, Attributes'Access);
360 pragma Assert (Result = 0 or else Result = ENOMEM);
362 if Result = ENOMEM then
363 Result := pthread_mutexattr_destroy (Attributes'Access);
367 Result := pthread_mutexattr_destroy (Attributes'Access);
368 pragma Assert (Result = 0);
375 procedure Finalize_Lock (L : access Lock) is
376 Result : Interfaces.C.int;
379 Result := pthread_mutex_destroy (L);
380 pragma Assert (Result = 0);
383 procedure Finalize_Lock (L : access RTS_Lock) is
384 Result : Interfaces.C.int;
387 Result := pthread_mutex_destroy (L);
388 pragma Assert (Result = 0);
395 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
396 Result : Interfaces.C.int;
399 Result := pthread_mutex_lock (L);
401 -- Assume that the cause of EINVAL is a priority ceiling violation
403 Ceiling_Violation := (Result = EINVAL);
404 pragma Assert (Result = 0 or else Result = EINVAL);
408 (L : access RTS_Lock;
409 Global_Lock : Boolean := False)
411 Result : Interfaces.C.int;
414 if not Single_Lock or else Global_Lock then
415 Result := pthread_mutex_lock (L);
416 pragma Assert (Result = 0);
420 procedure Write_Lock (T : Task_Id) is
421 Result : Interfaces.C.int;
424 if not Single_Lock then
425 Result := pthread_mutex_lock (T.Common.LL.L'Access);
426 pragma Assert (Result = 0);
434 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
436 Write_Lock (L, Ceiling_Violation);
443 procedure Unlock (L : access Lock) is
444 Result : Interfaces.C.int;
447 Result := pthread_mutex_unlock (L);
448 pragma Assert (Result = 0);
451 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
452 Result : Interfaces.C.int;
455 if not Single_Lock or else Global_Lock then
456 Result := pthread_mutex_unlock (L);
457 pragma Assert (Result = 0);
461 procedure Unlock (T : Task_Id) is
462 Result : Interfaces.C.int;
465 if not Single_Lock then
466 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
467 pragma Assert (Result = 0);
477 Reason : System.Tasking.Task_States)
479 pragma Warnings (Off, Reason);
481 Result : Interfaces.C.int;
485 Result := pthread_cond_wait
486 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
488 Result := pthread_cond_wait
489 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
492 -- EINTR is not considered a failure.
494 pragma Assert (Result = 0 or else Result = EINTR);
501 -- This is for use within the run-time system, so abort is
502 -- assumed to be already deferred, and the caller should be
503 -- holding its own ATCB lock.
505 procedure Timed_Sleep
508 Mode : ST.Delay_Modes;
509 Reason : Task_States;
510 Timedout : out Boolean;
511 Yielded : out Boolean)
513 pragma Warnings (Off, Reason);
515 Check_Time : constant Duration := Monotonic_Clock;
518 Request : aliased timespec;
519 Result : Interfaces.C.int;
525 if Mode = Relative then
526 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
528 if Relative_Timed_Wait then
529 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
533 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
535 if Relative_Timed_Wait then
536 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
540 if Abs_Time > Check_Time then
541 if Relative_Timed_Wait then
542 Request := To_Timespec (Rel_Time);
544 Request := To_Timespec (Abs_Time);
548 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
549 or else Self_ID.Pending_Priority_Change;
552 Result := pthread_cond_timedwait
553 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
557 Result := pthread_cond_timedwait
558 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
562 exit when Abs_Time <= Monotonic_Clock;
564 if Result = 0 or Result = EINTR then
566 -- Somebody may have called Wakeup for us
572 pragma Assert (Result = ETIMEDOUT);
581 -- This is for use in implementing delay statements, so
582 -- we assume the caller is abort-deferred but is holding
585 procedure Timed_Delay
588 Mode : ST.Delay_Modes)
590 Check_Time : constant Duration := Monotonic_Clock;
593 Request : aliased timespec;
594 Result : Interfaces.C.int;
601 Write_Lock (Self_ID);
603 if Mode = Relative then
604 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
606 if Relative_Timed_Wait then
607 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
611 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
613 if Relative_Timed_Wait then
614 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
618 if Abs_Time > Check_Time then
619 if Relative_Timed_Wait then
620 Request := To_Timespec (Rel_Time);
622 Request := To_Timespec (Abs_Time);
625 Self_ID.Common.State := Delay_Sleep;
628 if Self_ID.Pending_Priority_Change then
629 Self_ID.Pending_Priority_Change := False;
630 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
631 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
634 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
637 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
638 Single_RTS_Lock'Access, Request'Access);
640 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
641 Self_ID.Common.LL.L'Access, Request'Access);
644 exit when Abs_Time <= Monotonic_Clock;
646 pragma Assert (Result = 0
647 or else Result = ETIMEDOUT
648 or else Result = EINTR);
651 Self_ID.Common.State := Runnable;
660 Result := sched_yield;
663 ---------------------
664 -- Monotonic_Clock --
665 ---------------------
667 function Monotonic_Clock return Duration is
668 TS : aliased timespec;
669 Result : Interfaces.C.int;
671 Result := clock_gettime
672 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
673 pragma Assert (Result = 0);
674 return To_Duration (TS);
681 function RT_Resolution return Duration is
690 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
691 pragma Warnings (Off, Reason);
692 Result : Interfaces.C.int;
694 Result := pthread_cond_signal (T.Common.LL.CV'Access);
695 pragma Assert (Result = 0);
702 procedure Yield (Do_Yield : Boolean := True) is
703 Result : Interfaces.C.int;
704 pragma Unreferenced (Result);
707 Result := sched_yield;
715 procedure Set_Priority
717 Prio : System.Any_Priority;
718 Loss_Of_Inheritance : Boolean := False)
720 pragma Warnings (Off, Loss_Of_Inheritance);
722 Result : Interfaces.C.int;
723 Param : aliased struct_sched_param;
726 T.Common.Current_Priority := Prio;
727 Param.sched_priority := Interfaces.C.int (Prio);
729 if Time_Slice_Supported and then Time_Slice_Val > 0 then
730 Result := pthread_setschedparam
731 (T.Common.LL.Thread, SCHED_RR, Param'Access);
733 elsif Dispatching_Policy = 'F' or else Time_Slice_Val = 0 then
734 Result := pthread_setschedparam
735 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
738 Result := pthread_setschedparam
739 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
742 pragma Assert (Result = 0);
749 function Get_Priority (T : Task_Id) return System.Any_Priority is
751 return T.Common.Current_Priority;
758 procedure Enter_Task (Self_ID : Task_Id) is
760 Self_ID.Common.LL.Thread := pthread_self;
761 Self_ID.Common.LL.LWP := lwp_self;
763 Specific.Set (Self_ID);
767 for J in Known_Tasks'Range loop
768 if Known_Tasks (J) = null then
769 Known_Tasks (J) := Self_ID;
770 Self_ID.Known_Tasks_Index := J;
782 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
784 return new Ada_Task_Control_Block (Entry_Num);
791 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
793 -----------------------------
794 -- Register_Foreign_Thread --
795 -----------------------------
797 function Register_Foreign_Thread return Task_Id is
799 if Is_Valid_Task then
802 return Register_Foreign_Thread (pthread_self);
804 end Register_Foreign_Thread;
810 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
811 Mutex_Attr : aliased pthread_mutexattr_t;
812 Result : Interfaces.C.int;
813 Cond_Attr : aliased pthread_condattr_t;
816 -- Give the task a unique serial number.
818 Self_ID.Serial_Number := Next_Serial_Number;
819 Next_Serial_Number := Next_Serial_Number + 1;
820 pragma Assert (Next_Serial_Number /= 0);
822 if not Single_Lock then
823 Result := pthread_mutexattr_init (Mutex_Attr'Access);
824 pragma Assert (Result = 0 or else Result = ENOMEM);
827 if Locking_Policy = 'C' then
828 Result := pthread_mutexattr_setprotocol
829 (Mutex_Attr'Access, PTHREAD_PRIO_PROTECT);
830 pragma Assert (Result = 0);
832 Result := pthread_mutexattr_setprioceiling
834 Interfaces.C.int (System.Any_Priority'Last));
835 pragma Assert (Result = 0);
837 elsif Locking_Policy = 'I' then
838 Result := pthread_mutexattr_setprotocol
839 (Mutex_Attr'Access, PTHREAD_PRIO_INHERIT);
840 pragma Assert (Result = 0);
843 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
845 pragma Assert (Result = 0 or else Result = ENOMEM);
853 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
854 pragma Assert (Result = 0);
857 Result := pthread_condattr_init (Cond_Attr'Access);
858 pragma Assert (Result = 0 or else Result = ENOMEM);
861 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
863 pragma Assert (Result = 0 or else Result = ENOMEM);
869 if not Single_Lock then
870 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
871 pragma Assert (Result = 0);
877 Result := pthread_condattr_destroy (Cond_Attr'Access);
878 pragma Assert (Result = 0);
885 procedure Create_Task
887 Wrapper : System.Address;
888 Stack_Size : System.Parameters.Size_Type;
889 Priority : System.Any_Priority;
890 Succeeded : out Boolean)
892 Attributes : aliased pthread_attr_t;
893 Adjusted_Stack_Size : Interfaces.C.size_t;
894 Result : Interfaces.C.int;
896 function Thread_Body_Access is new
897 Unchecked_Conversion (System.Address, Thread_Body);
899 use System.Task_Info;
902 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
904 if Stack_Base_Available then
905 -- If Stack Checking is supported then allocate 2 additional pages:
907 -- In the worst case, stack is allocated at something like
908 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
909 -- to be sure the effective stack size is greater than what
912 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
915 Result := pthread_attr_init (Attributes'Access);
916 pragma Assert (Result = 0 or else Result = ENOMEM);
923 Result := pthread_attr_setdetachstate
924 (Attributes'Access, PTHREAD_CREATE_DETACHED);
925 pragma Assert (Result = 0);
927 Result := pthread_attr_setstacksize
928 (Attributes'Access, Adjusted_Stack_Size);
929 pragma Assert (Result = 0);
931 if T.Common.Task_Info /= Default_Scope then
933 -- We are assuming that Scope_Type has the same values than the
934 -- corresponding C macros
936 Result := pthread_attr_setscope
937 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
938 pragma Assert (Result = 0);
941 -- Since the initial signal mask of a thread is inherited from the
942 -- creator, and the Environment task has all its signals masked, we
943 -- do not need to manipulate caller's signal mask at this point.
944 -- All tasks in RTS will have All_Tasks_Mask initially.
946 Result := pthread_create
947 (T.Common.LL.Thread'Access,
949 Thread_Body_Access (Wrapper),
951 pragma Assert (Result = 0 or else Result = EAGAIN);
953 Succeeded := Result = 0;
955 Result := pthread_attr_destroy (Attributes'Access);
956 pragma Assert (Result = 0);
958 Set_Priority (T, Priority);
965 procedure Finalize_TCB (T : Task_Id) is
966 Result : Interfaces.C.int;
968 Is_Self : constant Boolean := T = Self;
970 procedure Free is new
971 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
974 if not Single_Lock then
975 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
976 pragma Assert (Result = 0);
979 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
980 pragma Assert (Result = 0);
982 if T.Known_Tasks_Index /= -1 then
983 Known_Tasks (T.Known_Tasks_Index) := null;
997 procedure Exit_Task is
999 -- Mark this task as unknown, so that if Self is called, it won't
1000 -- return a dangling pointer.
1002 Specific.Set (null);
1009 procedure Abort_Task (T : Task_Id) is
1010 Result : Interfaces.C.int;
1012 Result := pthread_kill (T.Common.LL.Thread,
1013 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1014 pragma Assert (Result = 0);
1021 procedure Initialize (S : in out Suspension_Object) is
1022 Mutex_Attr : aliased pthread_mutexattr_t;
1023 Cond_Attr : aliased pthread_condattr_t;
1024 Result : Interfaces.C.int;
1026 -- Initialize internal state. It is always initialized to False (ARM
1032 -- Initialize internal mutex
1034 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1035 pragma Assert (Result = 0 or else Result = ENOMEM);
1037 if Result = ENOMEM then
1038 raise Storage_Error;
1041 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1042 pragma Assert (Result = 0 or else Result = ENOMEM);
1044 if Result = ENOMEM then
1045 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1046 pragma Assert (Result = 0);
1048 raise Storage_Error;
1051 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1052 pragma Assert (Result = 0);
1054 -- Initialize internal condition variable
1056 Result := pthread_condattr_init (Cond_Attr'Access);
1057 pragma Assert (Result = 0 or else Result = ENOMEM);
1060 Result := pthread_mutex_destroy (S.L'Access);
1061 pragma Assert (Result = 0);
1063 if Result = ENOMEM then
1064 raise Storage_Error;
1068 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1069 pragma Assert (Result = 0 or else Result = ENOMEM);
1072 Result := pthread_mutex_destroy (S.L'Access);
1073 pragma Assert (Result = 0);
1075 if Result = ENOMEM then
1076 Result := pthread_condattr_destroy (Cond_Attr'Access);
1077 pragma Assert (Result = 0);
1079 raise Storage_Error;
1083 Result := pthread_condattr_destroy (Cond_Attr'Access);
1084 pragma Assert (Result = 0);
1091 procedure Finalize (S : in out Suspension_Object) is
1092 Result : Interfaces.C.int;
1094 -- Destroy internal mutex
1096 Result := pthread_mutex_destroy (S.L'Access);
1097 pragma Assert (Result = 0);
1099 -- Destroy internal condition variable
1101 Result := pthread_cond_destroy (S.CV'Access);
1102 pragma Assert (Result = 0);
1109 function Current_State (S : Suspension_Object) return Boolean is
1111 -- We do not want to use lock on this read operation. State is marked
1112 -- as Atomic so that we ensure that the value retrieved is correct.
1121 procedure Set_False (S : in out Suspension_Object) is
1122 Result : Interfaces.C.int;
1124 SSL.Abort_Defer.all;
1126 Result := pthread_mutex_lock (S.L'Access);
1127 pragma Assert (Result = 0);
1131 Result := pthread_mutex_unlock (S.L'Access);
1132 pragma Assert (Result = 0);
1134 SSL.Abort_Undefer.all;
1141 procedure Set_True (S : in out Suspension_Object) is
1142 Result : Interfaces.C.int;
1144 SSL.Abort_Defer.all;
1146 Result := pthread_mutex_lock (S.L'Access);
1147 pragma Assert (Result = 0);
1149 -- If there is already a task waiting on this suspension object then
1150 -- we resume it, leaving the state of the suspension object to False,
1151 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1152 -- the state to True.
1158 Result := pthread_cond_signal (S.CV'Access);
1159 pragma Assert (Result = 0);
1164 Result := pthread_mutex_unlock (S.L'Access);
1165 pragma Assert (Result = 0);
1167 SSL.Abort_Undefer.all;
1170 ------------------------
1171 -- Suspend_Until_True --
1172 ------------------------
1174 procedure Suspend_Until_True (S : in out Suspension_Object) is
1175 Result : Interfaces.C.int;
1177 SSL.Abort_Defer.all;
1179 Result := pthread_mutex_lock (S.L'Access);
1180 pragma Assert (Result = 0);
1183 -- Program_Error must be raised upon calling Suspend_Until_True
1184 -- if another task is already waiting on that suspension object
1185 -- (ARM D.10 par. 10).
1187 Result := pthread_mutex_unlock (S.L'Access);
1188 pragma Assert (Result = 0);
1190 SSL.Abort_Undefer.all;
1192 raise Program_Error;
1194 -- Suspend the task if the state is False. Otherwise, the task
1195 -- continues its execution, and the state of the suspension object
1196 -- is set to False (ARM D.10 par. 9).
1202 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1205 Result := pthread_mutex_unlock (S.L'Access);
1206 pragma Assert (Result = 0);
1208 SSL.Abort_Undefer.all;
1210 end Suspend_Until_True;
1218 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1219 pragma Warnings (Off, Self_ID);
1224 --------------------
1225 -- Check_No_Locks --
1226 --------------------
1228 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1229 pragma Warnings (Off, Self_ID);
1234 ----------------------
1235 -- Environment_Task --
1236 ----------------------
1238 function Environment_Task return Task_Id is
1240 return Environment_Task_Id;
1241 end Environment_Task;
1247 procedure Lock_RTS is
1249 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1256 procedure Unlock_RTS is
1258 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1265 function Suspend_Task
1267 Thread_Self : Thread_Id) return Boolean
1269 pragma Warnings (Off, T);
1270 pragma Warnings (Off, Thread_Self);
1279 function Resume_Task
1281 Thread_Self : Thread_Id) return Boolean
1283 pragma Warnings (Off, T);
1284 pragma Warnings (Off, Thread_Self);
1293 procedure Initialize (Environment_Task : Task_Id) is
1294 act : aliased struct_sigaction;
1295 old_act : aliased struct_sigaction;
1296 Tmp_Set : aliased sigset_t;
1297 Result : Interfaces.C.int;
1300 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1301 pragma Import (C, State, "__gnat_get_interrupt_state");
1302 -- Get interrupt state. Defined in a-init.c
1303 -- The input argument is the interrupt number,
1304 -- and the result is one of the following:
1306 Default : constant Character := 's';
1307 -- 'n' this interrupt not set by any Interrupt_State pragma
1308 -- 'u' Interrupt_State pragma set state to User
1309 -- 'r' Interrupt_State pragma set state to Runtime
1310 -- 's' Interrupt_State pragma set state to System (use "default"
1314 Environment_Task_Id := Environment_Task;
1316 Interrupt_Management.Initialize;
1318 -- Prepare the set of signals that should unblocked in all tasks
1320 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1321 pragma Assert (Result = 0);
1323 for J in Interrupt_Management.Interrupt_ID loop
1324 if System.Interrupt_Management.Keep_Unmasked (J) then
1325 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1326 pragma Assert (Result = 0);
1330 -- Initialize the lock used to synchronize chain of all ATCBs.
1332 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1334 Specific.Initialize (Environment_Task);
1336 Enter_Task (Environment_Task);
1338 -- Install the abort-signal handler
1340 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1344 act.sa_handler := Abort_Handler'Address;
1346 Result := sigemptyset (Tmp_Set'Access);
1347 pragma Assert (Result = 0);
1348 act.sa_mask := Tmp_Set;
1352 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1353 act'Unchecked_Access,
1354 old_act'Unchecked_Access);
1355 pragma Assert (Result = 0);
1359 end System.Task_Primitives.Operations;