* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / ada / s-taprop-solaris.adb
blob9da267ec4773407cc4a0e92b062851934a4073fa
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-2006, 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 GNAT.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 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 : access siginfo_t;
172 Context : 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 : access siginfo_t;
263 Context : 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;
274 begin
275 -- It is not safe to raise an exception when using ZCX and the GCC
276 -- exception handling mechanism.
278 if ZCX_By_Default and then GCC_ZCX_Support then
279 return;
280 end if;
282 if Self_ID.Deferral_Level = 0
283 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
284 and then not Self_ID.Aborting
285 then
286 Self_ID.Aborting := True;
288 -- Make sure signals used for RTS internal purpose are unmasked
290 Result := thr_sigsetmask (SIG_UNBLOCK,
291 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
292 pragma Assert (Result = 0);
294 raise Standard'Abort_Signal;
295 end if;
296 end Abort_Handler;
298 -----------------
299 -- Stack_Guard --
300 -----------------
302 -- The underlying thread system sets a guard page at the
303 -- bottom of a thread stack, so nothing is needed.
305 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
306 pragma Unreferenced (T);
307 pragma Unreferenced (On);
308 begin
309 null;
310 end Stack_Guard;
312 -------------------
313 -- Get_Thread_Id --
314 -------------------
316 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
317 begin
318 return T.Common.LL.Thread;
319 end Get_Thread_Id;
321 ----------------
322 -- Initialize --
323 ----------------
325 procedure Initialize (Environment_Task : ST.Task_Id) is
326 act : aliased struct_sigaction;
327 old_act : aliased struct_sigaction;
328 Tmp_Set : aliased sigset_t;
329 Result : Interfaces.C.int;
331 procedure Configure_Processors;
332 -- Processors configuration
333 -- The user can specify a processor which the program should run
334 -- on to emulate a single-processor system. This can be easily
335 -- done by setting environment variable GNAT_PROCESSOR to one of
336 -- the following :
338 -- -2 : use the default configuration (run the program on all
339 -- available processors) - this is the same as having
340 -- GNAT_PROCESSOR unset
341 -- -1 : let the RTS choose one processor and run the program on
342 -- that processor
343 -- 0 .. Last_Proc : run the program on the specified processor
345 -- Last_Proc is equal to the value of the system variable
346 -- _SC_NPROCESSORS_CONF, minus one.
348 procedure Configure_Processors is
349 Proc_Acc : constant GNAT.OS_Lib.String_Access :=
350 GNAT.OS_Lib.Getenv ("GNAT_PROCESSOR");
351 Proc : aliased processorid_t; -- User processor #
352 Last_Proc : processorid_t; -- Last processor #
354 begin
355 if Proc_Acc.all'Length /= 0 then
357 -- Environment variable is defined
359 Last_Proc := Num_Procs - 1;
361 if Last_Proc /= -1 then
362 Proc := processorid_t'Value (Proc_Acc.all);
364 if Proc <= -2 or else Proc > Last_Proc then
365 -- Use the default configuration
366 null;
367 elsif Proc = -1 then
368 -- Choose a processor
370 Result := 0;
372 while Proc < Last_Proc loop
373 Proc := Proc + 1;
374 Result := p_online (Proc, PR_STATUS);
375 exit when Result = PR_ONLINE;
376 end loop;
378 pragma Assert (Result = PR_ONLINE);
379 Result := processor_bind (P_PID, P_MYID, Proc, null);
380 pragma Assert (Result = 0);
382 else
383 -- Use user processor
385 Result := processor_bind (P_PID, P_MYID, Proc, null);
386 pragma Assert (Result = 0);
387 end if;
388 end if;
389 end if;
391 exception
392 when Constraint_Error =>
394 -- Illegal environment variable GNAT_PROCESSOR - ignored
396 null;
397 end Configure_Processors;
399 function State
400 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
401 pragma Import (C, State, "__gnat_get_interrupt_state");
402 -- Get interrupt state. Defined in a-init.c
403 -- The input argument is the interrupt number,
404 -- and the result is one of the following:
406 Default : constant Character := 's';
407 -- 'n' this interrupt not set by any Interrupt_State pragma
408 -- 'u' Interrupt_State pragma set state to User
409 -- 'r' Interrupt_State pragma set state to Runtime
410 -- 's' Interrupt_State pragma set state to System (use "default"
411 -- system handler)
413 -- Start of processing for Initialize
415 begin
416 Environment_Task_Id := Environment_Task;
418 Interrupt_Management.Initialize;
420 -- Prepare the set of signals that should unblocked in all tasks
422 Result := sigemptyset (Unblocked_Signal_Mask'Access);
423 pragma Assert (Result = 0);
425 for J in Interrupt_Management.Interrupt_ID loop
426 if System.Interrupt_Management.Keep_Unmasked (J) then
427 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
428 pragma Assert (Result = 0);
429 end if;
430 end loop;
432 if Dispatching_Policy = 'F' then
433 declare
434 Result : Interfaces.C.long;
435 Class_Info : aliased struct_pcinfo;
436 Secs, Nsecs : Interfaces.C.long;
438 begin
439 -- If a pragma Time_Slice is specified, takes the value in account
441 if Time_Slice_Val > 0 then
443 -- Convert Time_Slice_Val (microseconds) into seconds and
444 -- nanoseconds
446 Secs := Interfaces.C.long (Time_Slice_Val / 1_000_000);
447 Nsecs :=
448 Interfaces.C.long ((Time_Slice_Val rem 1_000_000) * 1_000);
450 -- Otherwise, default to no time slicing (i.e run until blocked)
452 else
453 Secs := RT_TQINF;
454 Nsecs := RT_TQINF;
455 end if;
457 -- Get the real time class id
459 Class_Info.pc_clname (1) := 'R';
460 Class_Info.pc_clname (2) := 'T';
461 Class_Info.pc_clname (3) := ASCII.NUL;
463 Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_GETCID,
464 Class_Info'Address);
466 -- Request the real time class
468 Prio_Param.pc_cid := Class_Info.pc_cid;
469 Prio_Param.rt_pri := pri_t (Class_Info.rt_maxpri);
470 Prio_Param.rt_tqsecs := Secs;
471 Prio_Param.rt_tqnsecs := Nsecs;
473 Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_SETPARMS,
474 Prio_Param'Address);
476 Using_Real_Time_Class := Result /= -1;
477 end;
478 end if;
480 Specific.Initialize (Environment_Task);
482 -- The following is done in Enter_Task, but this is too late for the
483 -- Environment Task, since we need to call Self in Check_Locks when
484 -- the run time is compiled with assertions on.
486 Specific.Set (Environment_Task);
488 -- Initialize the lock used to synchronize chain of all ATCBs
490 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
492 Enter_Task (Environment_Task);
494 -- Install the abort-signal handler
496 if State (System.Interrupt_Management.Abort_Task_Interrupt)
497 /= Default
498 then
499 -- Set sa_flags to SA_NODEFER so that during the handler execution
500 -- we do not change the Signal_Mask to be masked for the Abort_Signal
501 -- This is a temporary fix to the problem that the Signal_Mask is
502 -- not restored after the exception (longjmp) from the handler.
503 -- The right fix should be made in sigsetjmp so that we save
504 -- the Signal_Set and restore it after a longjmp.
505 -- In that case, this field should be changed back to 0. ???
507 act.sa_flags := 16;
509 act.sa_handler := Abort_Handler'Address;
510 Result := sigemptyset (Tmp_Set'Access);
511 pragma Assert (Result = 0);
512 act.sa_mask := Tmp_Set;
514 Result :=
515 sigaction (
516 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
517 act'Unchecked_Access,
518 old_act'Unchecked_Access);
519 pragma Assert (Result = 0);
520 end if;
522 Configure_Processors;
523 end Initialize;
525 ---------------------
526 -- Initialize_Lock --
527 ---------------------
529 -- Note: mutexes and cond_variables needed per-task basis are
530 -- initialized in Initialize_TCB and the Storage_Error is
531 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
532 -- used in RTS is initialized before any status change of RTS.
533 -- Therefore rasing Storage_Error in the following routines
534 -- should be able to be handled safely.
536 procedure Initialize_Lock
537 (Prio : System.Any_Priority;
538 L : access Lock)
540 Result : Interfaces.C.int;
542 begin
543 pragma Assert (Check_Initialize_Lock (Lock_Ptr (L), PO_Level));
545 if Priority_Ceiling_Emulation then
546 L.Ceiling := Prio;
547 end if;
549 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
550 pragma Assert (Result = 0 or else Result = ENOMEM);
552 if Result = ENOMEM then
553 raise Storage_Error with "Failed to allocate a lock";
554 end if;
555 end Initialize_Lock;
557 procedure Initialize_Lock
558 (L : access RTS_Lock;
559 Level : Lock_Level)
561 Result : Interfaces.C.int;
563 begin
564 pragma Assert (Check_Initialize_Lock
565 (To_Lock_Ptr (RTS_Lock_Ptr (L)), Level));
566 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
567 pragma Assert (Result = 0 or else Result = ENOMEM);
569 if Result = ENOMEM then
570 raise Storage_Error with "Failed to allocate a lock";
571 end if;
572 end Initialize_Lock;
574 -------------------
575 -- Finalize_Lock --
576 -------------------
578 procedure Finalize_Lock (L : access Lock) is
579 Result : Interfaces.C.int;
581 begin
582 pragma Assert (Check_Finalize_Lock (Lock_Ptr (L)));
583 Result := mutex_destroy (L.L'Access);
584 pragma Assert (Result = 0);
585 end Finalize_Lock;
587 procedure Finalize_Lock (L : access RTS_Lock) is
588 Result : Interfaces.C.int;
590 begin
591 pragma Assert (Check_Finalize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
592 Result := mutex_destroy (L.L'Access);
593 pragma Assert (Result = 0);
594 end Finalize_Lock;
596 ----------------
597 -- Write_Lock --
598 ----------------
600 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
601 Result : Interfaces.C.int;
603 begin
604 pragma Assert (Check_Lock (Lock_Ptr (L)));
606 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
607 declare
608 Self_Id : constant Task_Id := Self;
609 Saved_Priority : System.Any_Priority;
611 begin
612 if Self_Id.Common.LL.Active_Priority > L.Ceiling then
613 Ceiling_Violation := True;
614 return;
615 end if;
617 Saved_Priority := Self_Id.Common.LL.Active_Priority;
619 if Self_Id.Common.LL.Active_Priority < L.Ceiling then
620 Set_Priority (Self_Id, L.Ceiling);
621 end if;
623 Result := mutex_lock (L.L'Access);
624 pragma Assert (Result = 0);
625 Ceiling_Violation := False;
627 L.Saved_Priority := Saved_Priority;
628 end;
630 else
631 Result := mutex_lock (L.L'Access);
632 pragma Assert (Result = 0);
633 Ceiling_Violation := False;
634 end if;
636 pragma Assert (Record_Lock (Lock_Ptr (L)));
637 end Write_Lock;
639 procedure Write_Lock
640 (L : access RTS_Lock;
641 Global_Lock : Boolean := False)
643 Result : Interfaces.C.int;
645 begin
646 if not Single_Lock or else Global_Lock then
647 pragma Assert (Check_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
648 Result := mutex_lock (L.L'Access);
649 pragma Assert (Result = 0);
650 pragma Assert (Record_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
651 end if;
652 end Write_Lock;
654 procedure Write_Lock (T : Task_Id) is
655 Result : Interfaces.C.int;
657 begin
658 if not Single_Lock then
659 pragma Assert (Check_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
660 Result := mutex_lock (T.Common.LL.L.L'Access);
661 pragma Assert (Result = 0);
662 pragma Assert (Record_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
663 end if;
664 end Write_Lock;
666 ---------------
667 -- Read_Lock --
668 ---------------
670 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
671 begin
672 Write_Lock (L, Ceiling_Violation);
673 end Read_Lock;
675 ------------
676 -- Unlock --
677 ------------
679 procedure Unlock (L : access Lock) is
680 Result : Interfaces.C.int;
682 begin
683 pragma Assert (Check_Unlock (Lock_Ptr (L)));
685 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
686 declare
687 Self_Id : constant Task_Id := Self;
689 begin
690 Result := mutex_unlock (L.L'Access);
691 pragma Assert (Result = 0);
693 if Self_Id.Common.LL.Active_Priority > L.Saved_Priority then
694 Set_Priority (Self_Id, L.Saved_Priority);
695 end if;
696 end;
697 else
698 Result := mutex_unlock (L.L'Access);
699 pragma Assert (Result = 0);
700 end if;
701 end Unlock;
703 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
704 Result : Interfaces.C.int;
705 begin
706 if not Single_Lock or else Global_Lock then
707 pragma Assert (Check_Unlock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
708 Result := mutex_unlock (L.L'Access);
709 pragma Assert (Result = 0);
710 end if;
711 end Unlock;
713 procedure Unlock (T : Task_Id) is
714 Result : Interfaces.C.int;
715 begin
716 if not Single_Lock then
717 pragma Assert (Check_Unlock (To_Lock_Ptr (T.Common.LL.L'Access)));
718 Result := mutex_unlock (T.Common.LL.L.L'Access);
719 pragma Assert (Result = 0);
720 end if;
721 end Unlock;
723 -- For the time delay implementation, we need to make sure we
724 -- achieve following criteria:
726 -- 1) We have to delay at least for the amount requested.
727 -- 2) We have to give up CPU even though the actual delay does not
728 -- result in blocking.
729 -- 3) Except for restricted run-time systems that do not support
730 -- ATC or task abort, the delay must be interrupted by the
731 -- abort_task operation.
732 -- 4) The implementation has to be efficient so that the delay overhead
733 -- is relatively cheap.
734 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
735 -- requirement we still want to provide the effect in all cases.
736 -- The reason is that users may want to use short delays to implement
737 -- their own scheduling effect in the absence of language provided
738 -- scheduling policies.
740 ---------------------
741 -- Monotonic_Clock --
742 ---------------------
744 function Monotonic_Clock return Duration is
745 TS : aliased timespec;
746 Result : Interfaces.C.int;
747 begin
748 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
749 pragma Assert (Result = 0);
750 return To_Duration (TS);
751 end Monotonic_Clock;
753 -------------------
754 -- RT_Resolution --
755 -------------------
757 function RT_Resolution return Duration is
758 begin
759 return 10#1.0#E-6;
760 end RT_Resolution;
762 -----------
763 -- Yield --
764 -----------
766 procedure Yield (Do_Yield : Boolean := True) is
767 begin
768 if Do_Yield then
769 System.OS_Interface.thr_yield;
770 end if;
771 end Yield;
773 -----------
774 -- Self ---
775 -----------
777 function Self return Task_Id renames Specific.Self;
779 ------------------
780 -- Set_Priority --
781 ------------------
783 procedure Set_Priority
784 (T : Task_Id;
785 Prio : System.Any_Priority;
786 Loss_Of_Inheritance : Boolean := False)
788 pragma Unreferenced (Loss_Of_Inheritance);
790 Result : Interfaces.C.int;
791 pragma Unreferenced (Result);
793 Param : aliased struct_pcparms;
795 use Task_Info;
797 begin
798 T.Common.Current_Priority := Prio;
800 if Priority_Ceiling_Emulation then
801 T.Common.LL.Active_Priority := Prio;
802 end if;
804 if Using_Real_Time_Class then
805 Param.pc_cid := Prio_Param.pc_cid;
806 Param.rt_pri := pri_t (Prio);
807 Param.rt_tqsecs := Prio_Param.rt_tqsecs;
808 Param.rt_tqnsecs := Prio_Param.rt_tqnsecs;
810 Result := Interfaces.C.int (
811 priocntl (PC_VERSION, P_LWPID, T.Common.LL.LWP, PC_SETPARMS,
812 Param'Address));
814 else
815 if T.Common.Task_Info /= null
816 and then not T.Common.Task_Info.Bound_To_LWP
817 then
818 -- The task is not bound to a LWP, so use thr_setprio
820 Result :=
821 thr_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
823 else
824 -- The task is bound to a LWP, use priocntl
825 -- ??? TBD
827 null;
828 end if;
829 end if;
830 end Set_Priority;
832 ------------------
833 -- Get_Priority --
834 ------------------
836 function Get_Priority (T : Task_Id) return System.Any_Priority is
837 begin
838 return T.Common.Current_Priority;
839 end Get_Priority;
841 ----------------
842 -- Enter_Task --
843 ----------------
845 procedure Enter_Task (Self_ID : Task_Id) is
846 Result : Interfaces.C.int;
847 Proc : processorid_t; -- User processor #
848 Last_Proc : processorid_t; -- Last processor #
850 use System.Task_Info;
851 begin
852 Self_ID.Common.LL.Thread := thr_self;
854 Self_ID.Common.LL.LWP := lwp_self;
856 if Self_ID.Common.Task_Info /= null then
857 if Self_ID.Common.Task_Info.New_LWP
858 and then Self_ID.Common.Task_Info.CPU /= CPU_UNCHANGED
859 then
860 Last_Proc := Num_Procs - 1;
862 if Self_ID.Common.Task_Info.CPU = ANY_CPU then
863 Result := 0;
864 Proc := 0;
866 while Proc < Last_Proc loop
867 Result := p_online (Proc, PR_STATUS);
868 exit when Result = PR_ONLINE;
869 Proc := Proc + 1;
870 end loop;
872 Result := processor_bind (P_LWPID, P_MYID, Proc, null);
873 pragma Assert (Result = 0);
875 else
876 -- Use specified processor
878 if Self_ID.Common.Task_Info.CPU < 0
879 or else Self_ID.Common.Task_Info.CPU > Last_Proc
880 then
881 raise Invalid_CPU_Number;
882 end if;
884 Result := processor_bind
885 (P_LWPID, P_MYID, Self_ID.Common.Task_Info.CPU, null);
886 pragma Assert (Result = 0);
887 end if;
888 end if;
889 end if;
891 Specific.Set (Self_ID);
893 -- We need the above code even if we do direct fetch of Task_Id in Self
894 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
896 Lock_RTS;
898 for J in Known_Tasks'Range loop
899 if Known_Tasks (J) = null then
900 Known_Tasks (J) := Self_ID;
901 Self_ID.Known_Tasks_Index := J;
902 exit;
903 end if;
904 end loop;
906 Unlock_RTS;
907 end Enter_Task;
909 --------------
910 -- New_ATCB --
911 --------------
913 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
914 begin
915 return new Ada_Task_Control_Block (Entry_Num);
916 end New_ATCB;
918 -------------------
919 -- Is_Valid_Task --
920 -------------------
922 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
924 -----------------------------
925 -- Register_Foreign_Thread --
926 -----------------------------
928 function Register_Foreign_Thread return Task_Id is
929 begin
930 if Is_Valid_Task then
931 return Self;
932 else
933 return Register_Foreign_Thread (thr_self);
934 end if;
935 end Register_Foreign_Thread;
937 --------------------
938 -- Initialize_TCB --
939 --------------------
941 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
942 Result : Interfaces.C.int := 0;
944 begin
945 -- Give the task a unique serial number
947 Self_ID.Serial_Number := Next_Serial_Number;
948 Next_Serial_Number := Next_Serial_Number + 1;
949 pragma Assert (Next_Serial_Number /= 0);
951 Self_ID.Common.LL.Thread := To_thread_t (-1);
953 if not Single_Lock then
954 Result := mutex_init
955 (Self_ID.Common.LL.L.L'Access, USYNC_THREAD, System.Null_Address);
956 Self_ID.Common.LL.L.Level :=
957 Private_Task_Serial_Number (Self_ID.Serial_Number);
958 pragma Assert (Result = 0 or else Result = ENOMEM);
959 end if;
961 if Result = 0 then
962 Result := cond_init (Self_ID.Common.LL.CV'Access, USYNC_THREAD, 0);
963 pragma Assert (Result = 0 or else Result = ENOMEM);
964 end if;
966 if Result = 0 then
967 Succeeded := True;
968 else
969 if not Single_Lock then
970 Result := mutex_destroy (Self_ID.Common.LL.L.L'Access);
971 pragma Assert (Result = 0);
972 end if;
974 Succeeded := False;
975 end if;
976 end Initialize_TCB;
978 -----------------
979 -- Create_Task --
980 -----------------
982 procedure Create_Task
983 (T : Task_Id;
984 Wrapper : System.Address;
985 Stack_Size : System.Parameters.Size_Type;
986 Priority : System.Any_Priority;
987 Succeeded : out Boolean)
989 pragma Unreferenced (Priority);
991 Result : Interfaces.C.int;
992 Adjusted_Stack_Size : Interfaces.C.size_t;
993 Opts : Interfaces.C.int := THR_DETACHED;
995 Page_Size : constant System.Parameters.Size_Type := 4096;
996 -- This constant is for reserving extra space at the
997 -- end of the stack, which can be used by the stack
998 -- checking as guard page. The idea is that we need
999 -- to have at least Stack_Size bytes available for
1000 -- actual use.
1002 use System.Task_Info;
1004 begin
1005 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size + Page_Size);
1007 -- Since the initial signal mask of a thread is inherited from the
1008 -- creator, and the Environment task has all its signals masked, we
1009 -- do not need to manipulate caller's signal mask at this point.
1010 -- All tasks in RTS will have All_Tasks_Mask initially.
1012 if T.Common.Task_Info /= null then
1013 if T.Common.Task_Info.New_LWP then
1014 Opts := Opts + THR_NEW_LWP;
1015 end if;
1017 if T.Common.Task_Info.Bound_To_LWP then
1018 Opts := Opts + THR_BOUND;
1019 end if;
1021 else
1022 Opts := THR_DETACHED + THR_BOUND;
1023 end if;
1025 Result := thr_create
1026 (System.Null_Address,
1027 Adjusted_Stack_Size,
1028 Thread_Body_Access (Wrapper),
1029 To_Address (T),
1030 Opts,
1031 T.Common.LL.Thread'Access);
1033 Succeeded := Result = 0;
1034 pragma Assert
1035 (Result = 0
1036 or else Result = ENOMEM
1037 or else Result = EAGAIN);
1038 end Create_Task;
1040 ------------------
1041 -- Finalize_TCB --
1042 ------------------
1044 procedure Finalize_TCB (T : Task_Id) is
1045 Result : Interfaces.C.int;
1046 Tmp : Task_Id := T;
1047 Is_Self : constant Boolean := T = Self;
1049 procedure Free is new
1050 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1052 begin
1053 T.Common.LL.Thread := To_thread_t (0);
1055 if not Single_Lock then
1056 Result := mutex_destroy (T.Common.LL.L.L'Access);
1057 pragma Assert (Result = 0);
1058 end if;
1060 Result := cond_destroy (T.Common.LL.CV'Access);
1061 pragma Assert (Result = 0);
1063 if T.Known_Tasks_Index /= -1 then
1064 Known_Tasks (T.Known_Tasks_Index) := null;
1065 end if;
1067 Free (Tmp);
1069 if Is_Self then
1070 Specific.Set (null);
1071 end if;
1072 end Finalize_TCB;
1074 ---------------
1075 -- Exit_Task --
1076 ---------------
1078 -- This procedure must be called with abort deferred.
1079 -- It can no longer call Self or access
1080 -- the current task's ATCB, since the ATCB has been deallocated.
1082 procedure Exit_Task is
1083 begin
1084 Specific.Set (null);
1085 end Exit_Task;
1087 ----------------
1088 -- Abort_Task --
1089 ----------------
1091 procedure Abort_Task (T : Task_Id) is
1092 Result : Interfaces.C.int;
1093 begin
1094 pragma Assert (T /= Self);
1096 Result := thr_kill (T.Common.LL.Thread,
1097 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1098 pragma Assert (Result = 0);
1099 end Abort_Task;
1101 -----------
1102 -- Sleep --
1103 -----------
1105 procedure Sleep
1106 (Self_ID : Task_Id;
1107 Reason : Task_States)
1109 Result : Interfaces.C.int;
1111 begin
1112 pragma Assert (Check_Sleep (Reason));
1114 if Dynamic_Priority_Support
1115 and then Self_ID.Pending_Priority_Change
1116 then
1117 Self_ID.Pending_Priority_Change := False;
1118 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
1119 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
1120 end if;
1122 if Single_Lock then
1123 Result := cond_wait
1124 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock.L'Access);
1125 else
1126 Result := cond_wait
1127 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L.L'Access);
1128 end if;
1130 pragma Assert (Record_Wakeup
1131 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1132 pragma Assert (Result = 0 or else Result = EINTR);
1133 end Sleep;
1135 -- Note that we are relying heaviliy here on GNAT represting Calendar.Time,
1136 -- System.Real_Time.Time, Duration, System.Real_Time.Time_Span in the same
1137 -- way, i.e., as a 64-bit count of nanoseconds.
1139 -- This allows us to always pass the timeout value as a Duration
1141 -- ???
1142 -- We are taking liberties here with the semantics of the delays. That is,
1143 -- we make no distinction between delays on the Calendar clock and delays
1144 -- on the Real_Time clock. That is technically incorrect, if the Calendar
1145 -- clock happens to be reset or adjusted. To solve this defect will require
1146 -- modification to the compiler interface, so that it can pass through more
1147 -- information, to tell us here which clock to use!
1149 -- cond_timedwait will return if any of the following happens:
1150 -- 1) some other task did cond_signal on this condition variable
1151 -- In this case, the return value is 0
1152 -- 2) the call just returned, for no good reason
1153 -- This is called a "spurious wakeup".
1154 -- In this case, the return value may also be 0.
1155 -- 3) the time delay expires
1156 -- In this case, the return value is ETIME
1157 -- 4) this task received a signal, which was handled by some
1158 -- handler procedure, and now the thread is resuming execution
1159 -- UNIX calls this an "interrupted" system call.
1160 -- In this case, the return value is EINTR
1162 -- If the cond_timedwait returns 0 or EINTR, it is still possible that the
1163 -- time has actually expired, and by chance a signal or cond_signal
1164 -- occurred at around the same time.
1166 -- We have also observed that on some OS's the value ETIME will be
1167 -- returned, but the clock will show that the full delay has not yet
1168 -- expired.
1170 -- For these reasons, we need to check the clock after return from
1171 -- cond_timedwait. If the time has expired, we will set Timedout = True.
1173 -- This check might be omitted for systems on which the cond_timedwait()
1174 -- never returns early or wakes up spuriously.
1176 -- Annex D requires that completion of a delay cause the task to go to the
1177 -- end of its priority queue, regardless of whether the task actually was
1178 -- suspended by the delay. Since cond_timedwait does not do this on
1179 -- Solaris, we add a call to thr_yield at the end. We might do this at the
1180 -- beginning, instead, but then the round-robin effect would not be the
1181 -- same; the delayed task would be ahead of other tasks of the same
1182 -- priority that awoke while it was sleeping.
1184 -- For Timed_Sleep, we are expecting possible cond_signals to indicate
1185 -- other events (e.g., completion of a RV or completion of the abortable
1186 -- part of an async. select), we want to always return if interrupted. The
1187 -- caller will be responsible for checking the task state to see whether
1188 -- the wakeup was spurious, and to go back to sleep again in that case. We
1189 -- don't need to check for pending abort or priority change on the way in
1190 -- our out; that is the caller's responsibility.
1192 -- For Timed_Delay, we are not expecting any cond_signals or other
1193 -- interruptions, except for priority changes and aborts. Therefore, we
1194 -- don't want to return unless the delay has actually expired, or the call
1195 -- has been aborted. In this case, since we want to implement the entire
1196 -- delay statement semantics, we do need to check for pending abort and
1197 -- priority changes. We can quietly handle priority changes inside the
1198 -- procedure, since there is no entry-queue reordering involved.
1200 -----------------
1201 -- Timed_Sleep --
1202 -----------------
1204 procedure Timed_Sleep
1205 (Self_ID : Task_Id;
1206 Time : Duration;
1207 Mode : ST.Delay_Modes;
1208 Reason : System.Tasking.Task_States;
1209 Timedout : out Boolean;
1210 Yielded : out Boolean)
1212 Check_Time : constant Duration := Monotonic_Clock;
1213 Abs_Time : Duration;
1214 Request : aliased timespec;
1215 Result : Interfaces.C.int;
1217 begin
1218 pragma Assert (Check_Sleep (Reason));
1219 Timedout := True;
1220 Yielded := False;
1222 if Mode = Relative then
1223 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
1224 else
1225 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1226 end if;
1228 if Abs_Time > Check_Time then
1229 Request := To_Timespec (Abs_Time);
1231 loop
1232 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
1233 or else (Dynamic_Priority_Support and then
1234 Self_ID.Pending_Priority_Change);
1236 if Single_Lock then
1237 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1238 Single_RTS_Lock.L'Access, Request'Access);
1239 else
1240 Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
1241 Self_ID.Common.LL.L.L'Access, Request'Access);
1242 end if;
1244 Yielded := True;
1246 exit when Abs_Time <= Monotonic_Clock;
1248 if Result = 0 or Result = EINTR then
1250 -- Somebody may have called Wakeup for us
1252 Timedout := False;
1253 exit;
1254 end if;
1256 pragma Assert (Result = ETIME);
1257 end loop;
1258 end if;
1260 pragma Assert (Record_Wakeup
1261 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1262 end Timed_Sleep;
1264 -----------------
1265 -- Timed_Delay --
1266 -----------------
1268 procedure Timed_Delay
1269 (Self_ID : Task_Id;
1270 Time : Duration;
1271 Mode : ST.Delay_Modes)
1273 Check_Time : constant Duration := Monotonic_Clock;
1274 Abs_Time : Duration;
1275 Request : aliased timespec;
1276 Result : Interfaces.C.int;
1277 Yielded : Boolean := False;
1279 begin
1280 if Single_Lock then
1281 Lock_RTS;
1282 end if;
1284 Write_Lock (Self_ID);
1286 if Mode = Relative then
1287 Abs_Time := Time + Check_Time;
1288 else
1289 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
1290 end if;
1292 if Abs_Time > Check_Time then
1293 Request := To_Timespec (Abs_Time);
1294 Self_ID.Common.State := Delay_Sleep;
1296 pragma Assert (Check_Sleep (Delay_Sleep));
1298 loop
1299 if Dynamic_Priority_Support and then
1300 Self_ID.Pending_Priority_Change then
1301 Self_ID.Pending_Priority_Change := False;
1302 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
1303 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
1304 end if;
1306 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1308 if Single_Lock then
1309 Result := cond_timedwait
1310 (Self_ID.Common.LL.CV'Access,
1311 Single_RTS_Lock.L'Access,
1312 Request'Access);
1313 else
1314 Result := cond_timedwait
1315 (Self_ID.Common.LL.CV'Access,
1316 Self_ID.Common.LL.L.L'Access,
1317 Request'Access);
1318 end if;
1320 Yielded := True;
1322 exit when Abs_Time <= Monotonic_Clock;
1324 pragma Assert (Result = 0 or else
1325 Result = ETIME or else
1326 Result = EINTR);
1327 end loop;
1329 pragma Assert (Record_Wakeup
1330 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Delay_Sleep));
1332 Self_ID.Common.State := Runnable;
1333 end if;
1335 Unlock (Self_ID);
1337 if Single_Lock then
1338 Unlock_RTS;
1339 end if;
1341 if not Yielded then
1342 thr_yield;
1343 end if;
1344 end Timed_Delay;
1346 ------------
1347 -- Wakeup --
1348 ------------
1350 procedure Wakeup
1351 (T : Task_Id;
1352 Reason : Task_States)
1354 Result : Interfaces.C.int;
1356 begin
1357 pragma Assert (Check_Wakeup (T, Reason));
1358 Result := cond_signal (T.Common.LL.CV'Access);
1359 pragma Assert (Result = 0);
1360 end Wakeup;
1362 ---------------------------
1363 -- Check_Initialize_Lock --
1364 ---------------------------
1366 -- The following code is intended to check some of the invariant
1367 -- assertions related to lock usage, on which we depend.
1369 function Check_Initialize_Lock
1370 (L : Lock_Ptr;
1371 Level : Lock_Level) return Boolean
1373 Self_ID : constant Task_Id := Self;
1375 begin
1376 -- Check that caller is abort-deferred
1378 if Self_ID.Deferral_Level = 0 then
1379 return False;
1380 end if;
1382 -- Check that the lock is not yet initialized
1384 if L.Level /= 0 then
1385 return False;
1386 end if;
1388 L.Level := Lock_Level'Pos (Level) + 1;
1389 return True;
1390 end Check_Initialize_Lock;
1392 ----------------
1393 -- Check_Lock --
1394 ----------------
1396 function Check_Lock (L : Lock_Ptr) return Boolean is
1397 Self_ID : constant Task_Id := Self;
1398 P : Lock_Ptr;
1400 begin
1401 -- Check that the argument is not null
1403 if L = null then
1404 return False;
1405 end if;
1407 -- Check that L is not frozen
1409 if L.Frozen then
1410 return False;
1411 end if;
1413 -- Check that caller is abort-deferred
1415 if Self_ID.Deferral_Level = 0 then
1416 return False;
1417 end if;
1419 -- Check that caller is not holding this lock already
1421 if L.Owner = To_Owner_ID (To_Address (Self_ID)) then
1422 return False;
1423 end if;
1425 if Single_Lock then
1426 return True;
1427 end if;
1429 -- Check that TCB lock order rules are satisfied
1431 P := Self_ID.Common.LL.Locks;
1432 if P /= null then
1433 if P.Level >= L.Level
1434 and then (P.Level > 2 or else L.Level > 2)
1435 then
1436 return False;
1437 end if;
1438 end if;
1440 return True;
1441 end Check_Lock;
1443 -----------------
1444 -- Record_Lock --
1445 -----------------
1447 function Record_Lock (L : Lock_Ptr) return Boolean is
1448 Self_ID : constant Task_Id := Self;
1449 P : Lock_Ptr;
1451 begin
1452 Lock_Count := Lock_Count + 1;
1454 -- There should be no owner for this lock at this point
1456 if L.Owner /= null then
1457 return False;
1458 end if;
1460 -- Record new owner
1462 L.Owner := To_Owner_ID (To_Address (Self_ID));
1464 if Single_Lock then
1465 return True;
1466 end if;
1468 -- Check that TCB lock order rules are satisfied
1470 P := Self_ID.Common.LL.Locks;
1472 if P /= null then
1473 L.Next := P;
1474 end if;
1476 Self_ID.Common.LL.Locking := null;
1477 Self_ID.Common.LL.Locks := L;
1478 return True;
1479 end Record_Lock;
1481 -----------------
1482 -- Check_Sleep --
1483 -----------------
1485 function Check_Sleep (Reason : Task_States) return Boolean is
1486 pragma Unreferenced (Reason);
1488 Self_ID : constant Task_Id := Self;
1489 P : Lock_Ptr;
1491 begin
1492 -- Check that caller is abort-deferred
1494 if Self_ID.Deferral_Level = 0 then
1495 return False;
1496 end if;
1498 if Single_Lock then
1499 return True;
1500 end if;
1502 -- Check that caller is holding own lock, on top of list
1504 if Self_ID.Common.LL.Locks /=
1505 To_Lock_Ptr (Self_ID.Common.LL.L'Access)
1506 then
1507 return False;
1508 end if;
1510 -- Check that TCB lock order rules are satisfied
1512 if Self_ID.Common.LL.Locks.Next /= null then
1513 return False;
1514 end if;
1516 Self_ID.Common.LL.L.Owner := null;
1517 P := Self_ID.Common.LL.Locks;
1518 Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
1519 P.Next := null;
1520 return True;
1521 end Check_Sleep;
1523 -------------------
1524 -- Record_Wakeup --
1525 -------------------
1527 function Record_Wakeup
1528 (L : Lock_Ptr;
1529 Reason : Task_States) return Boolean
1531 pragma Unreferenced (Reason);
1533 Self_ID : constant Task_Id := Self;
1534 P : Lock_Ptr;
1536 begin
1537 -- Record new owner
1539 L.Owner := To_Owner_ID (To_Address (Self_ID));
1541 if Single_Lock then
1542 return True;
1543 end if;
1545 -- Check that TCB lock order rules are satisfied
1547 P := Self_ID.Common.LL.Locks;
1549 if P /= null then
1550 L.Next := P;
1551 end if;
1553 Self_ID.Common.LL.Locking := null;
1554 Self_ID.Common.LL.Locks := L;
1555 return True;
1556 end Record_Wakeup;
1558 ------------------
1559 -- Check_Wakeup --
1560 ------------------
1562 function Check_Wakeup
1563 (T : Task_Id;
1564 Reason : Task_States) return Boolean
1566 Self_ID : constant Task_Id := Self;
1568 begin
1569 -- Is caller holding T's lock?
1571 if T.Common.LL.L.Owner /= To_Owner_ID (To_Address (Self_ID)) then
1572 return False;
1573 end if;
1575 -- Are reasons for wakeup and sleep consistent?
1577 if T.Common.State /= Reason then
1578 return False;
1579 end if;
1581 return True;
1582 end Check_Wakeup;
1584 ------------------
1585 -- Check_Unlock --
1586 ------------------
1588 function Check_Unlock (L : Lock_Ptr) return Boolean is
1589 Self_ID : constant Task_Id := Self;
1590 P : Lock_Ptr;
1592 begin
1593 Unlock_Count := Unlock_Count + 1;
1595 if L = null then
1596 return False;
1597 end if;
1599 if L.Buddy /= null then
1600 return False;
1601 end if;
1603 if L.Level = 4 then
1604 Check_Count := Unlock_Count;
1605 end if;
1607 if Unlock_Count - Check_Count > 1000 then
1608 Check_Count := Unlock_Count;
1609 end if;
1611 -- Check that caller is abort-deferred
1613 if Self_ID.Deferral_Level = 0 then
1614 return False;
1615 end if;
1617 -- Check that caller is holding this lock, on top of list
1619 if Self_ID.Common.LL.Locks /= L then
1620 return False;
1621 end if;
1623 -- Record there is no owner now
1625 L.Owner := null;
1626 P := Self_ID.Common.LL.Locks;
1627 Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
1628 P.Next := null;
1629 return True;
1630 end Check_Unlock;
1632 --------------------
1633 -- Check_Finalize --
1634 --------------------
1636 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean is
1637 Self_ID : constant Task_Id := Self;
1639 begin
1640 -- Check that caller is abort-deferred
1642 if Self_ID.Deferral_Level = 0 then
1643 return False;
1644 end if;
1646 -- Check that no one is holding this lock
1648 if L.Owner /= null then
1649 return False;
1650 end if;
1652 L.Frozen := True;
1653 return True;
1654 end Check_Finalize_Lock;
1656 ----------------
1657 -- Initialize --
1658 ----------------
1660 procedure Initialize (S : in out Suspension_Object) is
1661 Result : Interfaces.C.int;
1662 begin
1663 -- Initialize internal state. It is always initialized to False (ARM
1664 -- D.10 par. 6).
1666 S.State := False;
1667 S.Waiting := False;
1669 -- Initialize internal mutex
1671 Result := mutex_init (S.L'Access, USYNC_THREAD, System.Null_Address);
1672 pragma Assert (Result = 0 or else Result = ENOMEM);
1674 if Result = ENOMEM then
1675 raise Storage_Error with "Failed to allocate a lock";
1676 end if;
1678 -- Initialize internal condition variable
1680 Result := cond_init (S.CV'Access, USYNC_THREAD, 0);
1681 pragma Assert (Result = 0 or else Result = ENOMEM);
1683 if Result /= 0 then
1684 Result := mutex_destroy (S.L'Access);
1685 pragma Assert (Result = 0);
1687 if Result = ENOMEM then
1688 raise Storage_Error;
1689 end if;
1690 end if;
1691 end Initialize;
1693 --------------
1694 -- Finalize --
1695 --------------
1697 procedure Finalize (S : in out Suspension_Object) is
1698 Result : Interfaces.C.int;
1699 begin
1700 -- Destroy internal mutex
1702 Result := mutex_destroy (S.L'Access);
1703 pragma Assert (Result = 0);
1705 -- Destroy internal condition variable
1707 Result := cond_destroy (S.CV'Access);
1708 pragma Assert (Result = 0);
1709 end Finalize;
1711 -------------------
1712 -- Current_State --
1713 -------------------
1715 function Current_State (S : Suspension_Object) return Boolean is
1716 begin
1717 -- We do not want to use lock on this read operation. State is marked
1718 -- as Atomic so that we ensure that the value retrieved is correct.
1720 return S.State;
1721 end Current_State;
1723 ---------------
1724 -- Set_False --
1725 ---------------
1727 procedure Set_False (S : in out Suspension_Object) is
1728 Result : Interfaces.C.int;
1729 begin
1730 SSL.Abort_Defer.all;
1732 Result := mutex_lock (S.L'Access);
1733 pragma Assert (Result = 0);
1735 S.State := False;
1737 Result := mutex_unlock (S.L'Access);
1738 pragma Assert (Result = 0);
1740 SSL.Abort_Undefer.all;
1741 end Set_False;
1743 --------------
1744 -- Set_True --
1745 --------------
1747 procedure Set_True (S : in out Suspension_Object) is
1748 Result : Interfaces.C.int;
1749 begin
1750 SSL.Abort_Defer.all;
1752 Result := mutex_lock (S.L'Access);
1753 pragma Assert (Result = 0);
1755 -- If there is already a task waiting on this suspension object then
1756 -- we resume it, leaving the state of the suspension object to False,
1757 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1758 -- the state to True.
1760 if S.Waiting then
1761 S.Waiting := False;
1762 S.State := False;
1764 Result := cond_signal (S.CV'Access);
1765 pragma Assert (Result = 0);
1766 else
1767 S.State := True;
1768 end if;
1770 Result := mutex_unlock (S.L'Access);
1771 pragma Assert (Result = 0);
1773 SSL.Abort_Undefer.all;
1774 end Set_True;
1776 ------------------------
1777 -- Suspend_Until_True --
1778 ------------------------
1780 procedure Suspend_Until_True (S : in out Suspension_Object) is
1781 Result : Interfaces.C.int;
1782 begin
1783 SSL.Abort_Defer.all;
1785 Result := mutex_lock (S.L'Access);
1786 pragma Assert (Result = 0);
1788 if S.Waiting then
1789 -- Program_Error must be raised upon calling Suspend_Until_True
1790 -- if another task is already waiting on that suspension object
1791 -- (ARM D.10 par. 10).
1793 Result := mutex_unlock (S.L'Access);
1794 pragma Assert (Result = 0);
1796 SSL.Abort_Undefer.all;
1798 raise Program_Error;
1799 else
1800 -- Suspend the task if the state is False. Otherwise, the task
1801 -- continues its execution, and the state of the suspension object
1802 -- is set to False (ARM D.10 par. 9).
1804 if S.State then
1805 S.State := False;
1806 else
1807 S.Waiting := True;
1808 Result := cond_wait (S.CV'Access, S.L'Access);
1809 end if;
1811 Result := mutex_unlock (S.L'Access);
1812 pragma Assert (Result = 0);
1814 SSL.Abort_Undefer.all;
1815 end if;
1816 end Suspend_Until_True;
1818 ----------------
1819 -- Check_Exit --
1820 ----------------
1822 function Check_Exit (Self_ID : Task_Id) return Boolean is
1823 begin
1824 -- Check that caller is just holding Global_Task_Lock and no other locks
1826 if Self_ID.Common.LL.Locks = null then
1827 return False;
1828 end if;
1830 -- 2 = Global_Task_Level
1832 if Self_ID.Common.LL.Locks.Level /= 2 then
1833 return False;
1834 end if;
1836 if Self_ID.Common.LL.Locks.Next /= null then
1837 return False;
1838 end if;
1840 -- Check that caller is abort-deferred
1842 if Self_ID.Deferral_Level = 0 then
1843 return False;
1844 end if;
1846 return True;
1847 end Check_Exit;
1849 --------------------
1850 -- Check_No_Locks --
1851 --------------------
1853 function Check_No_Locks (Self_ID : Task_Id) return Boolean is
1854 begin
1855 return Self_ID.Common.LL.Locks = null;
1856 end Check_No_Locks;
1858 ----------------------
1859 -- Environment_Task --
1860 ----------------------
1862 function Environment_Task return Task_Id is
1863 begin
1864 return Environment_Task_Id;
1865 end Environment_Task;
1867 --------------
1868 -- Lock_RTS --
1869 --------------
1871 procedure Lock_RTS is
1872 begin
1873 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1874 end Lock_RTS;
1876 ----------------
1877 -- Unlock_RTS --
1878 ----------------
1880 procedure Unlock_RTS is
1881 begin
1882 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1883 end Unlock_RTS;
1885 ------------------
1886 -- Suspend_Task --
1887 ------------------
1889 function Suspend_Task
1890 (T : ST.Task_Id;
1891 Thread_Self : Thread_Id) return Boolean
1893 begin
1894 if T.Common.LL.Thread /= Thread_Self then
1895 return thr_suspend (T.Common.LL.Thread) = 0;
1896 else
1897 return True;
1898 end if;
1899 end Suspend_Task;
1901 -----------------
1902 -- Resume_Task --
1903 -----------------
1905 function Resume_Task
1906 (T : ST.Task_Id;
1907 Thread_Self : Thread_Id) return Boolean
1909 begin
1910 if T.Common.LL.Thread /= Thread_Self then
1911 return thr_continue (T.Common.LL.Thread) = 0;
1912 else
1913 return True;
1914 end if;
1915 end Resume_Task;
1917 end System.Task_Primitives.Operations;