PR c++/29733
[official-gcc.git] / gcc / ada / s-taprop-hpux-dce.adb
blob4aefcda25bce064294d6db26797a32dc4d8e8c83
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a HP-UX DCE threads (HPUX 10) 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 System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.Interrupt_Management;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
49 -- Interrupt_ID
51 pragma Warnings (Off);
52 with System.Interrupt_Management.Operations;
53 -- used for Set_Interrupt_Mask
54 -- All_Tasks_Mask
55 pragma Elaborate_All (System.Interrupt_Management.Operations);
57 pragma Warnings (On);
59 with System.OS_Primitives;
60 -- used for Delay_Modes
62 with Interfaces.C;
63 -- used for int
64 -- size_t
66 with System.Task_Primitives.Interrupt_Operations;
67 -- used for Get_Interrupt_ID
69 with System.Soft_Links;
70 -- used for Defer/Undefer_Abort
72 -- We use System.Soft_Links instead of System.Tasking.Initialization
73 -- because the later is a higher level package that we shouldn't depend on.
74 -- For example when using the restricted run time, it is replaced by
75 -- System.Tasking.Restricted.Stages.
77 with Unchecked_Conversion;
78 with Unchecked_Deallocation;
80 package body System.Task_Primitives.Operations is
82 package SSL renames System.Soft_Links;
84 use System.Tasking.Debug;
85 use System.Tasking;
86 use Interfaces.C;
87 use System.OS_Interface;
88 use System.Parameters;
89 use System.OS_Primitives;
91 package PIO renames System.Task_Primitives.Interrupt_Operations;
93 ----------------
94 -- Local Data --
95 ----------------
97 -- The followings are logically constants, but need to be initialized
98 -- at run time.
100 Single_RTS_Lock : aliased RTS_Lock;
101 -- This is a lock to allow only one thread of control in the RTS at
102 -- a time; it is used to execute in mutual exclusion from all other tasks.
103 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
105 ATCB_Key : aliased pthread_key_t;
106 -- Key used to find the Ada Task_Id associated with a thread
108 Environment_Task_Id : Task_Id;
109 -- A variable to hold Task_Id for the environment task
111 Unblocked_Signal_Mask : aliased sigset_t;
112 -- The set of signals that should unblocked in all tasks
114 Time_Slice_Val : Integer;
115 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
117 Dispatching_Policy : Character;
118 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
120 -- Note: the reason that Locking_Policy is not needed is that this
121 -- is not implemented for DCE threads. The HPUX 10 port is at this
122 -- stage considered dead, and no further work is planned on it.
124 Foreign_Task_Elaborated : aliased Boolean := True;
125 -- Used to identified fake tasks (i.e., non-Ada Threads)
127 --------------------
128 -- Local Packages --
129 --------------------
131 package Specific is
133 procedure Initialize (Environment_Task : Task_Id);
134 pragma Inline (Initialize);
135 -- Initialize various data needed by this package
137 function Is_Valid_Task return Boolean;
138 pragma Inline (Is_Valid_Task);
139 -- Does the executing thread have a TCB?
141 procedure Set (Self_Id : Task_Id);
142 pragma Inline (Set);
143 -- Set the self id for the current task
145 function Self return Task_Id;
146 pragma Inline (Self);
147 -- Return a pointer to the Ada Task Control Block of the calling task
149 end Specific;
151 package body Specific is separate;
152 -- The body of this package is target specific
154 ---------------------------------
155 -- Support for foreign threads --
156 ---------------------------------
158 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
159 -- Allocate and Initialize a new ATCB for the current Thread
161 function Register_Foreign_Thread
162 (Thread : Thread_Id) return Task_Id is separate;
164 -----------------------
165 -- Local Subprograms --
166 -----------------------
168 procedure Abort_Handler (Sig : Signal);
170 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
172 -------------------
173 -- Abort_Handler --
174 -------------------
176 procedure Abort_Handler (Sig : Signal) is
177 pragma Unreferenced (Sig);
179 Self_Id : constant Task_Id := Self;
180 Result : Interfaces.C.int;
181 Old_Set : aliased sigset_t;
183 begin
184 if Self_Id.Deferral_Level = 0
185 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level and then
186 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 -- Stack_Guard --
202 -----------------
204 -- The underlying thread system sets a guard page at the
205 -- bottom of a thread stack, so nothing is needed.
206 -- ??? Check the comment above
208 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
209 pragma Unreferenced (T, On);
210 begin
211 null;
212 end Stack_Guard;
214 -------------------
215 -- Get_Thread_Id --
216 -------------------
218 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
219 begin
220 return T.Common.LL.Thread;
221 end Get_Thread_Id;
223 ----------
224 -- Self --
225 ----------
227 function Self return Task_Id renames Specific.Self;
229 ---------------------
230 -- Initialize_Lock --
231 ---------------------
233 -- Note: mutexes and cond_variables needed per-task basis are
234 -- initialized in Initialize_TCB and the Storage_Error is
235 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
236 -- used in RTS is initialized before any status change of RTS.
237 -- Therefore rasing Storage_Error in the following routines
238 -- should be able to be handled safely.
240 procedure Initialize_Lock
241 (Prio : System.Any_Priority;
242 L : access Lock)
244 Attributes : aliased pthread_mutexattr_t;
245 Result : Interfaces.C.int;
247 begin
248 Result := pthread_mutexattr_init (Attributes'Access);
249 pragma Assert (Result = 0 or else Result = ENOMEM);
251 if Result = ENOMEM then
252 raise Storage_Error;
253 end if;
255 L.Priority := Prio;
257 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
258 pragma Assert (Result = 0 or else Result = ENOMEM);
260 if Result = ENOMEM then
261 raise Storage_Error;
262 end if;
264 Result := pthread_mutexattr_destroy (Attributes'Access);
265 pragma Assert (Result = 0);
266 end Initialize_Lock;
268 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
269 pragma Unreferenced (Level);
271 Attributes : aliased pthread_mutexattr_t;
272 Result : Interfaces.C.int;
274 begin
275 Result := pthread_mutexattr_init (Attributes'Access);
276 pragma Assert (Result = 0 or else Result = ENOMEM);
278 if Result = ENOMEM then
279 raise Storage_Error;
280 end if;
282 Result := pthread_mutex_init (L, Attributes'Access);
284 pragma Assert (Result = 0 or else Result = ENOMEM);
286 if Result = ENOMEM then
287 raise Storage_Error;
288 end if;
290 Result := pthread_mutexattr_destroy (Attributes'Access);
291 pragma Assert (Result = 0);
292 end Initialize_Lock;
294 -------------------
295 -- Finalize_Lock --
296 -------------------
298 procedure Finalize_Lock (L : access Lock) is
299 Result : Interfaces.C.int;
300 begin
301 Result := pthread_mutex_destroy (L.L'Access);
302 pragma Assert (Result = 0);
303 end Finalize_Lock;
305 procedure Finalize_Lock (L : access RTS_Lock) is
306 Result : Interfaces.C.int;
307 begin
308 Result := pthread_mutex_destroy (L);
309 pragma Assert (Result = 0);
310 end Finalize_Lock;
312 ----------------
313 -- Write_Lock --
314 ----------------
316 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
317 Result : Interfaces.C.int;
319 begin
320 L.Owner_Priority := Get_Priority (Self);
322 if L.Priority < L.Owner_Priority then
323 Ceiling_Violation := True;
324 return;
325 end if;
327 Result := pthread_mutex_lock (L.L'Access);
328 pragma Assert (Result = 0);
329 Ceiling_Violation := False;
330 end Write_Lock;
332 procedure Write_Lock
333 (L : access RTS_Lock; Global_Lock : Boolean := False)
335 Result : Interfaces.C.int;
336 begin
337 if not Single_Lock or else Global_Lock then
338 Result := pthread_mutex_lock (L);
339 pragma Assert (Result = 0);
340 end if;
341 end Write_Lock;
343 procedure Write_Lock (T : Task_Id) is
344 Result : Interfaces.C.int;
345 begin
346 if not Single_Lock then
347 Result := pthread_mutex_lock (T.Common.LL.L'Access);
348 pragma Assert (Result = 0);
349 end if;
350 end Write_Lock;
352 ---------------
353 -- Read_Lock --
354 ---------------
356 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
357 begin
358 Write_Lock (L, Ceiling_Violation);
359 end Read_Lock;
361 ------------
362 -- Unlock --
363 ------------
365 procedure Unlock (L : access Lock) is
366 Result : Interfaces.C.int;
367 begin
368 Result := pthread_mutex_unlock (L.L'Access);
369 pragma Assert (Result = 0);
370 end Unlock;
372 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
373 Result : Interfaces.C.int;
374 begin
375 if not Single_Lock or else Global_Lock then
376 Result := pthread_mutex_unlock (L);
377 pragma Assert (Result = 0);
378 end if;
379 end Unlock;
381 procedure Unlock (T : Task_Id) is
382 Result : Interfaces.C.int;
383 begin
384 if not Single_Lock then
385 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
386 pragma Assert (Result = 0);
387 end if;
388 end Unlock;
390 -----------
391 -- Sleep --
392 -----------
394 procedure Sleep
395 (Self_ID : Task_Id;
396 Reason : System.Tasking.Task_States)
398 pragma Unreferenced (Reason);
400 Result : Interfaces.C.int;
401 begin
402 if Single_Lock then
403 Result := pthread_cond_wait
404 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
405 else
406 Result := pthread_cond_wait
407 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
408 end if;
410 -- EINTR is not considered a failure
412 pragma Assert (Result = 0 or else Result = EINTR);
413 end Sleep;
415 -----------------
416 -- Timed_Sleep --
417 -----------------
419 procedure Timed_Sleep
420 (Self_ID : Task_Id;
421 Time : Duration;
422 Mode : ST.Delay_Modes;
423 Reason : System.Tasking.Task_States;
424 Timedout : out Boolean;
425 Yielded : out Boolean)
427 pragma Unreferenced (Reason);
429 Check_Time : constant Duration := Monotonic_Clock;
430 Abs_Time : Duration;
431 Request : aliased timespec;
432 Result : Interfaces.C.int;
434 begin
435 Timedout := True;
436 Yielded := False;
438 if Mode = Relative then
439 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
440 else
441 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
442 end if;
444 if Abs_Time > Check_Time then
445 Request := To_Timespec (Abs_Time);
447 loop
448 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
449 or else Self_ID.Pending_Priority_Change;
451 if Single_Lock then
452 Result := pthread_cond_timedwait
453 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
454 Request'Access);
456 else
457 Result := pthread_cond_timedwait
458 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
459 Request'Access);
460 end if;
462 exit when Abs_Time <= Monotonic_Clock;
464 if Result = 0 or Result = EINTR then
466 -- Somebody may have called Wakeup for us
468 Timedout := False;
469 exit;
470 end if;
472 pragma Assert (Result = ETIMEDOUT);
473 end loop;
474 end if;
475 end Timed_Sleep;
477 -----------------
478 -- Timed_Delay --
479 -----------------
481 procedure Timed_Delay
482 (Self_ID : Task_Id;
483 Time : Duration;
484 Mode : ST.Delay_Modes)
486 Check_Time : constant Duration := Monotonic_Clock;
487 Abs_Time : Duration;
488 Request : aliased timespec;
489 Result : Interfaces.C.int;
491 begin
492 if Single_Lock then
493 Lock_RTS;
494 end if;
496 Write_Lock (Self_ID);
498 if Mode = Relative then
499 Abs_Time := Time + Check_Time;
500 else
501 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
502 end if;
504 if Abs_Time > Check_Time then
505 Request := To_Timespec (Abs_Time);
506 Self_ID.Common.State := Delay_Sleep;
508 loop
509 if Self_ID.Pending_Priority_Change then
510 Self_ID.Pending_Priority_Change := False;
511 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
512 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
513 end if;
515 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
517 if Single_Lock then
518 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
519 Single_RTS_Lock'Access, Request'Access);
520 else
521 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
522 Self_ID.Common.LL.L'Access, Request'Access);
523 end if;
525 exit when Abs_Time <= Monotonic_Clock;
527 pragma Assert (Result = 0 or else
528 Result = ETIMEDOUT or else
529 Result = EINTR);
530 end loop;
532 Self_ID.Common.State := Runnable;
533 end if;
535 Unlock (Self_ID);
537 if Single_Lock then
538 Unlock_RTS;
539 end if;
541 Result := sched_yield;
542 end Timed_Delay;
544 ---------------------
545 -- Monotonic_Clock --
546 ---------------------
548 function Monotonic_Clock return Duration is
549 TS : aliased timespec;
550 Result : Interfaces.C.int;
551 begin
552 Result := Clock_Gettime (CLOCK_REALTIME, TS'Unchecked_Access);
553 pragma Assert (Result = 0);
554 return To_Duration (TS);
555 end Monotonic_Clock;
557 -------------------
558 -- RT_Resolution --
559 -------------------
561 function RT_Resolution return Duration is
562 begin
563 return 10#1.0#E-6;
564 end RT_Resolution;
566 ------------
567 -- Wakeup --
568 ------------
570 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
571 pragma Unreferenced (Reason);
573 Result : Interfaces.C.int;
575 begin
576 Result := pthread_cond_signal (T.Common.LL.CV'Access);
577 pragma Assert (Result = 0);
578 end Wakeup;
580 -----------
581 -- Yield --
582 -----------
584 procedure Yield (Do_Yield : Boolean := True) is
585 Result : Interfaces.C.int;
586 pragma Unreferenced (Result);
587 begin
588 if Do_Yield then
589 Result := sched_yield;
590 end if;
591 end Yield;
593 ------------------
594 -- Set_Priority --
595 ------------------
597 type Prio_Array_Type is array (System.Any_Priority) of Integer;
598 pragma Atomic_Components (Prio_Array_Type);
600 Prio_Array : Prio_Array_Type;
601 -- Global array containing the id of the currently running task for
602 -- each priority.
604 -- Note: we assume that we are on a single processor with run-til-blocked
605 -- 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 begin
617 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
619 if Time_Slice_Val > 0 then
620 Result := pthread_setschedparam
621 (T.Common.LL.Thread, SCHED_RR, Param'Access);
623 elsif Dispatching_Policy = 'F' or else Time_Slice_Val = 0 then
624 Result := pthread_setschedparam
625 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
627 else
628 Result := pthread_setschedparam
629 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
630 end if;
632 pragma Assert (Result = 0);
634 if Dispatching_Policy = 'F' then
636 -- Annex D requirement [RM D.2.2 par. 9]:
637 -- If the task drops its priority due to the loss of inherited
638 -- priority, it is added at the head of the ready queue for its
639 -- new active priority.
641 if Loss_Of_Inheritance
642 and then Prio < T.Common.Current_Priority
643 then
644 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
645 Prio_Array (T.Common.Base_Priority) := Array_Item;
647 loop
648 -- Let some processes a chance to arrive
650 Yield;
652 -- Then wait for our turn to proceed
654 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
655 or else Prio_Array (T.Common.Base_Priority) = 1;
656 end loop;
658 Prio_Array (T.Common.Base_Priority) :=
659 Prio_Array (T.Common.Base_Priority) - 1;
660 end if;
661 end if;
663 T.Common.Current_Priority := Prio;
664 end Set_Priority;
666 ------------------
667 -- Get_Priority --
668 ------------------
670 function Get_Priority (T : Task_Id) return System.Any_Priority is
671 begin
672 return T.Common.Current_Priority;
673 end Get_Priority;
675 ----------------
676 -- Enter_Task --
677 ----------------
679 procedure Enter_Task (Self_ID : Task_Id) is
680 begin
681 Self_ID.Common.LL.Thread := pthread_self;
682 Specific.Set (Self_ID);
684 Lock_RTS;
686 for J in Known_Tasks'Range loop
687 if Known_Tasks (J) = null then
688 Known_Tasks (J) := Self_ID;
689 Self_ID.Known_Tasks_Index := J;
690 exit;
691 end if;
692 end loop;
694 Unlock_RTS;
695 end Enter_Task;
697 --------------
698 -- New_ATCB --
699 --------------
701 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
702 begin
703 return new Ada_Task_Control_Block (Entry_Num);
704 end New_ATCB;
706 -------------------
707 -- Is_Valid_Task --
708 -------------------
710 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
712 -----------------------------
713 -- Register_Foreign_Thread --
714 -----------------------------
716 function Register_Foreign_Thread return Task_Id is
717 begin
718 if Is_Valid_Task then
719 return Self;
720 else
721 return Register_Foreign_Thread (pthread_self);
722 end if;
723 end Register_Foreign_Thread;
725 --------------------
726 -- Initialize_TCB --
727 --------------------
729 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
730 Mutex_Attr : aliased pthread_mutexattr_t;
731 Result : Interfaces.C.int;
732 Cond_Attr : aliased pthread_condattr_t;
734 begin
735 if not Single_Lock then
736 Result := pthread_mutexattr_init (Mutex_Attr'Access);
737 pragma Assert (Result = 0 or else Result = ENOMEM);
739 if Result = 0 then
740 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
741 Mutex_Attr'Access);
742 pragma Assert (Result = 0 or else Result = ENOMEM);
743 end if;
745 if Result /= 0 then
746 Succeeded := False;
747 return;
748 end if;
750 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
751 pragma Assert (Result = 0);
752 end if;
754 Result := pthread_condattr_init (Cond_Attr'Access);
755 pragma Assert (Result = 0 or else Result = ENOMEM);
757 if Result = 0 then
758 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
759 Cond_Attr'Access);
760 pragma Assert (Result = 0 or else Result = ENOMEM);
761 end if;
763 if Result = 0 then
764 Succeeded := True;
765 else
766 if not Single_Lock then
767 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
768 pragma Assert (Result = 0);
769 end if;
771 Succeeded := False;
772 end if;
774 Result := pthread_condattr_destroy (Cond_Attr'Access);
775 pragma Assert (Result = 0);
776 end Initialize_TCB;
778 -----------------
779 -- Create_Task --
780 -----------------
782 procedure Create_Task
783 (T : Task_Id;
784 Wrapper : System.Address;
785 Stack_Size : System.Parameters.Size_Type;
786 Priority : System.Any_Priority;
787 Succeeded : out Boolean)
789 Attributes : aliased pthread_attr_t;
790 Result : Interfaces.C.int;
792 function Thread_Body_Access is new
793 Unchecked_Conversion (System.Address, Thread_Body);
795 begin
796 Result := pthread_attr_init (Attributes'Access);
797 pragma Assert (Result = 0 or else Result = ENOMEM);
799 if Result /= 0 then
800 Succeeded := False;
801 return;
802 end if;
804 Result := pthread_attr_setstacksize
805 (Attributes'Access, Interfaces.C.size_t (Stack_Size));
806 pragma Assert (Result = 0);
808 -- Since the initial signal mask of a thread is inherited from the
809 -- creator, and the Environment task has all its signals masked, we
810 -- do not need to manipulate caller's signal mask at this point.
811 -- All tasks in RTS will have All_Tasks_Mask initially.
813 Result := pthread_create
814 (T.Common.LL.Thread'Access,
815 Attributes'Access,
816 Thread_Body_Access (Wrapper),
817 To_Address (T));
818 pragma Assert (Result = 0 or else Result = EAGAIN);
820 Succeeded := Result = 0;
822 pthread_detach (T.Common.LL.Thread'Access);
823 -- Detach the thread using pthread_detach, sinc DCE threads do not have
824 -- pthread_attr_set_detachstate.
826 Result := pthread_attr_destroy (Attributes'Access);
827 pragma Assert (Result = 0);
829 Set_Priority (T, Priority);
830 end Create_Task;
832 ------------------
833 -- Finalize_TCB --
834 ------------------
836 procedure Finalize_TCB (T : Task_Id) is
837 Result : Interfaces.C.int;
838 Tmp : Task_Id := T;
839 Is_Self : constant Boolean := T = Self;
841 procedure Free is new
842 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
844 begin
845 if not Single_Lock then
846 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
847 pragma Assert (Result = 0);
848 end if;
850 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
851 pragma Assert (Result = 0);
853 if T.Known_Tasks_Index /= -1 then
854 Known_Tasks (T.Known_Tasks_Index) := null;
855 end if;
857 Free (Tmp);
859 if Is_Self then
860 Specific.Set (null);
861 end if;
862 end Finalize_TCB;
864 ---------------
865 -- Exit_Task --
866 ---------------
868 procedure Exit_Task is
869 begin
870 Specific.Set (null);
871 end Exit_Task;
873 ----------------
874 -- Abort_Task --
875 ----------------
877 procedure Abort_Task (T : Task_Id) is
878 begin
880 -- Interrupt Server_Tasks may be waiting on an "event" flag (signal)
882 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
883 System.Interrupt_Management.Operations.Interrupt_Self_Process
884 (System.Interrupt_Management.Interrupt_ID
885 (PIO.Get_Interrupt_ID (T)));
886 end if;
887 end Abort_Task;
889 ----------------
890 -- Initialize --
891 ----------------
893 procedure Initialize (S : in out Suspension_Object) is
894 Mutex_Attr : aliased pthread_mutexattr_t;
895 Cond_Attr : aliased pthread_condattr_t;
896 Result : Interfaces.C.int;
897 begin
898 -- Initialize internal state. It is always initialized to False (ARM
899 -- D.10 par. 6).
901 S.State := False;
902 S.Waiting := False;
904 -- Initialize internal mutex
906 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
907 pragma Assert (Result = 0 or else Result = ENOMEM);
909 if Result = ENOMEM then
910 raise Storage_Error;
911 end if;
913 -- Initialize internal condition variable
915 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
916 pragma Assert (Result = 0 or else Result = ENOMEM);
918 if Result /= 0 then
919 Result := pthread_mutex_destroy (S.L'Access);
920 pragma Assert (Result = 0);
922 if Result = ENOMEM then
923 raise Storage_Error;
924 end if;
925 end if;
926 end Initialize;
928 --------------
929 -- Finalize --
930 --------------
932 procedure Finalize (S : in out Suspension_Object) is
933 Result : Interfaces.C.int;
934 begin
935 -- Destroy internal mutex
937 Result := pthread_mutex_destroy (S.L'Access);
938 pragma Assert (Result = 0);
940 -- Destroy internal condition variable
942 Result := pthread_cond_destroy (S.CV'Access);
943 pragma Assert (Result = 0);
944 end Finalize;
946 -------------------
947 -- Current_State --
948 -------------------
950 function Current_State (S : Suspension_Object) return Boolean is
951 begin
952 -- We do not want to use lock on this read operation. State is marked
953 -- as Atomic so that we ensure that the value retrieved is correct.
955 return S.State;
956 end Current_State;
958 ---------------
959 -- Set_False --
960 ---------------
962 procedure Set_False (S : in out Suspension_Object) is
963 Result : Interfaces.C.int;
964 begin
965 SSL.Abort_Defer.all;
967 Result := pthread_mutex_lock (S.L'Access);
968 pragma Assert (Result = 0);
970 S.State := False;
972 Result := pthread_mutex_unlock (S.L'Access);
973 pragma Assert (Result = 0);
975 SSL.Abort_Undefer.all;
976 end Set_False;
978 --------------
979 -- Set_True --
980 --------------
982 procedure Set_True (S : in out Suspension_Object) is
983 Result : Interfaces.C.int;
984 begin
985 SSL.Abort_Defer.all;
987 Result := pthread_mutex_lock (S.L'Access);
988 pragma Assert (Result = 0);
990 -- If there is already a task waiting on this suspension object then
991 -- we resume it, leaving the state of the suspension object to False,
992 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
993 -- the state to True.
995 if S.Waiting then
996 S.Waiting := False;
997 S.State := False;
999 Result := pthread_cond_signal (S.CV'Access);
1000 pragma Assert (Result = 0);
1001 else
1002 S.State := True;
1003 end if;
1005 Result := pthread_mutex_unlock (S.L'Access);
1006 pragma Assert (Result = 0);
1008 SSL.Abort_Undefer.all;
1009 end Set_True;
1011 ------------------------
1012 -- Suspend_Until_True --
1013 ------------------------
1015 procedure Suspend_Until_True (S : in out Suspension_Object) is
1016 Result : Interfaces.C.int;
1017 begin
1018 SSL.Abort_Defer.all;
1020 Result := pthread_mutex_lock (S.L'Access);
1021 pragma Assert (Result = 0);
1023 if S.Waiting then
1024 -- Program_Error must be raised upon calling Suspend_Until_True
1025 -- if another task is already waiting on that suspension object
1026 -- (ARM D.10 par. 10).
1028 Result := pthread_mutex_unlock (S.L'Access);
1029 pragma Assert (Result = 0);
1031 SSL.Abort_Undefer.all;
1033 raise Program_Error;
1034 else
1035 -- Suspend the task if the state is False. Otherwise, the task
1036 -- continues its execution, and the state of the suspension object
1037 -- is set to False (ARM D.10 par. 9).
1039 if S.State then
1040 S.State := False;
1041 else
1042 S.Waiting := True;
1043 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1044 end if;
1046 Result := pthread_mutex_unlock (S.L'Access);
1047 pragma Assert (Result = 0);
1049 SSL.Abort_Undefer.all;
1050 end if;
1051 end Suspend_Until_True;
1053 ----------------
1054 -- Check_Exit --
1055 ----------------
1057 -- Dummy version
1059 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1060 pragma Unreferenced (Self_ID);
1061 begin
1062 return True;
1063 end Check_Exit;
1065 --------------------
1066 -- Check_No_Locks --
1067 --------------------
1069 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1070 pragma Unreferenced (Self_ID);
1071 begin
1072 return True;
1073 end Check_No_Locks;
1075 ----------------------
1076 -- Environment_Task --
1077 ----------------------
1079 function Environment_Task return Task_Id is
1080 begin
1081 return Environment_Task_Id;
1082 end Environment_Task;
1084 --------------
1085 -- Lock_RTS --
1086 --------------
1088 procedure Lock_RTS is
1089 begin
1090 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1091 end Lock_RTS;
1093 ----------------
1094 -- Unlock_RTS --
1095 ----------------
1097 procedure Unlock_RTS is
1098 begin
1099 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1100 end Unlock_RTS;
1102 ------------------
1103 -- Suspend_Task --
1104 ------------------
1106 function Suspend_Task
1107 (T : ST.Task_Id;
1108 Thread_Self : Thread_Id) return Boolean
1110 pragma Unreferenced (T);
1111 pragma Unreferenced (Thread_Self);
1112 begin
1113 return False;
1114 end Suspend_Task;
1116 -----------------
1117 -- Resume_Task --
1118 -----------------
1120 function Resume_Task
1121 (T : ST.Task_Id;
1122 Thread_Self : Thread_Id) return Boolean
1124 pragma Unreferenced (T);
1125 pragma Unreferenced (Thread_Self);
1126 begin
1127 return False;
1128 end Resume_Task;
1130 ----------------
1131 -- Initialize --
1132 ----------------
1134 procedure Initialize (Environment_Task : Task_Id) is
1135 act : aliased struct_sigaction;
1136 old_act : aliased struct_sigaction;
1137 Tmp_Set : aliased sigset_t;
1138 Result : Interfaces.C.int;
1140 function State
1141 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1142 pragma Import (C, State, "__gnat_get_interrupt_state");
1143 -- Get interrupt state. Defined in a-init.c. The input argument is
1144 -- the interrupt number, and the result is one of the following:
1146 Default : constant Character := 's';
1147 -- 'n' this interrupt not set by any Interrupt_State pragma
1148 -- 'u' Interrupt_State pragma set state to User
1149 -- 'r' Interrupt_State pragma set state to Runtime
1150 -- 's' Interrupt_State pragma set state to System (use "default"
1151 -- system handler)
1153 begin
1154 Environment_Task_Id := Environment_Task;
1156 Interrupt_Management.Initialize;
1158 -- Initialize the lock used to synchronize chain of all ATCBs
1160 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1162 Specific.Initialize (Environment_Task);
1164 Enter_Task (Environment_Task);
1166 -- Install the abort-signal handler
1168 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1169 /= Default
1170 then
1171 act.sa_flags := 0;
1172 act.sa_handler := Abort_Handler'Address;
1174 Result := sigemptyset (Tmp_Set'Access);
1175 pragma Assert (Result = 0);
1176 act.sa_mask := Tmp_Set;
1178 Result :=
1179 sigaction (
1180 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1181 act'Unchecked_Access,
1182 old_act'Unchecked_Access);
1183 pragma Assert (Result = 0);
1184 end if;
1185 end Initialize;
1187 -- NOTE: Unlike other pthread implementations, we do *not* mask all
1188 -- signals here since we handle signals using the process-wide primitive
1189 -- signal, rather than using sigthreadmask and sigwait. The reason of
1190 -- this difference is that sigwait doesn't work when some critical
1191 -- signals (SIGABRT, SIGPIPE) are masked.
1193 end System.Task_Primitives.Operations;