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 --
11 -- Copyright (C) 1991-2001, Florida State University --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This is a DEC Unix 4.0d version of this package
39 -- This package contains all the GNULL primitives that interface directly
40 -- with the underlying OS.
43 -- Turn off polling, we do not want ATC polling to take place during
44 -- tasking operations. It causes infinite loops and other problems.
46 with System.Tasking.Debug;
47 -- used for Known_Tasks
49 with System.Task_Info;
50 -- used for Task_Info_Type
53 -- used for Shift_Left
59 with System.Interrupt_Management;
60 -- used for Keep_Unmasked
61 -- Abort_Task_Interrupt
64 with System.Interrupt_Management.Operations;
65 -- used for Set_Interrupt_Mask
67 pragma Elaborate_All (System.Interrupt_Management.Operations);
69 with System.Parameters;
73 -- used for Ada_Task_Control_Block
75 -- ATCB components and types
77 with System.Soft_Links;
78 -- used for Defer/Undefer_Abort
80 -- Note that we do not use System.Tasking.Initialization directly since
81 -- this is a higher level package that we shouldn't depend on. For example
82 -- when using the restricted run time, it is replaced by
83 -- System.Tasking.Restricted.Initialization
85 with System.OS_Primitives;
86 -- used for Delay_Modes
88 with Unchecked_Conversion;
89 with Unchecked_Deallocation;
91 package body System.Task_Primitives.Operations is
93 use System.Tasking.Debug;
96 use System.OS_Interface;
97 use System.Parameters;
98 use System.OS_Primitives;
100 package SSL renames System.Soft_Links;
106 -- The followings are logically constants, but need to be initialized
109 All_Tasks_L : aliased System.Task_Primitives.RTS_Lock;
110 -- See comments on locking rules in System.Tasking (spec).
112 Environment_Task_ID : Task_ID;
113 -- A variable to hold Task_ID for the environment task.
115 Unblocked_Signal_Mask : aliased sigset_t;
116 -- The set of signals that should unblocked in all tasks
118 Time_Slice_Val : Integer;
119 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
121 Locking_Policy : Character;
122 pragma Import (C, Locking_Policy, "__gl_locking_policy");
124 Dispatching_Policy : Character;
125 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
127 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
128 -- Indicates whether FIFO_Within_Priorities is set.
132 -----------------------
133 -- Local Subprograms --
134 -----------------------
136 procedure Abort_Handler (Sig : Signal);
138 function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
140 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
148 procedure Initialize (Environment_Task : Task_ID);
149 pragma Inline (Initialize);
150 -- Initialize various data needed by this package.
152 procedure Set (Self_Id : Task_ID);
154 -- Set the self id for the current task.
156 function Self return Task_ID;
157 pragma Inline (Self);
158 -- Return a pointer to the Ada Task Control Block of the calling task.
162 package body Specific is separate;
163 -- The body of this package is target specific.
169 procedure Abort_Handler (Sig : Signal) is
170 T : constant Task_ID := Self;
171 Result : Interfaces.C.int;
172 Old_Set : aliased sigset_t;
175 if T.Deferral_Level = 0
176 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
181 -- Make sure signals used for RTS internal purpose are unmasked
183 Result := pthread_sigmask (SIG_UNBLOCK,
184 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
185 pragma Assert (Result = 0);
187 raise Standard'Abort_Signal;
195 -- The underlying thread system sets a guard page at the
196 -- bottom of a thread stack, so nothing is needed.
198 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
207 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
209 return T.Common.LL.Thread;
216 function Self return Task_ID renames Specific.Self;
218 ---------------------
219 -- Initialize_Lock --
220 ---------------------
222 -- Note: mutexes and cond_variables needed per-task basis are
223 -- initialized in Intialize_TCB and the Storage_Error is
224 -- handled. Other mutexes (such as All_Tasks_Lock, Memory_Lock...)
225 -- used in RTS is initialized before any status change of RTS.
226 -- Therefore rasing Storage_Error in the following routines
227 -- should be able to be handled safely.
229 procedure Initialize_Lock
230 (Prio : System.Any_Priority;
233 Attributes : aliased pthread_mutexattr_t;
234 Result : Interfaces.C.int;
237 Result := pthread_mutexattr_init (Attributes'Access);
238 pragma Assert (Result = 0 or else Result = ENOMEM);
240 if Result = ENOMEM then
244 if Locking_Policy = 'C' then
245 L.Ceiling := Interfaces.C.int (Prio);
248 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
249 pragma Assert (Result = 0 or else Result = ENOMEM);
251 if Result = ENOMEM then
252 Result := pthread_mutexattr_destroy (Attributes'Access);
256 Result := pthread_mutexattr_destroy (Attributes'Access);
257 pragma Assert (Result = 0);
260 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
261 Attributes : aliased pthread_mutexattr_t;
262 Result : Interfaces.C.int;
265 Result := pthread_mutexattr_init (Attributes'Access);
266 pragma Assert (Result = 0 or else Result = ENOMEM);
268 if Result = ENOMEM then
272 Result := pthread_mutex_init (L, Attributes'Access);
273 pragma Assert (Result = 0 or else Result = ENOMEM);
275 if Result = ENOMEM then
276 Result := pthread_mutexattr_destroy (Attributes'Access);
280 Result := pthread_mutexattr_destroy (Attributes'Access);
281 pragma Assert (Result = 0);
288 procedure Finalize_Lock (L : access Lock) is
289 Result : Interfaces.C.int;
291 Result := pthread_mutex_destroy (L.L'Access);
292 pragma Assert (Result = 0);
295 procedure Finalize_Lock (L : access RTS_Lock) is
296 Result : Interfaces.C.int;
298 Result := pthread_mutex_destroy (L);
299 pragma Assert (Result = 0);
306 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
307 Result : Interfaces.C.int;
309 All_Tasks_Link : Task_ID;
310 Current_Prio : System.Any_Priority;
313 -- Perform ceiling checks only when this is the locking policy in use.
315 if Locking_Policy = 'C' then
317 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
318 Current_Prio := Get_Priority (Self_ID);
320 -- if there is no other task, no need to check priorities
321 if All_Tasks_Link /= Null_Task and then
322 L.Ceiling < Interfaces.C.int (Current_Prio) then
323 Ceiling_Violation := True;
328 Result := pthread_mutex_lock (L.L'Access);
330 pragma Assert (Result = 0);
332 Ceiling_Violation := False;
335 procedure Write_Lock (L : access RTS_Lock) is
336 Result : Interfaces.C.int;
338 Result := pthread_mutex_lock (L);
339 pragma Assert (Result = 0);
342 procedure Write_Lock (T : Task_ID) is
343 Result : Interfaces.C.int;
345 Result := pthread_mutex_lock (T.Common.LL.L'Access);
346 pragma Assert (Result = 0);
353 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
355 Write_Lock (L, Ceiling_Violation);
362 procedure Unlock (L : access Lock) is
363 Result : Interfaces.C.int;
365 Result := pthread_mutex_unlock (L.L'Access);
366 pragma Assert (Result = 0);
369 procedure Unlock (L : access RTS_Lock) is
370 Result : Interfaces.C.int;
372 Result := pthread_mutex_unlock (L);
373 pragma Assert (Result = 0);
376 procedure Unlock (T : Task_ID) is
377 Result : Interfaces.C.int;
379 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
380 pragma Assert (Result = 0);
389 Reason : System.Tasking.Task_States)
391 Result : Interfaces.C.int;
393 pragma Assert (Self_ID = Self);
394 Result := pthread_cond_wait
395 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
397 -- EINTR is not considered a failure.
399 pragma Assert (Result = 0 or else Result = EINTR);
406 -- This is for use within the run-time system, so abort is
407 -- assumed to be already deferred, and the caller should be
408 -- holding its own ATCB lock.
410 procedure Timed_Sleep
413 Mode : ST.Delay_Modes;
414 Reason : System.Tasking.Task_States;
415 Timedout : out Boolean;
416 Yielded : out Boolean)
418 Check_Time : constant Duration := Monotonic_Clock;
420 Request : aliased timespec;
421 Result : Interfaces.C.int;
427 if Mode = Relative then
428 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
430 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
433 if Abs_Time > Check_Time then
434 Request := To_Timespec (Abs_Time);
437 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
438 or else Self_ID.Pending_Priority_Change;
440 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
441 Self_ID.Common.LL.L'Access, Request'Access);
443 exit when Abs_Time <= Monotonic_Clock;
445 if Result = 0 or Result = EINTR then
446 -- somebody may have called Wakeup for us
451 pragma Assert (Result = ETIMEDOUT);
460 -- This is for use in implementing delay statements, so
461 -- we assume the caller is abort-deferred but is holding
464 procedure Timed_Delay
467 Mode : ST.Delay_Modes)
469 Check_Time : constant Duration := Monotonic_Clock;
471 Request : aliased timespec;
472 Result : Interfaces.C.int;
475 -- Only the little window between deferring abort and
476 -- locking Self_ID is the reason we need to
477 -- check for pending abort and priority change below! :(
480 Write_Lock (Self_ID);
482 if Mode = Relative then
483 Abs_Time := Time + Check_Time;
485 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
488 if Abs_Time > Check_Time then
489 Request := To_Timespec (Abs_Time);
490 Self_ID.Common.State := Delay_Sleep;
493 if Self_ID.Pending_Priority_Change then
494 Self_ID.Pending_Priority_Change := False;
495 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
496 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
499 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
501 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
502 Self_ID.Common.LL.L'Access, Request'Access);
504 exit when Abs_Time <= Monotonic_Clock;
506 pragma Assert (Result = 0 or else
507 Result = ETIMEDOUT or else
511 Self_ID.Common.State := Runnable;
516 SSL.Abort_Undefer.all;
519 ---------------------
520 -- Monotonic_Clock --
521 ---------------------
523 function Monotonic_Clock return Duration is
524 TS : aliased timespec;
525 Result : Interfaces.C.int;
528 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
529 pragma Assert (Result = 0);
530 return To_Duration (TS);
537 function RT_Resolution return Duration is
539 return 1.0 / 1024.0; -- Clock on DEC Alpha ticks at 1024 Hz
546 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
547 Result : Interfaces.C.int;
549 Result := pthread_cond_signal (T.Common.LL.CV'Access);
550 pragma Assert (Result = 0);
557 procedure Yield (Do_Yield : Boolean := True) is
558 Result : Interfaces.C.int;
561 Result := sched_yield;
569 procedure Set_Priority
571 Prio : System.Any_Priority;
572 Loss_Of_Inheritance : Boolean := False)
574 Result : Interfaces.C.int;
575 Param : aliased struct_sched_param;
578 T.Common.Current_Priority := Prio;
579 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
581 if Time_Slice_Val > 0 then
582 Result := pthread_setschedparam
583 (T.Common.LL.Thread, SCHED_RR, Param'Access);
585 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
586 Result := pthread_setschedparam
587 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
590 Result := pthread_setschedparam
591 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
594 pragma Assert (Result = 0);
601 function Get_Priority (T : Task_ID) return System.Any_Priority is
603 return T.Common.Current_Priority;
610 procedure Enter_Task (Self_ID : Task_ID) is
612 Self_ID.Common.LL.Thread := pthread_self;
613 Specific.Set (Self_ID);
617 for J in Known_Tasks'Range loop
618 if Known_Tasks (J) = null then
619 Known_Tasks (J) := Self_ID;
620 Self_ID.Known_Tasks_Index := J;
625 Unlock_All_Tasks_List;
632 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
634 return new Ada_Task_Control_Block (Entry_Num);
641 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
642 Mutex_Attr : aliased pthread_mutexattr_t;
643 Result : Interfaces.C.int;
644 Cond_Attr : aliased pthread_condattr_t;
647 Result := pthread_mutexattr_init (Mutex_Attr'Access);
648 pragma Assert (Result = 0 or else Result = ENOMEM);
655 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
657 pragma Assert (Result = 0 or else Result = ENOMEM);
664 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
665 pragma Assert (Result = 0);
667 Result := pthread_condattr_init (Cond_Attr'Access);
668 pragma Assert (Result = 0 or else Result = ENOMEM);
671 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
672 pragma Assert (Result = 0);
677 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
679 pragma Assert (Result = 0 or else Result = ENOMEM);
684 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
685 pragma Assert (Result = 0);
689 Result := pthread_condattr_destroy (Cond_Attr'Access);
690 pragma Assert (Result = 0);
697 procedure Create_Task
699 Wrapper : System.Address;
700 Stack_Size : System.Parameters.Size_Type;
701 Priority : System.Any_Priority;
702 Succeeded : out Boolean)
704 Attributes : aliased pthread_attr_t;
705 Adjusted_Stack_Size : Interfaces.C.size_t;
706 Result : Interfaces.C.int;
707 Param : aliased System.OS_Interface.struct_sched_param;
709 function Thread_Body_Access is new
710 Unchecked_Conversion (System.Address, Thread_Body);
712 use System.Task_Info;
715 if Stack_Size = Unspecified_Size then
716 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
718 elsif Stack_Size < Minimum_Stack_Size then
719 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
722 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
725 Result := pthread_attr_init (Attributes'Access);
726 pragma Assert (Result = 0 or else Result = ENOMEM);
733 Result := pthread_attr_setdetachstate
734 (Attributes'Access, PTHREAD_CREATE_DETACHED);
735 pragma Assert (Result = 0);
737 Result := pthread_attr_setstacksize
738 (Attributes'Access, Adjusted_Stack_Size);
739 pragma Assert (Result = 0);
741 -- Set the scheduling parameters explicitely, since this is the only
742 -- way to force the OS to take the scope attribute into account
744 Result := pthread_attr_setinheritsched
745 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
746 pragma Assert (Result = 0);
748 Param.sched_priority :=
749 Interfaces.C.int (Underlying_Priorities (Priority));
750 Result := pthread_attr_setschedparam
751 (Attributes'Access, Param'Access);
752 pragma Assert (Result = 0);
754 if Time_Slice_Val > 0 then
755 Result := pthread_attr_setschedpolicy
756 (Attributes'Access, System.OS_Interface.SCHED_RR);
758 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
759 Result := pthread_attr_setschedpolicy
760 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
763 Result := pthread_attr_setschedpolicy
764 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
767 pragma Assert (Result = 0);
769 T.Common.Current_Priority := Priority;
771 if T.Common.Task_Info /= null then
772 case T.Common.Task_Info.Contention_Scope is
773 when System.Task_Info.Process_Scope =>
774 Result := pthread_attr_setscope
775 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
777 when System.Task_Info.System_Scope =>
778 Result := pthread_attr_setscope
779 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
781 when System.Task_Info.Default_Scope =>
785 pragma Assert (Result = 0);
788 -- Since the initial signal mask of a thread is inherited from the
789 -- creator, and the Environment task has all its signals masked, we
790 -- do not need to manipulate caller's signal mask at this point.
791 -- All tasks in RTS will have All_Tasks_Mask initially.
793 Result := pthread_create
794 (T.Common.LL.Thread'Access,
796 Thread_Body_Access (Wrapper),
798 pragma Assert (Result = 0 or else Result = EAGAIN);
800 Succeeded := Result = 0;
802 Result := pthread_attr_destroy (Attributes'Access);
803 pragma Assert (Result = 0);
805 if T.Common.Task_Info /= null then
806 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
807 Result := bind_to_cpu (Curpid, 0);
808 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
809 Result := bind_to_cpu
811 Interfaces.C.unsigned_long (
812 Interfaces.Shift_Left
813 (Interfaces.Unsigned_64'(1),
814 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
815 pragma Assert (Result = 0);
824 procedure Finalize_TCB (T : Task_ID) is
825 Result : Interfaces.C.int;
828 procedure Free is new
829 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
832 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
833 pragma Assert (Result = 0);
834 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
835 pragma Assert (Result = 0);
836 if T.Known_Tasks_Index /= -1 then
837 Known_Tasks (T.Known_Tasks_Index) := null;
846 procedure Exit_Task is
848 pthread_exit (System.Null_Address);
855 procedure Abort_Task (T : Task_ID) is
856 Result : Interfaces.C.int;
859 Result := pthread_kill (T.Common.LL.Thread,
860 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
861 pragma Assert (Result = 0);
868 -- Dummy versions. The only currently working versions is for solaris
871 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
880 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
885 ----------------------
886 -- Environment_Task --
887 ----------------------
889 function Environment_Task return Task_ID is
891 return Environment_Task_ID;
892 end Environment_Task;
894 -------------------------
895 -- Lock_All_Tasks_List --
896 -------------------------
898 procedure Lock_All_Tasks_List is
900 Write_Lock (All_Tasks_L'Access);
901 end Lock_All_Tasks_List;
903 ---------------------------
904 -- Unlock_All_Tasks_List --
905 ---------------------------
907 procedure Unlock_All_Tasks_List is
909 Unlock (All_Tasks_L'Access);
910 end Unlock_All_Tasks_List;
916 function Suspend_Task
918 Thread_Self : Thread_Id) return Boolean is
929 Thread_Self : Thread_Id) return Boolean is
938 procedure Initialize (Environment_Task : Task_ID) is
939 act : aliased struct_sigaction;
940 old_act : aliased struct_sigaction;
941 Tmp_Set : aliased sigset_t;
942 Result : Interfaces.C.int;
945 Environment_Task_ID := Environment_Task;
947 Initialize_Lock (All_Tasks_L'Access, All_Tasks_Level);
948 -- Initialize the lock used to synchronize chain of all ATCBs.
950 Specific.Initialize (Environment_Task);
952 Enter_Task (Environment_Task);
954 -- Install the abort-signal handler
957 act.sa_handler := Abort_Handler'Address;
959 Result := sigemptyset (Tmp_Set'Access);
960 pragma Assert (Result = 0);
961 act.sa_mask := Tmp_Set;
965 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
966 act'Unchecked_Access,
967 old_act'Unchecked_Access);
968 pragma Assert (Result = 0);
973 Result : Interfaces.C.int;
976 -- Mask Environment task for all signals. The original mask of the
977 -- Environment task will be recovered by Interrupt_Server task
978 -- during the elaboration of s-interr.adb.
980 System.Interrupt_Management.Operations.Set_Interrupt_Mask
981 (System.Interrupt_Management.Operations.All_Tasks_Mask'Access);
983 -- Prepare the set of signals that should unblocked in all tasks
985 Result := sigemptyset (Unblocked_Signal_Mask'Access);
986 pragma Assert (Result = 0);
988 for J in Interrupt_Management.Interrupt_ID loop
989 if System.Interrupt_Management.Keep_Unmasked (J) then
990 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
991 pragma Assert (Result = 0);
997 end System.Task_Primitives.Operations;