PR c++/33620
[official-gcc.git] / gcc / ada / s-taprop-linux.adb
blobb30fb2476174271840fadc0d658ae9ce22009017
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-2007, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a 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 -- We use System.Soft_Links instead of System.Tasking.Initialization
62 -- because the later is a higher level package that we shouldn't depend on.
63 -- For example when using the restricted run time, it is replaced by
64 -- System.Tasking.Restricted.Stages.
66 with System.Storage_Elements;
67 with System.Stack_Checking.Operations;
68 -- Used for Invalidate_Stack_Cache and Notify_Stack_Attributes;
70 with Ada.Exceptions;
71 -- used for Raise_Exception
72 -- Raise_From_Signal_Handler
73 -- Exception_Id
75 with Ada.Unchecked_Conversion;
76 with Ada.Unchecked_Deallocation;
78 package body System.Task_Primitives.Operations is
80 package SSL renames System.Soft_Links;
81 package SC renames System.Stack_Checking.Operations;
83 use System.Tasking.Debug;
84 use System.Tasking;
85 use Interfaces.C;
86 use System.OS_Interface;
87 use System.Parameters;
88 use System.OS_Primitives;
89 use System.Storage_Elements;
91 ----------------
92 -- Local Data --
93 ----------------
95 -- The followings are logically constants, but need to be initialized
96 -- at run time.
98 Single_RTS_Lock : aliased RTS_Lock;
99 -- This is a lock to allow only one thread of control in the RTS at
100 -- a time; it is used to execute in mutual exclusion from all other tasks.
101 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
103 ATCB_Key : aliased pthread_key_t;
104 -- Key used to find the Ada Task_Id associated with a thread
106 Environment_Task_Id : Task_Id;
107 -- A variable to hold Task_Id for the environment task
109 Unblocked_Signal_Mask : aliased sigset_t;
110 -- The set of signals that should be unblocked in all tasks
112 -- The followings are internal configuration constants needed
114 Next_Serial_Number : Task_Serial_Number := 100;
115 -- We start at 100 (reserve some special values for using in error checks)
117 Time_Slice_Val : Integer;
118 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
120 Dispatching_Policy : Character;
121 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
123 -- The following are effectively constants, but they need to be initialized
124 -- by calling a pthread_ function.
126 Mutex_Attr : aliased pthread_mutexattr_t;
127 Cond_Attr : aliased pthread_condattr_t;
129 Foreign_Task_Elaborated : aliased Boolean := True;
130 -- Used to identified fake tasks (i.e., non-Ada Threads)
132 --------------------
133 -- Local Packages --
134 --------------------
136 package Specific is
138 procedure Initialize (Environment_Task : Task_Id);
139 pragma Inline (Initialize);
140 -- Initialize various data needed by this package
142 function Is_Valid_Task return Boolean;
143 pragma Inline (Is_Valid_Task);
144 -- Does executing thread have a TCB?
146 procedure Set (Self_Id : Task_Id);
147 pragma Inline (Set);
148 -- Set the self id for the current task
150 function Self return Task_Id;
151 pragma Inline (Self);
152 -- Return a pointer to the Ada Task Control Block of the calling task
154 end Specific;
156 package body Specific is separate;
157 -- The body of this package is target specific
159 ---------------------------------
160 -- Support for foreign threads --
161 ---------------------------------
163 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
164 -- Allocate and Initialize a new ATCB for the current Thread
166 function Register_Foreign_Thread
167 (Thread : Thread_Id) return Task_Id is separate;
169 -----------------------
170 -- Local Subprograms --
171 -----------------------
173 subtype unsigned_long is Interfaces.C.unsigned_long;
175 procedure Abort_Handler (signo : Signal);
177 function To_pthread_t is new Ada.Unchecked_Conversion
178 (unsigned_long, System.OS_Interface.pthread_t);
180 procedure Get_Stack_Attributes
181 (T : Task_Id;
182 ISP : out System.Address;
183 Size : out Storage_Offset);
184 -- Fill ISP and Size with the Initial Stack Pointer value and the
185 -- thread stack size for task T.
187 -------------------
188 -- Abort_Handler --
189 -------------------
191 procedure Abort_Handler (signo : Signal) is
192 pragma Unreferenced (signo);
194 Self_Id : constant Task_Id := Self;
195 Result : Interfaces.C.int;
196 Old_Set : aliased sigset_t;
198 begin
199 if ZCX_By_Default and then GCC_ZCX_Support then
200 return;
201 end if;
203 if Self_Id.Deferral_Level = 0
204 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
205 and then not Self_Id.Aborting
206 then
207 Self_Id.Aborting := True;
209 -- Make sure signals used for RTS internal purpose are unmasked
211 Result :=
212 pthread_sigmask
213 (SIG_UNBLOCK,
214 Unblocked_Signal_Mask'Unchecked_Access,
215 Old_Set'Unchecked_Access);
216 pragma Assert (Result = 0);
218 raise Standard'Abort_Signal;
219 end if;
220 end Abort_Handler;
222 --------------
223 -- Lock_RTS --
224 --------------
226 procedure Lock_RTS is
227 begin
228 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
229 end Lock_RTS;
231 ----------------
232 -- Unlock_RTS --
233 ----------------
235 procedure Unlock_RTS is
236 begin
237 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
238 end Unlock_RTS;
240 -----------------
241 -- Stack_Guard --
242 -----------------
244 -- The underlying thread system extends the memory (up to 2MB) when needed
246 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
247 pragma Unreferenced (T);
248 pragma Unreferenced (On);
249 begin
250 null;
251 end Stack_Guard;
253 --------------------
254 -- Get_Thread_Id --
255 --------------------
257 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
258 begin
259 return T.Common.LL.Thread;
260 end Get_Thread_Id;
262 ----------
263 -- Self --
264 ----------
266 function Self return Task_Id renames Specific.Self;
268 ---------------------
269 -- Initialize_Lock --
270 ---------------------
272 -- Note: mutexes and cond_variables needed per-task basis are
273 -- initialized in Initialize_TCB and the Storage_Error is
274 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
275 -- used in RTS is initialized before any status change of RTS.
276 -- Therefore rasing Storage_Error in the following routines
277 -- should be able to be handled safely.
279 procedure Initialize_Lock
280 (Prio : System.Any_Priority;
281 L : not null access Lock)
283 pragma Unreferenced (Prio);
285 Result : Interfaces.C.int;
287 begin
288 Result := pthread_mutex_init (L, Mutex_Attr'Access);
290 pragma Assert (Result = 0 or else Result = ENOMEM);
292 if Result = ENOMEM then
293 Ada.Exceptions.Raise_Exception (Storage_Error'Identity,
294 "Failed to allocate a lock");
295 end if;
296 end Initialize_Lock;
298 procedure Initialize_Lock
299 (L : not null access RTS_Lock;
300 Level : Lock_Level)
302 pragma Unreferenced (Level);
304 Result : Interfaces.C.int;
306 begin
307 Result := pthread_mutex_init (L, Mutex_Attr'Access);
309 pragma Assert (Result = 0 or else Result = ENOMEM);
311 if Result = ENOMEM then
312 raise Storage_Error;
313 end if;
314 end Initialize_Lock;
316 -------------------
317 -- Finalize_Lock --
318 -------------------
320 procedure Finalize_Lock (L : not null access Lock) is
321 Result : Interfaces.C.int;
322 begin
323 Result := pthread_mutex_destroy (L);
324 pragma Assert (Result = 0);
325 end Finalize_Lock;
327 procedure Finalize_Lock (L : not null access RTS_Lock) is
328 Result : Interfaces.C.int;
329 begin
330 Result := pthread_mutex_destroy (L);
331 pragma Assert (Result = 0);
332 end Finalize_Lock;
334 ----------------
335 -- Write_Lock --
336 ----------------
338 procedure Write_Lock
339 (L : not null access Lock;
340 Ceiling_Violation : out Boolean)
342 Result : Interfaces.C.int;
343 begin
344 Result := pthread_mutex_lock (L);
345 Ceiling_Violation := Result = EINVAL;
347 -- Assume the cause of EINVAL is a priority ceiling violation
349 pragma Assert (Result = 0 or else Result = EINVAL);
350 end Write_Lock;
352 procedure Write_Lock
353 (L : not null access RTS_Lock;
354 Global_Lock : Boolean := False)
356 Result : Interfaces.C.int;
357 begin
358 if not Single_Lock or else Global_Lock then
359 Result := pthread_mutex_lock (L);
360 pragma Assert (Result = 0);
361 end if;
362 end Write_Lock;
364 procedure Write_Lock (T : Task_Id) is
365 Result : Interfaces.C.int;
366 begin
367 if not Single_Lock then
368 Result := pthread_mutex_lock (T.Common.LL.L'Access);
369 pragma Assert (Result = 0);
370 end if;
371 end Write_Lock;
373 ---------------
374 -- Read_Lock --
375 ---------------
377 procedure Read_Lock
378 (L : not null access Lock;
379 Ceiling_Violation : out Boolean)
381 begin
382 Write_Lock (L, Ceiling_Violation);
383 end Read_Lock;
385 ------------
386 -- Unlock --
387 ------------
389 procedure Unlock (L : not null access Lock) is
390 Result : Interfaces.C.int;
391 begin
392 Result := pthread_mutex_unlock (L);
393 pragma Assert (Result = 0);
394 end Unlock;
396 procedure Unlock
397 (L : not null access RTS_Lock;
398 Global_Lock : Boolean := False)
400 Result : Interfaces.C.int;
401 begin
402 if not Single_Lock or else Global_Lock then
403 Result := pthread_mutex_unlock (L);
404 pragma Assert (Result = 0);
405 end if;
406 end Unlock;
408 procedure Unlock (T : Task_Id) is
409 Result : Interfaces.C.int;
410 begin
411 if not Single_Lock then
412 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
413 pragma Assert (Result = 0);
414 end if;
415 end Unlock;
417 -----------------
418 -- Set_Ceiling --
419 -----------------
421 -- Dynamic priority ceilings are not supported by the underlying system
423 procedure Set_Ceiling
424 (L : not null access Lock;
425 Prio : System.Any_Priority)
427 pragma Unreferenced (L, Prio);
428 begin
429 null;
430 end Set_Ceiling;
432 -----------
433 -- Sleep --
434 -----------
436 procedure Sleep
437 (Self_ID : Task_Id;
438 Reason : System.Tasking.Task_States)
440 pragma Unreferenced (Reason);
442 Result : Interfaces.C.int;
444 begin
445 pragma Assert (Self_ID = Self);
447 if Single_Lock then
448 Result :=
449 pthread_cond_wait
450 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
451 else
452 Result :=
453 pthread_cond_wait
454 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
455 end if;
457 -- EINTR is not considered a failure
459 pragma Assert (Result = 0 or else Result = EINTR);
460 end Sleep;
462 -----------------
463 -- Timed_Sleep --
464 -----------------
466 -- This is for use within the run-time system, so abort is
467 -- assumed to be already deferred, and the caller should be
468 -- holding its own ATCB lock.
470 procedure Timed_Sleep
471 (Self_ID : Task_Id;
472 Time : Duration;
473 Mode : ST.Delay_Modes;
474 Reason : System.Tasking.Task_States;
475 Timedout : out Boolean;
476 Yielded : out Boolean)
478 pragma Unreferenced (Reason);
480 Base_Time : constant Duration := Monotonic_Clock;
481 Check_Time : Duration := Base_Time;
482 Abs_Time : Duration;
483 Request : aliased timespec;
484 Result : Interfaces.C.int;
486 begin
487 Timedout := True;
488 Yielded := False;
490 if Mode = Relative then
491 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
492 else
493 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
494 end if;
496 if Abs_Time > Check_Time then
497 Request := To_Timespec (Abs_Time);
499 loop
500 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
502 if Single_Lock then
503 Result :=
504 pthread_cond_timedwait
505 (Self_ID.Common.LL.CV'Access,
506 Single_RTS_Lock'Access,
507 Request'Access);
509 else
510 Result :=
511 pthread_cond_timedwait
512 (Self_ID.Common.LL.CV'Access,
513 Self_ID.Common.LL.L'Access,
514 Request'Access);
515 end if;
517 Check_Time := Monotonic_Clock;
518 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
520 if Result = 0 or else Result = EINTR then
522 -- Somebody may have called Wakeup for us
524 Timedout := False;
525 exit;
526 end if;
528 pragma Assert (Result = ETIMEDOUT);
529 end loop;
530 end if;
531 end Timed_Sleep;
533 -----------------
534 -- Timed_Delay --
535 -----------------
537 -- This is for use in implementing delay statements, so we assume the
538 -- caller is abort-deferred but is holding no locks.
540 procedure Timed_Delay
541 (Self_ID : Task_Id;
542 Time : Duration;
543 Mode : ST.Delay_Modes)
545 Base_Time : constant Duration := Monotonic_Clock;
546 Check_Time : Duration := Base_Time;
547 Abs_Time : Duration;
548 Request : aliased timespec;
550 Result : Interfaces.C.int;
551 pragma Warnings (Off, Result);
553 begin
554 if Single_Lock then
555 Lock_RTS;
556 end if;
558 Write_Lock (Self_ID);
560 if Mode = Relative then
561 Abs_Time := Time + Check_Time;
562 else
563 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
564 end if;
566 if Abs_Time > Check_Time then
567 Request := To_Timespec (Abs_Time);
568 Self_ID.Common.State := Delay_Sleep;
570 loop
571 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
573 if Single_Lock then
574 Result := pthread_cond_timedwait
575 (Self_ID.Common.LL.CV'Access,
576 Single_RTS_Lock'Access,
577 Request'Access);
578 else
579 Result := pthread_cond_timedwait
580 (Self_ID.Common.LL.CV'Access,
581 Self_ID.Common.LL.L'Access,
582 Request'Access);
583 end if;
585 Check_Time := Monotonic_Clock;
586 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
588 pragma Assert (Result = 0 or else
589 Result = ETIMEDOUT or else
590 Result = EINTR);
591 end loop;
593 Self_ID.Common.State := Runnable;
594 end if;
596 Unlock (Self_ID);
598 if Single_Lock then
599 Unlock_RTS;
600 end if;
602 Result := sched_yield;
603 end Timed_Delay;
605 ---------------------
606 -- Monotonic_Clock --
607 ---------------------
609 function Monotonic_Clock return Duration is
610 TV : aliased struct_timeval;
611 Result : Interfaces.C.int;
612 begin
613 Result := gettimeofday (TV'Access, System.Null_Address);
614 pragma Assert (Result = 0);
615 return To_Duration (TV);
616 end Monotonic_Clock;
618 -------------------
619 -- RT_Resolution --
620 -------------------
622 function RT_Resolution return Duration is
623 begin
624 return 10#1.0#E-6;
625 end RT_Resolution;
627 ------------
628 -- Wakeup --
629 ------------
631 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
632 pragma Unreferenced (Reason);
633 Result : Interfaces.C.int;
634 begin
635 Result := pthread_cond_signal (T.Common.LL.CV'Access);
636 pragma Assert (Result = 0);
637 end Wakeup;
639 -----------
640 -- Yield --
641 -----------
643 procedure Yield (Do_Yield : Boolean := True) is
644 Result : Interfaces.C.int;
645 pragma Unreferenced (Result);
646 begin
647 if Do_Yield then
648 Result := sched_yield;
649 end if;
650 end Yield;
652 ------------------
653 -- Set_Priority --
654 ------------------
656 procedure Set_Priority
657 (T : Task_Id;
658 Prio : System.Any_Priority;
659 Loss_Of_Inheritance : Boolean := False)
661 pragma Unreferenced (Loss_Of_Inheritance);
663 Result : Interfaces.C.int;
664 Param : aliased struct_sched_param;
666 function Get_Policy (Prio : System.Any_Priority) return Character;
667 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
668 -- Get priority specific dispatching policy
670 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
671 -- Upper case first character of the policy name corresponding to the
672 -- task as set by a Priority_Specific_Dispatching pragma.
674 begin
675 T.Common.Current_Priority := Prio;
677 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
679 Param.sched_priority := Interfaces.C.int (Prio) + 1;
681 if Dispatching_Policy = 'R'
682 or else Priority_Specific_Policy = 'R'
683 or else Time_Slice_Val > 0
684 then
685 Result :=
686 pthread_setschedparam
687 (T.Common.LL.Thread, SCHED_RR, Param'Access);
689 elsif Dispatching_Policy = 'F'
690 or else Priority_Specific_Policy = 'F'
691 or else Time_Slice_Val = 0
692 then
693 Result :=
694 pthread_setschedparam
695 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
697 else
698 Param.sched_priority := 0;
699 Result :=
700 pthread_setschedparam
701 (T.Common.LL.Thread,
702 SCHED_OTHER, Param'Access);
703 end if;
705 pragma Assert (Result = 0 or else Result = EPERM);
706 end Set_Priority;
708 ------------------
709 -- Get_Priority --
710 ------------------
712 function Get_Priority (T : Task_Id) return System.Any_Priority is
713 begin
714 return T.Common.Current_Priority;
715 end Get_Priority;
717 --------------------------
718 -- Get_Stack_Attributes --
719 --------------------------
721 procedure Get_Stack_Attributes
722 (T : Task_Id;
723 ISP : out System.Address;
724 Size : out Storage_Offset)
726 function pthread_getattr_np
727 (thread : pthread_t;
728 attr : System.Address) return Interfaces.C.int;
729 pragma Import (C, pthread_getattr_np, "pthread_getattr_np");
731 function pthread_attr_getstack
732 (attr : System.Address;
733 base : System.Address;
734 size : System.Address) return Interfaces.C.int;
735 pragma Import (C, pthread_attr_getstack, "pthread_attr_getstack");
737 Result : Interfaces.C.int;
739 Attributes : aliased pthread_attr_t;
740 Stack_Base : aliased System.Address;
741 Stack_Size : aliased Storage_Offset;
743 begin
744 Result :=
745 pthread_getattr_np
746 (T.Common.LL.Thread, Attributes'Address);
747 pragma Assert (Result = 0);
749 Result :=
750 pthread_attr_getstack
751 (Attributes'Address, Stack_Base'Address, Stack_Size'Address);
752 pragma Assert (Result = 0);
754 Result := pthread_attr_destroy (Attributes'Access);
755 pragma Assert (Result = 0);
757 ISP := Stack_Base + Stack_Size;
758 Size := Stack_Size;
759 end Get_Stack_Attributes;
761 ----------------
762 -- Enter_Task --
763 ----------------
765 procedure Enter_Task (Self_ID : Task_Id) is
766 begin
767 Self_ID.Common.LL.Thread := pthread_self;
769 Specific.Set (Self_ID);
771 Lock_RTS;
773 for J in Known_Tasks'Range loop
774 if Known_Tasks (J) = null then
775 Known_Tasks (J) := Self_ID;
776 Self_ID.Known_Tasks_Index := J;
777 exit;
778 end if;
779 end loop;
781 Unlock_RTS;
783 -- Determine where the task stack starts, how large it is, and let the
784 -- stack checking engine know about it.
786 declare
787 Initial_SP : System.Address;
788 Stack_Size : Storage_Offset;
789 begin
790 Get_Stack_Attributes (Self_ID, Initial_SP, Stack_Size);
791 System.Stack_Checking.Operations.Notify_Stack_Attributes
792 (Initial_SP, Stack_Size);
793 end;
794 end Enter_Task;
796 --------------
797 -- New_ATCB --
798 --------------
800 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
801 begin
802 return new Ada_Task_Control_Block (Entry_Num);
803 end New_ATCB;
805 -------------------
806 -- Is_Valid_Task --
807 -------------------
809 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
811 -----------------------------
812 -- Register_Foreign_Thread --
813 -----------------------------
815 function Register_Foreign_Thread return Task_Id is
816 begin
817 if Is_Valid_Task then
818 return Self;
819 else
820 return Register_Foreign_Thread (pthread_self);
821 end if;
822 end Register_Foreign_Thread;
824 --------------------
825 -- Initialize_TCB --
826 --------------------
828 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
829 Result : Interfaces.C.int;
831 begin
832 -- Give the task a unique serial number
834 Self_ID.Serial_Number := Next_Serial_Number;
835 Next_Serial_Number := Next_Serial_Number + 1;
836 pragma Assert (Next_Serial_Number /= 0);
838 Self_ID.Common.LL.Thread := To_pthread_t (-1);
840 if not Single_Lock then
841 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
842 Mutex_Attr'Access);
843 pragma Assert (Result = 0 or else Result = ENOMEM);
845 if Result /= 0 then
846 Succeeded := False;
847 return;
848 end if;
849 end if;
851 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
852 Cond_Attr'Access);
853 pragma Assert (Result = 0 or else Result = ENOMEM);
855 if Result = 0 then
856 Succeeded := True;
857 else
858 if not Single_Lock then
859 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
860 pragma Assert (Result = 0);
861 end if;
863 Succeeded := False;
864 end if;
865 end Initialize_TCB;
867 -----------------
868 -- Create_Task --
869 -----------------
871 procedure Create_Task
872 (T : Task_Id;
873 Wrapper : System.Address;
874 Stack_Size : System.Parameters.Size_Type;
875 Priority : System.Any_Priority;
876 Succeeded : out Boolean)
878 Attributes : aliased pthread_attr_t;
879 Result : Interfaces.C.int;
881 begin
882 Result := pthread_attr_init (Attributes'Access);
883 pragma Assert (Result = 0 or else Result = ENOMEM);
885 if Result /= 0 then
886 Succeeded := False;
887 return;
888 end if;
890 Result :=
891 pthread_attr_setstacksize
892 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
893 pragma Assert (Result = 0);
895 Result :=
896 pthread_attr_setdetachstate
897 (Attributes'Access, PTHREAD_CREATE_DETACHED);
898 pragma Assert (Result = 0);
900 -- Since the initial signal mask of a thread is inherited from the
901 -- creator, and the Environment task has all its signals masked, we
902 -- do not need to manipulate caller's signal mask at this point.
903 -- All tasks in RTS will have All_Tasks_Mask initially.
905 Result := pthread_create
906 (T.Common.LL.Thread'Access,
907 Attributes'Access,
908 Thread_Body_Access (Wrapper),
909 To_Address (T));
910 pragma Assert (Result = 0 or else Result = EAGAIN);
912 Succeeded := Result = 0;
914 Result := pthread_attr_destroy (Attributes'Access);
915 pragma Assert (Result = 0);
917 Set_Priority (T, Priority);
918 end Create_Task;
920 ------------------
921 -- Finalize_TCB --
922 ------------------
924 procedure Finalize_TCB (T : Task_Id) is
925 Result : Interfaces.C.int;
926 Tmp : Task_Id := T;
927 Is_Self : constant Boolean := T = Self;
929 procedure Free is new
930 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
932 begin
933 if not Single_Lock then
934 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
935 pragma Assert (Result = 0);
936 end if;
938 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
939 pragma Assert (Result = 0);
941 if T.Known_Tasks_Index /= -1 then
942 Known_Tasks (T.Known_Tasks_Index) := null;
943 end if;
944 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
945 Free (Tmp);
947 if Is_Self then
948 Specific.Set (null);
949 end if;
950 end Finalize_TCB;
952 ---------------
953 -- Exit_Task --
954 ---------------
956 procedure Exit_Task is
957 begin
958 Specific.Set (null);
959 end Exit_Task;
961 ----------------
962 -- Abort_Task --
963 ----------------
965 procedure Abort_Task (T : Task_Id) is
966 Result : Interfaces.C.int;
967 begin
968 Result :=
969 pthread_kill
970 (T.Common.LL.Thread,
971 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
972 pragma Assert (Result = 0);
973 end Abort_Task;
975 ----------------
976 -- Initialize --
977 ----------------
979 procedure Initialize (S : in out Suspension_Object) is
980 Result : Interfaces.C.int;
982 begin
983 -- Initialize internal state (always to False (RM D.10(6)))
985 S.State := False;
986 S.Waiting := False;
988 -- Initialize internal mutex
990 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
992 pragma Assert (Result = 0 or else Result = ENOMEM);
994 if Result = ENOMEM then
995 raise Storage_Error;
996 end if;
998 -- Initialize internal condition variable
1000 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1002 pragma Assert (Result = 0 or else Result = ENOMEM);
1004 if Result /= 0 then
1005 Result := pthread_mutex_destroy (S.L'Access);
1006 pragma Assert (Result = 0);
1008 if Result = ENOMEM then
1009 raise Storage_Error;
1010 end if;
1011 end if;
1012 end Initialize;
1014 --------------
1015 -- Finalize --
1016 --------------
1018 procedure Finalize (S : in out Suspension_Object) is
1019 Result : Interfaces.C.int;
1021 begin
1022 -- Destroy internal mutex
1024 Result := pthread_mutex_destroy (S.L'Access);
1025 pragma Assert (Result = 0);
1027 -- Destroy internal condition variable
1029 Result := pthread_cond_destroy (S.CV'Access);
1030 pragma Assert (Result = 0);
1031 end Finalize;
1033 -------------------
1034 -- Current_State --
1035 -------------------
1037 function Current_State (S : Suspension_Object) return Boolean is
1038 begin
1039 -- We do not want to use lock on this read operation. State is marked
1040 -- as Atomic so that we ensure that the value retrieved is correct.
1042 return S.State;
1043 end Current_State;
1045 ---------------
1046 -- Set_False --
1047 ---------------
1049 procedure Set_False (S : in out Suspension_Object) is
1050 Result : Interfaces.C.int;
1052 begin
1053 SSL.Abort_Defer.all;
1055 Result := pthread_mutex_lock (S.L'Access);
1056 pragma Assert (Result = 0);
1058 S.State := False;
1060 Result := pthread_mutex_unlock (S.L'Access);
1061 pragma Assert (Result = 0);
1063 SSL.Abort_Undefer.all;
1064 end Set_False;
1066 --------------
1067 -- Set_True --
1068 --------------
1070 procedure Set_True (S : in out Suspension_Object) is
1071 Result : Interfaces.C.int;
1073 begin
1074 SSL.Abort_Defer.all;
1076 Result := pthread_mutex_lock (S.L'Access);
1077 pragma Assert (Result = 0);
1079 -- If there is already a task waiting on this suspension object then
1080 -- we resume it, leaving the state of the suspension object to False,
1081 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1082 -- the state to True.
1084 if S.Waiting then
1085 S.Waiting := False;
1086 S.State := False;
1088 Result := pthread_cond_signal (S.CV'Access);
1089 pragma Assert (Result = 0);
1091 else
1092 S.State := True;
1093 end if;
1095 Result := pthread_mutex_unlock (S.L'Access);
1096 pragma Assert (Result = 0);
1098 SSL.Abort_Undefer.all;
1099 end Set_True;
1101 ------------------------
1102 -- Suspend_Until_True --
1103 ------------------------
1105 procedure Suspend_Until_True (S : in out Suspension_Object) is
1106 Result : Interfaces.C.int;
1108 begin
1109 SSL.Abort_Defer.all;
1111 Result := pthread_mutex_lock (S.L'Access);
1112 pragma Assert (Result = 0);
1114 if S.Waiting then
1116 -- Program_Error must be raised upon calling Suspend_Until_True
1117 -- if another task is already waiting on that suspension object
1118 -- (RM D.10(10)).
1120 Result := pthread_mutex_unlock (S.L'Access);
1121 pragma Assert (Result = 0);
1123 SSL.Abort_Undefer.all;
1125 raise Program_Error;
1126 else
1127 -- Suspend the task if the state is False. Otherwise, the task
1128 -- continues its execution, and the state of the suspension object
1129 -- is set to False (ARM D.10 par. 9).
1131 if S.State then
1132 S.State := False;
1133 else
1134 S.Waiting := True;
1135 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1136 end if;
1138 Result := pthread_mutex_unlock (S.L'Access);
1139 pragma Assert (Result = 0);
1141 SSL.Abort_Undefer.all;
1144 end Suspend_Until_True;
1146 ----------------
1147 -- Check_Exit --
1148 ----------------
1150 -- Dummy version
1152 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1153 pragma Unreferenced (Self_ID);
1154 begin
1155 return True;
1156 end Check_Exit;
1158 --------------------
1159 -- Check_No_Locks --
1160 --------------------
1162 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1163 pragma Unreferenced (Self_ID);
1164 begin
1165 return True;
1166 end Check_No_Locks;
1168 ----------------------
1169 -- Environment_Task --
1170 ----------------------
1172 function Environment_Task return Task_Id is
1173 begin
1174 return Environment_Task_Id;
1175 end Environment_Task;
1177 ------------------
1178 -- Suspend_Task --
1179 ------------------
1181 function Suspend_Task
1182 (T : ST.Task_Id;
1183 Thread_Self : Thread_Id) return Boolean
1185 begin
1186 if T.Common.LL.Thread /= Thread_Self then
1187 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1188 else
1189 return True;
1190 end if;
1191 end Suspend_Task;
1193 -----------------
1194 -- Resume_Task --
1195 -----------------
1197 function Resume_Task
1198 (T : ST.Task_Id;
1199 Thread_Self : Thread_Id) return Boolean
1201 begin
1202 if T.Common.LL.Thread /= Thread_Self then
1203 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1204 else
1205 return True;
1206 end if;
1207 end Resume_Task;
1209 --------------------
1210 -- Stop_All_Tasks --
1211 --------------------
1213 procedure Stop_All_Tasks is
1214 begin
1215 null;
1216 end Stop_All_Tasks;
1218 ---------------
1219 -- Stop_Task --
1220 ---------------
1222 function Stop_Task (T : ST.Task_Id) return Boolean is
1223 pragma Unreferenced (T);
1224 begin
1225 return False;
1226 end Stop_Task;
1228 -------------------
1229 -- Continue_Task --
1230 -------------------
1232 function Continue_Task (T : ST.Task_Id) return Boolean is
1233 pragma Unreferenced (T);
1234 begin
1235 return False;
1236 end Continue_Task;
1238 ----------------
1239 -- Initialize --
1240 ----------------
1242 procedure Initialize (Environment_Task : Task_Id) is
1243 act : aliased struct_sigaction;
1244 old_act : aliased struct_sigaction;
1245 Tmp_Set : aliased sigset_t;
1246 Result : Interfaces.C.int;
1248 function State
1249 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1250 pragma Import (C, State, "__gnat_get_interrupt_state");
1251 -- Get interrupt state. Defined in a-init.c
1252 -- The input argument is the interrupt number,
1253 -- and the result is one of the following:
1255 Default : constant Character := 's';
1256 -- 'n' this interrupt not set by any Interrupt_State pragma
1257 -- 'u' Interrupt_State pragma set state to User
1258 -- 'r' Interrupt_State pragma set state to Runtime
1259 -- 's' Interrupt_State pragma set state to System (use "default"
1260 -- system handler)
1262 begin
1263 Environment_Task_Id := Environment_Task;
1265 Interrupt_Management.Initialize;
1267 -- Prepare the set of signals that should be unblocked in all tasks
1269 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1270 pragma Assert (Result = 0);
1272 for J in Interrupt_Management.Interrupt_ID loop
1273 if System.Interrupt_Management.Keep_Unmasked (J) then
1274 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1275 pragma Assert (Result = 0);
1276 end if;
1277 end loop;
1279 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1280 pragma Assert (Result = 0);
1282 Result := pthread_condattr_init (Cond_Attr'Access);
1283 pragma Assert (Result = 0);
1285 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1287 -- Initialize the global RTS lock
1289 Specific.Initialize (Environment_Task);
1291 Enter_Task (Environment_Task);
1293 -- Install the abort-signal handler
1295 if State
1296 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1297 then
1298 act.sa_flags := 0;
1299 act.sa_handler := Abort_Handler'Address;
1301 Result := sigemptyset (Tmp_Set'Access);
1302 pragma Assert (Result = 0);
1303 act.sa_mask := Tmp_Set;
1305 Result :=
1306 sigaction
1307 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1308 act'Unchecked_Access,
1309 old_act'Unchecked_Access);
1310 pragma Assert (Result = 0);
1311 end if;
1312 end Initialize;
1314 end System.Task_Primitives.Operations;