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;
80 -- The followings are logically constants, but need to be initialized
83 Single_RTS_Lock : aliased RTS_Lock;
84 -- This is a lock to allow only one thread of control in the RTS at
85 -- a time; it is used to execute in mutual exclusion from all other tasks.
86 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
88 ATCB_Key : aliased pthread_key_t;
89 -- Key used to find the Ada Task_Id associated with a thread
91 Environment_Task_Id : Task_Id;
92 -- A variable to hold Task_Id for the environment task
94 Locking_Policy : Character;
95 pragma Import (C, Locking_Policy, "__gl_locking_policy");
96 -- Value of the pragma Locking_Policy:
97 -- 'C' for Ceiling_Locking
98 -- 'I' for Inherit_Locking
101 Unblocked_Signal_Mask : aliased sigset_t;
102 -- The set of signals that should unblocked in all tasks
104 -- The followings are internal configuration constants needed
106 Next_Serial_Number : Task_Serial_Number := 100;
107 -- We start at 100, to reserve some special values for
108 -- using in error checking.
110 Time_Slice_Val : Integer;
111 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
113 Dispatching_Policy : Character;
114 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
116 Foreign_Task_Elaborated : aliased Boolean := True;
117 -- Used to identified fake tasks (i.e., non-Ada Threads)
125 procedure Initialize (Environment_Task : Task_Id);
126 pragma Inline (Initialize);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task return Boolean;
130 pragma Inline (Is_Valid_Task);
131 -- Does executing thread have a TCB?
133 procedure Set (Self_Id : Task_Id);
135 -- Set the self id for the current task
137 function Self return Task_Id;
138 pragma Inline (Self);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread : Thread_Id) return Task_Id is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 procedure Abort_Handler (Sig : Signal);
161 -- Signal handler used to implement asynchronous abort.
162 -- See also comment before body, below.
164 function To_Address is
165 new Ada.Unchecked_Conversion (Task_Id, System.Address);
171 -- Target-dependent binding of inter-thread Abort signal to the raising of
172 -- the Abort_Signal exception.
174 -- The technical issues and alternatives here are essentially the
175 -- same as for raising exceptions in response to other signals
176 -- (e.g. Storage_Error). See code and comments in the package body
177 -- System.Interrupt_Management.
179 -- Some implementations may not allow an exception to be propagated out of
180 -- a handler, and others might leave the signal or interrupt that invoked
181 -- this handler masked after the exceptional return to the application
184 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
185 -- most UNIX systems, this will allow transfer out of a signal handler,
186 -- which is usually the only mechanism available for implementing
187 -- asynchronous handlers of this kind. However, some systems do not
188 -- restore the signal mask on longjmp(), leaving the abort signal masked.
190 procedure Abort_Handler (Sig : Signal) is
191 pragma Unreferenced (Sig);
193 T : constant Task_Id := Self;
194 Old_Set : aliased sigset_t;
196 Result : Interfaces.C.int;
197 pragma Warnings (Off, Result);
200 -- It is not safe to raise an exception when using ZCX and the GCC
201 -- exception handling mechanism.
203 if ZCX_By_Default and then GCC_ZCX_Support then
207 if T.Deferral_Level = 0
208 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
213 -- Make sure signals used for RTS internal purpose are unmasked
215 Result := pthread_sigmask (SIG_UNBLOCK,
216 Unblocked_Signal_Mask'Access, Old_Set'Access);
217 pragma Assert (Result = 0);
219 raise Standard'Abort_Signal;
227 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
228 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
229 Guard_Page_Address : Address;
231 Res : Interfaces.C.int;
234 if Stack_Base_Available then
236 -- Compute the guard page address
238 Guard_Page_Address :=
239 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
242 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
244 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
247 pragma Assert (Res = 0);
255 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
257 return T.Common.LL.Thread;
264 function Self return Task_Id renames Specific.Self;
266 ---------------------
267 -- Initialize_Lock --
268 ---------------------
270 -- Note: mutexes and cond_variables needed per-task basis are
271 -- initialized in Intialize_TCB and the Storage_Error is
272 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
273 -- used in RTS is initialized before any status change of RTS.
274 -- Therefore rasing Storage_Error in the following routines
275 -- should be able to be handled safely.
277 procedure Initialize_Lock
278 (Prio : System.Any_Priority;
279 L : not null access Lock)
281 Attributes : aliased pthread_mutexattr_t;
282 Result : Interfaces.C.int;
285 Result := pthread_mutexattr_init (Attributes'Access);
286 pragma Assert (Result = 0 or else Result = ENOMEM);
288 if Result = ENOMEM then
292 if Locking_Policy = 'C' then
293 Result := pthread_mutexattr_setprotocol
294 (Attributes'Access, PTHREAD_PRIO_PROTECT);
295 pragma Assert (Result = 0);
297 Result := pthread_mutexattr_setprioceiling
298 (Attributes'Access, Interfaces.C.int (Prio));
299 pragma Assert (Result = 0);
301 elsif Locking_Policy = 'I' then
302 Result := pthread_mutexattr_setprotocol
303 (Attributes'Access, PTHREAD_PRIO_INHERIT);
304 pragma Assert (Result = 0);
307 Result := pthread_mutex_init (L, Attributes'Access);
308 pragma Assert (Result = 0 or else Result = ENOMEM);
310 if Result = ENOMEM then
311 Result := pthread_mutexattr_destroy (Attributes'Access);
315 Result := pthread_mutexattr_destroy (Attributes'Access);
316 pragma Assert (Result = 0);
319 procedure Initialize_Lock
320 (L : not null access RTS_Lock; Level : Lock_Level)
322 pragma Unreferenced (Level);
324 Attributes : aliased pthread_mutexattr_t;
325 Result : Interfaces.C.int;
328 Result := pthread_mutexattr_init (Attributes'Access);
329 pragma Assert (Result = 0 or else Result = ENOMEM);
331 if Result = ENOMEM then
335 if Locking_Policy = 'C' then
336 Result := pthread_mutexattr_setprotocol
337 (Attributes'Access, PTHREAD_PRIO_PROTECT);
338 pragma Assert (Result = 0);
340 Result := pthread_mutexattr_setprioceiling
341 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
342 pragma Assert (Result = 0);
344 elsif Locking_Policy = 'I' then
345 Result := pthread_mutexattr_setprotocol
346 (Attributes'Access, PTHREAD_PRIO_INHERIT);
347 pragma Assert (Result = 0);
350 Result := pthread_mutex_init (L, Attributes'Access);
351 pragma Assert (Result = 0 or else Result = ENOMEM);
353 if Result = ENOMEM then
354 Result := pthread_mutexattr_destroy (Attributes'Access);
358 Result := pthread_mutexattr_destroy (Attributes'Access);
359 pragma Assert (Result = 0);
366 procedure Finalize_Lock (L : not null access Lock) is
367 Result : Interfaces.C.int;
369 Result := pthread_mutex_destroy (L);
370 pragma Assert (Result = 0);
373 procedure Finalize_Lock (L : not null access RTS_Lock) is
374 Result : Interfaces.C.int;
376 Result := pthread_mutex_destroy (L);
377 pragma Assert (Result = 0);
385 (L : not null access Lock; Ceiling_Violation : out Boolean)
387 Result : Interfaces.C.int;
390 Result := pthread_mutex_lock (L);
392 -- Assume that the cause of EINVAL is a priority ceiling violation
394 Ceiling_Violation := (Result = EINVAL);
395 pragma Assert (Result = 0 or else Result = EINVAL);
399 (L : not null access RTS_Lock;
400 Global_Lock : Boolean := False)
402 Result : Interfaces.C.int;
404 if not Single_Lock or else Global_Lock then
405 Result := pthread_mutex_lock (L);
406 pragma Assert (Result = 0);
410 procedure Write_Lock (T : Task_Id) is
411 Result : Interfaces.C.int;
413 if not Single_Lock then
414 Result := pthread_mutex_lock (T.Common.LL.L'Access);
415 pragma Assert (Result = 0);
424 (L : not null access Lock; Ceiling_Violation : out Boolean) is
426 Write_Lock (L, Ceiling_Violation);
433 procedure Unlock (L : not null access Lock) is
434 Result : Interfaces.C.int;
436 Result := pthread_mutex_unlock (L);
437 pragma Assert (Result = 0);
441 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
443 Result : Interfaces.C.int;
445 if not Single_Lock or else Global_Lock then
446 Result := pthread_mutex_unlock (L);
447 pragma Assert (Result = 0);
451 procedure Unlock (T : Task_Id) is
452 Result : Interfaces.C.int;
454 if not Single_Lock then
455 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
456 pragma Assert (Result = 0);
464 -- Dynamic priority ceilings are not supported by the underlying system
466 procedure Set_Ceiling
467 (L : not null access Lock;
468 Prio : System.Any_Priority)
470 pragma Unreferenced (L, Prio);
481 Reason : System.Tasking.Task_States)
483 pragma Unreferenced (Reason);
485 Result : Interfaces.C.int;
491 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
495 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
498 -- EINTR is not considered a failure
500 pragma Assert (Result = 0 or else Result = EINTR);
507 -- This is for use within the run-time system, so abort is
508 -- assumed to be already deferred, and the caller should be
509 -- holding its own ATCB lock.
511 procedure Timed_Sleep
514 Mode : ST.Delay_Modes;
515 Reason : Task_States;
516 Timedout : out Boolean;
517 Yielded : out Boolean)
519 pragma Unreferenced (Reason);
521 Base_Time : constant Duration := Monotonic_Clock;
522 Check_Time : Duration := Base_Time;
525 Request : aliased timespec;
526 Result : Interfaces.C.int;
532 if Mode = Relative then
533 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
535 if Relative_Timed_Wait then
536 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
540 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
542 if Relative_Timed_Wait then
543 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
547 if Abs_Time > Check_Time then
548 if Relative_Timed_Wait then
549 Request := To_Timespec (Rel_Time);
551 Request := To_Timespec (Abs_Time);
555 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
559 pthread_cond_timedwait
560 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
565 pthread_cond_timedwait
566 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
570 Check_Time := Monotonic_Clock;
571 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
573 if Result = 0 or Result = EINTR then
575 -- Somebody may have called Wakeup for us
581 pragma Assert (Result = ETIMEDOUT);
590 -- This is for use in implementing delay statements, so we assume the
591 -- caller is abort-deferred but is holding no locks.
593 procedure Timed_Delay
596 Mode : ST.Delay_Modes)
598 Base_Time : constant Duration := Monotonic_Clock;
599 Check_Time : Duration := Base_Time;
602 Request : aliased timespec;
604 Result : Interfaces.C.int;
605 pragma Warnings (Off, Result);
612 Write_Lock (Self_ID);
614 if Mode = Relative then
615 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
617 if Relative_Timed_Wait then
618 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
622 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
624 if Relative_Timed_Wait then
625 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
629 if Abs_Time > Check_Time then
630 if Relative_Timed_Wait then
631 Request := To_Timespec (Rel_Time);
633 Request := To_Timespec (Abs_Time);
636 Self_ID.Common.State := Delay_Sleep;
639 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
642 Result := pthread_cond_timedwait
643 (Self_ID.Common.LL.CV'Access,
644 Single_RTS_Lock'Access,
647 Result := pthread_cond_timedwait
648 (Self_ID.Common.LL.CV'Access,
649 Self_ID.Common.LL.L'Access,
653 Check_Time := Monotonic_Clock;
654 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
656 pragma Assert (Result = 0
657 or else Result = ETIMEDOUT
658 or else Result = EINTR);
661 Self_ID.Common.State := Runnable;
670 Result := sched_yield;
673 ---------------------
674 -- Monotonic_Clock --
675 ---------------------
677 function Monotonic_Clock return Duration is
678 TS : aliased timespec;
679 Result : Interfaces.C.int;
681 Result := clock_gettime
682 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
683 pragma Assert (Result = 0);
684 return To_Duration (TS);
691 function RT_Resolution return Duration is
700 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
701 pragma Unreferenced (Reason);
702 Result : Interfaces.C.int;
704 Result := pthread_cond_signal (T.Common.LL.CV'Access);
705 pragma Assert (Result = 0);
712 procedure Yield (Do_Yield : Boolean := True) is
713 Result : Interfaces.C.int;
714 pragma Unreferenced (Result);
717 Result := sched_yield;
725 procedure Set_Priority
727 Prio : System.Any_Priority;
728 Loss_Of_Inheritance : Boolean := False)
730 pragma Unreferenced (Loss_Of_Inheritance);
732 Result : Interfaces.C.int;
733 Param : aliased struct_sched_param;
735 function Get_Policy (Prio : System.Any_Priority) return Character;
736 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
737 -- Get priority specific dispatching policy
739 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
740 -- Upper case first character of the policy name corresponding to the
741 -- task as set by a Priority_Specific_Dispatching pragma.
744 T.Common.Current_Priority := Prio;
745 Param.sched_priority := To_Target_Priority (Prio);
747 if Time_Slice_Supported
748 and then (Dispatching_Policy = 'R'
749 or else Priority_Specific_Policy = 'R'
750 or else Time_Slice_Val > 0)
752 Result := pthread_setschedparam
753 (T.Common.LL.Thread, SCHED_RR, Param'Access);
755 elsif Dispatching_Policy = 'F'
756 or else Priority_Specific_Policy = 'F'
757 or else Time_Slice_Val = 0
759 Result := pthread_setschedparam
760 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
763 Result := pthread_setschedparam
764 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
767 pragma Assert (Result = 0);
774 function Get_Priority (T : Task_Id) return System.Any_Priority is
776 return T.Common.Current_Priority;
783 procedure Enter_Task (Self_ID : Task_Id) is
785 Self_ID.Common.LL.Thread := pthread_self;
786 Self_ID.Common.LL.LWP := lwp_self;
788 Specific.Set (Self_ID);
792 for J in Known_Tasks'Range loop
793 if Known_Tasks (J) = null then
794 Known_Tasks (J) := Self_ID;
795 Self_ID.Known_Tasks_Index := J;
807 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
809 return new Ada_Task_Control_Block (Entry_Num);
816 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
818 -----------------------------
819 -- Register_Foreign_Thread --
820 -----------------------------
822 function Register_Foreign_Thread return Task_Id is
824 if Is_Valid_Task then
827 return Register_Foreign_Thread (pthread_self);
829 end Register_Foreign_Thread;
835 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
836 Mutex_Attr : aliased pthread_mutexattr_t;
837 Result : Interfaces.C.int;
838 Cond_Attr : aliased pthread_condattr_t;
841 -- Give the task a unique serial number
843 Self_ID.Serial_Number := Next_Serial_Number;
844 Next_Serial_Number := Next_Serial_Number + 1;
845 pragma Assert (Next_Serial_Number /= 0);
847 if not Single_Lock then
848 Result := pthread_mutexattr_init (Mutex_Attr'Access);
849 pragma Assert (Result = 0 or else Result = ENOMEM);
852 if Locking_Policy = 'C' then
854 pthread_mutexattr_setprotocol
856 PTHREAD_PRIO_PROTECT);
857 pragma Assert (Result = 0);
860 pthread_mutexattr_setprioceiling
862 Interfaces.C.int (System.Any_Priority'Last));
863 pragma Assert (Result = 0);
865 elsif Locking_Policy = 'I' then
867 pthread_mutexattr_setprotocol
869 PTHREAD_PRIO_INHERIT);
870 pragma Assert (Result = 0);
875 (Self_ID.Common.LL.L'Access,
877 pragma Assert (Result = 0 or else Result = ENOMEM);
885 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
886 pragma Assert (Result = 0);
889 Result := pthread_condattr_init (Cond_Attr'Access);
890 pragma Assert (Result = 0 or else Result = ENOMEM);
895 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
896 pragma Assert (Result = 0 or else Result = ENOMEM);
902 if not Single_Lock then
903 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
904 pragma Assert (Result = 0);
910 Result := pthread_condattr_destroy (Cond_Attr'Access);
911 pragma Assert (Result = 0);
918 procedure Create_Task
920 Wrapper : System.Address;
921 Stack_Size : System.Parameters.Size_Type;
922 Priority : System.Any_Priority;
923 Succeeded : out Boolean)
925 Attributes : aliased pthread_attr_t;
926 Adjusted_Stack_Size : Interfaces.C.size_t;
927 Result : Interfaces.C.int;
929 function Thread_Body_Access is new
930 Ada.Unchecked_Conversion (System.Address, Thread_Body);
932 use System.Task_Info;
935 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
937 if Stack_Base_Available then
939 -- If Stack Checking is supported then allocate 2 additional pages:
941 -- In the worst case, stack is allocated at something like
942 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
943 -- to be sure the effective stack size is greater than what
946 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
949 Result := pthread_attr_init (Attributes'Access);
950 pragma Assert (Result = 0 or else Result = ENOMEM);
958 pthread_attr_setdetachstate
959 (Attributes'Access, PTHREAD_CREATE_DETACHED);
960 pragma Assert (Result = 0);
963 pthread_attr_setstacksize
964 (Attributes'Access, Adjusted_Stack_Size);
965 pragma Assert (Result = 0);
967 if T.Common.Task_Info /= Default_Scope then
968 case T.Common.Task_Info is
969 when System.Task_Info.Process_Scope =>
971 pthread_attr_setscope
972 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
974 when System.Task_Info.System_Scope =>
976 pthread_attr_setscope
977 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
979 when System.Task_Info.Default_Scope =>
983 pragma Assert (Result = 0);
986 -- Since the initial signal mask of a thread is inherited from the
987 -- creator, and the Environment task has all its signals masked, we
988 -- do not need to manipulate caller's signal mask at this point.
989 -- All tasks in RTS will have All_Tasks_Mask initially.
991 Result := pthread_create
992 (T.Common.LL.Thread'Access,
994 Thread_Body_Access (Wrapper),
996 pragma Assert (Result = 0 or else Result = EAGAIN);
998 Succeeded := Result = 0;
1000 Result := pthread_attr_destroy (Attributes'Access);
1001 pragma Assert (Result = 0);
1004 Set_Priority (T, Priority);
1012 procedure Finalize_TCB (T : Task_Id) is
1013 Result : Interfaces.C.int;
1015 Is_Self : constant Boolean := T = Self;
1017 procedure Free is new
1018 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1021 if not Single_Lock then
1022 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1023 pragma Assert (Result = 0);
1026 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1027 pragma Assert (Result = 0);
1029 if T.Known_Tasks_Index /= -1 then
1030 Known_Tasks (T.Known_Tasks_Index) := null;
1036 Specific.Set (null);
1044 procedure Exit_Task is
1046 -- Mark this task as unknown, so that if Self is called, it won't
1047 -- return a dangling pointer.
1049 Specific.Set (null);
1056 procedure Abort_Task (T : Task_Id) is
1057 Result : Interfaces.C.int;
1061 (T.Common.LL.Thread,
1062 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1063 pragma Assert (Result = 0);
1070 procedure Initialize (S : in out Suspension_Object) is
1071 Mutex_Attr : aliased pthread_mutexattr_t;
1072 Cond_Attr : aliased pthread_condattr_t;
1073 Result : Interfaces.C.int;
1076 -- Initialize internal state (always to False (RM D.10 (6)))
1081 -- Initialize internal mutex
1083 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1084 pragma Assert (Result = 0 or else Result = ENOMEM);
1086 if Result = ENOMEM then
1087 raise Storage_Error;
1090 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1091 pragma Assert (Result = 0 or else Result = ENOMEM);
1093 if Result = ENOMEM then
1094 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1095 pragma Assert (Result = 0);
1097 raise Storage_Error;
1100 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1101 pragma Assert (Result = 0);
1103 -- Initialize internal condition variable
1105 Result := pthread_condattr_init (Cond_Attr'Access);
1106 pragma Assert (Result = 0 or else Result = ENOMEM);
1109 Result := pthread_mutex_destroy (S.L'Access);
1110 pragma Assert (Result = 0);
1112 if Result = ENOMEM then
1113 raise Storage_Error;
1117 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1118 pragma Assert (Result = 0 or else Result = ENOMEM);
1121 Result := pthread_mutex_destroy (S.L'Access);
1122 pragma Assert (Result = 0);
1124 if Result = ENOMEM then
1125 Result := pthread_condattr_destroy (Cond_Attr'Access);
1126 pragma Assert (Result = 0);
1127 raise Storage_Error;
1131 Result := pthread_condattr_destroy (Cond_Attr'Access);
1132 pragma Assert (Result = 0);
1139 procedure Finalize (S : in out Suspension_Object) is
1140 Result : Interfaces.C.int;
1143 -- Destroy internal mutex
1145 Result := pthread_mutex_destroy (S.L'Access);
1146 pragma Assert (Result = 0);
1148 -- Destroy internal condition variable
1150 Result := pthread_cond_destroy (S.CV'Access);
1151 pragma Assert (Result = 0);
1158 function Current_State (S : Suspension_Object) return Boolean is
1160 -- We do not want to use lock on this read operation. State is marked
1161 -- as Atomic so that we ensure that the value retrieved is correct.
1170 procedure Set_False (S : in out Suspension_Object) is
1171 Result : Interfaces.C.int;
1174 SSL.Abort_Defer.all;
1176 Result := pthread_mutex_lock (S.L'Access);
1177 pragma Assert (Result = 0);
1181 Result := pthread_mutex_unlock (S.L'Access);
1182 pragma Assert (Result = 0);
1184 SSL.Abort_Undefer.all;
1191 procedure Set_True (S : in out Suspension_Object) is
1192 Result : Interfaces.C.int;
1195 SSL.Abort_Defer.all;
1197 Result := pthread_mutex_lock (S.L'Access);
1198 pragma Assert (Result = 0);
1200 -- If there is already a task waiting on this suspension object then
1201 -- we resume it, leaving the state of the suspension object to False,
1202 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1203 -- the state to True.
1209 Result := pthread_cond_signal (S.CV'Access);
1210 pragma Assert (Result = 0);
1216 Result := pthread_mutex_unlock (S.L'Access);
1217 pragma Assert (Result = 0);
1219 SSL.Abort_Undefer.all;
1222 ------------------------
1223 -- Suspend_Until_True --
1224 ------------------------
1226 procedure Suspend_Until_True (S : in out Suspension_Object) is
1227 Result : Interfaces.C.int;
1230 SSL.Abort_Defer.all;
1232 Result := pthread_mutex_lock (S.L'Access);
1233 pragma Assert (Result = 0);
1237 -- Program_Error must be raised upon calling Suspend_Until_True
1238 -- if another task is already waiting on that suspension object
1241 Result := pthread_mutex_unlock (S.L'Access);
1242 pragma Assert (Result = 0);
1244 SSL.Abort_Undefer.all;
1246 raise Program_Error;
1249 -- Suspend the task if the state is False. Otherwise, the task
1250 -- continues its execution, and the state of the suspension object
1251 -- is set to False (ARM D.10 par. 9).
1257 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1260 Result := pthread_mutex_unlock (S.L'Access);
1261 pragma Assert (Result = 0);
1263 SSL.Abort_Undefer.all;
1265 end Suspend_Until_True;
1273 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1274 pragma Unreferenced (Self_ID);
1279 --------------------
1280 -- Check_No_Locks --
1281 --------------------
1283 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1284 pragma Unreferenced (Self_ID);
1289 ----------------------
1290 -- Environment_Task --
1291 ----------------------
1293 function Environment_Task return Task_Id is
1295 return Environment_Task_Id;
1296 end Environment_Task;
1302 procedure Lock_RTS is
1304 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1311 procedure Unlock_RTS is
1313 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1320 function Suspend_Task
1322 Thread_Self : Thread_Id) return Boolean
1324 pragma Unreferenced (T, Thread_Self);
1333 function Resume_Task
1335 Thread_Self : Thread_Id) return Boolean
1337 pragma Unreferenced (T, Thread_Self);
1342 --------------------
1343 -- Stop_All_Tasks --
1344 --------------------
1346 procedure Stop_All_Tasks is
1355 function Stop_Task (T : ST.Task_Id) return Boolean is
1356 pragma Unreferenced (T);
1365 function Continue_Task (T : ST.Task_Id) return Boolean is
1366 pragma Unreferenced (T);
1375 procedure Initialize (Environment_Task : Task_Id) is
1376 act : aliased struct_sigaction;
1377 old_act : aliased struct_sigaction;
1378 Tmp_Set : aliased sigset_t;
1379 Result : Interfaces.C.int;
1382 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1383 pragma Import (C, State, "__gnat_get_interrupt_state");
1384 -- Get interrupt state. Defined in a-init.c
1385 -- The input argument is the interrupt number,
1386 -- and the result is one of the following:
1388 Default : constant Character := 's';
1389 -- 'n' this interrupt not set by any Interrupt_State pragma
1390 -- 'u' Interrupt_State pragma set state to User
1391 -- 'r' Interrupt_State pragma set state to Runtime
1392 -- 's' Interrupt_State pragma set state to System (use "default"
1396 Environment_Task_Id := Environment_Task;
1398 Interrupt_Management.Initialize;
1400 -- Prepare the set of signals that should unblocked in all tasks
1402 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1403 pragma Assert (Result = 0);
1405 for J in Interrupt_Management.Interrupt_ID loop
1406 if System.Interrupt_Management.Keep_Unmasked (J) then
1407 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1408 pragma Assert (Result = 0);
1412 -- Initialize the lock used to synchronize chain of all ATCBs
1414 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1416 Specific.Initialize (Environment_Task);
1418 Enter_Task (Environment_Task);
1420 -- Install the abort-signal handler
1423 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1426 act.sa_handler := Abort_Handler'Address;
1428 Result := sigemptyset (Tmp_Set'Access);
1429 pragma Assert (Result = 0);
1430 act.sa_mask := Tmp_Set;
1434 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1435 act'Unchecked_Access,
1436 old_act'Unchecked_Access);
1437 pragma Assert (Result = 0);
1441 end System.Task_Primitives.Operations;