Merge from mainline
[official-gcc.git] / gcc / ada / s-taprop-linux.adb
blob6a3596ead63780bafa9ee7bca9977d566bb7fdb1
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA 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 GNU/Linux (GNU/LinuxThreads) version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 pragma Polling (Off);
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with Interfaces.C;
44 -- used for int
45 -- size_t
47 with System.Tasking.Debug;
48 -- used for Known_Tasks
50 with System.Interrupt_Management;
51 -- used for Keep_Unmasked
52 -- Abort_Task_Interrupt
53 -- Interrupt_ID
55 with System.OS_Primitives;
56 -- used for Delay_Modes
58 with System.Soft_Links;
59 -- used for Abort_Defer/Undefer
61 with Ada.Exceptions;
62 -- used for Raise_Exception
63 -- Raise_From_Signal_Handler
64 -- Exception_Id
66 with Unchecked_Conversion;
67 with Unchecked_Deallocation;
69 package body System.Task_Primitives.Operations is
71 use System.Tasking.Debug;
72 use System.Tasking;
73 use Interfaces.C;
74 use System.OS_Interface;
75 use System.Parameters;
76 use System.OS_Primitives;
78 ----------------
79 -- Local Data --
80 ----------------
82 -- The followings are logically constants, but need to be initialized
83 -- at run time.
85 Single_RTS_Lock : aliased RTS_Lock;
86 -- This is a lock to allow only one thread of control in the RTS at
87 -- a time; it is used to execute in mutual exclusion from all other tasks.
88 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
90 ATCB_Key : aliased pthread_key_t;
91 -- Key used to find the Ada Task_Id associated with a thread
93 Environment_Task_Id : Task_Id;
94 -- A variable to hold Task_Id for the environment task
96 Unblocked_Signal_Mask : aliased sigset_t;
97 -- The set of signals that should be unblocked in all tasks
99 -- The followings are internal configuration constants needed
101 Next_Serial_Number : Task_Serial_Number := 100;
102 -- We start at 100, to reserve some special values for
103 -- using in error checking.
105 Time_Slice_Val : Integer;
106 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
108 Dispatching_Policy : Character;
109 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
111 -- The following are effectively constants, but they need to
112 -- be initialized by calling a pthread_ function.
114 Mutex_Attr : aliased pthread_mutexattr_t;
115 Cond_Attr : aliased pthread_condattr_t;
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 subtype unsigned_long is Interfaces.C.unsigned_long;
163 procedure Abort_Handler (signo : Signal);
165 function To_pthread_t is new Unchecked_Conversion
166 (unsigned_long, System.OS_Interface.pthread_t);
168 -------------------
169 -- Abort_Handler --
170 -------------------
172 procedure Abort_Handler (signo : Signal) is
173 pragma Unreferenced (signo);
175 Self_Id : constant Task_Id := Self;
176 Result : Interfaces.C.int;
177 Old_Set : aliased sigset_t;
179 begin
180 if ZCX_By_Default and then GCC_ZCX_Support then
181 return;
182 end if;
184 if Self_Id.Deferral_Level = 0
185 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
186 and then not Self_Id.Aborting
187 then
188 Self_Id.Aborting := True;
190 -- Make sure signals used for RTS internal purpose are unmasked
192 Result := pthread_sigmask (SIG_UNBLOCK,
193 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
194 pragma Assert (Result = 0);
196 raise Standard'Abort_Signal;
197 end if;
198 end Abort_Handler;
200 --------------
201 -- Lock_RTS --
202 --------------
204 procedure Lock_RTS is
205 begin
206 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
207 end Lock_RTS;
209 ----------------
210 -- Unlock_RTS --
211 ----------------
213 procedure Unlock_RTS is
214 begin
215 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
216 end Unlock_RTS;
218 -----------------
219 -- Stack_Guard --
220 -----------------
222 -- The underlying thread system extends the memory (up to 2MB) when needed
224 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
225 pragma Unreferenced (T);
226 pragma Unreferenced (On);
227 begin
228 null;
229 end Stack_Guard;
231 --------------------
232 -- Get_Thread_Id --
233 --------------------
235 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
236 begin
237 return T.Common.LL.Thread;
238 end Get_Thread_Id;
240 ----------
241 -- Self --
242 ----------
244 function Self return Task_Id renames Specific.Self;
246 ---------------------
247 -- Initialize_Lock --
248 ---------------------
250 -- Note: mutexes and cond_variables needed per-task basis are
251 -- initialized in Initialize_TCB and the Storage_Error is
252 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
253 -- used in RTS is initialized before any status change of RTS.
254 -- Therefore rasing Storage_Error in the following routines
255 -- should be able to be handled safely.
257 procedure Initialize_Lock
258 (Prio : System.Any_Priority;
259 L : access Lock)
261 pragma Unreferenced (Prio);
263 Result : Interfaces.C.int;
264 begin
265 Result := pthread_mutex_init (L, Mutex_Attr'Access);
267 pragma Assert (Result = 0 or else Result = ENOMEM);
269 if Result = ENOMEM then
270 Ada.Exceptions.Raise_Exception (Storage_Error'Identity,
271 "Failed to allocate a lock");
272 end if;
273 end Initialize_Lock;
275 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
276 pragma Unreferenced (Level);
278 Result : Interfaces.C.int;
280 begin
281 Result := pthread_mutex_init (L, Mutex_Attr'Access);
283 pragma Assert (Result = 0 or else Result = ENOMEM);
285 if Result = ENOMEM then
286 raise Storage_Error;
287 end if;
288 end Initialize_Lock;
290 -------------------
291 -- Finalize_Lock --
292 -------------------
294 procedure Finalize_Lock (L : access Lock) is
295 Result : Interfaces.C.int;
296 begin
297 Result := pthread_mutex_destroy (L);
298 pragma Assert (Result = 0);
299 end Finalize_Lock;
301 procedure Finalize_Lock (L : access RTS_Lock) is
302 Result : Interfaces.C.int;
303 begin
304 Result := pthread_mutex_destroy (L);
305 pragma Assert (Result = 0);
306 end Finalize_Lock;
308 ----------------
309 -- Write_Lock --
310 ----------------
312 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
313 Result : Interfaces.C.int;
314 begin
315 Result := pthread_mutex_lock (L);
316 Ceiling_Violation := Result = EINVAL;
318 -- Assume the cause of EINVAL is a priority ceiling violation
320 pragma Assert (Result = 0 or else Result = EINVAL);
321 end Write_Lock;
323 procedure Write_Lock
324 (L : access RTS_Lock;
325 Global_Lock : Boolean := False)
327 Result : Interfaces.C.int;
328 begin
329 if not Single_Lock or else Global_Lock then
330 Result := pthread_mutex_lock (L);
331 pragma Assert (Result = 0);
332 end if;
333 end Write_Lock;
335 procedure Write_Lock (T : Task_Id) is
336 Result : Interfaces.C.int;
337 begin
338 if not Single_Lock then
339 Result := pthread_mutex_lock (T.Common.LL.L'Access);
340 pragma Assert (Result = 0);
341 end if;
342 end Write_Lock;
344 ---------------
345 -- Read_Lock --
346 ---------------
348 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
349 begin
350 Write_Lock (L, Ceiling_Violation);
351 end Read_Lock;
353 ------------
354 -- Unlock --
355 ------------
357 procedure Unlock (L : access Lock) is
358 Result : Interfaces.C.int;
359 begin
360 Result := pthread_mutex_unlock (L);
361 pragma Assert (Result = 0);
362 end Unlock;
364 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
365 Result : Interfaces.C.int;
366 begin
367 if not Single_Lock or else Global_Lock then
368 Result := pthread_mutex_unlock (L);
369 pragma Assert (Result = 0);
370 end if;
371 end Unlock;
373 procedure Unlock (T : Task_Id) is
374 Result : Interfaces.C.int;
375 begin
376 if not Single_Lock then
377 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
378 pragma Assert (Result = 0);
379 end if;
380 end Unlock;
382 -----------
383 -- Sleep --
384 -----------
386 procedure Sleep
387 (Self_ID : Task_Id;
388 Reason : System.Tasking.Task_States)
390 pragma Unreferenced (Reason);
392 Result : Interfaces.C.int;
394 begin
395 pragma Assert (Self_ID = Self);
397 if Single_Lock then
398 Result := pthread_cond_wait
399 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
400 else
401 Result := pthread_cond_wait
402 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
403 end if;
405 -- EINTR is not considered a failure
407 pragma Assert (Result = 0 or else Result = EINTR);
408 end Sleep;
410 -----------------
411 -- Timed_Sleep --
412 -----------------
414 -- This is for use within the run-time system, so abort is
415 -- assumed to be already deferred, and the caller should be
416 -- holding its own ATCB lock.
418 procedure Timed_Sleep
419 (Self_ID : Task_Id;
420 Time : Duration;
421 Mode : ST.Delay_Modes;
422 Reason : System.Tasking.Task_States;
423 Timedout : out Boolean;
424 Yielded : out Boolean)
426 pragma Unreferenced (Reason);
428 Check_Time : constant Duration := Monotonic_Clock;
429 Abs_Time : Duration;
430 Request : aliased timespec;
431 Result : Interfaces.C.int;
433 begin
434 Timedout := True;
435 Yielded := False;
437 if Mode = Relative then
438 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
439 else
440 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
441 end if;
443 if Abs_Time > Check_Time then
444 Request := To_Timespec (Abs_Time);
446 loop
447 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
448 or else Self_ID.Pending_Priority_Change;
450 if Single_Lock then
451 Result := pthread_cond_timedwait
452 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
453 Request'Access);
455 else
456 Result := pthread_cond_timedwait
457 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
458 Request'Access);
459 end if;
461 exit when Abs_Time <= Monotonic_Clock;
463 if Result = 0 or Result = EINTR then
464 -- somebody may have called Wakeup for us
465 Timedout := False;
466 exit;
467 end if;
469 pragma Assert (Result = ETIMEDOUT);
470 end loop;
471 end if;
472 end Timed_Sleep;
474 -----------------
475 -- Timed_Delay --
476 -----------------
478 -- This is for use in implementing delay statements, so
479 -- we assume the caller is abort-deferred but is holding
480 -- no locks.
482 procedure Timed_Delay
483 (Self_ID : Task_Id;
484 Time : Duration;
485 Mode : ST.Delay_Modes)
487 Check_Time : constant Duration := Monotonic_Clock;
488 Abs_Time : Duration;
489 Request : aliased timespec;
490 Result : Interfaces.C.int;
492 begin
493 if Single_Lock then
494 Lock_RTS;
495 end if;
497 Write_Lock (Self_ID);
499 if Mode = Relative then
500 Abs_Time := Time + Check_Time;
501 else
502 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
503 end if;
505 if Abs_Time > Check_Time then
506 Request := To_Timespec (Abs_Time);
507 Self_ID.Common.State := Delay_Sleep;
509 loop
510 if Self_ID.Pending_Priority_Change then
511 Self_ID.Pending_Priority_Change := False;
512 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
513 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
514 end if;
516 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
518 if Single_Lock then
519 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
520 Single_RTS_Lock'Access, Request'Access);
521 else
522 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
523 Self_ID.Common.LL.L'Access, Request'Access);
524 end if;
526 exit when Abs_Time <= Monotonic_Clock;
528 pragma Assert (Result = 0 or else
529 Result = ETIMEDOUT or else
530 Result = EINTR);
531 end loop;
533 Self_ID.Common.State := Runnable;
534 end if;
536 Unlock (Self_ID);
538 if Single_Lock then
539 Unlock_RTS;
540 end if;
542 Result := sched_yield;
543 end Timed_Delay;
545 ---------------------
546 -- Monotonic_Clock --
547 ---------------------
549 function Monotonic_Clock return Duration is
550 TV : aliased struct_timeval;
551 Result : Interfaces.C.int;
552 begin
553 Result := gettimeofday (TV'Access, System.Null_Address);
554 pragma Assert (Result = 0);
555 return To_Duration (TV);
556 end Monotonic_Clock;
558 -------------------
559 -- RT_Resolution --
560 -------------------
562 function RT_Resolution return Duration is
563 begin
564 return 10#1.0#E-6;
565 end RT_Resolution;
567 ------------
568 -- Wakeup --
569 ------------
571 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
572 pragma Unreferenced (Reason);
573 Result : Interfaces.C.int;
574 begin
575 Result := pthread_cond_signal (T.Common.LL.CV'Access);
576 pragma Assert (Result = 0);
577 end Wakeup;
579 -----------
580 -- Yield --
581 -----------
583 procedure Yield (Do_Yield : Boolean := True) is
584 Result : Interfaces.C.int;
585 pragma Unreferenced (Result);
586 begin
587 if Do_Yield then
588 Result := sched_yield;
589 end if;
590 end Yield;
592 ------------------
593 -- Set_Priority --
594 ------------------
596 procedure Set_Priority
597 (T : Task_Id;
598 Prio : System.Any_Priority;
599 Loss_Of_Inheritance : Boolean := False)
601 pragma Unreferenced (Loss_Of_Inheritance);
603 Result : Interfaces.C.int;
604 Param : aliased struct_sched_param;
606 begin
607 T.Common.Current_Priority := Prio;
609 -- Priorities are in range 1 .. 99 on GNU/Linux, so we map
610 -- map 0 .. 31 to 1 .. 32
612 Param.sched_priority := Interfaces.C.int (Prio) + 1;
614 if Time_Slice_Val > 0 then
615 Result := pthread_setschedparam
616 (T.Common.LL.Thread, SCHED_RR, Param'Access);
618 elsif Dispatching_Policy = 'F' or else Time_Slice_Val = 0 then
619 Result := pthread_setschedparam
620 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
622 else
623 Param.sched_priority := 0;
624 Result := pthread_setschedparam
625 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
626 end if;
628 pragma Assert (Result = 0 or else Result = EPERM);
629 end Set_Priority;
631 ------------------
632 -- Get_Priority --
633 ------------------
635 function Get_Priority (T : Task_Id) return System.Any_Priority is
636 begin
637 return T.Common.Current_Priority;
638 end Get_Priority;
640 ----------------
641 -- Enter_Task --
642 ----------------
644 procedure Enter_Task (Self_ID : Task_Id) is
645 begin
646 Self_ID.Common.LL.Thread := pthread_self;
648 Specific.Set (Self_ID);
650 Lock_RTS;
652 for J in Known_Tasks'Range loop
653 if Known_Tasks (J) = null then
654 Known_Tasks (J) := Self_ID;
655 Self_ID.Known_Tasks_Index := J;
656 exit;
657 end if;
658 end loop;
660 Unlock_RTS;
661 end Enter_Task;
663 --------------
664 -- New_ATCB --
665 --------------
667 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
668 begin
669 return new Ada_Task_Control_Block (Entry_Num);
670 end New_ATCB;
672 -------------------
673 -- Is_Valid_Task --
674 -------------------
676 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
678 -----------------------------
679 -- Register_Foreign_Thread --
680 -----------------------------
682 function Register_Foreign_Thread return Task_Id is
683 begin
684 if Is_Valid_Task then
685 return Self;
686 else
687 return Register_Foreign_Thread (pthread_self);
688 end if;
689 end Register_Foreign_Thread;
691 --------------------
692 -- Initialize_TCB --
693 --------------------
695 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
696 Result : Interfaces.C.int;
698 begin
699 -- Give the task a unique serial number
701 Self_ID.Serial_Number := Next_Serial_Number;
702 Next_Serial_Number := Next_Serial_Number + 1;
703 pragma Assert (Next_Serial_Number /= 0);
705 Self_ID.Common.LL.Thread := To_pthread_t (-1);
707 if not Single_Lock then
708 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
709 Mutex_Attr'Access);
710 pragma Assert (Result = 0 or else Result = ENOMEM);
712 if Result /= 0 then
713 Succeeded := False;
714 return;
715 end if;
716 end if;
718 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
719 Cond_Attr'Access);
720 pragma Assert (Result = 0 or else Result = ENOMEM);
722 if Result = 0 then
723 Succeeded := True;
724 else
725 if not Single_Lock then
726 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
727 pragma Assert (Result = 0);
728 end if;
730 Succeeded := False;
731 end if;
732 end Initialize_TCB;
734 -----------------
735 -- Create_Task --
736 -----------------
738 procedure Create_Task
739 (T : Task_Id;
740 Wrapper : System.Address;
741 Stack_Size : System.Parameters.Size_Type;
742 Priority : System.Any_Priority;
743 Succeeded : out Boolean)
745 Attributes : aliased pthread_attr_t;
746 Result : Interfaces.C.int;
748 begin
749 Result := pthread_attr_init (Attributes'Access);
750 pragma Assert (Result = 0 or else Result = ENOMEM);
752 if Result /= 0 then
753 Succeeded := False;
754 return;
755 end if;
757 Result :=
758 pthread_attr_setstacksize
759 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
760 pragma Assert (Result = 0);
762 Result :=
763 pthread_attr_setdetachstate
764 (Attributes'Access, PTHREAD_CREATE_DETACHED);
765 pragma Assert (Result = 0);
767 -- Since the initial signal mask of a thread is inherited from the
768 -- creator, and the Environment task has all its signals masked, we
769 -- do not need to manipulate caller's signal mask at this point.
770 -- All tasks in RTS will have All_Tasks_Mask initially.
772 Result := pthread_create
773 (T.Common.LL.Thread'Access,
774 Attributes'Access,
775 Thread_Body_Access (Wrapper),
776 To_Address (T));
777 pragma Assert (Result = 0 or else Result = EAGAIN);
779 Succeeded := Result = 0;
781 Result := pthread_attr_destroy (Attributes'Access);
782 pragma Assert (Result = 0);
784 Set_Priority (T, Priority);
785 end Create_Task;
787 ------------------
788 -- Finalize_TCB --
789 ------------------
791 procedure Finalize_TCB (T : Task_Id) is
792 Result : Interfaces.C.int;
793 Tmp : Task_Id := T;
794 Is_Self : constant Boolean := T = Self;
796 procedure Free is new
797 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
799 begin
800 if not Single_Lock then
801 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
802 pragma Assert (Result = 0);
803 end if;
805 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
806 pragma Assert (Result = 0);
808 if T.Known_Tasks_Index /= -1 then
809 Known_Tasks (T.Known_Tasks_Index) := null;
810 end if;
812 Free (Tmp);
814 if Is_Self then
815 Specific.Set (null);
816 end if;
817 end Finalize_TCB;
819 ---------------
820 -- Exit_Task --
821 ---------------
823 procedure Exit_Task is
824 begin
825 Specific.Set (null);
826 end Exit_Task;
828 ----------------
829 -- Abort_Task --
830 ----------------
832 procedure Abort_Task (T : Task_Id) is
833 Result : Interfaces.C.int;
834 begin
835 Result := pthread_kill (T.Common.LL.Thread,
836 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
837 pragma Assert (Result = 0);
838 end Abort_Task;
840 ----------------
841 -- Initialize --
842 ----------------
844 procedure Initialize (S : in out Suspension_Object) is
845 Result : Interfaces.C.int;
846 begin
847 -- Initialize internal state. It is always initialized to False (ARM
848 -- D.10 par. 6).
850 S.State := False;
851 S.Waiting := False;
853 -- Initialize internal mutex
855 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
857 pragma Assert (Result = 0 or else Result = ENOMEM);
859 if Result = ENOMEM then
860 raise Storage_Error;
861 end if;
863 -- Initialize internal condition variable
865 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
867 pragma Assert (Result = 0 or else Result = ENOMEM);
869 if Result /= 0 then
870 Result := pthread_mutex_destroy (S.L'Access);
871 pragma Assert (Result = 0);
873 if Result = ENOMEM then
874 raise Storage_Error;
875 end if;
876 end if;
877 end Initialize;
879 --------------
880 -- Finalize --
881 --------------
883 procedure Finalize (S : in out Suspension_Object) is
884 Result : Interfaces.C.int;
885 begin
886 -- Destroy internal mutex
888 Result := pthread_mutex_destroy (S.L'Access);
889 pragma Assert (Result = 0);
891 -- Destroy internal condition variable
893 Result := pthread_cond_destroy (S.CV'Access);
894 pragma Assert (Result = 0);
895 end Finalize;
897 -------------------
898 -- Current_State --
899 -------------------
901 function Current_State (S : Suspension_Object) return Boolean is
902 begin
903 -- We do not want to use lock on this read operation. State is marked
904 -- as Atomic so that we ensure that the value retrieved is correct.
906 return S.State;
907 end Current_State;
909 ---------------
910 -- Set_False --
911 ---------------
913 procedure Set_False (S : in out Suspension_Object) is
914 Result : Interfaces.C.int;
915 begin
916 Result := pthread_mutex_lock (S.L'Access);
917 pragma Assert (Result = 0);
919 S.State := False;
921 Result := pthread_mutex_unlock (S.L'Access);
922 pragma Assert (Result = 0);
923 end Set_False;
925 --------------
926 -- Set_True --
927 --------------
929 procedure Set_True (S : in out Suspension_Object) is
930 Result : Interfaces.C.int;
931 begin
932 Result := pthread_mutex_lock (S.L'Access);
933 pragma Assert (Result = 0);
935 -- If there is already a task waiting on this suspension object then
936 -- we resume it, leaving the state of the suspension object to False,
937 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
938 -- the state to True.
940 if S.Waiting then
941 S.Waiting := False;
942 S.State := False;
944 Result := pthread_cond_signal (S.CV'Access);
945 pragma Assert (Result = 0);
946 else
947 S.State := True;
948 end if;
950 Result := pthread_mutex_unlock (S.L'Access);
951 pragma Assert (Result = 0);
952 end Set_True;
954 ------------------------
955 -- Suspend_Until_True --
956 ------------------------
958 procedure Suspend_Until_True (S : in out Suspension_Object) is
959 Result : Interfaces.C.int;
960 begin
961 Result := pthread_mutex_lock (S.L'Access);
962 pragma Assert (Result = 0);
964 if S.Waiting then
965 -- Program_Error must be raised upon calling Suspend_Until_True
966 -- if another task is already waiting on that suspension object
967 -- (ARM D.10 par. 10).
969 Result := pthread_mutex_unlock (S.L'Access);
970 pragma Assert (Result = 0);
972 raise Program_Error;
973 else
974 -- Suspend the task if the state is False. Otherwise, the task
975 -- continues its execution, and the state of the suspension object
976 -- is set to False (ARM D.10 par. 9).
978 if S.State then
979 S.State := False;
980 else
981 S.Waiting := True;
982 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
983 end if;
984 end if;
986 Result := pthread_mutex_unlock (S.L'Access);
987 pragma Assert (Result = 0);
988 end Suspend_Until_True;
990 ----------------
991 -- Check_Exit --
992 ----------------
994 -- Dummy version
996 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
997 pragma Unreferenced (Self_ID);
998 begin
999 return True;
1000 end Check_Exit;
1002 --------------------
1003 -- Check_No_Locks --
1004 --------------------
1006 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1007 pragma Unreferenced (Self_ID);
1008 begin
1009 return True;
1010 end Check_No_Locks;
1012 ----------------------
1013 -- Environment_Task --
1014 ----------------------
1016 function Environment_Task return Task_Id is
1017 begin
1018 return Environment_Task_Id;
1019 end Environment_Task;
1021 ------------------
1022 -- Suspend_Task --
1023 ------------------
1025 function Suspend_Task
1026 (T : ST.Task_Id;
1027 Thread_Self : Thread_Id) return Boolean
1029 begin
1030 if T.Common.LL.Thread /= Thread_Self then
1031 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1032 else
1033 return True;
1034 end if;
1035 end Suspend_Task;
1037 -----------------
1038 -- Resume_Task --
1039 -----------------
1041 function Resume_Task
1042 (T : ST.Task_Id;
1043 Thread_Self : Thread_Id) return Boolean
1045 begin
1046 if T.Common.LL.Thread /= Thread_Self then
1047 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1048 else
1049 return True;
1050 end if;
1051 end Resume_Task;
1053 ----------------
1054 -- Initialize --
1055 ----------------
1057 procedure Initialize (Environment_Task : Task_Id) is
1058 act : aliased struct_sigaction;
1059 old_act : aliased struct_sigaction;
1060 Tmp_Set : aliased sigset_t;
1061 Result : Interfaces.C.int;
1063 function State
1064 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1065 pragma Import (C, State, "__gnat_get_interrupt_state");
1066 -- Get interrupt state. Defined in a-init.c
1067 -- The input argument is the interrupt number,
1068 -- and the result is one of the following:
1070 Default : constant Character := 's';
1071 -- 'n' this interrupt not set by any Interrupt_State pragma
1072 -- 'u' Interrupt_State pragma set state to User
1073 -- 'r' Interrupt_State pragma set state to Runtime
1074 -- 's' Interrupt_State pragma set state to System (use "default"
1075 -- system handler)
1077 begin
1078 Environment_Task_Id := Environment_Task;
1080 Interrupt_Management.Initialize;
1082 -- Prepare the set of signals that should be unblocked in all tasks
1084 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1085 pragma Assert (Result = 0);
1087 for J in Interrupt_Management.Interrupt_ID loop
1088 if System.Interrupt_Management.Keep_Unmasked (J) then
1089 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1090 pragma Assert (Result = 0);
1091 end if;
1092 end loop;
1094 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1095 pragma Assert (Result = 0);
1097 Result := pthread_condattr_init (Cond_Attr'Access);
1098 pragma Assert (Result = 0);
1100 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1102 -- Initialize the global RTS lock
1104 Specific.Initialize (Environment_Task);
1106 Enter_Task (Environment_Task);
1108 -- Install the abort-signal handler
1110 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1111 /= Default
1112 then
1113 act.sa_flags := 0;
1114 act.sa_handler := Abort_Handler'Address;
1116 Result := sigemptyset (Tmp_Set'Access);
1117 pragma Assert (Result = 0);
1118 act.sa_mask := Tmp_Set;
1120 Result :=
1121 sigaction
1122 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1123 act'Unchecked_Access,
1124 old_act'Unchecked_Access);
1125 pragma Assert (Result = 0);
1126 end if;
1127 end Initialize;
1129 end System.Task_Primitives.Operations;