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-2011, 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;
51 with System.Tasking.Debug;
52 with System.Interrupt_Management;
53 with System.OS_Primitives;
54 with System.Task_Info;
56 with System.Soft_Links;
57 -- We use System.Soft_Links instead of System.Tasking.Initialization
58 -- because the later is a higher level package that we shouldn't depend on.
59 -- For example when using the restricted run time, it is replaced by
60 -- System.Tasking.Restricted.Stages.
62 package body System.Task_Primitives.Operations is
64 package SSL renames System.Soft_Links;
66 use System.Tasking.Debug;
69 use System.OS_Interface;
70 use System.Parameters;
71 use System.OS_Primitives;
77 -- The followings are logically constants, but need to be initialized
80 Single_RTS_Lock : aliased RTS_Lock;
81 -- This is a lock to allow only one thread of control in the RTS at
82 -- a time; it is used to execute in mutual exclusion from all other tasks.
83 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
85 Environment_Task_Id : Task_Id;
86 -- A variable to hold Task_Id for the environment task
88 Locking_Policy : Character;
89 pragma Import (C, Locking_Policy, "__gl_locking_policy");
90 -- Value of the pragma Locking_Policy:
91 -- 'C' for Ceiling_Locking
92 -- 'I' for Inherit_Locking
95 Unblocked_Signal_Mask : aliased sigset_t;
96 -- The set of signals that should unblocked in all tasks
98 -- The followings are internal configuration constants needed
100 Next_Serial_Number : Task_Serial_Number := 100;
101 -- We start at 100, to reserve some special values for
102 -- using in error checking.
104 Time_Slice_Val : Integer;
105 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
107 Dispatching_Policy : Character;
108 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
110 Foreign_Task_Elaborated : aliased Boolean := True;
111 -- Used to identified fake tasks (i.e., non-Ada Threads)
113 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
114 -- Whether to use an alternate signal stack for stack overflows
116 Abort_Handler_Installed : Boolean := False;
117 -- True if a handler for the abort signal is installed
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 -- ATCB allocation/deallocation --
148 ----------------------------------
150 package body ATCB_Allocation is separate;
151 -- The body of this package is shared across several targets
153 ---------------------------------
154 -- Support for foreign threads --
155 ---------------------------------
157 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
158 -- Allocate and Initialize a new ATCB for the current Thread
160 function Register_Foreign_Thread
161 (Thread : Thread_Id) return Task_Id is separate;
163 -----------------------
164 -- Local Subprograms --
165 -----------------------
167 procedure Abort_Handler (Sig : Signal);
168 -- Signal handler used to implement asynchronous abort.
169 -- See also comment before body, below.
171 function To_Address is
172 new Ada.Unchecked_Conversion (Task_Id, System.Address);
178 -- Target-dependent binding of inter-thread Abort signal to the raising of
179 -- the Abort_Signal exception.
181 -- The technical issues and alternatives here are essentially the
182 -- same as for raising exceptions in response to other signals
183 -- (e.g. Storage_Error). See code and comments in the package body
184 -- System.Interrupt_Management.
186 -- Some implementations may not allow an exception to be propagated out of
187 -- a handler, and others might leave the signal or interrupt that invoked
188 -- this handler masked after the exceptional return to the application
191 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
192 -- most UNIX systems, this will allow transfer out of a signal handler,
193 -- which is usually the only mechanism available for implementing
194 -- asynchronous handlers of this kind. However, some systems do not
195 -- restore the signal mask on longjmp(), leaving the abort signal masked.
197 procedure Abort_Handler (Sig : Signal) is
198 pragma Unreferenced (Sig);
200 T : constant Task_Id := Self;
201 Old_Set : aliased sigset_t;
203 Result : Interfaces.C.int;
204 pragma Warnings (Off, Result);
207 -- It's not safe to raise an exception when using GCC ZCX mechanism.
208 -- Note that we still need to install a signal handler, since in some
209 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
210 -- need to send the Abort signal to a task.
212 if ZCX_By_Default then
216 if T.Deferral_Level = 0
217 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
222 -- Make sure signals used for RTS internal purpose are unmasked
224 Result := pthread_sigmask (SIG_UNBLOCK,
225 Unblocked_Signal_Mask'Access, Old_Set'Access);
226 pragma Assert (Result = 0);
228 raise Standard'Abort_Signal;
236 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
237 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
238 Guard_Page_Address : Address;
240 Res : Interfaces.C.int;
243 if Stack_Base_Available then
245 -- Compute the guard page address
247 Guard_Page_Address :=
248 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
251 mprotect (Guard_Page_Address, Get_Page_Size,
252 prot => (if On then PROT_ON else 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;
496 (cond => Self_ID.Common.LL.CV'Access,
497 mutex => (if Single_Lock
498 then Single_RTS_Lock'Access
499 else 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
552 To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);
555 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
558 pthread_cond_timedwait
559 (cond => Self_ID.Common.LL.CV'Access,
560 mutex => (if Single_Lock
561 then Single_RTS_Lock'Access
562 else Self_ID.Common.LL.L'Access),
563 abstime => Request'Access);
565 Check_Time := Monotonic_Clock;
566 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
568 if Result = 0 or Result = EINTR then
570 -- Somebody may have called Wakeup for us
576 pragma Assert (Result = ETIMEDOUT);
585 -- This is for use in implementing delay statements, so we assume the
586 -- caller is abort-deferred but is holding no locks.
588 procedure Timed_Delay
591 Mode : ST.Delay_Modes)
593 Base_Time : constant Duration := Monotonic_Clock;
594 Check_Time : Duration := Base_Time;
597 Request : aliased timespec;
599 Result : Interfaces.C.int;
600 pragma Warnings (Off, Result);
607 Write_Lock (Self_ID);
609 if Mode = Relative then
610 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
612 if Relative_Timed_Wait then
613 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
617 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
619 if Relative_Timed_Wait then
620 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
624 if Abs_Time > Check_Time then
626 To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);
627 Self_ID.Common.State := Delay_Sleep;
630 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
633 pthread_cond_timedwait
634 (cond => Self_ID.Common.LL.CV'Access,
635 mutex => (if Single_Lock
636 then Single_RTS_Lock'Access
637 else Self_ID.Common.LL.L'Access),
638 abstime => Request'Access);
640 Check_Time := Monotonic_Clock;
641 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
643 pragma Assert (Result = 0
644 or else Result = ETIMEDOUT
645 or else Result = EINTR);
648 Self_ID.Common.State := Runnable;
657 Result := sched_yield;
660 ---------------------
661 -- Monotonic_Clock --
662 ---------------------
664 function Monotonic_Clock return Duration is
665 TS : aliased timespec;
666 Result : Interfaces.C.int;
668 Result := clock_gettime
669 (clock_id => CLOCK_MONOTONIC, tp => TS'Unchecked_Access);
670 pragma Assert (Result = 0);
671 return To_Duration (TS);
678 function RT_Resolution return Duration is
687 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
688 pragma Unreferenced (Reason);
689 Result : Interfaces.C.int;
691 Result := pthread_cond_signal (T.Common.LL.CV'Access);
692 pragma Assert (Result = 0);
699 procedure Yield (Do_Yield : Boolean := True) is
700 Result : Interfaces.C.int;
701 pragma Unreferenced (Result);
704 Result := sched_yield;
712 procedure Set_Priority
714 Prio : System.Any_Priority;
715 Loss_Of_Inheritance : Boolean := False)
717 pragma Unreferenced (Loss_Of_Inheritance);
719 Result : Interfaces.C.int;
720 Param : aliased struct_sched_param;
722 function Get_Policy (Prio : System.Any_Priority) return Character;
723 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
724 -- Get priority specific dispatching policy
726 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
727 -- Upper case first character of the policy name corresponding to the
728 -- task as set by a Priority_Specific_Dispatching pragma.
731 T.Common.Current_Priority := Prio;
732 Param.sched_priority := To_Target_Priority (Prio);
734 if Time_Slice_Supported
735 and then (Dispatching_Policy = 'R'
736 or else Priority_Specific_Policy = 'R'
737 or else Time_Slice_Val > 0)
739 Result := pthread_setschedparam
740 (T.Common.LL.Thread, SCHED_RR, Param'Access);
742 elsif Dispatching_Policy = 'F'
743 or else Priority_Specific_Policy = 'F'
744 or else Time_Slice_Val = 0
746 Result := pthread_setschedparam
747 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
750 Result := pthread_setschedparam
751 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
754 pragma Assert (Result = 0);
761 function Get_Priority (T : Task_Id) return System.Any_Priority is
763 return T.Common.Current_Priority;
770 procedure Enter_Task (Self_ID : Task_Id) is
772 Self_ID.Common.LL.Thread := pthread_self;
773 Self_ID.Common.LL.LWP := lwp_self;
775 Specific.Set (Self_ID);
777 if Use_Alternate_Stack then
779 Stack : aliased stack_t;
780 Result : Interfaces.C.int;
782 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
783 Stack.ss_size := Alternate_Stack_Size;
785 Result := sigaltstack (Stack'Access, null);
786 pragma Assert (Result = 0);
795 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
797 -----------------------------
798 -- Register_Foreign_Thread --
799 -----------------------------
801 function Register_Foreign_Thread return Task_Id is
803 if Is_Valid_Task then
806 return Register_Foreign_Thread (pthread_self);
808 end Register_Foreign_Thread;
814 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
815 Mutex_Attr : aliased pthread_mutexattr_t;
816 Result : Interfaces.C.int;
817 Cond_Attr : aliased pthread_condattr_t;
820 -- Give the task a unique serial number
822 Self_ID.Serial_Number := Next_Serial_Number;
823 Next_Serial_Number := Next_Serial_Number + 1;
824 pragma Assert (Next_Serial_Number /= 0);
826 if not Single_Lock then
827 Result := pthread_mutexattr_init (Mutex_Attr'Access);
828 pragma Assert (Result = 0 or else Result = ENOMEM);
831 if Locking_Policy = 'C' then
833 pthread_mutexattr_setprotocol
835 PTHREAD_PRIO_PROTECT);
836 pragma Assert (Result = 0);
839 pthread_mutexattr_setprioceiling
841 Interfaces.C.int (System.Any_Priority'Last));
842 pragma Assert (Result = 0);
844 elsif Locking_Policy = 'I' then
846 pthread_mutexattr_setprotocol
848 PTHREAD_PRIO_INHERIT);
849 pragma Assert (Result = 0);
854 (Self_ID.Common.LL.L'Access,
856 pragma Assert (Result = 0 or else Result = ENOMEM);
864 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
865 pragma Assert (Result = 0);
868 Result := pthread_condattr_init (Cond_Attr'Access);
869 pragma Assert (Result = 0 or else Result = ENOMEM);
874 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
875 pragma Assert (Result = 0 or else Result = ENOMEM);
881 if not Single_Lock then
882 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
883 pragma Assert (Result = 0);
889 Result := pthread_condattr_destroy (Cond_Attr'Access);
890 pragma Assert (Result = 0);
897 procedure Create_Task
899 Wrapper : System.Address;
900 Stack_Size : System.Parameters.Size_Type;
901 Priority : System.Any_Priority;
902 Succeeded : out Boolean)
904 Attributes : aliased pthread_attr_t;
905 Adjusted_Stack_Size : Interfaces.C.size_t;
906 Page_Size : constant Interfaces.C.size_t := Get_Page_Size;
907 Result : Interfaces.C.int;
909 function Thread_Body_Access is new
910 Ada.Unchecked_Conversion (System.Address, Thread_Body);
912 use System.Task_Info;
915 Adjusted_Stack_Size :=
916 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
918 if Stack_Base_Available then
920 -- If Stack Checking is supported then allocate 2 additional pages:
922 -- In the worst case, stack is allocated at something like
923 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
924 -- to be sure the effective stack size is greater than what
927 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
930 -- Round stack size as this is required by some OSes (Darwin)
932 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
933 Adjusted_Stack_Size :=
934 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
936 Result := pthread_attr_init (Attributes'Access);
937 pragma Assert (Result = 0 or else Result = ENOMEM);
945 pthread_attr_setdetachstate
946 (Attributes'Access, PTHREAD_CREATE_DETACHED);
947 pragma Assert (Result = 0);
950 pthread_attr_setstacksize
951 (Attributes'Access, Adjusted_Stack_Size);
952 pragma Assert (Result = 0);
954 if T.Common.Task_Info /= Default_Scope then
955 case T.Common.Task_Info is
956 when System.Task_Info.Process_Scope =>
958 pthread_attr_setscope
959 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
961 when System.Task_Info.System_Scope =>
963 pthread_attr_setscope
964 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
966 when System.Task_Info.Default_Scope =>
970 pragma Assert (Result = 0);
973 -- Since the initial signal mask of a thread is inherited from the
974 -- creator, and the Environment task has all its signals masked, we
975 -- do not need to manipulate caller's signal mask at this point.
976 -- All tasks in RTS will have All_Tasks_Mask initially.
978 Result := pthread_create
979 (T.Common.LL.Thread'Access,
981 Thread_Body_Access (Wrapper),
983 pragma Assert (Result = 0 or else Result = EAGAIN);
985 Succeeded := Result = 0;
987 Result := pthread_attr_destroy (Attributes'Access);
988 pragma Assert (Result = 0);
991 Set_Priority (T, Priority);
999 procedure Finalize_TCB (T : Task_Id) is
1000 Result : Interfaces.C.int;
1003 if not Single_Lock then
1004 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1005 pragma Assert (Result = 0);
1008 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1009 pragma Assert (Result = 0);
1011 if T.Known_Tasks_Index /= -1 then
1012 Known_Tasks (T.Known_Tasks_Index) := null;
1015 ATCB_Allocation.Free_ATCB (T);
1022 procedure Exit_Task is
1024 -- Mark this task as unknown, so that if Self is called, it won't
1025 -- return a dangling pointer.
1027 Specific.Set (null);
1034 procedure Abort_Task (T : Task_Id) is
1035 Result : Interfaces.C.int;
1037 if Abort_Handler_Installed then
1040 (T.Common.LL.Thread,
1041 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1042 pragma Assert (Result = 0);
1050 procedure Initialize (S : in out Suspension_Object) is
1051 Mutex_Attr : aliased pthread_mutexattr_t;
1052 Cond_Attr : aliased pthread_condattr_t;
1053 Result : Interfaces.C.int;
1056 -- Initialize internal state (always to False (RM D.10 (6)))
1061 -- Initialize internal mutex
1063 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1064 pragma Assert (Result = 0 or else Result = ENOMEM);
1066 if Result = ENOMEM then
1067 raise Storage_Error;
1070 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1071 pragma Assert (Result = 0 or else Result = ENOMEM);
1073 if Result = ENOMEM then
1074 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1075 pragma Assert (Result = 0);
1077 raise Storage_Error;
1080 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1081 pragma Assert (Result = 0);
1083 -- Initialize internal condition variable
1085 Result := pthread_condattr_init (Cond_Attr'Access);
1086 pragma Assert (Result = 0 or else Result = ENOMEM);
1089 Result := pthread_mutex_destroy (S.L'Access);
1090 pragma Assert (Result = 0);
1092 if Result = ENOMEM then
1093 raise Storage_Error;
1097 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1098 pragma Assert (Result = 0 or else Result = ENOMEM);
1101 Result := pthread_mutex_destroy (S.L'Access);
1102 pragma Assert (Result = 0);
1104 if Result = ENOMEM then
1105 Result := pthread_condattr_destroy (Cond_Attr'Access);
1106 pragma Assert (Result = 0);
1107 raise Storage_Error;
1111 Result := pthread_condattr_destroy (Cond_Attr'Access);
1112 pragma Assert (Result = 0);
1119 procedure Finalize (S : in out Suspension_Object) is
1120 Result : Interfaces.C.int;
1123 -- Destroy internal mutex
1125 Result := pthread_mutex_destroy (S.L'Access);
1126 pragma Assert (Result = 0);
1128 -- Destroy internal condition variable
1130 Result := pthread_cond_destroy (S.CV'Access);
1131 pragma Assert (Result = 0);
1138 function Current_State (S : Suspension_Object) return Boolean is
1140 -- We do not want to use lock on this read operation. State is marked
1141 -- as Atomic so that we ensure that the value retrieved is correct.
1150 procedure Set_False (S : in out Suspension_Object) is
1151 Result : Interfaces.C.int;
1154 SSL.Abort_Defer.all;
1156 Result := pthread_mutex_lock (S.L'Access);
1157 pragma Assert (Result = 0);
1161 Result := pthread_mutex_unlock (S.L'Access);
1162 pragma Assert (Result = 0);
1164 SSL.Abort_Undefer.all;
1171 procedure Set_True (S : in out Suspension_Object) is
1172 Result : Interfaces.C.int;
1175 SSL.Abort_Defer.all;
1177 Result := pthread_mutex_lock (S.L'Access);
1178 pragma Assert (Result = 0);
1180 -- If there is already a task waiting on this suspension object then
1181 -- we resume it, leaving the state of the suspension object to False,
1182 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1183 -- the state to True.
1189 Result := pthread_cond_signal (S.CV'Access);
1190 pragma Assert (Result = 0);
1196 Result := pthread_mutex_unlock (S.L'Access);
1197 pragma Assert (Result = 0);
1199 SSL.Abort_Undefer.all;
1202 ------------------------
1203 -- Suspend_Until_True --
1204 ------------------------
1206 procedure Suspend_Until_True (S : in out Suspension_Object) is
1207 Result : Interfaces.C.int;
1210 SSL.Abort_Defer.all;
1212 Result := pthread_mutex_lock (S.L'Access);
1213 pragma Assert (Result = 0);
1217 -- Program_Error must be raised upon calling Suspend_Until_True
1218 -- if another task is already waiting on that suspension object
1221 Result := pthread_mutex_unlock (S.L'Access);
1222 pragma Assert (Result = 0);
1224 SSL.Abort_Undefer.all;
1226 raise Program_Error;
1229 -- Suspend the task if the state is False. Otherwise, the task
1230 -- continues its execution, and the state of the suspension object
1231 -- is set to False (ARM D.10 par. 9).
1239 -- Loop in case pthread_cond_wait returns earlier than expected
1240 -- (e.g. in case of EINTR caused by a signal).
1242 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1243 pragma Assert (Result = 0 or else Result = EINTR);
1245 exit when not S.Waiting;
1249 Result := pthread_mutex_unlock (S.L'Access);
1250 pragma Assert (Result = 0);
1252 SSL.Abort_Undefer.all;
1254 end Suspend_Until_True;
1262 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1263 pragma Unreferenced (Self_ID);
1268 --------------------
1269 -- Check_No_Locks --
1270 --------------------
1272 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1273 pragma Unreferenced (Self_ID);
1278 ----------------------
1279 -- Environment_Task --
1280 ----------------------
1282 function Environment_Task return Task_Id is
1284 return Environment_Task_Id;
1285 end Environment_Task;
1291 procedure Lock_RTS is
1293 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1300 procedure Unlock_RTS is
1302 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1309 function Suspend_Task
1311 Thread_Self : Thread_Id) return Boolean
1313 pragma Unreferenced (T, Thread_Self);
1322 function Resume_Task
1324 Thread_Self : Thread_Id) return Boolean
1326 pragma Unreferenced (T, Thread_Self);
1331 --------------------
1332 -- Stop_All_Tasks --
1333 --------------------
1335 procedure Stop_All_Tasks is
1344 function Stop_Task (T : ST.Task_Id) return Boolean is
1345 pragma Unreferenced (T);
1354 function Continue_Task (T : ST.Task_Id) return Boolean is
1355 pragma Unreferenced (T);
1364 procedure Initialize (Environment_Task : Task_Id) is
1365 act : aliased struct_sigaction;
1366 old_act : aliased struct_sigaction;
1367 Tmp_Set : aliased sigset_t;
1368 Result : Interfaces.C.int;
1371 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1372 pragma Import (C, State, "__gnat_get_interrupt_state");
1373 -- Get interrupt state. Defined in a-init.c
1374 -- The input argument is the interrupt number,
1375 -- and the result is one of the following:
1377 Default : constant Character := 's';
1378 -- 'n' this interrupt not set by any Interrupt_State pragma
1379 -- 'u' Interrupt_State pragma set state to User
1380 -- 'r' Interrupt_State pragma set state to Runtime
1381 -- 's' Interrupt_State pragma set state to System (use "default"
1385 Environment_Task_Id := Environment_Task;
1387 Interrupt_Management.Initialize;
1389 -- Prepare the set of signals that should unblocked in all tasks
1391 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1392 pragma Assert (Result = 0);
1394 for J in Interrupt_Management.Interrupt_ID loop
1395 if System.Interrupt_Management.Keep_Unmasked (J) then
1396 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1397 pragma Assert (Result = 0);
1401 -- Initialize the lock used to synchronize chain of all ATCBs
1403 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1405 Specific.Initialize (Environment_Task);
1407 if Use_Alternate_Stack then
1408 Environment_Task.Common.Task_Alternate_Stack :=
1409 Alternate_Stack'Address;
1412 -- Make environment task known here because it doesn't go through
1413 -- Activate_Tasks, which does it for all other tasks.
1415 Known_Tasks (Known_Tasks'First) := Environment_Task;
1416 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1418 Enter_Task (Environment_Task);
1421 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1424 act.sa_handler := Abort_Handler'Address;
1426 Result := sigemptyset (Tmp_Set'Access);
1427 pragma Assert (Result = 0);
1428 act.sa_mask := Tmp_Set;
1432 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1433 act'Unchecked_Access,
1434 old_act'Unchecked_Access);
1435 pragma Assert (Result = 0);
1436 Abort_Handler_Installed := True;
1440 -----------------------
1441 -- Set_Task_Affinity --
1442 -----------------------
1444 procedure Set_Task_Affinity (T : ST.Task_Id) is
1445 pragma Unreferenced (T);
1448 -- Setting task affinity is not supported by the underlying system
1451 end Set_Task_Affinity;
1453 end System.Task_Primitives.Operations;