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-2002, 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.Initialization
86 with System
.OS_Primitives
;
87 -- used for Delay_Modes
89 with Unchecked_Conversion
;
90 with Unchecked_Deallocation
;
92 package body System
.Task_Primitives
.Operations
is
94 use System
.Tasking
.Debug
;
97 use System
.OS_Interface
;
98 use System
.Parameters
;
100 use System
.OS_Primitives
;
102 package SSL
renames System
.Soft_Links
;
108 -- The following are logically constants, but need to be initialized
111 Environment_Task_ID
: Task_ID
;
112 -- A variable to hold Task_ID for the environment task.
113 -- If we use this variable to get the Task_ID, we need the following
114 -- ATCB_Key only for non-Ada threads.
116 Unblocked_Signal_Mask
: aliased sigset_t
;
117 -- The set of signals that should unblocked in all tasks
119 ATCB_Key
: aliased thread_key_t
;
120 -- Key used to find the Ada Task_ID associated with a thread,
121 -- at least for C threads unknown to the Ada run-time system.
123 Single_RTS_Lock
: aliased RTS_Lock
;
124 -- This is a lock to allow only one thread of control in the RTS at
125 -- a time; it is used to execute in mutual exclusion from all other tasks.
126 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
128 Next_Serial_Number
: Task_Serial_Number
:= 100;
129 -- We start at 100, to reserve some special values for
130 -- using in error checking.
131 -- The following are internal configuration constants needed.
133 ------------------------
134 -- Priority Support --
135 ------------------------
137 Priority_Ceiling_Emulation
: constant Boolean := True;
138 -- controls whether we emulate priority ceiling locking
140 -- To get a scheduling close to annex D requirements, we use the real-time
141 -- class provided for LWP's and map each task/thread to a specific and
142 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
144 -- The real time class can only be set when the process has root
145 -- priviledges, so in the other cases, we use the normal thread scheduling
146 -- and priority handling.
148 Using_Real_Time_Class
: Boolean := False;
149 -- indicates wether the real time class is being used (i.e the process
150 -- has root priviledges).
152 Prio_Param
: aliased struct_pcparms
;
153 -- Hold priority info (Real_Time) initialized during the package
156 -------------------------------------
157 -- External Configuration Values --
158 -------------------------------------
160 Time_Slice_Val
: Interfaces
.C
.long
;
161 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
163 Locking_Policy
: Character;
164 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
166 Dispatching_Policy
: Character;
167 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
169 --------------------------------
170 -- Foreign Threads Detection --
171 --------------------------------
173 -- The following are used to allow the Self function to
174 -- automatically generate ATCB's for C threads that happen to call
175 -- Ada procedure, which in turn happen to call the Ada run-time system.
178 type Fake_ATCB_Ptr
is access Fake_ATCB
;
179 type Fake_ATCB
is record
180 Stack_Base
: Interfaces
.C
.unsigned
:= 0;
181 -- A value of zero indicates the node is not in use.
182 Next
: Fake_ATCB_Ptr
;
183 Real_ATCB
: aliased Ada_Task_Control_Block
(0);
186 Fake_ATCB_List
: Fake_ATCB_Ptr
;
187 -- A linear linked list.
188 -- The list is protected by Single_RTS_Lock;
189 -- Nodes are added to this list from the front.
190 -- Once a node is added to this list, it is never removed.
192 Fake_Task_Elaborated
: aliased Boolean := True;
193 -- Used to identified fake tasks (i.e., non-Ada Threads).
195 Next_Fake_ATCB
: Fake_ATCB_Ptr
;
196 -- Used to allocate one Fake_ATCB in advance. See comment in New_Fake_ATCB
202 Check_Count
: Integer := 0;
204 Lock_Count
: Integer := 0;
205 Unlock_Count
: Integer := 0;
207 function To_Lock_Ptr
is
208 new Unchecked_Conversion
(RTS_Lock_Ptr
, Lock_Ptr
);
209 function To_Task_ID
is
210 new Unchecked_Conversion
(Owner_ID
, Task_ID
);
211 function To_Owner_ID
is
212 new Unchecked_Conversion
(Task_ID
, Owner_ID
);
214 -----------------------
215 -- Local Subprograms --
216 -----------------------
218 function sysconf
(name
: System
.OS_Interface
.int
)
219 return processorid_t
;
220 pragma Import
(C
, sysconf
, "sysconf");
222 SC_NPROCESSORS_CONF
: constant System
.OS_Interface
.int
:= 14;
224 function Num_Procs
(name
: System
.OS_Interface
.int
:= SC_NPROCESSORS_CONF
)
225 return processorid_t
renames sysconf
;
227 procedure Abort_Handler
229 Code
: access siginfo_t
;
230 Context
: access ucontext_t
);
232 function To_thread_t
is new Unchecked_Conversion
233 (Integer, System
.OS_Interface
.thread_t
);
235 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
237 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
239 function Thread_Body_Access
is
240 new Unchecked_Conversion
(System
.Address
, Thread_Body
);
242 function New_Fake_ATCB
(Stack_Base
: Interfaces
.C
.unsigned
) return Task_ID
;
243 -- Allocate and Initialize a new ATCB. This code can safely be called from
244 -- a foreign thread, as it doesn't access implicitly or explicitly
245 -- "self" before having initialized the new ATCB.
246 pragma Warnings
(Off
, New_Fake_ATCB
);
247 -- Disable warning on this function, since the Solaris x86 version does
254 function Check_Initialize_Lock
(L
: Lock_Ptr
; Level
: Lock_Level
)
256 pragma Inline
(Check_Initialize_Lock
);
258 function Check_Lock
(L
: Lock_Ptr
) return Boolean;
259 pragma Inline
(Check_Lock
);
261 function Record_Lock
(L
: Lock_Ptr
) return Boolean;
262 pragma Inline
(Record_Lock
);
264 function Check_Sleep
(Reason
: Task_States
) return Boolean;
265 pragma Inline
(Check_Sleep
);
267 function Record_Wakeup
269 Reason
: Task_States
) return Boolean;
270 pragma Inline
(Record_Wakeup
);
272 function Check_Wakeup
274 Reason
: Task_States
) return Boolean;
275 pragma Inline
(Check_Wakeup
);
277 function Check_Unlock
(L
: Lock_Ptr
) return Boolean;
278 pragma Inline
(Check_Lock
);
280 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean;
281 pragma Inline
(Check_Finalize_Lock
);
287 function New_Fake_ATCB
(Stack_Base
: Interfaces
.C
.unsigned
)
291 P
, Q
: Fake_ATCB_Ptr
;
293 Result
: Interfaces
.C
.int
;
296 -- This section is ticklish.
297 -- We dare not call anything that might require an ATCB, until
298 -- we have the new ATCB in place.
299 -- Note: we don't use Lock_RTS because we don't yet have an ATCB, and
300 -- so can't pass the safety check.
302 Result
:= mutex_lock
(Single_RTS_Lock
.L
'Access);
307 if P
.Stack_Base
= 0 then
309 elsif thr_kill
(P
.Real_ATCB
.Common
.LL
.Thread
, 0) /= 0 then
311 -- If a C thread that has dependent Ada tasks terminates
312 -- abruptly, e.g. as a result of cancellation, any dependent
313 -- tasks are likely to hang up in termination.
323 -- Create a new ATCB with zero entries.
325 Self_ID
:= Next_Fake_ATCB
.Real_ATCB
'Access;
326 Next_Fake_ATCB
.Stack_Base
:= Stack_Base
;
327 Next_Fake_ATCB
.Next
:= Fake_ATCB_List
;
328 Fake_ATCB_List
:= Next_Fake_ATCB
;
329 Next_Fake_ATCB
:= null;
333 -- Reuse an existing fake ATCB.
335 Self_ID
:= Q
.Real_ATCB
'Access;
336 Q
.Stack_Base
:= Stack_Base
;
339 -- Do the standard initializations
341 System
.Tasking
.Initialize_ATCB
342 (Self_ID
, null, Null_Address
, Null_Task
, Fake_Task_Elaborated
'Access,
343 System
.Priority
'First, Task_Info
.Unspecified_Task_Info
, 0, Self_ID
,
345 pragma Assert
(Succeeded
);
347 -- Record this as the Task_ID for the current thread.
349 Self_ID
.Common
.LL
.Thread
:= thr_self
;
350 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
351 pragma Assert
(Result
= 0);
353 -- Finally, it is safe to use an allocator in this thread.
355 if Next_Fake_ATCB
= null then
356 Next_Fake_ATCB
:= new Fake_ATCB
;
359 Self_ID
.Master_of_Task
:= 0;
360 Self_ID
.Master_Within
:= Self_ID
.Master_of_Task
+ 1;
362 for L
in Self_ID
.Entry_Calls
'Range loop
363 Self_ID
.Entry_Calls
(L
).Self
:= Self_ID
;
364 Self_ID
.Entry_Calls
(L
).Level
:= L
;
367 Self_ID
.Common
.State
:= Runnable
;
368 Self_ID
.Awake_Count
:= 1;
370 -- Since this is not an ordinary Ada task, we will start out undeferred
372 Self_ID
.Deferral_Level
:= 0;
374 -- Give the task a unique serial number.
376 Self_ID
.Serial_Number
:= Next_Serial_Number
;
377 Next_Serial_Number
:= Next_Serial_Number
+ 1;
378 pragma Assert
(Next_Serial_Number
/= 0);
380 System
.Soft_Links
.Create_TSD
(Self_ID
.Common
.Compiler_Data
);
383 -- The following call is commented out to avoid dependence on
384 -- the System.Tasking.Initialization package.
386 -- It seems that if we want Ada.Task_Attributes to work correctly
387 -- for C threads we will need to raise the visibility of this soft
388 -- link to System.Soft_Links.
390 -- We are putting that off until this new functionality is otherwise
393 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
395 -- Must not unlock until Next_ATCB is again allocated.
397 for J
in Known_Tasks
'Range loop
398 if Known_Tasks
(J
) = null then
399 Known_Tasks
(J
) := Self_ID
;
400 Self_ID
.Known_Tasks_Index
:= J
;
405 Result
:= mutex_unlock
(Single_RTS_Lock
.L
'Access);
407 -- We cannot use Unlock_RTS because we did not use Write_Lock, and so
408 -- would not pass the checks.
417 -- Target-dependent binding of inter-thread Abort signal to
418 -- the raising of the Abort_Signal exception.
420 -- The technical issues and alternatives here are essentially
421 -- the same as for raising exceptions in response to other
422 -- signals (e.g. Storage_Error). See code and comments in
423 -- the package body System.Interrupt_Management.
425 -- Some implementations may not allow an exception to be propagated
426 -- out of a handler, and others might leave the signal or
427 -- interrupt that invoked this handler masked after the exceptional
428 -- return to the application code.
430 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
431 -- On most UNIX systems, this will allow transfer out of a signal handler,
432 -- which is usually the only mechanism available for implementing
433 -- asynchronous handlers of this kind. However, some
434 -- systems do not restore the signal mask on longjmp(), leaving the
435 -- abort signal masked.
437 -- Alternative solutions include:
439 -- 1. Change the PC saved in the system-dependent Context
440 -- parameter to point to code that raises the exception.
441 -- Normal return from this handler will then raise
442 -- the exception after the mask and other system state has
443 -- been restored (see example below).
444 -- 2. Use siglongjmp()/sigsetjmp() to implement exceptions.
445 -- 3. Unmask the signal in the Abortion_Signal exception handler
448 -- The following procedure would be needed if we can't longjmp out of
449 -- a signal handler. (See below.)
451 -- procedure Raise_Abort_Signal is
453 -- raise Standard'Abort_Signal;
457 -- The comments above need revising. They are partly obsolete.
459 procedure Abort_Handler
461 Code
: access siginfo_t
;
462 Context
: access ucontext_t
)
464 Self_ID
: Task_ID
:= Self
;
465 Result
: Interfaces
.C
.int
;
466 Old_Set
: aliased sigset_t
;
469 -- Assuming it is safe to longjmp out of a signal handler, the
470 -- following code can be used:
472 if Self_ID
.Deferral_Level
= 0
473 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
474 and then not Self_ID
.Aborting
476 -- You can comment the following out,
477 -- to make all aborts synchronous, for debugging.
479 Self_ID
.Aborting
:= True;
481 -- Make sure signals used for RTS internal purpose are unmasked
483 Result
:= thr_sigsetmask
(SIG_UNBLOCK
,
484 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
485 pragma Assert
(Result
= 0);
487 raise Standard
'Abort_Signal;
490 -- Must be certain that the implementation of "raise"
491 -- does not make any OS/thread calls, or at least that
492 -- if it makes any, they are safe for interruption by
496 -- Otherwise, something like this is required:
497 -- if not Abort_Is_Deferred.all then
498 -- -- Overwrite the return PC address with the address of the
499 -- -- special raise routine, and "return" to that routine's
500 -- -- starting address.
501 -- Context.PC := Raise_Abort_Signal'Address;
511 -- The underlying thread system sets a guard page at the
512 -- bottom of a thread stack, so nothing is needed.
514 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
523 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
525 return T
.Common
.LL
.Thread
;
532 function Self
return Task_ID
is separate;
534 ---------------------
535 -- Initialize_Lock --
536 ---------------------
538 -- Note: mutexes and cond_variables needed per-task basis are
539 -- initialized in Initialize_TCB and the Storage_Error is
540 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
541 -- used in RTS is initialized before any status change of RTS.
542 -- Therefore rasing Storage_Error in the following routines
543 -- should be able to be handled safely.
545 procedure Initialize_Lock
546 (Prio
: System
.Any_Priority
;
549 Result
: Interfaces
.C
.int
;
552 pragma Assert
(Check_Initialize_Lock
(Lock_Ptr
(L
), PO_Level
));
554 if Priority_Ceiling_Emulation
then
558 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
559 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
561 if Result
= ENOMEM
then
562 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
566 procedure Initialize_Lock
567 (L
: access RTS_Lock
;
570 Result
: Interfaces
.C
.int
;
573 pragma Assert
(Check_Initialize_Lock
574 (To_Lock_Ptr
(RTS_Lock_Ptr
(L
)), Level
));
575 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
576 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
578 if Result
= ENOMEM
then
579 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
587 procedure Finalize_Lock
(L
: access Lock
) is
588 Result
: Interfaces
.C
.int
;
591 pragma Assert
(Check_Finalize_Lock
(Lock_Ptr
(L
)));
592 Result
:= mutex_destroy
(L
.L
'Access);
593 pragma Assert
(Result
= 0);
596 procedure Finalize_Lock
(L
: access RTS_Lock
) is
597 Result
: Interfaces
.C
.int
;
600 pragma Assert
(Check_Finalize_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
601 Result
:= mutex_destroy
(L
.L
'Access);
602 pragma Assert
(Result
= 0);
609 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
610 Result
: Interfaces
.C
.int
;
613 pragma Assert
(Check_Lock
(Lock_Ptr
(L
)));
615 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
617 Self_Id
: constant Task_ID
:= Self
;
618 Saved_Priority
: System
.Any_Priority
;
621 if Self_Id
.Common
.LL
.Active_Priority
> L
.Ceiling
then
622 Ceiling_Violation
:= True;
626 Saved_Priority
:= Self_Id
.Common
.LL
.Active_Priority
;
628 if Self_Id
.Common
.LL
.Active_Priority
< L
.Ceiling
then
629 Set_Priority
(Self_Id
, L
.Ceiling
);
632 Result
:= mutex_lock
(L
.L
'Access);
633 pragma Assert
(Result
= 0);
634 Ceiling_Violation
:= False;
636 L
.Saved_Priority
:= Saved_Priority
;
640 Result
:= mutex_lock
(L
.L
'Access);
641 pragma Assert
(Result
= 0);
642 Ceiling_Violation
:= False;
645 pragma Assert
(Record_Lock
(Lock_Ptr
(L
)));
649 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
651 Result
: Interfaces
.C
.int
;
653 if not Single_Lock
or else Global_Lock
then
654 pragma Assert
(Check_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
655 Result
:= mutex_lock
(L
.L
'Access);
656 pragma Assert
(Result
= 0);
657 pragma Assert
(Record_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
661 procedure Write_Lock
(T
: Task_ID
) is
662 Result
: Interfaces
.C
.int
;
664 if not Single_Lock
then
665 pragma Assert
(Check_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
666 Result
:= mutex_lock
(T
.Common
.LL
.L
.L
'Access);
667 pragma Assert
(Result
= 0);
668 pragma Assert
(Record_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
676 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
678 Write_Lock
(L
, Ceiling_Violation
);
685 procedure Unlock
(L
: access Lock
) is
686 Result
: Interfaces
.C
.int
;
688 pragma Assert
(Check_Unlock
(Lock_Ptr
(L
)));
690 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
692 Self_Id
: constant Task_ID
:= Self
;
695 Result
:= mutex_unlock
(L
.L
'Access);
696 pragma Assert
(Result
= 0);
698 if Self_Id
.Common
.LL
.Active_Priority
> L
.Saved_Priority
then
699 Set_Priority
(Self_Id
, L
.Saved_Priority
);
703 Result
:= mutex_unlock
(L
.L
'Access);
704 pragma Assert
(Result
= 0);
708 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
709 Result
: Interfaces
.C
.int
;
711 if not Single_Lock
or else Global_Lock
then
712 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
713 Result
:= mutex_unlock
(L
.L
'Access);
714 pragma Assert
(Result
= 0);
718 procedure Unlock
(T
: Task_ID
) is
719 Result
: Interfaces
.C
.int
;
721 if not Single_Lock
then
722 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
723 Result
:= mutex_unlock
(T
.Common
.LL
.L
.L
'Access);
724 pragma Assert
(Result
= 0);
728 -- For the time delay implementation, we need to make sure we
729 -- achieve following criteria:
731 -- 1) We have to delay at least for the amount requested.
732 -- 2) We have to give up CPU even though the actual delay does not
733 -- result in blocking.
734 -- 3) Except for restricted run-time systems that do not support
735 -- ATC or task abort, the delay must be interrupted by the
736 -- abort_task operation.
737 -- 4) The implementation has to be efficient so that the delay overhead
738 -- is relatively cheap.
739 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
740 -- requirement we still want to provide the effect in all cases.
741 -- The reason is that users may want to use short delays to implement
742 -- their own scheduling effect in the absence of language provided
743 -- scheduling policies.
745 ---------------------
746 -- Monotonic_Clock --
747 ---------------------
749 function Monotonic_Clock
return Duration is
750 TS
: aliased timespec
;
751 Result
: Interfaces
.C
.int
;
754 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
755 pragma Assert
(Result
= 0);
756 return To_Duration
(TS
);
763 function RT_Resolution
return Duration is
772 procedure Yield
(Do_Yield
: Boolean := True) is
775 System
.OS_Interface
.thr_yield
;
783 procedure Set_Priority
785 Prio
: System
.Any_Priority
;
786 Loss_Of_Inheritance
: Boolean := False)
788 Result
: Interfaces
.C
.int
;
789 Param
: aliased struct_pcparms
;
794 T
.Common
.Current_Priority
:= Prio
;
796 if Priority_Ceiling_Emulation
then
797 T
.Common
.LL
.Active_Priority
:= Prio
;
800 if Using_Real_Time_Class
then
801 Param
.pc_cid
:= Prio_Param
.pc_cid
;
802 Param
.rt_pri
:= pri_t
(Prio
);
803 Param
.rt_tqsecs
:= Prio_Param
.rt_tqsecs
;
804 Param
.rt_tqnsecs
:= Prio_Param
.rt_tqnsecs
;
806 Result
:= Interfaces
.C
.int
(
807 priocntl
(PC_VERSION
, P_LWPID
, T
.Common
.LL
.LWP
, PC_SETPARMS
,
811 if T
.Common
.Task_Info
/= null
812 and then not T
.Common
.Task_Info
.Bound_To_LWP
814 -- The task is not bound to a LWP, so use thr_setprio
817 thr_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
821 -- The task is bound to a LWP, use priocntl
833 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
835 return T
.Common
.Current_Priority
;
842 procedure Enter_Task
(Self_ID
: Task_ID
) is
843 Result
: Interfaces
.C
.int
;
844 Proc
: processorid_t
; -- User processor #
845 Last_Proc
: processorid_t
; -- Last processor #
847 use System
.Task_Info
;
849 Self_ID
.Common
.LL
.Thread
:= thr_self
;
851 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
853 if Self_ID
.Common
.Task_Info
/= null then
854 if Self_ID
.Common
.Task_Info
.New_LWP
855 and then Self_ID
.Common
.Task_Info
.CPU
/= CPU_UNCHANGED
857 Last_Proc
:= Num_Procs
- 1;
859 if Self_ID
.Common
.Task_Info
.CPU
= ANY_CPU
then
863 while Proc
< Last_Proc
loop
864 Result
:= p_online
(Proc
, PR_STATUS
);
865 exit when Result
= PR_ONLINE
;
869 Result
:= processor_bind
(P_LWPID
, P_MYID
, Proc
, null);
870 pragma Assert
(Result
= 0);
873 -- Use specified processor
875 if Self_ID
.Common
.Task_Info
.CPU
< 0
876 or else Self_ID
.Common
.Task_Info
.CPU
> Last_Proc
878 raise Invalid_CPU_Number
;
881 Result
:= processor_bind
882 (P_LWPID
, P_MYID
, Self_ID
.Common
.Task_Info
.CPU
, null);
883 pragma Assert
(Result
= 0);
888 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
889 pragma Assert
(Result
= 0);
891 -- We need the above code even if we do direct fetch of Task_ID in Self
892 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
896 for J
in Known_Tasks
'Range loop
897 if Known_Tasks
(J
) = null then
898 Known_Tasks
(J
) := Self_ID
;
899 Self_ID
.Known_Tasks_Index
:= J
;
911 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
913 return new Ada_Task_Control_Block
(Entry_Num
);
920 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
921 Result
: Interfaces
.C
.int
:= 0;
923 -- Give the task a unique serial number.
925 Self_ID
.Serial_Number
:= Next_Serial_Number
;
926 Next_Serial_Number
:= Next_Serial_Number
+ 1;
927 pragma Assert
(Next_Serial_Number
/= 0);
929 Self_ID
.Common
.LL
.Thread
:= To_thread_t
(-1);
931 if not Single_Lock
then
933 (Self_ID
.Common
.LL
.L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
934 Self_ID
.Common
.LL
.L
.Level
:=
935 Private_Task_Serial_Number
(Self_ID
.Serial_Number
);
936 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
940 Result
:= cond_init
(Self_ID
.Common
.LL
.CV
'Access, USYNC_THREAD
, 0);
941 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
947 if not Single_Lock
then
948 Result
:= mutex_destroy
(Self_ID
.Common
.LL
.L
.L
'Access);
949 pragma Assert
(Result
= 0);
960 procedure Create_Task
962 Wrapper
: System
.Address
;
963 Stack_Size
: System
.Parameters
.Size_Type
;
964 Priority
: System
.Any_Priority
;
965 Succeeded
: out Boolean)
967 Result
: Interfaces
.C
.int
;
968 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
969 Opts
: Interfaces
.C
.int
:= THR_DETACHED
;
971 Page_Size
: constant System
.Parameters
.Size_Type
:= 4096;
972 -- This constant is for reserving extra space at the
973 -- end of the stack, which can be used by the stack
974 -- checking as guard page. The idea is that we need
975 -- to have at least Stack_Size bytes available for
978 use System
.Task_Info
;
980 if Stack_Size
= System
.Parameters
.Unspecified_Size
then
981 Adjusted_Stack_Size
:=
982 Interfaces
.C
.size_t
(Default_Stack_Size
+ Page_Size
);
984 elsif Stack_Size
< Minimum_Stack_Size
then
985 Adjusted_Stack_Size
:=
986 Interfaces
.C
.size_t
(Minimum_Stack_Size
+ Page_Size
);
989 Adjusted_Stack_Size
:=
990 Interfaces
.C
.size_t
(Stack_Size
+ Page_Size
);
993 -- Since the initial signal mask of a thread is inherited from the
994 -- creator, and the Environment task has all its signals masked, we
995 -- do not need to manipulate caller's signal mask at this point.
996 -- All tasks in RTS will have All_Tasks_Mask initially.
998 if T
.Common
.Task_Info
/= null then
1000 if T
.Common
.Task_Info
.New_LWP
then
1001 Opts
:= Opts
+ THR_NEW_LWP
;
1004 if T
.Common
.Task_Info
.Bound_To_LWP
then
1005 Opts
:= Opts
+ THR_BOUND
;
1009 Opts
:= THR_DETACHED
+ THR_BOUND
;
1012 Result
:= thr_create
1013 (System
.Null_Address
,
1014 Adjusted_Stack_Size
,
1015 Thread_Body_Access
(Wrapper
),
1018 T
.Common
.LL
.Thread
'Access);
1020 Succeeded
:= Result
= 0;
1023 or else Result
= ENOMEM
1024 or else Result
= EAGAIN
);
1031 procedure Finalize_TCB
(T
: Task_ID
) is
1032 Result
: Interfaces
.C
.int
;
1035 procedure Free
is new
1036 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
1039 T
.Common
.LL
.Thread
:= To_thread_t
(0);
1041 if not Single_Lock
then
1042 Result
:= mutex_destroy
(T
.Common
.LL
.L
.L
'Access);
1043 pragma Assert
(Result
= 0);
1046 Result
:= cond_destroy
(T
.Common
.LL
.CV
'Access);
1047 pragma Assert
(Result
= 0);
1049 if T
.Known_Tasks_Index
/= -1 then
1050 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1060 -- This procedure must be called with abort deferred.
1061 -- It can no longer call Self or access
1062 -- the current task's ATCB, since the ATCB has been deallocated.
1064 procedure Exit_Task
is
1066 thr_exit
(System
.Null_Address
);
1073 procedure Abort_Task
(T
: Task_ID
) is
1074 Result
: Interfaces
.C
.int
;
1076 pragma Assert
(T
/= Self
);
1078 Result
:= thr_kill
(T
.Common
.LL
.Thread
,
1079 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1082 pragma Assert
(Result
= 0);
1091 Reason
: Task_States
)
1093 Result
: Interfaces
.C
.int
;
1095 pragma Assert
(Check_Sleep
(Reason
));
1097 if Dynamic_Priority_Support
1098 and then Self_ID
.Pending_Priority_Change
1100 Self_ID
.Pending_Priority_Change
:= False;
1101 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1102 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1107 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
.L
'Access);
1110 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
.L
'Access);
1113 pragma Assert
(Record_Wakeup
1114 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1115 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1118 -- Note that we are relying heaviliy here on the GNAT feature
1119 -- that Calendar.Time, System.Real_Time.Time, Duration, and
1120 -- System.Real_Time.Time_Span are all represented in the same
1121 -- way, i.e., as a 64-bit count of nanoseconds.
1123 -- This allows us to always pass the timeout value as a Duration.
1126 -- We are taking liberties here with the semantics of the delays.
1127 -- That is, we make no distinction between delays on the Calendar clock
1128 -- and delays on the Real_Time clock. That is technically incorrect, if
1129 -- the Calendar clock happens to be reset or adjusted.
1130 -- To solve this defect will require modification to the compiler
1131 -- interface, so that it can pass through more information, to tell
1132 -- us here which clock to use!
1134 -- cond_timedwait will return if any of the following happens:
1135 -- 1) some other task did cond_signal on this condition variable
1136 -- In this case, the return value is 0
1137 -- 2) the call just returned, for no good reason
1138 -- This is called a "spurious wakeup".
1139 -- In this case, the return value may also be 0.
1140 -- 3) the time delay expires
1141 -- In this case, the return value is ETIME
1142 -- 4) this task received a signal, which was handled by some
1143 -- handler procedure, and now the thread is resuming execution
1144 -- UNIX calls this an "interrupted" system call.
1145 -- In this case, the return value is EINTR
1147 -- If the cond_timedwait returns 0 or EINTR, it is still
1148 -- possible that the time has actually expired, and by chance
1149 -- a signal or cond_signal occurred at around the same time.
1151 -- We have also observed that on some OS's the value ETIME
1152 -- will be returned, but the clock will show that the full delay
1153 -- has not yet expired.
1155 -- For these reasons, we need to check the clock after return
1156 -- from cond_timedwait. If the time has expired, we will set
1159 -- This check might be omitted for systems on which the
1160 -- cond_timedwait() never returns early or wakes up spuriously.
1162 -- Annex D requires that completion of a delay cause the task
1163 -- to go to the end of its priority queue, regardless of whether
1164 -- the task actually was suspended by the delay. Since
1165 -- cond_timedwait does not do this on Solaris, we add a call
1166 -- to thr_yield at the end. We might do this at the beginning,
1167 -- instead, but then the round-robin effect would not be the
1168 -- same; the delayed task would be ahead of other tasks of the
1169 -- same priority that awoke while it was sleeping.
1171 -- For Timed_Sleep, we are expecting possible cond_signals
1172 -- to indicate other events (e.g., completion of a RV or
1173 -- completion of the abortable part of an async. select),
1174 -- we want to always return if interrupted. The caller will
1175 -- be responsible for checking the task state to see whether
1176 -- the wakeup was spurious, and to go back to sleep again
1177 -- in that case. We don't need to check for pending abort
1178 -- or priority change on the way in our out; that is the
1179 -- caller's responsibility.
1181 -- For Timed_Delay, we are not expecting any cond_signals or
1182 -- other interruptions, except for priority changes and aborts.
1183 -- Therefore, we don't want to return unless the delay has
1184 -- actually expired, or the call has been aborted. In this
1185 -- case, since we want to implement the entire delay statement
1186 -- semantics, we do need to check for pending abort and priority
1187 -- changes. We can quietly handle priority changes inside the
1188 -- procedure, since there is no entry-queue reordering involved.
1194 procedure Timed_Sleep
1197 Mode
: ST
.Delay_Modes
;
1198 Reason
: System
.Tasking
.Task_States
;
1199 Timedout
: out Boolean;
1200 Yielded
: out Boolean)
1202 Check_Time
: constant Duration := Monotonic_Clock
;
1203 Abs_Time
: Duration;
1204 Request
: aliased timespec
;
1205 Result
: Interfaces
.C
.int
;
1208 pragma Assert
(Check_Sleep
(Reason
));
1212 if Mode
= Relative
then
1213 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
1215 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1218 if Abs_Time
> Check_Time
then
1219 Request
:= To_Timespec
(Abs_Time
);
1222 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
1223 or else (Dynamic_Priority_Support
and then
1224 Self_ID
.Pending_Priority_Change
);
1227 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1228 Single_RTS_Lock
.L
'Access, Request
'Access);
1230 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1231 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1236 exit when Abs_Time
<= Monotonic_Clock
;
1238 if Result
= 0 or Result
= EINTR
then
1239 -- somebody may have called Wakeup for us
1244 pragma Assert
(Result
= ETIME
);
1248 pragma Assert
(Record_Wakeup
1249 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1256 procedure Timed_Delay
1259 Mode
: ST
.Delay_Modes
)
1261 Check_Time
: constant Duration := Monotonic_Clock
;
1262 Abs_Time
: Duration;
1263 Request
: aliased timespec
;
1264 Result
: Interfaces
.C
.int
;
1265 Yielded
: Boolean := False;
1268 -- Only the little window between deferring abort and
1269 -- locking Self_ID is the reason we need to
1270 -- check for pending abort and priority change below!
1272 SSL
.Abort_Defer
.all;
1278 Write_Lock
(Self_ID
);
1280 if Mode
= Relative
then
1281 Abs_Time
:= Time
+ Check_Time
;
1283 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1286 if Abs_Time
> Check_Time
then
1287 Request
:= To_Timespec
(Abs_Time
);
1288 Self_ID
.Common
.State
:= Delay_Sleep
;
1290 pragma Assert
(Check_Sleep
(Delay_Sleep
));
1293 if Dynamic_Priority_Support
and then
1294 Self_ID
.Pending_Priority_Change
then
1295 Self_ID
.Pending_Priority_Change
:= False;
1296 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1297 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1300 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
1303 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1304 Single_RTS_Lock
.L
'Access, Request
'Access);
1306 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1307 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1312 exit when Abs_Time
<= Monotonic_Clock
;
1314 pragma Assert
(Result
= 0 or else
1315 Result
= ETIME
or else
1319 pragma Assert
(Record_Wakeup
1320 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Delay_Sleep
));
1322 Self_ID
.Common
.State
:= Runnable
;
1335 SSL
.Abort_Undefer
.all;
1344 Reason
: Task_States
)
1346 Result
: Interfaces
.C
.int
;
1348 pragma Assert
(Check_Wakeup
(T
, Reason
));
1349 Result
:= cond_signal
(T
.Common
.LL
.CV
'Access);
1350 pragma Assert
(Result
= 0);
1353 ---------------------------
1354 -- Check_Initialize_Lock --
1355 ---------------------------
1357 -- The following code is intended to check some of the invariant
1358 -- assertions related to lock usage, on which we depend.
1360 function Check_Initialize_Lock
1365 Self_ID
: constant Task_ID
:= Self
;
1368 -- Check that caller is abort-deferred
1370 if Self_ID
.Deferral_Level
<= 0 then
1374 -- Check that the lock is not yet initialized
1376 if L
.Level
/= 0 then
1380 L
.Level
:= Lock_Level
'Pos (Level
) + 1;
1382 end Check_Initialize_Lock
;
1388 function Check_Lock
(L
: Lock_Ptr
) return Boolean is
1389 Self_ID
: Task_ID
:= Self
;
1393 -- Check that the argument is not null
1399 -- Check that L is not frozen
1405 -- Check that caller is abort-deferred
1407 if Self_ID
.Deferral_Level
<= 0 then
1411 -- Check that caller is not holding this lock already
1413 if L
.Owner
= To_Owner_ID
(Self_ID
) then
1421 -- Check that TCB lock order rules are satisfied
1423 P
:= Self_ID
.Common
.LL
.Locks
;
1425 if P
.Level
>= L
.Level
1426 and then (P
.Level
> 2 or else L
.Level
> 2)
1439 function Record_Lock
(L
: Lock_Ptr
) return Boolean is
1440 Self_ID
: Task_ID
:= Self
;
1444 Lock_Count
:= Lock_Count
+ 1;
1446 -- There should be no owner for this lock at this point
1448 if L
.Owner
/= null then
1454 L
.Owner
:= To_Owner_ID
(Self_ID
);
1460 -- Check that TCB lock order rules are satisfied
1462 P
:= Self_ID
.Common
.LL
.Locks
;
1468 Self_ID
.Common
.LL
.Locking
:= null;
1469 Self_ID
.Common
.LL
.Locks
:= L
;
1477 function Check_Sleep
(Reason
: Task_States
) return Boolean is
1478 Self_ID
: Task_ID
:= Self
;
1482 -- Check that caller is abort-deferred
1484 if Self_ID
.Deferral_Level
<= 0 then
1492 -- Check that caller is holding own lock, on top of list
1494 if Self_ID
.Common
.LL
.Locks
/=
1495 To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access)
1500 -- Check that TCB lock order rules are satisfied
1502 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1506 Self_ID
.Common
.LL
.L
.Owner
:= null;
1507 P
:= Self_ID
.Common
.LL
.Locks
;
1508 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1517 function Record_Wakeup
1519 Reason
: Task_States
)
1522 Self_ID
: Task_ID
:= Self
;
1528 L
.Owner
:= To_Owner_ID
(Self_ID
);
1534 -- Check that TCB lock order rules are satisfied
1536 P
:= Self_ID
.Common
.LL
.Locks
;
1542 Self_ID
.Common
.LL
.Locking
:= null;
1543 Self_ID
.Common
.LL
.Locks
:= L
;
1551 function Check_Wakeup
1553 Reason
: Task_States
)
1556 Self_ID
: Task_ID
:= Self
;
1559 -- Is caller holding T's lock?
1561 if T
.Common
.LL
.L
.Owner
/= To_Owner_ID
(Self_ID
) then
1565 -- Are reasons for wakeup and sleep consistent?
1567 if T
.Common
.State
/= Reason
then
1578 function Check_Unlock
(L
: Lock_Ptr
) return Boolean is
1579 Self_ID
: Task_ID
:= Self
;
1583 Unlock_Count
:= Unlock_Count
+ 1;
1589 if L
.Buddy
/= null then
1594 Check_Count
:= Unlock_Count
;
1597 if Unlock_Count
- Check_Count
> 1000 then
1598 Check_Count
:= Unlock_Count
;
1599 Old_Owner
:= To_Task_ID
(Single_RTS_Lock
.Owner
);
1602 -- Check that caller is abort-deferred
1604 if Self_ID
.Deferral_Level
<= 0 then
1608 -- Check that caller is holding this lock, on top of list
1610 if Self_ID
.Common
.LL
.Locks
/= L
then
1614 -- Record there is no owner now
1617 P
:= Self_ID
.Common
.LL
.Locks
;
1618 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1623 --------------------
1624 -- Check_Finalize --
1625 --------------------
1627 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean is
1628 Self_ID
: Task_ID
:= Self
;
1630 -- Check that caller is abort-deferred
1632 if Self_ID
.Deferral_Level
<= 0 then
1636 -- Check that no one is holding this lock
1638 if L
.Owner
/= null then
1644 end Check_Finalize_Lock
;
1650 function Check_Exit
(Self_ID
: Task_ID
) return Boolean is
1652 -- Check that caller is just holding Global_Task_Lock
1653 -- and no other locks
1655 if Self_ID
.Common
.LL
.Locks
= null then
1659 -- 2 = Global_Task_Level
1661 if Self_ID
.Common
.LL
.Locks
.Level
/= 2 then
1665 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1669 -- Check that caller is abort-deferred
1671 if Self_ID
.Deferral_Level
<= 0 then
1678 --------------------
1679 -- Check_No_Locks --
1680 --------------------
1682 function Check_No_Locks
(Self_ID
: Task_ID
) return Boolean is
1684 return Self_ID
.Common
.LL
.Locks
= null;
1687 ----------------------
1688 -- Environment_Task --
1689 ----------------------
1691 function Environment_Task
return Task_ID
is
1693 return Environment_Task_ID
;
1694 end Environment_Task
;
1700 procedure Lock_RTS
is
1702 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1709 procedure Unlock_RTS
is
1711 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1718 function Suspend_Task
1720 Thread_Self
: Thread_Id
) return Boolean is
1722 if T
.Common
.LL
.Thread
/= Thread_Self
then
1723 return thr_suspend
(T
.Common
.LL
.Thread
) = 0;
1733 function Resume_Task
1735 Thread_Self
: Thread_Id
) return Boolean is
1737 if T
.Common
.LL
.Thread
/= Thread_Self
then
1738 return thr_continue
(T
.Common
.LL
.Thread
) = 0;
1748 procedure Initialize
(Environment_Task
: ST
.Task_ID
) is
1749 act
: aliased struct_sigaction
;
1750 old_act
: aliased struct_sigaction
;
1751 Tmp_Set
: aliased sigset_t
;
1752 Result
: Interfaces
.C
.int
;
1754 procedure Configure_Processors
;
1755 -- Processors configuration
1756 -- The user can specify a processor which the program should run
1757 -- on to emulate a single-processor system. This can be easily
1758 -- done by setting environment variable GNAT_PROCESSOR to one of
1761 -- -2 : use the default configuration (run the program on all
1762 -- available processors) - this is the same as having
1763 -- GNAT_PROCESSOR unset
1764 -- -1 : let the RTS choose one processor and run the program on
1766 -- 0 .. Last_Proc : run the program on the specified processor
1768 -- Last_Proc is equal to the value of the system variable
1769 -- _SC_NPROCESSORS_CONF, minus one.
1771 procedure Configure_Processors
is
1772 Proc_Acc
: constant GNAT
.OS_Lib
.String_Access
:=
1773 GNAT
.OS_Lib
.Getenv
("GNAT_PROCESSOR");
1774 Proc
: aliased processorid_t
; -- User processor #
1775 Last_Proc
: processorid_t
; -- Last processor #
1778 if Proc_Acc
.all'Length /= 0 then
1779 -- Environment variable is defined
1781 Last_Proc
:= Num_Procs
- 1;
1783 if Last_Proc
/= -1 then
1784 Proc
:= processorid_t
'Value (Proc_Acc
.all);
1786 if Proc
<= -2 or else Proc
> Last_Proc
then
1787 -- Use the default configuration
1789 elsif Proc
= -1 then
1790 -- Choose a processor
1794 while Proc
< Last_Proc
loop
1796 Result
:= p_online
(Proc
, PR_STATUS
);
1797 exit when Result
= PR_ONLINE
;
1800 pragma Assert
(Result
= PR_ONLINE
);
1801 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
1802 pragma Assert
(Result
= 0);
1805 -- Use user processor
1807 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
1808 pragma Assert
(Result
= 0);
1814 when Constraint_Error
=>
1815 -- Illegal environment variable GNAT_PROCESSOR - ignored
1817 end Configure_Processors
;
1819 -- Start of processing for Initialize
1822 Environment_Task_ID
:= Environment_Task
;
1824 -- This is done in Enter_Task, but this is too late for the
1825 -- Environment Task, since we need to call Self in Check_Locks when
1826 -- the run time is compiled with assertions on.
1828 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Environment_Task
));
1829 pragma Assert
(Result
= 0);
1831 -- Initialize the lock used to synchronize chain of all ATCBs.
1833 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1835 Enter_Task
(Environment_Task
);
1837 -- Install the abort-signal handler
1839 -- Set sa_flags to SA_NODEFER so that during the handler execution
1840 -- we do not change the Signal_Mask to be masked for the Abort_Signal.
1841 -- This is a temporary fix to the problem that the Signal_Mask is
1842 -- not restored after the exception (longjmp) from the handler.
1843 -- The right fix should be made in sigsetjmp so that we save
1844 -- the Signal_Set and restore it after a longjmp.
1845 -- In that case, this field should be changed back to 0. ???
1849 act
.sa_handler
:= Abort_Handler
'Address;
1850 Result
:= sigemptyset
(Tmp_Set
'Access);
1851 pragma Assert
(Result
= 0);
1852 act
.sa_mask
:= Tmp_Set
;
1856 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1857 act
'Unchecked_Access,
1858 old_act
'Unchecked_Access);
1859 pragma Assert
(Result
= 0);
1861 Configure_Processors
;
1863 -- Create a free ATCB for use on the Fake_ATCB_List.
1865 Next_Fake_ATCB
:= new Fake_ATCB
;
1868 -- Package elaboration
1872 Result
: Interfaces
.C
.int
;
1874 -- Mask Environment task for all signals. The original mask of the
1875 -- Environment task will be recovered by Interrupt_Server task
1876 -- during the elaboration of s-interr.adb.
1878 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1879 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1881 -- Prepare the set of signals that should unblocked in all tasks
1883 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1884 pragma Assert
(Result
= 0);
1886 for J
in Interrupt_Management
.Interrupt_ID
loop
1887 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1888 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1889 pragma Assert
(Result
= 0);
1893 -- We need the following code to support automatic creation of fake
1894 -- ATCB's for C threads that call the Ada run-time system, even if
1895 -- we use a faster way of getting Self for real Ada tasks.
1897 Result
:= thr_keycreate
(ATCB_Key
'Access, System
.Null_Address
);
1898 pragma Assert
(Result
= 0);
1901 if Dispatching_Policy
= 'F' then
1903 Result
: Interfaces
.C
.long
;
1904 Class_Info
: aliased struct_pcinfo
;
1905 Secs
, Nsecs
: Interfaces
.C
.long
;
1908 -- If a pragma Time_Slice is specified, takes the value in account.
1910 if Time_Slice_Val
> 0 then
1911 -- Convert Time_Slice_Val (microseconds) into seconds and
1914 Secs
:= Time_Slice_Val
/ 1_000_000
;
1915 Nsecs
:= (Time_Slice_Val
rem 1_000_000
) * 1_000
;
1917 -- Otherwise, default to no time slicing (i.e run until blocked)
1924 -- Get the real time class id.
1926 Class_Info
.pc_clname
(1) := 'R';
1927 Class_Info
.pc_clname
(2) := 'T';
1928 Class_Info
.pc_clname
(3) := ASCII
.NUL
;
1930 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_GETCID
,
1931 Class_Info
'Address);
1933 -- Request the real time class
1935 Prio_Param
.pc_cid
:= Class_Info
.pc_cid
;
1936 Prio_Param
.rt_pri
:= pri_t
(Class_Info
.rt_maxpri
);
1937 Prio_Param
.rt_tqsecs
:= Secs
;
1938 Prio_Param
.rt_tqnsecs
:= Nsecs
;
1940 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_SETPARMS
,
1941 Prio_Param
'Address);
1943 Using_Real_Time_Class
:= Result
/= -1;
1946 end System
.Task_Primitives
.Operations
;