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-2001, 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
;
216 Result
:= pthread_mutexattr_init
(Attributes
'Access);
218 if Result
= FUNC_ERR
then
222 if Locking_Policy
= 'C' then
223 Result
:= pthread_mutexattr_setqueueorder
224 (Attributes
'Access, MUTEX_PRIORITY_CEILING
);
225 pragma Assert
(Result
/= FUNC_ERR
);
227 Result
:= pthread_mutexattr_setceilingprio
228 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
229 pragma Assert
(Result
/= FUNC_ERR
);
232 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
234 if Result
= FUNC_ERR
then
235 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
239 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
246 procedure Finalize_Lock
(L
: access Lock
) is
247 Result
: Interfaces
.C
.int
;
250 Result
:= pthread_mutex_destroy
(L
);
251 pragma Assert
(Result
= 0);
254 procedure Finalize_Lock
(L
: access RTS_Lock
) is
255 Result
: Interfaces
.C
.int
;
258 Result
:= pthread_mutex_destroy
(L
);
259 pragma Assert
(Result
= 0);
266 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
267 Result
: Interfaces
.C
.int
;
269 Result
:= pthread_mutex_lock
(L
);
271 Ceiling_Violation
:= Result
= FUNC_ERR
and then errno
= EINVAL
;
272 pragma Assert
(Result
/= FUNC_ERR
);
276 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
278 Result
: Interfaces
.C
.int
;
280 if not Single_Lock
or else Global_Lock
then
281 Result
:= pthread_mutex_lock
(L
);
282 pragma Assert
(Result
= 0);
286 procedure Write_Lock
(T
: Task_ID
) is
287 Result
: Interfaces
.C
.int
;
289 if not Single_Lock
then
290 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
291 pragma Assert
(Result
= 0);
299 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
301 Write_Lock
(L
, Ceiling_Violation
);
308 procedure Unlock
(L
: access Lock
) is
309 Result
: Interfaces
.C
.int
;
311 Result
:= pthread_mutex_unlock
(L
);
312 pragma Assert
(Result
= 0);
315 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
316 Result
: Interfaces
.C
.int
;
318 if not Single_Lock
or else Global_Lock
then
319 Result
:= pthread_mutex_unlock
(L
);
320 pragma Assert
(Result
= 0);
324 procedure Unlock
(T
: Task_ID
) is
325 Result
: Interfaces
.C
.int
;
327 if not Single_Lock
then
328 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
329 pragma Assert
(Result
= 0);
338 (Self_ID
: ST
.Task_ID
;
339 Reason
: System
.Tasking
.Task_States
)
341 Result
: Interfaces
.C
.int
;
344 Result
:= pthread_cond_wait
345 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
347 Result
:= pthread_cond_wait
348 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
351 -- EINTR is not considered a failure.
352 pragma Assert
(Result
= 0 or else Result
= EINTR
);
359 procedure Timed_Sleep
362 Mode
: ST
.Delay_Modes
;
363 Reason
: System
.Tasking
.Task_States
;
364 Timedout
: out Boolean;
365 Yielded
: out Boolean)
367 Check_Time
: constant Duration := Monotonic_Clock
;
369 Request
: aliased struct_timeval
;
370 Result
: Interfaces
.C
.int
;
375 if Mode
= Relative
then
376 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
378 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
381 if Abs_Time
> Check_Time
then
382 Request
:= To_Timeval
(Abs_Time
);
385 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
386 or else Self_ID
.Pending_Priority_Change
;
389 Result
:= pthread_cond_timedwait
390 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
394 Result
:= pthread_cond_timedwait
395 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
399 exit when Abs_Time
<= Monotonic_Clock
;
401 if Result
= 0 or Result
= EINTR
then
402 -- somebody may have called Wakeup for us
407 pragma Assert
(Result
= ETIMEDOUT
408 or else (Result
= -1 and then errno
= EAGAIN
));
417 procedure Timed_Delay
420 Mode
: ST
.Delay_Modes
)
422 Check_Time
: constant Duration := Monotonic_Clock
;
424 Request
: aliased struct_timeval
;
425 Result
: Interfaces
.C
.int
;
428 -- Only the little window between deferring abort and
429 -- locking Self_ID is the reason we need to
430 -- check for pending abort and priority change below! :(
438 Write_Lock
(Self_ID
);
440 if Mode
= Relative
then
441 Abs_Time
:= Time
+ Check_Time
;
443 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
446 if Abs_Time
> Check_Time
then
447 Request
:= To_Timeval
(Abs_Time
);
448 Self_ID
.Common
.State
:= Delay_Sleep
;
451 if Self_ID
.Pending_Priority_Change
then
452 Self_ID
.Pending_Priority_Change
:= False;
453 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
454 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
457 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
460 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
461 Single_RTS_Lock
'Access, Request
'Access);
463 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
464 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
467 exit when Abs_Time
<= Monotonic_Clock
;
469 pragma Assert
(Result
= 0 or else
470 Result
= ETIMEDOUT
or else
471 (Result
= -1 and then errno
= EAGAIN
) or else
475 Self_ID
.Common
.State
:= Runnable
;
485 SSL
.Abort_Undefer
.all;
488 ---------------------
489 -- Monotonic_Clock --
490 ---------------------
492 function Monotonic_Clock
return Duration is
493 type timeval
is record
497 pragma Convention
(C
, timeval
);
499 tv
: aliased timeval
;
501 procedure gettimeofday
(tp
: access timeval
);
502 pragma Import
(C
, gettimeofday
, "gettimeofday", "gettimeofday");
505 gettimeofday
(tv
'Access);
506 return Duration (tv
.tv_sec
) + Duration (tv
.tv_usec
) / 1_000_000
.0
;
513 function RT_Resolution
return Duration is
524 Reason
: System
.Tasking
.Task_States
)
526 Result
: Interfaces
.C
.int
;
528 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
529 pragma Assert
(Result
= 0);
536 procedure Yield
(Do_Yield
: Boolean := True) is
547 procedure Set_Priority
549 Prio
: System
.Any_Priority
;
550 Loss_Of_Inheritance
: Boolean := False)
552 Result
: Interfaces
.C
.int
;
554 T
.Common
.Current_Priority
:= Prio
;
555 Result
:= pthread_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
556 pragma Assert
(Result
/= FUNC_ERR
);
564 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
566 return T
.Common
.Current_Priority
;
573 procedure Enter_Task
(Self_ID
: Task_ID
) is
574 Result
: Interfaces
.C
.int
;
576 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
577 Self_ID
.Common
.LL
.LWP
:= sproc_self
;
580 pthread_set_ada_tcb
(Self_ID
.Common
.LL
.Thread
, To_Address
(Self_ID
));
582 pragma Assert
(Result
= 0);
586 for J
in Known_Tasks
'Range loop
587 if Known_Tasks
(J
) = null then
588 Known_Tasks
(J
) := Self_ID
;
589 Self_ID
.Known_Tasks_Index
:= J
;
601 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
603 return new Ada_Task_Control_Block
(Entry_Num
);
606 ----------------------
608 ----------------------
610 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
611 Result
: Interfaces
.C
.int
;
612 Cond_Attr
: aliased pthread_condattr_t
;
615 if not Single_Lock
then
616 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
619 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
620 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
623 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
625 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
631 if not Single_Lock
then
632 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
633 pragma Assert
(Result
= 0);
639 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
640 pragma Assert
(Result
= 0);
647 procedure Create_Task
649 Wrapper
: System
.Address
;
650 Stack_Size
: System
.Parameters
.Size_Type
;
651 Priority
: System
.Any_Priority
;
652 Succeeded
: out Boolean)
654 Attributes
: aliased pthread_attr_t
;
655 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
656 Result
: Interfaces
.C
.int
;
658 function Thread_Body_Access
is new
659 Unchecked_Conversion
(System
.Address
, start_addr
);
661 function To_Resource_T
is new Unchecked_Conversion
662 (System
.Task_Info
.Resource_Vector_T
, System
.OS_Interface
.resource_t
);
664 use System
.Task_Info
;
667 if Stack_Size
= Unspecified_Size
then
668 Adjusted_Stack_Size
:=
669 Interfaces
.C
.size_t
(System
.Program_Info
.Default_Task_Stack
);
671 elsif Stack_Size
< Minimum_Stack_Size
then
672 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
675 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
678 Result
:= pthread_attr_init
(Attributes
'Access);
679 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
686 Result
:= pthread_attr_setdetachstate
(Attributes
'Access, 1);
687 pragma Assert
(Result
= 0);
689 Result
:= pthread_attr_setstacksize
690 (Attributes
'Access, Adjusted_Stack_Size
);
691 pragma Assert
(Result
= 0);
693 if T
.Common
.Task_Info
/= null then
694 Result
:= pthread_attr_setresources
696 To_Resource_T
(T
.Common
.Task_Info
.Thread_Resources
));
697 pragma Assert
(Result
/= FUNC_ERR
);
699 if T
.Common
.Task_Info
.Thread_Timeslice
/= 0.0 then
701 use System
.OS_Interface
;
703 Tv
: aliased struct_timeval
:= To_Timeval
704 (T
.Common
.Task_Info
.Thread_Timeslice
);
706 Result
:= pthread_attr_set_tslice
707 (Attributes
'Access, Tv
'Access);
711 if T
.Common
.Task_Info
.Bound_To_Sproc
then
712 Result
:= pthread_attr_set_boundtosproc
713 (Attributes
'Access, PTHREAD_BOUND
);
714 Result
:= pthread_attr_set_bsproc
715 (Attributes
'Access, T
.Common
.Task_Info
.Sproc
);
720 -- Since the initial signal mask of a thread is inherited from the
721 -- creator, and the Environment task has all its signals masked, we
722 -- do not need to manipulate caller's signal mask at this point.
723 -- All tasks in RTS will have All_Tasks_Mask initially.
725 Result
:= pthread_create
726 (T
.Common
.LL
.Thread
'Access,
728 Thread_Body_Access
(Wrapper
),
730 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
732 Succeeded
:= Result
= 0;
734 Set_Priority
(T
, Priority
);
736 Result
:= pthread_attr_destroy
(Attributes
'Access);
737 pragma Assert
(Result
/= FUNC_ERR
);
744 procedure Finalize_TCB
(T
: Task_ID
) is
745 procedure Free
is new
746 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
748 Result
: Interfaces
.C
.int
;
752 if not Single_Lock
then
753 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
754 pragma Assert
(Result
= 0);
757 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
758 pragma Assert
(Result
= 0);
760 if T
.Known_Tasks_Index
/= -1 then
761 Known_Tasks
(T
.Known_Tasks_Index
) := null;
771 procedure Exit_Task
is
773 pthread_exit
(System
.Null_Address
);
780 procedure Abort_Task
(T
: Task_ID
) is
781 Result
: Interfaces
.C
.int
;
783 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
784 Interfaces
.C
.int
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
785 pragma Assert
(Result
= 0);
792 -- Dummy versions. The only currently working versions is for solaris
795 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
804 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
809 ----------------------
810 -- Environment_Task --
811 ----------------------
813 function Environment_Task
return Task_ID
is
815 return Environment_Task_ID
;
816 end Environment_Task
;
822 procedure Lock_RTS
is
824 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
831 procedure Unlock_RTS
is
833 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
840 function Suspend_Task
842 Thread_Self
: Thread_Id
) return Boolean is
844 if T
.Common
.LL
.Thread
/= Thread_Self
then
845 return pthread_suspend
(T
.Common
.LL
.Thread
) = 0;
857 Thread_Self
: Thread_Id
) return Boolean is
859 if T
.Common
.LL
.Thread
/= Thread_Self
then
860 return pthread_resume
(T
.Common
.LL
.Thread
) = 0;
870 procedure Initialize
(Environment_Task
: Task_ID
) is
872 Environment_Task_ID
:= Environment_Task
;
874 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
875 -- Initialize the lock used to synchronize chain of all ATCBs.
877 Enter_Task
(Environment_Task
);
879 Set_Priority
(Environment_Task
,
880 Environment_Task
.Common
.Current_Priority
);
883 procedure Initialize_Athread_Library
is
884 Result
: Interfaces
.C
.int
;
885 Init
: aliased pthread_init_struct
;
887 package PINF
renames System
.Program_Info
;
888 package C
renames Interfaces
.C
;
891 Init
.conf_initsize
:= C
.int
(PINF
.Pthread_Arena_Size
);
892 Init
.max_sproc_count
:= C
.int
(PINF
.Max_Sproc_Count
);
893 Init
.sproc_stack_size
:= C
.size_t
(PINF
.Sproc_Stack_Size
);
894 Init
.os_default_priority
:= C
.int
(PINF
.Os_Default_Priority
);
895 Init
.os_sched_signal
:= C
.int
(PINF
.Pthread_Sched_Signal
);
896 Init
.guard_pages
:= C
.int
(PINF
.Stack_Guard_Pages
);
897 Init
.init_sproc_count
:= C
.int
(PINF
.Initial_Sproc_Count
);
899 Result
:= pthread_exec_begin
(Init
'Access);
900 pragma Assert
(Result
/= FUNC_ERR
);
902 if Result
= FUNC_ERR
then
903 raise Storage_Error
; -- Insufficient resources.
906 end Initialize_Athread_Library
;
909 Initialize_Athread_Library
;
910 end System
.Task_Primitives
.Operations
;