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. It is --
30 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 -- This is a OpenVMS/Alpha 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.
43 with System
.Tasking
.Debug
;
44 -- used for Known_Tasks
50 with System
.Parameters
;
54 -- used for Ada_Task_Control_Block
57 with System
.Soft_Links
;
58 -- used for Defer/Undefer_Abort
61 -- Note that we do not use System.Tasking.Initialization directly since
62 -- this is a higher level package that we shouldn't depend on. For example
63 -- when using the restricted run time, it is replaced by
64 -- System.Tasking.Restricted.Initialization
66 with System
.OS_Primitives
;
67 -- used for Delay_Modes
69 with Unchecked_Conversion
;
70 with Unchecked_Deallocation
;
72 package body System
.Task_Primitives
.Operations
is
74 use System
.Tasking
.Debug
;
77 use System
.OS_Interface
;
78 use System
.Parameters
;
79 use System
.OS_Primitives
;
80 use type System
.OS_Primitives
.OS_Time
;
82 package SSL
renames System
.Soft_Links
;
88 -- The followings are logically constants, but need to be initialized
91 ATCB_Key
: aliased pthread_key_t
;
92 -- Key used to find the Ada Task_ID associated with a thread
94 Single_RTS_Lock
: aliased RTS_Lock
;
95 -- This is a lock to allow only one thread of control in the RTS at
96 -- a time; it is used to execute in mutual exclusion from all other tasks.
97 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
99 Environment_Task_ID
: Task_ID
;
100 -- A variable to hold Task_ID for the environment task.
102 Time_Slice_Val
: Integer;
103 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
105 Dispatching_Policy
: Character;
106 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
108 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
109 -- Indicates whether FIFO_Within_Priorities is set.
111 -----------------------
112 -- Local Subprograms --
113 -----------------------
115 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
117 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
119 procedure Timer_Sleep_AST
(ID
: Address
);
120 -- Signal the condition variable when AST fires.
122 procedure Timer_Sleep_AST
(ID
: Address
) is
123 Result
: Interfaces
.C
.int
;
124 Self_ID
: Task_ID
:= To_Task_ID
(ID
);
127 Self_ID
.Common
.LL
.AST_Pending
:= False;
128 Result
:= pthread_cond_signal_int_np
(Self_ID
.Common
.LL
.CV
'Access);
135 -- The underlying thread system sets a guard page at the
136 -- bottom of a thread stack, so nothing is needed.
137 -- ??? Check the comment above
139 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
148 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
150 return T
.Common
.LL
.Thread
;
157 function Self
return Task_ID
is
158 Result
: System
.Address
;
161 Result
:= pthread_getspecific
(ATCB_Key
);
162 pragma Assert
(Result
/= System
.Null_Address
);
163 return To_Task_ID
(Result
);
166 ---------------------
167 -- Initialize_Lock --
168 ---------------------
170 -- Note: mutexes and cond_variables needed per-task basis are
171 -- initialized in Initialize_TCB and the Storage_Error is
172 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
173 -- used in RTS is initialized before any status change of RTS.
174 -- Therefore rasing Storage_Error in the following routines
175 -- should be able to be handled safely.
177 procedure Initialize_Lock
(Prio
: System
.Any_Priority
; L
: access Lock
) is
178 Attributes
: aliased pthread_mutexattr_t
;
179 Result
: Interfaces
.C
.int
;
182 Result
:= pthread_mutexattr_init
(Attributes
'Access);
183 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
185 if Result
= ENOMEM
then
190 L
.Prio
:= Interfaces
.C
.int
(Prio
);
192 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
193 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
195 if Result
= ENOMEM
then
199 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
200 pragma Assert
(Result
= 0);
203 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
204 Attributes
: aliased pthread_mutexattr_t
;
205 Result
: Interfaces
.C
.int
;
208 Result
:= pthread_mutexattr_init
(Attributes
'Access);
209 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
211 if Result
= ENOMEM
then
215 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes.
216 -- Result := pthread_mutexattr_settype_np
217 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
218 -- pragma Assert (Result = 0);
220 -- Result := pthread_mutexattr_setprotocol
221 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
222 -- pragma Assert (Result = 0);
224 -- Result := pthread_mutexattr_setprioceiling
225 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
226 -- pragma Assert (Result = 0);
228 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
230 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
232 if Result
= ENOMEM
then
236 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
237 pragma Assert
(Result
= 0);
244 procedure Finalize_Lock
(L
: access Lock
) is
245 Result
: Interfaces
.C
.int
;
247 Result
:= pthread_mutex_destroy
(L
.L
'Access);
248 pragma Assert
(Result
= 0);
251 procedure Finalize_Lock
(L
: access RTS_Lock
) is
252 Result
: Interfaces
.C
.int
;
254 Result
:= pthread_mutex_destroy
(L
);
255 pragma Assert
(Result
= 0);
262 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
263 Self_ID
: constant Task_ID
:= Self
;
264 All_Tasks_Link
: constant Task_ID
:= Self
.Common
.All_Tasks_Link
;
265 Current_Prio
: System
.Any_Priority
;
266 Result
: Interfaces
.C
.int
;
269 Current_Prio
:= Get_Priority
(Self_ID
);
271 -- If there is no other tasks, no need to check priorities.
273 if All_Tasks_Link
/= Null_Task
274 and then L
.Prio
< Interfaces
.C
.int
(Current_Prio
)
276 Ceiling_Violation
:= True;
280 Result
:= pthread_mutex_lock
(L
.L
'Access);
281 pragma Assert
(Result
= 0);
283 Ceiling_Violation
:= False;
284 -- Why is this commented out ???
285 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
286 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
290 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
292 Result
: Interfaces
.C
.int
;
294 if not Single_Lock
or else Global_Lock
then
295 Result
:= pthread_mutex_lock
(L
);
296 pragma Assert
(Result
= 0);
300 procedure Write_Lock
(T
: Task_ID
) is
301 Result
: Interfaces
.C
.int
;
303 if not Single_Lock
then
304 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
305 pragma Assert
(Result
= 0);
313 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
315 Write_Lock
(L
, Ceiling_Violation
);
322 procedure Unlock
(L
: access Lock
) is
323 Result
: Interfaces
.C
.int
;
325 Result
:= pthread_mutex_unlock
(L
.L
'Access);
326 pragma Assert
(Result
= 0);
329 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
330 Result
: Interfaces
.C
.int
;
332 if not Single_Lock
or else Global_Lock
then
333 Result
:= pthread_mutex_unlock
(L
);
334 pragma Assert
(Result
= 0);
338 procedure Unlock
(T
: Task_ID
) is
339 Result
: Interfaces
.C
.int
;
341 if not Single_Lock
then
342 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
343 pragma Assert
(Result
= 0);
353 Reason
: System
.Tasking
.Task_States
)
355 Result
: Interfaces
.C
.int
;
358 Result
:= pthread_cond_wait
359 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
361 Result
:= pthread_cond_wait
362 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
365 -- EINTR is not considered a failure.
366 pragma Assert
(Result
= 0 or else Result
= EINTR
);
368 if Self_ID
.Deferral_Level
= 0
369 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
372 raise Standard
'Abort_Signal;
380 procedure Timed_Sleep
383 Mode
: ST
.Delay_Modes
;
384 Reason
: System
.Tasking
.Task_States
;
385 Timedout
: out Boolean;
386 Yielded
: out Boolean)
388 Sleep_Time
: OS_Time
;
389 Result
: Interfaces
.C
.int
;
390 Status
: Cond_Value_Type
;
396 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
398 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
399 or else Self_ID
.Pending_Priority_Change
404 Self_ID
.Common
.LL
.AST_Pending
:= True;
407 (Status
, 0, Sleep_Time
,
408 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
410 if (Status
and 1) /= 1 then
415 Result
:= pthread_cond_wait
416 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
419 Result
:= pthread_cond_wait
420 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
425 if not Self_ID
.Common
.LL
.AST_Pending
then
428 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
429 pragma Assert
((Status
and 1) = 1);
437 procedure Timed_Delay
440 Mode
: ST
.Delay_Modes
)
442 Sleep_Time
: OS_Time
;
443 Result
: Interfaces
.C
.int
;
444 Status
: Cond_Value_Type
;
445 Yielded
: Boolean := False;
448 -- Only the little window between deferring abort and
449 -- locking Self_ID is the reason we need to
450 -- check for pending abort and priority change below!
457 Write_Lock
(Self_ID
);
459 if Time
/= 0.0 or else Mode
/= Relative
then
460 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
462 if Mode
= Relative
or else OS_Clock
< Sleep_Time
then
463 Self_ID
.Common
.State
:= Delay_Sleep
;
464 Self_ID
.Common
.LL
.AST_Pending
:= True;
467 (Status
, 0, Sleep_Time
,
468 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
470 if (Status
and 1) /= 1 then
475 if Self_ID
.Pending_Priority_Change
then
476 Self_ID
.Pending_Priority_Change
:= False;
477 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
478 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
481 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
482 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
483 pragma Assert
((Status
and 1) = 1);
488 Result
:= pthread_cond_wait
489 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
491 Result
:= pthread_cond_wait
492 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
497 exit when not Self_ID
.Common
.LL
.AST_Pending
;
500 Self_ID
.Common
.State
:= Runnable
;
511 Result
:= sched_yield
;
514 SSL
.Abort_Undefer
.all;
517 ---------------------
518 -- Monotonic_Clock --
519 ---------------------
521 function Monotonic_Clock
return Duration
522 renames System
.OS_Primitives
.Monotonic_Clock
;
528 function RT_Resolution
return Duration is
537 procedure Wakeup
(T
: Task_ID
; Reason
: System
.Tasking
.Task_States
) is
538 Result
: Interfaces
.C
.int
;
540 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
541 pragma Assert
(Result
= 0);
548 procedure Yield
(Do_Yield
: Boolean := True) is
549 Result
: Interfaces
.C
.int
;
552 Result
:= sched_yield
;
560 procedure Set_Priority
562 Prio
: System
.Any_Priority
;
563 Loss_Of_Inheritance
: Boolean := False)
565 Result
: Interfaces
.C
.int
;
566 Param
: aliased struct_sched_param
;
568 T
.Common
.Current_Priority
:= Prio
;
569 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
571 if Time_Slice_Val
> 0 then
572 Result
:= pthread_setschedparam
573 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
575 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
576 Result
:= pthread_setschedparam
577 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
580 Result
:= pthread_setschedparam
581 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
584 pragma Assert
(Result
= 0);
591 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
593 return T
.Common
.Current_Priority
;
600 procedure Enter_Task
(Self_ID
: Task_ID
) is
601 Result
: Interfaces
.C
.int
;
603 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
605 -- It is not safe for the new task accept signals until it
606 -- has bound its TCB pointer to the thread with pthread_setspecific (),
607 -- since the handler wrappers use the TCB pointer
608 -- to restore the stack limit.
610 Result
:= pthread_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
611 pragma Assert
(Result
= 0);
615 for J
in Known_Tasks
'Range loop
616 if Known_Tasks
(J
) = null then
617 Known_Tasks
(J
) := Self_ID
;
618 Self_ID
.Known_Tasks_Index
:= J
;
630 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
632 return new Ada_Task_Control_Block
(Entry_Num
);
635 ----------------------
637 ----------------------
639 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
640 Mutex_Attr
: aliased pthread_mutexattr_t
;
641 Result
: Interfaces
.C
.int
;
642 Cond_Attr
: aliased pthread_condattr_t
;
645 if not Single_Lock
then
646 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
647 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
650 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
652 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
660 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
661 pragma Assert
(Result
= 0);
664 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
665 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
668 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
670 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
675 Self_ID
.Common
.LL
.Exc_Stack_Ptr
:= new Exc_Stack_T
;
676 SSL
.Set_Exc_Stack_Addr
677 (To_Address
(Self_ID
),
678 Self_ID
.Common
.LL
.Exc_Stack_Ptr
(Exc_Stack_T
'Last)'Address);
681 if not Single_Lock
then
682 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
683 pragma Assert
(Result
= 0);
689 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
690 pragma Assert
(Result
= 0);
697 procedure Create_Task
699 Wrapper
: System
.Address
;
700 Stack_Size
: System
.Parameters
.Size_Type
;
701 Priority
: System
.Any_Priority
;
702 Succeeded
: out Boolean)
704 Attributes
: aliased pthread_attr_t
;
705 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
706 Result
: Interfaces
.C
.int
;
708 function Thread_Body_Access
is new
709 Unchecked_Conversion
(System
.Address
, Thread_Body
);
712 if Stack_Size
= Unspecified_Size
then
713 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
715 elsif Stack_Size
< Minimum_Stack_Size
then
716 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
719 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
722 -- Since the initial signal mask of a thread is inherited from the
723 -- creator, we need to set our local signal mask mask all signals
724 -- during the creation operation, to make sure the new thread is
725 -- not disturbed by signals before it has set its own Task_ID.
727 Result
:= pthread_attr_init
(Attributes
'Access);
728 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
735 Result
:= pthread_attr_setdetachstate
736 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
737 pragma Assert
(Result
= 0);
739 Result
:= pthread_attr_setstacksize
740 (Attributes
'Access, Adjusted_Stack_Size
);
741 pragma Assert
(Result
= 0);
743 -- This call may be unnecessary, not sure. ???
745 Result
:= pthread_attr_setinheritsched
746 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
747 pragma Assert
(Result
= 0);
749 Result
:= pthread_create
750 (T
.Common
.LL
.Thread
'Access,
752 Thread_Body_Access
(Wrapper
),
755 -- ENOMEM is a valid run-time error. Don't shut down.
757 pragma Assert
(Result
= 0
758 or else Result
= EAGAIN
or else Result
= ENOMEM
);
760 Succeeded
:= Result
= 0;
762 Result
:= pthread_attr_destroy
(Attributes
'Access);
763 pragma Assert
(Result
= 0);
766 Set_Priority
(T
, Priority
);
774 procedure Finalize_TCB
(T
: Task_ID
) is
775 Result
: Interfaces
.C
.int
;
778 procedure Free
is new
779 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
781 procedure Free
is new Unchecked_Deallocation
782 (Exc_Stack_T
, Exc_Stack_Ptr_T
);
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;
797 Free
(T
.Common
.LL
.Exc_Stack_Ptr
);
805 procedure Exit_Task
is
807 pthread_exit
(System
.Null_Address
);
814 procedure Abort_Task
(T
: Task_ID
) is
818 -- Why is this commented out ???
819 -- if T = Self and then T.Deferral_Level = 0
820 -- and then T.Pending_ATC_Level < T.ATC_Nesting_Level
822 -- raise Standard'Abort_Signal;
826 -- Interrupt Server_Tasks may be waiting on an event flag
828 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
829 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
838 -- Dummy versions. The only currently working versions is for solaris
841 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
850 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
855 ----------------------
856 -- Environment_Task --
857 ----------------------
859 function Environment_Task
return Task_ID
is
861 return Environment_Task_ID
;
862 end Environment_Task
;
868 procedure Lock_RTS
is
870 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
877 procedure Unlock_RTS
is
879 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
886 function Suspend_Task
888 Thread_Self
: Thread_Id
) return Boolean is
899 Thread_Self
: Thread_Id
) return Boolean is
908 procedure Initialize
(Environment_Task
: Task_ID
) is
910 Environment_Task_ID
:= Environment_Task
;
912 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
913 -- Initialize the lock used to synchronize chain of all ATCBs.
915 Enter_Task
(Environment_Task
);
920 Result
: Interfaces
.C
.int
;
922 Result
:= pthread_key_create
(ATCB_Key
'Access, null);
923 pragma Assert
(Result
= 0);
925 end System
.Task_Primitives
.Operations
;