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.Stages.
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
);
130 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
136 -- The underlying thread system sets a guard page at the
137 -- bottom of a thread stack, so nothing is needed.
138 -- ??? Check the comment above
140 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
141 pragma Unreferenced
(T
);
142 pragma Unreferenced
(On
);
151 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
153 return T
.Common
.LL
.Thread
;
160 function Self
return Task_Id
is
162 return To_Task_Id
(pthread_get_current_ada_tcb
);
165 ---------------------
166 -- Initialize_Lock --
167 ---------------------
169 -- Note: mutexes and cond_variables needed per-task basis are
170 -- initialized in Initialize_TCB and the Storage_Error is
171 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
172 -- used in RTS is initialized before any status change of RTS.
173 -- Therefore rasing Storage_Error in the following routines
174 -- should be able to be handled safely.
176 procedure Initialize_Lock
177 (Prio
: System
.Any_Priority
;
180 Attributes
: aliased pthread_mutexattr_t
;
181 Result
: Interfaces
.C
.int
;
184 Result
:= pthread_mutexattr_init
(Attributes
'Access);
186 if Result
= FUNC_ERR
then
190 if Locking_Policy
= 'C' then
192 Result
:= pthread_mutexattr_setqueueorder
193 (Attributes
'Access, MUTEX_PRIORITY_CEILING
);
195 pragma Assert
(Result
/= FUNC_ERR
);
197 Result
:= pthread_mutexattr_setceilingprio
198 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
200 pragma Assert
(Result
/= FUNC_ERR
);
203 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
205 if Result
= FUNC_ERR
then
206 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
210 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
213 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
214 pragma Unreferenced
(Level
);
216 Attributes
: aliased pthread_mutexattr_t
;
217 Result
: Interfaces
.C
.int
;
220 Result
:= pthread_mutexattr_init
(Attributes
'Access);
222 if Result
= FUNC_ERR
then
226 if Locking_Policy
= 'C' then
227 Result
:= pthread_mutexattr_setqueueorder
228 (Attributes
'Access, MUTEX_PRIORITY_CEILING
);
229 pragma Assert
(Result
/= FUNC_ERR
);
231 Result
:= pthread_mutexattr_setceilingprio
232 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
233 pragma Assert
(Result
/= FUNC_ERR
);
236 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
238 if Result
= FUNC_ERR
then
239 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
243 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
250 procedure Finalize_Lock
(L
: access Lock
) is
251 Result
: Interfaces
.C
.int
;
253 Result
:= pthread_mutex_destroy
(L
);
254 pragma Assert
(Result
= 0);
257 procedure Finalize_Lock
(L
: access RTS_Lock
) is
258 Result
: Interfaces
.C
.int
;
260 Result
:= pthread_mutex_destroy
(L
);
261 pragma Assert
(Result
= 0);
268 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
269 Result
: Interfaces
.C
.int
;
271 Result
:= pthread_mutex_lock
(L
);
272 Ceiling_Violation
:= Result
= FUNC_ERR
and then errno
= EINVAL
;
273 pragma Assert
(Result
/= FUNC_ERR
);
277 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
279 Result
: Interfaces
.C
.int
;
281 if not Single_Lock
or else Global_Lock
then
282 Result
:= pthread_mutex_lock
(L
);
283 pragma Assert
(Result
= 0);
287 procedure Write_Lock
(T
: Task_Id
) is
288 Result
: Interfaces
.C
.int
;
290 if not Single_Lock
then
291 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
292 pragma Assert
(Result
= 0);
300 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
302 Write_Lock
(L
, Ceiling_Violation
);
309 procedure Unlock
(L
: access Lock
) is
310 Result
: Interfaces
.C
.int
;
312 Result
:= pthread_mutex_unlock
(L
);
313 pragma Assert
(Result
= 0);
316 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
317 Result
: Interfaces
.C
.int
;
319 if not Single_Lock
or else Global_Lock
then
320 Result
:= pthread_mutex_unlock
(L
);
321 pragma Assert
(Result
= 0);
325 procedure Unlock
(T
: Task_Id
) is
326 Result
: Interfaces
.C
.int
;
328 if not Single_Lock
then
329 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
330 pragma Assert
(Result
= 0);
339 (Self_ID
: ST
.Task_Id
;
340 Reason
: System
.Tasking
.Task_States
)
342 pragma Unreferenced
(Reason
);
344 Result
: Interfaces
.C
.int
;
348 Result
:= pthread_cond_wait
349 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
351 Result
:= pthread_cond_wait
352 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
355 -- EINTR is not considered a failure.
357 pragma Assert
(Result
= 0 or else Result
= EINTR
);
364 procedure Timed_Sleep
367 Mode
: ST
.Delay_Modes
;
368 Reason
: System
.Tasking
.Task_States
;
369 Timedout
: out Boolean;
370 Yielded
: out Boolean)
372 pragma Unreferenced
(Reason
);
374 Check_Time
: constant Duration := Monotonic_Clock
;
376 Request
: aliased struct_timeval
;
377 Result
: Interfaces
.C
.int
;
383 if Mode
= Relative
then
384 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
386 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
389 if Abs_Time
> Check_Time
then
390 Request
:= To_Timeval
(Abs_Time
);
393 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
394 or else Self_ID
.Pending_Priority_Change
;
397 Result
:= pthread_cond_timedwait
398 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
402 Result
:= pthread_cond_timedwait
403 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
407 exit when Abs_Time
<= Monotonic_Clock
;
409 if Result
= 0 or Result
= EINTR
then
410 -- somebody may have called Wakeup for us
415 pragma Assert
(Result
= ETIMEDOUT
416 or else (Result
= -1 and then errno
= EAGAIN
));
425 procedure Timed_Delay
428 Mode
: ST
.Delay_Modes
)
430 Check_Time
: constant Duration := Monotonic_Clock
;
432 Request
: aliased struct_timeval
;
433 Result
: Interfaces
.C
.int
;
436 -- Only the little window between deferring abort and
437 -- locking Self_ID is the reason we need to
438 -- check for pending abort and priority change below!
446 Write_Lock
(Self_ID
);
448 if Mode
= Relative
then
449 Abs_Time
:= Time
+ Check_Time
;
451 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
454 if Abs_Time
> Check_Time
then
455 Request
:= To_Timeval
(Abs_Time
);
456 Self_ID
.Common
.State
:= Delay_Sleep
;
459 if Self_ID
.Pending_Priority_Change
then
460 Self_ID
.Pending_Priority_Change
:= False;
461 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
462 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
465 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
468 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
469 Single_RTS_Lock
'Access, Request
'Access);
471 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
472 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
475 exit when Abs_Time
<= Monotonic_Clock
;
477 pragma Assert
(Result
= 0 or else
478 Result
= ETIMEDOUT
or else
479 (Result
= -1 and then errno
= EAGAIN
) or else
483 Self_ID
.Common
.State
:= Runnable
;
493 SSL
.Abort_Undefer
.all;
496 ---------------------
497 -- Monotonic_Clock --
498 ---------------------
500 function Monotonic_Clock
return Duration is
501 type timeval
is record
505 pragma Convention
(C
, timeval
);
507 tv
: aliased timeval
;
509 procedure gettimeofday
(tp
: access timeval
);
510 pragma Import
(C
, gettimeofday
, "gettimeofday", "gettimeofday");
513 gettimeofday
(tv
'Access);
514 return Duration (tv
.tv_sec
) + Duration (tv
.tv_usec
) / 1_000_000
.0
;
521 function RT_Resolution
return Duration is
532 Reason
: System
.Tasking
.Task_States
)
534 pragma Unreferenced
(Reason
);
535 Result
: Interfaces
.C
.int
;
537 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
538 pragma Assert
(Result
= 0);
545 procedure Yield
(Do_Yield
: Boolean := True) is
556 procedure Set_Priority
558 Prio
: System
.Any_Priority
;
559 Loss_Of_Inheritance
: Boolean := False)
561 pragma Unreferenced
(Loss_Of_Inheritance
);
563 Result
: Interfaces
.C
.int
;
566 T
.Common
.Current_Priority
:= Prio
;
567 Result
:= pthread_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
568 pragma Assert
(Result
/= FUNC_ERR
);
575 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
577 return T
.Common
.Current_Priority
;
584 procedure Enter_Task
(Self_ID
: Task_Id
) is
585 Result
: Interfaces
.C
.int
;
588 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
589 Self_ID
.Common
.LL
.LWP
:= sproc_self
;
592 pthread_set_ada_tcb
(Self_ID
.Common
.LL
.Thread
, To_Address
(Self_ID
));
594 pragma Assert
(Result
= 0);
598 for J
in Known_Tasks
'Range loop
599 if Known_Tasks
(J
) = null then
600 Known_Tasks
(J
) := Self_ID
;
601 Self_ID
.Known_Tasks_Index
:= J
;
613 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
615 return new Ada_Task_Control_Block
(Entry_Num
);
622 function Is_Valid_Task
return Boolean is
627 -----------------------------
628 -- Register_Foreign_Thread --
629 -----------------------------
631 function Register_Foreign_Thread
return Task_Id
is
634 end Register_Foreign_Thread
;
640 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
641 Result
: Interfaces
.C
.int
;
642 Cond_Attr
: aliased pthread_condattr_t
;
645 if not Single_Lock
then
646 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
649 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
650 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
653 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
655 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
661 if not Single_Lock
then
662 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
663 pragma Assert
(Result
= 0);
669 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
670 pragma Assert
(Result
= 0);
677 procedure Create_Task
679 Wrapper
: System
.Address
;
680 Stack_Size
: System
.Parameters
.Size_Type
;
681 Priority
: System
.Any_Priority
;
682 Succeeded
: out Boolean)
684 Attributes
: aliased pthread_attr_t
;
685 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
686 Result
: Interfaces
.C
.int
;
688 function Thread_Body_Access
is new
689 Unchecked_Conversion
(System
.Address
, start_addr
);
691 function To_Resource_T
is new Unchecked_Conversion
692 (System
.Task_Info
.Resource_Vector_T
, System
.OS_Interface
.resource_t
);
694 use System
.Task_Info
;
697 if Stack_Size
= Unspecified_Size
then
698 Adjusted_Stack_Size
:=
699 Interfaces
.C
.size_t
(System
.Program_Info
.Default_Task_Stack
);
701 elsif Stack_Size
< Minimum_Stack_Size
then
702 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
705 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
708 Result
:= pthread_attr_init
(Attributes
'Access);
709 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
716 Result
:= pthread_attr_setdetachstate
(Attributes
'Access, 1);
717 pragma Assert
(Result
= 0);
719 Result
:= pthread_attr_setstacksize
720 (Attributes
'Access, Adjusted_Stack_Size
);
721 pragma Assert
(Result
= 0);
723 if T
.Common
.Task_Info
/= null then
724 Result
:= pthread_attr_setresources
726 To_Resource_T
(T
.Common
.Task_Info
.Thread_Resources
));
727 pragma Assert
(Result
/= FUNC_ERR
);
729 if T
.Common
.Task_Info
.Thread_Timeslice
/= 0.0 then
731 use System
.OS_Interface
;
733 Tv
: aliased struct_timeval
:= To_Timeval
734 (T
.Common
.Task_Info
.Thread_Timeslice
);
736 Result
:= pthread_attr_set_tslice
737 (Attributes
'Access, Tv
'Access);
741 if T
.Common
.Task_Info
.Bound_To_Sproc
then
742 Result
:= pthread_attr_set_boundtosproc
743 (Attributes
'Access, PTHREAD_BOUND
);
744 Result
:= pthread_attr_set_bsproc
745 (Attributes
'Access, T
.Common
.Task_Info
.Sproc
);
750 -- Since the initial signal mask of a thread is inherited from the
751 -- creator, and the Environment task has all its signals masked, we
752 -- do not need to manipulate caller's signal mask at this point.
753 -- All tasks in RTS will have All_Tasks_Mask initially.
755 Result
:= pthread_create
756 (T
.Common
.LL
.Thread
'Access,
758 Thread_Body_Access
(Wrapper
),
760 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
762 Succeeded
:= Result
= 0;
764 Set_Priority
(T
, Priority
);
766 Result
:= pthread_attr_destroy
(Attributes
'Access);
767 pragma Assert
(Result
/= FUNC_ERR
);
774 procedure Finalize_TCB
(T
: Task_Id
) is
775 procedure Free
is new
776 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
778 Result
: Interfaces
.C
.int
;
782 if not Single_Lock
then
783 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
784 pragma Assert
(Result
= 0);
787 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
788 pragma Assert
(Result
= 0);
790 if T
.Known_Tasks_Index
/= -1 then
791 Known_Tasks
(T
.Known_Tasks_Index
) := null;
801 procedure Exit_Task
is
802 Result
: Interfaces
.C
.int
;
804 Result
:= pthread_set_ada_tcb
(pthread_self
, System
.Null_Address
);
805 pragma Assert
(Result
= 0);
812 procedure Abort_Task
(T
: Task_Id
) is
813 Result
: Interfaces
.C
.int
;
816 pthread_kill
(T
.Common
.LL
.Thread
,
818 (System
.Interrupt_Management
.Abort_Task_Interrupt
));
819 pragma Assert
(Result
= 0);
828 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
829 pragma Unreferenced
(Self_ID
);
838 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
839 pragma Unreferenced
(Self_ID
);
844 ----------------------
845 -- Environment_Task --
846 ----------------------
848 function Environment_Task
return Task_Id
is
850 return Environment_Task_Id
;
851 end Environment_Task
;
857 procedure Lock_RTS
is
859 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
866 procedure Unlock_RTS
is
868 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
875 function Suspend_Task
877 Thread_Self
: Thread_Id
) return Boolean
880 if T
.Common
.LL
.Thread
/= Thread_Self
then
881 return pthread_suspend
(T
.Common
.LL
.Thread
) = 0;
893 Thread_Self
: Thread_Id
) return Boolean
896 if T
.Common
.LL
.Thread
/= Thread_Self
then
897 return pthread_resume
(T
.Common
.LL
.Thread
) = 0;
907 procedure Initialize
(Environment_Task
: Task_Id
) is
909 Environment_Task_Id
:= Environment_Task
;
911 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
912 -- Initialize the lock used to synchronize chain of all ATCBs.
914 Enter_Task
(Environment_Task
);
916 Set_Priority
(Environment_Task
,
917 Environment_Task
.Common
.Current_Priority
);
920 --------------------------------
921 -- Initialize_Athread_Library --
922 --------------------------------
924 procedure Initialize_Athread_Library
is
925 Result
: Interfaces
.C
.int
;
926 Init
: aliased pthread_init_struct
;
928 package PINF
renames System
.Program_Info
;
929 package C
renames Interfaces
.C
;
932 Init
.conf_initsize
:= C
.int
(PINF
.Pthread_Arena_Size
);
933 Init
.max_sproc_count
:= C
.int
(PINF
.Max_Sproc_Count
);
934 Init
.sproc_stack_size
:= C
.size_t
(PINF
.Sproc_Stack_Size
);
935 Init
.os_default_priority
:= C
.int
(PINF
.Os_Default_Priority
);
936 Init
.os_sched_signal
:= C
.int
(PINF
.Pthread_Sched_Signal
);
937 Init
.guard_pages
:= C
.int
(PINF
.Stack_Guard_Pages
);
938 Init
.init_sproc_count
:= C
.int
(PINF
.Initial_Sproc_Count
);
940 Result
:= pthread_exec_begin
(Init
'Access);
941 pragma Assert
(Result
/= FUNC_ERR
);
943 if Result
= FUNC_ERR
then
944 raise Storage_Error
; -- Insufficient resources
946 end Initialize_Athread_Library
;
948 -- Package initialization
951 Initialize_Athread_Library
;
952 end System
.Task_Primitives
.Operations
;