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
);
153 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
155 return T
.Common
.LL
.Thread
;
162 function Self
return Task_ID
is
164 return To_Task_ID
(pthread_get_current_ada_tcb
);
167 ---------------------
168 -- Initialize_Lock --
169 ---------------------
171 -- Note: mutexes and cond_variables needed per-task basis are
172 -- initialized in Initialize_TCB and the Storage_Error is
173 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
174 -- used in RTS is initialized before any status change of RTS.
175 -- Therefore rasing Storage_Error in the following routines
176 -- should be able to be handled safely.
178 procedure Initialize_Lock
179 (Prio
: System
.Any_Priority
;
182 Attributes
: aliased pthread_mutexattr_t
;
183 Result
: Interfaces
.C
.int
;
186 Result
:= pthread_mutexattr_init
(Attributes
'Access);
188 if Result
= FUNC_ERR
then
192 if Locking_Policy
= 'C' then
194 Result
:= pthread_mutexattr_setqueueorder
195 (Attributes
'Access, MUTEX_PRIORITY_CEILING
);
197 pragma Assert
(Result
/= FUNC_ERR
);
199 Result
:= pthread_mutexattr_setceilingprio
200 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
202 pragma Assert
(Result
/= FUNC_ERR
);
205 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
207 if Result
= FUNC_ERR
then
208 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
212 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
215 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
216 pragma Unreferenced
(Level
);
218 Attributes
: aliased pthread_mutexattr_t
;
219 Result
: Interfaces
.C
.int
;
222 Result
:= pthread_mutexattr_init
(Attributes
'Access);
224 if Result
= FUNC_ERR
then
228 if Locking_Policy
= 'C' then
229 Result
:= pthread_mutexattr_setqueueorder
230 (Attributes
'Access, MUTEX_PRIORITY_CEILING
);
231 pragma Assert
(Result
/= FUNC_ERR
);
233 Result
:= pthread_mutexattr_setceilingprio
234 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
235 pragma Assert
(Result
/= FUNC_ERR
);
238 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
240 if Result
= FUNC_ERR
then
241 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
245 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
252 procedure Finalize_Lock
(L
: access Lock
) is
253 Result
: Interfaces
.C
.int
;
256 Result
:= pthread_mutex_destroy
(L
);
257 pragma Assert
(Result
= 0);
260 procedure Finalize_Lock
(L
: access RTS_Lock
) is
261 Result
: Interfaces
.C
.int
;
264 Result
:= pthread_mutex_destroy
(L
);
265 pragma Assert
(Result
= 0);
272 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
273 Result
: Interfaces
.C
.int
;
276 Result
:= pthread_mutex_lock
(L
);
278 Ceiling_Violation
:= Result
= FUNC_ERR
and then errno
= EINVAL
;
279 pragma Assert
(Result
/= FUNC_ERR
);
283 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
285 Result
: Interfaces
.C
.int
;
288 if not Single_Lock
or else Global_Lock
then
289 Result
:= pthread_mutex_lock
(L
);
290 pragma Assert
(Result
= 0);
294 procedure Write_Lock
(T
: Task_ID
) is
295 Result
: Interfaces
.C
.int
;
298 if not Single_Lock
then
299 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
300 pragma Assert
(Result
= 0);
308 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
310 Write_Lock
(L
, Ceiling_Violation
);
317 procedure Unlock
(L
: access Lock
) is
318 Result
: Interfaces
.C
.int
;
321 Result
:= pthread_mutex_unlock
(L
);
322 pragma Assert
(Result
= 0);
325 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
326 Result
: Interfaces
.C
.int
;
329 if not Single_Lock
or else Global_Lock
then
330 Result
:= pthread_mutex_unlock
(L
);
331 pragma Assert
(Result
= 0);
335 procedure Unlock
(T
: Task_ID
) is
336 Result
: Interfaces
.C
.int
;
339 if not Single_Lock
then
340 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
341 pragma Assert
(Result
= 0);
350 (Self_ID
: ST
.Task_ID
;
351 Reason
: System
.Tasking
.Task_States
)
353 pragma Unreferenced
(Reason
);
355 Result
: Interfaces
.C
.int
;
359 Result
:= pthread_cond_wait
360 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
362 Result
:= pthread_cond_wait
363 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
366 -- EINTR is not considered a failure.
368 pragma Assert
(Result
= 0 or else Result
= EINTR
);
375 procedure Timed_Sleep
378 Mode
: ST
.Delay_Modes
;
379 Reason
: System
.Tasking
.Task_States
;
380 Timedout
: out Boolean;
381 Yielded
: out Boolean)
383 pragma Unreferenced
(Reason
);
385 Check_Time
: constant Duration := Monotonic_Clock
;
387 Request
: aliased struct_timeval
;
388 Result
: Interfaces
.C
.int
;
394 if Mode
= Relative
then
395 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
397 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
400 if Abs_Time
> Check_Time
then
401 Request
:= To_Timeval
(Abs_Time
);
404 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
405 or else Self_ID
.Pending_Priority_Change
;
408 Result
:= pthread_cond_timedwait
409 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
413 Result
:= pthread_cond_timedwait
414 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
418 exit when Abs_Time
<= Monotonic_Clock
;
420 if Result
= 0 or Result
= EINTR
then
421 -- somebody may have called Wakeup for us
426 pragma Assert
(Result
= ETIMEDOUT
427 or else (Result
= -1 and then errno
= EAGAIN
));
436 procedure Timed_Delay
439 Mode
: ST
.Delay_Modes
)
441 Check_Time
: constant Duration := Monotonic_Clock
;
443 Request
: aliased struct_timeval
;
444 Result
: Interfaces
.C
.int
;
447 -- Only the little window between deferring abort and
448 -- locking Self_ID is the reason we need to
449 -- check for pending abort and priority change below!
457 Write_Lock
(Self_ID
);
459 if Mode
= Relative
then
460 Abs_Time
:= Time
+ Check_Time
;
462 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
465 if Abs_Time
> Check_Time
then
466 Request
:= To_Timeval
(Abs_Time
);
467 Self_ID
.Common
.State
:= Delay_Sleep
;
470 if Self_ID
.Pending_Priority_Change
then
471 Self_ID
.Pending_Priority_Change
:= False;
472 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
473 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
476 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
479 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
480 Single_RTS_Lock
'Access, Request
'Access);
482 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
483 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
486 exit when Abs_Time
<= Monotonic_Clock
;
488 pragma Assert
(Result
= 0 or else
489 Result
= ETIMEDOUT
or else
490 (Result
= -1 and then errno
= EAGAIN
) or else
494 Self_ID
.Common
.State
:= Runnable
;
504 SSL
.Abort_Undefer
.all;
507 ---------------------
508 -- Monotonic_Clock --
509 ---------------------
511 function Monotonic_Clock
return Duration is
512 type timeval
is record
516 pragma Convention
(C
, timeval
);
518 tv
: aliased timeval
;
520 procedure gettimeofday
(tp
: access timeval
);
521 pragma Import
(C
, gettimeofday
, "gettimeofday", "gettimeofday");
524 gettimeofday
(tv
'Access);
525 return Duration (tv
.tv_sec
) + Duration (tv
.tv_usec
) / 1_000_000
.0
;
532 function RT_Resolution
return Duration is
543 Reason
: System
.Tasking
.Task_States
)
545 pragma Unreferenced
(Reason
);
547 Result
: Interfaces
.C
.int
;
549 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
550 pragma Assert
(Result
= 0);
557 procedure Yield
(Do_Yield
: Boolean := True) is
568 procedure Set_Priority
570 Prio
: System
.Any_Priority
;
571 Loss_Of_Inheritance
: Boolean := False)
573 pragma Unreferenced
(Loss_Of_Inheritance
);
575 Result
: Interfaces
.C
.int
;
578 T
.Common
.Current_Priority
:= Prio
;
579 Result
:= pthread_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
580 pragma Assert
(Result
/= FUNC_ERR
);
588 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
590 return T
.Common
.Current_Priority
;
597 procedure Enter_Task
(Self_ID
: Task_ID
) is
598 Result
: Interfaces
.C
.int
;
601 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
602 Self_ID
.Common
.LL
.LWP
:= sproc_self
;
605 pthread_set_ada_tcb
(Self_ID
.Common
.LL
.Thread
, To_Address
(Self_ID
));
607 pragma Assert
(Result
= 0);
611 for J
in Known_Tasks
'Range loop
612 if Known_Tasks
(J
) = null then
613 Known_Tasks
(J
) := Self_ID
;
614 Self_ID
.Known_Tasks_Index
:= J
;
626 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
628 return new Ada_Task_Control_Block
(Entry_Num
);
635 function Is_Valid_Task
return Boolean is
640 -----------------------------
641 -- Register_Foreign_Thread --
642 -----------------------------
644 function Register_Foreign_Thread
return Task_ID
is
647 end Register_Foreign_Thread
;
649 ----------------------
651 ----------------------
653 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
654 Result
: Interfaces
.C
.int
;
655 Cond_Attr
: aliased pthread_condattr_t
;
658 if not Single_Lock
then
659 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
662 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
663 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
666 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
668 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
674 if not Single_Lock
then
675 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
676 pragma Assert
(Result
= 0);
682 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
683 pragma Assert
(Result
= 0);
690 procedure Create_Task
692 Wrapper
: System
.Address
;
693 Stack_Size
: System
.Parameters
.Size_Type
;
694 Priority
: System
.Any_Priority
;
695 Succeeded
: out Boolean)
697 Attributes
: aliased pthread_attr_t
;
698 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
699 Result
: Interfaces
.C
.int
;
701 function Thread_Body_Access
is new
702 Unchecked_Conversion
(System
.Address
, start_addr
);
704 function To_Resource_T
is new Unchecked_Conversion
705 (System
.Task_Info
.Resource_Vector_T
, System
.OS_Interface
.resource_t
);
707 use System
.Task_Info
;
710 if Stack_Size
= Unspecified_Size
then
711 Adjusted_Stack_Size
:=
712 Interfaces
.C
.size_t
(System
.Program_Info
.Default_Task_Stack
);
714 elsif Stack_Size
< Minimum_Stack_Size
then
715 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
718 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
721 Result
:= pthread_attr_init
(Attributes
'Access);
722 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
729 Result
:= pthread_attr_setdetachstate
(Attributes
'Access, 1);
730 pragma Assert
(Result
= 0);
732 Result
:= pthread_attr_setstacksize
733 (Attributes
'Access, Adjusted_Stack_Size
);
734 pragma Assert
(Result
= 0);
736 if T
.Common
.Task_Info
/= null then
737 Result
:= pthread_attr_setresources
739 To_Resource_T
(T
.Common
.Task_Info
.Thread_Resources
));
740 pragma Assert
(Result
/= FUNC_ERR
);
742 if T
.Common
.Task_Info
.Thread_Timeslice
/= 0.0 then
744 use System
.OS_Interface
;
746 Tv
: aliased struct_timeval
:= To_Timeval
747 (T
.Common
.Task_Info
.Thread_Timeslice
);
749 Result
:= pthread_attr_set_tslice
750 (Attributes
'Access, Tv
'Access);
754 if T
.Common
.Task_Info
.Bound_To_Sproc
then
755 Result
:= pthread_attr_set_boundtosproc
756 (Attributes
'Access, PTHREAD_BOUND
);
757 Result
:= pthread_attr_set_bsproc
758 (Attributes
'Access, T
.Common
.Task_Info
.Sproc
);
763 -- Since the initial signal mask of a thread is inherited from the
764 -- creator, and the Environment task has all its signals masked, we
765 -- do not need to manipulate caller's signal mask at this point.
766 -- All tasks in RTS will have All_Tasks_Mask initially.
768 Result
:= pthread_create
769 (T
.Common
.LL
.Thread
'Access,
771 Thread_Body_Access
(Wrapper
),
773 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
775 Succeeded
:= Result
= 0;
777 Set_Priority
(T
, Priority
);
779 Result
:= pthread_attr_destroy
(Attributes
'Access);
780 pragma Assert
(Result
/= FUNC_ERR
);
787 procedure Finalize_TCB
(T
: Task_ID
) is
788 procedure Free
is new
789 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
791 Result
: Interfaces
.C
.int
;
795 if not Single_Lock
then
796 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
797 pragma Assert
(Result
= 0);
800 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
801 pragma Assert
(Result
= 0);
803 if T
.Known_Tasks_Index
/= -1 then
804 Known_Tasks
(T
.Known_Tasks_Index
) := null;
814 procedure Exit_Task
is
815 Result
: Interfaces
.C
.int
;
818 Result
:= pthread_set_ada_tcb
(pthread_self
, System
.Null_Address
);
820 pragma Assert
(Result
= 0);
827 procedure Abort_Task
(T
: Task_ID
) is
828 Result
: Interfaces
.C
.int
;
832 pthread_kill
(T
.Common
.LL
.Thread
,
834 (System
.Interrupt_Management
.Abort_Task_Interrupt
));
835 pragma Assert
(Result
= 0);
844 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
845 pragma Unreferenced
(Self_ID
);
855 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
856 pragma Unreferenced
(Self_ID
);
862 ----------------------
863 -- Environment_Task --
864 ----------------------
866 function Environment_Task
return Task_ID
is
868 return Environment_Task_ID
;
869 end Environment_Task
;
875 procedure Lock_RTS
is
877 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
884 procedure Unlock_RTS
is
886 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
893 function Suspend_Task
895 Thread_Self
: Thread_Id
) return Boolean
898 if T
.Common
.LL
.Thread
/= Thread_Self
then
899 return pthread_suspend
(T
.Common
.LL
.Thread
) = 0;
911 Thread_Self
: Thread_Id
) return Boolean
914 if T
.Common
.LL
.Thread
/= Thread_Self
then
915 return pthread_resume
(T
.Common
.LL
.Thread
) = 0;
925 procedure Initialize
(Environment_Task
: Task_ID
) is
927 Environment_Task_ID
:= Environment_Task
;
929 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
930 -- Initialize the lock used to synchronize chain of all ATCBs.
932 Enter_Task
(Environment_Task
);
934 Set_Priority
(Environment_Task
,
935 Environment_Task
.Common
.Current_Priority
);
938 --------------------------------
939 -- Initialize_Athread_Library --
940 --------------------------------
942 procedure Initialize_Athread_Library
is
943 Result
: Interfaces
.C
.int
;
944 Init
: aliased pthread_init_struct
;
946 package PINF
renames System
.Program_Info
;
947 package C
renames Interfaces
.C
;
950 Init
.conf_initsize
:= C
.int
(PINF
.Pthread_Arena_Size
);
951 Init
.max_sproc_count
:= C
.int
(PINF
.Max_Sproc_Count
);
952 Init
.sproc_stack_size
:= C
.size_t
(PINF
.Sproc_Stack_Size
);
953 Init
.os_default_priority
:= C
.int
(PINF
.Os_Default_Priority
);
954 Init
.os_sched_signal
:= C
.int
(PINF
.Pthread_Sched_Signal
);
955 Init
.guard_pages
:= C
.int
(PINF
.Stack_Guard_Pages
);
956 Init
.init_sproc_count
:= C
.int
(PINF
.Initial_Sproc_Count
);
958 Result
:= pthread_exec_begin
(Init
'Access);
959 pragma Assert
(Result
/= FUNC_ERR
);
961 if Result
= FUNC_ERR
then
962 raise Storage_Error
; -- Insufficient resources.
965 end Initialize_Athread_Library
;
968 Initialize_Athread_Library
;
969 end System
.Task_Primitives
.Operations
;