mips.h (set_volatile): Delete.
[official-gcc.git] / gcc / ada / s-taprop-solaris.adb
blob330519db8ea03bcf6a5a5c268dc5897dcf3c0148
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT 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 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a Solaris (native) version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 pragma Polling (Off);
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.Interrupt_Management;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
49 -- Interrupt_ID
51 with System.OS_Primitives;
52 -- used for Delay_Modes
54 pragma Warnings (Off);
55 with System.OS_Lib;
56 -- used for String_Access, Getenv
58 pragma Warnings (On);
60 with Interfaces.C;
61 -- used for int
62 -- size_t
64 with System.Task_Info;
65 -- to initialize Task_Info for a C thread, in function Self
67 with System.Soft_Links;
68 -- used for Defer/Undefer_Abort
70 -- We use System.Soft_Links instead of System.Tasking.Initialization
71 -- because the later is a higher level package that we shouldn't depend on.
72 -- For example when using the restricted run time, it is replaced by
73 -- System.Tasking.Restricted.Stages.
75 with Ada.Unchecked_Deallocation;
77 package body System.Task_Primitives.Operations is
79 package SSL renames System.Soft_Links;
81 use System.Tasking.Debug;
82 use System.Tasking;
83 use Interfaces.C;
84 use System.OS_Interface;
85 use System.Parameters;
86 use System.OS_Primitives;
88 ----------------
89 -- Local Data --
90 ----------------
92 -- The following are logically constants, but need to be initialized
93 -- at run time.
95 Environment_Task_Id : Task_Id;
96 -- A variable to hold Task_Id for the environment task.
97 -- If we use this variable to get the Task_Id, we need the following
98 -- ATCB_Key only for non-Ada threads.
100 Unblocked_Signal_Mask : aliased sigset_t;
101 -- The set of signals that should unblocked in all tasks
103 ATCB_Key : aliased thread_key_t;
104 -- Key used to find the Ada Task_Id associated with a thread,
105 -- at least for C threads unknown to the Ada run-time system.
107 Single_RTS_Lock : aliased RTS_Lock;
108 -- This is a lock to allow only one thread of control in the RTS at
109 -- a time; it is used to execute in mutual exclusion from all other tasks.
110 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
112 Next_Serial_Number : Task_Serial_Number := 100;
113 -- We start at 100, to reserve some special values for
114 -- using in error checking.
115 -- The following are internal configuration constants needed.
117 ----------------------
118 -- Priority Support --
119 ----------------------
121 Priority_Ceiling_Emulation : constant Boolean := True;
122 -- controls whether we emulate priority ceiling locking
124 -- To get a scheduling close to annex D requirements, we use the real-time
125 -- class provided for LWP's and map each task/thread to a specific and
126 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
128 -- The real time class can only be set when the process has root
129 -- priviledges, so in the other cases, we use the normal thread scheduling
130 -- and priority handling.
132 Using_Real_Time_Class : Boolean := False;
133 -- indicates wether the real time class is being used (i.e the process
134 -- has root priviledges).
136 Prio_Param : aliased struct_pcparms;
137 -- Hold priority info (Real_Time) initialized during the package
138 -- elaboration.
140 -----------------------------------
141 -- External Configuration Values --
142 -----------------------------------
144 Time_Slice_Val : Integer;
145 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
147 Locking_Policy : Character;
148 pragma Import (C, Locking_Policy, "__gl_locking_policy");
150 Dispatching_Policy : Character;
151 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
153 Foreign_Task_Elaborated : aliased Boolean := True;
154 -- Used to identified fake tasks (i.e., non-Ada Threads)
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 function sysconf (name : System.OS_Interface.int) return processorid_t;
161 pragma Import (C, sysconf, "sysconf");
163 SC_NPROCESSORS_CONF : constant System.OS_Interface.int := 14;
165 function Num_Procs
166 (name : System.OS_Interface.int := SC_NPROCESSORS_CONF)
167 return processorid_t renames sysconf;
169 procedure Abort_Handler
170 (Sig : Signal;
171 Code : not null access siginfo_t;
172 Context : not null access ucontext_t);
173 -- Target-dependent binding of inter-thread Abort signal to
174 -- the raising of the Abort_Signal exception.
175 -- See also comments in 7staprop.adb
177 ------------
178 -- Checks --
179 ------------
181 function Check_Initialize_Lock
182 (L : Lock_Ptr;
183 Level : Lock_Level) return Boolean;
184 pragma Inline (Check_Initialize_Lock);
186 function Check_Lock (L : Lock_Ptr) return Boolean;
187 pragma Inline (Check_Lock);
189 function Record_Lock (L : Lock_Ptr) return Boolean;
190 pragma Inline (Record_Lock);
192 function Check_Sleep (Reason : Task_States) return Boolean;
193 pragma Inline (Check_Sleep);
195 function Record_Wakeup
196 (L : Lock_Ptr;
197 Reason : Task_States) return Boolean;
198 pragma Inline (Record_Wakeup);
200 function Check_Wakeup
201 (T : Task_Id;
202 Reason : Task_States) return Boolean;
203 pragma Inline (Check_Wakeup);
205 function Check_Unlock (L : Lock_Ptr) return Boolean;
206 pragma Inline (Check_Unlock);
208 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean;
209 pragma Inline (Check_Finalize_Lock);
211 --------------------
212 -- Local Packages --
213 --------------------
215 package Specific is
217 procedure Initialize (Environment_Task : Task_Id);
218 pragma Inline (Initialize);
219 -- Initialize various data needed by this package
221 function Is_Valid_Task return Boolean;
222 pragma Inline (Is_Valid_Task);
223 -- Does executing thread have a TCB?
225 procedure Set (Self_Id : Task_Id);
226 pragma Inline (Set);
227 -- Set the self id for the current task
229 function Self return Task_Id;
230 pragma Inline (Self);
231 -- Return a pointer to the Ada Task Control Block of the calling task
233 end Specific;
235 package body Specific is separate;
236 -- The body of this package is target specific
238 ---------------------------------
239 -- Support for foreign threads --
240 ---------------------------------
242 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
243 -- Allocate and Initialize a new ATCB for the current Thread
245 function Register_Foreign_Thread
246 (Thread : Thread_Id) return Task_Id is separate;
248 ------------
249 -- Checks --
250 ------------
252 Check_Count : Integer := 0;
253 Lock_Count : Integer := 0;
254 Unlock_Count : Integer := 0;
256 -------------------
257 -- Abort_Handler --
258 -------------------
260 procedure Abort_Handler
261 (Sig : Signal;
262 Code : not null access siginfo_t;
263 Context : not null access ucontext_t)
265 pragma Unreferenced (Sig);
266 pragma Unreferenced (Code);
267 pragma Unreferenced (Context);
269 Self_ID : constant Task_Id := Self;
270 Old_Set : aliased sigset_t;
272 Result : Interfaces.C.int;
273 pragma Warnings (Off, Result);
275 begin
276 -- It is not safe to raise an exception when using ZCX and the GCC
277 -- exception handling mechanism.
279 if ZCX_By_Default and then GCC_ZCX_Support then
280 return;
281 end if;
283 if Self_ID.Deferral_Level = 0
284 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
285 and then not Self_ID.Aborting
286 then
287 Self_ID.Aborting := True;
289 -- Make sure signals used for RTS internal purpose are unmasked
291 Result :=
292 thr_sigsetmask
293 (SIG_UNBLOCK,
294 Unblocked_Signal_Mask'Unchecked_Access,
295 Old_Set'Unchecked_Access);
296 pragma Assert (Result = 0);
298 raise Standard'Abort_Signal;
299 end if;
300 end Abort_Handler;
302 -----------------
303 -- Stack_Guard --
304 -----------------
306 -- The underlying thread system sets a guard page at the
307 -- bottom of a thread stack, so nothing is needed.
309 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
310 pragma Unreferenced (T);
311 pragma Unreferenced (On);
312 begin
313 null;
314 end Stack_Guard;
316 -------------------
317 -- Get_Thread_Id --
318 -------------------
320 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
321 begin
322 return T.Common.LL.Thread;
323 end Get_Thread_Id;
325 ----------------
326 -- Initialize --
327 ----------------
329 procedure Initialize (Environment_Task : ST.Task_Id) is
330 act : aliased struct_sigaction;
331 old_act : aliased struct_sigaction;
332 Tmp_Set : aliased sigset_t;
333 Result : Interfaces.C.int;
335 procedure Configure_Processors;
336 -- Processors configuration
337 -- The user can specify a processor which the program should run
338 -- on to emulate a single-processor system. This can be easily
339 -- done by setting environment variable GNAT_PROCESSOR to one of
340 -- the following :
342 -- -2 : use the default configuration (run the program on all
343 -- available processors) - this is the same as having
344 -- GNAT_PROCESSOR unset
345 -- -1 : let the RTS choose one processor and run the program on
346 -- that processor
347 -- 0 .. Last_Proc : run the program on the specified processor
349 -- Last_Proc is equal to the value of the system variable
350 -- _SC_NPROCESSORS_CONF, minus one.
352 procedure Configure_Processors is
353 Proc_Acc : constant System.OS_Lib.String_Access :=
354 System.OS_Lib.Getenv ("GNAT_PROCESSOR");
355 Proc : aliased processorid_t; -- User processor #
356 Last_Proc : processorid_t; -- Last processor #
358 begin
359 if Proc_Acc.all'Length /= 0 then
361 -- Environment variable is defined
363 Last_Proc := Num_Procs - 1;
365 if Last_Proc /= -1 then
366 Proc := processorid_t'Value (Proc_Acc.all);
368 if Proc <= -2 or else Proc > Last_Proc then
370 -- Use the default configuration
372 null;
374 elsif Proc = -1 then
376 -- Choose a processor
378 Result := 0;
379 while Proc < Last_Proc loop
380 Proc := Proc + 1;
381 Result := p_online (Proc, PR_STATUS);
382 exit when Result = PR_ONLINE;
383 end loop;
385 pragma Assert (Result = PR_ONLINE);
386 Result := processor_bind (P_PID, P_MYID, Proc, null);
387 pragma Assert (Result = 0);
389 else
390 -- Use user processor
392 Result := processor_bind (P_PID, P_MYID, Proc, null);
393 pragma Assert (Result = 0);
394 end if;
395 end if;
396 end if;
398 exception
399 when Constraint_Error =>
401 -- Illegal environment variable GNAT_PROCESSOR - ignored
403 null;
404 end Configure_Processors;
406 function State
407 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
408 pragma Import (C, State, "__gnat_get_interrupt_state");
409 -- Get interrupt state. Defined in a-init.c
410 -- The input argument is the interrupt number,
411 -- and the result is one of the following:
413 Default : constant Character := 's';
414 -- 'n' this interrupt not set by any Interrupt_State pragma
415 -- 'u' Interrupt_State pragma set state to User
416 -- 'r' Interrupt_State pragma set state to Runtime
417 -- 's' Interrupt_State pragma set state to System (use "default"
418 -- system handler)
420 -- Start of processing for Initialize
422 begin
423 Environment_Task_Id := Environment_Task;
425 Interrupt_Management.Initialize;
427 -- Prepare the set of signals that should unblocked in all tasks
429 Result := sigemptyset (Unblocked_Signal_Mask'Access);
430 pragma Assert (Result = 0);
432 for J in Interrupt_Management.Interrupt_ID loop
433 if System.Interrupt_Management.Keep_Unmasked (J) then
434 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
435 pragma Assert (Result = 0);
436 end if;
437 end loop;
439 if Dispatching_Policy = 'F' then
440 declare
441 Result : Interfaces.C.long;
442 Class_Info : aliased struct_pcinfo;
443 Secs, Nsecs : Interfaces.C.long;
445 begin
446 -- If a pragma Time_Slice is specified, takes the value in account
448 if Time_Slice_Val > 0 then
450 -- Convert Time_Slice_Val (microseconds) to seconds/nanosecs
452 Secs := Interfaces.C.long (Time_Slice_Val / 1_000_000);
453 Nsecs :=
454 Interfaces.C.long ((Time_Slice_Val rem 1_000_000) * 1_000);
456 -- Otherwise, default to no time slicing (i.e run until blocked)
458 else
459 Secs := RT_TQINF;
460 Nsecs := RT_TQINF;
461 end if;
463 -- Get the real time class id
465 Class_Info.pc_clname (1) := 'R';
466 Class_Info.pc_clname (2) := 'T';
467 Class_Info.pc_clname (3) := ASCII.NUL;
469 Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_GETCID,
470 Class_Info'Address);
472 -- Request the real time class
474 Prio_Param.pc_cid := Class_Info.pc_cid;
475 Prio_Param.rt_pri := pri_t (Class_Info.rt_maxpri);
476 Prio_Param.rt_tqsecs := Secs;
477 Prio_Param.rt_tqnsecs := Nsecs;
479 Result :=
480 priocntl
481 (PC_VERSION, P_LWPID, P_MYID, PC_SETPARMS, Prio_Param'Address);
483 Using_Real_Time_Class := Result /= -1;
484 end;
485 end if;
487 Specific.Initialize (Environment_Task);
489 -- The following is done in Enter_Task, but this is too late for the
490 -- Environment Task, since we need to call Self in Check_Locks when
491 -- the run time is compiled with assertions on.
493 Specific.Set (Environment_Task);
495 -- Initialize the lock used to synchronize chain of all ATCBs
497 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
499 Enter_Task (Environment_Task);
501 -- Install the abort-signal handler
503 if State
504 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
505 then
506 -- Set sa_flags to SA_NODEFER so that during the handler execution
507 -- we do not change the Signal_Mask to be masked for the Abort_Signal
508 -- This is a temporary fix to the problem that the Signal_Mask is
509 -- not restored after the exception (longjmp) from the handler.
510 -- The right fix should be made in sigsetjmp so that we save
511 -- the Signal_Set and restore it after a longjmp.
512 -- In that case, this field should be changed back to 0. ???
514 act.sa_flags := 16;
516 act.sa_handler := Abort_Handler'Address;
517 Result := sigemptyset (Tmp_Set'Access);
518 pragma Assert (Result = 0);
519 act.sa_mask := Tmp_Set;
521 Result :=
522 sigaction
523 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
524 act'Unchecked_Access,
525 old_act'Unchecked_Access);
526 pragma Assert (Result = 0);
527 end if;
529 Configure_Processors;
530 end Initialize;
532 ---------------------
533 -- Initialize_Lock --
534 ---------------------
536 -- Note: mutexes and cond_variables needed per-task basis are initialized
537 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
538 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
539 -- status change of RTS. Therefore rasing Storage_Error in the following
540 -- routines should be able to be handled safely.
542 procedure Initialize_Lock
543 (Prio : System.Any_Priority;
544 L : not null access Lock)
546 Result : Interfaces.C.int;
548 begin
549 pragma Assert (Check_Initialize_Lock (Lock_Ptr (L), PO_Level));
551 if Priority_Ceiling_Emulation then
552 L.Ceiling := Prio;
553 end if;
555 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
556 pragma Assert (Result = 0 or else Result = ENOMEM);
558 if Result = ENOMEM then
559 raise Storage_Error with "Failed to allocate a lock";
560 end if;
561 end Initialize_Lock;
563 procedure Initialize_Lock
564 (L : not null access RTS_Lock;
565 Level : Lock_Level)
567 Result : Interfaces.C.int;
569 begin
570 pragma Assert
571 (Check_Initialize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L)), Level));
572 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
573 pragma Assert (Result = 0 or else Result = ENOMEM);
575 if Result = ENOMEM then
576 raise Storage_Error with "Failed to allocate a lock";
577 end if;
578 end Initialize_Lock;
580 -------------------
581 -- Finalize_Lock --
582 -------------------
584 procedure Finalize_Lock (L : not null access Lock) is
585 Result : Interfaces.C.int;
586 begin
587 pragma Assert (Check_Finalize_Lock (Lock_Ptr (L)));
588 Result := mutex_destroy (L.L'Access);
589 pragma Assert (Result = 0);
590 end Finalize_Lock;
592 procedure Finalize_Lock (L : not null access RTS_Lock) is
593 Result : Interfaces.C.int;
594 begin
595 pragma Assert (Check_Finalize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
596 Result := mutex_destroy (L.L'Access);
597 pragma Assert (Result = 0);
598 end Finalize_Lock;
600 ----------------
601 -- Write_Lock --
602 ----------------
604 procedure Write_Lock
605 (L : not null access Lock;
606 Ceiling_Violation : out Boolean)
608 Result : Interfaces.C.int;
610 begin
611 pragma Assert (Check_Lock (Lock_Ptr (L)));
613 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
614 declare
615 Self_Id : constant Task_Id := Self;
616 Saved_Priority : System.Any_Priority;
618 begin
619 if Self_Id.Common.LL.Active_Priority > L.Ceiling then
620 Ceiling_Violation := True;
621 return;
622 end if;
624 Saved_Priority := Self_Id.Common.LL.Active_Priority;
626 if Self_Id.Common.LL.Active_Priority < L.Ceiling then
627 Set_Priority (Self_Id, L.Ceiling);
628 end if;
630 Result := mutex_lock (L.L'Access);
631 pragma Assert (Result = 0);
632 Ceiling_Violation := False;
634 L.Saved_Priority := Saved_Priority;
635 end;
637 else
638 Result := mutex_lock (L.L'Access);
639 pragma Assert (Result = 0);
640 Ceiling_Violation := False;
641 end if;
643 pragma Assert (Record_Lock (Lock_Ptr (L)));
644 end Write_Lock;
646 procedure Write_Lock
647 (L : not null access RTS_Lock;
648 Global_Lock : Boolean := False)
650 Result : Interfaces.C.int;
651 begin
652 if not Single_Lock or else Global_Lock then
653 pragma Assert (Check_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
654 Result := mutex_lock (L.L'Access);
655 pragma Assert (Result = 0);
656 pragma Assert (Record_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
657 end if;
658 end Write_Lock;
660 procedure Write_Lock (T : Task_Id) is
661 Result : Interfaces.C.int;
662 begin
663 if not Single_Lock then
664 pragma Assert (Check_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
665 Result := mutex_lock (T.Common.LL.L.L'Access);
666 pragma Assert (Result = 0);
667 pragma Assert (Record_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
668 end if;
669 end Write_Lock;
671 ---------------
672 -- Read_Lock --
673 ---------------
675 procedure Read_Lock
676 (L : not null access Lock;
677 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 : not null access Lock) is
687 Result : Interfaces.C.int;
689 begin
690 pragma Assert (Check_Unlock (Lock_Ptr (L)));
692 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
693 declare
694 Self_Id : constant Task_Id := Self;
696 begin
697 Result := mutex_unlock (L.L'Access);
698 pragma Assert (Result = 0);
700 if Self_Id.Common.LL.Active_Priority > L.Saved_Priority then
701 Set_Priority (Self_Id, L.Saved_Priority);
702 end if;
703 end;
704 else
705 Result := mutex_unlock (L.L'Access);
706 pragma Assert (Result = 0);
707 end if;
708 end Unlock;
710 procedure Unlock
711 (L : not null access RTS_Lock;
712 Global_Lock : Boolean := False)
714 Result : Interfaces.C.int;
715 begin
716 if not Single_Lock or else Global_Lock then
717 pragma Assert (Check_Unlock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
718 Result := mutex_unlock (L.L'Access);
719 pragma Assert (Result = 0);
720 end if;
721 end Unlock;
723 procedure Unlock (T : Task_Id) is
724 Result : Interfaces.C.int;
725 begin
726 if not Single_Lock then
727 pragma Assert (Check_Unlock (To_Lock_Ptr (T.Common.LL.L'Access)));
728 Result := mutex_unlock (T.Common.LL.L.L'Access);
729 pragma Assert (Result = 0);
730 end if;
731 end Unlock;
733 -----------------
734 -- Set_Ceiling --
735 -----------------
737 -- Dynamic priority ceilings are not supported by the underlying system
739 procedure Set_Ceiling
740 (L : not null access Lock;
741 Prio : System.Any_Priority)
743 pragma Unreferenced (L, Prio);
744 begin
745 null;
746 end Set_Ceiling;
748 -- For the time delay implementation, we need to make sure we
749 -- achieve following criteria:
751 -- 1) We have to delay at least for the amount requested.
752 -- 2) We have to give up CPU even though the actual delay does not
753 -- result in blocking.
754 -- 3) Except for restricted run-time systems that do not support
755 -- ATC or task abort, the delay must be interrupted by the
756 -- abort_task operation.
757 -- 4) The implementation has to be efficient so that the delay overhead
758 -- is relatively cheap.
759 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
760 -- requirement we still want to provide the effect in all cases.
761 -- The reason is that users may want to use short delays to implement
762 -- their own scheduling effect in the absence of language provided
763 -- scheduling policies.
765 ---------------------
766 -- Monotonic_Clock --
767 ---------------------
769 function Monotonic_Clock return Duration is
770 TS : aliased timespec;
771 Result : Interfaces.C.int;
772 begin
773 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
774 pragma Assert (Result = 0);
775 return To_Duration (TS);
776 end Monotonic_Clock;
778 -------------------
779 -- RT_Resolution --
780 -------------------
782 function RT_Resolution return Duration is
783 begin
784 return 10#1.0#E-6;
785 end RT_Resolution;
787 -----------
788 -- Yield --
789 -----------
791 procedure Yield (Do_Yield : Boolean := True) is
792 begin
793 if Do_Yield then
794 System.OS_Interface.thr_yield;
795 end if;
796 end Yield;
798 -----------
799 -- Self ---
800 -----------
802 function Self return Task_Id renames Specific.Self;
804 ------------------
805 -- Set_Priority --
806 ------------------
808 procedure Set_Priority
809 (T : Task_Id;
810 Prio : System.Any_Priority;
811 Loss_Of_Inheritance : Boolean := False)
813 pragma Unreferenced (Loss_Of_Inheritance);
815 Result : Interfaces.C.int;
816 pragma Unreferenced (Result);
818 Param : aliased struct_pcparms;
820 use Task_Info;
822 begin
823 T.Common.Current_Priority := Prio;
825 if Priority_Ceiling_Emulation then
826 T.Common.LL.Active_Priority := Prio;
827 end if;
829 if Using_Real_Time_Class then
830 Param.pc_cid := Prio_Param.pc_cid;
831 Param.rt_pri := pri_t (Prio);
832 Param.rt_tqsecs := Prio_Param.rt_tqsecs;
833 Param.rt_tqnsecs := Prio_Param.rt_tqnsecs;
835 Result := Interfaces.C.int (
836 priocntl (PC_VERSION, P_LWPID, T.Common.LL.LWP, PC_SETPARMS,
837 Param'Address));
839 else
840 if T.Common.Task_Info /= null
841 and then not T.Common.Task_Info.Bound_To_LWP
842 then
843 -- The task is not bound to a LWP, so use thr_setprio
845 Result :=
846 thr_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
848 else
849 -- The task is bound to a LWP, use priocntl
850 -- ??? TBD
852 null;
853 end if;
854 end if;
855 end Set_Priority;
857 ------------------
858 -- Get_Priority --
859 ------------------
861 function Get_Priority (T : Task_Id) return System.Any_Priority is
862 begin
863 return T.Common.Current_Priority;
864 end Get_Priority;
866 ----------------
867 -- Enter_Task --
868 ----------------
870 procedure Enter_Task (Self_ID : Task_Id) is
871 Result : Interfaces.C.int;
872 Proc : processorid_t; -- User processor #
873 Last_Proc : processorid_t; -- Last processor #
875 use System.Task_Info;
876 begin
877 Self_ID.Common.LL.Thread := thr_self;
879 Self_ID.Common.LL.LWP := lwp_self;
881 if Self_ID.Common.Task_Info /= null then
882 if Self_ID.Common.Task_Info.New_LWP
883 and then Self_ID.Common.Task_Info.CPU /= CPU_UNCHANGED
884 then
885 Last_Proc := Num_Procs - 1;
887 if Self_ID.Common.Task_Info.CPU = ANY_CPU then
888 Result := 0;
889 Proc := 0;
890 while Proc < Last_Proc loop
891 Result := p_online (Proc, PR_STATUS);
892 exit when Result = PR_ONLINE;
893 Proc := Proc + 1;
894 end loop;
896 Result := processor_bind (P_LWPID, P_MYID, Proc, null);
897 pragma Assert (Result = 0);
899 else
900 -- Use specified processor
902 if Self_ID.Common.Task_Info.CPU < 0
903 or else Self_ID.Common.Task_Info.CPU > Last_Proc
904 then
905 raise Invalid_CPU_Number;
906 end if;
908 Result :=
909 processor_bind
910 (P_LWPID, P_MYID, Self_ID.Common.Task_Info.CPU, null);
911 pragma Assert (Result = 0);
912 end if;
913 end if;
914 end if;
916 Specific.Set (Self_ID);
918 -- We need the above code even if we do direct fetch of Task_Id in Self
919 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
921 Lock_RTS;
923 for J in Known_Tasks'Range loop
924 if Known_Tasks (J) = null then
925 Known_Tasks (J) := Self_ID;
926 Self_ID.Known_Tasks_Index := J;
927 exit;
928 end if;
929 end loop;
931 Unlock_RTS;
932 end Enter_Task;
934 --------------
935 -- New_ATCB --
936 --------------
938 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
939 begin
940 return new Ada_Task_Control_Block (Entry_Num);
941 end New_ATCB;
943 -------------------
944 -- Is_Valid_Task --
945 -------------------
947 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
949 -----------------------------
950 -- Register_Foreign_Thread --
951 -----------------------------
953 function Register_Foreign_Thread return Task_Id is
954 begin
955 if Is_Valid_Task then
956 return Self;
957 else
958 return Register_Foreign_Thread (thr_self);
959 end if;
960 end Register_Foreign_Thread;
962 --------------------
963 -- Initialize_TCB --
964 --------------------
966 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
967 Result : Interfaces.C.int := 0;
969 begin
970 -- Give the task a unique serial number
972 Self_ID.Serial_Number := Next_Serial_Number;
973 Next_Serial_Number := Next_Serial_Number + 1;
974 pragma Assert (Next_Serial_Number /= 0);
976 Self_ID.Common.LL.Thread := To_thread_t (-1);
978 if not Single_Lock then
979 Result :=
980 mutex_init
981 (Self_ID.Common.LL.L.L'Access, USYNC_THREAD, System.Null_Address);
982 Self_ID.Common.LL.L.Level :=
983 Private_Task_Serial_Number (Self_ID.Serial_Number);
984 pragma Assert (Result = 0 or else Result = ENOMEM);
985 end if;
987 if Result = 0 then
988 Result := cond_init (Self_ID.Common.LL.CV'Access, USYNC_THREAD, 0);
989 pragma Assert (Result = 0 or else Result = ENOMEM);
990 end if;
992 if Result = 0 then
993 Succeeded := True;
994 else
995 if not Single_Lock then
996 Result := mutex_destroy (Self_ID.Common.LL.L.L'Access);
997 pragma Assert (Result = 0);
998 end if;
1000 Succeeded := False;
1001 end if;
1002 end Initialize_TCB;
1004 -----------------
1005 -- Create_Task --
1006 -----------------
1008 procedure Create_Task
1009 (T : Task_Id;
1010 Wrapper : System.Address;
1011 Stack_Size : System.Parameters.Size_Type;
1012 Priority : System.Any_Priority;
1013 Succeeded : out Boolean)
1015 pragma Unreferenced (Priority);
1017 Result : Interfaces.C.int;
1018 Adjusted_Stack_Size : Interfaces.C.size_t;
1019 Opts : Interfaces.C.int := THR_DETACHED;
1021 Page_Size : constant System.Parameters.Size_Type := 4096;
1022 -- This constant is for reserving extra space at the
1023 -- end of the stack, which can be used by the stack
1024 -- checking as guard page. The idea is that we need
1025 -- to have at least Stack_Size bytes available for
1026 -- actual use.
1028 use System.Task_Info;
1030 begin
1031 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size + Page_Size);
1033 -- Since the initial signal mask of a thread is inherited from the
1034 -- creator, and the Environment task has all its signals masked, we
1035 -- do not need to manipulate caller's signal mask at this point.
1036 -- All tasks in RTS will have All_Tasks_Mask initially.
1038 if T.Common.Task_Info /= null then
1039 if T.Common.Task_Info.New_LWP then
1040 Opts := Opts + THR_NEW_LWP;
1041 end if;
1043 if T.Common.Task_Info.Bound_To_LWP then
1044 Opts := Opts + THR_BOUND;
1045 end if;
1047 else
1048 Opts := THR_DETACHED + THR_BOUND;
1049 end if;
1051 Result :=
1052 thr_create
1053 (System.Null_Address,
1054 Adjusted_Stack_Size,
1055 Thread_Body_Access (Wrapper),
1056 To_Address (T),
1057 Opts,
1058 T.Common.LL.Thread'Access);
1060 Succeeded := Result = 0;
1061 pragma Assert
1062 (Result = 0
1063 or else Result = ENOMEM
1064 or else Result = EAGAIN);
1065 end Create_Task;
1067 ------------------
1068 -- Finalize_TCB --
1069 ------------------
1071 procedure Finalize_TCB (T : Task_Id) is
1072 Result : Interfaces.C.int;
1073 Tmp : Task_Id := T;
1074 Is_Self : constant Boolean := T = Self;
1076 procedure Free is new
1077 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1079 begin
1080 T.Common.LL.Thread := To_thread_t (0);
1082 if not Single_Lock then
1083 Result := mutex_destroy (T.Common.LL.L.L'Access);
1084 pragma Assert (Result = 0);
1085 end if;
1087 Result := cond_destroy (T.Common.LL.CV'Access);
1088 pragma Assert (Result = 0);
1090 if T.Known_Tasks_Index /= -1 then
1091 Known_Tasks (T.Known_Tasks_Index) := null;
1092 end if;
1094 Free (Tmp);
1096 if Is_Self then
1097 Specific.Set (null);
1098 end if;
1099 end Finalize_TCB;
1101 ---------------
1102 -- Exit_Task --
1103 ---------------
1105 -- This procedure must be called with abort deferred. It can no longer
1106 -- call Self or access the current task's ATCB, since the ATCB has been
1107 -- deallocated.
1109 procedure Exit_Task is
1110 begin
1111 Specific.Set (null);
1112 end Exit_Task;
1114 ----------------
1115 -- Abort_Task --
1116 ----------------
1118 procedure Abort_Task (T : Task_Id) is
1119 Result : Interfaces.C.int;
1120 begin
1121 pragma Assert (T /= Self);
1122 Result :=
1123 thr_kill
1124 (T.Common.LL.Thread,
1125 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1126 pragma Assert (Result = 0);
1127 end Abort_Task;
1129 -----------
1130 -- Sleep --
1131 -----------
1133 procedure Sleep
1134 (Self_ID : Task_Id;
1135 Reason : Task_States)
1137 Result : Interfaces.C.int;
1139 begin
1140 pragma Assert (Check_Sleep (Reason));
1142 if Single_Lock then
1143 Result :=
1144 cond_wait
1145 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock.L'Access);
1146 else
1147 Result :=
1148 cond_wait
1149 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L.L'Access);
1150 end if;
1152 pragma Assert
1153 (Record_Wakeup (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1154 pragma Assert (Result = 0 or else Result = EINTR);
1155 end Sleep;
1157 -- Note that we are relying heaviliy here on GNAT represting Calendar.Time,
1158 -- System.Real_Time.Time, Duration, System.Real_Time.Time_Span in the same
1159 -- way, i.e., as a 64-bit count of nanoseconds.
1161 -- This allows us to always pass the timeout value as a Duration
1163 -- ???
1164 -- We are taking liberties here with the semantics of the delays. That is,
1165 -- we make no distinction between delays on the Calendar clock and delays
1166 -- on the Real_Time clock. That is technically incorrect, if the Calendar
1167 -- clock happens to be reset or adjusted. To solve this defect will require
1168 -- modification to the compiler interface, so that it can pass through more
1169 -- information, to tell us here which clock to use!
1171 -- cond_timedwait will return if any of the following happens:
1172 -- 1) some other task did cond_signal on this condition variable
1173 -- In this case, the return value is 0
1174 -- 2) the call just returned, for no good reason
1175 -- This is called a "spurious wakeup".
1176 -- In this case, the return value may also be 0.
1177 -- 3) the time delay expires
1178 -- In this case, the return value is ETIME
1179 -- 4) this task received a signal, which was handled by some
1180 -- handler procedure, and now the thread is resuming execution
1181 -- UNIX calls this an "interrupted" system call.
1182 -- In this case, the return value is EINTR
1184 -- If the cond_timedwait returns 0 or EINTR, it is still possible that the
1185 -- time has actually expired, and by chance a signal or cond_signal
1186 -- occurred at around the same time.
1188 -- We have also observed that on some OS's the value ETIME will be
1189 -- returned, but the clock will show that the full delay has not yet
1190 -- expired.
1192 -- For these reasons, we need to check the clock after return from
1193 -- cond_timedwait. If the time has expired, we will set Timedout = True.
1195 -- This check might be omitted for systems on which the cond_timedwait()
1196 -- never returns early or wakes up spuriously.
1198 -- Annex D requires that completion of a delay cause the task to go to the
1199 -- end of its priority queue, regardless of whether the task actually was
1200 -- suspended by the delay. Since cond_timedwait does not do this on
1201 -- Solaris, we add a call to thr_yield at the end. We might do this at the
1202 -- beginning, instead, but then the round-robin effect would not be the
1203 -- same; the delayed task would be ahead of other tasks of the same
1204 -- priority that awoke while it was sleeping.
1206 -- For Timed_Sleep, we are expecting possible cond_signals to indicate
1207 -- other events (e.g., completion of a RV or completion of the abortable
1208 -- part of an async. select), we want to always return if interrupted. The
1209 -- caller will be responsible for checking the task state to see whether
1210 -- the wakeup was spurious, and to go back to sleep again in that case. We
1211 -- don't need to check for pending abort or priority change on the way in
1212 -- our out; that is the caller's responsibility.
1214 -- For Timed_Delay, we are not expecting any cond_signals or other
1215 -- interruptions, except for priority changes and aborts. Therefore, we
1216 -- don't want to return unless the delay has actually expired, or the call
1217 -- has been aborted. In this case, since we want to implement the entire
1218 -- delay statement semantics, we do need to check for pending abort and
1219 -- priority changes. We can quietly handle priority changes inside the
1220 -- procedure, since there is no entry-queue reordering involved.
1222 -----------------
1223 -- Timed_Sleep --
1224 -----------------
1226 procedure Timed_Sleep
1227 (Self_ID : Task_Id;
1228 Time : Duration;
1229 Mode : ST.Delay_Modes;
1230 Reason : System.Tasking.Task_States;
1231 Timedout : out Boolean;
1232 Yielded : out Boolean)
1234 Base_Time : constant Duration := Monotonic_Clock;
1235 Check_Time : Duration := Base_Time;
1236 Abs_Time : Duration;
1237 Request : aliased timespec;
1238 Result : Interfaces.C.int;
1240 begin
1241 pragma Assert (Check_Sleep (Reason));
1242 Timedout := True;
1243 Yielded := False;
1245 if Mode = Relative then
1246 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
1247 else
1248 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1249 end if;
1251 if Abs_Time > Check_Time then
1252 Request := To_Timespec (Abs_Time);
1254 loop
1255 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1257 if Single_Lock then
1258 Result :=
1259 cond_timedwait
1260 (Self_ID.Common.LL.CV'Access,
1261 Single_RTS_Lock.L'Access, Request'Access);
1262 else
1263 Result :=
1264 cond_timedwait
1265 (Self_ID.Common.LL.CV'Access,
1266 Self_ID.Common.LL.L.L'Access, Request'Access);
1267 end if;
1269 Yielded := True;
1271 Check_Time := Monotonic_Clock;
1272 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
1274 if Result = 0 or Result = EINTR then
1276 -- Somebody may have called Wakeup for us
1278 Timedout := False;
1279 exit;
1280 end if;
1282 pragma Assert (Result = ETIME);
1283 end loop;
1284 end if;
1286 pragma Assert
1287 (Record_Wakeup (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1288 end Timed_Sleep;
1290 -----------------
1291 -- Timed_Delay --
1292 -----------------
1294 procedure Timed_Delay
1295 (Self_ID : Task_Id;
1296 Time : Duration;
1297 Mode : ST.Delay_Modes)
1299 Base_Time : constant Duration := Monotonic_Clock;
1300 Check_Time : Duration := Base_Time;
1301 Abs_Time : Duration;
1302 Request : aliased timespec;
1303 Result : Interfaces.C.int;
1304 Yielded : Boolean := False;
1306 begin
1307 if Single_Lock then
1308 Lock_RTS;
1309 end if;
1311 Write_Lock (Self_ID);
1313 if Mode = Relative then
1314 Abs_Time := Time + Check_Time;
1315 else
1316 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1317 end if;
1319 if Abs_Time > Check_Time then
1320 Request := To_Timespec (Abs_Time);
1321 Self_ID.Common.State := Delay_Sleep;
1323 pragma Assert (Check_Sleep (Delay_Sleep));
1325 loop
1326 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1328 if Single_Lock then
1329 Result :=
1330 cond_timedwait
1331 (Self_ID.Common.LL.CV'Access,
1332 Single_RTS_Lock.L'Access,
1333 Request'Access);
1334 else
1335 Result :=
1336 cond_timedwait
1337 (Self_ID.Common.LL.CV'Access,
1338 Self_ID.Common.LL.L.L'Access,
1339 Request'Access);
1340 end if;
1342 Yielded := True;
1344 Check_Time := Monotonic_Clock;
1345 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
1347 pragma Assert
1348 (Result = 0 or else
1349 Result = ETIME or else
1350 Result = EINTR);
1351 end loop;
1353 pragma Assert
1354 (Record_Wakeup
1355 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Delay_Sleep));
1357 Self_ID.Common.State := Runnable;
1358 end if;
1360 Unlock (Self_ID);
1362 if Single_Lock then
1363 Unlock_RTS;
1364 end if;
1366 if not Yielded then
1367 thr_yield;
1368 end if;
1369 end Timed_Delay;
1371 ------------
1372 -- Wakeup --
1373 ------------
1375 procedure Wakeup
1376 (T : Task_Id;
1377 Reason : Task_States)
1379 Result : Interfaces.C.int;
1380 begin
1381 pragma Assert (Check_Wakeup (T, Reason));
1382 Result := cond_signal (T.Common.LL.CV'Access);
1383 pragma Assert (Result = 0);
1384 end Wakeup;
1386 ---------------------------
1387 -- Check_Initialize_Lock --
1388 ---------------------------
1390 -- The following code is intended to check some of the invariant assertions
1391 -- related to lock usage, on which we depend.
1393 function Check_Initialize_Lock
1394 (L : Lock_Ptr;
1395 Level : Lock_Level) return Boolean
1397 Self_ID : constant Task_Id := Self;
1399 begin
1400 -- Check that caller is abort-deferred
1402 if Self_ID.Deferral_Level = 0 then
1403 return False;
1404 end if;
1406 -- Check that the lock is not yet initialized
1408 if L.Level /= 0 then
1409 return False;
1410 end if;
1412 L.Level := Lock_Level'Pos (Level) + 1;
1413 return True;
1414 end Check_Initialize_Lock;
1416 ----------------
1417 -- Check_Lock --
1418 ----------------
1420 function Check_Lock (L : Lock_Ptr) return Boolean is
1421 Self_ID : constant Task_Id := Self;
1422 P : Lock_Ptr;
1424 begin
1425 -- Check that the argument is not null
1427 if L = null then
1428 return False;
1429 end if;
1431 -- Check that L is not frozen
1433 if L.Frozen then
1434 return False;
1435 end if;
1437 -- Check that caller is abort-deferred
1439 if Self_ID.Deferral_Level = 0 then
1440 return False;
1441 end if;
1443 -- Check that caller is not holding this lock already
1445 if L.Owner = To_Owner_ID (To_Address (Self_ID)) then
1446 return False;
1447 end if;
1449 if Single_Lock then
1450 return True;
1451 end if;
1453 -- Check that TCB lock order rules are satisfied
1455 P := Self_ID.Common.LL.Locks;
1456 if P /= null then
1457 if P.Level >= L.Level
1458 and then (P.Level > 2 or else L.Level > 2)
1459 then
1460 return False;
1461 end if;
1462 end if;
1464 return True;
1465 end Check_Lock;
1467 -----------------
1468 -- Record_Lock --
1469 -----------------
1471 function Record_Lock (L : Lock_Ptr) return Boolean is
1472 Self_ID : constant Task_Id := Self;
1473 P : Lock_Ptr;
1475 begin
1476 Lock_Count := Lock_Count + 1;
1478 -- There should be no owner for this lock at this point
1480 if L.Owner /= null then
1481 return False;
1482 end if;
1484 -- Record new owner
1486 L.Owner := To_Owner_ID (To_Address (Self_ID));
1488 if Single_Lock then
1489 return True;
1490 end if;
1492 -- Check that TCB lock order rules are satisfied
1494 P := Self_ID.Common.LL.Locks;
1496 if P /= null then
1497 L.Next := P;
1498 end if;
1500 Self_ID.Common.LL.Locking := null;
1501 Self_ID.Common.LL.Locks := L;
1502 return True;
1503 end Record_Lock;
1505 -----------------
1506 -- Check_Sleep --
1507 -----------------
1509 function Check_Sleep (Reason : Task_States) return Boolean is
1510 pragma Unreferenced (Reason);
1512 Self_ID : constant Task_Id := Self;
1513 P : Lock_Ptr;
1515 begin
1516 -- Check that caller is abort-deferred
1518 if Self_ID.Deferral_Level = 0 then
1519 return False;
1520 end if;
1522 if Single_Lock then
1523 return True;
1524 end if;
1526 -- Check that caller is holding own lock, on top of list
1528 if Self_ID.Common.LL.Locks /=
1529 To_Lock_Ptr (Self_ID.Common.LL.L'Access)
1530 then
1531 return False;
1532 end if;
1534 -- Check that TCB lock order rules are satisfied
1536 if Self_ID.Common.LL.Locks.Next /= null then
1537 return False;
1538 end if;
1540 Self_ID.Common.LL.L.Owner := null;
1541 P := Self_ID.Common.LL.Locks;
1542 Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
1543 P.Next := null;
1544 return True;
1545 end Check_Sleep;
1547 -------------------
1548 -- Record_Wakeup --
1549 -------------------
1551 function Record_Wakeup
1552 (L : Lock_Ptr;
1553 Reason : Task_States) return Boolean
1555 pragma Unreferenced (Reason);
1557 Self_ID : constant Task_Id := Self;
1558 P : Lock_Ptr;
1560 begin
1561 -- Record new owner
1563 L.Owner := To_Owner_ID (To_Address (Self_ID));
1565 if Single_Lock then
1566 return True;
1567 end if;
1569 -- Check that TCB lock order rules are satisfied
1571 P := Self_ID.Common.LL.Locks;
1573 if P /= null then
1574 L.Next := P;
1575 end if;
1577 Self_ID.Common.LL.Locking := null;
1578 Self_ID.Common.LL.Locks := L;
1579 return True;
1580 end Record_Wakeup;
1582 ------------------
1583 -- Check_Wakeup --
1584 ------------------
1586 function Check_Wakeup
1587 (T : Task_Id;
1588 Reason : Task_States) return Boolean
1590 Self_ID : constant Task_Id := Self;
1592 begin
1593 -- Is caller holding T's lock?
1595 if T.Common.LL.L.Owner /= To_Owner_ID (To_Address (Self_ID)) then
1596 return False;
1597 end if;
1599 -- Are reasons for wakeup and sleep consistent?
1601 if T.Common.State /= Reason then
1602 return False;
1603 end if;
1605 return True;
1606 end Check_Wakeup;
1608 ------------------
1609 -- Check_Unlock --
1610 ------------------
1612 function Check_Unlock (L : Lock_Ptr) return Boolean is
1613 Self_ID : constant Task_Id := Self;
1614 P : Lock_Ptr;
1616 begin
1617 Unlock_Count := Unlock_Count + 1;
1619 if L = null then
1620 return False;
1621 end if;
1623 if L.Buddy /= null then
1624 return False;
1625 end if;
1627 -- Magic constant 4???
1629 if L.Level = 4 then
1630 Check_Count := Unlock_Count;
1631 end if;
1633 -- Magic constant 1000???
1635 if Unlock_Count - Check_Count > 1000 then
1636 Check_Count := Unlock_Count;
1637 end if;
1639 -- Check that caller is abort-deferred
1641 if Self_ID.Deferral_Level = 0 then
1642 return False;
1643 end if;
1645 -- Check that caller is holding this lock, on top of list
1647 if Self_ID.Common.LL.Locks /= L then
1648 return False;
1649 end if;
1651 -- Record there is no owner now
1653 L.Owner := null;
1654 P := Self_ID.Common.LL.Locks;
1655 Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
1656 P.Next := null;
1657 return True;
1658 end Check_Unlock;
1660 --------------------
1661 -- Check_Finalize --
1662 --------------------
1664 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean is
1665 Self_ID : constant Task_Id := Self;
1667 begin
1668 -- Check that caller is abort-deferred
1670 if Self_ID.Deferral_Level = 0 then
1671 return False;
1672 end if;
1674 -- Check that no one is holding this lock
1676 if L.Owner /= null then
1677 return False;
1678 end if;
1680 L.Frozen := True;
1681 return True;
1682 end Check_Finalize_Lock;
1684 ----------------
1685 -- Initialize --
1686 ----------------
1688 procedure Initialize (S : in out Suspension_Object) is
1689 Result : Interfaces.C.int;
1691 begin
1692 -- Initialize internal state (always to zero (RM D.10(6)))
1694 S.State := False;
1695 S.Waiting := False;
1697 -- Initialize internal mutex
1699 Result := mutex_init (S.L'Access, USYNC_THREAD, System.Null_Address);
1700 pragma Assert (Result = 0 or else Result = ENOMEM);
1702 if Result = ENOMEM then
1703 raise Storage_Error with "Failed to allocate a lock";
1704 end if;
1706 -- Initialize internal condition variable
1708 Result := cond_init (S.CV'Access, USYNC_THREAD, 0);
1709 pragma Assert (Result = 0 or else Result = ENOMEM);
1711 if Result /= 0 then
1712 Result := mutex_destroy (S.L'Access);
1713 pragma Assert (Result = 0);
1715 if Result = ENOMEM then
1716 raise Storage_Error;
1717 end if;
1718 end if;
1719 end Initialize;
1721 --------------
1722 -- Finalize --
1723 --------------
1725 procedure Finalize (S : in out Suspension_Object) is
1726 Result : Interfaces.C.int;
1728 begin
1729 -- Destroy internal mutex
1731 Result := mutex_destroy (S.L'Access);
1732 pragma Assert (Result = 0);
1734 -- Destroy internal condition variable
1736 Result := cond_destroy (S.CV'Access);
1737 pragma Assert (Result = 0);
1738 end Finalize;
1740 -------------------
1741 -- Current_State --
1742 -------------------
1744 function Current_State (S : Suspension_Object) return Boolean is
1745 begin
1746 -- We do not want to use lock on this read operation. State is marked
1747 -- as Atomic so that we ensure that the value retrieved is correct.
1749 return S.State;
1750 end Current_State;
1752 ---------------
1753 -- Set_False --
1754 ---------------
1756 procedure Set_False (S : in out Suspension_Object) is
1757 Result : Interfaces.C.int;
1759 begin
1760 SSL.Abort_Defer.all;
1762 Result := mutex_lock (S.L'Access);
1763 pragma Assert (Result = 0);
1765 S.State := False;
1767 Result := mutex_unlock (S.L'Access);
1768 pragma Assert (Result = 0);
1770 SSL.Abort_Undefer.all;
1771 end Set_False;
1773 --------------
1774 -- Set_True --
1775 --------------
1777 procedure Set_True (S : in out Suspension_Object) is
1778 Result : Interfaces.C.int;
1780 begin
1781 SSL.Abort_Defer.all;
1783 Result := mutex_lock (S.L'Access);
1784 pragma Assert (Result = 0);
1786 -- If there is already a task waiting on this suspension object then
1787 -- we resume it, leaving the state of the suspension object to False,
1788 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1789 -- the state to True.
1791 if S.Waiting then
1792 S.Waiting := False;
1793 S.State := False;
1795 Result := cond_signal (S.CV'Access);
1796 pragma Assert (Result = 0);
1798 else
1799 S.State := True;
1800 end if;
1802 Result := mutex_unlock (S.L'Access);
1803 pragma Assert (Result = 0);
1805 SSL.Abort_Undefer.all;
1806 end Set_True;
1808 ------------------------
1809 -- Suspend_Until_True --
1810 ------------------------
1812 procedure Suspend_Until_True (S : in out Suspension_Object) is
1813 Result : Interfaces.C.int;
1815 begin
1816 SSL.Abort_Defer.all;
1818 Result := mutex_lock (S.L'Access);
1819 pragma Assert (Result = 0);
1821 if S.Waiting then
1823 -- Program_Error must be raised upon calling Suspend_Until_True
1824 -- if another task is already waiting on that suspension object
1825 -- (RM D.10(10)).
1827 Result := mutex_unlock (S.L'Access);
1828 pragma Assert (Result = 0);
1830 SSL.Abort_Undefer.all;
1832 raise Program_Error;
1834 else
1835 -- Suspend the task if the state is False. Otherwise, the task
1836 -- continues its execution, and the state of the suspension object
1837 -- is set to False (ARM D.10 par. 9).
1839 if S.State then
1840 S.State := False;
1841 else
1842 S.Waiting := True;
1843 Result := cond_wait (S.CV'Access, S.L'Access);
1844 end if;
1846 Result := mutex_unlock (S.L'Access);
1847 pragma Assert (Result = 0);
1849 SSL.Abort_Undefer.all;
1850 end if;
1851 end Suspend_Until_True;
1853 ----------------
1854 -- Check_Exit --
1855 ----------------
1857 function Check_Exit (Self_ID : Task_Id) return Boolean is
1858 begin
1859 -- Check that caller is just holding Global_Task_Lock and no other locks
1861 if Self_ID.Common.LL.Locks = null then
1862 return False;
1863 end if;
1865 -- 2 = Global_Task_Level
1867 if Self_ID.Common.LL.Locks.Level /= 2 then
1868 return False;
1869 end if;
1871 if Self_ID.Common.LL.Locks.Next /= null then
1872 return False;
1873 end if;
1875 -- Check that caller is abort-deferred
1877 if Self_ID.Deferral_Level = 0 then
1878 return False;
1879 end if;
1881 return True;
1882 end Check_Exit;
1884 --------------------
1885 -- Check_No_Locks --
1886 --------------------
1888 function Check_No_Locks (Self_ID : Task_Id) return Boolean is
1889 begin
1890 return Self_ID.Common.LL.Locks = null;
1891 end Check_No_Locks;
1893 ----------------------
1894 -- Environment_Task --
1895 ----------------------
1897 function Environment_Task return Task_Id is
1898 begin
1899 return Environment_Task_Id;
1900 end Environment_Task;
1902 --------------
1903 -- Lock_RTS --
1904 --------------
1906 procedure Lock_RTS is
1907 begin
1908 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1909 end Lock_RTS;
1911 ----------------
1912 -- Unlock_RTS --
1913 ----------------
1915 procedure Unlock_RTS is
1916 begin
1917 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1918 end Unlock_RTS;
1920 ------------------
1921 -- Suspend_Task --
1922 ------------------
1924 function Suspend_Task
1925 (T : ST.Task_Id;
1926 Thread_Self : Thread_Id) return Boolean
1928 begin
1929 if T.Common.LL.Thread /= Thread_Self then
1930 return thr_suspend (T.Common.LL.Thread) = 0;
1931 else
1932 return True;
1933 end if;
1934 end Suspend_Task;
1936 -----------------
1937 -- Resume_Task --
1938 -----------------
1940 function Resume_Task
1941 (T : ST.Task_Id;
1942 Thread_Self : Thread_Id) return Boolean
1944 begin
1945 if T.Common.LL.Thread /= Thread_Self then
1946 return thr_continue (T.Common.LL.Thread) = 0;
1947 else
1948 return True;
1949 end if;
1950 end Resume_Task;
1952 --------------------
1953 -- Stop_All_Tasks --
1954 --------------------
1956 procedure Stop_All_Tasks is
1957 begin
1958 null;
1959 end Stop_All_Tasks;
1961 ---------------
1962 -- Stop_Task --
1963 ---------------
1965 function Stop_Task (T : ST.Task_Id) return Boolean is
1966 pragma Unreferenced (T);
1967 begin
1968 return False;
1969 end Stop_Task;
1971 -------------------
1972 -- Continue_Task --
1973 -------------------
1975 function Continue_Task (T : ST.Task_Id) return Boolean is
1976 pragma Unreferenced (T);
1977 begin
1978 return False;
1979 end Continue_Task;
1981 end System.Task_Primitives.Operations;