1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
10 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
33 ------------------------------------------------------------------------------
35 -- This is a Solaris (native) version of this package
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 -- used for Known_Tasks
48 -- used for Raise_Exception
51 -- used for String_Access, Getenv
57 with System
.Interrupt_Management
;
58 -- used for Keep_Unmasked
59 -- Abort_Task_Interrupt
62 with System
.Interrupt_Management
.Operations
;
63 -- used for Set_Interrupt_Mask
65 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
67 with System
.Parameters
;
71 -- used for Ada_Task_Control_Block
73 -- ATCB components and types
75 with System
.Task_Info
;
76 -- to initialize Task_Info for a C thread, in function Self
78 with System
.Soft_Links
;
79 -- used for Defer/Undefer_Abort
80 -- to initialize TSD for a C thread, in function Self
82 -- Note that we do not use System.Tasking.Initialization directly since
83 -- this is a higher level package that we shouldn't depend on. For example
84 -- when using the restricted run time, it is replaced by
85 -- System.Tasking.Restricted.Initialization
87 with System
.OS_Primitives
;
88 -- used for Delay_Modes
90 with Unchecked_Conversion
;
91 with Unchecked_Deallocation
;
93 package body System
.Task_Primitives
.Operations
is
95 use System
.Tasking
.Debug
;
98 use System
.OS_Interface
;
99 use System
.Parameters
;
101 use System
.OS_Primitives
;
103 package SSL
renames System
.Soft_Links
;
109 -- The following are logically constants, but need to be initialized
112 Environment_Task_ID
: Task_ID
;
113 -- A variable to hold Task_ID for the environment task.
114 -- If we use this variable to get the Task_ID, we need the following
115 -- ATCB_Key only for non-Ada threads.
117 Unblocked_Signal_Mask
: aliased sigset_t
;
118 -- The set of signals that should unblocked in all tasks
120 ATCB_Key
: aliased thread_key_t
;
121 -- Key used to find the Ada Task_ID associated with a thread,
122 -- at least for C threads unknown to the Ada run-time system.
124 Single_RTS_Lock
: aliased RTS_Lock
;
125 -- This is a lock to allow only one thread of control in the RTS at
126 -- a time; it is used to execute in mutual exclusion from all other tasks.
127 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
129 Next_Serial_Number
: Task_Serial_Number
:= 100;
130 -- We start at 100, to reserve some special values for
131 -- using in error checking.
132 -- The following are internal configuration constants needed.
134 ------------------------
135 -- Priority Support --
136 ------------------------
138 Priority_Ceiling_Emulation
: constant Boolean := True;
139 -- controls whether we emulate priority ceiling locking
141 -- To get a scheduling close to annex D requirements, we use the real-time
142 -- class provided for LWP's and map each task/thread to a specific and
143 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
145 -- The real time class can only be set when the process has root
146 -- priviledges, so in the other cases, we use the normal thread scheduling
147 -- and priority handling.
149 Using_Real_Time_Class
: Boolean := False;
150 -- indicates wether the real time class is being used (i.e the process
151 -- has root priviledges).
153 Prio_Param
: aliased struct_pcparms
;
154 -- Hold priority info (Real_Time) initialized during the package
157 -------------------------------------
158 -- External Configuration Values --
159 -------------------------------------
161 Time_Slice_Val
: Interfaces
.C
.long
;
162 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
164 Locking_Policy
: Character;
165 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
167 Dispatching_Policy
: Character;
168 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
170 --------------------------------
171 -- Foreign Threads Detection --
172 --------------------------------
174 -- The following are used to allow the Self function to
175 -- automatically generate ATCB's for C threads that happen to call
176 -- Ada procedure, which in turn happen to call the Ada run-time system.
179 type Fake_ATCB_Ptr
is access Fake_ATCB
;
180 type Fake_ATCB
is record
181 Stack_Base
: Interfaces
.C
.unsigned
:= 0;
182 -- A value of zero indicates the node is not in use.
183 Next
: Fake_ATCB_Ptr
;
184 Real_ATCB
: aliased Ada_Task_Control_Block
(0);
187 Fake_ATCB_List
: Fake_ATCB_Ptr
;
188 -- A linear linked list.
189 -- The list is protected by Single_RTS_Lock;
190 -- Nodes are added to this list from the front.
191 -- Once a node is added to this list, it is never removed.
193 Fake_Task_Elaborated
: aliased Boolean := True;
194 -- Used to identified fake tasks (i.e., non-Ada Threads).
196 Next_Fake_ATCB
: Fake_ATCB_Ptr
;
197 -- Used to allocate one Fake_ATCB in advance. See comment in New_Fake_ATCB
203 Check_Count
: Integer := 0;
205 Lock_Count
: Integer := 0;
206 Unlock_Count
: Integer := 0;
208 function To_Lock_Ptr
is
209 new Unchecked_Conversion
(RTS_Lock_Ptr
, Lock_Ptr
);
210 function To_Task_ID
is
211 new Unchecked_Conversion
(Owner_ID
, Task_ID
);
212 function To_Owner_ID
is
213 new Unchecked_Conversion
(Task_ID
, Owner_ID
);
215 -----------------------
216 -- Local Subprograms --
217 -----------------------
219 function sysconf
(name
: System
.OS_Interface
.int
)
220 return processorid_t
;
221 pragma Import
(C
, sysconf
, "sysconf");
223 SC_NPROCESSORS_CONF
: constant System
.OS_Interface
.int
:= 14;
225 function Num_Procs
(name
: System
.OS_Interface
.int
:= SC_NPROCESSORS_CONF
)
226 return processorid_t
renames sysconf
;
228 procedure Abort_Handler
230 Code
: access siginfo_t
;
231 Context
: access ucontext_t
);
233 function To_thread_t
is new Unchecked_Conversion
234 (Integer, System
.OS_Interface
.thread_t
);
236 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
238 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
240 function Thread_Body_Access
is
241 new Unchecked_Conversion
(System
.Address
, Thread_Body
);
243 function New_Fake_ATCB
(Stack_Base
: Interfaces
.C
.unsigned
) return Task_ID
;
244 -- Allocate and Initialize a new ATCB. This code can safely be called from
245 -- a foreign thread, as it doesn't access implicitly or explicitly
246 -- "self" before having initialized the new ATCB.
247 pragma Warnings
(Off
, New_Fake_ATCB
);
248 -- Disable warning on this function, since the Solaris x86 version does
255 function Check_Initialize_Lock
(L
: Lock_Ptr
; Level
: Lock_Level
)
257 pragma Inline
(Check_Initialize_Lock
);
259 function Check_Lock
(L
: Lock_Ptr
) return Boolean;
260 pragma Inline
(Check_Lock
);
262 function Record_Lock
(L
: Lock_Ptr
) return Boolean;
263 pragma Inline
(Record_Lock
);
265 function Check_Sleep
(Reason
: Task_States
) return Boolean;
266 pragma Inline
(Check_Sleep
);
268 function Record_Wakeup
270 Reason
: Task_States
) return Boolean;
271 pragma Inline
(Record_Wakeup
);
273 function Check_Wakeup
275 Reason
: Task_States
) return Boolean;
276 pragma Inline
(Check_Wakeup
);
278 function Check_Unlock
(L
: Lock_Ptr
) return Boolean;
279 pragma Inline
(Check_Lock
);
281 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean;
282 pragma Inline
(Check_Finalize_Lock
);
288 function New_Fake_ATCB
(Stack_Base
: Interfaces
.C
.unsigned
)
292 P
, Q
: Fake_ATCB_Ptr
;
294 Result
: Interfaces
.C
.int
;
297 -- This section is ticklish.
298 -- We dare not call anything that might require an ATCB, until
299 -- we have the new ATCB in place.
300 -- Note: we don't use Lock_RTS because we don't yet have an ATCB, and
301 -- so can't pass the safety check.
303 Result
:= mutex_lock
(Single_RTS_Lock
.L
'Access);
308 if P
.Stack_Base
= 0 then
310 elsif thr_kill
(P
.Real_ATCB
.Common
.LL
.Thread
, 0) /= 0 then
312 -- If a C thread that has dependent Ada tasks terminates
313 -- abruptly, e.g. as a result of cancellation, any dependent
314 -- tasks are likely to hang up in termination.
324 -- Create a new ATCB with zero entries.
326 Self_ID
:= Next_Fake_ATCB
.Real_ATCB
'Access;
327 Next_Fake_ATCB
.Stack_Base
:= Stack_Base
;
328 Next_Fake_ATCB
.Next
:= Fake_ATCB_List
;
329 Fake_ATCB_List
:= Next_Fake_ATCB
;
330 Next_Fake_ATCB
:= null;
334 -- Reuse an existing fake ATCB.
336 Self_ID
:= Q
.Real_ATCB
'Access;
337 Q
.Stack_Base
:= Stack_Base
;
340 -- Do the standard initializations
342 System
.Tasking
.Initialize_ATCB
343 (Self_ID
, null, Null_Address
, Null_Task
, Fake_Task_Elaborated
'Access,
344 System
.Priority
'First, Task_Info
.Unspecified_Task_Info
, 0, Self_ID
,
346 pragma Assert
(Succeeded
);
348 -- Record this as the Task_ID for the current thread.
350 Self_ID
.Common
.LL
.Thread
:= thr_self
;
351 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
352 pragma Assert
(Result
= 0);
354 -- Finally, it is safe to use an allocator in this thread.
356 if Next_Fake_ATCB
= null then
357 Next_Fake_ATCB
:= new Fake_ATCB
;
360 Self_ID
.Master_of_Task
:= 0;
361 Self_ID
.Master_Within
:= Self_ID
.Master_of_Task
+ 1;
363 for L
in Self_ID
.Entry_Calls
'Range loop
364 Self_ID
.Entry_Calls
(L
).Self
:= Self_ID
;
365 Self_ID
.Entry_Calls
(L
).Level
:= L
;
368 Self_ID
.Common
.State
:= Runnable
;
369 Self_ID
.Awake_Count
:= 1;
371 -- Since this is not an ordinary Ada task, we will start out undeferred
373 Self_ID
.Deferral_Level
:= 0;
375 -- Give the task a unique serial number.
377 Self_ID
.Serial_Number
:= Next_Serial_Number
;
378 Next_Serial_Number
:= Next_Serial_Number
+ 1;
379 pragma Assert
(Next_Serial_Number
/= 0);
381 System
.Soft_Links
.Create_TSD
(Self_ID
.Common
.Compiler_Data
);
384 -- The following call is commented out to avoid dependence on
385 -- the System.Tasking.Initialization package.
387 -- It seems that if we want Ada.Task_Attributes to work correctly
388 -- for C threads we will need to raise the visibility of this soft
389 -- link to System.Soft_Links.
391 -- We are putting that off until this new functionality is otherwise
394 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
396 -- Must not unlock until Next_ATCB is again allocated.
398 for J
in Known_Tasks
'Range loop
399 if Known_Tasks
(J
) = null then
400 Known_Tasks
(J
) := Self_ID
;
401 Self_ID
.Known_Tasks_Index
:= J
;
406 Result
:= mutex_unlock
(Single_RTS_Lock
.L
'Access);
408 -- We cannot use Unlock_RTS because we did not use Write_Lock, and so
409 -- would not pass the checks.
418 -- Target-dependent binding of inter-thread Abort signal to
419 -- the raising of the Abort_Signal exception.
421 -- The technical issues and alternatives here are essentially
422 -- the same as for raising exceptions in response to other
423 -- signals (e.g. Storage_Error). See code and comments in
424 -- the package body System.Interrupt_Management.
426 -- Some implementations may not allow an exception to be propagated
427 -- out of a handler, and others might leave the signal or
428 -- interrupt that invoked this handler masked after the exceptional
429 -- return to the application code.
431 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
432 -- On most UNIX systems, this will allow transfer out of a signal handler,
433 -- which is usually the only mechanism available for implementing
434 -- asynchronous handlers of this kind. However, some
435 -- systems do not restore the signal mask on longjmp(), leaving the
436 -- abort signal masked.
438 -- Alternative solutions include:
440 -- 1. Change the PC saved in the system-dependent Context
441 -- parameter to point to code that raises the exception.
442 -- Normal return from this handler will then raise
443 -- the exception after the mask and other system state has
444 -- been restored (see example below).
445 -- 2. Use siglongjmp()/sigsetjmp() to implement exceptions.
446 -- 3. Unmask the signal in the Abortion_Signal exception handler
449 -- The following procedure would be needed if we can't longjmp out of
450 -- a signal handler. (See below.)
452 -- procedure Raise_Abort_Signal is
454 -- raise Standard'Abort_Signal;
458 -- The comments above need revising. They are partly obsolete.
460 procedure Abort_Handler
462 Code
: access siginfo_t
;
463 Context
: access ucontext_t
)
465 Self_ID
: Task_ID
:= Self
;
466 Result
: Interfaces
.C
.int
;
467 Old_Set
: aliased sigset_t
;
470 -- Assuming it is safe to longjmp out of a signal handler, the
471 -- following code can be used:
473 if Self_ID
.Deferral_Level
= 0
474 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
475 and then not Self_ID
.Aborting
477 -- You can comment the following out,
478 -- to make all aborts synchronous, for debugging.
480 Self_ID
.Aborting
:= True;
482 -- Make sure signals used for RTS internal purpose are unmasked
484 Result
:= thr_sigsetmask
(SIG_UNBLOCK
,
485 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
486 pragma Assert
(Result
= 0);
488 raise Standard
'Abort_Signal;
491 -- Must be certain that the implementation of "raise"
492 -- does not make any OS/thread calls, or at least that
493 -- if it makes any, they are safe for interruption by
497 -- Otherwise, something like this is required:
498 -- if not Abort_Is_Deferred.all then
499 -- -- Overwrite the return PC address with the address of the
500 -- -- special raise routine, and "return" to that routine's
501 -- -- starting address.
502 -- Context.PC := Raise_Abort_Signal'Address;
512 -- The underlying thread system sets a guard page at the
513 -- bottom of a thread stack, so nothing is needed.
515 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
524 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
526 return T
.Common
.LL
.Thread
;
533 function Self
return Task_ID
is separate;
535 ---------------------
536 -- Initialize_Lock --
537 ---------------------
539 -- Note: mutexes and cond_variables needed per-task basis are
540 -- initialized in Initialize_TCB and the Storage_Error is
541 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
542 -- used in RTS is initialized before any status change of RTS.
543 -- Therefore rasing Storage_Error in the following routines
544 -- should be able to be handled safely.
546 procedure Initialize_Lock
547 (Prio
: System
.Any_Priority
;
550 Result
: Interfaces
.C
.int
;
553 pragma Assert
(Check_Initialize_Lock
(Lock_Ptr
(L
), PO_Level
));
555 if Priority_Ceiling_Emulation
then
559 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
560 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
562 if Result
= ENOMEM
then
563 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
567 procedure Initialize_Lock
568 (L
: access RTS_Lock
;
571 Result
: Interfaces
.C
.int
;
574 pragma Assert
(Check_Initialize_Lock
575 (To_Lock_Ptr
(RTS_Lock_Ptr
(L
)), Level
));
576 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
577 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
579 if Result
= ENOMEM
then
580 Raise_Exception
(Storage_Error
'Identity, "Failed to allocate a lock");
588 procedure Finalize_Lock
(L
: access Lock
) is
589 Result
: Interfaces
.C
.int
;
592 pragma Assert
(Check_Finalize_Lock
(Lock_Ptr
(L
)));
593 Result
:= mutex_destroy
(L
.L
'Access);
594 pragma Assert
(Result
= 0);
597 procedure Finalize_Lock
(L
: access RTS_Lock
) is
598 Result
: Interfaces
.C
.int
;
601 pragma Assert
(Check_Finalize_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
602 Result
:= mutex_destroy
(L
.L
'Access);
603 pragma Assert
(Result
= 0);
610 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
611 Result
: Interfaces
.C
.int
;
614 pragma Assert
(Check_Lock
(Lock_Ptr
(L
)));
616 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
618 Self_Id
: constant Task_ID
:= Self
;
619 Saved_Priority
: System
.Any_Priority
;
622 if Self_Id
.Common
.LL
.Active_Priority
> L
.Ceiling
then
623 Ceiling_Violation
:= True;
627 Saved_Priority
:= Self_Id
.Common
.LL
.Active_Priority
;
629 if Self_Id
.Common
.LL
.Active_Priority
< L
.Ceiling
then
630 Set_Priority
(Self_Id
, L
.Ceiling
);
633 Result
:= mutex_lock
(L
.L
'Access);
634 pragma Assert
(Result
= 0);
635 Ceiling_Violation
:= False;
637 L
.Saved_Priority
:= Saved_Priority
;
641 Result
:= mutex_lock
(L
.L
'Access);
642 pragma Assert
(Result
= 0);
643 Ceiling_Violation
:= False;
646 pragma Assert
(Record_Lock
(Lock_Ptr
(L
)));
650 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
652 Result
: Interfaces
.C
.int
;
654 if not Single_Lock
or else Global_Lock
then
655 pragma Assert
(Check_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
656 Result
:= mutex_lock
(L
.L
'Access);
657 pragma Assert
(Result
= 0);
658 pragma Assert
(Record_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
662 procedure Write_Lock
(T
: Task_ID
) is
663 Result
: Interfaces
.C
.int
;
665 if not Single_Lock
then
666 pragma Assert
(Check_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
667 Result
:= mutex_lock
(T
.Common
.LL
.L
.L
'Access);
668 pragma Assert
(Result
= 0);
669 pragma Assert
(Record_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
677 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
679 Write_Lock
(L
, Ceiling_Violation
);
686 procedure Unlock
(L
: access Lock
) is
687 Result
: Interfaces
.C
.int
;
689 pragma Assert
(Check_Unlock
(Lock_Ptr
(L
)));
691 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
693 Self_Id
: constant Task_ID
:= Self
;
696 Result
:= mutex_unlock
(L
.L
'Access);
697 pragma Assert
(Result
= 0);
699 if Self_Id
.Common
.LL
.Active_Priority
> L
.Saved_Priority
then
700 Set_Priority
(Self_Id
, L
.Saved_Priority
);
704 Result
:= mutex_unlock
(L
.L
'Access);
705 pragma Assert
(Result
= 0);
709 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
710 Result
: Interfaces
.C
.int
;
712 if not Single_Lock
or else Global_Lock
then
713 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
714 Result
:= mutex_unlock
(L
.L
'Access);
715 pragma Assert
(Result
= 0);
719 procedure Unlock
(T
: Task_ID
) is
720 Result
: Interfaces
.C
.int
;
722 if not Single_Lock
then
723 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
724 Result
:= mutex_unlock
(T
.Common
.LL
.L
.L
'Access);
725 pragma Assert
(Result
= 0);
729 -- For the time delay implementation, we need to make sure we
730 -- achieve following criteria:
732 -- 1) We have to delay at least for the amount requested.
733 -- 2) We have to give up CPU even though the actual delay does not
734 -- result in blocking.
735 -- 3) Except for restricted run-time systems that do not support
736 -- ATC or task abort, the delay must be interrupted by the
737 -- abort_task operation.
738 -- 4) The implementation has to be efficient so that the delay overhead
739 -- is relatively cheap.
740 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
741 -- requirement we still want to provide the effect in all cases.
742 -- The reason is that users may want to use short delays to implement
743 -- their own scheduling effect in the absence of language provided
744 -- scheduling policies.
746 ---------------------
747 -- Monotonic_Clock --
748 ---------------------
750 function Monotonic_Clock
return Duration is
751 TS
: aliased timespec
;
752 Result
: Interfaces
.C
.int
;
755 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
756 pragma Assert
(Result
= 0);
757 return To_Duration
(TS
);
764 function RT_Resolution
return Duration is
773 procedure Yield
(Do_Yield
: Boolean := True) is
776 System
.OS_Interface
.thr_yield
;
784 procedure Set_Priority
786 Prio
: System
.Any_Priority
;
787 Loss_Of_Inheritance
: Boolean := False)
789 Result
: Interfaces
.C
.int
;
790 Param
: aliased struct_pcparms
;
795 T
.Common
.Current_Priority
:= Prio
;
797 if Priority_Ceiling_Emulation
then
798 T
.Common
.LL
.Active_Priority
:= Prio
;
801 if Using_Real_Time_Class
then
802 Param
.pc_cid
:= Prio_Param
.pc_cid
;
803 Param
.rt_pri
:= pri_t
(Prio
);
804 Param
.rt_tqsecs
:= Prio_Param
.rt_tqsecs
;
805 Param
.rt_tqnsecs
:= Prio_Param
.rt_tqnsecs
;
807 Result
:= Interfaces
.C
.int
(
808 priocntl
(PC_VERSION
, P_LWPID
, T
.Common
.LL
.LWP
, PC_SETPARMS
,
812 if T
.Common
.Task_Info
/= null
813 and then not T
.Common
.Task_Info
.Bound_To_LWP
815 -- The task is not bound to a LWP, so use thr_setprio
818 thr_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
822 -- The task is bound to a LWP, use priocntl
834 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
836 return T
.Common
.Current_Priority
;
843 procedure Enter_Task
(Self_ID
: Task_ID
) is
844 Result
: Interfaces
.C
.int
;
845 Proc
: processorid_t
; -- User processor #
846 Last_Proc
: processorid_t
; -- Last processor #
848 use System
.Task_Info
;
850 Self_ID
.Common
.LL
.Thread
:= thr_self
;
852 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
854 if Self_ID
.Common
.Task_Info
/= null then
855 if Self_ID
.Common
.Task_Info
.New_LWP
856 and then Self_ID
.Common
.Task_Info
.CPU
/= CPU_UNCHANGED
858 Last_Proc
:= Num_Procs
- 1;
860 if Self_ID
.Common
.Task_Info
.CPU
= ANY_CPU
then
864 while Proc
< Last_Proc
loop
865 Result
:= p_online
(Proc
, PR_STATUS
);
866 exit when Result
= PR_ONLINE
;
870 Result
:= processor_bind
(P_LWPID
, P_MYID
, Proc
, null);
871 pragma Assert
(Result
= 0);
874 -- Use specified processor
876 if Self_ID
.Common
.Task_Info
.CPU
< 0
877 or else Self_ID
.Common
.Task_Info
.CPU
> Last_Proc
879 raise Invalid_CPU_Number
;
882 Result
:= processor_bind
883 (P_LWPID
, P_MYID
, Self_ID
.Common
.Task_Info
.CPU
, null);
884 pragma Assert
(Result
= 0);
889 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Self_ID
));
890 pragma Assert
(Result
= 0);
892 -- We need the above code even if we do direct fetch of Task_ID in Self
893 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
897 for J
in Known_Tasks
'Range loop
898 if Known_Tasks
(J
) = null then
899 Known_Tasks
(J
) := Self_ID
;
900 Self_ID
.Known_Tasks_Index
:= J
;
912 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
914 return new Ada_Task_Control_Block
(Entry_Num
);
921 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
922 Result
: Interfaces
.C
.int
:= 0;
924 -- Give the task a unique serial number.
926 Self_ID
.Serial_Number
:= Next_Serial_Number
;
927 Next_Serial_Number
:= Next_Serial_Number
+ 1;
928 pragma Assert
(Next_Serial_Number
/= 0);
930 Self_ID
.Common
.LL
.Thread
:= To_thread_t
(-1);
932 if not Single_Lock
then
934 (Self_ID
.Common
.LL
.L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
935 Self_ID
.Common
.LL
.L
.Level
:=
936 Private_Task_Serial_Number
(Self_ID
.Serial_Number
);
937 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
941 Result
:= cond_init
(Self_ID
.Common
.LL
.CV
'Access, USYNC_THREAD
, 0);
942 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
948 if not Single_Lock
then
949 Result
:= mutex_destroy
(Self_ID
.Common
.LL
.L
.L
'Access);
950 pragma Assert
(Result
= 0);
961 procedure Create_Task
963 Wrapper
: System
.Address
;
964 Stack_Size
: System
.Parameters
.Size_Type
;
965 Priority
: System
.Any_Priority
;
966 Succeeded
: out Boolean)
968 Result
: Interfaces
.C
.int
;
969 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
970 Opts
: Interfaces
.C
.int
:= THR_DETACHED
;
972 Page_Size
: constant System
.Parameters
.Size_Type
:= 4096;
973 -- This constant is for reserving extra space at the
974 -- end of the stack, which can be used by the stack
975 -- checking as guard page. The idea is that we need
976 -- to have at least Stack_Size bytes available for
979 use System
.Task_Info
;
981 if Stack_Size
= System
.Parameters
.Unspecified_Size
then
982 Adjusted_Stack_Size
:=
983 Interfaces
.C
.size_t
(Default_Stack_Size
+ Page_Size
);
985 elsif Stack_Size
< Minimum_Stack_Size
then
986 Adjusted_Stack_Size
:=
987 Interfaces
.C
.size_t
(Minimum_Stack_Size
+ Page_Size
);
990 Adjusted_Stack_Size
:=
991 Interfaces
.C
.size_t
(Stack_Size
+ Page_Size
);
994 -- Since the initial signal mask of a thread is inherited from the
995 -- creator, and the Environment task has all its signals masked, we
996 -- do not need to manipulate caller's signal mask at this point.
997 -- All tasks in RTS will have All_Tasks_Mask initially.
999 if T
.Common
.Task_Info
/= null then
1001 if T
.Common
.Task_Info
.New_LWP
then
1002 Opts
:= Opts
+ THR_NEW_LWP
;
1005 if T
.Common
.Task_Info
.Bound_To_LWP
then
1006 Opts
:= Opts
+ THR_BOUND
;
1010 Opts
:= THR_DETACHED
+ THR_BOUND
;
1013 Result
:= thr_create
1014 (System
.Null_Address
,
1015 Adjusted_Stack_Size
,
1016 Thread_Body_Access
(Wrapper
),
1019 T
.Common
.LL
.Thread
'Access);
1021 Succeeded
:= Result
= 0;
1024 or else Result
= ENOMEM
1025 or else Result
= EAGAIN
);
1032 procedure Finalize_TCB
(T
: Task_ID
) is
1033 Result
: Interfaces
.C
.int
;
1036 procedure Free
is new
1037 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
1040 T
.Common
.LL
.Thread
:= To_thread_t
(0);
1042 if not Single_Lock
then
1043 Result
:= mutex_destroy
(T
.Common
.LL
.L
.L
'Access);
1044 pragma Assert
(Result
= 0);
1047 Result
:= cond_destroy
(T
.Common
.LL
.CV
'Access);
1048 pragma Assert
(Result
= 0);
1050 if T
.Known_Tasks_Index
/= -1 then
1051 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1061 -- This procedure must be called with abort deferred.
1062 -- It can no longer call Self or access
1063 -- the current task's ATCB, since the ATCB has been deallocated.
1065 procedure Exit_Task
is
1067 thr_exit
(System
.Null_Address
);
1074 procedure Abort_Task
(T
: Task_ID
) is
1075 Result
: Interfaces
.C
.int
;
1077 pragma Assert
(T
/= Self
);
1079 Result
:= thr_kill
(T
.Common
.LL
.Thread
,
1080 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1083 pragma Assert
(Result
= 0);
1092 Reason
: Task_States
)
1094 Result
: Interfaces
.C
.int
;
1096 pragma Assert
(Check_Sleep
(Reason
));
1098 if Dynamic_Priority_Support
1099 and then Self_ID
.Pending_Priority_Change
1101 Self_ID
.Pending_Priority_Change
:= False;
1102 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1103 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1108 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
.L
'Access);
1111 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
.L
'Access);
1114 pragma Assert
(Record_Wakeup
1115 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1116 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1119 -- Note that we are relying heaviliy here on the GNAT feature
1120 -- that Calendar.Time, System.Real_Time.Time, Duration, and
1121 -- System.Real_Time.Time_Span are all represented in the same
1122 -- way, i.e., as a 64-bit count of nanoseconds.
1124 -- This allows us to always pass the timeout value as a Duration.
1127 -- We are taking liberties here with the semantics of the delays.
1128 -- That is, we make no distinction between delays on the Calendar clock
1129 -- and delays on the Real_Time clock. That is technically incorrect, if
1130 -- the Calendar clock happens to be reset or adjusted.
1131 -- To solve this defect will require modification to the compiler
1132 -- interface, so that it can pass through more information, to tell
1133 -- us here which clock to use!
1135 -- cond_timedwait will return if any of the following happens:
1136 -- 1) some other task did cond_signal on this condition variable
1137 -- In this case, the return value is 0
1138 -- 2) the call just returned, for no good reason
1139 -- This is called a "spurious wakeup".
1140 -- In this case, the return value may also be 0.
1141 -- 3) the time delay expires
1142 -- In this case, the return value is ETIME
1143 -- 4) this task received a signal, which was handled by some
1144 -- handler procedure, and now the thread is resuming execution
1145 -- UNIX calls this an "interrupted" system call.
1146 -- In this case, the return value is EINTR
1148 -- If the cond_timedwait returns 0 or EINTR, it is still
1149 -- possible that the time has actually expired, and by chance
1150 -- a signal or cond_signal occurred at around the same time.
1152 -- We have also observed that on some OS's the value ETIME
1153 -- will be returned, but the clock will show that the full delay
1154 -- has not yet expired.
1156 -- For these reasons, we need to check the clock after return
1157 -- from cond_timedwait. If the time has expired, we will set
1160 -- This check might be omitted for systems on which the
1161 -- cond_timedwait() never returns early or wakes up spuriously.
1163 -- Annex D requires that completion of a delay cause the task
1164 -- to go to the end of its priority queue, regardless of whether
1165 -- the task actually was suspended by the delay. Since
1166 -- cond_timedwait does not do this on Solaris, we add a call
1167 -- to thr_yield at the end. We might do this at the beginning,
1168 -- instead, but then the round-robin effect would not be the
1169 -- same; the delayed task would be ahead of other tasks of the
1170 -- same priority that awoke while it was sleeping.
1172 -- For Timed_Sleep, we are expecting possible cond_signals
1173 -- to indicate other events (e.g., completion of a RV or
1174 -- completion of the abortable part of an async. select),
1175 -- we want to always return if interrupted. The caller will
1176 -- be responsible for checking the task state to see whether
1177 -- the wakeup was spurious, and to go back to sleep again
1178 -- in that case. We don't need to check for pending abort
1179 -- or priority change on the way in our out; that is the
1180 -- caller's responsibility.
1182 -- For Timed_Delay, we are not expecting any cond_signals or
1183 -- other interruptions, except for priority changes and aborts.
1184 -- Therefore, we don't want to return unless the delay has
1185 -- actually expired, or the call has been aborted. In this
1186 -- case, since we want to implement the entire delay statement
1187 -- semantics, we do need to check for pending abort and priority
1188 -- changes. We can quietly handle priority changes inside the
1189 -- procedure, since there is no entry-queue reordering involved.
1195 procedure Timed_Sleep
1198 Mode
: ST
.Delay_Modes
;
1199 Reason
: System
.Tasking
.Task_States
;
1200 Timedout
: out Boolean;
1201 Yielded
: out Boolean)
1203 Check_Time
: constant Duration := Monotonic_Clock
;
1204 Abs_Time
: Duration;
1205 Request
: aliased timespec
;
1206 Result
: Interfaces
.C
.int
;
1209 pragma Assert
(Check_Sleep
(Reason
));
1213 if Mode
= Relative
then
1214 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
1216 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1219 if Abs_Time
> Check_Time
then
1220 Request
:= To_Timespec
(Abs_Time
);
1223 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
1224 or else (Dynamic_Priority_Support
and then
1225 Self_ID
.Pending_Priority_Change
);
1228 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1229 Single_RTS_Lock
.L
'Access, Request
'Access);
1231 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1232 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1237 exit when Abs_Time
<= Monotonic_Clock
;
1239 if Result
= 0 or Result
= EINTR
then
1240 -- somebody may have called Wakeup for us
1245 pragma Assert
(Result
= ETIME
);
1249 pragma Assert
(Record_Wakeup
1250 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1257 procedure Timed_Delay
1260 Mode
: ST
.Delay_Modes
)
1262 Check_Time
: constant Duration := Monotonic_Clock
;
1263 Abs_Time
: Duration;
1264 Request
: aliased timespec
;
1265 Result
: Interfaces
.C
.int
;
1266 Yielded
: Boolean := False;
1269 -- Only the little window between deferring abort and
1270 -- locking Self_ID is the reason we need to
1271 -- check for pending abort and priority change below!
1273 SSL
.Abort_Defer
.all;
1279 Write_Lock
(Self_ID
);
1281 if Mode
= Relative
then
1282 Abs_Time
:= Time
+ Check_Time
;
1284 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
1287 if Abs_Time
> Check_Time
then
1288 Request
:= To_Timespec
(Abs_Time
);
1289 Self_ID
.Common
.State
:= Delay_Sleep
;
1291 pragma Assert
(Check_Sleep
(Delay_Sleep
));
1294 if Dynamic_Priority_Support
and then
1295 Self_ID
.Pending_Priority_Change
then
1296 Self_ID
.Pending_Priority_Change
:= False;
1297 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
1298 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
1301 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
1304 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1305 Single_RTS_Lock
.L
'Access, Request
'Access);
1307 Result
:= cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
1308 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1313 exit when Abs_Time
<= Monotonic_Clock
;
1315 pragma Assert
(Result
= 0 or else
1316 Result
= ETIME
or else
1320 pragma Assert
(Record_Wakeup
1321 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Delay_Sleep
));
1323 Self_ID
.Common
.State
:= Runnable
;
1336 SSL
.Abort_Undefer
.all;
1345 Reason
: Task_States
)
1347 Result
: Interfaces
.C
.int
;
1349 pragma Assert
(Check_Wakeup
(T
, Reason
));
1350 Result
:= cond_signal
(T
.Common
.LL
.CV
'Access);
1351 pragma Assert
(Result
= 0);
1354 ---------------------------
1355 -- Check_Initialize_Lock --
1356 ---------------------------
1358 -- The following code is intended to check some of the invariant
1359 -- assertions related to lock usage, on which we depend.
1361 function Check_Initialize_Lock
1366 Self_ID
: constant Task_ID
:= Self
;
1369 -- Check that caller is abort-deferred
1371 if Self_ID
.Deferral_Level
<= 0 then
1375 -- Check that the lock is not yet initialized
1377 if L
.Level
/= 0 then
1381 L
.Level
:= Lock_Level
'Pos (Level
) + 1;
1383 end Check_Initialize_Lock
;
1389 function Check_Lock
(L
: Lock_Ptr
) return Boolean is
1390 Self_ID
: Task_ID
:= Self
;
1394 -- Check that the argument is not null
1400 -- Check that L is not frozen
1406 -- Check that caller is abort-deferred
1408 if Self_ID
.Deferral_Level
<= 0 then
1412 -- Check that caller is not holding this lock already
1414 if L
.Owner
= To_Owner_ID
(Self_ID
) then
1422 -- Check that TCB lock order rules are satisfied
1424 P
:= Self_ID
.Common
.LL
.Locks
;
1426 if P
.Level
>= L
.Level
1427 and then (P
.Level
> 2 or else L
.Level
> 2)
1440 function Record_Lock
(L
: Lock_Ptr
) return Boolean is
1441 Self_ID
: Task_ID
:= Self
;
1445 Lock_Count
:= Lock_Count
+ 1;
1447 -- There should be no owner for this lock at this point
1449 if L
.Owner
/= null then
1455 L
.Owner
:= To_Owner_ID
(Self_ID
);
1461 -- Check that TCB lock order rules are satisfied
1463 P
:= Self_ID
.Common
.LL
.Locks
;
1469 Self_ID
.Common
.LL
.Locking
:= null;
1470 Self_ID
.Common
.LL
.Locks
:= L
;
1478 function Check_Sleep
(Reason
: Task_States
) return Boolean is
1479 Self_ID
: Task_ID
:= Self
;
1483 -- Check that caller is abort-deferred
1485 if Self_ID
.Deferral_Level
<= 0 then
1493 -- Check that caller is holding own lock, on top of list
1495 if Self_ID
.Common
.LL
.Locks
/=
1496 To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access)
1501 -- Check that TCB lock order rules are satisfied
1503 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1507 Self_ID
.Common
.LL
.L
.Owner
:= null;
1508 P
:= Self_ID
.Common
.LL
.Locks
;
1509 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1518 function Record_Wakeup
1520 Reason
: Task_States
)
1523 Self_ID
: Task_ID
:= Self
;
1529 L
.Owner
:= To_Owner_ID
(Self_ID
);
1535 -- Check that TCB lock order rules are satisfied
1537 P
:= Self_ID
.Common
.LL
.Locks
;
1543 Self_ID
.Common
.LL
.Locking
:= null;
1544 Self_ID
.Common
.LL
.Locks
:= L
;
1552 function Check_Wakeup
1554 Reason
: Task_States
)
1557 Self_ID
: Task_ID
:= Self
;
1560 -- Is caller holding T's lock?
1562 if T
.Common
.LL
.L
.Owner
/= To_Owner_ID
(Self_ID
) then
1566 -- Are reasons for wakeup and sleep consistent?
1568 if T
.Common
.State
/= Reason
then
1579 function Check_Unlock
(L
: Lock_Ptr
) return Boolean is
1580 Self_ID
: Task_ID
:= Self
;
1584 Unlock_Count
:= Unlock_Count
+ 1;
1590 if L
.Buddy
/= null then
1595 Check_Count
:= Unlock_Count
;
1598 if Unlock_Count
- Check_Count
> 1000 then
1599 Check_Count
:= Unlock_Count
;
1600 Old_Owner
:= To_Task_ID
(Single_RTS_Lock
.Owner
);
1603 -- Check that caller is abort-deferred
1605 if Self_ID
.Deferral_Level
<= 0 then
1609 -- Check that caller is holding this lock, on top of list
1611 if Self_ID
.Common
.LL
.Locks
/= L
then
1615 -- Record there is no owner now
1618 P
:= Self_ID
.Common
.LL
.Locks
;
1619 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1624 --------------------
1625 -- Check_Finalize --
1626 --------------------
1628 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean is
1629 Self_ID
: Task_ID
:= Self
;
1631 -- Check that caller is abort-deferred
1633 if Self_ID
.Deferral_Level
<= 0 then
1637 -- Check that no one is holding this lock
1639 if L
.Owner
/= null then
1645 end Check_Finalize_Lock
;
1651 function Check_Exit
(Self_ID
: Task_ID
) return Boolean is
1653 -- Check that caller is just holding Global_Task_Lock
1654 -- and no other locks
1656 if Self_ID
.Common
.LL
.Locks
= null then
1660 -- 2 = Global_Task_Level
1662 if Self_ID
.Common
.LL
.Locks
.Level
/= 2 then
1666 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1670 -- Check that caller is abort-deferred
1672 if Self_ID
.Deferral_Level
<= 0 then
1679 --------------------
1680 -- Check_No_Locks --
1681 --------------------
1683 function Check_No_Locks
(Self_ID
: Task_ID
) return Boolean is
1685 return Self_ID
.Common
.LL
.Locks
= null;
1688 ----------------------
1689 -- Environment_Task --
1690 ----------------------
1692 function Environment_Task
return Task_ID
is
1694 return Environment_Task_ID
;
1695 end Environment_Task
;
1701 procedure Lock_RTS
is
1703 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1710 procedure Unlock_RTS
is
1712 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1719 function Suspend_Task
1721 Thread_Self
: Thread_Id
) return Boolean is
1723 if T
.Common
.LL
.Thread
/= Thread_Self
then
1724 return thr_suspend
(T
.Common
.LL
.Thread
) = 0;
1734 function Resume_Task
1736 Thread_Self
: Thread_Id
) return Boolean is
1738 if T
.Common
.LL
.Thread
/= Thread_Self
then
1739 return thr_continue
(T
.Common
.LL
.Thread
) = 0;
1749 procedure Initialize
(Environment_Task
: ST
.Task_ID
) is
1750 act
: aliased struct_sigaction
;
1751 old_act
: aliased struct_sigaction
;
1752 Tmp_Set
: aliased sigset_t
;
1753 Result
: Interfaces
.C
.int
;
1755 procedure Configure_Processors
;
1756 -- Processors configuration
1757 -- The user can specify a processor which the program should run
1758 -- on to emulate a single-processor system. This can be easily
1759 -- done by setting environment variable GNAT_PROCESSOR to one of
1762 -- -2 : use the default configuration (run the program on all
1763 -- available processors) - this is the same as having
1764 -- GNAT_PROCESSOR unset
1765 -- -1 : let the RTS choose one processor and run the program on
1767 -- 0 .. Last_Proc : run the program on the specified processor
1769 -- Last_Proc is equal to the value of the system variable
1770 -- _SC_NPROCESSORS_CONF, minus one.
1772 procedure Configure_Processors
is
1773 Proc_Acc
: constant GNAT
.OS_Lib
.String_Access
:=
1774 GNAT
.OS_Lib
.Getenv
("GNAT_PROCESSOR");
1775 Proc
: aliased processorid_t
; -- User processor #
1776 Last_Proc
: processorid_t
; -- Last processor #
1779 if Proc_Acc
.all'Length /= 0 then
1780 -- Environment variable is defined
1782 Last_Proc
:= Num_Procs
- 1;
1784 if Last_Proc
/= -1 then
1785 Proc
:= processorid_t
'Value (Proc_Acc
.all);
1787 if Proc
<= -2 or else Proc
> Last_Proc
then
1788 -- Use the default configuration
1790 elsif Proc
= -1 then
1791 -- Choose a processor
1795 while Proc
< Last_Proc
loop
1797 Result
:= p_online
(Proc
, PR_STATUS
);
1798 exit when Result
= PR_ONLINE
;
1801 pragma Assert
(Result
= PR_ONLINE
);
1802 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
1803 pragma Assert
(Result
= 0);
1806 -- Use user processor
1808 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
1809 pragma Assert
(Result
= 0);
1815 when Constraint_Error
=>
1816 -- Illegal environment variable GNAT_PROCESSOR - ignored
1818 end Configure_Processors
;
1820 -- Start of processing for Initialize
1823 Environment_Task_ID
:= Environment_Task
;
1825 -- This is done in Enter_Task, but this is too late for the
1826 -- Environment Task, since we need to call Self in Check_Locks when
1827 -- the run time is compiled with assertions on.
1829 Result
:= thr_setspecific
(ATCB_Key
, To_Address
(Environment_Task
));
1830 pragma Assert
(Result
= 0);
1832 -- Initialize the lock used to synchronize chain of all ATCBs.
1834 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1836 Enter_Task
(Environment_Task
);
1838 -- Install the abort-signal handler
1840 -- Set sa_flags to SA_NODEFER so that during the handler execution
1841 -- we do not change the Signal_Mask to be masked for the Abort_Signal.
1842 -- This is a temporary fix to the problem that the Signal_Mask is
1843 -- not restored after the exception (longjmp) from the handler.
1844 -- The right fix should be made in sigsetjmp so that we save
1845 -- the Signal_Set and restore it after a longjmp.
1846 -- In that case, this field should be changed back to 0. ???
1850 act
.sa_handler
:= Abort_Handler
'Address;
1851 Result
:= sigemptyset
(Tmp_Set
'Access);
1852 pragma Assert
(Result
= 0);
1853 act
.sa_mask
:= Tmp_Set
;
1857 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1858 act
'Unchecked_Access,
1859 old_act
'Unchecked_Access);
1860 pragma Assert
(Result
= 0);
1862 Configure_Processors
;
1864 -- Create a free ATCB for use on the Fake_ATCB_List.
1866 Next_Fake_ATCB
:= new Fake_ATCB
;
1869 -- Package elaboration
1873 Result
: Interfaces
.C
.int
;
1875 -- Mask Environment task for all signals. The original mask of the
1876 -- Environment task will be recovered by Interrupt_Server task
1877 -- during the elaboration of s-interr.adb.
1879 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1880 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1882 -- Prepare the set of signals that should unblocked in all tasks
1884 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1885 pragma Assert
(Result
= 0);
1887 for J
in Interrupt_Management
.Interrupt_ID
loop
1888 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1889 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1890 pragma Assert
(Result
= 0);
1894 -- We need the following code to support automatic creation of fake
1895 -- ATCB's for C threads that call the Ada run-time system, even if
1896 -- we use a faster way of getting Self for real Ada tasks.
1898 Result
:= thr_keycreate
(ATCB_Key
'Access, System
.Null_Address
);
1899 pragma Assert
(Result
= 0);
1902 if Dispatching_Policy
= 'F' then
1904 Result
: Interfaces
.C
.long
;
1905 Class_Info
: aliased struct_pcinfo
;
1906 Secs
, Nsecs
: Interfaces
.C
.long
;
1909 -- If a pragma Time_Slice is specified, takes the value in account.
1911 if Time_Slice_Val
> 0 then
1912 -- Convert Time_Slice_Val (microseconds) into seconds and
1915 Secs
:= Time_Slice_Val
/ 1_000_000
;
1916 Nsecs
:= (Time_Slice_Val
rem 1_000_000
) * 1_000
;
1918 -- Otherwise, default to no time slicing (i.e run until blocked)
1925 -- Get the real time class id.
1927 Class_Info
.pc_clname
(1) := 'R';
1928 Class_Info
.pc_clname
(2) := 'T';
1929 Class_Info
.pc_clname
(3) := ASCII
.NUL
;
1931 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_GETCID
,
1932 Class_Info
'Address);
1934 -- Request the real time class
1936 Prio_Param
.pc_cid
:= Class_Info
.pc_cid
;
1937 Prio_Param
.rt_pri
:= pri_t
(Class_Info
.rt_maxpri
);
1938 Prio_Param
.rt_tqsecs
:= Secs
;
1939 Prio_Param
.rt_tqnsecs
:= Nsecs
;
1941 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_SETPARMS
,
1942 Prio_Param
'Address);
1944 Using_Real_Time_Class
:= Result
/= -1;
1947 end System
.Task_Primitives
.Operations
;