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-2004, 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 : constant Task_Id := To_Task_Id (ID);
166 Self_ID.Common.LL.AST_Pending := False;
167 Result := pthread_cond_signal_int_np (Self_ID.Common.LL.CV'Access);
168 pragma Assert (Result = 0);
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);
190 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
192 return T.Common.LL.Thread;
199 function Self return Task_Id renames Specific.Self;
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 (Prio : System.Any_Priority; L : access Lock) is
213 Attributes : aliased pthread_mutexattr_t;
214 Result : Interfaces.C.int;
217 Result := pthread_mutexattr_init (Attributes'Access);
218 pragma Assert (Result = 0 or else Result = ENOMEM);
220 if Result = ENOMEM then
225 L.Prio := Interfaces.C.int (Prio);
227 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
228 pragma Assert (Result = 0 or else Result = ENOMEM);
230 if Result = ENOMEM then
234 Result := pthread_mutexattr_destroy (Attributes'Access);
235 pragma Assert (Result = 0);
238 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
239 pragma Unreferenced (Level);
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 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes???
253 -- Result := pthread_mutexattr_settype_np
254 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
255 -- pragma Assert (Result = 0);
257 -- Result := pthread_mutexattr_setprotocol
258 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
259 -- pragma Assert (Result = 0);
261 -- Result := pthread_mutexattr_setprioceiling
262 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
263 -- pragma Assert (Result = 0);
265 Result := pthread_mutex_init (L, Attributes'Access);
267 pragma Assert (Result = 0 or else Result = ENOMEM);
269 if Result = ENOMEM then
273 Result := pthread_mutexattr_destroy (Attributes'Access);
274 pragma Assert (Result = 0);
281 procedure Finalize_Lock (L : access Lock) is
282 Result : Interfaces.C.int;
284 Result := pthread_mutex_destroy (L.L'Access);
285 pragma Assert (Result = 0);
288 procedure Finalize_Lock (L : access RTS_Lock) is
289 Result : Interfaces.C.int;
291 Result := pthread_mutex_destroy (L);
292 pragma Assert (Result = 0);
299 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
300 Self_ID : constant Task_Id := Self;
301 All_Tasks_Link : constant Task_Id := Self.Common.All_Tasks_Link;
302 Current_Prio : System.Any_Priority;
303 Result : Interfaces.C.int;
306 Current_Prio := Get_Priority (Self_ID);
308 -- If there is no other tasks, no need to check priorities
310 if All_Tasks_Link /= Null_Task
311 and then L.Prio < Interfaces.C.int (Current_Prio)
313 Ceiling_Violation := True;
317 Result := pthread_mutex_lock (L.L'Access);
318 pragma Assert (Result = 0);
320 Ceiling_Violation := False;
321 -- Why is this commented out ???
322 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
323 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
327 (L : access RTS_Lock;
328 Global_Lock : Boolean := False)
330 Result : Interfaces.C.int;
332 if not Single_Lock or else Global_Lock then
333 Result := pthread_mutex_lock (L);
334 pragma Assert (Result = 0);
338 procedure Write_Lock (T : Task_Id) is
339 Result : Interfaces.C.int;
341 if not Single_Lock then
342 Result := pthread_mutex_lock (T.Common.LL.L'Access);
343 pragma Assert (Result = 0);
351 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
353 Write_Lock (L, Ceiling_Violation);
360 procedure Unlock (L : access Lock) is
361 Result : Interfaces.C.int;
363 Result := pthread_mutex_unlock (L.L'Access);
364 pragma Assert (Result = 0);
367 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
368 Result : Interfaces.C.int;
370 if not Single_Lock or else Global_Lock then
371 Result := pthread_mutex_unlock (L);
372 pragma Assert (Result = 0);
376 procedure Unlock (T : Task_Id) is
377 Result : Interfaces.C.int;
379 if not Single_Lock then
380 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
381 pragma Assert (Result = 0);
391 Reason : System.Tasking.Task_States)
393 pragma Unreferenced (Reason);
394 Result : Interfaces.C.int;
398 Result := pthread_cond_wait
399 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
401 Result := pthread_cond_wait
402 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
405 -- EINTR is not considered a failure
407 pragma Assert (Result = 0 or else Result = EINTR);
409 if Self_ID.Deferral_Level = 0
410 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
413 raise Standard'Abort_Signal;
421 procedure Timed_Sleep
424 Mode : ST.Delay_Modes;
425 Reason : System.Tasking.Task_States;
426 Timedout : out Boolean;
427 Yielded : out Boolean)
429 pragma Unreferenced (Reason);
431 Sleep_Time : OS_Time;
432 Result : Interfaces.C.int;
433 Status : Cond_Value_Type;
435 -- The body below requires more comments ???
441 Sleep_Time := To_OS_Time (Time, Mode);
443 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
444 or else Self_ID.Pending_Priority_Change
449 Self_ID.Common.LL.AST_Pending := True;
452 (Status, 0, Sleep_Time,
453 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
455 if (Status and 1) /= 1 then
460 Result := pthread_cond_wait
461 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
462 pragma Assert (Result = 0);
465 Result := pthread_cond_wait
466 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
467 pragma Assert (Result = 0);
472 if not Self_ID.Common.LL.AST_Pending then
475 Sys_Cantim (Status, To_Address (Self_ID), 0);
476 pragma Assert ((Status and 1) = 1);
484 procedure Timed_Delay
487 Mode : ST.Delay_Modes)
489 Sleep_Time : OS_Time;
490 Result : Interfaces.C.int;
491 Status : Cond_Value_Type;
492 Yielded : Boolean := False;
495 -- Only the little window between deferring abort and
496 -- locking Self_ID is the reason we need to
497 -- check for pending abort and priority change below!
503 -- More comments required in body below ???
506 Write_Lock (Self_ID);
508 if Time /= 0.0 or else Mode /= Relative then
509 Sleep_Time := To_OS_Time (Time, Mode);
511 if Mode = Relative or else OS_Clock < Sleep_Time then
512 Self_ID.Common.State := Delay_Sleep;
513 Self_ID.Common.LL.AST_Pending := True;
516 (Status, 0, Sleep_Time,
517 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
519 if (Status and 1) /= 1 then
524 if Self_ID.Pending_Priority_Change then
525 Self_ID.Pending_Priority_Change := False;
526 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
527 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
530 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
531 Sys_Cantim (Status, To_Address (Self_ID), 0);
532 pragma Assert ((Status and 1) = 1);
537 Result := pthread_cond_wait
538 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
539 pragma Assert (Result = 0);
541 Result := pthread_cond_wait
542 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
543 pragma Assert (Result = 0);
548 exit when not Self_ID.Common.LL.AST_Pending;
551 Self_ID.Common.State := Runnable;
562 Result := sched_yield;
563 pragma Assert (Result = 0);
566 SSL.Abort_Undefer.all;
569 ---------------------
570 -- Monotonic_Clock --
571 ---------------------
573 function Monotonic_Clock return Duration
574 renames System.OS_Primitives.Monotonic_Clock;
580 function RT_Resolution return Duration is
589 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
590 pragma Unreferenced (Reason);
592 Result : Interfaces.C.int;
595 Result := pthread_cond_signal (T.Common.LL.CV'Access);
596 pragma Assert (Result = 0);
603 procedure Yield (Do_Yield : Boolean := True) is
604 Result : Interfaces.C.int;
605 pragma Unreferenced (Result);
608 Result := sched_yield;
616 procedure Set_Priority
618 Prio : System.Any_Priority;
619 Loss_Of_Inheritance : Boolean := False)
621 pragma Unreferenced (Loss_Of_Inheritance);
623 Result : Interfaces.C.int;
624 Param : aliased struct_sched_param;
627 T.Common.Current_Priority := Prio;
628 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
630 if Time_Slice_Val > 0 then
631 Result := pthread_setschedparam
632 (T.Common.LL.Thread, SCHED_RR, Param'Access);
634 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
635 Result := pthread_setschedparam
636 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
639 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
640 -- Since the translation from Underlying priorities results
641 -- in a range of 16 - 31, dividing by 2 gives the correct result.
643 Param.sched_priority := Param.sched_priority / 2;
644 Result := pthread_setschedparam
645 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
648 pragma Assert (Result = 0);
655 function Get_Priority (T : Task_Id) return System.Any_Priority is
657 return T.Common.Current_Priority;
664 procedure Enter_Task (Self_ID : Task_Id) is
666 Self_ID.Common.LL.Thread := pthread_self;
668 Specific.Set (Self_ID);
672 for J in Known_Tasks'Range loop
673 if Known_Tasks (J) = null then
674 Known_Tasks (J) := Self_ID;
675 Self_ID.Known_Tasks_Index := J;
687 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
689 return new Ada_Task_Control_Block (Entry_Num);
696 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
698 -----------------------------
699 -- Register_Foreign_Thread --
700 -----------------------------
702 function Register_Foreign_Thread return Task_Id is
704 if Is_Valid_Task then
707 return Register_Foreign_Thread (pthread_self);
709 end Register_Foreign_Thread;
711 ----------------------
713 ----------------------
715 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
716 Mutex_Attr : aliased pthread_mutexattr_t;
717 Result : Interfaces.C.int;
718 Cond_Attr : aliased pthread_condattr_t;
721 -- More comments required in body below ???
723 if not Single_Lock then
724 Result := pthread_mutexattr_init (Mutex_Attr'Access);
725 pragma Assert (Result = 0 or else Result = ENOMEM);
728 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
730 pragma Assert (Result = 0 or else Result = ENOMEM);
738 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
739 pragma Assert (Result = 0);
742 Result := pthread_condattr_init (Cond_Attr'Access);
743 pragma Assert (Result = 0 or else Result = ENOMEM);
746 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
748 pragma Assert (Result = 0 or else Result = ENOMEM);
753 Self_ID.Common.LL.Exc_Stack_Ptr := new Exc_Stack_T;
754 SSL.Set_Exc_Stack_Addr
755 (To_Address (Self_ID),
756 Self_ID.Common.LL.Exc_Stack_Ptr (Exc_Stack_T'Last)'Address);
759 if not Single_Lock then
760 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
761 pragma Assert (Result = 0);
767 Result := pthread_condattr_destroy (Cond_Attr'Access);
768 pragma Assert (Result = 0);
775 procedure Create_Task
777 Wrapper : System.Address;
778 Stack_Size : System.Parameters.Size_Type;
779 Priority : System.Any_Priority;
780 Succeeded : out Boolean)
782 Attributes : aliased pthread_attr_t;
783 Adjusted_Stack_Size : Interfaces.C.size_t;
784 Result : Interfaces.C.int;
786 function Thread_Body_Access is new
787 Unchecked_Conversion (System.Address, Thread_Body);
790 if Stack_Size = Unspecified_Size then
791 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
793 elsif Stack_Size < Minimum_Stack_Size then
794 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
797 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
800 -- Since the initial signal mask of a thread is inherited from the
801 -- creator, we need to set our local signal mask mask all signals
802 -- during the creation operation, to make sure the new thread is
803 -- not disturbed by signals before it has set its own Task_Id.
805 Result := pthread_attr_init (Attributes'Access);
806 pragma Assert (Result = 0 or else Result = ENOMEM);
813 Result := pthread_attr_setdetachstate
814 (Attributes'Access, PTHREAD_CREATE_DETACHED);
815 pragma Assert (Result = 0);
817 Result := pthread_attr_setstacksize
818 (Attributes'Access, Adjusted_Stack_Size);
819 pragma Assert (Result = 0);
821 -- This call may be unnecessary, not sure. ???
824 pthread_attr_setinheritsched
825 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
826 pragma Assert (Result = 0);
828 Result := pthread_create
829 (T.Common.LL.Thread'Access,
831 Thread_Body_Access (Wrapper),
834 -- ENOMEM is a valid run-time error. Don't shut down.
836 pragma Assert (Result = 0
837 or else Result = EAGAIN or else Result = ENOMEM);
839 Succeeded := Result = 0;
841 Result := pthread_attr_destroy (Attributes'Access);
842 pragma Assert (Result = 0);
845 Set_Priority (T, Priority);
853 procedure Finalize_TCB (T : Task_Id) is
854 Result : Interfaces.C.int;
856 Is_Self : constant Boolean := T = Self;
858 procedure Free is new
859 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
861 procedure Free is new Unchecked_Deallocation
862 (Exc_Stack_T, Exc_Stack_Ptr_T);
865 if not Single_Lock then
866 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
867 pragma Assert (Result = 0);
870 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
871 pragma Assert (Result = 0);
873 if T.Known_Tasks_Index /= -1 then
874 Known_Tasks (T.Known_Tasks_Index) := null;
877 Free (T.Common.LL.Exc_Stack_Ptr);
890 procedure Exit_Task is
899 procedure Abort_Task (T : Task_Id) is
901 -- Interrupt Server_Tasks may be waiting on an event flag
903 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
904 Wakeup (T, Interrupt_Server_Blocked_On_Event_Flag);
914 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
915 pragma Unreferenced (Self_ID);
925 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
926 pragma Unreferenced (Self_ID);
932 ----------------------
933 -- Environment_Task --
934 ----------------------
936 function Environment_Task return Task_Id is
938 return Environment_Task_Id;
939 end Environment_Task;
945 procedure Lock_RTS is
947 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
954 procedure Unlock_RTS is
956 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
963 function Suspend_Task
965 Thread_Self : Thread_Id) return Boolean
967 pragma Unreferenced (T);
968 pragma Unreferenced (Thread_Self);
980 Thread_Self : Thread_Id) return Boolean
982 pragma Unreferenced (T);
983 pragma Unreferenced (Thread_Self);
992 procedure Initialize (Environment_Task : Task_Id) is
994 Environment_Task_Id := Environment_Task;
996 -- Initialize the lock used to synchronize chain of all ATCBs
998 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1000 Specific.Initialize (Environment_Task);
1002 Enter_Task (Environment_Task);
1005 end System.Task_Primitives.Operations;