Daily bump.
[official-gcc.git] / gcc / ada / s-taprop-posix.adb
blobac147000b7a8d9d5d0ab46ac1533facae4d8ade7
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-2009, 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;
48 with Ada.Unchecked_Deallocation;
50 with Interfaces.C;
52 with System.Tasking.Debug;
53 with System.Interrupt_Management;
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 SSL renames System.Soft_Links;
67 use System.Tasking.Debug;
68 use System.Tasking;
69 use Interfaces.C;
70 use System.OS_Interface;
71 use System.Parameters;
72 use System.OS_Primitives;
74 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
75 -- Whether to use an alternate signal stack for stack overflows
77 ----------------
78 -- Local Data --
79 ----------------
81 -- The followings are logically constants, but need to be initialized
82 -- at run time.
84 Single_RTS_Lock : aliased RTS_Lock;
85 -- This is a lock to allow only one thread of control in the RTS at
86 -- a time; it is used to execute in mutual exclusion from all other tasks.
87 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
89 ATCB_Key : aliased pthread_key_t;
90 -- Key used to find the Ada Task_Id associated with a thread
92 Environment_Task_Id : Task_Id;
93 -- A variable to hold Task_Id for the environment task
95 Locking_Policy : Character;
96 pragma Import (C, Locking_Policy, "__gl_locking_policy");
97 -- Value of the pragma Locking_Policy:
98 -- 'C' for Ceiling_Locking
99 -- 'I' for Inherit_Locking
100 -- ' ' for none.
102 Unblocked_Signal_Mask : aliased sigset_t;
103 -- The set of signals that should unblocked in all tasks
105 -- The followings are internal configuration constants needed
107 Next_Serial_Number : Task_Serial_Number := 100;
108 -- We start at 100, to reserve some special values for
109 -- using in error checking.
111 Time_Slice_Val : Integer;
112 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
114 Dispatching_Policy : Character;
115 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
117 Foreign_Task_Elaborated : aliased Boolean := True;
118 -- Used to identified fake tasks (i.e., non-Ada Threads)
120 --------------------
121 -- Local Packages --
122 --------------------
124 package Specific is
126 procedure Initialize (Environment_Task : Task_Id);
127 pragma Inline (Initialize);
128 -- Initialize various data needed by this package
130 function Is_Valid_Task return Boolean;
131 pragma Inline (Is_Valid_Task);
132 -- Does executing thread have a TCB?
134 procedure Set (Self_Id : Task_Id);
135 pragma Inline (Set);
136 -- Set the self id for the current task
138 function Self return Task_Id;
139 pragma Inline (Self);
140 -- Return a pointer to the Ada Task Control Block of the calling task
142 end Specific;
144 package body Specific is separate;
145 -- The body of this package is target specific
147 ---------------------------------
148 -- Support for foreign threads --
149 ---------------------------------
151 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
152 -- Allocate and Initialize a new ATCB for the current Thread
154 function Register_Foreign_Thread
155 (Thread : Thread_Id) return Task_Id is separate;
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 procedure Abort_Handler (Sig : Signal);
162 -- Signal handler used to implement asynchronous abort.
163 -- See also comment before body, below.
165 function To_Address is
166 new Ada.Unchecked_Conversion (Task_Id, System.Address);
168 -------------------
169 -- Abort_Handler --
170 -------------------
172 -- Target-dependent binding of inter-thread Abort signal to the raising of
173 -- the Abort_Signal exception.
175 -- The technical issues and alternatives here are essentially the
176 -- same as for raising exceptions in response to other signals
177 -- (e.g. Storage_Error). See code and comments in the package body
178 -- System.Interrupt_Management.
180 -- Some implementations may not allow an exception to be propagated out of
181 -- a handler, and others might leave the signal or interrupt that invoked
182 -- this handler masked after the exceptional return to the application
183 -- code.
185 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
186 -- most UNIX systems, this will allow transfer out of a signal handler,
187 -- which is usually the only mechanism available for implementing
188 -- asynchronous handlers of this kind. However, some systems do not
189 -- restore the signal mask on longjmp(), leaving the abort signal masked.
191 procedure Abort_Handler (Sig : Signal) is
192 pragma Unreferenced (Sig);
194 T : constant Task_Id := Self;
195 Old_Set : aliased sigset_t;
197 Result : Interfaces.C.int;
198 pragma Warnings (Off, Result);
200 begin
201 -- It is not safe to raise an exception when using ZCX and the GCC
202 -- exception handling mechanism.
204 if ZCX_By_Default and then GCC_ZCX_Support then
205 return;
206 end if;
208 if T.Deferral_Level = 0
209 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
210 not T.Aborting
211 then
212 T.Aborting := True;
214 -- Make sure signals used for RTS internal purpose are unmasked
216 Result := pthread_sigmask (SIG_UNBLOCK,
217 Unblocked_Signal_Mask'Access, Old_Set'Access);
218 pragma Assert (Result = 0);
220 raise Standard'Abort_Signal;
221 end if;
222 end Abort_Handler;
224 -----------------
225 -- Stack_Guard --
226 -----------------
228 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
229 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
230 Guard_Page_Address : Address;
232 Res : Interfaces.C.int;
234 begin
235 if Stack_Base_Available then
237 -- Compute the guard page address
239 Guard_Page_Address :=
240 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
242 if On then
243 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
244 else
245 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
246 end if;
248 pragma Assert (Res = 0);
249 end if;
250 end Stack_Guard;
252 --------------------
253 -- Get_Thread_Id --
254 --------------------
256 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
257 begin
258 return T.Common.LL.Thread;
259 end Get_Thread_Id;
261 ----------
262 -- Self --
263 ----------
265 function Self return Task_Id renames Specific.Self;
267 ---------------------
268 -- Initialize_Lock --
269 ---------------------
271 -- Note: mutexes and cond_variables needed per-task basis are
272 -- initialized in Initialize_TCB and the Storage_Error is
273 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
274 -- used in RTS is initialized before any status change of RTS.
275 -- Therefore raising Storage_Error in the following routines
276 -- should be able to be handled safely.
278 procedure Initialize_Lock
279 (Prio : System.Any_Priority;
280 L : not null access Lock)
282 Attributes : aliased pthread_mutexattr_t;
283 Result : Interfaces.C.int;
285 begin
286 Result := pthread_mutexattr_init (Attributes'Access);
287 pragma Assert (Result = 0 or else Result = ENOMEM);
289 if Result = ENOMEM then
290 raise Storage_Error;
291 end if;
293 if Locking_Policy = 'C' then
294 Result := pthread_mutexattr_setprotocol
295 (Attributes'Access, PTHREAD_PRIO_PROTECT);
296 pragma Assert (Result = 0);
298 Result := pthread_mutexattr_setprioceiling
299 (Attributes'Access, Interfaces.C.int (Prio));
300 pragma Assert (Result = 0);
302 elsif Locking_Policy = 'I' then
303 Result := pthread_mutexattr_setprotocol
304 (Attributes'Access, PTHREAD_PRIO_INHERIT);
305 pragma Assert (Result = 0);
306 end if;
308 Result := pthread_mutex_init (L, Attributes'Access);
309 pragma Assert (Result = 0 or else Result = ENOMEM);
311 if Result = ENOMEM then
312 Result := pthread_mutexattr_destroy (Attributes'Access);
313 raise Storage_Error;
314 end if;
316 Result := pthread_mutexattr_destroy (Attributes'Access);
317 pragma Assert (Result = 0);
318 end Initialize_Lock;
320 procedure Initialize_Lock
321 (L : not null access RTS_Lock; Level : Lock_Level)
323 pragma Unreferenced (Level);
325 Attributes : aliased pthread_mutexattr_t;
326 Result : Interfaces.C.int;
328 begin
329 Result := pthread_mutexattr_init (Attributes'Access);
330 pragma Assert (Result = 0 or else Result = ENOMEM);
332 if Result = ENOMEM then
333 raise Storage_Error;
334 end if;
336 if Locking_Policy = 'C' then
337 Result := pthread_mutexattr_setprotocol
338 (Attributes'Access, PTHREAD_PRIO_PROTECT);
339 pragma Assert (Result = 0);
341 Result := pthread_mutexattr_setprioceiling
342 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
343 pragma Assert (Result = 0);
345 elsif Locking_Policy = 'I' then
346 Result := pthread_mutexattr_setprotocol
347 (Attributes'Access, PTHREAD_PRIO_INHERIT);
348 pragma Assert (Result = 0);
349 end if;
351 Result := pthread_mutex_init (L, Attributes'Access);
352 pragma Assert (Result = 0 or else Result = ENOMEM);
354 if Result = ENOMEM then
355 Result := pthread_mutexattr_destroy (Attributes'Access);
356 raise Storage_Error;
357 end if;
359 Result := pthread_mutexattr_destroy (Attributes'Access);
360 pragma Assert (Result = 0);
361 end Initialize_Lock;
363 -------------------
364 -- Finalize_Lock --
365 -------------------
367 procedure Finalize_Lock (L : not null access Lock) is
368 Result : Interfaces.C.int;
369 begin
370 Result := pthread_mutex_destroy (L);
371 pragma Assert (Result = 0);
372 end Finalize_Lock;
374 procedure Finalize_Lock (L : not null access RTS_Lock) is
375 Result : Interfaces.C.int;
376 begin
377 Result := pthread_mutex_destroy (L);
378 pragma Assert (Result = 0);
379 end Finalize_Lock;
381 ----------------
382 -- Write_Lock --
383 ----------------
385 procedure Write_Lock
386 (L : not null access Lock; Ceiling_Violation : out Boolean)
388 Result : Interfaces.C.int;
390 begin
391 Result := pthread_mutex_lock (L);
393 -- Assume that the cause of EINVAL is a priority ceiling violation
395 Ceiling_Violation := (Result = EINVAL);
396 pragma Assert (Result = 0 or else Result = EINVAL);
397 end Write_Lock;
399 procedure Write_Lock
400 (L : not null access RTS_Lock;
401 Global_Lock : Boolean := False)
403 Result : Interfaces.C.int;
404 begin
405 if not Single_Lock or else Global_Lock then
406 Result := pthread_mutex_lock (L);
407 pragma Assert (Result = 0);
408 end if;
409 end Write_Lock;
411 procedure Write_Lock (T : Task_Id) is
412 Result : Interfaces.C.int;
413 begin
414 if not Single_Lock then
415 Result := pthread_mutex_lock (T.Common.LL.L'Access);
416 pragma Assert (Result = 0);
417 end if;
418 end Write_Lock;
420 ---------------
421 -- Read_Lock --
422 ---------------
424 procedure Read_Lock
425 (L : not null access Lock; Ceiling_Violation : out Boolean) is
426 begin
427 Write_Lock (L, Ceiling_Violation);
428 end Read_Lock;
430 ------------
431 -- Unlock --
432 ------------
434 procedure Unlock (L : not null access Lock) is
435 Result : Interfaces.C.int;
436 begin
437 Result := pthread_mutex_unlock (L);
438 pragma Assert (Result = 0);
439 end Unlock;
441 procedure Unlock
442 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
444 Result : Interfaces.C.int;
445 begin
446 if not Single_Lock or else Global_Lock then
447 Result := pthread_mutex_unlock (L);
448 pragma Assert (Result = 0);
449 end if;
450 end Unlock;
452 procedure Unlock (T : Task_Id) is
453 Result : Interfaces.C.int;
454 begin
455 if not Single_Lock then
456 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
457 pragma Assert (Result = 0);
458 end if;
459 end Unlock;
461 -----------------
462 -- Set_Ceiling --
463 -----------------
465 -- Dynamic priority ceilings are not supported by the underlying system
467 procedure Set_Ceiling
468 (L : not null access Lock;
469 Prio : System.Any_Priority)
471 pragma Unreferenced (L, Prio);
472 begin
473 null;
474 end Set_Ceiling;
476 -----------
477 -- Sleep --
478 -----------
480 procedure Sleep
481 (Self_ID : Task_Id;
482 Reason : System.Tasking.Task_States)
484 pragma Unreferenced (Reason);
486 Result : Interfaces.C.int;
488 begin
489 if Single_Lock then
490 Result :=
491 pthread_cond_wait
492 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
493 else
494 Result :=
495 pthread_cond_wait
496 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
497 end if;
499 -- EINTR is not considered a failure
501 pragma Assert (Result = 0 or else Result = EINTR);
502 end Sleep;
504 -----------------
505 -- Timed_Sleep --
506 -----------------
508 -- This is for use within the run-time system, so abort is
509 -- assumed to be already deferred, and the caller should be
510 -- holding its own ATCB lock.
512 procedure Timed_Sleep
513 (Self_ID : Task_Id;
514 Time : Duration;
515 Mode : ST.Delay_Modes;
516 Reason : Task_States;
517 Timedout : out Boolean;
518 Yielded : out Boolean)
520 pragma Unreferenced (Reason);
522 Base_Time : constant Duration := Monotonic_Clock;
523 Check_Time : Duration := Base_Time;
524 Rel_Time : Duration;
525 Abs_Time : Duration;
526 Request : aliased timespec;
527 Result : Interfaces.C.int;
529 begin
530 Timedout := True;
531 Yielded := False;
533 if Mode = Relative then
534 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
536 if Relative_Timed_Wait then
537 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
538 end if;
540 else
541 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
543 if Relative_Timed_Wait then
544 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
545 end if;
546 end if;
548 if Abs_Time > Check_Time then
549 if Relative_Timed_Wait then
550 Request := To_Timespec (Rel_Time);
551 else
552 Request := To_Timespec (Abs_Time);
553 end if;
555 loop
556 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
558 if Single_Lock then
559 Result :=
560 pthread_cond_timedwait
561 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
562 Request'Access);
564 else
565 Result :=
566 pthread_cond_timedwait
567 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
568 Request'Access);
569 end if;
571 Check_Time := Monotonic_Clock;
572 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
574 if Result = 0 or Result = EINTR then
576 -- Somebody may have called Wakeup for us
578 Timedout := False;
579 exit;
580 end if;
582 pragma Assert (Result = ETIMEDOUT);
583 end loop;
584 end if;
585 end Timed_Sleep;
587 -----------------
588 -- Timed_Delay --
589 -----------------
591 -- This is for use in implementing delay statements, so we assume the
592 -- caller is abort-deferred but is holding no locks.
594 procedure Timed_Delay
595 (Self_ID : Task_Id;
596 Time : Duration;
597 Mode : ST.Delay_Modes)
599 Base_Time : constant Duration := Monotonic_Clock;
600 Check_Time : Duration := Base_Time;
601 Abs_Time : Duration;
602 Rel_Time : Duration;
603 Request : aliased timespec;
605 Result : Interfaces.C.int;
606 pragma Warnings (Off, Result);
608 begin
609 if Single_Lock then
610 Lock_RTS;
611 end if;
613 Write_Lock (Self_ID);
615 if Mode = Relative then
616 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
618 if Relative_Timed_Wait then
619 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
620 end if;
622 else
623 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
625 if Relative_Timed_Wait then
626 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
627 end if;
628 end if;
630 if Abs_Time > Check_Time then
631 if Relative_Timed_Wait then
632 Request := To_Timespec (Rel_Time);
633 else
634 Request := To_Timespec (Abs_Time);
635 end if;
637 Self_ID.Common.State := Delay_Sleep;
639 loop
640 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
642 if Single_Lock then
643 Result := pthread_cond_timedwait
644 (Self_ID.Common.LL.CV'Access,
645 Single_RTS_Lock'Access,
646 Request'Access);
647 else
648 Result := pthread_cond_timedwait
649 (Self_ID.Common.LL.CV'Access,
650 Self_ID.Common.LL.L'Access,
651 Request'Access);
652 end if;
654 Check_Time := Monotonic_Clock;
655 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
657 pragma Assert (Result = 0
658 or else Result = ETIMEDOUT
659 or else Result = EINTR);
660 end loop;
662 Self_ID.Common.State := Runnable;
663 end if;
665 Unlock (Self_ID);
667 if Single_Lock then
668 Unlock_RTS;
669 end if;
671 Result := sched_yield;
672 end Timed_Delay;
674 ---------------------
675 -- Monotonic_Clock --
676 ---------------------
678 function Monotonic_Clock return Duration is
679 TS : aliased timespec;
680 Result : Interfaces.C.int;
681 begin
682 Result := clock_gettime
683 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
684 pragma Assert (Result = 0);
685 return To_Duration (TS);
686 end Monotonic_Clock;
688 -------------------
689 -- RT_Resolution --
690 -------------------
692 function RT_Resolution return Duration is
693 begin
694 return 10#1.0#E-6;
695 end RT_Resolution;
697 ------------
698 -- Wakeup --
699 ------------
701 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
702 pragma Unreferenced (Reason);
703 Result : Interfaces.C.int;
704 begin
705 Result := pthread_cond_signal (T.Common.LL.CV'Access);
706 pragma Assert (Result = 0);
707 end Wakeup;
709 -----------
710 -- Yield --
711 -----------
713 procedure Yield (Do_Yield : Boolean := True) is
714 Result : Interfaces.C.int;
715 pragma Unreferenced (Result);
716 begin
717 if Do_Yield then
718 Result := sched_yield;
719 end if;
720 end Yield;
722 ------------------
723 -- Set_Priority --
724 ------------------
726 procedure Set_Priority
727 (T : Task_Id;
728 Prio : System.Any_Priority;
729 Loss_Of_Inheritance : Boolean := False)
731 pragma Unreferenced (Loss_Of_Inheritance);
733 Result : Interfaces.C.int;
734 Param : aliased struct_sched_param;
736 function Get_Policy (Prio : System.Any_Priority) return Character;
737 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
738 -- Get priority specific dispatching policy
740 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
741 -- Upper case first character of the policy name corresponding to the
742 -- task as set by a Priority_Specific_Dispatching pragma.
744 begin
745 T.Common.Current_Priority := Prio;
746 Param.sched_priority := To_Target_Priority (Prio);
748 if Time_Slice_Supported
749 and then (Dispatching_Policy = 'R'
750 or else Priority_Specific_Policy = 'R'
751 or else Time_Slice_Val > 0)
752 then
753 Result := pthread_setschedparam
754 (T.Common.LL.Thread, SCHED_RR, Param'Access);
756 elsif Dispatching_Policy = 'F'
757 or else Priority_Specific_Policy = 'F'
758 or else Time_Slice_Val = 0
759 then
760 Result := pthread_setschedparam
761 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
763 else
764 Result := pthread_setschedparam
765 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
766 end if;
768 pragma Assert (Result = 0);
769 end Set_Priority;
771 ------------------
772 -- Get_Priority --
773 ------------------
775 function Get_Priority (T : Task_Id) return System.Any_Priority is
776 begin
777 return T.Common.Current_Priority;
778 end Get_Priority;
780 ----------------
781 -- Enter_Task --
782 ----------------
784 procedure Enter_Task (Self_ID : Task_Id) is
785 begin
786 Self_ID.Common.LL.Thread := pthread_self;
787 Self_ID.Common.LL.LWP := lwp_self;
789 Specific.Set (Self_ID);
791 if Use_Alternate_Stack then
792 declare
793 Stack : aliased stack_t;
794 Result : Interfaces.C.int;
795 begin
796 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
797 Stack.ss_size := Alternate_Stack_Size;
798 Stack.ss_flags := 0;
799 Result := sigaltstack (Stack'Access, null);
800 pragma Assert (Result = 0);
801 end;
802 end if;
803 end Enter_Task;
805 --------------
806 -- New_ATCB --
807 --------------
809 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
810 begin
811 return new Ada_Task_Control_Block (Entry_Num);
812 end New_ATCB;
814 -------------------
815 -- Is_Valid_Task --
816 -------------------
818 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
820 -----------------------------
821 -- Register_Foreign_Thread --
822 -----------------------------
824 function Register_Foreign_Thread return Task_Id is
825 begin
826 if Is_Valid_Task then
827 return Self;
828 else
829 return Register_Foreign_Thread (pthread_self);
830 end if;
831 end Register_Foreign_Thread;
833 --------------------
834 -- Initialize_TCB --
835 --------------------
837 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
838 Mutex_Attr : aliased pthread_mutexattr_t;
839 Result : Interfaces.C.int;
840 Cond_Attr : aliased pthread_condattr_t;
842 begin
843 -- Give the task a unique serial number
845 Self_ID.Serial_Number := Next_Serial_Number;
846 Next_Serial_Number := Next_Serial_Number + 1;
847 pragma Assert (Next_Serial_Number /= 0);
849 if not Single_Lock then
850 Result := pthread_mutexattr_init (Mutex_Attr'Access);
851 pragma Assert (Result = 0 or else Result = ENOMEM);
853 if Result = 0 then
854 if Locking_Policy = 'C' then
855 Result :=
856 pthread_mutexattr_setprotocol
857 (Mutex_Attr'Access,
858 PTHREAD_PRIO_PROTECT);
859 pragma Assert (Result = 0);
861 Result :=
862 pthread_mutexattr_setprioceiling
863 (Mutex_Attr'Access,
864 Interfaces.C.int (System.Any_Priority'Last));
865 pragma Assert (Result = 0);
867 elsif Locking_Policy = 'I' then
868 Result :=
869 pthread_mutexattr_setprotocol
870 (Mutex_Attr'Access,
871 PTHREAD_PRIO_INHERIT);
872 pragma Assert (Result = 0);
873 end if;
875 Result :=
876 pthread_mutex_init
877 (Self_ID.Common.LL.L'Access,
878 Mutex_Attr'Access);
879 pragma Assert (Result = 0 or else Result = ENOMEM);
880 end if;
882 if Result /= 0 then
883 Succeeded := False;
884 return;
885 end if;
887 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
888 pragma Assert (Result = 0);
889 end if;
891 Result := pthread_condattr_init (Cond_Attr'Access);
892 pragma Assert (Result = 0 or else Result = ENOMEM);
894 if Result = 0 then
895 Result :=
896 pthread_cond_init
897 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
898 pragma Assert (Result = 0 or else Result = ENOMEM);
899 end if;
901 if Result = 0 then
902 Succeeded := True;
903 else
904 if not Single_Lock then
905 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
906 pragma Assert (Result = 0);
907 end if;
909 Succeeded := False;
910 end if;
912 Result := pthread_condattr_destroy (Cond_Attr'Access);
913 pragma Assert (Result = 0);
914 end Initialize_TCB;
916 -----------------
917 -- Create_Task --
918 -----------------
920 procedure Create_Task
921 (T : Task_Id;
922 Wrapper : System.Address;
923 Stack_Size : System.Parameters.Size_Type;
924 Priority : System.Any_Priority;
925 Succeeded : out Boolean)
927 Attributes : aliased pthread_attr_t;
928 Adjusted_Stack_Size : Interfaces.C.size_t;
929 Page_Size : constant Interfaces.C.size_t := Get_Page_Size;
930 Result : Interfaces.C.int;
932 function Thread_Body_Access is new
933 Ada.Unchecked_Conversion (System.Address, Thread_Body);
935 use System.Task_Info;
937 begin
938 Adjusted_Stack_Size :=
939 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
941 if Stack_Base_Available then
943 -- If Stack Checking is supported then allocate 2 additional pages:
945 -- In the worst case, stack is allocated at something like
946 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
947 -- to be sure the effective stack size is greater than what
948 -- has been asked.
950 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
951 end if;
953 -- Round stack size as this is required by some OSes (Darwin)
955 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
956 Adjusted_Stack_Size :=
957 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
959 Result := pthread_attr_init (Attributes'Access);
960 pragma Assert (Result = 0 or else Result = ENOMEM);
962 if Result /= 0 then
963 Succeeded := False;
964 return;
965 end if;
967 Result :=
968 pthread_attr_setdetachstate
969 (Attributes'Access, PTHREAD_CREATE_DETACHED);
970 pragma Assert (Result = 0);
972 Result :=
973 pthread_attr_setstacksize
974 (Attributes'Access, Adjusted_Stack_Size);
975 pragma Assert (Result = 0);
977 if T.Common.Task_Info /= Default_Scope then
978 case T.Common.Task_Info is
979 when System.Task_Info.Process_Scope =>
980 Result :=
981 pthread_attr_setscope
982 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
984 when System.Task_Info.System_Scope =>
985 Result :=
986 pthread_attr_setscope
987 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
989 when System.Task_Info.Default_Scope =>
990 Result := 0;
991 end case;
993 pragma Assert (Result = 0);
994 end if;
996 -- Since the initial signal mask of a thread is inherited from the
997 -- creator, and the Environment task has all its signals masked, we
998 -- do not need to manipulate caller's signal mask at this point.
999 -- All tasks in RTS will have All_Tasks_Mask initially.
1001 Result := pthread_create
1002 (T.Common.LL.Thread'Access,
1003 Attributes'Access,
1004 Thread_Body_Access (Wrapper),
1005 To_Address (T));
1006 pragma Assert (Result = 0 or else Result = EAGAIN);
1008 Succeeded := Result = 0;
1010 Result := pthread_attr_destroy (Attributes'Access);
1011 pragma Assert (Result = 0);
1013 if Succeeded then
1014 Set_Priority (T, Priority);
1015 end if;
1016 end Create_Task;
1018 ------------------
1019 -- Finalize_TCB --
1020 ------------------
1022 procedure Finalize_TCB (T : Task_Id) is
1023 Result : Interfaces.C.int;
1024 Tmp : Task_Id := T;
1025 Is_Self : constant Boolean := T = Self;
1027 procedure Free is new
1028 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1030 begin
1031 if not Single_Lock then
1032 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1033 pragma Assert (Result = 0);
1034 end if;
1036 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1037 pragma Assert (Result = 0);
1039 if T.Known_Tasks_Index /= -1 then
1040 Known_Tasks (T.Known_Tasks_Index) := null;
1041 end if;
1043 Free (Tmp);
1045 if Is_Self then
1046 Specific.Set (null);
1047 end if;
1048 end Finalize_TCB;
1050 ---------------
1051 -- Exit_Task --
1052 ---------------
1054 procedure Exit_Task is
1055 begin
1056 -- Mark this task as unknown, so that if Self is called, it won't
1057 -- return a dangling pointer.
1059 Specific.Set (null);
1060 end Exit_Task;
1062 ----------------
1063 -- Abort_Task --
1064 ----------------
1066 procedure Abort_Task (T : Task_Id) is
1067 Result : Interfaces.C.int;
1068 begin
1069 Result :=
1070 pthread_kill
1071 (T.Common.LL.Thread,
1072 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1073 pragma Assert (Result = 0);
1074 end Abort_Task;
1076 ----------------
1077 -- Initialize --
1078 ----------------
1080 procedure Initialize (S : in out Suspension_Object) is
1081 Mutex_Attr : aliased pthread_mutexattr_t;
1082 Cond_Attr : aliased pthread_condattr_t;
1083 Result : Interfaces.C.int;
1085 begin
1086 -- Initialize internal state (always to False (RM D.10 (6)))
1088 S.State := False;
1089 S.Waiting := False;
1091 -- Initialize internal mutex
1093 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1094 pragma Assert (Result = 0 or else Result = ENOMEM);
1096 if Result = ENOMEM then
1097 raise Storage_Error;
1098 end if;
1100 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1101 pragma Assert (Result = 0 or else Result = ENOMEM);
1103 if Result = ENOMEM then
1104 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1105 pragma Assert (Result = 0);
1107 raise Storage_Error;
1108 end if;
1110 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1111 pragma Assert (Result = 0);
1113 -- Initialize internal condition variable
1115 Result := pthread_condattr_init (Cond_Attr'Access);
1116 pragma Assert (Result = 0 or else Result = ENOMEM);
1118 if Result /= 0 then
1119 Result := pthread_mutex_destroy (S.L'Access);
1120 pragma Assert (Result = 0);
1122 if Result = ENOMEM then
1123 raise Storage_Error;
1124 end if;
1125 end if;
1127 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1128 pragma Assert (Result = 0 or else Result = ENOMEM);
1130 if Result /= 0 then
1131 Result := pthread_mutex_destroy (S.L'Access);
1132 pragma Assert (Result = 0);
1134 if Result = ENOMEM then
1135 Result := pthread_condattr_destroy (Cond_Attr'Access);
1136 pragma Assert (Result = 0);
1137 raise Storage_Error;
1138 end if;
1139 end if;
1141 Result := pthread_condattr_destroy (Cond_Attr'Access);
1142 pragma Assert (Result = 0);
1143 end Initialize;
1145 --------------
1146 -- Finalize --
1147 --------------
1149 procedure Finalize (S : in out Suspension_Object) is
1150 Result : Interfaces.C.int;
1152 begin
1153 -- Destroy internal mutex
1155 Result := pthread_mutex_destroy (S.L'Access);
1156 pragma Assert (Result = 0);
1158 -- Destroy internal condition variable
1160 Result := pthread_cond_destroy (S.CV'Access);
1161 pragma Assert (Result = 0);
1162 end Finalize;
1164 -------------------
1165 -- Current_State --
1166 -------------------
1168 function Current_State (S : Suspension_Object) return Boolean is
1169 begin
1170 -- We do not want to use lock on this read operation. State is marked
1171 -- as Atomic so that we ensure that the value retrieved is correct.
1173 return S.State;
1174 end Current_State;
1176 ---------------
1177 -- Set_False --
1178 ---------------
1180 procedure Set_False (S : in out Suspension_Object) is
1181 Result : Interfaces.C.int;
1183 begin
1184 SSL.Abort_Defer.all;
1186 Result := pthread_mutex_lock (S.L'Access);
1187 pragma Assert (Result = 0);
1189 S.State := False;
1191 Result := pthread_mutex_unlock (S.L'Access);
1192 pragma Assert (Result = 0);
1194 SSL.Abort_Undefer.all;
1195 end Set_False;
1197 --------------
1198 -- Set_True --
1199 --------------
1201 procedure Set_True (S : in out Suspension_Object) is
1202 Result : Interfaces.C.int;
1204 begin
1205 SSL.Abort_Defer.all;
1207 Result := pthread_mutex_lock (S.L'Access);
1208 pragma Assert (Result = 0);
1210 -- If there is already a task waiting on this suspension object then
1211 -- we resume it, leaving the state of the suspension object to False,
1212 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1213 -- the state to True.
1215 if S.Waiting then
1216 S.Waiting := False;
1217 S.State := False;
1219 Result := pthread_cond_signal (S.CV'Access);
1220 pragma Assert (Result = 0);
1222 else
1223 S.State := True;
1224 end if;
1226 Result := pthread_mutex_unlock (S.L'Access);
1227 pragma Assert (Result = 0);
1229 SSL.Abort_Undefer.all;
1230 end Set_True;
1232 ------------------------
1233 -- Suspend_Until_True --
1234 ------------------------
1236 procedure Suspend_Until_True (S : in out Suspension_Object) is
1237 Result : Interfaces.C.int;
1239 begin
1240 SSL.Abort_Defer.all;
1242 Result := pthread_mutex_lock (S.L'Access);
1243 pragma Assert (Result = 0);
1245 if S.Waiting then
1247 -- Program_Error must be raised upon calling Suspend_Until_True
1248 -- if another task is already waiting on that suspension object
1249 -- (RM D.10(10)).
1251 Result := pthread_mutex_unlock (S.L'Access);
1252 pragma Assert (Result = 0);
1254 SSL.Abort_Undefer.all;
1256 raise Program_Error;
1258 else
1259 -- Suspend the task if the state is False. Otherwise, the task
1260 -- continues its execution, and the state of the suspension object
1261 -- is set to False (ARM D.10 par. 9).
1263 if S.State then
1264 S.State := False;
1265 else
1266 S.Waiting := True;
1268 loop
1269 -- Loop in case pthread_cond_wait returns earlier than expected
1270 -- (e.g. in case of EINTR caused by a signal).
1272 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1273 pragma Assert (Result = 0 or else Result = EINTR);
1275 exit when not S.Waiting;
1276 end loop;
1277 end if;
1279 Result := pthread_mutex_unlock (S.L'Access);
1280 pragma Assert (Result = 0);
1282 SSL.Abort_Undefer.all;
1283 end if;
1284 end Suspend_Until_True;
1286 ----------------
1287 -- Check_Exit --
1288 ----------------
1290 -- Dummy version
1292 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1293 pragma Unreferenced (Self_ID);
1294 begin
1295 return True;
1296 end Check_Exit;
1298 --------------------
1299 -- Check_No_Locks --
1300 --------------------
1302 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1303 pragma Unreferenced (Self_ID);
1304 begin
1305 return True;
1306 end Check_No_Locks;
1308 ----------------------
1309 -- Environment_Task --
1310 ----------------------
1312 function Environment_Task return Task_Id is
1313 begin
1314 return Environment_Task_Id;
1315 end Environment_Task;
1317 --------------
1318 -- Lock_RTS --
1319 --------------
1321 procedure Lock_RTS is
1322 begin
1323 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1324 end Lock_RTS;
1326 ----------------
1327 -- Unlock_RTS --
1328 ----------------
1330 procedure Unlock_RTS is
1331 begin
1332 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1333 end Unlock_RTS;
1335 ------------------
1336 -- Suspend_Task --
1337 ------------------
1339 function Suspend_Task
1340 (T : ST.Task_Id;
1341 Thread_Self : Thread_Id) return Boolean
1343 pragma Unreferenced (T, Thread_Self);
1344 begin
1345 return False;
1346 end Suspend_Task;
1348 -----------------
1349 -- Resume_Task --
1350 -----------------
1352 function Resume_Task
1353 (T : ST.Task_Id;
1354 Thread_Self : Thread_Id) return Boolean
1356 pragma Unreferenced (T, Thread_Self);
1357 begin
1358 return False;
1359 end Resume_Task;
1361 --------------------
1362 -- Stop_All_Tasks --
1363 --------------------
1365 procedure Stop_All_Tasks is
1366 begin
1367 null;
1368 end Stop_All_Tasks;
1370 ---------------
1371 -- Stop_Task --
1372 ---------------
1374 function Stop_Task (T : ST.Task_Id) return Boolean is
1375 pragma Unreferenced (T);
1376 begin
1377 return False;
1378 end Stop_Task;
1380 -------------------
1381 -- Continue_Task --
1382 -------------------
1384 function Continue_Task (T : ST.Task_Id) return Boolean is
1385 pragma Unreferenced (T);
1386 begin
1387 return False;
1388 end Continue_Task;
1390 ----------------
1391 -- Initialize --
1392 ----------------
1394 procedure Initialize (Environment_Task : Task_Id) is
1395 act : aliased struct_sigaction;
1396 old_act : aliased struct_sigaction;
1397 Tmp_Set : aliased sigset_t;
1398 Result : Interfaces.C.int;
1400 function State
1401 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1402 pragma Import (C, State, "__gnat_get_interrupt_state");
1403 -- Get interrupt state. Defined in a-init.c
1404 -- The input argument is the interrupt number,
1405 -- and the result is one of the following:
1407 Default : constant Character := 's';
1408 -- 'n' this interrupt not set by any Interrupt_State pragma
1409 -- 'u' Interrupt_State pragma set state to User
1410 -- 'r' Interrupt_State pragma set state to Runtime
1411 -- 's' Interrupt_State pragma set state to System (use "default"
1412 -- system handler)
1414 begin
1415 Environment_Task_Id := Environment_Task;
1417 Interrupt_Management.Initialize;
1419 -- Prepare the set of signals that should unblocked in all tasks
1421 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1422 pragma Assert (Result = 0);
1424 for J in Interrupt_Management.Interrupt_ID loop
1425 if System.Interrupt_Management.Keep_Unmasked (J) then
1426 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1427 pragma Assert (Result = 0);
1428 end if;
1429 end loop;
1431 -- Initialize the lock used to synchronize chain of all ATCBs
1433 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1435 Specific.Initialize (Environment_Task);
1437 if Use_Alternate_Stack then
1438 Environment_Task.Common.Task_Alternate_Stack :=
1439 Alternate_Stack'Address;
1440 end if;
1442 -- Make environment task known here because it doesn't go through
1443 -- Activate_Tasks, which does it for all other tasks.
1445 Known_Tasks (Known_Tasks'First) := Environment_Task;
1446 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1448 Enter_Task (Environment_Task);
1450 -- Install the abort-signal handler
1452 if State
1453 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1454 then
1455 act.sa_flags := 0;
1456 act.sa_handler := Abort_Handler'Address;
1458 Result := sigemptyset (Tmp_Set'Access);
1459 pragma Assert (Result = 0);
1460 act.sa_mask := Tmp_Set;
1462 Result :=
1463 sigaction
1464 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1465 act'Unchecked_Access,
1466 old_act'Unchecked_Access);
1467 pragma Assert (Result = 0);
1468 end if;
1469 end Initialize;
1471 end System.Task_Primitives.Operations;