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 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
150 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
152 return T.Common.LL.Thread;
159 function Self return Task_ID is
161 return To_Task_ID (pthread_get_current_ada_tcb);
164 ---------------------
165 -- Initialize_Lock --
166 ---------------------
168 -- Note: mutexes and cond_variables needed per-task basis are
169 -- initialized in Initialize_TCB and the Storage_Error is
170 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
171 -- used in RTS is initialized before any status change of RTS.
172 -- Therefore rasing Storage_Error in the following routines
173 -- should be able to be handled safely.
175 procedure Initialize_Lock
176 (Prio : System.Any_Priority;
179 Attributes : aliased pthread_mutexattr_t;
180 Result : Interfaces.C.int;
183 Result := pthread_mutexattr_init (Attributes'Access);
185 if Result = FUNC_ERR then
189 if Locking_Policy = 'C' then
191 Result := pthread_mutexattr_setqueueorder
192 (Attributes'Access, MUTEX_PRIORITY_CEILING);
194 pragma Assert (Result /= FUNC_ERR);
196 Result := pthread_mutexattr_setceilingprio
197 (Attributes'Access, Interfaces.C.int (Prio));
199 pragma Assert (Result /= FUNC_ERR);
202 Result := pthread_mutex_init (L, Attributes'Access);
204 if Result = FUNC_ERR then
205 Result := pthread_mutexattr_destroy (Attributes'Access);
209 Result := pthread_mutexattr_destroy (Attributes'Access);
212 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
213 Attributes : aliased pthread_mutexattr_t;
214 Result : Interfaces.C.int;
217 Result := pthread_mutexattr_init (Attributes'Access);
219 if Result = FUNC_ERR then
223 if Locking_Policy = 'C' then
224 Result := pthread_mutexattr_setqueueorder
225 (Attributes'Access, MUTEX_PRIORITY_CEILING);
226 pragma Assert (Result /= FUNC_ERR);
228 Result := pthread_mutexattr_setceilingprio
229 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
230 pragma Assert (Result /= FUNC_ERR);
233 Result := pthread_mutex_init (L, Attributes'Access);
235 if Result = FUNC_ERR then
236 Result := pthread_mutexattr_destroy (Attributes'Access);
240 Result := pthread_mutexattr_destroy (Attributes'Access);
247 procedure Finalize_Lock (L : access Lock) is
248 Result : Interfaces.C.int;
251 Result := pthread_mutex_destroy (L);
252 pragma Assert (Result = 0);
255 procedure Finalize_Lock (L : access RTS_Lock) is
256 Result : Interfaces.C.int;
259 Result := pthread_mutex_destroy (L);
260 pragma Assert (Result = 0);
267 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
268 Result : Interfaces.C.int;
271 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;
283 if not Single_Lock or else Global_Lock then
284 Result := pthread_mutex_lock (L);
285 pragma Assert (Result = 0);
289 procedure Write_Lock (T : Task_ID) is
290 Result : Interfaces.C.int;
293 if not Single_Lock then
294 Result := pthread_mutex_lock (T.Common.LL.L'Access);
295 pragma Assert (Result = 0);
303 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
305 Write_Lock (L, Ceiling_Violation);
312 procedure Unlock (L : access Lock) is
313 Result : Interfaces.C.int;
316 Result := pthread_mutex_unlock (L);
317 pragma Assert (Result = 0);
320 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
321 Result : Interfaces.C.int;
324 if not Single_Lock or else Global_Lock then
325 Result := pthread_mutex_unlock (L);
326 pragma Assert (Result = 0);
330 procedure Unlock (T : Task_ID) is
331 Result : Interfaces.C.int;
334 if not Single_Lock then
335 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
336 pragma Assert (Result = 0);
345 (Self_ID : ST.Task_ID;
346 Reason : System.Tasking.Task_States)
348 Result : Interfaces.C.int;
352 Result := pthread_cond_wait
353 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
355 Result := pthread_cond_wait
356 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
359 -- EINTR is not considered a failure.
361 pragma Assert (Result = 0 or else Result = EINTR);
368 procedure Timed_Sleep
371 Mode : ST.Delay_Modes;
372 Reason : System.Tasking.Task_States;
373 Timedout : out Boolean;
374 Yielded : out Boolean)
376 Check_Time : constant Duration := Monotonic_Clock;
378 Request : aliased struct_timeval;
379 Result : Interfaces.C.int;
385 if Mode = Relative then
386 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
388 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
391 if Abs_Time > Check_Time then
392 Request := To_Timeval (Abs_Time);
395 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
396 or else Self_ID.Pending_Priority_Change;
399 Result := pthread_cond_timedwait
400 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
404 Result := pthread_cond_timedwait
405 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
409 exit when Abs_Time <= Monotonic_Clock;
411 if Result = 0 or Result = EINTR then
412 -- somebody may have called Wakeup for us
417 pragma Assert (Result = ETIMEDOUT
418 or else (Result = -1 and then errno = EAGAIN));
427 procedure Timed_Delay
430 Mode : ST.Delay_Modes)
432 Check_Time : constant Duration := Monotonic_Clock;
434 Request : aliased struct_timeval;
435 Result : Interfaces.C.int;
438 -- Only the little window between deferring abort and
439 -- locking Self_ID is the reason we need to
440 -- check for pending abort and priority change below!
448 Write_Lock (Self_ID);
450 if Mode = Relative then
451 Abs_Time := Time + Check_Time;
453 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
456 if Abs_Time > Check_Time then
457 Request := To_Timeval (Abs_Time);
458 Self_ID.Common.State := Delay_Sleep;
461 if Self_ID.Pending_Priority_Change then
462 Self_ID.Pending_Priority_Change := False;
463 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
464 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
467 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
470 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
471 Single_RTS_Lock'Access, Request'Access);
473 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
474 Self_ID.Common.LL.L'Access, Request'Access);
477 exit when Abs_Time <= Monotonic_Clock;
479 pragma Assert (Result = 0 or else
480 Result = ETIMEDOUT or else
481 (Result = -1 and then errno = EAGAIN) or else
485 Self_ID.Common.State := Runnable;
495 SSL.Abort_Undefer.all;
498 ---------------------
499 -- Monotonic_Clock --
500 ---------------------
502 function Monotonic_Clock return Duration is
503 type timeval is record
507 pragma Convention (C, timeval);
509 tv : aliased timeval;
511 procedure gettimeofday (tp : access timeval);
512 pragma Import (C, gettimeofday, "gettimeofday", "gettimeofday");
515 gettimeofday (tv'Access);
516 return Duration (tv.tv_sec) + Duration (tv.tv_usec) / 1_000_000.0;
523 function RT_Resolution return Duration is
534 Reason : System.Tasking.Task_States)
536 Result : Interfaces.C.int;
539 Result := pthread_cond_signal (T.Common.LL.CV'Access);
540 pragma Assert (Result = 0);
547 procedure Yield (Do_Yield : Boolean := True) is
558 procedure Set_Priority
560 Prio : System.Any_Priority;
561 Loss_Of_Inheritance : Boolean := False)
563 pragma Unreferenced (Loss_Of_Inheritance);
565 Result : Interfaces.C.int;
568 T.Common.Current_Priority := Prio;
569 Result := pthread_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
570 pragma Assert (Result /= FUNC_ERR);
578 function Get_Priority (T : Task_ID) return System.Any_Priority is
580 return T.Common.Current_Priority;
587 procedure Enter_Task (Self_ID : Task_ID) is
588 Result : Interfaces.C.int;
591 Self_ID.Common.LL.Thread := pthread_self;
592 Self_ID.Common.LL.LWP := sproc_self;
595 pthread_set_ada_tcb (Self_ID.Common.LL.Thread, To_Address (Self_ID));
597 pragma Assert (Result = 0);
601 for J in Known_Tasks'Range loop
602 if Known_Tasks (J) = null then
603 Known_Tasks (J) := Self_ID;
604 Self_ID.Known_Tasks_Index := J;
616 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
618 return new Ada_Task_Control_Block (Entry_Num);
625 function Is_Valid_Task return Boolean is
630 -----------------------------
631 -- Register_Foreign_Thread --
632 -----------------------------
634 function Register_Foreign_Thread return Task_ID is
637 end Register_Foreign_Thread;
639 ----------------------
641 ----------------------
643 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
644 Result : Interfaces.C.int;
645 Cond_Attr : aliased pthread_condattr_t;
648 if not Single_Lock then
649 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
652 Result := pthread_condattr_init (Cond_Attr'Access);
653 pragma Assert (Result = 0 or else Result = ENOMEM);
656 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
658 pragma Assert (Result = 0 or else Result = ENOMEM);
664 if not Single_Lock then
665 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
666 pragma Assert (Result = 0);
672 Result := pthread_condattr_destroy (Cond_Attr'Access);
673 pragma Assert (Result = 0);
680 procedure Create_Task
682 Wrapper : System.Address;
683 Stack_Size : System.Parameters.Size_Type;
684 Priority : System.Any_Priority;
685 Succeeded : out Boolean)
687 Attributes : aliased pthread_attr_t;
688 Adjusted_Stack_Size : Interfaces.C.size_t;
689 Result : Interfaces.C.int;
691 function Thread_Body_Access is new
692 Unchecked_Conversion (System.Address, start_addr);
694 function To_Resource_T is new Unchecked_Conversion
695 (System.Task_Info.Resource_Vector_T, System.OS_Interface.resource_t);
697 use System.Task_Info;
700 if Stack_Size = Unspecified_Size then
701 Adjusted_Stack_Size :=
702 Interfaces.C.size_t (System.Program_Info.Default_Task_Stack);
704 elsif Stack_Size < Minimum_Stack_Size then
705 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
708 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
711 Result := pthread_attr_init (Attributes'Access);
712 pragma Assert (Result = 0 or else Result = ENOMEM);
719 Result := pthread_attr_setdetachstate (Attributes'Access, 1);
720 pragma Assert (Result = 0);
722 Result := pthread_attr_setstacksize
723 (Attributes'Access, Adjusted_Stack_Size);
724 pragma Assert (Result = 0);
726 if T.Common.Task_Info /= null then
727 Result := pthread_attr_setresources
729 To_Resource_T (T.Common.Task_Info.Thread_Resources));
730 pragma Assert (Result /= FUNC_ERR);
732 if T.Common.Task_Info.Thread_Timeslice /= 0.0 then
734 use System.OS_Interface;
736 Tv : aliased struct_timeval := To_Timeval
737 (T.Common.Task_Info.Thread_Timeslice);
739 Result := pthread_attr_set_tslice
740 (Attributes'Access, Tv'Access);
744 if T.Common.Task_Info.Bound_To_Sproc then
745 Result := pthread_attr_set_boundtosproc
746 (Attributes'Access, PTHREAD_BOUND);
747 Result := pthread_attr_set_bsproc
748 (Attributes'Access, T.Common.Task_Info.Sproc);
753 -- Since the initial signal mask of a thread is inherited from the
754 -- creator, and the Environment task has all its signals masked, we
755 -- do not need to manipulate caller's signal mask at this point.
756 -- All tasks in RTS will have All_Tasks_Mask initially.
758 Result := pthread_create
759 (T.Common.LL.Thread'Access,
761 Thread_Body_Access (Wrapper),
763 pragma Assert (Result = 0 or else Result = EAGAIN);
765 Succeeded := Result = 0;
767 Set_Priority (T, Priority);
769 Result := pthread_attr_destroy (Attributes'Access);
770 pragma Assert (Result /= FUNC_ERR);
777 procedure Finalize_TCB (T : Task_ID) is
778 procedure Free is new
779 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
781 Result : Interfaces.C.int;
785 if not Single_Lock then
786 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
787 pragma Assert (Result = 0);
790 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
791 pragma Assert (Result = 0);
793 if T.Known_Tasks_Index /= -1 then
794 Known_Tasks (T.Known_Tasks_Index) := null;
804 procedure Exit_Task is
805 Result : Interfaces.C.int;
808 Result := pthread_set_ada_tcb (pthread_self, System.Null_Address);
815 procedure Abort_Task (T : Task_ID) is
816 Result : Interfaces.C.int;
820 pthread_kill (T.Common.LL.Thread,
822 (System.Interrupt_Management.Abort_Task_Interrupt));
823 pragma Assert (Result = 0);
832 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
833 pragma Unreferenced (Self_ID);
843 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
848 ----------------------
849 -- Environment_Task --
850 ----------------------
852 function Environment_Task return Task_ID is
854 return Environment_Task_ID;
855 end Environment_Task;
861 procedure Lock_RTS is
863 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
870 procedure Unlock_RTS is
872 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
879 function Suspend_Task
881 Thread_Self : Thread_Id)
885 if T.Common.LL.Thread /= Thread_Self then
886 return pthread_suspend (T.Common.LL.Thread) = 0;
898 Thread_Self : Thread_Id)
902 if T.Common.LL.Thread /= Thread_Self then
903 return pthread_resume (T.Common.LL.Thread) = 0;
913 procedure Initialize (Environment_Task : Task_ID) is
915 Environment_Task_ID := Environment_Task;
917 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
918 -- Initialize the lock used to synchronize chain of all ATCBs.
920 Enter_Task (Environment_Task);
922 Set_Priority (Environment_Task,
923 Environment_Task.Common.Current_Priority);
926 --------------------------------
927 -- Initialize_Athread_Library --
928 --------------------------------
930 procedure Initialize_Athread_Library is
931 Result : Interfaces.C.int;
932 Init : aliased pthread_init_struct;
934 package PINF renames System.Program_Info;
935 package C renames Interfaces.C;
938 Init.conf_initsize := C.int (PINF.Pthread_Arena_Size);
939 Init.max_sproc_count := C.int (PINF.Max_Sproc_Count);
940 Init.sproc_stack_size := C.size_t (PINF.Sproc_Stack_Size);
941 Init.os_default_priority := C.int (PINF.Os_Default_Priority);
942 Init.os_sched_signal := C.int (PINF.Pthread_Sched_Signal);
943 Init.guard_pages := C.int (PINF.Stack_Guard_Pages);
944 Init.init_sproc_count := C.int (PINF.Initial_Sproc_Count);
946 Result := pthread_exec_begin (Init'Access);
947 pragma Assert (Result /= FUNC_ERR);
949 if Result = FUNC_ERR then
950 raise Storage_Error; -- Insufficient resources.
953 end Initialize_Athread_Library;
956 Initialize_Athread_Library;
957 end System.Task_Primitives.Operations;