1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2001, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 DEC Unix 4.0d version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.Task_Info;
47 -- used for Task_Info_Type
50 -- used for Shift_Left
56 with System.Interrupt_Management;
57 -- used for Keep_Unmasked
58 -- Abort_Task_Interrupt
61 with System.Interrupt_Management.Operations;
62 -- used for Set_Interrupt_Mask
64 pragma Elaborate_All (System.Interrupt_Management.Operations);
66 with System.Parameters;
70 -- used for Ada_Task_Control_Block
72 -- ATCB components and types
74 with System.Soft_Links;
75 -- used for Defer/Undefer_Abort
77 -- Note that we do not use System.Tasking.Initialization directly since
78 -- this is a higher level package that we shouldn't depend on. For example
79 -- when using the restricted run time, it is replaced by
80 -- System.Tasking.Restricted.Initialization
82 with System.OS_Primitives;
83 -- used for Delay_Modes
85 with Unchecked_Conversion;
86 with Unchecked_Deallocation;
88 package body System.Task_Primitives.Operations is
90 use System.Tasking.Debug;
93 use System.OS_Interface;
94 use System.Parameters;
95 use System.OS_Primitives;
97 package SSL renames System.Soft_Links;
103 -- The followings are logically constants, but need to be initialized
106 Single_RTS_Lock : aliased RTS_Lock;
107 -- This is a lock to allow only one thread of control in the RTS at
108 -- a time; it is used to execute in mutual exclusion from all other tasks.
109 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
111 Environment_Task_ID : Task_ID;
112 -- A variable to hold Task_ID for the environment task.
114 Unblocked_Signal_Mask : aliased sigset_t;
115 -- The set of signals that should unblocked in all tasks
117 Time_Slice_Val : Integer;
118 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
120 Locking_Policy : Character;
121 pragma Import (C, Locking_Policy, "__gl_locking_policy");
123 Dispatching_Policy : Character;
124 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
126 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
127 -- Indicates whether FIFO_Within_Priorities is set.
131 -----------------------
132 -- Local Subprograms --
133 -----------------------
135 procedure Abort_Handler (Sig : Signal);
137 function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
139 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
147 procedure Initialize (Environment_Task : Task_ID);
148 pragma Inline (Initialize);
149 -- Initialize various data needed by this package.
151 procedure Set (Self_Id : Task_ID);
153 -- Set the self id for the current task.
155 function Self return Task_ID;
156 pragma Inline (Self);
157 -- Return a pointer to the Ada Task Control Block of the calling task.
161 package body Specific is separate;
162 -- The body of this package is target specific.
168 procedure Abort_Handler (Sig : Signal) is
169 T : constant Task_ID := Self;
170 Result : Interfaces.C.int;
171 Old_Set : aliased sigset_t;
174 if T.Deferral_Level = 0
175 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
180 -- Make sure signals used for RTS internal purpose are unmasked
182 Result := pthread_sigmask (SIG_UNBLOCK,
183 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
184 pragma Assert (Result = 0);
186 raise Standard'Abort_Signal;
194 -- The underlying thread system sets a guard page at the
195 -- bottom of a thread stack, so nothing is needed.
197 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
206 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
208 return T.Common.LL.Thread;
215 function Self return Task_ID renames Specific.Self;
217 ---------------------
218 -- Initialize_Lock --
219 ---------------------
221 -- Note: mutexes and cond_variables needed per-task basis are
222 -- initialized in Initialize_TCB and the Storage_Error is
223 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
224 -- used in RTS is initialized before any status change of RTS.
225 -- Therefore rasing Storage_Error in the following routines
226 -- should be able to be handled safely.
228 procedure Initialize_Lock
229 (Prio : System.Any_Priority;
232 Attributes : aliased pthread_mutexattr_t;
233 Result : Interfaces.C.int;
236 Result := pthread_mutexattr_init (Attributes'Access);
237 pragma Assert (Result = 0 or else Result = ENOMEM);
239 if Result = ENOMEM then
243 if Locking_Policy = 'C' then
244 L.Ceiling := Interfaces.C.int (Prio);
247 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
248 pragma Assert (Result = 0 or else Result = ENOMEM);
250 if Result = ENOMEM then
251 Result := pthread_mutexattr_destroy (Attributes'Access);
255 Result := pthread_mutexattr_destroy (Attributes'Access);
256 pragma Assert (Result = 0);
259 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
260 Attributes : aliased pthread_mutexattr_t;
261 Result : Interfaces.C.int;
264 Result := pthread_mutexattr_init (Attributes'Access);
265 pragma Assert (Result = 0 or else Result = ENOMEM);
267 if Result = ENOMEM then
271 Result := pthread_mutex_init (L, Attributes'Access);
272 pragma Assert (Result = 0 or else Result = ENOMEM);
274 if Result = ENOMEM then
275 Result := pthread_mutexattr_destroy (Attributes'Access);
279 Result := pthread_mutexattr_destroy (Attributes'Access);
280 pragma Assert (Result = 0);
287 procedure Finalize_Lock (L : access Lock) is
288 Result : Interfaces.C.int;
290 Result := pthread_mutex_destroy (L.L'Access);
291 pragma Assert (Result = 0);
294 procedure Finalize_Lock (L : access RTS_Lock) is
295 Result : Interfaces.C.int;
297 Result := pthread_mutex_destroy (L);
298 pragma Assert (Result = 0);
305 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
306 Result : Interfaces.C.int;
308 All_Tasks_Link : Task_ID;
309 Current_Prio : System.Any_Priority;
312 -- Perform ceiling checks only when this is the locking policy in use.
314 if Locking_Policy = 'C' then
316 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
317 Current_Prio := Get_Priority (Self_ID);
319 -- If there is no other task, no need to check priorities
321 if All_Tasks_Link /= Null_Task
322 and then L.Ceiling < Interfaces.C.int (Current_Prio)
324 Ceiling_Violation := True;
329 Result := pthread_mutex_lock (L.L'Access);
330 pragma Assert (Result = 0);
332 Ceiling_Violation := False;
336 (L : access RTS_Lock; Global_Lock : Boolean := False)
338 Result : Interfaces.C.int;
340 if not Single_Lock or else Global_Lock then
341 Result := pthread_mutex_lock (L);
342 pragma Assert (Result = 0);
346 procedure Write_Lock (T : Task_ID) is
347 Result : Interfaces.C.int;
349 if not Single_Lock then
350 Result := pthread_mutex_lock (T.Common.LL.L'Access);
351 pragma Assert (Result = 0);
359 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
361 Write_Lock (L, Ceiling_Violation);
368 procedure Unlock (L : access Lock) is
369 Result : Interfaces.C.int;
371 Result := pthread_mutex_unlock (L.L'Access);
372 pragma Assert (Result = 0);
375 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
376 Result : Interfaces.C.int;
378 if not Single_Lock or else Global_Lock then
379 Result := pthread_mutex_unlock (L);
380 pragma Assert (Result = 0);
384 procedure Unlock (T : Task_ID) is
385 Result : Interfaces.C.int;
387 if not Single_Lock then
388 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
389 pragma Assert (Result = 0);
399 Reason : System.Tasking.Task_States)
401 Result : Interfaces.C.int;
404 Result := pthread_cond_wait
405 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
407 Result := pthread_cond_wait
408 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
411 -- EINTR is not considered a failure.
413 pragma Assert (Result = 0 or else Result = EINTR);
420 -- This is for use within the run-time system, so abort is
421 -- assumed to be already deferred, and the caller should be
422 -- holding its own ATCB lock.
424 procedure Timed_Sleep
427 Mode : ST.Delay_Modes;
428 Reason : System.Tasking.Task_States;
429 Timedout : out Boolean;
430 Yielded : out Boolean)
432 Check_Time : constant Duration := Monotonic_Clock;
434 Request : aliased timespec;
435 Result : Interfaces.C.int;
441 if Mode = Relative then
442 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
444 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
447 if Abs_Time > Check_Time then
448 Request := To_Timespec (Abs_Time);
451 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
452 or else Self_ID.Pending_Priority_Change;
455 Result := pthread_cond_timedwait
456 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
460 Result := pthread_cond_timedwait
461 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
465 exit when Abs_Time <= Monotonic_Clock;
467 if Result = 0 or Result = EINTR then
468 -- somebody may have called Wakeup for us
473 pragma Assert (Result = ETIMEDOUT);
482 -- This is for use in implementing delay statements, so
483 -- we assume the caller is abort-deferred but is holding
486 procedure Timed_Delay
489 Mode : ST.Delay_Modes)
491 Check_Time : constant Duration := Monotonic_Clock;
493 Request : aliased timespec;
494 Result : Interfaces.C.int;
497 -- Only the little window between deferring abort and
498 -- locking Self_ID is the reason we need to
499 -- check for pending abort and priority change below! :(
507 Write_Lock (Self_ID);
509 if Mode = Relative then
510 Abs_Time := Time + Check_Time;
512 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
515 if Abs_Time > Check_Time then
516 Request := To_Timespec (Abs_Time);
517 Self_ID.Common.State := Delay_Sleep;
520 if Self_ID.Pending_Priority_Change then
521 Self_ID.Pending_Priority_Change := False;
522 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
523 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
526 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
529 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
530 Single_RTS_Lock'Access, Request'Access);
532 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
533 Self_ID.Common.LL.L'Access, Request'Access);
536 exit when Abs_Time <= Monotonic_Clock;
538 pragma Assert (Result = 0 or else
539 Result = ETIMEDOUT or else
543 Self_ID.Common.State := Runnable;
553 SSL.Abort_Undefer.all;
556 ---------------------
557 -- Monotonic_Clock --
558 ---------------------
560 function Monotonic_Clock return Duration is
561 TS : aliased timespec;
562 Result : Interfaces.C.int;
565 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
566 pragma Assert (Result = 0);
567 return To_Duration (TS);
574 function RT_Resolution return Duration is
576 return 1.0 / 1024.0; -- Clock on DEC Alpha ticks at 1024 Hz
583 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
584 Result : Interfaces.C.int;
586 Result := pthread_cond_signal (T.Common.LL.CV'Access);
587 pragma Assert (Result = 0);
594 procedure Yield (Do_Yield : Boolean := True) is
595 Result : Interfaces.C.int;
598 Result := sched_yield;
606 procedure Set_Priority
608 Prio : System.Any_Priority;
609 Loss_Of_Inheritance : Boolean := False)
611 Result : Interfaces.C.int;
612 Param : aliased struct_sched_param;
615 T.Common.Current_Priority := Prio;
616 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
618 if Time_Slice_Val > 0 then
619 Result := pthread_setschedparam
620 (T.Common.LL.Thread, SCHED_RR, Param'Access);
622 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
623 Result := pthread_setschedparam
624 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
627 Result := pthread_setschedparam
628 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
631 pragma Assert (Result = 0);
638 function Get_Priority (T : Task_ID) return System.Any_Priority is
640 return T.Common.Current_Priority;
647 procedure Enter_Task (Self_ID : Task_ID) is
649 Self_ID.Common.LL.Thread := pthread_self;
650 Specific.Set (Self_ID);
654 for J in Known_Tasks'Range loop
655 if Known_Tasks (J) = null then
656 Known_Tasks (J) := Self_ID;
657 Self_ID.Known_Tasks_Index := J;
669 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
671 return new Ada_Task_Control_Block (Entry_Num);
678 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
679 Mutex_Attr : aliased pthread_mutexattr_t;
680 Result : Interfaces.C.int;
681 Cond_Attr : aliased pthread_condattr_t;
684 if not Single_Lock then
685 Result := pthread_mutexattr_init (Mutex_Attr'Access);
686 pragma Assert (Result = 0 or else Result = ENOMEM);
689 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
691 pragma Assert (Result = 0 or else Result = ENOMEM);
699 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
700 pragma Assert (Result = 0);
703 Result := pthread_condattr_init (Cond_Attr'Access);
704 pragma Assert (Result = 0 or else Result = ENOMEM);
707 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
709 pragma Assert (Result = 0 or else Result = ENOMEM);
715 if not Single_Lock then
716 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
717 pragma Assert (Result = 0);
723 Result := pthread_condattr_destroy (Cond_Attr'Access);
724 pragma Assert (Result = 0);
731 procedure Create_Task
733 Wrapper : System.Address;
734 Stack_Size : System.Parameters.Size_Type;
735 Priority : System.Any_Priority;
736 Succeeded : out Boolean)
738 Attributes : aliased pthread_attr_t;
739 Adjusted_Stack_Size : Interfaces.C.size_t;
740 Result : Interfaces.C.int;
741 Param : aliased System.OS_Interface.struct_sched_param;
743 function Thread_Body_Access is new
744 Unchecked_Conversion (System.Address, Thread_Body);
746 use System.Task_Info;
749 if Stack_Size = Unspecified_Size then
750 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
752 elsif Stack_Size < Minimum_Stack_Size then
753 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
756 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
759 Result := pthread_attr_init (Attributes'Access);
760 pragma Assert (Result = 0 or else Result = ENOMEM);
767 Result := pthread_attr_setdetachstate
768 (Attributes'Access, PTHREAD_CREATE_DETACHED);
769 pragma Assert (Result = 0);
771 Result := pthread_attr_setstacksize
772 (Attributes'Access, Adjusted_Stack_Size);
773 pragma Assert (Result = 0);
775 -- Set the scheduling parameters explicitly, since this is the only
776 -- way to force the OS to take the scope attribute into account
778 Result := pthread_attr_setinheritsched
779 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
780 pragma Assert (Result = 0);
782 Param.sched_priority :=
783 Interfaces.C.int (Underlying_Priorities (Priority));
784 Result := pthread_attr_setschedparam
785 (Attributes'Access, Param'Access);
786 pragma Assert (Result = 0);
788 if Time_Slice_Val > 0 then
789 Result := pthread_attr_setschedpolicy
790 (Attributes'Access, System.OS_Interface.SCHED_RR);
792 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
793 Result := pthread_attr_setschedpolicy
794 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
797 Result := pthread_attr_setschedpolicy
798 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
801 pragma Assert (Result = 0);
803 T.Common.Current_Priority := Priority;
805 if T.Common.Task_Info /= null then
806 case T.Common.Task_Info.Contention_Scope is
807 when System.Task_Info.Process_Scope =>
808 Result := pthread_attr_setscope
809 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
811 when System.Task_Info.System_Scope =>
812 Result := pthread_attr_setscope
813 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
815 when System.Task_Info.Default_Scope =>
819 pragma Assert (Result = 0);
822 -- Since the initial signal mask of a thread is inherited from the
823 -- creator, and the Environment task has all its signals masked, we
824 -- do not need to manipulate caller's signal mask at this point.
825 -- All tasks in RTS will have All_Tasks_Mask initially.
827 Result := pthread_create
828 (T.Common.LL.Thread'Access,
830 Thread_Body_Access (Wrapper),
832 pragma Assert (Result = 0 or else Result = EAGAIN);
834 Succeeded := Result = 0;
836 Result := pthread_attr_destroy (Attributes'Access);
837 pragma Assert (Result = 0);
839 if T.Common.Task_Info /= null then
840 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
841 Result := bind_to_cpu (Curpid, 0);
842 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
843 Result := bind_to_cpu
845 Interfaces.C.unsigned_long (
846 Interfaces.Shift_Left
847 (Interfaces.Unsigned_64'(1),
848 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
849 pragma Assert (Result = 0);
858 procedure Finalize_TCB (T : Task_ID) is
859 Result : Interfaces.C.int;
862 procedure Free is new
863 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
866 if not Single_Lock then
867 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
868 pragma Assert (Result = 0);
871 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
872 pragma Assert (Result = 0);
874 if T.Known_Tasks_Index /= -1 then
875 Known_Tasks (T.Known_Tasks_Index) := null;
885 procedure Exit_Task is
887 pthread_exit (System.Null_Address);
894 procedure Abort_Task (T : Task_ID) is
895 Result : Interfaces.C.int;
898 Result := pthread_kill (T.Common.LL.Thread,
899 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
900 pragma Assert (Result = 0);
907 -- Dummy versions. The only currently working versions is for solaris
910 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
919 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
924 ----------------------
925 -- Environment_Task --
926 ----------------------
928 function Environment_Task return Task_ID is
930 return Environment_Task_ID;
931 end Environment_Task;
937 procedure Lock_RTS is
939 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
946 procedure Unlock_RTS is
948 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
955 function Suspend_Task
957 Thread_Self : Thread_Id) return Boolean is
968 Thread_Self : Thread_Id) return Boolean is
977 procedure Initialize (Environment_Task : Task_ID) is
978 act : aliased struct_sigaction;
979 old_act : aliased struct_sigaction;
980 Tmp_Set : aliased sigset_t;
981 Result : Interfaces.C.int;
984 Environment_Task_ID := Environment_Task;
986 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
987 -- Initialize the lock used to synchronize chain of all ATCBs.
989 Specific.Initialize (Environment_Task);
991 Enter_Task (Environment_Task);
993 -- Install the abort-signal handler
996 act.sa_handler := Abort_Handler'Address;
998 Result := sigemptyset (Tmp_Set'Access);
999 pragma Assert (Result = 0);
1000 act.sa_mask := Tmp_Set;
1004 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1005 act'Unchecked_Access,
1006 old_act'Unchecked_Access);
1007 pragma Assert (Result = 0);
1012 Result : Interfaces.C.int;
1014 -- Mask Environment task for all signals. The original mask of the
1015 -- Environment task will be recovered by Interrupt_Server task
1016 -- during the elaboration of s-interr.adb.
1018 System.Interrupt_Management.Operations.Set_Interrupt_Mask
1019 (System.Interrupt_Management.Operations.All_Tasks_Mask'Access);
1021 -- Prepare the set of signals that should unblocked in all tasks
1023 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1024 pragma Assert (Result = 0);
1026 for J in Interrupt_Management.Interrupt_ID loop
1027 if System.Interrupt_Management.Keep_Unmasked (J) then
1028 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1029 pragma Assert (Result = 0);
1035 end System.Task_Primitives.Operations;