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 HP-UX DCE threads 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
50 with System.Interrupt_Management;
51 -- used for Keep_Unmasked
52 -- Abort_Task_Interrupt
55 with System.Interrupt_Management.Operations;
56 -- used for Set_Interrupt_Mask
58 pragma Elaborate_All (System.Interrupt_Management.Operations);
60 with System.Parameters;
63 with System.Task_Primitives.Interrupt_Operations;
64 -- used for Get_Interrupt_ID
67 -- used for Ada_Task_Control_Block
70 with System.Soft_Links;
71 -- used for Defer/Undefer_Abort
73 -- Note that we do not use System.Tasking.Initialization directly since
74 -- this is a higher level package that we shouldn't depend on. For example
75 -- when using the restricted run time, it is replaced by
76 -- System.Tasking.Restricted.Initialization
78 with System.OS_Primitives;
79 -- used for Delay_Modes
81 with Unchecked_Conversion;
82 with Unchecked_Deallocation;
84 package body System.Task_Primitives.Operations is
86 use System.Tasking.Debug;
89 use System.OS_Interface;
90 use System.Parameters;
91 use System.OS_Primitives;
93 package PIO renames System.Task_Primitives.Interrupt_Operations;
94 package SSL renames System.Soft_Links;
100 -- The followings are logically constants, but need to be initialized
103 ATCB_Key : aliased pthread_key_t;
104 -- Key used to find the Ada Task_ID associated with a thread
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.
129 -- The followings are internal configuration constants needed.
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);
145 procedure Abort_Handler (Sig : Signal) is
146 Self_Id : constant Task_ID := Self;
147 Result : Interfaces.C.int;
148 Old_Set : aliased sigset_t;
151 if Self_Id.Deferral_Level = 0
152 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level and then
155 Self_Id.Aborting := True;
157 -- Make sure signals used for RTS internal purpose are unmasked
159 Result := pthread_sigmask (SIG_UNBLOCK,
160 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
161 pragma Assert (Result = 0);
163 raise Standard'Abort_Signal;
171 -- The underlying thread system sets a guard page at the
172 -- bottom of a thread stack, so nothing is needed.
173 -- ??? Check the comment above
175 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
184 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
186 return T.Common.LL.Thread;
193 function Self return Task_ID is
194 Result : System.Address;
196 Result := pthread_getspecific (ATCB_Key);
197 pragma Assert (Result /= System.Null_Address);
198 return To_Task_ID (Result);
201 ---------------------
202 -- Initialize_Lock --
203 ---------------------
205 -- Note: mutexes and cond_variables needed per-task basis are
206 -- initialized in Initialize_TCB and the Storage_Error is
207 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
208 -- used in RTS is initialized before any status change of RTS.
209 -- Therefore rasing Storage_Error in the following routines
210 -- should be able to be handled safely.
212 procedure Initialize_Lock
213 (Prio : System.Any_Priority;
216 Attributes : aliased pthread_mutexattr_t;
217 Result : Interfaces.C.int;
220 Result := pthread_mutexattr_init (Attributes'Access);
221 pragma Assert (Result = 0 or else Result = ENOMEM);
223 if Result = ENOMEM then
229 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
230 pragma Assert (Result = 0 or else Result = ENOMEM);
232 if Result = ENOMEM then
236 Result := pthread_mutexattr_destroy (Attributes'Access);
237 pragma Assert (Result = 0);
240 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
241 Attributes : aliased pthread_mutexattr_t;
242 Result : Interfaces.C.int;
245 Result := pthread_mutexattr_init (Attributes'Access);
246 pragma Assert (Result = 0 or else Result = ENOMEM);
248 if Result = ENOMEM then
252 Result := pthread_mutex_init (L, Attributes'Access);
254 pragma Assert (Result = 0 or else Result = ENOMEM);
256 if Result = ENOMEM then
260 Result := pthread_mutexattr_destroy (Attributes'Access);
261 pragma Assert (Result = 0);
268 procedure Finalize_Lock (L : access Lock) is
269 Result : Interfaces.C.int;
271 Result := pthread_mutex_destroy (L.L'Access);
272 pragma Assert (Result = 0);
275 procedure Finalize_Lock (L : access RTS_Lock) is
276 Result : Interfaces.C.int;
278 Result := pthread_mutex_destroy (L);
279 pragma Assert (Result = 0);
286 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
287 Result : Interfaces.C.int;
289 L.Owner_Priority := Get_Priority (Self);
291 if L.Priority < L.Owner_Priority then
292 Ceiling_Violation := True;
296 Result := pthread_mutex_lock (L.L'Access);
297 pragma Assert (Result = 0);
298 Ceiling_Violation := False;
302 (L : access RTS_Lock; Global_Lock : Boolean := False)
304 Result : Interfaces.C.int;
306 if not Single_Lock or else Global_Lock then
307 Result := pthread_mutex_lock (L);
308 pragma Assert (Result = 0);
312 procedure Write_Lock (T : Task_ID) is
313 Result : Interfaces.C.int;
315 if not Single_Lock then
316 Result := pthread_mutex_lock (T.Common.LL.L'Access);
317 pragma Assert (Result = 0);
325 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
327 Write_Lock (L, Ceiling_Violation);
334 procedure Unlock (L : access Lock) is
335 Result : Interfaces.C.int;
337 Result := pthread_mutex_unlock (L.L'Access);
338 pragma Assert (Result = 0);
341 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
342 Result : Interfaces.C.int;
344 if not Single_Lock or else Global_Lock then
345 Result := pthread_mutex_unlock (L);
346 pragma Assert (Result = 0);
350 procedure Unlock (T : Task_ID) is
351 Result : Interfaces.C.int;
353 if not Single_Lock then
354 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
355 pragma Assert (Result = 0);
365 Reason : System.Tasking.Task_States)
367 Result : Interfaces.C.int;
370 Result := pthread_cond_wait
371 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
373 Result := pthread_cond_wait
374 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
377 -- EINTR is not considered a failure.
378 pragma Assert (Result = 0 or else Result = EINTR);
385 procedure Timed_Sleep
388 Mode : ST.Delay_Modes;
389 Reason : System.Tasking.Task_States;
390 Timedout : out Boolean;
391 Yielded : out Boolean)
393 Check_Time : constant Duration := Monotonic_Clock;
395 Request : aliased timespec;
396 Result : Interfaces.C.int;
402 if Mode = Relative then
403 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
405 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
408 if Abs_Time > Check_Time then
409 Request := To_Timespec (Abs_Time);
412 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
413 or else Self_ID.Pending_Priority_Change;
416 Result := pthread_cond_timedwait
417 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
421 Result := pthread_cond_timedwait
422 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
426 exit when Abs_Time <= Monotonic_Clock;
428 if Result = 0 or Result = EINTR then
429 -- somebody may have called Wakeup for us
434 pragma Assert (Result = ETIMEDOUT);
443 procedure Timed_Delay
446 Mode : ST.Delay_Modes)
448 Check_Time : constant Duration := Monotonic_Clock;
450 Request : aliased timespec;
451 Result : Interfaces.C.int;
454 -- Only the little window between deferring abort and
455 -- locking Self_ID is the reason we need to
456 -- check for pending abort and priority change below! :(
464 Write_Lock (Self_ID);
466 if Mode = Relative then
467 Abs_Time := Time + Check_Time;
469 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
472 if Abs_Time > Check_Time then
473 Request := To_Timespec (Abs_Time);
474 Self_ID.Common.State := Delay_Sleep;
477 if Self_ID.Pending_Priority_Change then
478 Self_ID.Pending_Priority_Change := False;
479 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
480 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
483 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
486 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
487 Single_RTS_Lock'Access, Request'Access);
489 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
490 Self_ID.Common.LL.L'Access, Request'Access);
493 exit when Abs_Time <= Monotonic_Clock;
495 pragma Assert (Result = 0 or else
496 Result = ETIMEDOUT or else
500 Self_ID.Common.State := Runnable;
509 Result := sched_yield;
510 SSL.Abort_Undefer.all;
513 ---------------------
514 -- Monotonic_Clock --
515 ---------------------
517 function Monotonic_Clock return Duration is
518 TS : aliased timespec;
519 Result : Interfaces.C.int;
522 Result := Clock_Gettime (CLOCK_REALTIME, TS'Unchecked_Access);
523 pragma Assert (Result = 0);
524 return To_Duration (TS);
531 function RT_Resolution return Duration is
540 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
541 Result : Interfaces.C.int;
543 Result := pthread_cond_signal (T.Common.LL.CV'Access);
544 pragma Assert (Result = 0);
551 procedure Yield (Do_Yield : Boolean := True) is
552 Result : Interfaces.C.int;
555 Result := sched_yield;
563 type Prio_Array_Type is array (System.Any_Priority) of Integer;
564 pragma Atomic_Components (Prio_Array_Type);
566 Prio_Array : Prio_Array_Type;
567 -- Global array containing the id of the currently running task for
570 -- Note: we assume that we are on a single processor with run-til-blocked
573 procedure Set_Priority
575 Prio : System.Any_Priority;
576 Loss_Of_Inheritance : Boolean := False)
578 Result : Interfaces.C.int;
579 Array_Item : Integer;
580 Param : aliased struct_sched_param;
583 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
585 if Time_Slice_Val > 0 then
586 Result := pthread_setschedparam
587 (T.Common.LL.Thread, SCHED_RR, Param'Access);
589 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
590 Result := pthread_setschedparam
591 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
594 Result := pthread_setschedparam
595 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
598 pragma Assert (Result = 0);
600 if FIFO_Within_Priorities then
602 -- Annex D requirement [RM D.2.2 par. 9]:
603 -- If the task drops its priority due to the loss of inherited
604 -- priority, it is added at the head of the ready queue for its
605 -- new active priority.
607 if Loss_Of_Inheritance
608 and then Prio < T.Common.Current_Priority
610 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
611 Prio_Array (T.Common.Base_Priority) := Array_Item;
614 -- Let some processes a chance to arrive
618 -- Then wait for our turn to proceed
620 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
621 or else Prio_Array (T.Common.Base_Priority) = 1;
624 Prio_Array (T.Common.Base_Priority) :=
625 Prio_Array (T.Common.Base_Priority) - 1;
629 T.Common.Current_Priority := Prio;
636 function Get_Priority (T : Task_ID) return System.Any_Priority is
638 return T.Common.Current_Priority;
645 procedure Enter_Task (Self_ID : Task_ID) is
646 Result : Interfaces.C.int;
649 Self_ID.Common.LL.Thread := pthread_self;
651 Result := pthread_setspecific (ATCB_Key, To_Address (Self_ID));
652 pragma Assert (Result = 0);
656 for J in Known_Tasks'Range loop
657 if Known_Tasks (J) = null then
658 Known_Tasks (J) := Self_ID;
659 Self_ID.Known_Tasks_Index := J;
671 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
673 return new Ada_Task_Control_Block (Entry_Num);
680 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
681 Mutex_Attr : aliased pthread_mutexattr_t;
682 Result : Interfaces.C.int;
683 Cond_Attr : aliased pthread_condattr_t;
686 if not Single_Lock then
687 Result := pthread_mutexattr_init (Mutex_Attr'Access);
688 pragma Assert (Result = 0 or else Result = ENOMEM);
691 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
693 pragma Assert (Result = 0 or else Result = ENOMEM);
701 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
702 pragma Assert (Result = 0);
705 Result := pthread_condattr_init (Cond_Attr'Access);
706 pragma Assert (Result = 0 or else Result = ENOMEM);
709 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
711 pragma Assert (Result = 0 or else Result = ENOMEM);
717 if not Single_Lock then
718 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
719 pragma Assert (Result = 0);
725 Result := pthread_condattr_destroy (Cond_Attr'Access);
726 pragma Assert (Result = 0);
733 procedure Create_Task
735 Wrapper : System.Address;
736 Stack_Size : System.Parameters.Size_Type;
737 Priority : System.Any_Priority;
738 Succeeded : out Boolean)
740 Attributes : aliased pthread_attr_t;
741 Adjusted_Stack_Size : Interfaces.C.size_t;
742 Result : Interfaces.C.int;
744 function Thread_Body_Access is new
745 Unchecked_Conversion (System.Address, Thread_Body);
748 if Stack_Size = Unspecified_Size then
749 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
751 elsif Stack_Size < Minimum_Stack_Size then
752 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
755 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
758 Result := pthread_attr_init (Attributes'Access);
759 pragma Assert (Result = 0 or else Result = ENOMEM);
766 Result := pthread_attr_setstacksize
767 (Attributes'Access, Adjusted_Stack_Size);
768 pragma Assert (Result = 0);
770 -- Since the initial signal mask of a thread is inherited from the
771 -- creator, and the Environment task has all its signals masked, we
772 -- do not need to manipulate caller's signal mask at this point.
773 -- All tasks in RTS will have All_Tasks_Mask initially.
775 Result := pthread_create
776 (T.Common.LL.Thread'Access,
778 Thread_Body_Access (Wrapper),
780 pragma Assert (Result = 0 or else Result = EAGAIN);
782 Succeeded := Result = 0;
784 pthread_detach (T.Common.LL.Thread'Access);
785 -- Detach the thread using pthread_detach, sinc DCE threads do not have
786 -- pthread_attr_set_detachstate.
788 Result := pthread_attr_destroy (Attributes'Access);
789 pragma Assert (Result = 0);
791 Set_Priority (T, Priority);
798 procedure Finalize_TCB (T : Task_ID) is
799 Result : Interfaces.C.int;
802 procedure Free is new
803 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
806 if not Single_Lock then
807 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
808 pragma Assert (Result = 0);
811 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
812 pragma Assert (Result = 0);
814 if T.Known_Tasks_Index /= -1 then
815 Known_Tasks (T.Known_Tasks_Index) := null;
825 procedure Exit_Task is
827 pthread_exit (System.Null_Address);
834 procedure Abort_Task (T : Task_ID) is
837 -- Interrupt Server_Tasks may be waiting on an "event" flag (signal)
839 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
840 System.Interrupt_Management.Operations.Interrupt_Self_Process
841 (System.Interrupt_Management.Interrupt_ID
842 (PIO.Get_Interrupt_ID (T)));
850 -- Dummy versions. The only currently working versions is for solaris
853 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
862 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
867 ----------------------
868 -- Environment_Task --
869 ----------------------
871 function Environment_Task return Task_ID is
873 return Environment_Task_ID;
874 end Environment_Task;
880 procedure Lock_RTS is
882 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
889 procedure Unlock_RTS is
891 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
898 function Suspend_Task
900 Thread_Self : Thread_Id) return Boolean is
911 Thread_Self : Thread_Id) return Boolean is
920 procedure Initialize (Environment_Task : Task_ID) is
921 act : aliased struct_sigaction;
922 old_act : aliased struct_sigaction;
923 Tmp_Set : aliased sigset_t;
924 Result : Interfaces.C.int;
928 Environment_Task_ID := Environment_Task;
930 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
931 -- Initialize the lock used to synchronize chain of all ATCBs.
933 Enter_Task (Environment_Task);
935 -- Install the abort-signal handler
938 act.sa_handler := Abort_Handler'Address;
940 Result := sigemptyset (Tmp_Set'Access);
941 pragma Assert (Result = 0);
942 act.sa_mask := Tmp_Set;
946 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
947 act'Unchecked_Access,
948 old_act'Unchecked_Access);
949 pragma Assert (Result = 0);
952 procedure do_nothing (arg : System.Address);
954 procedure do_nothing (arg : System.Address) is
961 Result : Interfaces.C.int;
963 -- NOTE: Unlike other pthread implementations, we do *not* mask all
964 -- signals here since we handle signals using the process-wide primitive
965 -- signal, rather than using sigthreadmask and sigwait. The reason of
966 -- this difference is that sigwait doesn't work when some critical
967 -- signals (SIGABRT, SIGPIPE) are masked.
969 Result := pthread_key_create (ATCB_Key'Access, do_nothing'Access);
970 pragma Assert (Result = 0);
972 end System.Task_Primitives.Operations;