* config/sparc/driver-sparc.c (cpu_names): Add SPARC-T5 entry.
[official-gcc.git] / gcc / ada / s-taprop-posix.adb
blobfc647aa2d5e2e8268f40d53a06da649acc0a693c
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-2016, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is a POSIX-like version of this package
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
37 -- Note: this file can only be used for POSIX compliant systems that implement
38 -- SCHED_FIFO and Ceiling Locking correctly.
40 -- For configurations where SCHED_FIFO and priority ceiling are not a
41 -- requirement, this file can also be used (e.g AiX threads)
43 pragma Polling (Off);
44 -- Turn off polling, we do not want ATC polling to take place during tasking
45 -- operations. It causes infinite loops and other problems.
47 with Ada.Unchecked_Conversion;
49 with Interfaces.C;
51 with System.Tasking.Debug;
52 with System.Interrupt_Management;
53 with System.OS_Constants;
54 with System.OS_Primitives;
55 with System.Task_Info;
57 with System.Soft_Links;
58 -- We use System.Soft_Links instead of System.Tasking.Initialization
59 -- because the later is a higher level package that we shouldn't depend on.
60 -- For example when using the restricted run time, it is replaced by
61 -- System.Tasking.Restricted.Stages.
63 package body System.Task_Primitives.Operations is
65 package OSC renames System.OS_Constants;
66 package SSL renames System.Soft_Links;
68 use System.Tasking.Debug;
69 use System.Tasking;
70 use Interfaces.C;
71 use System.OS_Interface;
72 use System.Parameters;
73 use System.OS_Primitives;
75 ----------------
76 -- Local Data --
77 ----------------
79 -- The followings are logically constants, but need to be initialized
80 -- at run time.
82 Single_RTS_Lock : aliased RTS_Lock;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 Environment_Task_Id : Task_Id;
88 -- A variable to hold Task_Id for the environment task
90 Locking_Policy : Character;
91 pragma Import (C, Locking_Policy, "__gl_locking_policy");
92 -- Value of the pragma Locking_Policy:
93 -- 'C' for Ceiling_Locking
94 -- 'I' for Inherit_Locking
95 -- ' ' for none.
97 Unblocked_Signal_Mask : aliased sigset_t;
98 -- The set of signals that should unblocked in all tasks
100 -- The followings are internal configuration constants needed
102 Next_Serial_Number : Task_Serial_Number := 100;
103 -- We start at 100, to reserve some special values for
104 -- using in error checking.
106 Time_Slice_Val : Integer;
107 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
109 Dispatching_Policy : Character;
110 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
112 Foreign_Task_Elaborated : aliased Boolean := True;
113 -- Used to identified fake tasks (i.e., non-Ada Threads)
115 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
116 -- Whether to use an alternate signal stack for stack overflows
118 Abort_Handler_Installed : Boolean := False;
119 -- True if a handler for the abort signal is installed
121 --------------------
122 -- Local Packages --
123 --------------------
125 package Specific is
127 procedure Initialize (Environment_Task : Task_Id);
128 pragma Inline (Initialize);
129 -- Initialize various data needed by this package
131 function Is_Valid_Task return Boolean;
132 pragma Inline (Is_Valid_Task);
133 -- Does executing thread have a TCB?
135 procedure Set (Self_Id : Task_Id);
136 pragma Inline (Set);
137 -- Set the self id for the current task
139 function Self return Task_Id;
140 pragma Inline (Self);
141 -- Return a pointer to the Ada Task Control Block of the calling task
143 end Specific;
145 package body Specific is separate;
146 -- The body of this package is target specific
148 ----------------------------------
149 -- ATCB allocation/deallocation --
150 ----------------------------------
152 package body ATCB_Allocation is separate;
153 -- The body of this package is shared across several targets
155 ---------------------------------
156 -- Support for foreign threads --
157 ---------------------------------
159 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
160 -- Allocate and Initialize a new ATCB for the current Thread
162 function Register_Foreign_Thread
163 (Thread : Thread_Id) return Task_Id is separate;
165 -----------------------
166 -- Local Subprograms --
167 -----------------------
169 procedure Abort_Handler (Sig : Signal);
170 -- Signal handler used to implement asynchronous abort.
171 -- See also comment before body, below.
173 function To_Address is
174 new Ada.Unchecked_Conversion (Task_Id, System.Address);
176 function GNAT_pthread_condattr_setup
177 (attr : access pthread_condattr_t) return int;
178 pragma Import (C,
179 GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup");
181 procedure Compute_Deadline
182 (Time : Duration;
183 Mode : ST.Delay_Modes;
184 Check_Time : out Duration;
185 Abs_Time : out Duration;
186 Rel_Time : out Duration);
187 -- Helper for Timed_Sleep and Timed_Delay: given a deadline specified by
188 -- Time and Mode, compute the current clock reading (Check_Time), and the
189 -- target absolute and relative clock readings (Abs_Time, Rel_Time). The
190 -- epoch for Time depends on Mode; the epoch for Check_Time and Abs_Time
191 -- is always that of CLOCK_RT_Ada.
193 -------------------
194 -- Abort_Handler --
195 -------------------
197 -- Target-dependent binding of inter-thread Abort signal to the raising of
198 -- the Abort_Signal exception.
200 -- The technical issues and alternatives here are essentially the
201 -- same as for raising exceptions in response to other signals
202 -- (e.g. Storage_Error). See code and comments in the package body
203 -- System.Interrupt_Management.
205 -- Some implementations may not allow an exception to be propagated out of
206 -- a handler, and others might leave the signal or interrupt that invoked
207 -- this handler masked after the exceptional return to the application
208 -- code.
210 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
211 -- most UNIX systems, this will allow transfer out of a signal handler,
212 -- which is usually the only mechanism available for implementing
213 -- asynchronous handlers of this kind. However, some systems do not
214 -- restore the signal mask on longjmp(), leaving the abort signal masked.
216 procedure Abort_Handler (Sig : Signal) is
217 pragma Unreferenced (Sig);
219 T : constant Task_Id := Self;
220 Old_Set : aliased sigset_t;
222 Result : Interfaces.C.int;
223 pragma Warnings (Off, Result);
225 begin
226 -- It's not safe to raise an exception when using GCC ZCX mechanism.
227 -- Note that we still need to install a signal handler, since in some
228 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
229 -- need to send the Abort signal to a task.
231 if ZCX_By_Default then
232 return;
233 end if;
235 if T.Deferral_Level = 0
236 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
237 not T.Aborting
238 then
239 T.Aborting := True;
241 -- Make sure signals used for RTS internal purpose are unmasked
243 Result := pthread_sigmask (SIG_UNBLOCK,
244 Unblocked_Signal_Mask'Access, Old_Set'Access);
245 pragma Assert (Result = 0);
247 raise Standard'Abort_Signal;
248 end if;
249 end Abort_Handler;
251 ----------------------
252 -- Compute_Deadline --
253 ----------------------
255 procedure Compute_Deadline
256 (Time : Duration;
257 Mode : ST.Delay_Modes;
258 Check_Time : out Duration;
259 Abs_Time : out Duration;
260 Rel_Time : out Duration)
262 begin
263 Check_Time := Monotonic_Clock;
265 -- Relative deadline
267 if Mode = Relative then
268 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
270 if Relative_Timed_Wait then
271 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
272 end if;
274 pragma Warnings (Off);
275 -- Comparison "OSC.CLOCK_RT_Ada = OSC.CLOCK_REALTIME" is compile
276 -- time known.
278 -- Absolute deadline specified using the tasking clock (CLOCK_RT_Ada)
280 elsif Mode = Absolute_RT
281 or else OSC.CLOCK_RT_Ada = OSC.CLOCK_REALTIME
282 then
283 pragma Warnings (On);
284 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
286 if Relative_Timed_Wait then
287 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
288 end if;
290 -- Absolute deadline specified using the calendar clock, in the
291 -- case where it is not the same as the tasking clock: compensate for
292 -- difference between clock epochs (Base_Time - Base_Cal_Time).
294 else
295 declare
296 Cal_Check_Time : constant Duration := OS_Primitives.Clock;
297 RT_Time : constant Duration :=
298 Time + Check_Time - Cal_Check_Time;
300 begin
301 Abs_Time :=
302 Duration'Min (Check_Time + Max_Sensible_Delay, RT_Time);
304 if Relative_Timed_Wait then
305 Rel_Time :=
306 Duration'Min (Max_Sensible_Delay, RT_Time - Check_Time);
307 end if;
308 end;
309 end if;
310 end Compute_Deadline;
312 -----------------
313 -- Stack_Guard --
314 -----------------
316 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
317 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
318 Page_Size : Address;
319 Res : Interfaces.C.int;
321 begin
322 if Stack_Base_Available then
324 -- Compute the guard page address
326 Page_Size := Address (Get_Page_Size);
327 Res :=
328 mprotect
329 (Stack_Base - (Stack_Base mod Page_Size) + Page_Size,
330 size_t (Page_Size),
331 prot => (if On then PROT_ON else PROT_OFF));
332 pragma Assert (Res = 0);
333 end if;
334 end Stack_Guard;
336 --------------------
337 -- Get_Thread_Id --
338 --------------------
340 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
341 begin
342 return T.Common.LL.Thread;
343 end Get_Thread_Id;
345 ----------
346 -- Self --
347 ----------
349 function Self return Task_Id renames Specific.Self;
351 ---------------------
352 -- Initialize_Lock --
353 ---------------------
355 -- Note: mutexes and cond_variables needed per-task basis are initialized
356 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
357 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
358 -- status change of RTS. Therefore raising Storage_Error in the following
359 -- routines should be able to be handled safely.
361 procedure Initialize_Lock
362 (Prio : System.Any_Priority;
363 L : not null access Lock)
365 Attributes : aliased pthread_mutexattr_t;
366 Result : Interfaces.C.int;
368 begin
369 Result := pthread_mutexattr_init (Attributes'Access);
370 pragma Assert (Result = 0 or else Result = ENOMEM);
372 if Result = ENOMEM then
373 raise Storage_Error;
374 end if;
376 if Locking_Policy = 'C' then
377 Result := pthread_mutexattr_setprotocol
378 (Attributes'Access, PTHREAD_PRIO_PROTECT);
379 pragma Assert (Result = 0);
381 Result := pthread_mutexattr_setprioceiling
382 (Attributes'Access, Interfaces.C.int (Prio));
383 pragma Assert (Result = 0);
385 elsif Locking_Policy = 'I' then
386 Result := pthread_mutexattr_setprotocol
387 (Attributes'Access, PTHREAD_PRIO_INHERIT);
388 pragma Assert (Result = 0);
389 end if;
391 Result := pthread_mutex_init (L.WO'Access, Attributes'Access);
392 pragma Assert (Result = 0 or else Result = ENOMEM);
394 if Result = ENOMEM then
395 Result := pthread_mutexattr_destroy (Attributes'Access);
396 raise Storage_Error;
397 end if;
399 Result := pthread_mutexattr_destroy (Attributes'Access);
400 pragma Assert (Result = 0);
401 end Initialize_Lock;
403 procedure Initialize_Lock
404 (L : not null access RTS_Lock; Level : Lock_Level)
406 pragma Unreferenced (Level);
408 Attributes : aliased pthread_mutexattr_t;
409 Result : Interfaces.C.int;
411 begin
412 Result := pthread_mutexattr_init (Attributes'Access);
413 pragma Assert (Result = 0 or else Result = ENOMEM);
415 if Result = ENOMEM then
416 raise Storage_Error;
417 end if;
419 if Locking_Policy = 'C' then
420 Result := pthread_mutexattr_setprotocol
421 (Attributes'Access, PTHREAD_PRIO_PROTECT);
422 pragma Assert (Result = 0);
424 Result := pthread_mutexattr_setprioceiling
425 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
426 pragma Assert (Result = 0);
428 elsif Locking_Policy = 'I' then
429 Result := pthread_mutexattr_setprotocol
430 (Attributes'Access, PTHREAD_PRIO_INHERIT);
431 pragma Assert (Result = 0);
432 end if;
434 Result := pthread_mutex_init (L, Attributes'Access);
435 pragma Assert (Result = 0 or else Result = ENOMEM);
437 if Result = ENOMEM then
438 Result := pthread_mutexattr_destroy (Attributes'Access);
439 raise Storage_Error;
440 end if;
442 Result := pthread_mutexattr_destroy (Attributes'Access);
443 pragma Assert (Result = 0);
444 end Initialize_Lock;
446 -------------------
447 -- Finalize_Lock --
448 -------------------
450 procedure Finalize_Lock (L : not null access Lock) is
451 Result : Interfaces.C.int;
452 begin
453 Result := pthread_mutex_destroy (L.WO'Access);
454 pragma Assert (Result = 0);
455 end Finalize_Lock;
457 procedure Finalize_Lock (L : not null access RTS_Lock) is
458 Result : Interfaces.C.int;
459 begin
460 Result := pthread_mutex_destroy (L);
461 pragma Assert (Result = 0);
462 end Finalize_Lock;
464 ----------------
465 -- Write_Lock --
466 ----------------
468 procedure Write_Lock
469 (L : not null access Lock; Ceiling_Violation : out Boolean)
471 Result : Interfaces.C.int;
473 begin
474 Result := pthread_mutex_lock (L.WO'Access);
476 -- The cause of EINVAL is a priority ceiling violation
478 Ceiling_Violation := Result = EINVAL;
479 pragma Assert (Result = 0 or else Ceiling_Violation);
480 end Write_Lock;
482 procedure Write_Lock
483 (L : not null access RTS_Lock;
484 Global_Lock : Boolean := False)
486 Result : Interfaces.C.int;
487 begin
488 if not Single_Lock or else Global_Lock then
489 Result := pthread_mutex_lock (L);
490 pragma Assert (Result = 0);
491 end if;
492 end Write_Lock;
494 procedure Write_Lock (T : Task_Id) is
495 Result : Interfaces.C.int;
496 begin
497 if not Single_Lock then
498 Result := pthread_mutex_lock (T.Common.LL.L'Access);
499 pragma Assert (Result = 0);
500 end if;
501 end Write_Lock;
503 ---------------
504 -- Read_Lock --
505 ---------------
507 procedure Read_Lock
508 (L : not null access Lock; Ceiling_Violation : out Boolean) is
509 begin
510 Write_Lock (L, Ceiling_Violation);
511 end Read_Lock;
513 ------------
514 -- Unlock --
515 ------------
517 procedure Unlock (L : not null access Lock) is
518 Result : Interfaces.C.int;
519 begin
520 Result := pthread_mutex_unlock (L.WO'Access);
521 pragma Assert (Result = 0);
522 end Unlock;
524 procedure Unlock
525 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
527 Result : Interfaces.C.int;
528 begin
529 if not Single_Lock or else Global_Lock then
530 Result := pthread_mutex_unlock (L);
531 pragma Assert (Result = 0);
532 end if;
533 end Unlock;
535 procedure Unlock (T : Task_Id) is
536 Result : Interfaces.C.int;
537 begin
538 if not Single_Lock then
539 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
540 pragma Assert (Result = 0);
541 end if;
542 end Unlock;
544 -----------------
545 -- Set_Ceiling --
546 -----------------
548 -- Dynamic priority ceilings are not supported by the underlying system
550 procedure Set_Ceiling
551 (L : not null access Lock;
552 Prio : System.Any_Priority)
554 pragma Unreferenced (L, Prio);
555 begin
556 null;
557 end Set_Ceiling;
559 -----------
560 -- Sleep --
561 -----------
563 procedure Sleep
564 (Self_ID : Task_Id;
565 Reason : System.Tasking.Task_States)
567 pragma Unreferenced (Reason);
569 Result : Interfaces.C.int;
571 begin
572 Result :=
573 pthread_cond_wait
574 (cond => Self_ID.Common.LL.CV'Access,
575 mutex => (if Single_Lock
576 then Single_RTS_Lock'Access
577 else Self_ID.Common.LL.L'Access));
579 -- EINTR is not considered a failure
581 pragma Assert (Result = 0 or else Result = EINTR);
582 end Sleep;
584 -----------------
585 -- Timed_Sleep --
586 -----------------
588 -- This is for use within the run-time system, so abort is
589 -- assumed to be already deferred, and the caller should be
590 -- holding its own ATCB lock.
592 procedure Timed_Sleep
593 (Self_ID : Task_Id;
594 Time : Duration;
595 Mode : ST.Delay_Modes;
596 Reason : Task_States;
597 Timedout : out Boolean;
598 Yielded : out Boolean)
600 pragma Unreferenced (Reason);
602 Base_Time : Duration;
603 Check_Time : Duration;
604 Abs_Time : Duration;
605 Rel_Time : Duration;
607 Request : aliased timespec;
608 Result : Interfaces.C.int;
610 begin
611 Timedout := True;
612 Yielded := False;
614 Compute_Deadline
615 (Time => Time,
616 Mode => Mode,
617 Check_Time => Check_Time,
618 Abs_Time => Abs_Time,
619 Rel_Time => Rel_Time);
620 Base_Time := Check_Time;
622 if Abs_Time > Check_Time then
623 Request :=
624 To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);
626 loop
627 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
629 Result :=
630 pthread_cond_timedwait
631 (cond => Self_ID.Common.LL.CV'Access,
632 mutex => (if Single_Lock
633 then Single_RTS_Lock'Access
634 else Self_ID.Common.LL.L'Access),
635 abstime => Request'Access);
637 Check_Time := Monotonic_Clock;
638 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
640 if Result = 0 or Result = EINTR then
642 -- Somebody may have called Wakeup for us
644 Timedout := False;
645 exit;
646 end if;
648 pragma Assert (Result = ETIMEDOUT);
649 end loop;
650 end if;
651 end Timed_Sleep;
653 -----------------
654 -- Timed_Delay --
655 -----------------
657 -- This is for use in implementing delay statements, so we assume the
658 -- caller is abort-deferred but is holding no locks.
660 procedure Timed_Delay
661 (Self_ID : Task_Id;
662 Time : Duration;
663 Mode : ST.Delay_Modes)
665 Base_Time : Duration;
666 Check_Time : Duration;
667 Abs_Time : Duration;
668 Rel_Time : Duration;
669 Request : aliased timespec;
671 Result : Interfaces.C.int;
672 pragma Warnings (Off, Result);
674 begin
675 if Single_Lock then
676 Lock_RTS;
677 end if;
679 Write_Lock (Self_ID);
681 Compute_Deadline
682 (Time => Time,
683 Mode => Mode,
684 Check_Time => Check_Time,
685 Abs_Time => Abs_Time,
686 Rel_Time => Rel_Time);
687 Base_Time := Check_Time;
689 if Abs_Time > Check_Time then
690 Request :=
691 To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);
692 Self_ID.Common.State := Delay_Sleep;
694 loop
695 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
697 Result :=
698 pthread_cond_timedwait
699 (cond => Self_ID.Common.LL.CV'Access,
700 mutex => (if Single_Lock
701 then Single_RTS_Lock'Access
702 else Self_ID.Common.LL.L'Access),
703 abstime => Request'Access);
705 Check_Time := Monotonic_Clock;
706 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
708 pragma Assert (Result = 0
709 or else Result = ETIMEDOUT
710 or else Result = EINTR);
711 end loop;
713 Self_ID.Common.State := Runnable;
714 end if;
716 Unlock (Self_ID);
718 if Single_Lock then
719 Unlock_RTS;
720 end if;
722 Result := sched_yield;
723 end Timed_Delay;
725 ---------------------
726 -- Monotonic_Clock --
727 ---------------------
729 function Monotonic_Clock return Duration is
730 TS : aliased timespec;
731 Result : Interfaces.C.int;
732 begin
733 Result := clock_gettime
734 (clock_id => OSC.CLOCK_RT_Ada, tp => TS'Unchecked_Access);
735 pragma Assert (Result = 0);
736 return To_Duration (TS);
737 end Monotonic_Clock;
739 -------------------
740 -- RT_Resolution --
741 -------------------
743 function RT_Resolution return Duration is
744 TS : aliased timespec;
745 Result : Interfaces.C.int;
746 begin
747 Result := clock_getres (OSC.CLOCK_REALTIME, TS'Unchecked_Access);
748 pragma Assert (Result = 0);
750 return To_Duration (TS);
751 end RT_Resolution;
753 ------------
754 -- Wakeup --
755 ------------
757 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
758 pragma Unreferenced (Reason);
759 Result : Interfaces.C.int;
760 begin
761 Result := pthread_cond_signal (T.Common.LL.CV'Access);
762 pragma Assert (Result = 0);
763 end Wakeup;
765 -----------
766 -- Yield --
767 -----------
769 procedure Yield (Do_Yield : Boolean := True) is
770 Result : Interfaces.C.int;
771 pragma Unreferenced (Result);
772 begin
773 if Do_Yield then
774 Result := sched_yield;
775 end if;
776 end Yield;
778 ------------------
779 -- Set_Priority --
780 ------------------
782 procedure Set_Priority
783 (T : Task_Id;
784 Prio : System.Any_Priority;
785 Loss_Of_Inheritance : Boolean := False)
787 pragma Unreferenced (Loss_Of_Inheritance);
789 Result : Interfaces.C.int;
790 Param : aliased struct_sched_param;
792 function Get_Policy (Prio : System.Any_Priority) return Character;
793 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
794 -- Get priority specific dispatching policy
796 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
797 -- Upper case first character of the policy name corresponding to the
798 -- task as set by a Priority_Specific_Dispatching pragma.
800 begin
801 T.Common.Current_Priority := Prio;
802 Param.sched_priority := To_Target_Priority (Prio);
804 if Time_Slice_Supported
805 and then (Dispatching_Policy = 'R'
806 or else Priority_Specific_Policy = 'R'
807 or else Time_Slice_Val > 0)
808 then
809 Result := pthread_setschedparam
810 (T.Common.LL.Thread, SCHED_RR, Param'Access);
812 elsif Dispatching_Policy = 'F'
813 or else Priority_Specific_Policy = 'F'
814 or else Time_Slice_Val = 0
815 then
816 Result := pthread_setschedparam
817 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
819 else
820 Result := pthread_setschedparam
821 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
822 end if;
824 pragma Assert (Result = 0);
825 end Set_Priority;
827 ------------------
828 -- Get_Priority --
829 ------------------
831 function Get_Priority (T : Task_Id) return System.Any_Priority is
832 begin
833 return T.Common.Current_Priority;
834 end Get_Priority;
836 ----------------
837 -- Enter_Task --
838 ----------------
840 procedure Enter_Task (Self_ID : Task_Id) is
841 begin
842 Self_ID.Common.LL.Thread := pthread_self;
843 Self_ID.Common.LL.LWP := lwp_self;
845 Specific.Set (Self_ID);
847 if Use_Alternate_Stack then
848 declare
849 Stack : aliased stack_t;
850 Result : Interfaces.C.int;
851 begin
852 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
853 Stack.ss_size := Alternate_Stack_Size;
854 Stack.ss_flags := 0;
855 Result := sigaltstack (Stack'Access, null);
856 pragma Assert (Result = 0);
857 end;
858 end if;
859 end Enter_Task;
861 -------------------
862 -- Is_Valid_Task --
863 -------------------
865 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
867 -----------------------------
868 -- Register_Foreign_Thread --
869 -----------------------------
871 function Register_Foreign_Thread return Task_Id is
872 begin
873 if Is_Valid_Task then
874 return Self;
875 else
876 return Register_Foreign_Thread (pthread_self);
877 end if;
878 end Register_Foreign_Thread;
880 --------------------
881 -- Initialize_TCB --
882 --------------------
884 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
885 Mutex_Attr : aliased pthread_mutexattr_t;
886 Result : Interfaces.C.int;
887 Cond_Attr : aliased pthread_condattr_t;
889 begin
890 -- Give the task a unique serial number
892 Self_ID.Serial_Number := Next_Serial_Number;
893 Next_Serial_Number := Next_Serial_Number + 1;
894 pragma Assert (Next_Serial_Number /= 0);
896 if not Single_Lock then
897 Result := pthread_mutexattr_init (Mutex_Attr'Access);
898 pragma Assert (Result = 0 or else Result = ENOMEM);
900 if Result = 0 then
901 if Locking_Policy = 'C' then
902 Result :=
903 pthread_mutexattr_setprotocol
904 (Mutex_Attr'Access,
905 PTHREAD_PRIO_PROTECT);
906 pragma Assert (Result = 0);
908 Result :=
909 pthread_mutexattr_setprioceiling
910 (Mutex_Attr'Access,
911 Interfaces.C.int (System.Any_Priority'Last));
912 pragma Assert (Result = 0);
914 elsif Locking_Policy = 'I' then
915 Result :=
916 pthread_mutexattr_setprotocol
917 (Mutex_Attr'Access,
918 PTHREAD_PRIO_INHERIT);
919 pragma Assert (Result = 0);
920 end if;
922 Result :=
923 pthread_mutex_init
924 (Self_ID.Common.LL.L'Access,
925 Mutex_Attr'Access);
926 pragma Assert (Result = 0 or else Result = ENOMEM);
927 end if;
929 if Result /= 0 then
930 Succeeded := False;
931 return;
932 end if;
934 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
935 pragma Assert (Result = 0);
936 end if;
938 Result := pthread_condattr_init (Cond_Attr'Access);
939 pragma Assert (Result = 0 or else Result = ENOMEM);
941 if Result = 0 then
942 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
943 pragma Assert (Result = 0);
945 Result :=
946 pthread_cond_init
947 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
948 pragma Assert (Result = 0 or else Result = ENOMEM);
949 end if;
951 if Result = 0 then
952 Succeeded := True;
953 else
954 if not Single_Lock then
955 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
956 pragma Assert (Result = 0);
957 end if;
959 Succeeded := False;
960 end if;
962 Result := pthread_condattr_destroy (Cond_Attr'Access);
963 pragma Assert (Result = 0);
964 end Initialize_TCB;
966 -----------------
967 -- Create_Task --
968 -----------------
970 procedure Create_Task
971 (T : Task_Id;
972 Wrapper : System.Address;
973 Stack_Size : System.Parameters.Size_Type;
974 Priority : System.Any_Priority;
975 Succeeded : out Boolean)
977 Attributes : aliased pthread_attr_t;
978 Adjusted_Stack_Size : Interfaces.C.size_t;
979 Page_Size : constant Interfaces.C.size_t :=
980 Interfaces.C.size_t (Get_Page_Size);
981 Result : Interfaces.C.int;
983 function Thread_Body_Access is new
984 Ada.Unchecked_Conversion (System.Address, Thread_Body);
986 use System.Task_Info;
988 begin
989 Adjusted_Stack_Size :=
990 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
992 if Stack_Base_Available then
994 -- If Stack Checking is supported then allocate 2 additional pages:
996 -- In the worst case, stack is allocated at something like
997 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
998 -- to be sure the effective stack size is greater than what
999 -- has been asked.
1001 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
1002 end if;
1004 -- Round stack size as this is required by some OSes (Darwin)
1006 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
1007 Adjusted_Stack_Size :=
1008 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
1010 Result := pthread_attr_init (Attributes'Access);
1011 pragma Assert (Result = 0 or else Result = ENOMEM);
1013 if Result /= 0 then
1014 Succeeded := False;
1015 return;
1016 end if;
1018 Result :=
1019 pthread_attr_setdetachstate
1020 (Attributes'Access, PTHREAD_CREATE_DETACHED);
1021 pragma Assert (Result = 0);
1023 Result :=
1024 pthread_attr_setstacksize
1025 (Attributes'Access, Adjusted_Stack_Size);
1026 pragma Assert (Result = 0);
1028 if T.Common.Task_Info /= Default_Scope then
1029 case T.Common.Task_Info is
1030 when System.Task_Info.Process_Scope =>
1031 Result :=
1032 pthread_attr_setscope
1033 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
1035 when System.Task_Info.System_Scope =>
1036 Result :=
1037 pthread_attr_setscope
1038 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
1040 when System.Task_Info.Default_Scope =>
1041 Result := 0;
1042 end case;
1044 pragma Assert (Result = 0);
1045 end if;
1047 -- Since the initial signal mask of a thread is inherited from the
1048 -- creator, and the Environment task has all its signals masked, we
1049 -- do not need to manipulate caller's signal mask at this point.
1050 -- All tasks in RTS will have All_Tasks_Mask initially.
1052 -- Note: the use of Unrestricted_Access in the following call is needed
1053 -- because otherwise we have an error of getting a access-to-volatile
1054 -- value which points to a non-volatile object. But in this case it is
1055 -- safe to do this, since we know we have no problems with aliasing and
1056 -- Unrestricted_Access bypasses this check.
1058 Result := pthread_create
1059 (T.Common.LL.Thread'Unrestricted_Access,
1060 Attributes'Access,
1061 Thread_Body_Access (Wrapper),
1062 To_Address (T));
1063 pragma Assert (Result = 0 or else Result = EAGAIN);
1065 Succeeded := Result = 0;
1067 Result := pthread_attr_destroy (Attributes'Access);
1068 pragma Assert (Result = 0);
1070 if Succeeded then
1071 Set_Priority (T, Priority);
1072 end if;
1073 end Create_Task;
1075 ------------------
1076 -- Finalize_TCB --
1077 ------------------
1079 procedure Finalize_TCB (T : Task_Id) is
1080 Result : Interfaces.C.int;
1082 begin
1083 if not Single_Lock then
1084 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1085 pragma Assert (Result = 0);
1086 end if;
1088 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1089 pragma Assert (Result = 0);
1091 if T.Known_Tasks_Index /= -1 then
1092 Known_Tasks (T.Known_Tasks_Index) := null;
1093 end if;
1095 ATCB_Allocation.Free_ATCB (T);
1096 end Finalize_TCB;
1098 ---------------
1099 -- Exit_Task --
1100 ---------------
1102 procedure Exit_Task is
1103 begin
1104 -- Mark this task as unknown, so that if Self is called, it won't
1105 -- return a dangling pointer.
1107 Specific.Set (null);
1108 end Exit_Task;
1110 ----------------
1111 -- Abort_Task --
1112 ----------------
1114 procedure Abort_Task (T : Task_Id) is
1115 Result : Interfaces.C.int;
1116 begin
1117 if Abort_Handler_Installed then
1118 Result :=
1119 pthread_kill
1120 (T.Common.LL.Thread,
1121 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1122 pragma Assert (Result = 0);
1123 end if;
1124 end Abort_Task;
1126 ----------------
1127 -- Initialize --
1128 ----------------
1130 procedure Initialize (S : in out Suspension_Object) is
1131 Mutex_Attr : aliased pthread_mutexattr_t;
1132 Cond_Attr : aliased pthread_condattr_t;
1133 Result : Interfaces.C.int;
1135 begin
1136 -- Initialize internal state (always to False (RM D.10 (6)))
1138 S.State := False;
1139 S.Waiting := False;
1141 -- Initialize internal mutex
1143 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1144 pragma Assert (Result = 0 or else Result = ENOMEM);
1146 if Result = ENOMEM then
1147 raise Storage_Error;
1148 end if;
1150 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1151 pragma Assert (Result = 0 or else Result = ENOMEM);
1153 if Result = ENOMEM then
1154 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1155 pragma Assert (Result = 0);
1157 raise Storage_Error;
1158 end if;
1160 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1161 pragma Assert (Result = 0);
1163 -- Initialize internal condition variable
1165 Result := pthread_condattr_init (Cond_Attr'Access);
1166 pragma Assert (Result = 0 or else Result = ENOMEM);
1168 if Result /= 0 then
1169 Result := pthread_mutex_destroy (S.L'Access);
1170 pragma Assert (Result = 0);
1172 -- Storage_Error is propagated as intended if the allocation of the
1173 -- underlying OS entities fails.
1175 raise Storage_Error;
1177 else
1178 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
1179 pragma Assert (Result = 0);
1180 end if;
1182 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1183 pragma Assert (Result = 0 or else Result = ENOMEM);
1185 if Result /= 0 then
1186 Result := pthread_mutex_destroy (S.L'Access);
1187 pragma Assert (Result = 0);
1189 Result := pthread_condattr_destroy (Cond_Attr'Access);
1190 pragma Assert (Result = 0);
1192 -- Storage_Error is propagated as intended if the allocation of the
1193 -- underlying OS entities fails.
1195 raise Storage_Error;
1196 end if;
1198 Result := pthread_condattr_destroy (Cond_Attr'Access);
1199 pragma Assert (Result = 0);
1200 end Initialize;
1202 --------------
1203 -- Finalize --
1204 --------------
1206 procedure Finalize (S : in out Suspension_Object) is
1207 Result : Interfaces.C.int;
1209 begin
1210 -- Destroy internal mutex
1212 Result := pthread_mutex_destroy (S.L'Access);
1213 pragma Assert (Result = 0);
1215 -- Destroy internal condition variable
1217 Result := pthread_cond_destroy (S.CV'Access);
1218 pragma Assert (Result = 0);
1219 end Finalize;
1221 -------------------
1222 -- Current_State --
1223 -------------------
1225 function Current_State (S : Suspension_Object) return Boolean is
1226 begin
1227 -- We do not want to use lock on this read operation. State is marked
1228 -- as Atomic so that we ensure that the value retrieved is correct.
1230 return S.State;
1231 end Current_State;
1233 ---------------
1234 -- Set_False --
1235 ---------------
1237 procedure Set_False (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 S.State := False;
1248 Result := pthread_mutex_unlock (S.L'Access);
1249 pragma Assert (Result = 0);
1251 SSL.Abort_Undefer.all;
1252 end Set_False;
1254 --------------
1255 -- Set_True --
1256 --------------
1258 procedure Set_True (S : in out Suspension_Object) is
1259 Result : Interfaces.C.int;
1261 begin
1262 SSL.Abort_Defer.all;
1264 Result := pthread_mutex_lock (S.L'Access);
1265 pragma Assert (Result = 0);
1267 -- If there is already a task waiting on this suspension object then
1268 -- we resume it, leaving the state of the suspension object to False,
1269 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1270 -- the state to True.
1272 if S.Waiting then
1273 S.Waiting := False;
1274 S.State := False;
1276 Result := pthread_cond_signal (S.CV'Access);
1277 pragma Assert (Result = 0);
1279 else
1280 S.State := True;
1281 end if;
1283 Result := pthread_mutex_unlock (S.L'Access);
1284 pragma Assert (Result = 0);
1286 SSL.Abort_Undefer.all;
1287 end Set_True;
1289 ------------------------
1290 -- Suspend_Until_True --
1291 ------------------------
1293 procedure Suspend_Until_True (S : in out Suspension_Object) is
1294 Result : Interfaces.C.int;
1296 begin
1297 SSL.Abort_Defer.all;
1299 Result := pthread_mutex_lock (S.L'Access);
1300 pragma Assert (Result = 0);
1302 if S.Waiting then
1304 -- Program_Error must be raised upon calling Suspend_Until_True
1305 -- if another task is already waiting on that suspension object
1306 -- (RM D.10(10)).
1308 Result := pthread_mutex_unlock (S.L'Access);
1309 pragma Assert (Result = 0);
1311 SSL.Abort_Undefer.all;
1313 raise Program_Error;
1315 else
1316 -- Suspend the task if the state is False. Otherwise, the task
1317 -- continues its execution, and the state of the suspension object
1318 -- is set to False (ARM D.10 par. 9).
1320 if S.State then
1321 S.State := False;
1322 else
1323 S.Waiting := True;
1325 loop
1326 -- Loop in case pthread_cond_wait returns earlier than expected
1327 -- (e.g. in case of EINTR caused by a signal).
1329 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1330 pragma Assert (Result = 0 or else Result = EINTR);
1332 exit when not S.Waiting;
1333 end loop;
1334 end if;
1336 Result := pthread_mutex_unlock (S.L'Access);
1337 pragma Assert (Result = 0);
1339 SSL.Abort_Undefer.all;
1340 end if;
1341 end Suspend_Until_True;
1343 ----------------
1344 -- Check_Exit --
1345 ----------------
1347 -- Dummy version
1349 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1350 pragma Unreferenced (Self_ID);
1351 begin
1352 return True;
1353 end Check_Exit;
1355 --------------------
1356 -- Check_No_Locks --
1357 --------------------
1359 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1360 pragma Unreferenced (Self_ID);
1361 begin
1362 return True;
1363 end Check_No_Locks;
1365 ----------------------
1366 -- Environment_Task --
1367 ----------------------
1369 function Environment_Task return Task_Id is
1370 begin
1371 return Environment_Task_Id;
1372 end Environment_Task;
1374 --------------
1375 -- Lock_RTS --
1376 --------------
1378 procedure Lock_RTS is
1379 begin
1380 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1381 end Lock_RTS;
1383 ----------------
1384 -- Unlock_RTS --
1385 ----------------
1387 procedure Unlock_RTS is
1388 begin
1389 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1390 end Unlock_RTS;
1392 ------------------
1393 -- Suspend_Task --
1394 ------------------
1396 function Suspend_Task
1397 (T : ST.Task_Id;
1398 Thread_Self : Thread_Id) return Boolean
1400 pragma Unreferenced (T, Thread_Self);
1401 begin
1402 return False;
1403 end Suspend_Task;
1405 -----------------
1406 -- Resume_Task --
1407 -----------------
1409 function Resume_Task
1410 (T : ST.Task_Id;
1411 Thread_Self : Thread_Id) return Boolean
1413 pragma Unreferenced (T, Thread_Self);
1414 begin
1415 return False;
1416 end Resume_Task;
1418 --------------------
1419 -- Stop_All_Tasks --
1420 --------------------
1422 procedure Stop_All_Tasks is
1423 begin
1424 null;
1425 end Stop_All_Tasks;
1427 ---------------
1428 -- Stop_Task --
1429 ---------------
1431 function Stop_Task (T : ST.Task_Id) return Boolean is
1432 pragma Unreferenced (T);
1433 begin
1434 return False;
1435 end Stop_Task;
1437 -------------------
1438 -- Continue_Task --
1439 -------------------
1441 function Continue_Task (T : ST.Task_Id) return Boolean is
1442 pragma Unreferenced (T);
1443 begin
1444 return False;
1445 end Continue_Task;
1447 ----------------
1448 -- Initialize --
1449 ----------------
1451 procedure Initialize (Environment_Task : Task_Id) is
1452 act : aliased struct_sigaction;
1453 old_act : aliased struct_sigaction;
1454 Tmp_Set : aliased sigset_t;
1455 Result : Interfaces.C.int;
1457 function State
1458 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1459 pragma Import (C, State, "__gnat_get_interrupt_state");
1460 -- Get interrupt state. Defined in a-init.c
1461 -- The input argument is the interrupt number,
1462 -- and the result is one of the following:
1464 Default : constant Character := 's';
1465 -- 'n' this interrupt not set by any Interrupt_State pragma
1466 -- 'u' Interrupt_State pragma set state to User
1467 -- 'r' Interrupt_State pragma set state to Runtime
1468 -- 's' Interrupt_State pragma set state to System (use "default"
1469 -- system handler)
1471 begin
1472 Environment_Task_Id := Environment_Task;
1474 Interrupt_Management.Initialize;
1476 -- Prepare the set of signals that should unblocked in all tasks
1478 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1479 pragma Assert (Result = 0);
1481 for J in Interrupt_Management.Interrupt_ID loop
1482 if System.Interrupt_Management.Keep_Unmasked (J) then
1483 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1484 pragma Assert (Result = 0);
1485 end if;
1486 end loop;
1488 -- Initialize the lock used to synchronize chain of all ATCBs
1490 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1492 Specific.Initialize (Environment_Task);
1494 if Use_Alternate_Stack then
1495 Environment_Task.Common.Task_Alternate_Stack :=
1496 Alternate_Stack'Address;
1497 end if;
1499 -- Make environment task known here because it doesn't go through
1500 -- Activate_Tasks, which does it for all other tasks.
1502 Known_Tasks (Known_Tasks'First) := Environment_Task;
1503 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1505 Enter_Task (Environment_Task);
1507 if State
1508 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1509 then
1510 act.sa_flags := 0;
1511 act.sa_handler := Abort_Handler'Address;
1513 Result := sigemptyset (Tmp_Set'Access);
1514 pragma Assert (Result = 0);
1515 act.sa_mask := Tmp_Set;
1517 Result :=
1518 sigaction
1519 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1520 act'Unchecked_Access,
1521 old_act'Unchecked_Access);
1522 pragma Assert (Result = 0);
1523 Abort_Handler_Installed := True;
1524 end if;
1525 end Initialize;
1527 -----------------------
1528 -- Set_Task_Affinity --
1529 -----------------------
1531 procedure Set_Task_Affinity (T : ST.Task_Id) is
1532 pragma Unreferenced (T);
1534 begin
1535 -- Setting task affinity is not supported by the underlying system
1537 null;
1538 end Set_Task_Affinity;
1540 end System.Task_Primitives.Operations;