* MAINTAINERS: (Write After Approval): Add myself.
[official-gcc.git] / gcc / ada / 5staprop.adb
blob63501a3e9cc279fd5b846f648b874d81b9f3eb43
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
11 -- --
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. --
22 -- --
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. --
29 -- --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
32 -- --
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.
40 pragma Polling (Off);
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
47 with Ada.Exceptions;
48 -- used for Raise_Exception
50 with GNAT.OS_Lib;
51 -- used for String_Access, Getenv
53 with Interfaces.C;
54 -- used for int
55 -- size_t
57 with System.Interrupt_Management;
58 -- used for Keep_Unmasked
59 -- Abort_Task_Interrupt
60 -- Interrupt_ID
62 with System.Interrupt_Management.Operations;
63 -- used for Set_Interrupt_Mask
64 -- All_Tasks_Mask
65 pragma Elaborate_All (System.Interrupt_Management.Operations);
67 with System.Parameters;
68 -- used for Size_Type
70 with System.Tasking;
71 -- used for Ada_Task_Control_Block
72 -- Task_ID
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;
96 use System.Tasking;
97 use Interfaces.C;
98 use System.OS_Interface;
99 use System.Parameters;
100 use Ada.Exceptions;
101 use System.OS_Primitives;
103 package SSL renames System.Soft_Links;
105 ------------------
106 -- Local Data --
107 ------------------
109 -- The following are logically constants, but need to be initialized
110 -- at run time.
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
155 -- elaboration.
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.
178 type Fake_ATCB;
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);
185 end record;
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
199 ------------
200 -- Checks --
201 ------------
203 Check_Count : Integer := 0;
204 Old_Owner : Task_ID;
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
229 (Sig : Signal;
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
249 -- not use it.
251 ------------
252 -- Checks --
253 ------------
255 function Check_Initialize_Lock (L : Lock_Ptr; Level : Lock_Level)
256 return Boolean;
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
269 (L : Lock_Ptr;
270 Reason : Task_States) return Boolean;
271 pragma Inline (Record_Wakeup);
273 function Check_Wakeup
274 (T : Task_ID;
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);
284 -------------------
285 -- New_Fake_ATCB --
286 -------------------
288 function New_Fake_ATCB (Stack_Base : Interfaces.C.unsigned)
289 return Task_ID
291 Self_ID : Task_ID;
292 P, Q : Fake_ATCB_Ptr;
293 Succeeded : Boolean;
294 Result : Interfaces.C.int;
296 begin
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);
304 Q := null;
305 P := Fake_ATCB_List;
307 while P /= null loop
308 if P.Stack_Base = 0 then
309 Q := P;
310 elsif thr_kill (P.Real_ATCB.Common.LL.Thread, 0) /= 0 then
311 -- ????
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.
315 P.Stack_Base := 0;
316 Q := P;
317 end if;
319 P := P.Next;
320 end loop;
322 if Q = null then
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;
332 else
334 -- Reuse an existing fake ATCB.
336 Self_ID := Q.Real_ATCB'Access;
337 Q.Stack_Base := Stack_Base;
338 end if;
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,
345 Succeeded);
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;
358 end if;
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;
366 end loop;
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);
383 -- ????
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
392 -- stable.
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;
402 exit;
403 end if;
404 end loop;
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.
411 return Self_ID;
412 end New_Fake_ATCB;
414 -------------------
415 -- Abort_Handler --
416 -------------------
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
447 -- (in the RTS).
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
453 -- begin
454 -- raise Standard'Abort_Signal;
455 -- end if;
457 -- ???
458 -- The comments above need revising. They are partly obsolete.
460 procedure Abort_Handler
461 (Sig : Signal;
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;
469 begin
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
476 then
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;
490 -- ?????
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
494 -- async. signals.
495 end if;
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;
503 -- return;
504 -- end if;
506 end Abort_Handler;
508 -------------------
509 -- Stack_Guard --
510 -------------------
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
516 begin
517 null;
518 end Stack_Guard;
520 --------------------
521 -- Get_Thread_Id --
522 --------------------
524 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
525 begin
526 return T.Common.LL.Thread;
527 end Get_Thread_Id;
529 -----------
530 -- Self --
531 -----------
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;
548 L : access Lock)
550 Result : Interfaces.C.int;
552 begin
553 pragma Assert (Check_Initialize_Lock (Lock_Ptr (L), PO_Level));
555 if Priority_Ceiling_Emulation then
556 L.Ceiling := Prio;
557 end if;
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");
564 end if;
565 end Initialize_Lock;
567 procedure Initialize_Lock
568 (L : access RTS_Lock;
569 Level : Lock_Level)
571 Result : Interfaces.C.int;
573 begin
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");
581 end if;
582 end Initialize_Lock;
584 -------------------
585 -- Finalize_Lock --
586 -------------------
588 procedure Finalize_Lock (L : access Lock) is
589 Result : Interfaces.C.int;
591 begin
592 pragma Assert (Check_Finalize_Lock (Lock_Ptr (L)));
593 Result := mutex_destroy (L.L'Access);
594 pragma Assert (Result = 0);
595 end Finalize_Lock;
597 procedure Finalize_Lock (L : access RTS_Lock) is
598 Result : Interfaces.C.int;
600 begin
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);
604 end Finalize_Lock;
606 ----------------
607 -- Write_Lock --
608 ----------------
610 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
611 Result : Interfaces.C.int;
613 begin
614 pragma Assert (Check_Lock (Lock_Ptr (L)));
616 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
617 declare
618 Self_Id : constant Task_ID := Self;
619 Saved_Priority : System.Any_Priority;
621 begin
622 if Self_Id.Common.LL.Active_Priority > L.Ceiling then
623 Ceiling_Violation := True;
624 return;
625 end if;
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);
631 end if;
633 Result := mutex_lock (L.L'Access);
634 pragma Assert (Result = 0);
635 Ceiling_Violation := False;
637 L.Saved_Priority := Saved_Priority;
638 end;
640 else
641 Result := mutex_lock (L.L'Access);
642 pragma Assert (Result = 0);
643 Ceiling_Violation := False;
644 end if;
646 pragma Assert (Record_Lock (Lock_Ptr (L)));
647 end Write_Lock;
649 procedure Write_Lock
650 (L : access RTS_Lock; Global_Lock : Boolean := False)
652 Result : Interfaces.C.int;
653 begin
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))));
659 end if;
660 end Write_Lock;
662 procedure Write_Lock (T : Task_ID) is
663 Result : Interfaces.C.int;
664 begin
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)));
670 end if;
671 end Write_Lock;
673 ---------------
674 -- Read_Lock --
675 ---------------
677 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
678 begin
679 Write_Lock (L, Ceiling_Violation);
680 end Read_Lock;
682 ------------
683 -- Unlock --
684 ------------
686 procedure Unlock (L : access Lock) is
687 Result : Interfaces.C.int;
688 begin
689 pragma Assert (Check_Unlock (Lock_Ptr (L)));
691 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
692 declare
693 Self_Id : constant Task_ID := Self;
695 begin
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);
701 end if;
702 end;
703 else
704 Result := mutex_unlock (L.L'Access);
705 pragma Assert (Result = 0);
706 end if;
707 end Unlock;
709 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
710 Result : Interfaces.C.int;
711 begin
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);
716 end if;
717 end Unlock;
719 procedure Unlock (T : Task_ID) is
720 Result : Interfaces.C.int;
721 begin
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);
726 end if;
727 end Unlock;
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;
754 begin
755 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
756 pragma Assert (Result = 0);
757 return To_Duration (TS);
758 end Monotonic_Clock;
760 -------------------
761 -- RT_Resolution --
762 -------------------
764 function RT_Resolution return Duration is
765 begin
766 return 10#1.0#E-6;
767 end RT_Resolution;
769 -----------
770 -- Yield --
771 -----------
773 procedure Yield (Do_Yield : Boolean := True) is
774 begin
775 if Do_Yield then
776 System.OS_Interface.thr_yield;
777 end if;
778 end Yield;
780 ------------------
781 -- Set_Priority --
782 ------------------
784 procedure Set_Priority
785 (T : Task_ID;
786 Prio : System.Any_Priority;
787 Loss_Of_Inheritance : Boolean := False)
789 Result : Interfaces.C.int;
790 Param : aliased struct_pcparms;
792 use Task_Info;
794 begin
795 T.Common.Current_Priority := Prio;
797 if Priority_Ceiling_Emulation then
798 T.Common.LL.Active_Priority := Prio;
799 end if;
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,
809 Param'Address));
811 else
812 if T.Common.Task_Info /= null
813 and then not T.Common.Task_Info.Bound_To_LWP
814 then
815 -- The task is not bound to a LWP, so use thr_setprio
817 Result :=
818 thr_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
820 else
822 -- The task is bound to a LWP, use priocntl
823 -- ??? TBD
825 null;
826 end if;
827 end if;
828 end Set_Priority;
830 ------------------
831 -- Get_Priority --
832 ------------------
834 function Get_Priority (T : Task_ID) return System.Any_Priority is
835 begin
836 return T.Common.Current_Priority;
837 end Get_Priority;
839 ----------------
840 -- Enter_Task --
841 ----------------
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;
849 begin
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
857 then
858 Last_Proc := Num_Procs - 1;
860 if Self_ID.Common.Task_Info.CPU = ANY_CPU then
861 Result := 0;
862 Proc := 0;
864 while Proc < Last_Proc loop
865 Result := p_online (Proc, PR_STATUS);
866 exit when Result = PR_ONLINE;
867 Proc := Proc + 1;
868 end loop;
870 Result := processor_bind (P_LWPID, P_MYID, Proc, null);
871 pragma Assert (Result = 0);
873 else
874 -- Use specified processor
876 if Self_ID.Common.Task_Info.CPU < 0
877 or else Self_ID.Common.Task_Info.CPU > Last_Proc
878 then
879 raise Invalid_CPU_Number;
880 end if;
882 Result := processor_bind
883 (P_LWPID, P_MYID, Self_ID.Common.Task_Info.CPU, null);
884 pragma Assert (Result = 0);
885 end if;
886 end if;
887 end if;
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.
895 Lock_RTS;
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;
901 exit;
902 end if;
903 end loop;
905 Unlock_RTS;
906 end Enter_Task;
908 --------------
909 -- New_ATCB --
910 --------------
912 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
913 begin
914 return new Ada_Task_Control_Block (Entry_Num);
915 end New_ATCB;
917 --------------------
918 -- Initialize_TCB --
919 --------------------
921 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
922 Result : Interfaces.C.int := 0;
923 begin
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
933 Result := mutex_init
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);
938 end if;
940 if Result = 0 then
941 Result := cond_init (Self_ID.Common.LL.CV'Access, USYNC_THREAD, 0);
942 pragma Assert (Result = 0 or else Result = ENOMEM);
943 end if;
945 if Result = 0 then
946 Succeeded := True;
947 else
948 if not Single_Lock then
949 Result := mutex_destroy (Self_ID.Common.LL.L.L'Access);
950 pragma Assert (Result = 0);
951 end if;
953 Succeeded := False;
954 end if;
955 end Initialize_TCB;
957 -----------------
958 -- Create_Task --
959 -----------------
961 procedure Create_Task
962 (T : Task_ID;
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
977 -- actual use.
979 use System.Task_Info;
980 begin
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);
989 else
990 Adjusted_Stack_Size :=
991 Interfaces.C.size_t (Stack_Size + Page_Size);
992 end if;
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;
1003 end if;
1005 if T.Common.Task_Info.Bound_To_LWP then
1006 Opts := Opts + THR_BOUND;
1007 end if;
1009 else
1010 Opts := THR_DETACHED + THR_BOUND;
1011 end if;
1013 Result := thr_create
1014 (System.Null_Address,
1015 Adjusted_Stack_Size,
1016 Thread_Body_Access (Wrapper),
1017 To_Address (T),
1018 Opts,
1019 T.Common.LL.Thread'Access);
1021 Succeeded := Result = 0;
1022 pragma Assert
1023 (Result = 0
1024 or else Result = ENOMEM
1025 or else Result = EAGAIN);
1026 end Create_Task;
1028 ------------------
1029 -- Finalize_TCB --
1030 ------------------
1032 procedure Finalize_TCB (T : Task_ID) is
1033 Result : Interfaces.C.int;
1034 Tmp : Task_ID := T;
1036 procedure Free is new
1037 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
1039 begin
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);
1045 end if;
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;
1052 end if;
1054 Free (Tmp);
1055 end Finalize_TCB;
1057 ---------------
1058 -- Exit_Task --
1059 ---------------
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
1066 begin
1067 thr_exit (System.Null_Address);
1068 end Exit_Task;
1070 ----------------
1071 -- Abort_Task --
1072 ----------------
1074 procedure Abort_Task (T : Task_ID) is
1075 Result : Interfaces.C.int;
1076 begin
1077 pragma Assert (T /= Self);
1079 Result := thr_kill (T.Common.LL.Thread,
1080 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1081 null;
1083 pragma Assert (Result = 0);
1084 end Abort_Task;
1086 -----------
1087 -- Sleep --
1088 -----------
1090 procedure Sleep
1091 (Self_ID : Task_ID;
1092 Reason : Task_States)
1094 Result : Interfaces.C.int;
1095 begin
1096 pragma Assert (Check_Sleep (Reason));
1098 if Dynamic_Priority_Support
1099 and then Self_ID.Pending_Priority_Change
1100 then
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);
1104 end if;
1106 if Single_Lock then
1107 Result := cond_wait
1108 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock.L'Access);
1109 else
1110 Result := cond_wait
1111 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L.L'Access);
1112 end if;
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);
1117 end Sleep;
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.
1126 -- ???
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
1158 -- Timedout = True.
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.
1191 -----------------
1192 -- Timed_Sleep --
1193 -----------------
1195 procedure Timed_Sleep
1196 (Self_ID : Task_ID;
1197 Time : Duration;
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;
1208 begin
1209 pragma Assert (Check_Sleep (Reason));
1210 Timedout := True;
1211 Yielded := False;
1213 if Mode = Relative then
1214 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
1215 else
1216 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1217 end if;
1219 if Abs_Time > Check_Time then
1220 Request := To_Timespec (Abs_Time);
1222 loop
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);
1227 if Single_Lock then
1228 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1229 Single_RTS_Lock.L'Access, Request'Access);
1230 else
1231 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1232 Self_ID.Common.LL.L.L'Access, Request'Access);
1233 end if;
1235 Yielded := True;
1237 exit when Abs_Time <= Monotonic_Clock;
1239 if Result = 0 or Result = EINTR then
1240 -- somebody may have called Wakeup for us
1241 Timedout := False;
1242 exit;
1243 end if;
1245 pragma Assert (Result = ETIME);
1246 end loop;
1247 end if;
1249 pragma Assert (Record_Wakeup
1250 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1251 end Timed_Sleep;
1253 -----------------
1254 -- Timed_Delay --
1255 -----------------
1257 procedure Timed_Delay
1258 (Self_ID : Task_ID;
1259 Time : Duration;
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;
1268 begin
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;
1275 if Single_Lock then
1276 Lock_RTS;
1277 end if;
1279 Write_Lock (Self_ID);
1281 if Mode = Relative then
1282 Abs_Time := Time + Check_Time;
1283 else
1284 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1285 end if;
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));
1293 loop
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);
1299 end if;
1301 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1303 if Single_Lock then
1304 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1305 Single_RTS_Lock.L'Access, Request'Access);
1306 else
1307 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1308 Self_ID.Common.LL.L.L'Access, Request'Access);
1309 end if;
1311 Yielded := True;
1313 exit when Abs_Time <= Monotonic_Clock;
1315 pragma Assert (Result = 0 or else
1316 Result = ETIME or else
1317 Result = EINTR);
1318 end loop;
1320 pragma Assert (Record_Wakeup
1321 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Delay_Sleep));
1323 Self_ID.Common.State := Runnable;
1324 end if;
1326 Unlock (Self_ID);
1328 if Single_Lock then
1329 Unlock_RTS;
1330 end if;
1332 if not Yielded then
1333 thr_yield;
1334 end if;
1336 SSL.Abort_Undefer.all;
1337 end Timed_Delay;
1339 ------------
1340 -- Wakeup --
1341 ------------
1343 procedure Wakeup
1344 (T : Task_ID;
1345 Reason : Task_States)
1347 Result : Interfaces.C.int;
1348 begin
1349 pragma Assert (Check_Wakeup (T, Reason));
1350 Result := cond_signal (T.Common.LL.CV'Access);
1351 pragma Assert (Result = 0);
1352 end Wakeup;
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
1362 (L : Lock_Ptr;
1363 Level : Lock_Level)
1364 return Boolean
1366 Self_ID : constant Task_ID := Self;
1368 begin
1369 -- Check that caller is abort-deferred
1371 if Self_ID.Deferral_Level <= 0 then
1372 return False;
1373 end if;
1375 -- Check that the lock is not yet initialized
1377 if L.Level /= 0 then
1378 return False;
1379 end if;
1381 L.Level := Lock_Level'Pos (Level) + 1;
1382 return True;
1383 end Check_Initialize_Lock;
1385 ----------------
1386 -- Check_Lock --
1387 ----------------
1389 function Check_Lock (L : Lock_Ptr) return Boolean is
1390 Self_ID : Task_ID := Self;
1391 P : Lock_Ptr;
1393 begin
1394 -- Check that the argument is not null
1396 if L = null then
1397 return False;
1398 end if;
1400 -- Check that L is not frozen
1402 if L.Frozen then
1403 return False;
1404 end if;
1406 -- Check that caller is abort-deferred
1408 if Self_ID.Deferral_Level <= 0 then
1409 return False;
1410 end if;
1412 -- Check that caller is not holding this lock already
1414 if L.Owner = To_Owner_ID (Self_ID) then
1415 return False;
1416 end if;
1418 if Single_Lock then
1419 return True;
1420 end if;
1422 -- Check that TCB lock order rules are satisfied
1424 P := Self_ID.Common.LL.Locks;
1425 if P /= null then
1426 if P.Level >= L.Level
1427 and then (P.Level > 2 or else L.Level > 2)
1428 then
1429 return False;
1430 end if;
1431 end if;
1433 return True;
1434 end Check_Lock;
1436 -----------------
1437 -- Record_Lock --
1438 -----------------
1440 function Record_Lock (L : Lock_Ptr) return Boolean is
1441 Self_ID : Task_ID := Self;
1442 P : Lock_Ptr;
1444 begin
1445 Lock_Count := Lock_Count + 1;
1447 -- There should be no owner for this lock at this point
1449 if L.Owner /= null then
1450 return False;
1451 end if;
1453 -- Record new owner
1455 L.Owner := To_Owner_ID (Self_ID);
1457 if Single_Lock then
1458 return True;
1459 end if;
1461 -- Check that TCB lock order rules are satisfied
1463 P := Self_ID.Common.LL.Locks;
1465 if P /= null then
1466 L.Next := P;
1467 end if;
1469 Self_ID.Common.LL.Locking := null;
1470 Self_ID.Common.LL.Locks := L;
1471 return True;
1472 end Record_Lock;
1474 -----------------
1475 -- Check_Sleep --
1476 -----------------
1478 function Check_Sleep (Reason : Task_States) return Boolean is
1479 Self_ID : Task_ID := Self;
1480 P : Lock_Ptr;
1482 begin
1483 -- Check that caller is abort-deferred
1485 if Self_ID.Deferral_Level <= 0 then
1486 return False;
1487 end if;
1489 if Single_Lock then
1490 return True;
1491 end if;
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)
1497 then
1498 return False;
1499 end if;
1501 -- Check that TCB lock order rules are satisfied
1503 if Self_ID.Common.LL.Locks.Next /= null then
1504 return False;
1505 end if;
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;
1510 P.Next := null;
1511 return True;
1512 end Check_Sleep;
1514 -------------------
1515 -- Record_Wakeup --
1516 -------------------
1518 function Record_Wakeup
1519 (L : Lock_Ptr;
1520 Reason : Task_States)
1521 return Boolean
1523 Self_ID : Task_ID := Self;
1524 P : Lock_Ptr;
1526 begin
1527 -- Record new owner
1529 L.Owner := To_Owner_ID (Self_ID);
1531 if Single_Lock then
1532 return True;
1533 end if;
1535 -- Check that TCB lock order rules are satisfied
1537 P := Self_ID.Common.LL.Locks;
1539 if P /= null then
1540 L.Next := P;
1541 end if;
1543 Self_ID.Common.LL.Locking := null;
1544 Self_ID.Common.LL.Locks := L;
1545 return True;
1546 end Record_Wakeup;
1548 ------------------
1549 -- Check_Wakeup --
1550 ------------------
1552 function Check_Wakeup
1553 (T : Task_ID;
1554 Reason : Task_States)
1555 return Boolean
1557 Self_ID : Task_ID := Self;
1559 begin
1560 -- Is caller holding T's lock?
1562 if T.Common.LL.L.Owner /= To_Owner_ID (Self_ID) then
1563 return False;
1564 end if;
1566 -- Are reasons for wakeup and sleep consistent?
1568 if T.Common.State /= Reason then
1569 return False;
1570 end if;
1572 return True;
1573 end Check_Wakeup;
1575 ------------------
1576 -- Check_Unlock --
1577 ------------------
1579 function Check_Unlock (L : Lock_Ptr) return Boolean is
1580 Self_ID : Task_ID := Self;
1581 P : Lock_Ptr;
1583 begin
1584 Unlock_Count := Unlock_Count + 1;
1586 if L = null then
1587 return False;
1588 end if;
1590 if L.Buddy /= null then
1591 return False;
1592 end if;
1594 if L.Level = 4 then
1595 Check_Count := Unlock_Count;
1596 end if;
1598 if Unlock_Count - Check_Count > 1000 then
1599 Check_Count := Unlock_Count;
1600 Old_Owner := To_Task_ID (Single_RTS_Lock.Owner);
1601 end if;
1603 -- Check that caller is abort-deferred
1605 if Self_ID.Deferral_Level <= 0 then
1606 return False;
1607 end if;
1609 -- Check that caller is holding this lock, on top of list
1611 if Self_ID.Common.LL.Locks /= L then
1612 return False;
1613 end if;
1615 -- Record there is no owner now
1617 L.Owner := null;
1618 P := Self_ID.Common.LL.Locks;
1619 Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
1620 P.Next := null;
1621 return True;
1622 end Check_Unlock;
1624 --------------------
1625 -- Check_Finalize --
1626 --------------------
1628 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean is
1629 Self_ID : Task_ID := Self;
1630 begin
1631 -- Check that caller is abort-deferred
1633 if Self_ID.Deferral_Level <= 0 then
1634 return False;
1635 end if;
1637 -- Check that no one is holding this lock
1639 if L.Owner /= null then
1640 return False;
1641 end if;
1643 L.Frozen := True;
1644 return True;
1645 end Check_Finalize_Lock;
1647 ----------------
1648 -- Check_Exit --
1649 ----------------
1651 function Check_Exit (Self_ID : Task_ID) return Boolean is
1652 begin
1653 -- Check that caller is just holding Global_Task_Lock
1654 -- and no other locks
1656 if Self_ID.Common.LL.Locks = null then
1657 return False;
1658 end if;
1660 -- 2 = Global_Task_Level
1662 if Self_ID.Common.LL.Locks.Level /= 2 then
1663 return False;
1664 end if;
1666 if Self_ID.Common.LL.Locks.Next /= null then
1667 return False;
1668 end if;
1670 -- Check that caller is abort-deferred
1672 if Self_ID.Deferral_Level <= 0 then
1673 return False;
1674 end if;
1676 return True;
1677 end Check_Exit;
1679 --------------------
1680 -- Check_No_Locks --
1681 --------------------
1683 function Check_No_Locks (Self_ID : Task_ID) return Boolean is
1684 begin
1685 return Self_ID.Common.LL.Locks = null;
1686 end Check_No_Locks;
1688 ----------------------
1689 -- Environment_Task --
1690 ----------------------
1692 function Environment_Task return Task_ID is
1693 begin
1694 return Environment_Task_ID;
1695 end Environment_Task;
1697 --------------
1698 -- Lock_RTS --
1699 --------------
1701 procedure Lock_RTS is
1702 begin
1703 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1704 end Lock_RTS;
1706 ----------------
1707 -- Unlock_RTS --
1708 ----------------
1710 procedure Unlock_RTS is
1711 begin
1712 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1713 end Unlock_RTS;
1715 ------------------
1716 -- Suspend_Task --
1717 ------------------
1719 function Suspend_Task
1720 (T : ST.Task_ID;
1721 Thread_Self : Thread_Id) return Boolean is
1722 begin
1723 if T.Common.LL.Thread /= Thread_Self then
1724 return thr_suspend (T.Common.LL.Thread) = 0;
1725 else
1726 return True;
1727 end if;
1728 end Suspend_Task;
1730 -----------------
1731 -- Resume_Task --
1732 -----------------
1734 function Resume_Task
1735 (T : ST.Task_ID;
1736 Thread_Self : Thread_Id) return Boolean is
1737 begin
1738 if T.Common.LL.Thread /= Thread_Self then
1739 return thr_continue (T.Common.LL.Thread) = 0;
1740 else
1741 return True;
1742 end if;
1743 end Resume_Task;
1745 ----------------
1746 -- Initialize --
1747 ----------------
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
1760 -- the following :
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
1766 -- that processor
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 #
1778 begin
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
1789 null;
1790 elsif Proc = -1 then
1791 -- Choose a processor
1793 Result := 0;
1795 while Proc < Last_Proc loop
1796 Proc := Proc + 1;
1797 Result := p_online (Proc, PR_STATUS);
1798 exit when Result = PR_ONLINE;
1799 end loop;
1801 pragma Assert (Result = PR_ONLINE);
1802 Result := processor_bind (P_PID, P_MYID, Proc, null);
1803 pragma Assert (Result = 0);
1805 else
1806 -- Use user processor
1808 Result := processor_bind (P_PID, P_MYID, Proc, null);
1809 pragma Assert (Result = 0);
1810 end if;
1811 end if;
1812 end if;
1814 exception
1815 when Constraint_Error =>
1816 -- Illegal environment variable GNAT_PROCESSOR - ignored
1817 null;
1818 end Configure_Processors;
1820 -- Start of processing for Initialize
1822 begin
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. ???
1848 act.sa_flags := 16;
1850 act.sa_handler := Abort_Handler'Address;
1851 Result := sigemptyset (Tmp_Set'Access);
1852 pragma Assert (Result = 0);
1853 act.sa_mask := Tmp_Set;
1855 Result :=
1856 sigaction (
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;
1867 end Initialize;
1869 -- Package elaboration
1871 begin
1872 declare
1873 Result : Interfaces.C.int;
1874 begin
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);
1891 end if;
1892 end loop;
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);
1900 end;
1902 if Dispatching_Policy = 'F' then
1903 declare
1904 Result : Interfaces.C.long;
1905 Class_Info : aliased struct_pcinfo;
1906 Secs, Nsecs : Interfaces.C.long;
1908 begin
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
1913 -- nanoseconds
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)
1920 else
1921 Secs := RT_TQINF;
1922 Nsecs := RT_TQINF;
1923 end if;
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;
1945 end;
1946 end if;
1947 end System.Task_Primitives.Operations;