PR c++/33620
[official-gcc.git] / gcc / ada / s-taprop-posix.adb
blobf9b30ce69c6102295f940fedbab572a6973457b7
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 POSIX-like version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 -- Note: this file can only be used for POSIX compliant systems that
40 -- implement SCHED_FIFO and Ceiling Locking correctly.
42 -- For configurations where SCHED_FIFO and priority ceiling are not a
43 -- requirement, this file can also be used (e.g AiX threads)
45 pragma Polling (Off);
46 -- Turn off polling, we do not want ATC polling to take place during
47 -- tasking operations. It causes infinite loops and other problems.
49 with System.Tasking.Debug;
50 -- used for Known_Tasks
52 with System.Interrupt_Management;
53 -- used for Keep_Unmasked
54 -- Abort_Task_Interrupt
55 -- Interrupt_ID
57 with System.OS_Primitives;
58 -- used for Delay_Modes
60 with System.Task_Info;
61 -- used for Task_Info_Type
63 with Interfaces.C;
64 -- used for int
65 -- size_t
67 with System.Soft_Links;
68 -- used for Abort_Defer/Undefer
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_Conversion;
76 with Ada.Unchecked_Deallocation;
78 package body System.Task_Primitives.Operations is
80 package SSL renames System.Soft_Links;
82 use System.Tasking.Debug;
83 use System.Tasking;
84 use Interfaces.C;
85 use System.OS_Interface;
86 use System.Parameters;
87 use System.OS_Primitives;
89 ----------------
90 -- Local Data --
91 ----------------
93 -- The followings are logically constants, but need to be initialized
94 -- at run time.
96 Single_RTS_Lock : aliased RTS_Lock;
97 -- This is a lock to allow only one thread of control in the RTS at
98 -- a time; it is used to execute in mutual exclusion from all other tasks.
99 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
101 ATCB_Key : aliased pthread_key_t;
102 -- Key used to find the Ada Task_Id associated with a thread
104 Environment_Task_Id : Task_Id;
105 -- A variable to hold Task_Id for the environment task
107 Locking_Policy : Character;
108 pragma Import (C, Locking_Policy, "__gl_locking_policy");
109 -- Value of the pragma Locking_Policy:
110 -- 'C' for Ceiling_Locking
111 -- 'I' for Inherit_Locking
112 -- ' ' for none.
114 Unblocked_Signal_Mask : aliased sigset_t;
115 -- The set of signals that should unblocked in all tasks
117 -- The followings are internal configuration constants needed
119 Next_Serial_Number : Task_Serial_Number := 100;
120 -- We start at 100, to reserve some special values for
121 -- using in error checking.
123 Time_Slice_Val : Integer;
124 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
126 Dispatching_Policy : Character;
127 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
129 Foreign_Task_Elaborated : aliased Boolean := True;
130 -- Used to identified fake tasks (i.e., non-Ada Threads)
132 --------------------
133 -- Local Packages --
134 --------------------
136 package Specific is
138 procedure Initialize (Environment_Task : Task_Id);
139 pragma Inline (Initialize);
140 -- Initialize various data needed by this package
142 function Is_Valid_Task return Boolean;
143 pragma Inline (Is_Valid_Task);
144 -- Does executing thread have a TCB?
146 procedure Set (Self_Id : Task_Id);
147 pragma Inline (Set);
148 -- Set the self id for the current task
150 function Self return Task_Id;
151 pragma Inline (Self);
152 -- Return a pointer to the Ada Task Control Block of the calling task
154 end Specific;
156 package body Specific is separate;
157 -- The body of this package is target specific
159 ---------------------------------
160 -- Support for foreign threads --
161 ---------------------------------
163 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
164 -- Allocate and Initialize a new ATCB for the current Thread
166 function Register_Foreign_Thread
167 (Thread : Thread_Id) return Task_Id is separate;
169 -----------------------
170 -- Local Subprograms --
171 -----------------------
173 procedure Abort_Handler (Sig : Signal);
174 -- Signal handler used to implement asynchronous abort.
175 -- See also comment before body, below.
177 function To_Address is
178 new Ada.Unchecked_Conversion (Task_Id, System.Address);
180 -------------------
181 -- Abort_Handler --
182 -------------------
184 -- Target-dependent binding of inter-thread Abort signal to the raising of
185 -- the Abort_Signal exception.
187 -- The technical issues and alternatives here are essentially the
188 -- same as for raising exceptions in response to other signals
189 -- (e.g. Storage_Error). See code and comments in the package body
190 -- System.Interrupt_Management.
192 -- Some implementations may not allow an exception to be propagated out of
193 -- a handler, and others might leave the signal or interrupt that invoked
194 -- this handler masked after the exceptional return to the application
195 -- code.
197 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
198 -- most UNIX systems, this will allow transfer out of a signal handler,
199 -- which is usually the only mechanism available for implementing
200 -- asynchronous handlers of this kind. However, some systems do not
201 -- restore the signal mask on longjmp(), leaving the abort signal masked.
203 procedure Abort_Handler (Sig : Signal) is
204 pragma Unreferenced (Sig);
206 T : constant Task_Id := Self;
207 Old_Set : aliased sigset_t;
209 Result : Interfaces.C.int;
210 pragma Warnings (Off, Result);
212 begin
213 -- It is not safe to raise an exception when using ZCX and the GCC
214 -- exception handling mechanism.
216 if ZCX_By_Default and then GCC_ZCX_Support then
217 return;
218 end if;
220 if T.Deferral_Level = 0
221 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
222 not T.Aborting
223 then
224 T.Aborting := True;
226 -- Make sure signals used for RTS internal purpose are unmasked
228 Result := pthread_sigmask (SIG_UNBLOCK,
229 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
230 pragma Assert (Result = 0);
232 raise Standard'Abort_Signal;
233 end if;
234 end Abort_Handler;
236 -----------------
237 -- Stack_Guard --
238 -----------------
240 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
241 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
242 Guard_Page_Address : Address;
244 Res : Interfaces.C.int;
246 begin
247 if Stack_Base_Available then
249 -- Compute the guard page address
251 Guard_Page_Address :=
252 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
254 if On then
255 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
256 else
257 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
258 end if;
260 pragma Assert (Res = 0);
261 end if;
262 end Stack_Guard;
264 --------------------
265 -- Get_Thread_Id --
266 --------------------
268 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
269 begin
270 return T.Common.LL.Thread;
271 end Get_Thread_Id;
273 ----------
274 -- Self --
275 ----------
277 function Self return Task_Id renames Specific.Self;
279 ---------------------
280 -- Initialize_Lock --
281 ---------------------
283 -- Note: mutexes and cond_variables needed per-task basis are
284 -- initialized in Intialize_TCB and the Storage_Error is
285 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
286 -- used in RTS is initialized before any status change of RTS.
287 -- Therefore rasing Storage_Error in the following routines
288 -- should be able to be handled safely.
290 procedure Initialize_Lock
291 (Prio : System.Any_Priority;
292 L : not null access Lock)
294 Attributes : aliased pthread_mutexattr_t;
295 Result : Interfaces.C.int;
297 begin
298 Result := pthread_mutexattr_init (Attributes'Access);
299 pragma Assert (Result = 0 or else Result = ENOMEM);
301 if Result = ENOMEM then
302 raise Storage_Error;
303 end if;
305 if Locking_Policy = 'C' then
306 Result := pthread_mutexattr_setprotocol
307 (Attributes'Access, PTHREAD_PRIO_PROTECT);
308 pragma Assert (Result = 0);
310 Result := pthread_mutexattr_setprioceiling
311 (Attributes'Access, Interfaces.C.int (Prio));
312 pragma Assert (Result = 0);
314 elsif Locking_Policy = 'I' then
315 Result := pthread_mutexattr_setprotocol
316 (Attributes'Access, PTHREAD_PRIO_INHERIT);
317 pragma Assert (Result = 0);
318 end if;
320 Result := pthread_mutex_init (L, Attributes'Access);
321 pragma Assert (Result = 0 or else Result = ENOMEM);
323 if Result = ENOMEM then
324 Result := pthread_mutexattr_destroy (Attributes'Access);
325 raise Storage_Error;
326 end if;
328 Result := pthread_mutexattr_destroy (Attributes'Access);
329 pragma Assert (Result = 0);
330 end Initialize_Lock;
332 procedure Initialize_Lock
333 (L : not null access RTS_Lock; Level : Lock_Level)
335 pragma Unreferenced (Level);
337 Attributes : aliased pthread_mutexattr_t;
338 Result : Interfaces.C.int;
340 begin
341 Result := pthread_mutexattr_init (Attributes'Access);
342 pragma Assert (Result = 0 or else Result = ENOMEM);
344 if Result = ENOMEM then
345 raise Storage_Error;
346 end if;
348 if Locking_Policy = 'C' then
349 Result := pthread_mutexattr_setprotocol
350 (Attributes'Access, PTHREAD_PRIO_PROTECT);
351 pragma Assert (Result = 0);
353 Result := pthread_mutexattr_setprioceiling
354 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
355 pragma Assert (Result = 0);
357 elsif Locking_Policy = 'I' then
358 Result := pthread_mutexattr_setprotocol
359 (Attributes'Access, PTHREAD_PRIO_INHERIT);
360 pragma Assert (Result = 0);
361 end if;
363 Result := pthread_mutex_init (L, Attributes'Access);
364 pragma Assert (Result = 0 or else Result = ENOMEM);
366 if Result = ENOMEM then
367 Result := pthread_mutexattr_destroy (Attributes'Access);
368 raise Storage_Error;
369 end if;
371 Result := pthread_mutexattr_destroy (Attributes'Access);
372 pragma Assert (Result = 0);
373 end Initialize_Lock;
375 -------------------
376 -- Finalize_Lock --
377 -------------------
379 procedure Finalize_Lock (L : not null access Lock) is
380 Result : Interfaces.C.int;
381 begin
382 Result := pthread_mutex_destroy (L);
383 pragma Assert (Result = 0);
384 end Finalize_Lock;
386 procedure Finalize_Lock (L : not null access RTS_Lock) is
387 Result : Interfaces.C.int;
388 begin
389 Result := pthread_mutex_destroy (L);
390 pragma Assert (Result = 0);
391 end Finalize_Lock;
393 ----------------
394 -- Write_Lock --
395 ----------------
397 procedure Write_Lock
398 (L : not null access Lock; Ceiling_Violation : out Boolean)
400 Result : Interfaces.C.int;
402 begin
403 Result := pthread_mutex_lock (L);
405 -- Assume that the cause of EINVAL is a priority ceiling violation
407 Ceiling_Violation := (Result = EINVAL);
408 pragma Assert (Result = 0 or else Result = EINVAL);
409 end Write_Lock;
411 procedure Write_Lock
412 (L : not null access RTS_Lock;
413 Global_Lock : Boolean := False)
415 Result : Interfaces.C.int;
416 begin
417 if not Single_Lock or else Global_Lock then
418 Result := pthread_mutex_lock (L);
419 pragma Assert (Result = 0);
420 end if;
421 end Write_Lock;
423 procedure Write_Lock (T : Task_Id) is
424 Result : Interfaces.C.int;
425 begin
426 if not Single_Lock then
427 Result := pthread_mutex_lock (T.Common.LL.L'Access);
428 pragma Assert (Result = 0);
429 end if;
430 end Write_Lock;
432 ---------------
433 -- Read_Lock --
434 ---------------
436 procedure Read_Lock
437 (L : not null access Lock; Ceiling_Violation : out Boolean) is
438 begin
439 Write_Lock (L, Ceiling_Violation);
440 end Read_Lock;
442 ------------
443 -- Unlock --
444 ------------
446 procedure Unlock (L : not null access Lock) is
447 Result : Interfaces.C.int;
448 begin
449 Result := pthread_mutex_unlock (L);
450 pragma Assert (Result = 0);
451 end Unlock;
453 procedure Unlock
454 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
456 Result : Interfaces.C.int;
457 begin
458 if not Single_Lock or else Global_Lock then
459 Result := pthread_mutex_unlock (L);
460 pragma Assert (Result = 0);
461 end if;
462 end Unlock;
464 procedure Unlock (T : Task_Id) is
465 Result : Interfaces.C.int;
466 begin
467 if not Single_Lock then
468 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
469 pragma Assert (Result = 0);
470 end if;
471 end Unlock;
473 -----------------
474 -- Set_Ceiling --
475 -----------------
477 -- Dynamic priority ceilings are not supported by the underlying system
479 procedure Set_Ceiling
480 (L : not null access Lock;
481 Prio : System.Any_Priority)
483 pragma Unreferenced (L, Prio);
484 begin
485 null;
486 end Set_Ceiling;
488 -----------
489 -- Sleep --
490 -----------
492 procedure Sleep
493 (Self_ID : Task_Id;
494 Reason : System.Tasking.Task_States)
496 pragma Unreferenced (Reason);
498 Result : Interfaces.C.int;
500 begin
501 if Single_Lock then
502 Result :=
503 pthread_cond_wait
504 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
505 else
506 Result :=
507 pthread_cond_wait
508 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
509 end if;
511 -- EINTR is not considered a failure
513 pragma Assert (Result = 0 or else Result = EINTR);
514 end Sleep;
516 -----------------
517 -- Timed_Sleep --
518 -----------------
520 -- This is for use within the run-time system, so abort is
521 -- assumed to be already deferred, and the caller should be
522 -- holding its own ATCB lock.
524 procedure Timed_Sleep
525 (Self_ID : Task_Id;
526 Time : Duration;
527 Mode : ST.Delay_Modes;
528 Reason : Task_States;
529 Timedout : out Boolean;
530 Yielded : out Boolean)
532 pragma Unreferenced (Reason);
534 Base_Time : constant Duration := Monotonic_Clock;
535 Check_Time : Duration := Base_Time;
536 Rel_Time : Duration;
537 Abs_Time : Duration;
538 Request : aliased timespec;
539 Result : Interfaces.C.int;
541 begin
542 Timedout := True;
543 Yielded := False;
545 if Mode = Relative then
546 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
548 if Relative_Timed_Wait then
549 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
550 end if;
552 else
553 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
555 if Relative_Timed_Wait then
556 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
557 end if;
558 end if;
560 if Abs_Time > Check_Time then
561 if Relative_Timed_Wait then
562 Request := To_Timespec (Rel_Time);
563 else
564 Request := To_Timespec (Abs_Time);
565 end if;
567 loop
568 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
570 if Single_Lock then
571 Result :=
572 pthread_cond_timedwait
573 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
574 Request'Access);
576 else
577 Result :=
578 pthread_cond_timedwait
579 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
580 Request'Access);
581 end if;
583 Check_Time := Monotonic_Clock;
584 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
586 if Result = 0 or Result = EINTR then
588 -- Somebody may have called Wakeup for us
590 Timedout := False;
591 exit;
592 end if;
594 pragma Assert (Result = ETIMEDOUT);
595 end loop;
596 end if;
597 end Timed_Sleep;
599 -----------------
600 -- Timed_Delay --
601 -----------------
603 -- This is for use in implementing delay statements, so we assume the
604 -- caller is abort-deferred but is holding no locks.
606 procedure Timed_Delay
607 (Self_ID : Task_Id;
608 Time : Duration;
609 Mode : ST.Delay_Modes)
611 Base_Time : constant Duration := Monotonic_Clock;
612 Check_Time : Duration := Base_Time;
613 Abs_Time : Duration;
614 Rel_Time : Duration;
615 Request : aliased timespec;
617 Result : Interfaces.C.int;
618 pragma Warnings (Off, Result);
620 begin
621 if Single_Lock then
622 Lock_RTS;
623 end if;
625 Write_Lock (Self_ID);
627 if Mode = Relative then
628 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
630 if Relative_Timed_Wait then
631 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
632 end if;
634 else
635 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
637 if Relative_Timed_Wait then
638 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
639 end if;
640 end if;
642 if Abs_Time > Check_Time then
643 if Relative_Timed_Wait then
644 Request := To_Timespec (Rel_Time);
645 else
646 Request := To_Timespec (Abs_Time);
647 end if;
649 Self_ID.Common.State := Delay_Sleep;
651 loop
652 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
654 if Single_Lock then
655 Result := pthread_cond_timedwait
656 (Self_ID.Common.LL.CV'Access,
657 Single_RTS_Lock'Access,
658 Request'Access);
659 else
660 Result := pthread_cond_timedwait
661 (Self_ID.Common.LL.CV'Access,
662 Self_ID.Common.LL.L'Access,
663 Request'Access);
664 end if;
666 Check_Time := Monotonic_Clock;
667 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
669 pragma Assert (Result = 0
670 or else Result = ETIMEDOUT
671 or else Result = EINTR);
672 end loop;
674 Self_ID.Common.State := Runnable;
675 end if;
677 Unlock (Self_ID);
679 if Single_Lock then
680 Unlock_RTS;
681 end if;
683 Result := sched_yield;
684 end Timed_Delay;
686 ---------------------
687 -- Monotonic_Clock --
688 ---------------------
690 function Monotonic_Clock return Duration is
691 TS : aliased timespec;
692 Result : Interfaces.C.int;
693 begin
694 Result := clock_gettime
695 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
696 pragma Assert (Result = 0);
697 return To_Duration (TS);
698 end Monotonic_Clock;
700 -------------------
701 -- RT_Resolution --
702 -------------------
704 function RT_Resolution return Duration is
705 begin
706 return 10#1.0#E-6;
707 end RT_Resolution;
709 ------------
710 -- Wakeup --
711 ------------
713 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
714 pragma Unreferenced (Reason);
715 Result : Interfaces.C.int;
716 begin
717 Result := pthread_cond_signal (T.Common.LL.CV'Access);
718 pragma Assert (Result = 0);
719 end Wakeup;
721 -----------
722 -- Yield --
723 -----------
725 procedure Yield (Do_Yield : Boolean := True) is
726 Result : Interfaces.C.int;
727 pragma Unreferenced (Result);
728 begin
729 if Do_Yield then
730 Result := sched_yield;
731 end if;
732 end Yield;
734 ------------------
735 -- Set_Priority --
736 ------------------
738 procedure Set_Priority
739 (T : Task_Id;
740 Prio : System.Any_Priority;
741 Loss_Of_Inheritance : Boolean := False)
743 pragma Unreferenced (Loss_Of_Inheritance);
745 Result : Interfaces.C.int;
746 Param : aliased struct_sched_param;
748 function Get_Policy (Prio : System.Any_Priority) return Character;
749 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
750 -- Get priority specific dispatching policy
752 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
753 -- Upper case first character of the policy name corresponding to the
754 -- task as set by a Priority_Specific_Dispatching pragma.
756 begin
757 T.Common.Current_Priority := Prio;
758 Param.sched_priority := To_Target_Priority (Prio);
760 if Time_Slice_Supported
761 and then (Dispatching_Policy = 'R'
762 or else Priority_Specific_Policy = 'R'
763 or else Time_Slice_Val > 0)
764 then
765 Result := pthread_setschedparam
766 (T.Common.LL.Thread, SCHED_RR, Param'Access);
768 elsif Dispatching_Policy = 'F'
769 or else Priority_Specific_Policy = 'F'
770 or else Time_Slice_Val = 0
771 then
772 Result := pthread_setschedparam
773 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
775 else
776 Result := pthread_setschedparam
777 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
778 end if;
780 pragma Assert (Result = 0);
781 end Set_Priority;
783 ------------------
784 -- Get_Priority --
785 ------------------
787 function Get_Priority (T : Task_Id) return System.Any_Priority is
788 begin
789 return T.Common.Current_Priority;
790 end Get_Priority;
792 ----------------
793 -- Enter_Task --
794 ----------------
796 procedure Enter_Task (Self_ID : Task_Id) is
797 begin
798 Self_ID.Common.LL.Thread := pthread_self;
799 Self_ID.Common.LL.LWP := lwp_self;
801 Specific.Set (Self_ID);
803 Lock_RTS;
805 for J in Known_Tasks'Range loop
806 if Known_Tasks (J) = null then
807 Known_Tasks (J) := Self_ID;
808 Self_ID.Known_Tasks_Index := J;
809 exit;
810 end if;
811 end loop;
813 Unlock_RTS;
814 end Enter_Task;
816 --------------
817 -- New_ATCB --
818 --------------
820 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
821 begin
822 return new Ada_Task_Control_Block (Entry_Num);
823 end New_ATCB;
825 -------------------
826 -- Is_Valid_Task --
827 -------------------
829 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
831 -----------------------------
832 -- Register_Foreign_Thread --
833 -----------------------------
835 function Register_Foreign_Thread return Task_Id is
836 begin
837 if Is_Valid_Task then
838 return Self;
839 else
840 return Register_Foreign_Thread (pthread_self);
841 end if;
842 end Register_Foreign_Thread;
844 --------------------
845 -- Initialize_TCB --
846 --------------------
848 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
849 Mutex_Attr : aliased pthread_mutexattr_t;
850 Result : Interfaces.C.int;
851 Cond_Attr : aliased pthread_condattr_t;
853 begin
854 -- Give the task a unique serial number
856 Self_ID.Serial_Number := Next_Serial_Number;
857 Next_Serial_Number := Next_Serial_Number + 1;
858 pragma Assert (Next_Serial_Number /= 0);
860 if not Single_Lock then
861 Result := pthread_mutexattr_init (Mutex_Attr'Access);
862 pragma Assert (Result = 0 or else Result = ENOMEM);
864 if Result = 0 then
865 if Locking_Policy = 'C' then
866 Result :=
867 pthread_mutexattr_setprotocol
868 (Mutex_Attr'Access,
869 PTHREAD_PRIO_PROTECT);
870 pragma Assert (Result = 0);
872 Result :=
873 pthread_mutexattr_setprioceiling
874 (Mutex_Attr'Access,
875 Interfaces.C.int (System.Any_Priority'Last));
876 pragma Assert (Result = 0);
878 elsif Locking_Policy = 'I' then
879 Result :=
880 pthread_mutexattr_setprotocol
881 (Mutex_Attr'Access,
882 PTHREAD_PRIO_INHERIT);
883 pragma Assert (Result = 0);
884 end if;
886 Result :=
887 pthread_mutex_init
888 (Self_ID.Common.LL.L'Access,
889 Mutex_Attr'Access);
890 pragma Assert (Result = 0 or else Result = ENOMEM);
891 end if;
893 if Result /= 0 then
894 Succeeded := False;
895 return;
896 end if;
898 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
899 pragma Assert (Result = 0);
900 end if;
902 Result := pthread_condattr_init (Cond_Attr'Access);
903 pragma Assert (Result = 0 or else Result = ENOMEM);
905 if Result = 0 then
906 Result :=
907 pthread_cond_init
908 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
909 pragma Assert (Result = 0 or else Result = ENOMEM);
910 end if;
912 if Result = 0 then
913 Succeeded := True;
914 else
915 if not Single_Lock then
916 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
917 pragma Assert (Result = 0);
918 end if;
920 Succeeded := False;
921 end if;
923 Result := pthread_condattr_destroy (Cond_Attr'Access);
924 pragma Assert (Result = 0);
925 end Initialize_TCB;
927 -----------------
928 -- Create_Task --
929 -----------------
931 procedure Create_Task
932 (T : Task_Id;
933 Wrapper : System.Address;
934 Stack_Size : System.Parameters.Size_Type;
935 Priority : System.Any_Priority;
936 Succeeded : out Boolean)
938 Attributes : aliased pthread_attr_t;
939 Adjusted_Stack_Size : Interfaces.C.size_t;
940 Result : Interfaces.C.int;
942 function Thread_Body_Access is new
943 Ada.Unchecked_Conversion (System.Address, Thread_Body);
945 use System.Task_Info;
947 begin
948 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
950 if Stack_Base_Available then
952 -- If Stack Checking is supported then allocate 2 additional pages:
954 -- In the worst case, stack is allocated at something like
955 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
956 -- to be sure the effective stack size is greater than what
957 -- has been asked.
959 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
960 end if;
962 Result := pthread_attr_init (Attributes'Access);
963 pragma Assert (Result = 0 or else Result = ENOMEM);
965 if Result /= 0 then
966 Succeeded := False;
967 return;
968 end if;
970 Result :=
971 pthread_attr_setdetachstate
972 (Attributes'Access, PTHREAD_CREATE_DETACHED);
973 pragma Assert (Result = 0);
975 Result :=
976 pthread_attr_setstacksize
977 (Attributes'Access, Adjusted_Stack_Size);
978 pragma Assert (Result = 0);
980 if T.Common.Task_Info /= Default_Scope then
981 case T.Common.Task_Info is
982 when System.Task_Info.Process_Scope =>
983 Result :=
984 pthread_attr_setscope
985 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
987 when System.Task_Info.System_Scope =>
988 Result :=
989 pthread_attr_setscope
990 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
992 when System.Task_Info.Default_Scope =>
993 Result := 0;
994 end case;
996 pragma Assert (Result = 0);
997 end if;
999 -- Since the initial signal mask of a thread is inherited from the
1000 -- creator, and the Environment task has all its signals masked, we
1001 -- do not need to manipulate caller's signal mask at this point.
1002 -- All tasks in RTS will have All_Tasks_Mask initially.
1004 Result := pthread_create
1005 (T.Common.LL.Thread'Access,
1006 Attributes'Access,
1007 Thread_Body_Access (Wrapper),
1008 To_Address (T));
1009 pragma Assert (Result = 0 or else Result = EAGAIN);
1011 Succeeded := Result = 0;
1013 Result := pthread_attr_destroy (Attributes'Access);
1014 pragma Assert (Result = 0);
1016 Set_Priority (T, Priority);
1017 end Create_Task;
1019 ------------------
1020 -- Finalize_TCB --
1021 ------------------
1023 procedure Finalize_TCB (T : Task_Id) is
1024 Result : Interfaces.C.int;
1025 Tmp : Task_Id := T;
1026 Is_Self : constant Boolean := T = Self;
1028 procedure Free is new
1029 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1031 begin
1032 if not Single_Lock then
1033 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1034 pragma Assert (Result = 0);
1035 end if;
1037 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1038 pragma Assert (Result = 0);
1040 if T.Known_Tasks_Index /= -1 then
1041 Known_Tasks (T.Known_Tasks_Index) := null;
1042 end if;
1044 Free (Tmp);
1046 if Is_Self then
1047 Specific.Set (null);
1048 end if;
1049 end Finalize_TCB;
1051 ---------------
1052 -- Exit_Task --
1053 ---------------
1055 procedure Exit_Task is
1056 begin
1057 -- Mark this task as unknown, so that if Self is called, it won't
1058 -- return a dangling pointer.
1060 Specific.Set (null);
1061 end Exit_Task;
1063 ----------------
1064 -- Abort_Task --
1065 ----------------
1067 procedure Abort_Task (T : Task_Id) is
1068 Result : Interfaces.C.int;
1069 begin
1070 Result :=
1071 pthread_kill
1072 (T.Common.LL.Thread,
1073 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1074 pragma Assert (Result = 0);
1075 end Abort_Task;
1077 ----------------
1078 -- Initialize --
1079 ----------------
1081 procedure Initialize (S : in out Suspension_Object) is
1082 Mutex_Attr : aliased pthread_mutexattr_t;
1083 Cond_Attr : aliased pthread_condattr_t;
1084 Result : Interfaces.C.int;
1086 begin
1087 -- Initialize internal state (always to False (RM D.10 (6)))
1089 S.State := False;
1090 S.Waiting := False;
1092 -- Initialize internal mutex
1094 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1095 pragma Assert (Result = 0 or else Result = ENOMEM);
1097 if Result = ENOMEM then
1098 raise Storage_Error;
1099 end if;
1101 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1102 pragma Assert (Result = 0 or else Result = ENOMEM);
1104 if Result = ENOMEM then
1105 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1106 pragma Assert (Result = 0);
1108 raise Storage_Error;
1109 end if;
1111 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1112 pragma Assert (Result = 0);
1114 -- Initialize internal condition variable
1116 Result := pthread_condattr_init (Cond_Attr'Access);
1117 pragma Assert (Result = 0 or else Result = ENOMEM);
1119 if Result /= 0 then
1120 Result := pthread_mutex_destroy (S.L'Access);
1121 pragma Assert (Result = 0);
1123 if Result = ENOMEM then
1124 raise Storage_Error;
1125 end if;
1126 end if;
1128 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1129 pragma Assert (Result = 0 or else Result = ENOMEM);
1131 if Result /= 0 then
1132 Result := pthread_mutex_destroy (S.L'Access);
1133 pragma Assert (Result = 0);
1135 if Result = ENOMEM then
1136 Result := pthread_condattr_destroy (Cond_Attr'Access);
1137 pragma Assert (Result = 0);
1138 raise Storage_Error;
1139 end if;
1140 end if;
1142 Result := pthread_condattr_destroy (Cond_Attr'Access);
1143 pragma Assert (Result = 0);
1144 end Initialize;
1146 --------------
1147 -- Finalize --
1148 --------------
1150 procedure Finalize (S : in out Suspension_Object) is
1151 Result : Interfaces.C.int;
1153 begin
1154 -- Destroy internal mutex
1156 Result := pthread_mutex_destroy (S.L'Access);
1157 pragma Assert (Result = 0);
1159 -- Destroy internal condition variable
1161 Result := pthread_cond_destroy (S.CV'Access);
1162 pragma Assert (Result = 0);
1163 end Finalize;
1165 -------------------
1166 -- Current_State --
1167 -------------------
1169 function Current_State (S : Suspension_Object) return Boolean is
1170 begin
1171 -- We do not want to use lock on this read operation. State is marked
1172 -- as Atomic so that we ensure that the value retrieved is correct.
1174 return S.State;
1175 end Current_State;
1177 ---------------
1178 -- Set_False --
1179 ---------------
1181 procedure Set_False (S : in out Suspension_Object) is
1182 Result : Interfaces.C.int;
1184 begin
1185 SSL.Abort_Defer.all;
1187 Result := pthread_mutex_lock (S.L'Access);
1188 pragma Assert (Result = 0);
1190 S.State := False;
1192 Result := pthread_mutex_unlock (S.L'Access);
1193 pragma Assert (Result = 0);
1195 SSL.Abort_Undefer.all;
1196 end Set_False;
1198 --------------
1199 -- Set_True --
1200 --------------
1202 procedure Set_True (S : in out Suspension_Object) is
1203 Result : Interfaces.C.int;
1205 begin
1206 SSL.Abort_Defer.all;
1208 Result := pthread_mutex_lock (S.L'Access);
1209 pragma Assert (Result = 0);
1211 -- If there is already a task waiting on this suspension object then
1212 -- we resume it, leaving the state of the suspension object to False,
1213 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1214 -- the state to True.
1216 if S.Waiting then
1217 S.Waiting := False;
1218 S.State := False;
1220 Result := pthread_cond_signal (S.CV'Access);
1221 pragma Assert (Result = 0);
1223 else
1224 S.State := True;
1225 end if;
1227 Result := pthread_mutex_unlock (S.L'Access);
1228 pragma Assert (Result = 0);
1230 SSL.Abort_Undefer.all;
1231 end Set_True;
1233 ------------------------
1234 -- Suspend_Until_True --
1235 ------------------------
1237 procedure Suspend_Until_True (S : in out Suspension_Object) is
1238 Result : Interfaces.C.int;
1240 begin
1241 SSL.Abort_Defer.all;
1243 Result := pthread_mutex_lock (S.L'Access);
1244 pragma Assert (Result = 0);
1246 if S.Waiting then
1248 -- Program_Error must be raised upon calling Suspend_Until_True
1249 -- if another task is already waiting on that suspension object
1250 -- (RM D.10(10)).
1252 Result := pthread_mutex_unlock (S.L'Access);
1253 pragma Assert (Result = 0);
1255 SSL.Abort_Undefer.all;
1257 raise Program_Error;
1259 else
1260 -- Suspend the task if the state is False. Otherwise, the task
1261 -- continues its execution, and the state of the suspension object
1262 -- is set to False (ARM D.10 par. 9).
1264 if S.State then
1265 S.State := False;
1266 else
1267 S.Waiting := True;
1268 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1269 end if;
1271 Result := pthread_mutex_unlock (S.L'Access);
1272 pragma Assert (Result = 0);
1274 SSL.Abort_Undefer.all;
1275 end if;
1276 end Suspend_Until_True;
1278 ----------------
1279 -- Check_Exit --
1280 ----------------
1282 -- Dummy version
1284 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1285 pragma Unreferenced (Self_ID);
1286 begin
1287 return True;
1288 end Check_Exit;
1290 --------------------
1291 -- Check_No_Locks --
1292 --------------------
1294 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1295 pragma Unreferenced (Self_ID);
1296 begin
1297 return True;
1298 end Check_No_Locks;
1300 ----------------------
1301 -- Environment_Task --
1302 ----------------------
1304 function Environment_Task return Task_Id is
1305 begin
1306 return Environment_Task_Id;
1307 end Environment_Task;
1309 --------------
1310 -- Lock_RTS --
1311 --------------
1313 procedure Lock_RTS is
1314 begin
1315 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1316 end Lock_RTS;
1318 ----------------
1319 -- Unlock_RTS --
1320 ----------------
1322 procedure Unlock_RTS is
1323 begin
1324 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1325 end Unlock_RTS;
1327 ------------------
1328 -- Suspend_Task --
1329 ------------------
1331 function Suspend_Task
1332 (T : ST.Task_Id;
1333 Thread_Self : Thread_Id) return Boolean
1335 pragma Unreferenced (T, Thread_Self);
1336 begin
1337 return False;
1338 end Suspend_Task;
1340 -----------------
1341 -- Resume_Task --
1342 -----------------
1344 function Resume_Task
1345 (T : ST.Task_Id;
1346 Thread_Self : Thread_Id) return Boolean
1348 pragma Unreferenced (T, Thread_Self);
1349 begin
1350 return False;
1351 end Resume_Task;
1353 --------------------
1354 -- Stop_All_Tasks --
1355 --------------------
1357 procedure Stop_All_Tasks is
1358 begin
1359 null;
1360 end Stop_All_Tasks;
1362 ---------------
1363 -- Stop_Task --
1364 ---------------
1366 function Stop_Task (T : ST.Task_Id) return Boolean is
1367 pragma Unreferenced (T);
1368 begin
1369 return False;
1370 end Stop_Task;
1372 -------------------
1373 -- Continue_Task --
1374 -------------------
1376 function Continue_Task (T : ST.Task_Id) return Boolean is
1377 pragma Unreferenced (T);
1378 begin
1379 return False;
1380 end Continue_Task;
1382 ----------------
1383 -- Initialize --
1384 ----------------
1386 procedure Initialize (Environment_Task : Task_Id) is
1387 act : aliased struct_sigaction;
1388 old_act : aliased struct_sigaction;
1389 Tmp_Set : aliased sigset_t;
1390 Result : Interfaces.C.int;
1392 function State
1393 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1394 pragma Import (C, State, "__gnat_get_interrupt_state");
1395 -- Get interrupt state. Defined in a-init.c
1396 -- The input argument is the interrupt number,
1397 -- and the result is one of the following:
1399 Default : constant Character := 's';
1400 -- 'n' this interrupt not set by any Interrupt_State pragma
1401 -- 'u' Interrupt_State pragma set state to User
1402 -- 'r' Interrupt_State pragma set state to Runtime
1403 -- 's' Interrupt_State pragma set state to System (use "default"
1404 -- system handler)
1406 begin
1407 Environment_Task_Id := Environment_Task;
1409 Interrupt_Management.Initialize;
1411 -- Prepare the set of signals that should unblocked in all tasks
1413 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1414 pragma Assert (Result = 0);
1416 for J in Interrupt_Management.Interrupt_ID loop
1417 if System.Interrupt_Management.Keep_Unmasked (J) then
1418 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1419 pragma Assert (Result = 0);
1420 end if;
1421 end loop;
1423 -- Initialize the lock used to synchronize chain of all ATCBs
1425 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1427 Specific.Initialize (Environment_Task);
1429 Enter_Task (Environment_Task);
1431 -- Install the abort-signal handler
1433 if State
1434 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1435 then
1436 act.sa_flags := 0;
1437 act.sa_handler := Abort_Handler'Address;
1439 Result := sigemptyset (Tmp_Set'Access);
1440 pragma Assert (Result = 0);
1441 act.sa_mask := Tmp_Set;
1443 Result :=
1444 sigaction
1445 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1446 act'Unchecked_Access,
1447 old_act'Unchecked_Access);
1448 pragma Assert (Result = 0);
1449 end if;
1450 end Initialize;
1452 end System.Task_Primitives.Operations;