1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a POSIX-like version of this package
34 -- This package contains all the GNULL primitives that interface directly with
37 -- Note: this file can only be used for POSIX compliant systems that implement
38 -- SCHED_FIFO and Ceiling Locking correctly.
40 -- For configurations where SCHED_FIFO and priority ceiling are not a
41 -- requirement, this file can also be used (e.g AiX threads)
44 -- Turn off polling, we do not want ATC polling to take place during tasking
45 -- operations. It causes infinite loops and other problems.
47 with Ada.Unchecked_Conversion;
48 with Ada.Unchecked_Deallocation;
52 with System.Tasking.Debug;
53 with System.Interrupt_Management;
54 with System.OS_Primitives;
55 with System.Task_Info;
57 with System.Soft_Links;
58 -- We use System.Soft_Links instead of System.Tasking.Initialization
59 -- because the later is a higher level package that we shouldn't depend on.
60 -- For example when using the restricted run time, it is replaced by
61 -- System.Tasking.Restricted.Stages.
63 package body System.Task_Primitives.Operations is
65 package SSL renames System.Soft_Links;
67 use System.Tasking.Debug;
70 use System.OS_Interface;
71 use System.Parameters;
72 use System.OS_Primitives;
78 -- The followings are logically constants, but need to be initialized
81 Single_RTS_Lock : aliased RTS_Lock;
82 -- This is a lock to allow only one thread of control in the RTS at
83 -- a time; it is used to execute in mutual exclusion from all other tasks.
84 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
86 ATCB_Key : aliased pthread_key_t;
87 -- Key used to find the Ada Task_Id associated with a thread
89 Environment_Task_Id : Task_Id;
90 -- A variable to hold Task_Id for the environment task
92 Locking_Policy : Character;
93 pragma Import (C, Locking_Policy, "__gl_locking_policy");
94 -- Value of the pragma Locking_Policy:
95 -- 'C' for Ceiling_Locking
96 -- 'I' for Inherit_Locking
99 Unblocked_Signal_Mask : aliased sigset_t;
100 -- The set of signals that should unblocked in all tasks
102 -- The followings are internal configuration constants needed
104 Next_Serial_Number : Task_Serial_Number := 100;
105 -- We start at 100, to reserve some special values for
106 -- using in error checking.
108 Time_Slice_Val : Integer;
109 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
111 Dispatching_Policy : Character;
112 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
114 Foreign_Task_Elaborated : aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
118 -- Whether to use an alternate signal stack for stack overflows
120 Abort_Handler_Installed : Boolean := False;
121 -- True if a handler for the abort signal is installed
129 procedure Initialize (Environment_Task : Task_Id);
130 pragma Inline (Initialize);
131 -- Initialize various data needed by this package
133 function Is_Valid_Task return Boolean;
134 pragma Inline (Is_Valid_Task);
135 -- Does executing thread have a TCB?
137 procedure Set (Self_Id : Task_Id);
139 -- Set the self id for the current task
141 function Self return Task_Id;
142 pragma Inline (Self);
143 -- Return a pointer to the Ada Task Control Block of the calling task
147 package body Specific is separate;
148 -- The body of this package is target specific
150 ---------------------------------
151 -- Support for foreign threads --
152 ---------------------------------
154 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
155 -- Allocate and Initialize a new ATCB for the current Thread
157 function Register_Foreign_Thread
158 (Thread : Thread_Id) return Task_Id is separate;
160 -----------------------
161 -- Local Subprograms --
162 -----------------------
164 procedure Abort_Handler (Sig : Signal);
165 -- Signal handler used to implement asynchronous abort.
166 -- See also comment before body, below.
168 function To_Address is
169 new Ada.Unchecked_Conversion (Task_Id, System.Address);
175 -- Target-dependent binding of inter-thread Abort signal to the raising of
176 -- the Abort_Signal exception.
178 -- The technical issues and alternatives here are essentially the
179 -- same as for raising exceptions in response to other signals
180 -- (e.g. Storage_Error). See code and comments in the package body
181 -- System.Interrupt_Management.
183 -- Some implementations may not allow an exception to be propagated out of
184 -- a handler, and others might leave the signal or interrupt that invoked
185 -- this handler masked after the exceptional return to the application
188 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
189 -- most UNIX systems, this will allow transfer out of a signal handler,
190 -- which is usually the only mechanism available for implementing
191 -- asynchronous handlers of this kind. However, some systems do not
192 -- restore the signal mask on longjmp(), leaving the abort signal masked.
194 procedure Abort_Handler (Sig : Signal) is
195 pragma Unreferenced (Sig);
197 T : constant Task_Id := Self;
198 Old_Set : aliased sigset_t;
200 Result : Interfaces.C.int;
201 pragma Warnings (Off, Result);
204 -- It's not safe to raise an exception when using GCC ZCX mechanism.
205 -- Note that we still need to install a signal handler, since in some
206 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
207 -- need to send the Abort signal to a task.
209 if ZCX_By_Default and then GCC_ZCX_Support then
213 if T.Deferral_Level = 0
214 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
219 -- Make sure signals used for RTS internal purpose are unmasked
221 Result := pthread_sigmask (SIG_UNBLOCK,
222 Unblocked_Signal_Mask'Access, Old_Set'Access);
223 pragma Assert (Result = 0);
225 raise Standard'Abort_Signal;
233 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
234 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
235 Guard_Page_Address : Address;
237 Res : Interfaces.C.int;
240 if Stack_Base_Available then
242 -- Compute the guard page address
244 Guard_Page_Address :=
245 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
248 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
250 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
253 pragma Assert (Res = 0);
261 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
263 return T.Common.LL.Thread;
270 function Self return Task_Id renames Specific.Self;
272 ---------------------
273 -- Initialize_Lock --
274 ---------------------
276 -- Note: mutexes and cond_variables needed per-task basis are
277 -- initialized in Initialize_TCB and the Storage_Error is
278 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
279 -- used in RTS is initialized before any status change of RTS.
280 -- Therefore raising Storage_Error in the following routines
281 -- should be able to be handled safely.
283 procedure Initialize_Lock
284 (Prio : System.Any_Priority;
285 L : not null access Lock)
287 Attributes : aliased pthread_mutexattr_t;
288 Result : Interfaces.C.int;
291 Result := pthread_mutexattr_init (Attributes'Access);
292 pragma Assert (Result = 0 or else Result = ENOMEM);
294 if Result = ENOMEM then
298 if Locking_Policy = 'C' then
299 Result := pthread_mutexattr_setprotocol
300 (Attributes'Access, PTHREAD_PRIO_PROTECT);
301 pragma Assert (Result = 0);
303 Result := pthread_mutexattr_setprioceiling
304 (Attributes'Access, Interfaces.C.int (Prio));
305 pragma Assert (Result = 0);
307 elsif Locking_Policy = 'I' then
308 Result := pthread_mutexattr_setprotocol
309 (Attributes'Access, PTHREAD_PRIO_INHERIT);
310 pragma Assert (Result = 0);
313 Result := pthread_mutex_init (L, Attributes'Access);
314 pragma Assert (Result = 0 or else Result = ENOMEM);
316 if Result = ENOMEM then
317 Result := pthread_mutexattr_destroy (Attributes'Access);
321 Result := pthread_mutexattr_destroy (Attributes'Access);
322 pragma Assert (Result = 0);
325 procedure Initialize_Lock
326 (L : not null access RTS_Lock; Level : Lock_Level)
328 pragma Unreferenced (Level);
330 Attributes : aliased pthread_mutexattr_t;
331 Result : Interfaces.C.int;
334 Result := pthread_mutexattr_init (Attributes'Access);
335 pragma Assert (Result = 0 or else Result = ENOMEM);
337 if Result = ENOMEM then
341 if Locking_Policy = 'C' then
342 Result := pthread_mutexattr_setprotocol
343 (Attributes'Access, PTHREAD_PRIO_PROTECT);
344 pragma Assert (Result = 0);
346 Result := pthread_mutexattr_setprioceiling
347 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
348 pragma Assert (Result = 0);
350 elsif Locking_Policy = 'I' then
351 Result := pthread_mutexattr_setprotocol
352 (Attributes'Access, PTHREAD_PRIO_INHERIT);
353 pragma Assert (Result = 0);
356 Result := pthread_mutex_init (L, Attributes'Access);
357 pragma Assert (Result = 0 or else Result = ENOMEM);
359 if Result = ENOMEM then
360 Result := pthread_mutexattr_destroy (Attributes'Access);
364 Result := pthread_mutexattr_destroy (Attributes'Access);
365 pragma Assert (Result = 0);
372 procedure Finalize_Lock (L : not null access Lock) is
373 Result : Interfaces.C.int;
375 Result := pthread_mutex_destroy (L);
376 pragma Assert (Result = 0);
379 procedure Finalize_Lock (L : not null access RTS_Lock) is
380 Result : Interfaces.C.int;
382 Result := pthread_mutex_destroy (L);
383 pragma Assert (Result = 0);
391 (L : not null access Lock; Ceiling_Violation : out Boolean)
393 Result : Interfaces.C.int;
396 Result := pthread_mutex_lock (L);
398 -- Assume that the cause of EINVAL is a priority ceiling violation
400 Ceiling_Violation := (Result = EINVAL);
401 pragma Assert (Result = 0 or else Result = EINVAL);
405 (L : not null access RTS_Lock;
406 Global_Lock : Boolean := False)
408 Result : Interfaces.C.int;
410 if not Single_Lock or else Global_Lock then
411 Result := pthread_mutex_lock (L);
412 pragma Assert (Result = 0);
416 procedure Write_Lock (T : Task_Id) is
417 Result : Interfaces.C.int;
419 if not Single_Lock then
420 Result := pthread_mutex_lock (T.Common.LL.L'Access);
421 pragma Assert (Result = 0);
430 (L : not null access Lock; Ceiling_Violation : out Boolean) is
432 Write_Lock (L, Ceiling_Violation);
439 procedure Unlock (L : not null access Lock) is
440 Result : Interfaces.C.int;
442 Result := pthread_mutex_unlock (L);
443 pragma Assert (Result = 0);
447 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
449 Result : Interfaces.C.int;
451 if not Single_Lock or else Global_Lock then
452 Result := pthread_mutex_unlock (L);
453 pragma Assert (Result = 0);
457 procedure Unlock (T : Task_Id) is
458 Result : Interfaces.C.int;
460 if not Single_Lock then
461 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
462 pragma Assert (Result = 0);
470 -- Dynamic priority ceilings are not supported by the underlying system
472 procedure Set_Ceiling
473 (L : not null access Lock;
474 Prio : System.Any_Priority)
476 pragma Unreferenced (L, Prio);
487 Reason : System.Tasking.Task_States)
489 pragma Unreferenced (Reason);
491 Result : Interfaces.C.int;
497 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
501 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
504 -- EINTR is not considered a failure
506 pragma Assert (Result = 0 or else Result = EINTR);
513 -- This is for use within the run-time system, so abort is
514 -- assumed to be already deferred, and the caller should be
515 -- holding its own ATCB lock.
517 procedure Timed_Sleep
520 Mode : ST.Delay_Modes;
521 Reason : Task_States;
522 Timedout : out Boolean;
523 Yielded : out Boolean)
525 pragma Unreferenced (Reason);
527 Base_Time : constant Duration := Monotonic_Clock;
528 Check_Time : Duration := Base_Time;
531 Request : aliased timespec;
532 Result : Interfaces.C.int;
538 if Mode = Relative then
539 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
541 if Relative_Timed_Wait then
542 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
546 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
548 if Relative_Timed_Wait then
549 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
553 if Abs_Time > Check_Time then
554 if Relative_Timed_Wait then
555 Request := To_Timespec (Rel_Time);
557 Request := To_Timespec (Abs_Time);
561 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
565 pthread_cond_timedwait
566 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
571 pthread_cond_timedwait
572 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
576 Check_Time := Monotonic_Clock;
577 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
579 if Result = 0 or Result = EINTR then
581 -- Somebody may have called Wakeup for us
587 pragma Assert (Result = ETIMEDOUT);
596 -- This is for use in implementing delay statements, so we assume the
597 -- caller is abort-deferred but is holding no locks.
599 procedure Timed_Delay
602 Mode : ST.Delay_Modes)
604 Base_Time : constant Duration := Monotonic_Clock;
605 Check_Time : Duration := Base_Time;
608 Request : aliased timespec;
610 Result : Interfaces.C.int;
611 pragma Warnings (Off, Result);
618 Write_Lock (Self_ID);
620 if Mode = Relative then
621 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
623 if Relative_Timed_Wait then
624 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
628 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
630 if Relative_Timed_Wait then
631 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
635 if Abs_Time > Check_Time then
636 if Relative_Timed_Wait then
637 Request := To_Timespec (Rel_Time);
639 Request := To_Timespec (Abs_Time);
642 Self_ID.Common.State := Delay_Sleep;
645 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
648 Result := pthread_cond_timedwait
649 (Self_ID.Common.LL.CV'Access,
650 Single_RTS_Lock'Access,
653 Result := pthread_cond_timedwait
654 (Self_ID.Common.LL.CV'Access,
655 Self_ID.Common.LL.L'Access,
659 Check_Time := Monotonic_Clock;
660 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
662 pragma Assert (Result = 0
663 or else Result = ETIMEDOUT
664 or else Result = EINTR);
667 Self_ID.Common.State := Runnable;
676 Result := sched_yield;
679 ---------------------
680 -- Monotonic_Clock --
681 ---------------------
683 function Monotonic_Clock return Duration is
684 TS : aliased timespec;
685 Result : Interfaces.C.int;
687 Result := clock_gettime
688 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
689 pragma Assert (Result = 0);
690 return To_Duration (TS);
697 function RT_Resolution return Duration is
706 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
707 pragma Unreferenced (Reason);
708 Result : Interfaces.C.int;
710 Result := pthread_cond_signal (T.Common.LL.CV'Access);
711 pragma Assert (Result = 0);
718 procedure Yield (Do_Yield : Boolean := True) is
719 Result : Interfaces.C.int;
720 pragma Unreferenced (Result);
723 Result := sched_yield;
731 procedure Set_Priority
733 Prio : System.Any_Priority;
734 Loss_Of_Inheritance : Boolean := False)
736 pragma Unreferenced (Loss_Of_Inheritance);
738 Result : Interfaces.C.int;
739 Param : aliased struct_sched_param;
741 function Get_Policy (Prio : System.Any_Priority) return Character;
742 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
743 -- Get priority specific dispatching policy
745 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
746 -- Upper case first character of the policy name corresponding to the
747 -- task as set by a Priority_Specific_Dispatching pragma.
750 T.Common.Current_Priority := Prio;
751 Param.sched_priority := To_Target_Priority (Prio);
753 if Time_Slice_Supported
754 and then (Dispatching_Policy = 'R'
755 or else Priority_Specific_Policy = 'R'
756 or else Time_Slice_Val > 0)
758 Result := pthread_setschedparam
759 (T.Common.LL.Thread, SCHED_RR, Param'Access);
761 elsif Dispatching_Policy = 'F'
762 or else Priority_Specific_Policy = 'F'
763 or else Time_Slice_Val = 0
765 Result := pthread_setschedparam
766 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
769 Result := pthread_setschedparam
770 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
773 pragma Assert (Result = 0);
780 function Get_Priority (T : Task_Id) return System.Any_Priority is
782 return T.Common.Current_Priority;
789 procedure Enter_Task (Self_ID : Task_Id) is
791 Self_ID.Common.LL.Thread := pthread_self;
792 Self_ID.Common.LL.LWP := lwp_self;
794 Specific.Set (Self_ID);
796 if Use_Alternate_Stack then
798 Stack : aliased stack_t;
799 Result : Interfaces.C.int;
801 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
802 Stack.ss_size := Alternate_Stack_Size;
804 Result := sigaltstack (Stack'Access, null);
805 pragma Assert (Result = 0);
814 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
816 return new Ada_Task_Control_Block (Entry_Num);
823 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
825 -----------------------------
826 -- Register_Foreign_Thread --
827 -----------------------------
829 function Register_Foreign_Thread return Task_Id is
831 if Is_Valid_Task then
834 return Register_Foreign_Thread (pthread_self);
836 end Register_Foreign_Thread;
842 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
843 Mutex_Attr : aliased pthread_mutexattr_t;
844 Result : Interfaces.C.int;
845 Cond_Attr : aliased pthread_condattr_t;
848 -- Give the task a unique serial number
850 Self_ID.Serial_Number := Next_Serial_Number;
851 Next_Serial_Number := Next_Serial_Number + 1;
852 pragma Assert (Next_Serial_Number /= 0);
854 if not Single_Lock then
855 Result := pthread_mutexattr_init (Mutex_Attr'Access);
856 pragma Assert (Result = 0 or else Result = ENOMEM);
859 if Locking_Policy = 'C' then
861 pthread_mutexattr_setprotocol
863 PTHREAD_PRIO_PROTECT);
864 pragma Assert (Result = 0);
867 pthread_mutexattr_setprioceiling
869 Interfaces.C.int (System.Any_Priority'Last));
870 pragma Assert (Result = 0);
872 elsif Locking_Policy = 'I' then
874 pthread_mutexattr_setprotocol
876 PTHREAD_PRIO_INHERIT);
877 pragma Assert (Result = 0);
882 (Self_ID.Common.LL.L'Access,
884 pragma Assert (Result = 0 or else Result = ENOMEM);
892 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
893 pragma Assert (Result = 0);
896 Result := pthread_condattr_init (Cond_Attr'Access);
897 pragma Assert (Result = 0 or else Result = ENOMEM);
902 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
903 pragma Assert (Result = 0 or else Result = ENOMEM);
909 if not Single_Lock then
910 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
911 pragma Assert (Result = 0);
917 Result := pthread_condattr_destroy (Cond_Attr'Access);
918 pragma Assert (Result = 0);
925 procedure Create_Task
927 Wrapper : System.Address;
928 Stack_Size : System.Parameters.Size_Type;
929 Priority : System.Any_Priority;
930 Succeeded : out Boolean)
932 Attributes : aliased pthread_attr_t;
933 Adjusted_Stack_Size : Interfaces.C.size_t;
934 Page_Size : constant Interfaces.C.size_t := Get_Page_Size;
935 Result : Interfaces.C.int;
937 function Thread_Body_Access is new
938 Ada.Unchecked_Conversion (System.Address, Thread_Body);
940 use System.Task_Info;
943 Adjusted_Stack_Size :=
944 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
946 if Stack_Base_Available then
948 -- If Stack Checking is supported then allocate 2 additional pages:
950 -- In the worst case, stack is allocated at something like
951 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
952 -- to be sure the effective stack size is greater than what
955 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
958 -- Round stack size as this is required by some OSes (Darwin)
960 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
961 Adjusted_Stack_Size :=
962 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
964 Result := pthread_attr_init (Attributes'Access);
965 pragma Assert (Result = 0 or else Result = ENOMEM);
973 pthread_attr_setdetachstate
974 (Attributes'Access, PTHREAD_CREATE_DETACHED);
975 pragma Assert (Result = 0);
978 pthread_attr_setstacksize
979 (Attributes'Access, Adjusted_Stack_Size);
980 pragma Assert (Result = 0);
982 if T.Common.Task_Info /= Default_Scope then
983 case T.Common.Task_Info is
984 when System.Task_Info.Process_Scope =>
986 pthread_attr_setscope
987 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
989 when System.Task_Info.System_Scope =>
991 pthread_attr_setscope
992 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
994 when System.Task_Info.Default_Scope =>
998 pragma Assert (Result = 0);
1001 -- Since the initial signal mask of a thread is inherited from the
1002 -- creator, and the Environment task has all its signals masked, we
1003 -- do not need to manipulate caller's signal mask at this point.
1004 -- All tasks in RTS will have All_Tasks_Mask initially.
1006 Result := pthread_create
1007 (T.Common.LL.Thread'Access,
1009 Thread_Body_Access (Wrapper),
1011 pragma Assert (Result = 0 or else Result = EAGAIN);
1013 Succeeded := Result = 0;
1015 Result := pthread_attr_destroy (Attributes'Access);
1016 pragma Assert (Result = 0);
1019 Set_Priority (T, Priority);
1027 procedure Finalize_TCB (T : Task_Id) is
1028 Result : Interfaces.C.int;
1030 Is_Self : constant Boolean := T = Self;
1032 procedure Free is new
1033 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1036 if not Single_Lock then
1037 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1038 pragma Assert (Result = 0);
1041 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1042 pragma Assert (Result = 0);
1044 if T.Known_Tasks_Index /= -1 then
1045 Known_Tasks (T.Known_Tasks_Index) := null;
1051 Specific.Set (null);
1059 procedure Exit_Task is
1061 -- Mark this task as unknown, so that if Self is called, it won't
1062 -- return a dangling pointer.
1064 Specific.Set (null);
1071 procedure Abort_Task (T : Task_Id) is
1072 Result : Interfaces.C.int;
1074 if Abort_Handler_Installed then
1077 (T.Common.LL.Thread,
1078 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1079 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).
1276 -- Loop in case pthread_cond_wait returns earlier than expected
1277 -- (e.g. in case of EINTR caused by a signal).
1279 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1280 pragma Assert (Result = 0 or else Result = EINTR);
1282 exit when not S.Waiting;
1286 Result := pthread_mutex_unlock (S.L'Access);
1287 pragma Assert (Result = 0);
1289 SSL.Abort_Undefer.all;
1291 end Suspend_Until_True;
1299 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1300 pragma Unreferenced (Self_ID);
1305 --------------------
1306 -- Check_No_Locks --
1307 --------------------
1309 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1310 pragma Unreferenced (Self_ID);
1315 ----------------------
1316 -- Environment_Task --
1317 ----------------------
1319 function Environment_Task return Task_Id is
1321 return Environment_Task_Id;
1322 end Environment_Task;
1328 procedure Lock_RTS is
1330 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1337 procedure Unlock_RTS is
1339 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1346 function Suspend_Task
1348 Thread_Self : Thread_Id) return Boolean
1350 pragma Unreferenced (T, Thread_Self);
1359 function Resume_Task
1361 Thread_Self : Thread_Id) return Boolean
1363 pragma Unreferenced (T, Thread_Self);
1368 --------------------
1369 -- Stop_All_Tasks --
1370 --------------------
1372 procedure Stop_All_Tasks is
1381 function Stop_Task (T : ST.Task_Id) return Boolean is
1382 pragma Unreferenced (T);
1391 function Continue_Task (T : ST.Task_Id) return Boolean is
1392 pragma Unreferenced (T);
1401 procedure Initialize (Environment_Task : Task_Id) is
1402 act : aliased struct_sigaction;
1403 old_act : aliased struct_sigaction;
1404 Tmp_Set : aliased sigset_t;
1405 Result : Interfaces.C.int;
1408 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1409 pragma Import (C, State, "__gnat_get_interrupt_state");
1410 -- Get interrupt state. Defined in a-init.c
1411 -- The input argument is the interrupt number,
1412 -- and the result is one of the following:
1414 Default : constant Character := 's';
1415 -- 'n' this interrupt not set by any Interrupt_State pragma
1416 -- 'u' Interrupt_State pragma set state to User
1417 -- 'r' Interrupt_State pragma set state to Runtime
1418 -- 's' Interrupt_State pragma set state to System (use "default"
1422 Environment_Task_Id := Environment_Task;
1424 Interrupt_Management.Initialize;
1426 -- Prepare the set of signals that should unblocked in all tasks
1428 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1429 pragma Assert (Result = 0);
1431 for J in Interrupt_Management.Interrupt_ID loop
1432 if System.Interrupt_Management.Keep_Unmasked (J) then
1433 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1434 pragma Assert (Result = 0);
1438 -- Initialize the lock used to synchronize chain of all ATCBs
1440 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1442 Specific.Initialize (Environment_Task);
1444 if Use_Alternate_Stack then
1445 Environment_Task.Common.Task_Alternate_Stack :=
1446 Alternate_Stack'Address;
1449 -- Make environment task known here because it doesn't go through
1450 -- Activate_Tasks, which does it for all other tasks.
1452 Known_Tasks (Known_Tasks'First) := Environment_Task;
1453 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1455 Enter_Task (Environment_Task);
1458 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1461 act.sa_handler := Abort_Handler'Address;
1463 Result := sigemptyset (Tmp_Set'Access);
1464 pragma Assert (Result = 0);
1465 act.sa_mask := Tmp_Set;
1469 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1470 act'Unchecked_Access,
1471 old_act'Unchecked_Access);
1472 pragma Assert (Result = 0);
1473 Abort_Handler_Installed := True;
1477 end System.Task_Primitives.Operations;