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 a Solaris (native) 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
47 -- used for Raise_Exception
50 -- used for String_Access, Getenv
56 with System
.Interrupt_Management
;
57 -- used for Keep_Unmasked
58 -- Abort_Task_Interrupt
61 with System
.Interrupt_Management
.Operations
;
62 -- used for Set_Interrupt_Mask
64 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
66 with System
.Parameters
;
70 -- used for Ada_Task_Control_Block
72 -- ATCB components and types
74 with System
.Task_Info
;
75 -- to initialize Task_Info for a C thread, in function Self
77 with System
.Soft_Links
;
78 -- used for Defer/Undefer_Abort
79 -- to initialize TSD for a C thread, in function Self
81 -- Note that we do not use System.Tasking.Initialization directly since
82 -- this is a higher level package that we shouldn't depend on. For example
83 -- when using the restricted run time, it is replaced by
84 -- System.Tasking.Restricted.Stages.
86 with System
.OS_Primitives
;
87 -- used for Delay_Modes
89 with Unchecked_Deallocation
;
91 package body System
.Task_Primitives
.Operations
is
93 use System
.Tasking
.Debug
;
96 use System
.OS_Interface
;
97 use System
.Parameters
;
99 use System
.OS_Primitives
;
101 package SSL
renames System
.Soft_Links
;
107 -- The following are logically constants, but need to be initialized
110 Environment_Task_Id
: Task_Id
;
111 -- A variable to hold Task_Id for the environment task.
112 -- If we use this variable to get the Task_Id, we need the following
113 -- ATCB_Key only for non-Ada threads.
115 Unblocked_Signal_Mask
: aliased sigset_t
;
116 -- The set of signals that should unblocked in all tasks
118 ATCB_Key
: aliased thread_key_t
;
119 -- Key used to find the Ada Task_Id associated with a thread,
120 -- at least for C threads unknown to the Ada run-time system.
122 Single_RTS_Lock
: aliased RTS_Lock
;
123 -- This is a lock to allow only one thread of control in the RTS at
124 -- a time; it is used to execute in mutual exclusion from all other tasks.
125 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
127 Next_Serial_Number
: Task_Serial_Number
:= 100;
128 -- We start at 100, to reserve some special values for
129 -- using in error checking.
130 -- The following are internal configuration constants needed.
132 ----------------------
133 -- Priority Support --
134 ----------------------
136 Priority_Ceiling_Emulation
: constant Boolean := True;
137 -- controls whether we emulate priority ceiling locking
139 -- To get a scheduling close to annex D requirements, we use the real-time
140 -- class provided for LWP's and map each task/thread to a specific and
141 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
143 -- The real time class can only be set when the process has root
144 -- priviledges, so in the other cases, we use the normal thread scheduling
145 -- and priority handling.
147 Using_Real_Time_Class
: Boolean := False;
148 -- indicates wether the real time class is being used (i.e the process
149 -- has root priviledges).
151 Prio_Param
: aliased struct_pcparms
;
152 -- Hold priority info (Real_Time) initialized during the package
155 -----------------------------------
156 -- External Configuration Values --
157 -----------------------------------
159 Time_Slice_Val
: Interfaces
.C
.long
;
160 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
162 Locking_Policy
: Character;
163 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
165 Dispatching_Policy
: Character;
166 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
168 Foreign_Task_Elaborated
: aliased Boolean := True;
169 -- Used to identified fake tasks (i.e., non-Ada Threads).
171 -----------------------
172 -- Local Subprograms --
173 -----------------------
175 function sysconf
(name
: System
.OS_Interface
.int
) return processorid_t
;
176 pragma Import
(C
, sysconf
, "sysconf");
178 SC_NPROCESSORS_CONF
: constant System
.OS_Interface
.int
:= 14;
181 (name
: System
.OS_Interface
.int
:= SC_NPROCESSORS_CONF
)
182 return processorid_t
renames sysconf
;
184 procedure Abort_Handler
186 Code
: access siginfo_t
;
187 Context
: access ucontext_t
);
188 -- Target-dependent binding of inter-thread Abort signal to
189 -- the raising of the Abort_Signal exception.
190 -- See also comments in 7staprop.adb
196 function Check_Initialize_Lock
198 Level
: Lock_Level
) return Boolean;
199 pragma Inline
(Check_Initialize_Lock
);
201 function Check_Lock
(L
: Lock_Ptr
) return Boolean;
202 pragma Inline
(Check_Lock
);
204 function Record_Lock
(L
: Lock_Ptr
) return Boolean;
205 pragma Inline
(Record_Lock
);
207 function Check_Sleep
(Reason
: Task_States
) return Boolean;
208 pragma Inline
(Check_Sleep
);
210 function Record_Wakeup
212 Reason
: Task_States
) return Boolean;
213 pragma Inline
(Record_Wakeup
);
215 function Check_Wakeup
217 Reason
: Task_States
) return Boolean;
218 pragma Inline
(Check_Wakeup
);
220 function Check_Unlock
(L
: Lock_Ptr
) return Boolean;
221 pragma Inline
(Check_Unlock
);
223 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean;
224 pragma Inline
(Check_Finalize_Lock
);
232 procedure Initialize
(Environment_Task
: Task_Id
);
233 pragma Inline
(Initialize
);
234 -- Initialize various data needed by this package.
236 function Is_Valid_Task
return Boolean;
237 pragma Inline
(Is_Valid_Task
);
238 -- Does executing thread have a TCB?
240 procedure Set
(Self_Id
: Task_Id
);
242 -- Set the self id for the current task.
244 function Self
return Task_Id
;
245 pragma Inline
(Self
);
246 -- Return a pointer to the Ada Task Control Block of the calling task.
250 package body Specific
is separate;
251 -- The body of this package is target specific.
253 ---------------------------------
254 -- Support for foreign threads --
255 ---------------------------------
257 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
258 -- Allocate and Initialize a new ATCB for the current Thread.
260 function Register_Foreign_Thread
261 (Thread
: Thread_Id
) return Task_Id
is separate;
267 Check_Count
: Integer := 0;
268 Lock_Count
: Integer := 0;
269 Unlock_Count
: Integer := 0;
275 procedure Abort_Handler
277 Code
: access siginfo_t
;
278 Context
: access ucontext_t
)
280 pragma Unreferenced
(Sig
);
281 pragma Unreferenced
(Code
);
282 pragma Unreferenced
(Context
);
284 Self_ID
: constant Task_Id
:= Self
;
285 Old_Set
: aliased sigset_t
;
287 Result
: Interfaces
.C
.int
;
288 pragma Unreferenced
(Result
);
291 -- It is not safe to raise an exception when using ZCX and the GCC
292 -- exception handling mechanism.
294 if ZCX_By_Default
and then GCC_ZCX_Support
then
298 if Self_ID
.Deferral_Level
= 0
299 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
300 and then not Self_ID
.Aborting
302 Self_ID
.Aborting
:= True;
304 -- Make sure signals used for RTS internal purpose are unmasked
306 Result
:= thr_sigsetmask
(SIG_UNBLOCK
,
307 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
308 pragma Assert
(Result
= 0);
310 raise Standard
'Abort_Signal;
318 -- The underlying thread system sets a guard page at the
319 -- bottom of a thread stack, so nothing is needed.
321 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
322 pragma Unreferenced
(T
);
323 pragma Unreferenced
(On
);
332 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
334 return T
.Common
.LL
.Thread
;
341 procedure Initialize
(Environment_Task
: ST
.Task_Id
) is
342 act
: aliased struct_sigaction
;
343 old_act
: aliased struct_sigaction
;
344 Tmp_Set
: aliased sigset_t
;
345 Result
: Interfaces
.C
.int
;
347 procedure Configure_Processors
;
348 -- Processors configuration
349 -- The user can specify a processor which the program should run
350 -- on to emulate a single-processor system. This can be easily
351 -- done by setting environment variable GNAT_PROCESSOR to one of
354 -- -2 : use the default configuration (run the program on all
355 -- available processors) - this is the same as having
356 -- GNAT_PROCESSOR unset
357 -- -1 : let the RTS choose one processor and run the program on
359 -- 0 .. Last_Proc : run the program on the specified processor
361 -- Last_Proc is equal to the value of the system variable
362 -- _SC_NPROCESSORS_CONF, minus one.
364 procedure Configure_Processors
is
365 Proc_Acc
: constant GNAT
.OS_Lib
.String_Access
:=
366 GNAT
.OS_Lib
.Getenv
("GNAT_PROCESSOR");
367 Proc
: aliased processorid_t
; -- User processor #
368 Last_Proc
: processorid_t
; -- Last processor #
371 if Proc_Acc
.all'Length /= 0 then
372 -- Environment variable is defined
374 Last_Proc
:= Num_Procs
- 1;
376 if Last_Proc
/= -1 then
377 Proc
:= processorid_t
'Value (Proc_Acc
.all);
379 if Proc
<= -2 or else Proc
> Last_Proc
then
380 -- Use the default configuration
383 -- Choose a processor
387 while Proc
< Last_Proc
loop
389 Result
:= p_online
(Proc
, PR_STATUS
);
390 exit when Result
= PR_ONLINE
;
393 pragma Assert
(Result
= PR_ONLINE
);
394 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
395 pragma Assert
(Result
= 0);
398 -- Use user processor
400 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
401 pragma Assert
(Result
= 0);
407 when Constraint_Error
=>
409 -- Illegal environment variable GNAT_PROCESSOR - ignored
412 end Configure_Processors
;
415 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
416 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
417 -- Get interrupt state. Defined in a-init.c
418 -- The input argument is the interrupt number,
419 -- and the result is one of the following:
421 Default
: constant Character := 's';
422 -- 'n' this interrupt not set by any Interrupt_State pragma
423 -- 'u' Interrupt_State pragma set state to User
424 -- 'r' Interrupt_State pragma set state to Runtime
425 -- 's' Interrupt_State pragma set state to System (use "default"
428 -- Start of processing for Initialize
431 Environment_Task_Id
:= Environment_Task
;
433 -- This is done in Enter_Task, but this is too late for the
434 -- Environment Task, since we need to call Self in Check_Locks when
435 -- the run time is compiled with assertions on.
437 Specific
.Initialize
(Environment_Task
);
439 -- Initialize the lock used to synchronize chain of all ATCBs.
441 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
443 Enter_Task
(Environment_Task
);
445 -- Install the abort-signal handler
447 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
450 -- Set sa_flags to SA_NODEFER so that during the handler execution
451 -- we do not change the Signal_Mask to be masked for the Abort_Signal
452 -- This is a temporary fix to the problem that the Signal_Mask is
453 -- not restored after the exception (longjmp) from the handler.
454 -- The right fix should be made in sigsetjmp so that we save
455 -- the Signal_Set and restore it after a longjmp.
456 -- In that case, this field should be changed back to 0. ???
460 act
.sa_handler
:= Abort_Handler
'Address;
461 Result
:= sigemptyset
(Tmp_Set
'Access);
462 pragma Assert
(Result
= 0);
463 act
.sa_mask
:= Tmp_Set
;
467 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
468 act
'Unchecked_Access,
469 old_act
'Unchecked_Access);
470 pragma Assert
(Result
= 0);
473 Configure_Processors
;
476 ---------------------
477 -- Initialize_Lock --
478 ---------------------
480 -- Note: mutexes and cond_variables needed per-task basis are
481 -- initialized in Initialize_TCB and the Storage_Error is
482 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
483 -- used in RTS is initialized before any status change of RTS.
484 -- Therefore rasing Storage_Error in the following routines
485 -- should be able to be handled safely.
487 procedure Initialize_Lock
488 (Prio
: System
.Any_Priority
;
491 Result
: Interfaces
.C
.int
;
494 pragma Assert
(Check_Initialize_Lock
(Lock_Ptr
(L
), PO_Level
));
496 if Priority_Ceiling_Emulation
then
500 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
501 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
503 if Result
= ENOMEM
then
504 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
508 procedure Initialize_Lock
509 (L
: access RTS_Lock
;
512 Result
: Interfaces
.C
.int
;
515 pragma Assert
(Check_Initialize_Lock
516 (To_Lock_Ptr
(RTS_Lock_Ptr
(L
)), Level
));
517 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
518 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
520 if Result
= ENOMEM
then
521 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
529 procedure Finalize_Lock
(L
: access Lock
) is
530 Result
: Interfaces
.C
.int
;
533 pragma Assert
(Check_Finalize_Lock
(Lock_Ptr
(L
)));
534 Result
:= mutex_destroy
(L
.L
'Access);
535 pragma Assert
(Result
= 0);
538 procedure Finalize_Lock
(L
: access RTS_Lock
) is
539 Result
: Interfaces
.C
.int
;
542 pragma Assert
(Check_Finalize_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
543 Result
:= mutex_destroy
(L
.L
'Access);
544 pragma Assert
(Result
= 0);
551 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
552 Result
: Interfaces
.C
.int
;
555 pragma Assert
(Check_Lock
(Lock_Ptr
(L
)));
557 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
559 Self_Id
: constant Task_Id
:= Self
;
560 Saved_Priority
: System
.Any_Priority
;
563 if Self_Id
.Common
.LL
.Active_Priority
> L
.Ceiling
then
564 Ceiling_Violation
:= True;
568 Saved_Priority
:= Self_Id
.Common
.LL
.Active_Priority
;
570 if Self_Id
.Common
.LL
.Active_Priority
< L
.Ceiling
then
571 Set_Priority
(Self_Id
, L
.Ceiling
);
574 Result
:= mutex_lock
(L
.L
'Access);
575 pragma Assert
(Result
= 0);
576 Ceiling_Violation
:= False;
578 L
.Saved_Priority
:= Saved_Priority
;
582 Result
:= mutex_lock
(L
.L
'Access);
583 pragma Assert
(Result
= 0);
584 Ceiling_Violation
:= False;
587 pragma Assert
(Record_Lock
(Lock_Ptr
(L
)));
591 (L
: access RTS_Lock
;
592 Global_Lock
: Boolean := False)
594 Result
: Interfaces
.C
.int
;
597 if not Single_Lock
or else Global_Lock
then
598 pragma Assert
(Check_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
599 Result
:= mutex_lock
(L
.L
'Access);
600 pragma Assert
(Result
= 0);
601 pragma Assert
(Record_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
605 procedure Write_Lock
(T
: Task_Id
) is
606 Result
: Interfaces
.C
.int
;
609 if not Single_Lock
then
610 pragma Assert
(Check_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
611 Result
:= mutex_lock
(T
.Common
.LL
.L
.L
'Access);
612 pragma Assert
(Result
= 0);
613 pragma Assert
(Record_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
621 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
623 Write_Lock
(L
, Ceiling_Violation
);
630 procedure Unlock
(L
: access Lock
) is
631 Result
: Interfaces
.C
.int
;
634 pragma Assert
(Check_Unlock
(Lock_Ptr
(L
)));
636 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
638 Self_Id
: constant Task_Id
:= Self
;
641 Result
:= mutex_unlock
(L
.L
'Access);
642 pragma Assert
(Result
= 0);
644 if Self_Id
.Common
.LL
.Active_Priority
> L
.Saved_Priority
then
645 Set_Priority
(Self_Id
, L
.Saved_Priority
);
649 Result
:= mutex_unlock
(L
.L
'Access);
650 pragma Assert
(Result
= 0);
654 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
655 Result
: Interfaces
.C
.int
;
658 if not Single_Lock
or else Global_Lock
then
659 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
660 Result
:= mutex_unlock
(L
.L
'Access);
661 pragma Assert
(Result
= 0);
665 procedure Unlock
(T
: Task_Id
) is
666 Result
: Interfaces
.C
.int
;
669 if not Single_Lock
then
670 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
671 Result
:= mutex_unlock
(T
.Common
.LL
.L
.L
'Access);
672 pragma Assert
(Result
= 0);
676 -- For the time delay implementation, we need to make sure we
677 -- achieve following criteria:
679 -- 1) We have to delay at least for the amount requested.
680 -- 2) We have to give up CPU even though the actual delay does not
681 -- result in blocking.
682 -- 3) Except for restricted run-time systems that do not support
683 -- ATC or task abort, the delay must be interrupted by the
684 -- abort_task operation.
685 -- 4) The implementation has to be efficient so that the delay overhead
686 -- is relatively cheap.
687 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
688 -- requirement we still want to provide the effect in all cases.
689 -- The reason is that users may want to use short delays to implement
690 -- their own scheduling effect in the absence of language provided
691 -- scheduling policies.
693 ---------------------
694 -- Monotonic_Clock --
695 ---------------------
697 function Monotonic_Clock
return Duration is
698 TS
: aliased timespec
;
699 Result
: Interfaces
.C
.int
;
701 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
702 pragma Assert
(Result
= 0);
703 return To_Duration
(TS
);
710 function RT_Resolution
return Duration is
719 procedure Yield
(Do_Yield
: Boolean := True) is
722 System
.OS_Interface
.thr_yield
;
730 function Self
return Task_Id
renames Specific
.Self
;
736 procedure Set_Priority
738 Prio
: System
.Any_Priority
;
739 Loss_Of_Inheritance
: Boolean := False)
741 pragma Unreferenced
(Loss_Of_Inheritance
);
743 Result
: Interfaces
.C
.int
;
744 pragma Unreferenced
(Result
);
746 Param
: aliased struct_pcparms
;
751 T
.Common
.Current_Priority
:= Prio
;
753 if Priority_Ceiling_Emulation
then
754 T
.Common
.LL
.Active_Priority
:= Prio
;
757 if Using_Real_Time_Class
then
758 Param
.pc_cid
:= Prio_Param
.pc_cid
;
759 Param
.rt_pri
:= pri_t
(Prio
);
760 Param
.rt_tqsecs
:= Prio_Param
.rt_tqsecs
;
761 Param
.rt_tqnsecs
:= Prio_Param
.rt_tqnsecs
;
763 Result
:= Interfaces
.C
.int
(
764 priocntl
(PC_VERSION
, P_LWPID
, T
.Common
.LL
.LWP
, PC_SETPARMS
,
768 if T
.Common
.Task_Info
/= null
769 and then not T
.Common
.Task_Info
.Bound_To_LWP
771 -- The task is not bound to a LWP, so use thr_setprio
774 thr_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
778 -- The task is bound to a LWP, use priocntl
790 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
792 return T
.Common
.Current_Priority
;
799 procedure Enter_Task
(Self_ID
: Task_Id
) is
800 Result
: Interfaces
.C
.int
;
801 Proc
: processorid_t
; -- User processor #
802 Last_Proc
: processorid_t
; -- Last processor #
804 use System
.Task_Info
;
806 Self_ID
.Common
.LL
.Thread
:= thr_self
;
808 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
810 if Self_ID
.Common
.Task_Info
/= null then
811 if Self_ID
.Common
.Task_Info
.New_LWP
812 and then Self_ID
.Common
.Task_Info
.CPU
/= CPU_UNCHANGED
814 Last_Proc
:= Num_Procs
- 1;
816 if Self_ID
.Common
.Task_Info
.CPU
= ANY_CPU
then
820 while Proc
< Last_Proc
loop
821 Result
:= p_online
(Proc
, PR_STATUS
);
822 exit when Result
= PR_ONLINE
;
826 Result
:= processor_bind
(P_LWPID
, P_MYID
, Proc
, null);
827 pragma Assert
(Result
= 0);
830 -- Use specified processor
832 if Self_ID
.Common
.Task_Info
.CPU
< 0
833 or else Self_ID
.Common
.Task_Info
.CPU
> Last_Proc
835 raise Invalid_CPU_Number
;
838 Result
:= processor_bind
839 (P_LWPID
, P_MYID
, Self_ID
.Common
.Task_Info
.CPU
, null);
840 pragma Assert
(Result
= 0);
845 Specific
.Set
(Self_ID
);
847 -- We need the above code even if we do direct fetch of Task_Id in Self
848 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
852 for J
in Known_Tasks
'Range loop
853 if Known_Tasks
(J
) = null then
854 Known_Tasks
(J
) := Self_ID
;
855 Self_ID
.Known_Tasks_Index
:= J
;
867 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
869 return new Ada_Task_Control_Block
(Entry_Num
);
876 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
878 -----------------------------
879 -- Register_Foreign_Thread --
880 -----------------------------
882 function Register_Foreign_Thread
return Task_Id
is
884 if Is_Valid_Task
then
887 return Register_Foreign_Thread
(thr_self
);
889 end Register_Foreign_Thread
;
895 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
896 Result
: Interfaces
.C
.int
:= 0;
899 -- Give the task a unique serial number.
901 Self_ID
.Serial_Number
:= Next_Serial_Number
;
902 Next_Serial_Number
:= Next_Serial_Number
+ 1;
903 pragma Assert
(Next_Serial_Number
/= 0);
905 Self_ID
.Common
.LL
.Thread
:= To_thread_t
(-1);
907 if not Single_Lock
then
909 (Self_ID
.Common
.LL
.L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
910 Self_ID
.Common
.LL
.L
.Level
:=
911 Private_Task_Serial_Number
(Self_ID
.Serial_Number
);
912 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
916 Result
:= cond_init
(Self_ID
.Common
.LL
.CV
'Access, USYNC_THREAD
, 0);
917 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
923 if not Single_Lock
then
924 Result
:= mutex_destroy
(Self_ID
.Common
.LL
.L
.L
'Access);
925 pragma Assert
(Result
= 0);
936 procedure Create_Task
938 Wrapper
: System
.Address
;
939 Stack_Size
: System
.Parameters
.Size_Type
;
940 Priority
: System
.Any_Priority
;
941 Succeeded
: out Boolean)
943 pragma Unreferenced
(Priority
);
945 Result
: Interfaces
.C
.int
;
946 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
947 Opts
: Interfaces
.C
.int
:= THR_DETACHED
;
949 Page_Size
: constant System
.Parameters
.Size_Type
:= 4096;
950 -- This constant is for reserving extra space at the
951 -- end of the stack, which can be used by the stack
952 -- checking as guard page. The idea is that we need
953 -- to have at least Stack_Size bytes available for
956 use System
.Task_Info
;
959 if Stack_Size
= System
.Parameters
.Unspecified_Size
then
960 Adjusted_Stack_Size
:=
961 Interfaces
.C
.size_t
(Default_Stack_Size
+ Page_Size
);
963 elsif Stack_Size
< Minimum_Stack_Size
then
964 Adjusted_Stack_Size
:=
965 Interfaces
.C
.size_t
(Minimum_Stack_Size
+ Page_Size
);
968 Adjusted_Stack_Size
:=
969 Interfaces
.C
.size_t
(Stack_Size
+ Page_Size
);
972 -- Since the initial signal mask of a thread is inherited from the
973 -- creator, and the Environment task has all its signals masked, we
974 -- do not need to manipulate caller's signal mask at this point.
975 -- All tasks in RTS will have All_Tasks_Mask initially.
977 if T
.Common
.Task_Info
/= null then
978 if T
.Common
.Task_Info
.New_LWP
then
979 Opts
:= Opts
+ THR_NEW_LWP
;
982 if T
.Common
.Task_Info
.Bound_To_LWP
then
983 Opts
:= Opts
+ THR_BOUND
;
987 Opts
:= THR_DETACHED
+ THR_BOUND
;
991 (System
.Null_Address
,
993 Thread_Body_Access
(Wrapper
),
996 T
.Common
.LL
.Thread
'Access);
998 Succeeded
:= Result
= 0;
1001 or else Result
= ENOMEM
1002 or else Result
= EAGAIN
);
1009 procedure Finalize_TCB
(T
: Task_Id
) is
1010 Result
: Interfaces
.C
.int
;
1012 Is_Self
: constant Boolean := T
= Self
;
1014 procedure Free
is new
1015 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
1018 T
.Common
.LL
.Thread
:= To_thread_t
(0);
1020 if not Single_Lock
then
1021 Result
:= mutex_destroy
(T
.Common
.LL
.L
.L
'Access);
1022 pragma Assert
(Result
= 0);
1025 Result
:= cond_destroy
(T
.Common
.LL
.CV
'Access);
1026 pragma Assert
(Result
= 0);
1028 if T
.Known_Tasks_Index
/= -1 then
1029 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1035 Specific
.Set
(null);
1043 -- This procedure must be called with abort deferred.
1044 -- It can no longer call Self or access
1045 -- the current task's ATCB, since the ATCB has been deallocated.
1047 procedure Exit_Task
is
1049 Specific
.Set
(null);
1056 procedure Abort_Task
(T
: Task_Id
) is
1057 Result
: Interfaces
.C
.int
;
1059 pragma Assert
(T
/= Self
);
1061 Result
:= thr_kill
(T
.Common
.LL
.Thread
,
1062 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1065 pragma Assert
(Result
= 0);
1074 Reason
: Task_States
)
1076 Result
: Interfaces
.C
.int
;
1079 pragma Assert
(Check_Sleep
(Reason
));
1081 if Dynamic_Priority_Support
1082 and then Self_ID
.Pending_Priority_Change
1084 Self_ID
.Pending_Priority_Change
:= False;
1085 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1086 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1091 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
.L
'Access);
1094 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
.L
'Access);
1097 pragma Assert
(Record_Wakeup
1098 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1099 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1102 -- Note that we are relying heaviliy here on the GNAT feature
1103 -- that Calendar.Time, System.Real_Time.Time, Duration, and
1104 -- System.Real_Time.Time_Span are all represented in the same
1105 -- way, i.e., as a 64-bit count of nanoseconds.
1107 -- This allows us to always pass the timeout value as a Duration.
1110 -- We are taking liberties here with the semantics of the delays.
1111 -- That is, we make no distinction between delays on the Calendar clock
1112 -- and delays on the Real_Time clock. That is technically incorrect, if
1113 -- the Calendar clock happens to be reset or adjusted.
1114 -- To solve this defect will require modification to the compiler
1115 -- interface, so that it can pass through more information, to tell
1116 -- us here which clock to use!
1118 -- cond_timedwait will return if any of the following happens:
1119 -- 1) some other task did cond_signal on this condition variable
1120 -- In this case, the return value is 0
1121 -- 2) the call just returned, for no good reason
1122 -- This is called a "spurious wakeup".
1123 -- In this case, the return value may also be 0.
1124 -- 3) the time delay expires
1125 -- In this case, the return value is ETIME
1126 -- 4) this task received a signal, which was handled by some
1127 -- handler procedure, and now the thread is resuming execution
1128 -- UNIX calls this an "interrupted" system call.
1129 -- In this case, the return value is EINTR
1131 -- If the cond_timedwait returns 0 or EINTR, it is still
1132 -- possible that the time has actually expired, and by chance
1133 -- a signal or cond_signal occurred at around the same time.
1135 -- We have also observed that on some OS's the value ETIME
1136 -- will be returned, but the clock will show that the full delay
1137 -- has not yet expired.
1139 -- For these reasons, we need to check the clock after return
1140 -- from cond_timedwait. If the time has expired, we will set
1143 -- This check might be omitted for systems on which the
1144 -- cond_timedwait() never returns early or wakes up spuriously.
1146 -- Annex D requires that completion of a delay cause the task
1147 -- to go to the end of its priority queue, regardless of whether
1148 -- the task actually was suspended by the delay. Since
1149 -- cond_timedwait does not do this on Solaris, we add a call
1150 -- to thr_yield at the end. We might do this at the beginning,
1151 -- instead, but then the round-robin effect would not be the
1152 -- same; the delayed task would be ahead of other tasks of the
1153 -- same priority that awoke while it was sleeping.
1155 -- For Timed_Sleep, we are expecting possible cond_signals
1156 -- to indicate other events (e.g., completion of a RV or
1157 -- completion of the abortable part of an async. select),
1158 -- we want to always return if interrupted. The caller will
1159 -- be responsible for checking the task state to see whether
1160 -- the wakeup was spurious, and to go back to sleep again
1161 -- in that case. We don't need to check for pending abort
1162 -- or priority change on the way in our out; that is the
1163 -- caller's responsibility.
1165 -- For Timed_Delay, we are not expecting any cond_signals or
1166 -- other interruptions, except for priority changes and aborts.
1167 -- Therefore, we don't want to return unless the delay has
1168 -- actually expired, or the call has been aborted. In this
1169 -- case, since we want to implement the entire delay statement
1170 -- semantics, we do need to check for pending abort and priority
1171 -- changes. We can quietly handle priority changes inside the
1172 -- procedure, since there is no entry-queue reordering involved.
1178 procedure Timed_Sleep
1181 Mode
: ST
.Delay_Modes
;
1182 Reason
: System
.Tasking
.Task_States
;
1183 Timedout
: out Boolean;
1184 Yielded
: out Boolean)
1186 Check_Time
: constant Duration := Monotonic_Clock
;
1187 Abs_Time
: Duration;
1188 Request
: aliased timespec
;
1189 Result
: Interfaces
.C
.int
;
1192 pragma Assert
(Check_Sleep
(Reason
));
1196 if Mode
= Relative
then
1197 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
1199 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1202 if Abs_Time
> Check_Time
then
1203 Request
:= To_Timespec
(Abs_Time
);
1206 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
1207 or else (Dynamic_Priority_Support
and then
1208 Self_ID
.Pending_Priority_Change
);
1211 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1212 Single_RTS_Lock
.L
'Access, Request
'Access);
1214 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1215 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1220 exit when Abs_Time
<= Monotonic_Clock
;
1222 if Result
= 0 or Result
= EINTR
then
1224 -- Somebody may have called Wakeup for us
1230 pragma Assert
(Result
= ETIME
);
1234 pragma Assert
(Record_Wakeup
1235 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1242 procedure Timed_Delay
1245 Mode
: ST
.Delay_Modes
)
1247 Check_Time
: constant Duration := Monotonic_Clock
;
1248 Abs_Time
: Duration;
1249 Request
: aliased timespec
;
1250 Result
: Interfaces
.C
.int
;
1251 Yielded
: Boolean := False;
1254 -- Only the little window between deferring abort and
1255 -- locking Self_ID is the reason we need to
1256 -- check for pending abort and priority change below!
1258 SSL
.Abort_Defer
.all;
1264 Write_Lock
(Self_ID
);
1266 if Mode
= Relative
then
1267 Abs_Time
:= Time
+ Check_Time
;
1269 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1272 if Abs_Time
> Check_Time
then
1273 Request
:= To_Timespec
(Abs_Time
);
1274 Self_ID
.Common
.State
:= Delay_Sleep
;
1276 pragma Assert
(Check_Sleep
(Delay_Sleep
));
1279 if Dynamic_Priority_Support
and then
1280 Self_ID
.Pending_Priority_Change
then
1281 Self_ID
.Pending_Priority_Change
:= False;
1282 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1283 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1286 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
1289 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1290 Single_RTS_Lock
.L
'Access, Request
'Access);
1292 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1293 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1298 exit when Abs_Time
<= Monotonic_Clock
;
1300 pragma Assert
(Result
= 0 or else
1301 Result
= ETIME
or else
1305 pragma Assert
(Record_Wakeup
1306 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Delay_Sleep
));
1308 Self_ID
.Common
.State
:= Runnable
;
1321 SSL
.Abort_Undefer
.all;
1330 Reason
: Task_States
)
1332 Result
: Interfaces
.C
.int
;
1335 pragma Assert
(Check_Wakeup
(T
, Reason
));
1336 Result
:= cond_signal
(T
.Common
.LL
.CV
'Access);
1337 pragma Assert
(Result
= 0);
1340 ---------------------------
1341 -- Check_Initialize_Lock --
1342 ---------------------------
1344 -- The following code is intended to check some of the invariant
1345 -- assertions related to lock usage, on which we depend.
1347 function Check_Initialize_Lock
1349 Level
: Lock_Level
) return Boolean
1351 Self_ID
: constant Task_Id
:= Self
;
1354 -- Check that caller is abort-deferred
1356 if Self_ID
.Deferral_Level
<= 0 then
1360 -- Check that the lock is not yet initialized
1362 if L
.Level
/= 0 then
1366 L
.Level
:= Lock_Level
'Pos (Level
) + 1;
1368 end Check_Initialize_Lock
;
1374 function Check_Lock
(L
: Lock_Ptr
) return Boolean is
1375 Self_ID
: constant Task_Id
:= Self
;
1379 -- Check that the argument is not null
1385 -- Check that L is not frozen
1391 -- Check that caller is abort-deferred
1393 if Self_ID
.Deferral_Level
<= 0 then
1397 -- Check that caller is not holding this lock already
1399 if L
.Owner
= To_Owner_ID
(To_Address
(Self_ID
)) then
1407 -- Check that TCB lock order rules are satisfied
1409 P
:= Self_ID
.Common
.LL
.Locks
;
1411 if P
.Level
>= L
.Level
1412 and then (P
.Level
> 2 or else L
.Level
> 2)
1425 function Record_Lock
(L
: Lock_Ptr
) return Boolean is
1426 Self_ID
: constant Task_Id
:= Self
;
1430 Lock_Count
:= Lock_Count
+ 1;
1432 -- There should be no owner for this lock at this point
1434 if L
.Owner
/= null then
1440 L
.Owner
:= To_Owner_ID
(To_Address
(Self_ID
));
1446 -- Check that TCB lock order rules are satisfied
1448 P
:= Self_ID
.Common
.LL
.Locks
;
1454 Self_ID
.Common
.LL
.Locking
:= null;
1455 Self_ID
.Common
.LL
.Locks
:= L
;
1463 function Check_Sleep
(Reason
: Task_States
) return Boolean is
1464 pragma Unreferenced
(Reason
);
1466 Self_ID
: constant Task_Id
:= Self
;
1470 -- Check that caller is abort-deferred
1472 if Self_ID
.Deferral_Level
<= 0 then
1480 -- Check that caller is holding own lock, on top of list
1482 if Self_ID
.Common
.LL
.Locks
/=
1483 To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access)
1488 -- Check that TCB lock order rules are satisfied
1490 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1494 Self_ID
.Common
.LL
.L
.Owner
:= null;
1495 P
:= Self_ID
.Common
.LL
.Locks
;
1496 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1505 function Record_Wakeup
1507 Reason
: Task_States
) return Boolean
1509 pragma Unreferenced
(Reason
);
1511 Self_ID
: constant Task_Id
:= Self
;
1517 L
.Owner
:= To_Owner_ID
(To_Address
(Self_ID
));
1523 -- Check that TCB lock order rules are satisfied
1525 P
:= Self_ID
.Common
.LL
.Locks
;
1531 Self_ID
.Common
.LL
.Locking
:= null;
1532 Self_ID
.Common
.LL
.Locks
:= L
;
1540 function Check_Wakeup
1542 Reason
: Task_States
) return Boolean
1544 Self_ID
: constant Task_Id
:= Self
;
1547 -- Is caller holding T's lock?
1549 if T
.Common
.LL
.L
.Owner
/= To_Owner_ID
(To_Address
(Self_ID
)) then
1553 -- Are reasons for wakeup and sleep consistent?
1555 if T
.Common
.State
/= Reason
then
1566 function Check_Unlock
(L
: Lock_Ptr
) return Boolean is
1567 Self_ID
: constant Task_Id
:= Self
;
1571 Unlock_Count
:= Unlock_Count
+ 1;
1577 if L
.Buddy
/= null then
1582 Check_Count
:= Unlock_Count
;
1585 if Unlock_Count
- Check_Count
> 1000 then
1586 Check_Count
:= Unlock_Count
;
1589 -- Check that caller is abort-deferred
1591 if Self_ID
.Deferral_Level
<= 0 then
1595 -- Check that caller is holding this lock, on top of list
1597 if Self_ID
.Common
.LL
.Locks
/= L
then
1601 -- Record there is no owner now
1604 P
:= Self_ID
.Common
.LL
.Locks
;
1605 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1610 --------------------
1611 -- Check_Finalize --
1612 --------------------
1614 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean is
1615 Self_ID
: constant Task_Id
:= Self
;
1618 -- Check that caller is abort-deferred
1620 if Self_ID
.Deferral_Level
<= 0 then
1624 -- Check that no one is holding this lock
1626 if L
.Owner
/= null then
1632 end Check_Finalize_Lock
;
1638 function Check_Exit
(Self_ID
: Task_Id
) return Boolean is
1640 -- Check that caller is just holding Global_Task_Lock
1641 -- and no other locks
1643 if Self_ID
.Common
.LL
.Locks
= null then
1647 -- 2 = Global_Task_Level
1649 if Self_ID
.Common
.LL
.Locks
.Level
/= 2 then
1653 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1657 -- Check that caller is abort-deferred
1659 if Self_ID
.Deferral_Level
<= 0 then
1666 --------------------
1667 -- Check_No_Locks --
1668 --------------------
1670 function Check_No_Locks
(Self_ID
: Task_Id
) return Boolean is
1672 return Self_ID
.Common
.LL
.Locks
= null;
1675 ----------------------
1676 -- Environment_Task --
1677 ----------------------
1679 function Environment_Task
return Task_Id
is
1681 return Environment_Task_Id
;
1682 end Environment_Task
;
1688 procedure Lock_RTS
is
1690 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1697 procedure Unlock_RTS
is
1699 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1706 function Suspend_Task
1708 Thread_Self
: Thread_Id
) return Boolean
1711 if T
.Common
.LL
.Thread
/= Thread_Self
then
1712 return thr_suspend
(T
.Common
.LL
.Thread
) = 0;
1722 function Resume_Task
1724 Thread_Self
: Thread_Id
) return Boolean
1727 if T
.Common
.LL
.Thread
/= Thread_Self
then
1728 return thr_continue
(T
.Common
.LL
.Thread
) = 0;
1734 -- Package elaboration
1738 Result
: Interfaces
.C
.int
;
1741 -- Mask Environment task for all signals. The original mask of the
1742 -- Environment task will be recovered by Interrupt_Server task
1743 -- during the elaboration of s-interr.adb.
1745 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1746 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1748 -- Prepare the set of signals that should unblocked in all tasks
1750 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1751 pragma Assert
(Result
= 0);
1753 for J
in Interrupt_Management
.Interrupt_ID
loop
1754 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1755 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1756 pragma Assert
(Result
= 0);
1760 -- We need the following code to support automatic creation of fake
1761 -- ATCB's for C threads that call the Ada run-time system, even if
1762 -- we use a faster way of getting Self for real Ada tasks.
1764 Result
:= thr_keycreate
(ATCB_Key
'Access, System
.Null_Address
);
1765 pragma Assert
(Result
= 0);
1768 if Dispatching_Policy
= 'F' then
1770 Result
: Interfaces
.C
.long
;
1771 Class_Info
: aliased struct_pcinfo
;
1772 Secs
, Nsecs
: Interfaces
.C
.long
;
1775 -- If a pragma Time_Slice is specified, takes the value in account.
1777 if Time_Slice_Val
> 0 then
1778 -- Convert Time_Slice_Val (microseconds) into seconds and
1781 Secs
:= Time_Slice_Val
/ 1_000_000
;
1782 Nsecs
:= (Time_Slice_Val
rem 1_000_000
) * 1_000
;
1784 -- Otherwise, default to no time slicing (i.e run until blocked)
1791 -- Get the real time class id.
1793 Class_Info
.pc_clname
(1) := 'R';
1794 Class_Info
.pc_clname
(2) := 'T';
1795 Class_Info
.pc_clname
(3) := ASCII
.NUL
;
1797 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_GETCID
,
1798 Class_Info
'Address);
1800 -- Request the real time class
1802 Prio_Param
.pc_cid
:= Class_Info
.pc_cid
;
1803 Prio_Param
.rt_pri
:= pri_t
(Class_Info
.rt_maxpri
);
1804 Prio_Param
.rt_tqsecs
:= Secs
;
1805 Prio_Param
.rt_tqnsecs
:= Nsecs
;
1807 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_SETPARMS
,
1808 Prio_Param
'Address);
1810 Using_Real_Time_Class
:= Result
/= -1;
1813 end System
.Task_Primitives
.Operations
;