In gcc/testsuite/: 2010-09-30 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / ada / s-taprop-hpux-dce.adb
blobebc2f9ddc0c94dfb3e3fa2bc31beb4d6be2630cb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is a HP-UX DCE threads (HPUX 10) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
37 pragma Polling (Off);
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
41 with Ada.Unchecked_Conversion;
42 with Ada.Unchecked_Deallocation;
44 with Interfaces.C;
46 with System.Tasking.Debug;
47 with System.Interrupt_Management;
48 with System.OS_Primitives;
49 with System.Task_Primitives.Interrupt_Operations;
51 pragma Warnings (Off);
52 with System.Interrupt_Management.Operations;
53 pragma Elaborate_All (System.Interrupt_Management.Operations);
54 pragma Warnings (On);
56 with System.Soft_Links;
57 -- We use System.Soft_Links instead of System.Tasking.Initialization
58 -- because the later is a higher level package that we shouldn't depend on.
59 -- For example when using the restricted run time, it is replaced by
60 -- System.Tasking.Restricted.Stages.
62 package body System.Task_Primitives.Operations is
64 package SSL renames System.Soft_Links;
66 use System.Tasking.Debug;
67 use System.Tasking;
68 use Interfaces.C;
69 use System.OS_Interface;
70 use System.Parameters;
71 use System.OS_Primitives;
73 package PIO renames System.Task_Primitives.Interrupt_Operations;
75 ----------------
76 -- Local Data --
77 ----------------
79 -- The followings are logically constants, but need to be initialized
80 -- at run time.
82 Single_RTS_Lock : aliased RTS_Lock;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 ATCB_Key : aliased pthread_key_t;
88 -- Key used to find the Ada Task_Id associated with a thread
90 Environment_Task_Id : Task_Id;
91 -- A variable to hold Task_Id for the environment task
93 Unblocked_Signal_Mask : aliased sigset_t;
94 -- The set of signals that should unblocked in all tasks
96 Time_Slice_Val : Integer;
97 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
99 Dispatching_Policy : Character;
100 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
102 -- Note: the reason that Locking_Policy is not needed is that this
103 -- is not implemented for DCE threads. The HPUX 10 port is at this
104 -- stage considered dead, and no further work is planned on it.
106 Foreign_Task_Elaborated : aliased Boolean := True;
107 -- Used to identified fake tasks (i.e., non-Ada Threads)
109 --------------------
110 -- Local Packages --
111 --------------------
113 package Specific is
115 procedure Initialize (Environment_Task : Task_Id);
116 pragma Inline (Initialize);
117 -- Initialize various data needed by this package
119 function Is_Valid_Task return Boolean;
120 pragma Inline (Is_Valid_Task);
121 -- Does the executing thread have a TCB?
123 procedure Set (Self_Id : Task_Id);
124 pragma Inline (Set);
125 -- Set the self id for the current task
127 function Self return Task_Id;
128 pragma Inline (Self);
129 -- Return a pointer to the Ada Task Control Block of the calling task
131 end Specific;
133 package body Specific is separate;
134 -- The body of this package is target specific
136 ---------------------------------
137 -- Support for foreign threads --
138 ---------------------------------
140 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
141 -- Allocate and Initialize a new ATCB for the current Thread
143 function Register_Foreign_Thread
144 (Thread : Thread_Id) return Task_Id is separate;
146 -----------------------
147 -- Local Subprograms --
148 -----------------------
150 procedure Abort_Handler (Sig : Signal);
152 function To_Address is
153 new Ada.Unchecked_Conversion (Task_Id, System.Address);
155 -------------------
156 -- Abort_Handler --
157 -------------------
159 procedure Abort_Handler (Sig : Signal) is
160 pragma Unreferenced (Sig);
162 Self_Id : constant Task_Id := Self;
163 Result : Interfaces.C.int;
164 Old_Set : aliased sigset_t;
166 begin
167 if Self_Id.Deferral_Level = 0
168 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
169 and then not Self_Id.Aborting
170 then
171 Self_Id.Aborting := True;
173 -- Make sure signals used for RTS internal purpose are unmasked
175 Result :=
176 pthread_sigmask
177 (SIG_UNBLOCK,
178 Unblocked_Signal_Mask'Access,
179 Old_Set'Access);
180 pragma Assert (Result = 0);
182 raise Standard'Abort_Signal;
183 end if;
184 end Abort_Handler;
186 -----------------
187 -- Stack_Guard --
188 -----------------
190 -- The underlying thread system sets a guard page at the bottom of a thread
191 -- stack, so nothing is needed.
192 -- ??? Check the comment above
194 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
195 pragma Unreferenced (T, On);
196 begin
197 null;
198 end Stack_Guard;
200 -------------------
201 -- Get_Thread_Id --
202 -------------------
204 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
205 begin
206 return T.Common.LL.Thread;
207 end Get_Thread_Id;
209 ----------
210 -- Self --
211 ----------
213 function Self return Task_Id renames Specific.Self;
215 ---------------------
216 -- Initialize_Lock --
217 ---------------------
219 -- Note: mutexes and cond_variables needed per-task basis are initialized
220 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
221 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
222 -- status change of RTS. Therefore raising Storage_Error in the following
223 -- routines should be able to be handled safely.
225 procedure Initialize_Lock
226 (Prio : System.Any_Priority;
227 L : not null access Lock)
229 Attributes : aliased pthread_mutexattr_t;
230 Result : Interfaces.C.int;
232 begin
233 Result := pthread_mutexattr_init (Attributes'Access);
234 pragma Assert (Result = 0 or else Result = ENOMEM);
236 if Result = ENOMEM then
237 raise Storage_Error;
238 end if;
240 L.Priority := Prio;
242 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
243 pragma Assert (Result = 0 or else Result = ENOMEM);
245 if Result = ENOMEM then
246 raise Storage_Error;
247 end if;
249 Result := pthread_mutexattr_destroy (Attributes'Access);
250 pragma Assert (Result = 0);
251 end Initialize_Lock;
253 procedure Initialize_Lock
254 (L : not null access RTS_Lock;
255 Level : Lock_Level)
257 pragma Unreferenced (Level);
259 Attributes : aliased pthread_mutexattr_t;
260 Result : Interfaces.C.int;
262 begin
263 Result := pthread_mutexattr_init (Attributes'Access);
264 pragma Assert (Result = 0 or else Result = ENOMEM);
266 if Result = ENOMEM then
267 raise Storage_Error;
268 end if;
270 Result := pthread_mutex_init (L, Attributes'Access);
272 pragma Assert (Result = 0 or else Result = ENOMEM);
274 if Result = ENOMEM then
275 raise Storage_Error;
276 end if;
278 Result := pthread_mutexattr_destroy (Attributes'Access);
279 pragma Assert (Result = 0);
280 end Initialize_Lock;
282 -------------------
283 -- Finalize_Lock --
284 -------------------
286 procedure Finalize_Lock (L : not null access Lock) is
287 Result : Interfaces.C.int;
288 begin
289 Result := pthread_mutex_destroy (L.L'Access);
290 pragma Assert (Result = 0);
291 end Finalize_Lock;
293 procedure Finalize_Lock (L : not null access RTS_Lock) is
294 Result : Interfaces.C.int;
295 begin
296 Result := pthread_mutex_destroy (L);
297 pragma Assert (Result = 0);
298 end Finalize_Lock;
300 ----------------
301 -- Write_Lock --
302 ----------------
304 procedure Write_Lock
305 (L : not null access Lock;
306 Ceiling_Violation : out Boolean)
308 Result : Interfaces.C.int;
310 begin
311 L.Owner_Priority := Get_Priority (Self);
313 if L.Priority < L.Owner_Priority then
314 Ceiling_Violation := True;
315 return;
316 end if;
318 Result := pthread_mutex_lock (L.L'Access);
319 pragma Assert (Result = 0);
320 Ceiling_Violation := False;
321 end Write_Lock;
323 procedure Write_Lock
324 (L : not null 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
349 (L : not null access Lock;
350 Ceiling_Violation : out Boolean)
352 begin
353 Write_Lock (L, Ceiling_Violation);
354 end Read_Lock;
356 ------------
357 -- Unlock --
358 ------------
360 procedure Unlock (L : not null access Lock) is
361 Result : Interfaces.C.int;
362 begin
363 Result := pthread_mutex_unlock (L.L'Access);
364 pragma Assert (Result = 0);
365 end Unlock;
367 procedure Unlock
368 (L : not null access RTS_Lock;
369 Global_Lock : Boolean := False)
371 Result : Interfaces.C.int;
372 begin
373 if not Single_Lock or else Global_Lock then
374 Result := pthread_mutex_unlock (L);
375 pragma Assert (Result = 0);
376 end if;
377 end Unlock;
379 procedure Unlock (T : Task_Id) is
380 Result : Interfaces.C.int;
381 begin
382 if not Single_Lock then
383 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
384 pragma Assert (Result = 0);
385 end if;
386 end Unlock;
388 -----------------
389 -- Set_Ceiling --
390 -----------------
392 -- Dynamic priority ceilings are not supported by the underlying system
394 procedure Set_Ceiling
395 (L : not null access Lock;
396 Prio : System.Any_Priority)
398 pragma Unreferenced (L, Prio);
399 begin
400 null;
401 end Set_Ceiling;
403 -----------
404 -- Sleep --
405 -----------
407 procedure Sleep
408 (Self_ID : Task_Id;
409 Reason : System.Tasking.Task_States)
411 pragma Unreferenced (Reason);
413 Result : Interfaces.C.int;
415 begin
416 Result :=
417 pthread_cond_wait
418 (cond => Self_ID.Common.LL.CV'Access,
419 mutex => (if Single_Lock
420 then Single_RTS_Lock'Access
421 else Self_ID.Common.LL.L'Access));
423 -- EINTR is not considered a failure
425 pragma Assert (Result = 0 or else Result = EINTR);
426 end Sleep;
428 -----------------
429 -- Timed_Sleep --
430 -----------------
432 procedure Timed_Sleep
433 (Self_ID : Task_Id;
434 Time : Duration;
435 Mode : ST.Delay_Modes;
436 Reason : System.Tasking.Task_States;
437 Timedout : out Boolean;
438 Yielded : out Boolean)
440 pragma Unreferenced (Reason);
442 Check_Time : constant Duration := Monotonic_Clock;
443 Abs_Time : Duration;
444 Request : aliased timespec;
445 Result : Interfaces.C.int;
447 begin
448 Timedout := True;
449 Yielded := False;
451 Abs_Time :=
452 (if Mode = Relative
453 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
454 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
456 if Abs_Time > Check_Time then
457 Request := To_Timespec (Abs_Time);
459 loop
460 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
462 Result :=
463 pthread_cond_timedwait
464 (cond => Self_ID.Common.LL.CV'Access,
465 mutex => (if Single_Lock
466 then Single_RTS_Lock'Access
467 else Self_ID.Common.LL.L'Access),
468 abstime => Request'Access);
470 exit when Abs_Time <= Monotonic_Clock;
472 if Result = 0 or Result = EINTR then
474 -- Somebody may have called Wakeup for us
476 Timedout := False;
477 exit;
478 end if;
480 pragma Assert (Result = ETIMEDOUT);
481 end loop;
482 end if;
483 end Timed_Sleep;
485 -----------------
486 -- Timed_Delay --
487 -----------------
489 procedure Timed_Delay
490 (Self_ID : Task_Id;
491 Time : Duration;
492 Mode : ST.Delay_Modes)
494 Check_Time : constant Duration := Monotonic_Clock;
495 Abs_Time : Duration;
496 Request : aliased timespec;
498 Result : Interfaces.C.int;
499 pragma Warnings (Off, Result);
501 begin
502 if Single_Lock then
503 Lock_RTS;
504 end if;
506 Write_Lock (Self_ID);
508 Abs_Time :=
509 (if Mode = Relative
510 then Time + Check_Time
511 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
513 if Abs_Time > Check_Time then
514 Request := To_Timespec (Abs_Time);
515 Self_ID.Common.State := Delay_Sleep;
517 loop
518 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
520 Result :=
521 pthread_cond_timedwait
522 (cond => Self_ID.Common.LL.CV'Access,
523 mutex => (if Single_Lock
524 then Single_RTS_Lock'Access
525 else Self_ID.Common.LL.L'Access),
526 abstime => Request'Access);
528 exit when Abs_Time <= Monotonic_Clock;
530 pragma Assert (Result = 0 or else
531 Result = ETIMEDOUT or else
532 Result = EINTR);
533 end loop;
535 Self_ID.Common.State := Runnable;
536 end if;
538 Unlock (Self_ID);
540 if Single_Lock then
541 Unlock_RTS;
542 end if;
544 Result := sched_yield;
545 end Timed_Delay;
547 ---------------------
548 -- Monotonic_Clock --
549 ---------------------
551 function Monotonic_Clock return Duration is
552 TS : aliased timespec;
553 Result : Interfaces.C.int;
554 begin
555 Result := Clock_Gettime (CLOCK_REALTIME, TS'Unchecked_Access);
556 pragma Assert (Result = 0);
557 return To_Duration (TS);
558 end Monotonic_Clock;
560 -------------------
561 -- RT_Resolution --
562 -------------------
564 function RT_Resolution return Duration is
565 begin
566 return 10#1.0#E-6;
567 end RT_Resolution;
569 ------------
570 -- Wakeup --
571 ------------
573 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
574 pragma Unreferenced (Reason);
575 Result : Interfaces.C.int;
576 begin
577 Result := pthread_cond_signal (T.Common.LL.CV'Access);
578 pragma Assert (Result = 0);
579 end Wakeup;
581 -----------
582 -- Yield --
583 -----------
585 procedure Yield (Do_Yield : Boolean := True) is
586 Result : Interfaces.C.int;
587 pragma Unreferenced (Result);
588 begin
589 if Do_Yield then
590 Result := sched_yield;
591 end if;
592 end Yield;
594 ------------------
595 -- Set_Priority --
596 ------------------
598 type Prio_Array_Type is array (System.Any_Priority) of Integer;
599 pragma Atomic_Components (Prio_Array_Type);
601 Prio_Array : Prio_Array_Type;
602 -- Global array containing the id of the currently running task for
603 -- each priority.
605 -- Note: assume we are on single processor with run-til-blocked scheduling
607 procedure Set_Priority
608 (T : Task_Id;
609 Prio : System.Any_Priority;
610 Loss_Of_Inheritance : Boolean := False)
612 Result : Interfaces.C.int;
613 Array_Item : Integer;
614 Param : aliased struct_sched_param;
616 function Get_Policy (Prio : System.Any_Priority) return Character;
617 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
618 -- Get priority specific dispatching policy
620 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
621 -- Upper case first character of the policy name corresponding to the
622 -- task as set by a Priority_Specific_Dispatching pragma.
624 begin
625 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
627 if Dispatching_Policy = 'R'
628 or else Priority_Specific_Policy = 'R'
629 or else Time_Slice_Val > 0
630 then
631 Result :=
632 pthread_setschedparam
633 (T.Common.LL.Thread, SCHED_RR, Param'Access);
635 elsif Dispatching_Policy = 'F'
636 or else Priority_Specific_Policy = 'F'
637 or else Time_Slice_Val = 0
638 then
639 Result :=
640 pthread_setschedparam
641 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
643 else
644 Result :=
645 pthread_setschedparam
646 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
647 end if;
649 pragma Assert (Result = 0);
651 if Dispatching_Policy = 'F' or else Priority_Specific_Policy = 'F' then
653 -- Annex D requirement [RM D.2.2 par. 9]:
654 -- If the task drops its priority due to the loss of inherited
655 -- priority, it is added at the head of the ready queue for its
656 -- new active priority.
658 if Loss_Of_Inheritance
659 and then Prio < T.Common.Current_Priority
660 then
661 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
662 Prio_Array (T.Common.Base_Priority) := Array_Item;
664 loop
665 -- Let some processes a chance to arrive
667 Yield;
669 -- Then wait for our turn to proceed
671 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
672 or else Prio_Array (T.Common.Base_Priority) = 1;
673 end loop;
675 Prio_Array (T.Common.Base_Priority) :=
676 Prio_Array (T.Common.Base_Priority) - 1;
677 end if;
678 end if;
680 T.Common.Current_Priority := Prio;
681 end Set_Priority;
683 ------------------
684 -- Get_Priority --
685 ------------------
687 function Get_Priority (T : Task_Id) return System.Any_Priority is
688 begin
689 return T.Common.Current_Priority;
690 end Get_Priority;
692 ----------------
693 -- Enter_Task --
694 ----------------
696 procedure Enter_Task (Self_ID : Task_Id) is
697 begin
698 Self_ID.Common.LL.Thread := pthread_self;
699 Specific.Set (Self_ID);
700 end Enter_Task;
702 --------------
703 -- New_ATCB --
704 --------------
706 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
707 begin
708 return new Ada_Task_Control_Block (Entry_Num);
709 end New_ATCB;
711 -------------------
712 -- Is_Valid_Task --
713 -------------------
715 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
717 -----------------------------
718 -- Register_Foreign_Thread --
719 -----------------------------
721 function Register_Foreign_Thread return Task_Id is
722 begin
723 if Is_Valid_Task then
724 return Self;
725 else
726 return Register_Foreign_Thread (pthread_self);
727 end if;
728 end Register_Foreign_Thread;
730 --------------------
731 -- Initialize_TCB --
732 --------------------
734 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
735 Mutex_Attr : aliased pthread_mutexattr_t;
736 Result : Interfaces.C.int;
737 Cond_Attr : aliased pthread_condattr_t;
739 begin
740 if not Single_Lock then
741 Result := pthread_mutexattr_init (Mutex_Attr'Access);
742 pragma Assert (Result = 0 or else Result = ENOMEM);
744 if Result = 0 then
745 Result :=
746 pthread_mutex_init
747 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
748 pragma Assert (Result = 0 or else Result = ENOMEM);
749 end if;
751 if Result /= 0 then
752 Succeeded := False;
753 return;
754 end if;
756 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
757 pragma Assert (Result = 0);
758 end if;
760 Result := pthread_condattr_init (Cond_Attr'Access);
761 pragma Assert (Result = 0 or else Result = ENOMEM);
763 if Result = 0 then
764 Result :=
765 pthread_cond_init
766 (Self_ID.Common.LL.CV'Access,
767 Cond_Attr'Access);
768 pragma Assert (Result = 0 or else Result = ENOMEM);
769 end if;
771 if Result = 0 then
772 Succeeded := True;
773 else
774 if not Single_Lock then
775 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
776 pragma Assert (Result = 0);
777 end if;
779 Succeeded := False;
780 end if;
782 Result := pthread_condattr_destroy (Cond_Attr'Access);
783 pragma Assert (Result = 0);
784 end Initialize_TCB;
786 -----------------
787 -- Create_Task --
788 -----------------
790 procedure Create_Task
791 (T : Task_Id;
792 Wrapper : System.Address;
793 Stack_Size : System.Parameters.Size_Type;
794 Priority : System.Any_Priority;
795 Succeeded : out Boolean)
797 Attributes : aliased pthread_attr_t;
798 Result : Interfaces.C.int;
800 function Thread_Body_Access is new
801 Ada.Unchecked_Conversion (System.Address, Thread_Body);
803 begin
804 Result := pthread_attr_init (Attributes'Access);
805 pragma Assert (Result = 0 or else Result = ENOMEM);
807 if Result /= 0 then
808 Succeeded := False;
809 return;
810 end if;
812 Result := pthread_attr_setstacksize
813 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
814 pragma Assert (Result = 0);
816 -- Since the initial signal mask of a thread is inherited from the
817 -- creator, and the Environment task has all its signals masked, we
818 -- do not need to manipulate caller's signal mask at this point.
819 -- All tasks in RTS will have All_Tasks_Mask initially.
821 Result := pthread_create
822 (T.Common.LL.Thread'Access,
823 Attributes'Access,
824 Thread_Body_Access (Wrapper),
825 To_Address (T));
826 pragma Assert (Result = 0 or else Result = EAGAIN);
828 Succeeded := Result = 0;
830 pthread_detach (T.Common.LL.Thread'Access);
831 -- Detach the thread using pthread_detach, since DCE threads do not have
832 -- pthread_attr_set_detachstate.
834 Result := pthread_attr_destroy (Attributes'Access);
835 pragma Assert (Result = 0);
837 Set_Priority (T, Priority);
838 end Create_Task;
840 ------------------
841 -- Finalize_TCB --
842 ------------------
844 procedure Finalize_TCB (T : Task_Id) is
845 Result : Interfaces.C.int;
846 Tmp : Task_Id := T;
847 Is_Self : constant Boolean := T = Self;
849 procedure Free is new
850 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
852 begin
853 if not Single_Lock then
854 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
855 pragma Assert (Result = 0);
856 end if;
858 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
859 pragma Assert (Result = 0);
861 if T.Known_Tasks_Index /= -1 then
862 Known_Tasks (T.Known_Tasks_Index) := null;
863 end if;
865 Free (Tmp);
867 if Is_Self then
868 Specific.Set (null);
869 end if;
870 end Finalize_TCB;
872 ---------------
873 -- Exit_Task --
874 ---------------
876 procedure Exit_Task is
877 begin
878 Specific.Set (null);
879 end Exit_Task;
881 ----------------
882 -- Abort_Task --
883 ----------------
885 procedure Abort_Task (T : Task_Id) is
886 begin
887 -- Interrupt Server_Tasks may be waiting on an "event" flag (signal)
889 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
890 System.Interrupt_Management.Operations.Interrupt_Self_Process
891 (System.Interrupt_Management.Interrupt_ID
892 (PIO.Get_Interrupt_ID (T)));
893 end if;
894 end Abort_Task;
896 ----------------
897 -- Initialize --
898 ----------------
900 procedure Initialize (S : in out Suspension_Object) is
901 Mutex_Attr : aliased pthread_mutexattr_t;
902 Cond_Attr : aliased pthread_condattr_t;
903 Result : Interfaces.C.int;
904 begin
905 -- Initialize internal state (always to False (ARM D.10(6)))
907 S.State := False;
908 S.Waiting := False;
910 -- Initialize internal mutex
912 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
913 pragma Assert (Result = 0 or else Result = ENOMEM);
915 if Result = ENOMEM then
916 raise Storage_Error;
917 end if;
919 -- Initialize internal condition variable
921 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
922 pragma Assert (Result = 0 or else Result = ENOMEM);
924 if Result /= 0 then
925 Result := pthread_mutex_destroy (S.L'Access);
926 pragma Assert (Result = 0);
928 if Result = ENOMEM then
929 raise Storage_Error;
930 end if;
931 end if;
932 end Initialize;
934 --------------
935 -- Finalize --
936 --------------
938 procedure Finalize (S : in out Suspension_Object) is
939 Result : Interfaces.C.int;
941 begin
942 -- Destroy internal mutex
944 Result := pthread_mutex_destroy (S.L'Access);
945 pragma Assert (Result = 0);
947 -- Destroy internal condition variable
949 Result := pthread_cond_destroy (S.CV'Access);
950 pragma Assert (Result = 0);
951 end Finalize;
953 -------------------
954 -- Current_State --
955 -------------------
957 function Current_State (S : Suspension_Object) return Boolean is
958 begin
959 -- We do not want to use lock on this read operation. State is marked
960 -- as Atomic so that we ensure that the value retrieved is correct.
962 return S.State;
963 end Current_State;
965 ---------------
966 -- Set_False --
967 ---------------
969 procedure Set_False (S : in out Suspension_Object) is
970 Result : Interfaces.C.int;
972 begin
973 SSL.Abort_Defer.all;
975 Result := pthread_mutex_lock (S.L'Access);
976 pragma Assert (Result = 0);
978 S.State := False;
980 Result := pthread_mutex_unlock (S.L'Access);
981 pragma Assert (Result = 0);
983 SSL.Abort_Undefer.all;
984 end Set_False;
986 --------------
987 -- Set_True --
988 --------------
990 procedure Set_True (S : in out Suspension_Object) is
991 Result : Interfaces.C.int;
993 begin
994 SSL.Abort_Defer.all;
996 Result := pthread_mutex_lock (S.L'Access);
997 pragma Assert (Result = 0);
999 -- If there is already a task waiting on this suspension object then
1000 -- we resume it, leaving the state of the suspension object to False,
1001 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1002 -- the state to True.
1004 if S.Waiting then
1005 S.Waiting := False;
1006 S.State := False;
1008 Result := pthread_cond_signal (S.CV'Access);
1009 pragma Assert (Result = 0);
1011 else
1012 S.State := True;
1013 end if;
1015 Result := pthread_mutex_unlock (S.L'Access);
1016 pragma Assert (Result = 0);
1018 SSL.Abort_Undefer.all;
1019 end Set_True;
1021 ------------------------
1022 -- Suspend_Until_True --
1023 ------------------------
1025 procedure Suspend_Until_True (S : in out Suspension_Object) is
1026 Result : Interfaces.C.int;
1028 begin
1029 SSL.Abort_Defer.all;
1031 Result := pthread_mutex_lock (S.L'Access);
1032 pragma Assert (Result = 0);
1034 if S.Waiting then
1035 -- Program_Error must be raised upon calling Suspend_Until_True
1036 -- if another task is already waiting on that suspension object
1037 -- (ARM D.10 par. 10).
1039 Result := pthread_mutex_unlock (S.L'Access);
1040 pragma Assert (Result = 0);
1042 SSL.Abort_Undefer.all;
1044 raise Program_Error;
1045 else
1046 -- Suspend the task if the state is False. Otherwise, the task
1047 -- continues its execution, and the state of the suspension object
1048 -- is set to False (ARM D.10 par. 9).
1050 if S.State then
1051 S.State := False;
1052 else
1053 S.Waiting := True;
1055 loop
1056 -- Loop in case pthread_cond_wait returns earlier than expected
1057 -- (e.g. in case of EINTR caused by a signal).
1059 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1060 pragma Assert (Result = 0 or else Result = EINTR);
1062 exit when not S.Waiting;
1063 end loop;
1064 end if;
1066 Result := pthread_mutex_unlock (S.L'Access);
1067 pragma Assert (Result = 0);
1069 SSL.Abort_Undefer.all;
1070 end if;
1071 end Suspend_Until_True;
1073 ----------------
1074 -- Check_Exit --
1075 ----------------
1077 -- Dummy version
1079 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1080 pragma Unreferenced (Self_ID);
1081 begin
1082 return True;
1083 end Check_Exit;
1085 --------------------
1086 -- Check_No_Locks --
1087 --------------------
1089 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1090 pragma Unreferenced (Self_ID);
1091 begin
1092 return True;
1093 end Check_No_Locks;
1095 ----------------------
1096 -- Environment_Task --
1097 ----------------------
1099 function Environment_Task return Task_Id is
1100 begin
1101 return Environment_Task_Id;
1102 end Environment_Task;
1104 --------------
1105 -- Lock_RTS --
1106 --------------
1108 procedure Lock_RTS is
1109 begin
1110 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1111 end Lock_RTS;
1113 ----------------
1114 -- Unlock_RTS --
1115 ----------------
1117 procedure Unlock_RTS is
1118 begin
1119 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1120 end Unlock_RTS;
1122 ------------------
1123 -- Suspend_Task --
1124 ------------------
1126 function Suspend_Task
1127 (T : ST.Task_Id;
1128 Thread_Self : Thread_Id) return Boolean
1130 pragma Unreferenced (T);
1131 pragma Unreferenced (Thread_Self);
1132 begin
1133 return False;
1134 end Suspend_Task;
1136 -----------------
1137 -- Resume_Task --
1138 -----------------
1140 function Resume_Task
1141 (T : ST.Task_Id;
1142 Thread_Self : Thread_Id) return Boolean
1144 pragma Unreferenced (T);
1145 pragma Unreferenced (Thread_Self);
1146 begin
1147 return False;
1148 end Resume_Task;
1150 --------------------
1151 -- Stop_All_Tasks --
1152 --------------------
1154 procedure Stop_All_Tasks is
1155 begin
1156 null;
1157 end Stop_All_Tasks;
1159 ---------------
1160 -- Stop_Task --
1161 ---------------
1163 function Stop_Task (T : ST.Task_Id) return Boolean is
1164 pragma Unreferenced (T);
1165 begin
1166 return False;
1167 end Stop_Task;
1169 -------------------
1170 -- Continue_Task --
1171 -------------------
1173 function Continue_Task (T : ST.Task_Id) return Boolean is
1174 pragma Unreferenced (T);
1175 begin
1176 return False;
1177 end Continue_Task;
1179 ----------------
1180 -- Initialize --
1181 ----------------
1183 procedure Initialize (Environment_Task : Task_Id) is
1184 act : aliased struct_sigaction;
1185 old_act : aliased struct_sigaction;
1186 Tmp_Set : aliased sigset_t;
1187 Result : Interfaces.C.int;
1189 function State
1190 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1191 pragma Import (C, State, "__gnat_get_interrupt_state");
1192 -- Get interrupt state. Defined in a-init.c. The input argument is
1193 -- the interrupt number, and the result is one of the following:
1195 Default : constant Character := 's';
1196 -- 'n' this interrupt not set by any Interrupt_State pragma
1197 -- 'u' Interrupt_State pragma set state to User
1198 -- 'r' Interrupt_State pragma set state to Runtime
1199 -- 's' Interrupt_State pragma set state to System (use "default"
1200 -- system handler)
1202 begin
1203 Environment_Task_Id := Environment_Task;
1205 Interrupt_Management.Initialize;
1207 -- Initialize the lock used to synchronize chain of all ATCBs
1209 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1211 Specific.Initialize (Environment_Task);
1213 -- Make environment task known here because it doesn't go through
1214 -- Activate_Tasks, which does it for all other tasks.
1216 Known_Tasks (Known_Tasks'First) := Environment_Task;
1217 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1219 Enter_Task (Environment_Task);
1221 -- Install the abort-signal handler
1223 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1224 /= Default
1225 then
1226 act.sa_flags := 0;
1227 act.sa_handler := Abort_Handler'Address;
1229 Result := sigemptyset (Tmp_Set'Access);
1230 pragma Assert (Result = 0);
1231 act.sa_mask := Tmp_Set;
1233 Result :=
1234 sigaction (
1235 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1236 act'Unchecked_Access,
1237 old_act'Unchecked_Access);
1238 pragma Assert (Result = 0);
1239 end if;
1240 end Initialize;
1242 -- NOTE: Unlike other pthread implementations, we do *not* mask all
1243 -- signals here since we handle signals using the process-wide primitive
1244 -- signal, rather than using sigthreadmask and sigwait. The reason of
1245 -- this difference is that sigwait doesn't work when some critical
1246 -- signals (SIGABRT, SIGPIPE) are masked.
1248 end System.Task_Primitives.Operations;