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-2003, 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 OpenVMS/Alpha 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.Parameters;
54 -- used for Ada_Task_Control_Block
57 with System.Soft_Links;
58 -- used for Defer/Undefer_Abort
61 -- Note that we do not use System.Tasking.Initialization directly since
62 -- this is a higher level package that we shouldn't depend on. For example
63 -- when using the restricted run time, it is replaced by
64 -- System.Tasking.Restricted.Initialization
66 with System.OS_Primitives;
67 -- used for Delay_Modes
69 with Unchecked_Conversion;
70 with Unchecked_Deallocation;
72 package body System.Task_Primitives.Operations is
74 use System.Tasking.Debug;
77 use System.OS_Interface;
78 use System.Parameters;
79 use System.OS_Primitives;
80 use type System.OS_Primitives.OS_Time;
82 package SSL renames System.Soft_Links;
88 -- The followings are logically constants, but need to be initialized
91 Single_RTS_Lock : aliased RTS_Lock;
92 -- This is a lock to allow only one thread of control in the RTS at
93 -- a time; it is used to execute in mutual exclusion from all other tasks.
94 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
96 ATCB_Key : aliased pthread_key_t;
97 -- Key used to find the Ada Task_ID associated with a thread
99 Environment_Task_ID : Task_ID;
100 -- A variable to hold Task_ID for the environment task.
102 Time_Slice_Val : Integer;
103 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
105 Dispatching_Policy : Character;
106 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
108 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
109 -- Indicates whether FIFO_Within_Priorities is set.
111 Foreign_Task_Elaborated : aliased Boolean := True;
112 -- Used to identified fake tasks (i.e., non-Ada Threads).
120 procedure Initialize (Environment_Task : Task_ID);
121 pragma Inline (Initialize);
122 -- Initialize various data needed by this package.
124 function Is_Valid_Task return Boolean;
125 pragma Inline (Is_Valid_Task);
126 -- Does executing thread have a TCB?
128 procedure Set (Self_Id : Task_ID);
130 -- Set the self id for the current task.
132 function Self return Task_ID;
133 pragma Inline (Self);
134 -- Return a pointer to the Ada Task Control Block of the calling task.
138 package body Specific is separate;
139 -- The body of this package is target specific.
141 ---------------------------------
142 -- Support for foreign threads --
143 ---------------------------------
145 function Register_Foreign_Thread (Thread : Thread_Id) return Task_ID;
146 -- Allocate and Initialize a new ATCB for the current Thread.
148 function Register_Foreign_Thread
149 (Thread : Thread_Id) return Task_ID is separate;
151 -----------------------
152 -- Local Subprograms --
153 -----------------------
155 function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
157 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
159 procedure Timer_Sleep_AST (ID : Address);
160 -- Signal the condition variable when AST fires.
162 procedure Timer_Sleep_AST (ID : Address) is
163 Result : Interfaces.C.int;
164 Self_ID : Task_ID := To_Task_ID (ID);
167 Self_ID.Common.LL.AST_Pending := False;
168 Result := pthread_cond_signal_int_np (Self_ID.Common.LL.CV'Access);
175 -- The underlying thread system sets a guard page at the
176 -- bottom of a thread stack, so nothing is needed.
177 -- ??? Check the comment above
179 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
180 pragma Unreferenced (T);
181 pragma Unreferenced (On);
191 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
193 return T.Common.LL.Thread;
200 function Self return Task_ID renames Specific.Self;
202 ---------------------
203 -- Initialize_Lock --
204 ---------------------
206 -- Note: mutexes and cond_variables needed per-task basis are
207 -- initialized in Initialize_TCB and the Storage_Error is
208 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
209 -- used in RTS is initialized before any status change of RTS.
210 -- Therefore rasing Storage_Error in the following routines
211 -- should be able to be handled safely.
213 procedure Initialize_Lock (Prio : System.Any_Priority; L : access Lock) is
214 Attributes : aliased pthread_mutexattr_t;
215 Result : Interfaces.C.int;
218 Result := pthread_mutexattr_init (Attributes'Access);
219 pragma Assert (Result = 0 or else Result = ENOMEM);
221 if Result = ENOMEM then
226 L.Prio := Interfaces.C.int (Prio);
228 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
229 pragma Assert (Result = 0 or else Result = ENOMEM);
231 if Result = ENOMEM then
235 Result := pthread_mutexattr_destroy (Attributes'Access);
236 pragma Assert (Result = 0);
239 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
240 pragma Unreferenced (Level);
242 Attributes : aliased pthread_mutexattr_t;
243 Result : Interfaces.C.int;
246 Result := pthread_mutexattr_init (Attributes'Access);
247 pragma Assert (Result = 0 or else Result = ENOMEM);
249 if Result = ENOMEM then
253 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes???
254 -- Result := pthread_mutexattr_settype_np
255 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
256 -- pragma Assert (Result = 0);
258 -- Result := pthread_mutexattr_setprotocol
259 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
260 -- pragma Assert (Result = 0);
262 -- Result := pthread_mutexattr_setprioceiling
263 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
264 -- pragma Assert (Result = 0);
266 Result := pthread_mutex_init (L, Attributes'Access);
268 pragma Assert (Result = 0 or else Result = ENOMEM);
270 if Result = ENOMEM then
274 Result := pthread_mutexattr_destroy (Attributes'Access);
275 pragma Assert (Result = 0);
282 procedure Finalize_Lock (L : access Lock) is
283 Result : Interfaces.C.int;
286 Result := pthread_mutex_destroy (L.L'Access);
287 pragma Assert (Result = 0);
290 procedure Finalize_Lock (L : access RTS_Lock) is
291 Result : Interfaces.C.int;
294 Result := pthread_mutex_destroy (L);
295 pragma Assert (Result = 0);
302 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
303 Self_ID : constant Task_ID := Self;
304 All_Tasks_Link : constant Task_ID := Self.Common.All_Tasks_Link;
305 Current_Prio : System.Any_Priority;
306 Result : Interfaces.C.int;
309 Current_Prio := Get_Priority (Self_ID);
311 -- If there is no other tasks, no need to check priorities.
313 if All_Tasks_Link /= Null_Task
314 and then L.Prio < Interfaces.C.int (Current_Prio)
316 Ceiling_Violation := True;
320 Result := pthread_mutex_lock (L.L'Access);
321 pragma Assert (Result = 0);
323 Ceiling_Violation := False;
324 -- Why is this commented out ???
325 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
326 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
330 (L : access RTS_Lock;
331 Global_Lock : Boolean := False)
333 Result : Interfaces.C.int;
336 if not Single_Lock or else Global_Lock then
337 Result := pthread_mutex_lock (L);
338 pragma Assert (Result = 0);
342 procedure Write_Lock (T : Task_ID) is
343 Result : Interfaces.C.int;
346 if not Single_Lock then
347 Result := pthread_mutex_lock (T.Common.LL.L'Access);
348 pragma Assert (Result = 0);
356 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
358 Write_Lock (L, Ceiling_Violation);
365 procedure Unlock (L : access Lock) is
366 Result : Interfaces.C.int;
369 Result := pthread_mutex_unlock (L.L'Access);
370 pragma Assert (Result = 0);
373 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
374 Result : Interfaces.C.int;
377 if not Single_Lock or else Global_Lock then
378 Result := pthread_mutex_unlock (L);
379 pragma Assert (Result = 0);
383 procedure Unlock (T : Task_ID) is
384 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 pragma Unreferenced (Reason);
402 Result : Interfaces.C.int;
406 Result := pthread_cond_wait
407 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
409 Result := pthread_cond_wait
410 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
413 -- EINTR is not considered a failure.
415 pragma Assert (Result = 0 or else Result = EINTR);
417 if Self_ID.Deferral_Level = 0
418 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
421 raise Standard'Abort_Signal;
429 procedure Timed_Sleep
432 Mode : ST.Delay_Modes;
433 Reason : System.Tasking.Task_States;
434 Timedout : out Boolean;
435 Yielded : out Boolean)
437 pragma Unreferenced (Reason);
439 Sleep_Time : OS_Time;
440 Result : Interfaces.C.int;
441 Status : Cond_Value_Type;
447 Sleep_Time := To_OS_Time (Time, Mode);
449 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
450 or else Self_ID.Pending_Priority_Change
455 Self_ID.Common.LL.AST_Pending := True;
458 (Status, 0, Sleep_Time,
459 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
461 if (Status and 1) /= 1 then
466 Result := pthread_cond_wait
467 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
470 Result := pthread_cond_wait
471 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
476 if not Self_ID.Common.LL.AST_Pending then
479 Sys_Cantim (Status, To_Address (Self_ID), 0);
480 pragma Assert ((Status and 1) = 1);
488 procedure Timed_Delay
491 Mode : ST.Delay_Modes)
493 Sleep_Time : OS_Time;
494 Result : Interfaces.C.int;
495 Status : Cond_Value_Type;
496 Yielded : Boolean := False;
499 -- Only the little window between deferring abort and
500 -- locking Self_ID is the reason we need to
501 -- check for pending abort and priority change below!
508 Write_Lock (Self_ID);
510 if Time /= 0.0 or else Mode /= Relative then
511 Sleep_Time := To_OS_Time (Time, Mode);
513 if Mode = Relative or else OS_Clock < Sleep_Time then
514 Self_ID.Common.State := Delay_Sleep;
515 Self_ID.Common.LL.AST_Pending := True;
518 (Status, 0, Sleep_Time,
519 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
521 if (Status and 1) /= 1 then
526 if Self_ID.Pending_Priority_Change then
527 Self_ID.Pending_Priority_Change := False;
528 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
529 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
532 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
533 Sys_Cantim (Status, To_Address (Self_ID), 0);
534 pragma Assert ((Status and 1) = 1);
539 Result := pthread_cond_wait
540 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
542 Result := pthread_cond_wait
543 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
548 exit when not Self_ID.Common.LL.AST_Pending;
551 Self_ID.Common.State := Runnable;
562 Result := sched_yield;
565 SSL.Abort_Undefer.all;
568 ---------------------
569 -- Monotonic_Clock --
570 ---------------------
572 function Monotonic_Clock return Duration
573 renames System.OS_Primitives.Monotonic_Clock;
579 function RT_Resolution return Duration is
588 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
589 pragma Unreferenced (Reason);
591 Result : Interfaces.C.int;
594 Result := pthread_cond_signal (T.Common.LL.CV'Access);
595 pragma Assert (Result = 0);
602 procedure Yield (Do_Yield : Boolean := True) is
603 Result : Interfaces.C.int;
607 Result := sched_yield;
615 procedure Set_Priority
617 Prio : System.Any_Priority;
618 Loss_Of_Inheritance : Boolean := False)
620 pragma Unreferenced (Loss_Of_Inheritance);
622 Result : Interfaces.C.int;
623 Param : aliased struct_sched_param;
626 T.Common.Current_Priority := Prio;
627 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
629 if Time_Slice_Val > 0 then
630 Result := pthread_setschedparam
631 (T.Common.LL.Thread, SCHED_RR, Param'Access);
633 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
634 Result := pthread_setschedparam
635 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
638 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
639 -- Since the translation from Underlying priorities results
640 -- in a range of 16 - 31, dividing by 2 gives the correct result.
642 Param.sched_priority := Param.sched_priority / 2;
643 Result := pthread_setschedparam
644 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
647 pragma Assert (Result = 0);
654 function Get_Priority (T : Task_ID) return System.Any_Priority is
656 return T.Common.Current_Priority;
663 procedure Enter_Task (Self_ID : Task_ID) is
665 Self_ID.Common.LL.Thread := pthread_self;
667 Specific.Set (Self_ID);
671 for J in Known_Tasks'Range loop
672 if Known_Tasks (J) = null then
673 Known_Tasks (J) := Self_ID;
674 Self_ID.Known_Tasks_Index := J;
686 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
688 return new Ada_Task_Control_Block (Entry_Num);
695 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
697 -----------------------------
698 -- Register_Foreign_Thread --
699 -----------------------------
701 function Register_Foreign_Thread return Task_ID is
703 if Is_Valid_Task then
706 return Register_Foreign_Thread (pthread_self);
708 end Register_Foreign_Thread;
710 ----------------------
712 ----------------------
714 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
715 Mutex_Attr : aliased pthread_mutexattr_t;
716 Result : Interfaces.C.int;
717 Cond_Attr : aliased pthread_condattr_t;
720 if not Single_Lock then
721 Result := pthread_mutexattr_init (Mutex_Attr'Access);
722 pragma Assert (Result = 0 or else Result = ENOMEM);
725 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
727 pragma Assert (Result = 0 or else Result = ENOMEM);
735 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
736 pragma Assert (Result = 0);
739 Result := pthread_condattr_init (Cond_Attr'Access);
740 pragma Assert (Result = 0 or else Result = ENOMEM);
743 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
745 pragma Assert (Result = 0 or else Result = ENOMEM);
750 Self_ID.Common.LL.Exc_Stack_Ptr := new Exc_Stack_T;
751 SSL.Set_Exc_Stack_Addr
752 (To_Address (Self_ID),
753 Self_ID.Common.LL.Exc_Stack_Ptr (Exc_Stack_T'Last)'Address);
756 if not Single_Lock then
757 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
758 pragma Assert (Result = 0);
764 Result := pthread_condattr_destroy (Cond_Attr'Access);
765 pragma Assert (Result = 0);
772 procedure Create_Task
774 Wrapper : System.Address;
775 Stack_Size : System.Parameters.Size_Type;
776 Priority : System.Any_Priority;
777 Succeeded : out Boolean)
779 Attributes : aliased pthread_attr_t;
780 Adjusted_Stack_Size : Interfaces.C.size_t;
781 Result : Interfaces.C.int;
783 function Thread_Body_Access is new
784 Unchecked_Conversion (System.Address, Thread_Body);
787 if Stack_Size = Unspecified_Size then
788 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
790 elsif Stack_Size < Minimum_Stack_Size then
791 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
794 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
797 -- Since the initial signal mask of a thread is inherited from the
798 -- creator, we need to set our local signal mask mask all signals
799 -- during the creation operation, to make sure the new thread is
800 -- not disturbed by signals before it has set its own Task_ID.
802 Result := pthread_attr_init (Attributes'Access);
803 pragma Assert (Result = 0 or else Result = ENOMEM);
810 Result := pthread_attr_setdetachstate
811 (Attributes'Access, PTHREAD_CREATE_DETACHED);
812 pragma Assert (Result = 0);
814 Result := pthread_attr_setstacksize
815 (Attributes'Access, Adjusted_Stack_Size);
816 pragma Assert (Result = 0);
818 -- This call may be unnecessary, not sure. ???
821 pthread_attr_setinheritsched
822 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
823 pragma Assert (Result = 0);
825 Result := pthread_create
826 (T.Common.LL.Thread'Access,
828 Thread_Body_Access (Wrapper),
831 -- ENOMEM is a valid run-time error. Don't shut down.
833 pragma Assert (Result = 0
834 or else Result = EAGAIN or else Result = ENOMEM);
836 Succeeded := Result = 0;
838 Result := pthread_attr_destroy (Attributes'Access);
839 pragma Assert (Result = 0);
842 Set_Priority (T, Priority);
850 procedure Finalize_TCB (T : Task_ID) is
851 Result : Interfaces.C.int;
853 Is_Self : constant Boolean := T = Self;
855 procedure Free is new
856 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
858 procedure Free is new Unchecked_Deallocation
859 (Exc_Stack_T, Exc_Stack_Ptr_T);
862 if not Single_Lock then
863 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
864 pragma Assert (Result = 0);
867 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
868 pragma Assert (Result = 0);
870 if T.Known_Tasks_Index /= -1 then
871 Known_Tasks (T.Known_Tasks_Index) := null;
874 Free (T.Common.LL.Exc_Stack_Ptr);
879 Result := pthread_setspecific (ATCB_Key, System.Null_Address);
880 pragma Assert (Result = 0);
888 procedure Exit_Task is
897 procedure Abort_Task (T : Task_ID) is
899 -- Interrupt Server_Tasks may be waiting on an event flag
901 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
902 Wakeup (T, Interrupt_Server_Blocked_On_Event_Flag);
912 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
913 pragma Unreferenced (Self_ID);
923 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
924 pragma Unreferenced (Self_ID);
930 ----------------------
931 -- Environment_Task --
932 ----------------------
934 function Environment_Task return Task_ID is
936 return Environment_Task_ID;
937 end Environment_Task;
943 procedure Lock_RTS is
945 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
952 procedure Unlock_RTS is
954 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
961 function Suspend_Task
963 Thread_Self : Thread_Id)
966 pragma Unreferenced (T);
967 pragma Unreferenced (Thread_Self);
979 Thread_Self : Thread_Id)
982 pragma Unreferenced (T);
983 pragma Unreferenced (Thread_Self);
993 procedure Initialize (Environment_Task : Task_ID) is
995 Environment_Task_ID := Environment_Task;
997 -- Initialize the lock used to synchronize chain of all ATCBs.
999 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1001 Specific.Initialize (Environment_Task);
1003 Enter_Task (Environment_Task);
1006 end System.Task_Primitives.Operations;