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 --
10 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 -- This is a OpenVMS/Alpha version of this package
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 -- used for Known_Tasks
51 with System
.Parameters
;
55 -- used for Ada_Task_Control_Block
58 with System
.Soft_Links
;
59 -- used for Defer/Undefer_Abort
62 -- Note that we do not use System.Tasking.Initialization directly since
63 -- this is a higher level package that we shouldn't depend on. For example
64 -- when using the restricted run time, it is replaced by
65 -- System.Tasking.Restricted.Initialization
67 with System
.OS_Primitives
;
68 -- used for Delay_Modes
70 with Unchecked_Conversion
;
71 with Unchecked_Deallocation
;
73 package body System
.Task_Primitives
.Operations
is
75 use System
.Tasking
.Debug
;
78 use System
.OS_Interface
;
79 use System
.Parameters
;
80 use System
.OS_Primitives
;
81 use type System
.OS_Primitives
.OS_Time
;
83 package SSL
renames System
.Soft_Links
;
89 -- The followings are logically constants, but need to be initialized
92 ATCB_Key
: aliased pthread_key_t
;
93 -- Key used to find the Ada Task_ID associated with a thread
95 Single_RTS_Lock
: aliased RTS_Lock
;
96 -- This is a lock to allow only one thread of control in the RTS at
97 -- a time; it is used to execute in mutual exclusion from all other tasks.
98 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
100 Environment_Task_ID
: Task_ID
;
101 -- A variable to hold Task_ID for the environment task.
103 Time_Slice_Val
: Integer;
104 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
106 Dispatching_Policy
: Character;
107 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
109 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
110 -- Indicates whether FIFO_Within_Priorities is set.
112 -----------------------
113 -- Local Subprograms --
114 -----------------------
116 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
118 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
120 procedure Timer_Sleep_AST
(ID
: Address
);
121 -- Signal the condition variable when AST fires.
123 procedure Timer_Sleep_AST
(ID
: Address
) is
124 Result
: Interfaces
.C
.int
;
125 Self_ID
: Task_ID
:= To_Task_ID
(ID
);
128 Self_ID
.Common
.LL
.AST_Pending
:= False;
129 Result
:= pthread_cond_signal_int_np
(Self_ID
.Common
.LL
.CV
'Access);
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
149 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
151 return T
.Common
.LL
.Thread
;
158 function Self
return Task_ID
is
159 Result
: System
.Address
;
162 Result
:= pthread_getspecific
(ATCB_Key
);
163 pragma Assert
(Result
/= System
.Null_Address
);
164 return To_Task_ID
(Result
);
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
(Prio
: System
.Any_Priority
; L
: access Lock
) is
179 Attributes
: aliased pthread_mutexattr_t
;
180 Result
: Interfaces
.C
.int
;
183 Result
:= pthread_mutexattr_init
(Attributes
'Access);
184 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
186 if Result
= ENOMEM
then
191 L
.Prio
:= Interfaces
.C
.int
(Prio
);
193 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
194 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
196 if Result
= ENOMEM
then
200 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
201 pragma Assert
(Result
= 0);
204 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
205 Attributes
: aliased pthread_mutexattr_t
;
206 Result
: Interfaces
.C
.int
;
209 Result
:= pthread_mutexattr_init
(Attributes
'Access);
210 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
212 if Result
= ENOMEM
then
216 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes.
217 -- Result := pthread_mutexattr_settype_np
218 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
219 -- pragma Assert (Result = 0);
221 -- Result := pthread_mutexattr_setprotocol
222 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
223 -- pragma Assert (Result = 0);
225 -- Result := pthread_mutexattr_setprioceiling
226 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
227 -- pragma Assert (Result = 0);
229 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
231 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
233 if Result
= ENOMEM
then
237 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
238 pragma Assert
(Result
= 0);
245 procedure Finalize_Lock
(L
: access Lock
) is
246 Result
: Interfaces
.C
.int
;
248 Result
:= pthread_mutex_destroy
(L
.L
'Access);
249 pragma Assert
(Result
= 0);
252 procedure Finalize_Lock
(L
: access RTS_Lock
) is
253 Result
: Interfaces
.C
.int
;
255 Result
:= pthread_mutex_destroy
(L
);
256 pragma Assert
(Result
= 0);
263 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
264 Self_ID
: constant Task_ID
:= Self
;
265 All_Tasks_Link
: constant Task_ID
:= Self
.Common
.All_Tasks_Link
;
266 Current_Prio
: System
.Any_Priority
;
267 Result
: Interfaces
.C
.int
;
270 Current_Prio
:= Get_Priority
(Self_ID
);
272 -- If there is no other tasks, no need to check priorities.
274 if All_Tasks_Link
/= Null_Task
275 and then L
.Prio
< Interfaces
.C
.int
(Current_Prio
)
277 Ceiling_Violation
:= True;
281 Result
:= pthread_mutex_lock
(L
.L
'Access);
282 pragma Assert
(Result
= 0);
284 Ceiling_Violation
:= False;
285 -- Why is this commented out ???
286 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
287 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
291 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
293 Result
: Interfaces
.C
.int
;
295 if not Single_Lock
or else Global_Lock
then
296 Result
:= pthread_mutex_lock
(L
);
297 pragma Assert
(Result
= 0);
301 procedure Write_Lock
(T
: Task_ID
) is
302 Result
: Interfaces
.C
.int
;
304 if not Single_Lock
then
305 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
306 pragma Assert
(Result
= 0);
314 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
316 Write_Lock
(L
, Ceiling_Violation
);
323 procedure Unlock
(L
: access Lock
) is
324 Result
: Interfaces
.C
.int
;
326 Result
:= pthread_mutex_unlock
(L
.L
'Access);
327 pragma Assert
(Result
= 0);
330 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
331 Result
: Interfaces
.C
.int
;
333 if not Single_Lock
or else Global_Lock
then
334 Result
:= pthread_mutex_unlock
(L
);
335 pragma Assert
(Result
= 0);
339 procedure Unlock
(T
: Task_ID
) is
340 Result
: Interfaces
.C
.int
;
342 if not Single_Lock
then
343 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
344 pragma Assert
(Result
= 0);
354 Reason
: System
.Tasking
.Task_States
)
356 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.
367 pragma Assert
(Result
= 0 or else Result
= EINTR
);
369 if Self_ID
.Deferral_Level
= 0
370 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
373 raise Standard
'Abort_Signal;
381 procedure Timed_Sleep
384 Mode
: ST
.Delay_Modes
;
385 Reason
: System
.Tasking
.Task_States
;
386 Timedout
: out Boolean;
387 Yielded
: out Boolean)
389 Sleep_Time
: OS_Time
;
390 Result
: Interfaces
.C
.int
;
391 Status
: Cond_Value_Type
;
397 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
399 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
400 or else Self_ID
.Pending_Priority_Change
405 Self_ID
.Common
.LL
.AST_Pending
:= True;
408 (Status
, 0, Sleep_Time
,
409 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
411 if (Status
and 1) /= 1 then
416 Result
:= pthread_cond_wait
417 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
420 Result
:= pthread_cond_wait
421 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
426 if not Self_ID
.Common
.LL
.AST_Pending
then
429 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
430 pragma Assert
((Status
and 1) = 1);
438 procedure Timed_Delay
441 Mode
: ST
.Delay_Modes
)
443 Sleep_Time
: OS_Time
;
444 Result
: Interfaces
.C
.int
;
445 Status
: Cond_Value_Type
;
446 Yielded
: Boolean := False;
449 -- Only the little window between deferring abort and
450 -- locking Self_ID is the reason we need to
451 -- check for pending abort and priority change below!
458 Write_Lock
(Self_ID
);
460 if Time
/= 0.0 or else Mode
/= Relative
then
461 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
463 if Mode
= Relative
or else OS_Clock
< Sleep_Time
then
464 Self_ID
.Common
.State
:= Delay_Sleep
;
465 Self_ID
.Common
.LL
.AST_Pending
:= True;
468 (Status
, 0, Sleep_Time
,
469 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
471 if (Status
and 1) /= 1 then
476 if Self_ID
.Pending_Priority_Change
then
477 Self_ID
.Pending_Priority_Change
:= False;
478 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
479 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
482 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
483 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
484 pragma Assert
((Status
and 1) = 1);
489 Result
:= pthread_cond_wait
490 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
492 Result
:= pthread_cond_wait
493 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
498 exit when not Self_ID
.Common
.LL
.AST_Pending
;
501 Self_ID
.Common
.State
:= Runnable
;
512 Result
:= sched_yield
;
515 SSL
.Abort_Undefer
.all;
518 ---------------------
519 -- Monotonic_Clock --
520 ---------------------
522 function Monotonic_Clock
return Duration
523 renames System
.OS_Primitives
.Monotonic_Clock
;
529 function RT_Resolution
return Duration is
538 procedure Wakeup
(T
: Task_ID
; Reason
: System
.Tasking
.Task_States
) is
539 Result
: Interfaces
.C
.int
;
541 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
542 pragma Assert
(Result
= 0);
549 procedure Yield
(Do_Yield
: Boolean := True) is
550 Result
: Interfaces
.C
.int
;
553 Result
:= sched_yield
;
561 procedure Set_Priority
563 Prio
: System
.Any_Priority
;
564 Loss_Of_Inheritance
: Boolean := False)
566 Result
: Interfaces
.C
.int
;
567 Param
: aliased struct_sched_param
;
569 T
.Common
.Current_Priority
:= Prio
;
570 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
572 if Time_Slice_Val
> 0 then
573 Result
:= pthread_setschedparam
574 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
576 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
577 Result
:= pthread_setschedparam
578 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
581 Result
:= pthread_setschedparam
582 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
585 pragma Assert
(Result
= 0);
592 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
594 return T
.Common
.Current_Priority
;
601 procedure Enter_Task
(Self_ID
: Task_ID
) is
602 Result
: Interfaces
.C
.int
;
604 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
606 -- It is not safe for the new task accept signals until it
607 -- has bound its TCB pointer to the thread with pthread_setspecific (),
608 -- since the handler wrappers use the TCB pointer
609 -- to restore the stack limit.
611 Result
:= pthread_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
612 pragma Assert
(Result
= 0);
616 for J
in Known_Tasks
'Range loop
617 if Known_Tasks
(J
) = null then
618 Known_Tasks
(J
) := Self_ID
;
619 Self_ID
.Known_Tasks_Index
:= J
;
631 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
633 return new Ada_Task_Control_Block
(Entry_Num
);
636 ----------------------
638 ----------------------
640 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
641 Mutex_Attr
: aliased pthread_mutexattr_t
;
642 Result
: Interfaces
.C
.int
;
643 Cond_Attr
: aliased pthread_condattr_t
;
646 if not Single_Lock
then
647 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
648 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
651 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
653 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
661 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
662 pragma Assert
(Result
= 0);
665 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
666 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
669 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
671 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
676 Self_ID
.Common
.LL
.Exc_Stack_Ptr
:= new Exc_Stack_T
;
677 SSL
.Set_Exc_Stack_Addr
678 (To_Address
(Self_ID
),
679 Self_ID
.Common
.LL
.Exc_Stack_Ptr
(Exc_Stack_T
'Last)'Address);
682 if not Single_Lock
then
683 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
684 pragma Assert
(Result
= 0);
690 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
691 pragma Assert
(Result
= 0);
698 procedure Create_Task
700 Wrapper
: System
.Address
;
701 Stack_Size
: System
.Parameters
.Size_Type
;
702 Priority
: System
.Any_Priority
;
703 Succeeded
: out Boolean)
705 Attributes
: aliased pthread_attr_t
;
706 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
707 Result
: Interfaces
.C
.int
;
709 function Thread_Body_Access
is new
710 Unchecked_Conversion
(System
.Address
, Thread_Body
);
713 if Stack_Size
= Unspecified_Size
then
714 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
716 elsif Stack_Size
< Minimum_Stack_Size
then
717 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
720 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
723 -- Since the initial signal mask of a thread is inherited from the
724 -- creator, we need to set our local signal mask mask all signals
725 -- during the creation operation, to make sure the new thread is
726 -- not disturbed by signals before it has set its own Task_ID.
728 Result
:= pthread_attr_init
(Attributes
'Access);
729 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
736 Result
:= pthread_attr_setdetachstate
737 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
738 pragma Assert
(Result
= 0);
740 Result
:= pthread_attr_setstacksize
741 (Attributes
'Access, Adjusted_Stack_Size
);
742 pragma Assert
(Result
= 0);
744 -- This call may be unnecessary, not sure. ???
746 Result
:= pthread_attr_setinheritsched
747 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
748 pragma Assert
(Result
= 0);
750 Result
:= pthread_create
751 (T
.Common
.LL
.Thread
'Access,
753 Thread_Body_Access
(Wrapper
),
756 -- ENOMEM is a valid run-time error. Don't shut down.
758 pragma Assert
(Result
= 0
759 or else Result
= EAGAIN
or else Result
= ENOMEM
);
761 Succeeded
:= Result
= 0;
763 Result
:= pthread_attr_destroy
(Attributes
'Access);
764 pragma Assert
(Result
= 0);
767 Set_Priority
(T
, Priority
);
775 procedure Finalize_TCB
(T
: Task_ID
) is
776 Result
: Interfaces
.C
.int
;
779 procedure Free
is new
780 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
782 procedure Free
is new Unchecked_Deallocation
783 (Exc_Stack_T
, Exc_Stack_Ptr_T
);
786 if not Single_Lock
then
787 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
788 pragma Assert
(Result
= 0);
791 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
792 pragma Assert
(Result
= 0);
794 if T
.Known_Tasks_Index
/= -1 then
795 Known_Tasks
(T
.Known_Tasks_Index
) := null;
798 Free
(T
.Common
.LL
.Exc_Stack_Ptr
);
806 procedure Exit_Task
is
808 pthread_exit
(System
.Null_Address
);
815 procedure Abort_Task
(T
: Task_ID
) is
819 -- Why is this commented out ???
820 -- if T = Self and then T.Deferral_Level = 0
821 -- and then T.Pending_ATC_Level < T.ATC_Nesting_Level
823 -- raise Standard'Abort_Signal;
827 -- Interrupt Server_Tasks may be waiting on an event flag
829 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
830 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
839 -- Dummy versions. The only currently working versions is for solaris
842 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
851 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
856 ----------------------
857 -- Environment_Task --
858 ----------------------
860 function Environment_Task
return Task_ID
is
862 return Environment_Task_ID
;
863 end Environment_Task
;
869 procedure Lock_RTS
is
871 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
878 procedure Unlock_RTS
is
880 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
887 function Suspend_Task
889 Thread_Self
: Thread_Id
) return Boolean is
900 Thread_Self
: Thread_Id
) return Boolean is
909 procedure Initialize
(Environment_Task
: Task_ID
) is
911 Environment_Task_ID
:= Environment_Task
;
913 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
914 -- Initialize the lock used to synchronize chain of all ATCBs.
916 Enter_Task
(Environment_Task
);
921 Result
: Interfaces
.C
.int
;
923 Result
:= pthread_key_create
(ATCB_Key
'Access, null);
924 pragma Assert
(Result
= 0);
926 end System
.Task_Primitives
.Operations
;