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 an Irix (old athread library) 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.
47 with System.Tasking.Debug;
48 -- used for Known_Tasks
50 with System.Task_Info;
52 with System.Interrupt_Management;
53 -- used for Keep_Unmasked
54 -- Abort_Task_Interrupt
57 with System.Parameters;
61 -- used for Ada_Task_Control_Block
64 with System.Program_Info;
65 -- used for Default_Task_Stack
68 -- Pthread_Sched_Signal
71 with System.Soft_Links;
72 -- used for Defer/Undefer_Abort
74 -- Note that we do not use System.Tasking.Initialization directly since
75 -- this is a higher level package that we shouldn't depend on. For example
76 -- when using the restricted run time, it is replaced by
77 -- System.Tasking.Restricted.Initialization
79 with System.OS_Primitives;
80 -- used for Delay_Modes
82 with System.Storage_Elements;
83 -- used for To_Address
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 Locking_Policy : Character;
115 pragma Import (C, Locking_Policy, "__gl_locking_policy");
117 Clock_Address : constant System.Address :=
118 System.Storage_Elements.To_Address (16#200F90#);
120 RT_Clock_Id : clockid_t;
121 for RT_Clock_Id'Address use Clock_Address;
123 -----------------------
124 -- Local Subprograms --
125 -----------------------
127 procedure Initialize_Athread_Library;
129 function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
131 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
137 -- The underlying thread system sets a guard page at the
138 -- bottom of a thread stack, so nothing is needed.
139 -- ??? Check the comment above
141 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
142 pragma Unreferenced (T);
143 pragma Unreferenced (On);
152 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
154 return T.Common.LL.Thread;
161 function Self return Task_ID is
163 return To_Task_ID (pthread_get_current_ada_tcb);
166 ---------------------
167 -- Initialize_Lock --
168 ---------------------
170 -- Note: mutexes and cond_variables needed per-task basis are
171 -- initialized in Initialize_TCB and the Storage_Error is
172 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
173 -- used in RTS is initialized before any status change of RTS.
174 -- Therefore rasing Storage_Error in the following routines
175 -- should be able to be handled safely.
177 procedure Initialize_Lock
178 (Prio : System.Any_Priority;
181 Attributes : aliased pthread_mutexattr_t;
182 Result : Interfaces.C.int;
185 Result := pthread_mutexattr_init (Attributes'Access);
187 if Result = FUNC_ERR then
191 if Locking_Policy = 'C' then
193 Result := pthread_mutexattr_setqueueorder
194 (Attributes'Access, MUTEX_PRIORITY_CEILING);
196 pragma Assert (Result /= FUNC_ERR);
198 Result := pthread_mutexattr_setceilingprio
199 (Attributes'Access, Interfaces.C.int (Prio));
201 pragma Assert (Result /= FUNC_ERR);
204 Result := pthread_mutex_init (L, Attributes'Access);
206 if Result = FUNC_ERR then
207 Result := pthread_mutexattr_destroy (Attributes'Access);
211 Result := pthread_mutexattr_destroy (Attributes'Access);
214 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
215 pragma Unreferenced (Level);
217 Attributes : aliased pthread_mutexattr_t;
218 Result : Interfaces.C.int;
221 Result := pthread_mutexattr_init (Attributes'Access);
223 if Result = FUNC_ERR then
227 if Locking_Policy = 'C' then
228 Result := pthread_mutexattr_setqueueorder
229 (Attributes'Access, MUTEX_PRIORITY_CEILING);
230 pragma Assert (Result /= FUNC_ERR);
232 Result := pthread_mutexattr_setceilingprio
233 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
234 pragma Assert (Result /= FUNC_ERR);
237 Result := pthread_mutex_init (L, Attributes'Access);
239 if Result = FUNC_ERR then
240 Result := pthread_mutexattr_destroy (Attributes'Access);
244 Result := pthread_mutexattr_destroy (Attributes'Access);
251 procedure Finalize_Lock (L : access Lock) is
252 Result : Interfaces.C.int;
254 Result := pthread_mutex_destroy (L);
255 pragma Assert (Result = 0);
258 procedure Finalize_Lock (L : access RTS_Lock) is
259 Result : Interfaces.C.int;
261 Result := pthread_mutex_destroy (L);
262 pragma Assert (Result = 0);
269 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
270 Result : Interfaces.C.int;
272 Result := pthread_mutex_lock (L);
273 Ceiling_Violation := Result = FUNC_ERR and then errno = EINVAL;
274 pragma Assert (Result /= FUNC_ERR);
278 (L : access RTS_Lock; Global_Lock : Boolean := False)
280 Result : Interfaces.C.int;
282 if not Single_Lock or else Global_Lock then
283 Result := pthread_mutex_lock (L);
284 pragma Assert (Result = 0);
288 procedure Write_Lock (T : Task_ID) is
289 Result : Interfaces.C.int;
291 if not Single_Lock then
292 Result := pthread_mutex_lock (T.Common.LL.L'Access);
293 pragma Assert (Result = 0);
301 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
303 Write_Lock (L, Ceiling_Violation);
310 procedure Unlock (L : access Lock) is
311 Result : Interfaces.C.int;
313 Result := pthread_mutex_unlock (L);
314 pragma Assert (Result = 0);
317 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
318 Result : Interfaces.C.int;
320 if not Single_Lock or else Global_Lock then
321 Result := pthread_mutex_unlock (L);
322 pragma Assert (Result = 0);
326 procedure Unlock (T : Task_ID) is
327 Result : Interfaces.C.int;
329 if not Single_Lock then
330 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
331 pragma Assert (Result = 0);
340 (Self_ID : ST.Task_ID;
341 Reason : System.Tasking.Task_States)
343 pragma Unreferenced (Reason);
345 Result : Interfaces.C.int;
349 Result := pthread_cond_wait
350 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
352 Result := pthread_cond_wait
353 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
356 -- EINTR is not considered a failure.
358 pragma Assert (Result = 0 or else Result = EINTR);
365 procedure Timed_Sleep
368 Mode : ST.Delay_Modes;
369 Reason : System.Tasking.Task_States;
370 Timedout : out Boolean;
371 Yielded : out Boolean)
373 pragma Unreferenced (Reason);
375 Check_Time : constant Duration := Monotonic_Clock;
377 Request : aliased struct_timeval;
378 Result : Interfaces.C.int;
384 if Mode = Relative then
385 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
387 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
390 if Abs_Time > Check_Time then
391 Request := To_Timeval (Abs_Time);
394 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
395 or else Self_ID.Pending_Priority_Change;
398 Result := pthread_cond_timedwait
399 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
403 Result := pthread_cond_timedwait
404 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
408 exit when Abs_Time <= Monotonic_Clock;
410 if Result = 0 or Result = EINTR then
411 -- somebody may have called Wakeup for us
416 pragma Assert (Result = ETIMEDOUT
417 or else (Result = -1 and then errno = EAGAIN));
426 procedure Timed_Delay
429 Mode : ST.Delay_Modes)
431 Check_Time : constant Duration := Monotonic_Clock;
433 Request : aliased struct_timeval;
434 Result : Interfaces.C.int;
437 -- Only the little window between deferring abort and
438 -- locking Self_ID is the reason we need to
439 -- check for pending abort and priority change below!
447 Write_Lock (Self_ID);
449 if Mode = Relative then
450 Abs_Time := Time + Check_Time;
452 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
455 if Abs_Time > Check_Time then
456 Request := To_Timeval (Abs_Time);
457 Self_ID.Common.State := Delay_Sleep;
460 if Self_ID.Pending_Priority_Change then
461 Self_ID.Pending_Priority_Change := False;
462 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
463 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
466 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
469 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
470 Single_RTS_Lock'Access, Request'Access);
472 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
473 Self_ID.Common.LL.L'Access, Request'Access);
476 exit when Abs_Time <= Monotonic_Clock;
478 pragma Assert (Result = 0 or else
479 Result = ETIMEDOUT or else
480 (Result = -1 and then errno = EAGAIN) or else
484 Self_ID.Common.State := Runnable;
494 SSL.Abort_Undefer.all;
497 ---------------------
498 -- Monotonic_Clock --
499 ---------------------
501 function Monotonic_Clock return Duration is
502 type timeval is record
506 pragma Convention (C, timeval);
508 tv : aliased timeval;
510 procedure gettimeofday (tp : access timeval);
511 pragma Import (C, gettimeofday, "gettimeofday", "gettimeofday");
514 gettimeofday (tv'Access);
515 return Duration (tv.tv_sec) + Duration (tv.tv_usec) / 1_000_000.0;
522 function RT_Resolution return Duration is
533 Reason : System.Tasking.Task_States)
535 pragma Unreferenced (Reason);
536 Result : Interfaces.C.int;
538 Result := pthread_cond_signal (T.Common.LL.CV'Access);
539 pragma Assert (Result = 0);
546 procedure Yield (Do_Yield : Boolean := True) is
557 procedure Set_Priority
559 Prio : System.Any_Priority;
560 Loss_Of_Inheritance : Boolean := False)
562 pragma Unreferenced (Loss_Of_Inheritance);
564 Result : Interfaces.C.int;
567 T.Common.Current_Priority := Prio;
568 Result := pthread_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
569 pragma Assert (Result /= FUNC_ERR);
577 function Get_Priority (T : Task_ID) return System.Any_Priority is
579 return T.Common.Current_Priority;
586 procedure Enter_Task (Self_ID : Task_ID) is
587 Result : Interfaces.C.int;
590 Self_ID.Common.LL.Thread := pthread_self;
591 Self_ID.Common.LL.LWP := sproc_self;
594 pthread_set_ada_tcb (Self_ID.Common.LL.Thread, To_Address (Self_ID));
596 pragma Assert (Result = 0);
600 for J in Known_Tasks'Range loop
601 if Known_Tasks (J) = null then
602 Known_Tasks (J) := Self_ID;
603 Self_ID.Known_Tasks_Index := J;
615 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
617 return new Ada_Task_Control_Block (Entry_Num);
624 function Is_Valid_Task return Boolean is
629 -----------------------------
630 -- Register_Foreign_Thread --
631 -----------------------------
633 function Register_Foreign_Thread return Task_ID is
636 end Register_Foreign_Thread;
638 ----------------------
640 ----------------------
642 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
643 Result : Interfaces.C.int;
644 Cond_Attr : aliased pthread_condattr_t;
647 if not Single_Lock then
648 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
651 Result := pthread_condattr_init (Cond_Attr'Access);
652 pragma Assert (Result = 0 or else Result = ENOMEM);
655 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
657 pragma Assert (Result = 0 or else Result = ENOMEM);
663 if not Single_Lock then
664 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
665 pragma Assert (Result = 0);
671 Result := pthread_condattr_destroy (Cond_Attr'Access);
672 pragma Assert (Result = 0);
679 procedure Create_Task
681 Wrapper : System.Address;
682 Stack_Size : System.Parameters.Size_Type;
683 Priority : System.Any_Priority;
684 Succeeded : out Boolean)
686 Attributes : aliased pthread_attr_t;
687 Adjusted_Stack_Size : Interfaces.C.size_t;
688 Result : Interfaces.C.int;
690 function Thread_Body_Access is new
691 Unchecked_Conversion (System.Address, start_addr);
693 function To_Resource_T is new Unchecked_Conversion
694 (System.Task_Info.Resource_Vector_T, System.OS_Interface.resource_t);
696 use System.Task_Info;
699 if Stack_Size = Unspecified_Size then
700 Adjusted_Stack_Size :=
701 Interfaces.C.size_t (System.Program_Info.Default_Task_Stack);
703 elsif Stack_Size < Minimum_Stack_Size then
704 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
707 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
710 Result := pthread_attr_init (Attributes'Access);
711 pragma Assert (Result = 0 or else Result = ENOMEM);
718 Result := pthread_attr_setdetachstate (Attributes'Access, 1);
719 pragma Assert (Result = 0);
721 Result := pthread_attr_setstacksize
722 (Attributes'Access, Adjusted_Stack_Size);
723 pragma Assert (Result = 0);
725 if T.Common.Task_Info /= null then
726 Result := pthread_attr_setresources
728 To_Resource_T (T.Common.Task_Info.Thread_Resources));
729 pragma Assert (Result /= FUNC_ERR);
731 if T.Common.Task_Info.Thread_Timeslice /= 0.0 then
733 use System.OS_Interface;
735 Tv : aliased struct_timeval := To_Timeval
736 (T.Common.Task_Info.Thread_Timeslice);
738 Result := pthread_attr_set_tslice
739 (Attributes'Access, Tv'Access);
743 if T.Common.Task_Info.Bound_To_Sproc then
744 Result := pthread_attr_set_boundtosproc
745 (Attributes'Access, PTHREAD_BOUND);
746 Result := pthread_attr_set_bsproc
747 (Attributes'Access, T.Common.Task_Info.Sproc);
752 -- Since the initial signal mask of a thread is inherited from the
753 -- creator, and the Environment task has all its signals masked, we
754 -- do not need to manipulate caller's signal mask at this point.
755 -- All tasks in RTS will have All_Tasks_Mask initially.
757 Result := pthread_create
758 (T.Common.LL.Thread'Access,
760 Thread_Body_Access (Wrapper),
762 pragma Assert (Result = 0 or else Result = EAGAIN);
764 Succeeded := Result = 0;
766 Set_Priority (T, Priority);
768 Result := pthread_attr_destroy (Attributes'Access);
769 pragma Assert (Result /= FUNC_ERR);
776 procedure Finalize_TCB (T : Task_ID) is
777 procedure Free is new
778 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
780 Result : Interfaces.C.int;
784 if not Single_Lock then
785 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
786 pragma Assert (Result = 0);
789 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
790 pragma Assert (Result = 0);
792 if T.Known_Tasks_Index /= -1 then
793 Known_Tasks (T.Known_Tasks_Index) := null;
803 procedure Exit_Task is
804 Result : Interfaces.C.int;
806 Result := pthread_set_ada_tcb (pthread_self, System.Null_Address);
807 pragma Assert (Result = 0);
814 procedure Abort_Task (T : Task_ID) is
815 Result : Interfaces.C.int;
818 pthread_kill (T.Common.LL.Thread,
820 (System.Interrupt_Management.Abort_Task_Interrupt));
821 pragma Assert (Result = 0);
830 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
831 pragma Unreferenced (Self_ID);
841 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
842 pragma Unreferenced (Self_ID);
847 ----------------------
848 -- Environment_Task --
849 ----------------------
851 function Environment_Task return Task_ID is
853 return Environment_Task_ID;
854 end Environment_Task;
860 procedure Lock_RTS is
862 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
869 procedure Unlock_RTS is
871 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
878 function Suspend_Task
880 Thread_Self : Thread_Id) return Boolean
883 if T.Common.LL.Thread /= Thread_Self then
884 return pthread_suspend (T.Common.LL.Thread) = 0;
896 Thread_Self : Thread_Id) return Boolean
899 if T.Common.LL.Thread /= Thread_Self then
900 return pthread_resume (T.Common.LL.Thread) = 0;
910 procedure Initialize (Environment_Task : Task_ID) is
912 Environment_Task_ID := Environment_Task;
914 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
915 -- Initialize the lock used to synchronize chain of all ATCBs.
917 Enter_Task (Environment_Task);
919 Set_Priority (Environment_Task,
920 Environment_Task.Common.Current_Priority);
923 --------------------------------
924 -- Initialize_Athread_Library --
925 --------------------------------
927 procedure Initialize_Athread_Library is
928 Result : Interfaces.C.int;
929 Init : aliased pthread_init_struct;
931 package PINF renames System.Program_Info;
932 package C renames Interfaces.C;
935 Init.conf_initsize := C.int (PINF.Pthread_Arena_Size);
936 Init.max_sproc_count := C.int (PINF.Max_Sproc_Count);
937 Init.sproc_stack_size := C.size_t (PINF.Sproc_Stack_Size);
938 Init.os_default_priority := C.int (PINF.Os_Default_Priority);
939 Init.os_sched_signal := C.int (PINF.Pthread_Sched_Signal);
940 Init.guard_pages := C.int (PINF.Stack_Guard_Pages);
941 Init.init_sproc_count := C.int (PINF.Initial_Sproc_Count);
943 Result := pthread_exec_begin (Init'Access);
944 pragma Assert (Result /= FUNC_ERR);
946 if Result = FUNC_ERR then
947 raise Storage_Error; -- Insufficient resources.
949 end Initialize_Athread_Library;
951 -- Package initialization
954 Initialize_Athread_Library;
955 end System.Task_Primitives.Operations;