Merge from mainline
[official-gcc.git] / gcc / ada / s-taprop-posix.adb
blob6195f242c7553d8d2afd5a79e10cb8313f218087
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a 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 Unchecked_Conversion;
68 with Unchecked_Deallocation;
70 package body System.Task_Primitives.Operations is
72 use System.Tasking.Debug;
73 use System.Tasking;
74 use Interfaces.C;
75 use System.OS_Interface;
76 use System.Parameters;
77 use System.OS_Primitives;
79 ----------------
80 -- Local Data --
81 ----------------
83 -- The followings are logically constants, but need to be initialized
84 -- at run time.
86 Single_RTS_Lock : aliased RTS_Lock;
87 -- This is a lock to allow only one thread of control in the RTS at
88 -- a time; it is used to execute in mutual exclusion from all other tasks.
89 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
91 ATCB_Key : aliased pthread_key_t;
92 -- Key used to find the Ada Task_Id associated with a thread
94 Environment_Task_Id : Task_Id;
95 -- A variable to hold Task_Id for the environment task.
97 Locking_Policy : Character;
98 pragma Import (C, Locking_Policy, "__gl_locking_policy");
99 -- Value of the pragma Locking_Policy:
100 -- 'C' for Ceiling_Locking
101 -- 'I' for Inherit_Locking
102 -- ' ' for none.
104 Unblocked_Signal_Mask : aliased sigset_t;
105 -- The set of signals that should unblocked in all tasks
107 -- The followings are internal configuration constants needed.
109 Next_Serial_Number : Task_Serial_Number := 100;
110 -- We start at 100, to reserve some special values for
111 -- using in error checking.
113 Time_Slice_Val : Integer;
114 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
116 Dispatching_Policy : Character;
117 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
119 Foreign_Task_Elaborated : aliased Boolean := True;
120 -- Used to identified fake tasks (i.e., non-Ada Threads).
122 --------------------
123 -- Local Packages --
124 --------------------
126 package Specific is
128 procedure Initialize (Environment_Task : Task_Id);
129 pragma Inline (Initialize);
130 -- Initialize various data needed by this package.
132 function Is_Valid_Task return Boolean;
133 pragma Inline (Is_Valid_Task);
134 -- Does executing thread have a TCB?
136 procedure Set (Self_Id : Task_Id);
137 pragma Inline (Set);
138 -- Set the self id for the current task.
140 function Self return Task_Id;
141 pragma Inline (Self);
142 -- Return a pointer to the Ada Task Control Block of the calling task.
144 end Specific;
146 package body Specific is separate;
147 -- The body of this package is target specific.
149 ---------------------------------
150 -- Support for foreign threads --
151 ---------------------------------
153 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
154 -- Allocate and Initialize a new ATCB for the current Thread.
156 function Register_Foreign_Thread
157 (Thread : Thread_Id) return Task_Id is separate;
159 -----------------------
160 -- Local Subprograms --
161 -----------------------
163 procedure Abort_Handler (Sig : Signal);
164 -- Signal handler used to implement asynchronous abort.
165 -- See also comment before body, below.
167 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
169 -------------------
170 -- Abort_Handler --
171 -------------------
173 -- Target-dependent binding of inter-thread Abort signal to
174 -- the raising of the Abort_Signal exception.
176 -- The technical issues and alternatives here are essentially
177 -- the same as for raising exceptions in response to other
178 -- signals (e.g. Storage_Error). See code and comments in
179 -- the package body System.Interrupt_Management.
181 -- Some implementations may not allow an exception to be propagated
182 -- out of a handler, and others might leave the signal or
183 -- interrupt that invoked this handler masked after the exceptional
184 -- return to the application code.
186 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
187 -- On most UNIX systems, this will allow transfer out of a signal handler,
188 -- which is usually the only mechanism available for implementing
189 -- asynchronous handlers of this kind. However, some
190 -- systems do not restore the signal mask on longjmp(), leaving the
191 -- abort signal masked.
193 procedure Abort_Handler (Sig : Signal) is
194 pragma Warnings (Off, Sig);
196 T : constant Task_Id := Self;
197 Result : Interfaces.C.int;
198 Old_Set : aliased sigset_t;
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'Unchecked_Access, Old_Set'Unchecked_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 Intialize_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 rasing 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 : 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 (L : access RTS_Lock; Level : Lock_Level) is
321 pragma Warnings (Off, Level);
323 Attributes : aliased pthread_mutexattr_t;
324 Result : Interfaces.C.int;
326 begin
327 Result := pthread_mutexattr_init (Attributes'Access);
328 pragma Assert (Result = 0 or else Result = ENOMEM);
330 if Result = ENOMEM then
331 raise Storage_Error;
332 end if;
334 if Locking_Policy = 'C' then
335 Result := pthread_mutexattr_setprotocol
336 (Attributes'Access, PTHREAD_PRIO_PROTECT);
337 pragma Assert (Result = 0);
339 Result := pthread_mutexattr_setprioceiling
340 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
341 pragma Assert (Result = 0);
343 elsif Locking_Policy = 'I' then
344 Result := pthread_mutexattr_setprotocol
345 (Attributes'Access, PTHREAD_PRIO_INHERIT);
346 pragma Assert (Result = 0);
347 end if;
349 Result := pthread_mutex_init (L, Attributes'Access);
350 pragma Assert (Result = 0 or else Result = ENOMEM);
352 if Result = ENOMEM then
353 Result := pthread_mutexattr_destroy (Attributes'Access);
354 raise Storage_Error;
355 end if;
357 Result := pthread_mutexattr_destroy (Attributes'Access);
358 pragma Assert (Result = 0);
359 end Initialize_Lock;
361 -------------------
362 -- Finalize_Lock --
363 -------------------
365 procedure Finalize_Lock (L : access Lock) is
366 Result : Interfaces.C.int;
368 begin
369 Result := pthread_mutex_destroy (L);
370 pragma Assert (Result = 0);
371 end Finalize_Lock;
373 procedure Finalize_Lock (L : access RTS_Lock) is
374 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 (L : access Lock; Ceiling_Violation : out Boolean) is
386 Result : Interfaces.C.int;
388 begin
389 Result := pthread_mutex_lock (L);
391 -- Assume that the cause of EINVAL is a priority ceiling violation
393 Ceiling_Violation := (Result = EINVAL);
394 pragma Assert (Result = 0 or else Result = EINVAL);
395 end Write_Lock;
397 procedure Write_Lock
398 (L : access RTS_Lock;
399 Global_Lock : Boolean := False)
401 Result : Interfaces.C.int;
403 begin
404 if not Single_Lock or else Global_Lock then
405 Result := pthread_mutex_lock (L);
406 pragma Assert (Result = 0);
407 end if;
408 end Write_Lock;
410 procedure Write_Lock (T : Task_Id) is
411 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 (L : access Lock; Ceiling_Violation : out Boolean) is
425 begin
426 Write_Lock (L, Ceiling_Violation);
427 end Read_Lock;
429 ------------
430 -- Unlock --
431 ------------
433 procedure Unlock (L : access Lock) is
434 Result : Interfaces.C.int;
436 begin
437 Result := pthread_mutex_unlock (L);
438 pragma Assert (Result = 0);
439 end Unlock;
441 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
442 Result : Interfaces.C.int;
444 begin
445 if not Single_Lock or else Global_Lock then
446 Result := pthread_mutex_unlock (L);
447 pragma Assert (Result = 0);
448 end if;
449 end Unlock;
451 procedure Unlock (T : Task_Id) is
452 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 -- Sleep --
463 -----------
465 procedure Sleep
466 (Self_ID : Task_Id;
467 Reason : System.Tasking.Task_States)
469 pragma Warnings (Off, Reason);
471 Result : Interfaces.C.int;
473 begin
474 if Single_Lock then
475 Result := pthread_cond_wait
476 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
477 else
478 Result := pthread_cond_wait
479 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
480 end if;
482 -- EINTR is not considered a failure.
484 pragma Assert (Result = 0 or else Result = EINTR);
485 end Sleep;
487 -----------------
488 -- Timed_Sleep --
489 -----------------
491 -- This is for use within the run-time system, so abort is
492 -- assumed to be already deferred, and the caller should be
493 -- holding its own ATCB lock.
495 procedure Timed_Sleep
496 (Self_ID : Task_Id;
497 Time : Duration;
498 Mode : ST.Delay_Modes;
499 Reason : Task_States;
500 Timedout : out Boolean;
501 Yielded : out Boolean)
503 pragma Warnings (Off, Reason);
505 Check_Time : constant Duration := Monotonic_Clock;
506 Rel_Time : Duration;
507 Abs_Time : Duration;
508 Request : aliased timespec;
509 Result : Interfaces.C.int;
511 begin
512 Timedout := True;
513 Yielded := False;
515 if Mode = Relative then
516 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
518 if Relative_Timed_Wait then
519 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
520 end if;
522 else
523 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
525 if Relative_Timed_Wait then
526 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
527 end if;
528 end if;
530 if Abs_Time > Check_Time then
531 if Relative_Timed_Wait then
532 Request := To_Timespec (Rel_Time);
533 else
534 Request := To_Timespec (Abs_Time);
535 end if;
537 loop
538 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
539 or else Self_ID.Pending_Priority_Change;
541 if Single_Lock then
542 Result := pthread_cond_timedwait
543 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
544 Request'Access);
546 else
547 Result := pthread_cond_timedwait
548 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
549 Request'Access);
550 end if;
552 exit when Abs_Time <= Monotonic_Clock;
554 if Result = 0 or Result = EINTR then
556 -- Somebody may have called Wakeup for us
558 Timedout := False;
559 exit;
560 end if;
562 pragma Assert (Result = ETIMEDOUT);
563 end loop;
564 end if;
565 end Timed_Sleep;
567 -----------------
568 -- Timed_Delay --
569 -----------------
571 -- This is for use in implementing delay statements, so
572 -- we assume the caller is abort-deferred but is holding
573 -- no locks.
575 procedure Timed_Delay
576 (Self_ID : Task_Id;
577 Time : Duration;
578 Mode : ST.Delay_Modes)
580 Check_Time : constant Duration := Monotonic_Clock;
581 Abs_Time : Duration;
582 Rel_Time : Duration;
583 Request : aliased timespec;
584 Result : Interfaces.C.int;
586 begin
587 if Single_Lock then
588 Lock_RTS;
589 end if;
591 Write_Lock (Self_ID);
593 if Mode = Relative then
594 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
596 if Relative_Timed_Wait then
597 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
598 end if;
600 else
601 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
603 if Relative_Timed_Wait then
604 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
605 end if;
606 end if;
608 if Abs_Time > Check_Time then
609 if Relative_Timed_Wait then
610 Request := To_Timespec (Rel_Time);
611 else
612 Request := To_Timespec (Abs_Time);
613 end if;
615 Self_ID.Common.State := Delay_Sleep;
617 loop
618 if Self_ID.Pending_Priority_Change then
619 Self_ID.Pending_Priority_Change := False;
620 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
621 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
622 end if;
624 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
626 if Single_Lock then
627 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
628 Single_RTS_Lock'Access, Request'Access);
629 else
630 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
631 Self_ID.Common.LL.L'Access, Request'Access);
632 end if;
634 exit when Abs_Time <= Monotonic_Clock;
636 pragma Assert (Result = 0
637 or else Result = ETIMEDOUT
638 or else Result = EINTR);
639 end loop;
641 Self_ID.Common.State := Runnable;
642 end if;
644 Unlock (Self_ID);
646 if Single_Lock then
647 Unlock_RTS;
648 end if;
650 Result := sched_yield;
651 end Timed_Delay;
653 ---------------------
654 -- Monotonic_Clock --
655 ---------------------
657 function Monotonic_Clock return Duration is
658 TS : aliased timespec;
659 Result : Interfaces.C.int;
660 begin
661 Result := clock_gettime
662 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
663 pragma Assert (Result = 0);
664 return To_Duration (TS);
665 end Monotonic_Clock;
667 -------------------
668 -- RT_Resolution --
669 -------------------
671 function RT_Resolution return Duration is
672 begin
673 return 10#1.0#E-6;
674 end RT_Resolution;
676 ------------
677 -- Wakeup --
678 ------------
680 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
681 pragma Warnings (Off, Reason);
682 Result : Interfaces.C.int;
683 begin
684 Result := pthread_cond_signal (T.Common.LL.CV'Access);
685 pragma Assert (Result = 0);
686 end Wakeup;
688 -----------
689 -- Yield --
690 -----------
692 procedure Yield (Do_Yield : Boolean := True) is
693 Result : Interfaces.C.int;
694 pragma Unreferenced (Result);
695 begin
696 if Do_Yield then
697 Result := sched_yield;
698 end if;
699 end Yield;
701 ------------------
702 -- Set_Priority --
703 ------------------
705 procedure Set_Priority
706 (T : Task_Id;
707 Prio : System.Any_Priority;
708 Loss_Of_Inheritance : Boolean := False)
710 pragma Warnings (Off, Loss_Of_Inheritance);
712 Result : Interfaces.C.int;
713 Param : aliased struct_sched_param;
715 begin
716 T.Common.Current_Priority := Prio;
717 Param.sched_priority := Interfaces.C.int (Prio);
719 if Time_Slice_Supported and then Time_Slice_Val > 0 then
720 Result := pthread_setschedparam
721 (T.Common.LL.Thread, SCHED_RR, Param'Access);
723 elsif Dispatching_Policy = 'F' or else Time_Slice_Val = 0 then
724 Result := pthread_setschedparam
725 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
727 else
728 Result := pthread_setschedparam
729 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
730 end if;
732 pragma Assert (Result = 0);
733 end Set_Priority;
735 ------------------
736 -- Get_Priority --
737 ------------------
739 function Get_Priority (T : Task_Id) return System.Any_Priority is
740 begin
741 return T.Common.Current_Priority;
742 end Get_Priority;
744 ----------------
745 -- Enter_Task --
746 ----------------
748 procedure Enter_Task (Self_ID : Task_Id) is
749 begin
750 Self_ID.Common.LL.Thread := pthread_self;
751 Self_ID.Common.LL.LWP := lwp_self;
753 Specific.Set (Self_ID);
755 Lock_RTS;
757 for J in Known_Tasks'Range loop
758 if Known_Tasks (J) = null then
759 Known_Tasks (J) := Self_ID;
760 Self_ID.Known_Tasks_Index := J;
761 exit;
762 end if;
763 end loop;
765 Unlock_RTS;
766 end Enter_Task;
768 --------------
769 -- New_ATCB --
770 --------------
772 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
773 begin
774 return new Ada_Task_Control_Block (Entry_Num);
775 end New_ATCB;
777 -------------------
778 -- Is_Valid_Task --
779 -------------------
781 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
783 -----------------------------
784 -- Register_Foreign_Thread --
785 -----------------------------
787 function Register_Foreign_Thread return Task_Id is
788 begin
789 if Is_Valid_Task then
790 return Self;
791 else
792 return Register_Foreign_Thread (pthread_self);
793 end if;
794 end Register_Foreign_Thread;
796 --------------------
797 -- Initialize_TCB --
798 --------------------
800 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
801 Mutex_Attr : aliased pthread_mutexattr_t;
802 Result : Interfaces.C.int;
803 Cond_Attr : aliased pthread_condattr_t;
805 begin
806 -- Give the task a unique serial number.
808 Self_ID.Serial_Number := Next_Serial_Number;
809 Next_Serial_Number := Next_Serial_Number + 1;
810 pragma Assert (Next_Serial_Number /= 0);
812 if not Single_Lock then
813 Result := pthread_mutexattr_init (Mutex_Attr'Access);
814 pragma Assert (Result = 0 or else Result = ENOMEM);
816 if Result = 0 then
817 if Locking_Policy = 'C' then
818 Result := pthread_mutexattr_setprotocol
819 (Mutex_Attr'Access, PTHREAD_PRIO_PROTECT);
820 pragma Assert (Result = 0);
822 Result := pthread_mutexattr_setprioceiling
823 (Mutex_Attr'Access,
824 Interfaces.C.int (System.Any_Priority'Last));
825 pragma Assert (Result = 0);
827 elsif Locking_Policy = 'I' then
828 Result := pthread_mutexattr_setprotocol
829 (Mutex_Attr'Access, PTHREAD_PRIO_INHERIT);
830 pragma Assert (Result = 0);
831 end if;
833 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
834 Mutex_Attr'Access);
835 pragma Assert (Result = 0 or else Result = ENOMEM);
836 end if;
838 if Result /= 0 then
839 Succeeded := False;
840 return;
841 end if;
843 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
844 pragma Assert (Result = 0);
845 end if;
847 Result := pthread_condattr_init (Cond_Attr'Access);
848 pragma Assert (Result = 0 or else Result = ENOMEM);
850 if Result = 0 then
851 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
852 Cond_Attr'Access);
853 pragma Assert (Result = 0 or else Result = ENOMEM);
854 end if;
856 if Result = 0 then
857 Succeeded := True;
858 else
859 if not Single_Lock then
860 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
861 pragma Assert (Result = 0);
862 end if;
864 Succeeded := False;
865 end if;
867 Result := pthread_condattr_destroy (Cond_Attr'Access);
868 pragma Assert (Result = 0);
869 end Initialize_TCB;
871 -----------------
872 -- Create_Task --
873 -----------------
875 procedure Create_Task
876 (T : Task_Id;
877 Wrapper : System.Address;
878 Stack_Size : System.Parameters.Size_Type;
879 Priority : System.Any_Priority;
880 Succeeded : out Boolean)
882 Attributes : aliased pthread_attr_t;
883 Adjusted_Stack_Size : Interfaces.C.size_t;
884 Result : Interfaces.C.int;
886 function Thread_Body_Access is new
887 Unchecked_Conversion (System.Address, Thread_Body);
889 use System.Task_Info;
891 begin
892 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
894 if Stack_Base_Available then
895 -- If Stack Checking is supported then allocate 2 additional pages:
897 -- In the worst case, stack is allocated at something like
898 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
899 -- to be sure the effective stack size is greater than what
900 -- has been asked.
902 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
903 end if;
905 Result := pthread_attr_init (Attributes'Access);
906 pragma Assert (Result = 0 or else Result = ENOMEM);
908 if Result /= 0 then
909 Succeeded := False;
910 return;
911 end if;
913 Result := pthread_attr_setdetachstate
914 (Attributes'Access, PTHREAD_CREATE_DETACHED);
915 pragma Assert (Result = 0);
917 Result := pthread_attr_setstacksize
918 (Attributes'Access, Adjusted_Stack_Size);
919 pragma Assert (Result = 0);
921 if T.Common.Task_Info /= Default_Scope then
923 -- We are assuming that Scope_Type has the same values than the
924 -- corresponding C macros
926 Result := pthread_attr_setscope
927 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
928 pragma Assert (Result = 0);
929 end if;
931 -- Since the initial signal mask of a thread is inherited from the
932 -- creator, and the Environment task has all its signals masked, we
933 -- do not need to manipulate caller's signal mask at this point.
934 -- All tasks in RTS will have All_Tasks_Mask initially.
936 Result := pthread_create
937 (T.Common.LL.Thread'Access,
938 Attributes'Access,
939 Thread_Body_Access (Wrapper),
940 To_Address (T));
941 pragma Assert (Result = 0 or else Result = EAGAIN);
943 Succeeded := Result = 0;
945 Result := pthread_attr_destroy (Attributes'Access);
946 pragma Assert (Result = 0);
948 Set_Priority (T, Priority);
949 end Create_Task;
951 ------------------
952 -- Finalize_TCB --
953 ------------------
955 procedure Finalize_TCB (T : Task_Id) is
956 Result : Interfaces.C.int;
957 Tmp : Task_Id := T;
958 Is_Self : constant Boolean := T = Self;
960 procedure Free is new
961 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
963 begin
964 if not Single_Lock then
965 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
966 pragma Assert (Result = 0);
967 end if;
969 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
970 pragma Assert (Result = 0);
972 if T.Known_Tasks_Index /= -1 then
973 Known_Tasks (T.Known_Tasks_Index) := null;
974 end if;
976 Free (Tmp);
978 if Is_Self then
979 Specific.Set (null);
980 end if;
981 end Finalize_TCB;
983 ---------------
984 -- Exit_Task --
985 ---------------
987 procedure Exit_Task is
988 begin
989 -- Mark this task as unknown, so that if Self is called, it won't
990 -- return a dangling pointer.
992 Specific.Set (null);
993 end Exit_Task;
995 ----------------
996 -- Abort_Task --
997 ----------------
999 procedure Abort_Task (T : Task_Id) is
1000 Result : Interfaces.C.int;
1001 begin
1002 Result := pthread_kill (T.Common.LL.Thread,
1003 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1004 pragma Assert (Result = 0);
1005 end Abort_Task;
1007 ----------------
1008 -- Initialize --
1009 ----------------
1011 procedure Initialize (S : in out Suspension_Object) is
1012 Mutex_Attr : aliased pthread_mutexattr_t;
1013 Cond_Attr : aliased pthread_condattr_t;
1014 Result : Interfaces.C.int;
1015 begin
1016 -- Initialize internal state. It is always initialized to False (ARM
1017 -- D.10 par. 6).
1019 S.State := False;
1020 S.Waiting := False;
1022 -- Initialize internal mutex
1024 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1025 pragma Assert (Result = 0 or else Result = ENOMEM);
1027 if Result = ENOMEM then
1028 raise Storage_Error;
1029 end if;
1031 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1032 pragma Assert (Result = 0 or else Result = ENOMEM);
1034 if Result = ENOMEM then
1035 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1036 pragma Assert (Result = 0);
1038 raise Storage_Error;
1039 end if;
1041 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1042 pragma Assert (Result = 0);
1044 -- Initialize internal condition variable
1046 Result := pthread_condattr_init (Cond_Attr'Access);
1047 pragma Assert (Result = 0 or else Result = ENOMEM);
1049 if Result /= 0 then
1050 Result := pthread_mutex_destroy (S.L'Access);
1051 pragma Assert (Result = 0);
1053 if Result = ENOMEM then
1054 raise Storage_Error;
1055 end if;
1056 end if;
1058 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1059 pragma Assert (Result = 0 or else Result = ENOMEM);
1061 if Result /= 0 then
1062 Result := pthread_mutex_destroy (S.L'Access);
1063 pragma Assert (Result = 0);
1065 if Result = ENOMEM then
1066 Result := pthread_condattr_destroy (Cond_Attr'Access);
1067 pragma Assert (Result = 0);
1069 raise Storage_Error;
1070 end if;
1071 end if;
1073 Result := pthread_condattr_destroy (Cond_Attr'Access);
1074 pragma Assert (Result = 0);
1075 end Initialize;
1077 --------------
1078 -- Finalize --
1079 --------------
1081 procedure Finalize (S : in out Suspension_Object) is
1082 Result : Interfaces.C.int;
1083 begin
1084 -- Destroy internal mutex
1086 Result := pthread_mutex_destroy (S.L'Access);
1087 pragma Assert (Result = 0);
1089 -- Destroy internal condition variable
1091 Result := pthread_cond_destroy (S.CV'Access);
1092 pragma Assert (Result = 0);
1093 end Finalize;
1095 -------------------
1096 -- Current_State --
1097 -------------------
1099 function Current_State (S : Suspension_Object) return Boolean is
1100 begin
1101 -- We do not want to use lock on this read operation. State is marked
1102 -- as Atomic so that we ensure that the value retrieved is correct.
1104 return S.State;
1105 end Current_State;
1107 ---------------
1108 -- Set_False --
1109 ---------------
1111 procedure Set_False (S : in out Suspension_Object) is
1112 Result : Interfaces.C.int;
1113 begin
1114 Result := pthread_mutex_lock (S.L'Access);
1115 pragma Assert (Result = 0);
1117 S.State := False;
1119 Result := pthread_mutex_unlock (S.L'Access);
1120 pragma Assert (Result = 0);
1121 end Set_False;
1123 --------------
1124 -- Set_True --
1125 --------------
1127 procedure Set_True (S : in out Suspension_Object) is
1128 Result : Interfaces.C.int;
1129 begin
1130 Result := pthread_mutex_lock (S.L'Access);
1131 pragma Assert (Result = 0);
1133 -- If there is already a task waiting on this suspension object then
1134 -- we resume it, leaving the state of the suspension object to False,
1135 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1136 -- the state to True.
1138 if S.Waiting then
1139 S.Waiting := False;
1140 S.State := False;
1142 Result := pthread_cond_signal (S.CV'Access);
1143 pragma Assert (Result = 0);
1144 else
1145 S.State := True;
1146 end if;
1148 Result := pthread_mutex_unlock (S.L'Access);
1149 pragma Assert (Result = 0);
1150 end Set_True;
1152 ------------------------
1153 -- Suspend_Until_True --
1154 ------------------------
1156 procedure Suspend_Until_True (S : in out Suspension_Object) is
1157 Result : Interfaces.C.int;
1158 begin
1159 Result := pthread_mutex_lock (S.L'Access);
1160 pragma Assert (Result = 0);
1162 if S.Waiting then
1163 -- Program_Error must be raised upon calling Suspend_Until_True
1164 -- if another task is already waiting on that suspension object
1165 -- (ARM D.10 par. 10).
1167 Result := pthread_mutex_unlock (S.L'Access);
1168 pragma Assert (Result = 0);
1170 raise Program_Error;
1171 else
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1176 if S.State then
1177 S.State := False;
1178 else
1179 S.Waiting := True;
1180 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1181 end if;
1182 end if;
1184 Result := pthread_mutex_unlock (S.L'Access);
1185 pragma Assert (Result = 0);
1186 end Suspend_Until_True;
1188 ----------------
1189 -- Check_Exit --
1190 ----------------
1192 -- Dummy version
1194 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1195 pragma Warnings (Off, Self_ID);
1196 begin
1197 return True;
1198 end Check_Exit;
1200 --------------------
1201 -- Check_No_Locks --
1202 --------------------
1204 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1205 pragma Warnings (Off, Self_ID);
1206 begin
1207 return True;
1208 end Check_No_Locks;
1210 ----------------------
1211 -- Environment_Task --
1212 ----------------------
1214 function Environment_Task return Task_Id is
1215 begin
1216 return Environment_Task_Id;
1217 end Environment_Task;
1219 --------------
1220 -- Lock_RTS --
1221 --------------
1223 procedure Lock_RTS is
1224 begin
1225 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1226 end Lock_RTS;
1228 ----------------
1229 -- Unlock_RTS --
1230 ----------------
1232 procedure Unlock_RTS is
1233 begin
1234 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1235 end Unlock_RTS;
1237 ------------------
1238 -- Suspend_Task --
1239 ------------------
1241 function Suspend_Task
1242 (T : ST.Task_Id;
1243 Thread_Self : Thread_Id) return Boolean
1245 pragma Warnings (Off, T);
1246 pragma Warnings (Off, Thread_Self);
1247 begin
1248 return False;
1249 end Suspend_Task;
1251 -----------------
1252 -- Resume_Task --
1253 -----------------
1255 function Resume_Task
1256 (T : ST.Task_Id;
1257 Thread_Self : Thread_Id) return Boolean
1259 pragma Warnings (Off, T);
1260 pragma Warnings (Off, Thread_Self);
1261 begin
1262 return False;
1263 end Resume_Task;
1265 ----------------
1266 -- Initialize --
1267 ----------------
1269 procedure Initialize (Environment_Task : Task_Id) is
1270 act : aliased struct_sigaction;
1271 old_act : aliased struct_sigaction;
1272 Tmp_Set : aliased sigset_t;
1273 Result : Interfaces.C.int;
1275 function State
1276 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1277 pragma Import (C, State, "__gnat_get_interrupt_state");
1278 -- Get interrupt state. Defined in a-init.c
1279 -- The input argument is the interrupt number,
1280 -- and the result is one of the following:
1282 Default : constant Character := 's';
1283 -- 'n' this interrupt not set by any Interrupt_State pragma
1284 -- 'u' Interrupt_State pragma set state to User
1285 -- 'r' Interrupt_State pragma set state to Runtime
1286 -- 's' Interrupt_State pragma set state to System (use "default"
1287 -- system handler)
1289 begin
1290 Environment_Task_Id := Environment_Task;
1292 Interrupt_Management.Initialize;
1294 -- Prepare the set of signals that should unblocked in all tasks
1296 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1297 pragma Assert (Result = 0);
1299 for J in Interrupt_Management.Interrupt_ID loop
1300 if System.Interrupt_Management.Keep_Unmasked (J) then
1301 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1302 pragma Assert (Result = 0);
1303 end if;
1304 end loop;
1306 -- Initialize the lock used to synchronize chain of all ATCBs.
1308 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1310 Specific.Initialize (Environment_Task);
1312 Enter_Task (Environment_Task);
1314 -- Install the abort-signal handler
1316 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1317 /= Default
1318 then
1319 act.sa_flags := 0;
1320 act.sa_handler := Abort_Handler'Address;
1322 Result := sigemptyset (Tmp_Set'Access);
1323 pragma Assert (Result = 0);
1324 act.sa_mask := Tmp_Set;
1326 Result :=
1327 sigaction
1328 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1329 act'Unchecked_Access,
1330 old_act'Unchecked_Access);
1331 pragma Assert (Result = 0);
1332 end if;
1333 end Initialize;
1335 end System.Task_Primitives.Operations;