2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / 5htaprop.adb
blob434806c426efc76a455514cadf69e07aeb712fa8
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-2003, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Interfaces.C;
47 -- used for int
48 -- size_t
50 with System.Interrupt_Management;
51 -- used for Keep_Unmasked
52 -- Abort_Task_Interrupt
53 -- Interrupt_ID
55 with System.Interrupt_Management.Operations;
56 -- used for Set_Interrupt_Mask
57 -- All_Tasks_Mask
58 pragma Elaborate_All (System.Interrupt_Management.Operations);
60 with System.Parameters;
61 -- used for Size_Type
63 with System.Task_Primitives.Interrupt_Operations;
64 -- used for Get_Interrupt_ID
66 with System.Tasking;
67 -- used for Ada_Task_Control_Block
68 -- Task_ID
70 with System.Soft_Links;
71 -- used for Defer/Undefer_Abort
73 -- Note that we do not use System.Tasking.Initialization directly since
74 -- this is a higher level package that we shouldn't depend on. For example
75 -- when using the restricted run time, it is replaced by
76 -- System.Tasking.Restricted.Initialization
78 with System.OS_Primitives;
79 -- used for Delay_Modes
81 with Unchecked_Conversion;
82 with Unchecked_Deallocation;
84 package body System.Task_Primitives.Operations is
86 use System.Tasking.Debug;
87 use System.Tasking;
88 use Interfaces.C;
89 use System.OS_Interface;
90 use System.Parameters;
91 use System.OS_Primitives;
93 package PIO renames System.Task_Primitives.Interrupt_Operations;
94 package SSL renames System.Soft_Links;
96 ------------------
97 -- Local Data --
98 ------------------
100 -- The followings are logically constants, but need to be initialized
101 -- at run time.
103 Single_RTS_Lock : aliased RTS_Lock;
104 -- This is a lock to allow only one thread of control in the RTS at
105 -- a time; it is used to execute in mutual exclusion from all other tasks.
106 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
108 ATCB_Key : aliased pthread_key_t;
109 -- Key used to find the Ada Task_ID associated with a thread
111 Environment_Task_ID : Task_ID;
112 -- A variable to hold Task_ID for the environment task.
114 Unblocked_Signal_Mask : aliased sigset_t;
115 -- The set of signals that should unblocked in all tasks
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 -- Note: the reason that Locking_Policy is not needed is that this
124 -- is not implemented for DCE threads. The HPUX 10 port is at this
125 -- stage considered dead, and no further work is planned on it.
127 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
128 -- Indicates whether FIFO_Within_Priorities is set.
130 Foreign_Task_Elaborated : aliased Boolean := True;
131 -- Used to identified fake tasks (i.e., non-Ada Threads).
133 --------------------
134 -- Local Packages --
135 --------------------
137 package Specific is
139 procedure Initialize (Environment_Task : Task_ID);
140 pragma Inline (Initialize);
141 -- Initialize various data needed by this package.
143 function Is_Valid_Task return Boolean;
144 pragma Inline (Is_Valid_Task);
145 -- Does the executing thread have a TCB?
147 procedure Set (Self_Id : Task_ID);
148 pragma Inline (Set);
149 -- Set the self id for the current task.
151 function Self return Task_ID;
152 pragma Inline (Self);
153 -- Return a pointer to the Ada Task Control Block of the calling task.
155 end Specific;
157 package body Specific is separate;
158 -- The body of this package is target specific.
160 ---------------------------------
161 -- Support for foreign threads --
162 ---------------------------------
164 function Register_Foreign_Thread (Thread : Thread_Id) return Task_ID;
165 -- Allocate and Initialize a new ATCB for the current Thread.
167 function Register_Foreign_Thread
168 (Thread : Thread_Id) return Task_ID is separate;
170 -----------------------
171 -- Local Subprograms --
172 -----------------------
174 procedure Abort_Handler (Sig : Signal);
176 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
178 -------------------
179 -- Abort_Handler --
180 -------------------
182 procedure Abort_Handler (Sig : Signal) is
183 pragma Unreferenced (Sig);
185 Self_Id : constant Task_ID := Self;
186 Result : Interfaces.C.int;
187 Old_Set : aliased sigset_t;
189 begin
190 if Self_Id.Deferral_Level = 0
191 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level and then
192 not Self_Id.Aborting
193 then
194 Self_Id.Aborting := True;
196 -- Make sure signals used for RTS internal purpose are unmasked
198 Result := pthread_sigmask (SIG_UNBLOCK,
199 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
200 pragma Assert (Result = 0);
202 raise Standard'Abort_Signal;
203 end if;
204 end Abort_Handler;
206 -----------------
207 -- Stack_Guard --
208 -----------------
210 -- The underlying thread system sets a guard page at the
211 -- bottom of a thread stack, so nothing is needed.
212 -- ??? Check the comment above
214 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
215 pragma Unreferenced (T, On);
216 begin
217 null;
218 end Stack_Guard;
220 -------------------
221 -- Get_Thread_Id --
222 -------------------
224 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
225 begin
226 return T.Common.LL.Thread;
227 end Get_Thread_Id;
229 ----------
230 -- Self --
231 ----------
233 function Self return Task_ID renames Specific.Self;
235 ---------------------
236 -- Initialize_Lock --
237 ---------------------
239 -- Note: mutexes and cond_variables needed per-task basis are
240 -- initialized in Initialize_TCB and the Storage_Error is
241 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
242 -- used in RTS is initialized before any status change of RTS.
243 -- Therefore rasing Storage_Error in the following routines
244 -- should be able to be handled safely.
246 procedure Initialize_Lock
247 (Prio : System.Any_Priority;
248 L : access Lock)
250 Attributes : aliased pthread_mutexattr_t;
251 Result : Interfaces.C.int;
253 begin
254 Result := pthread_mutexattr_init (Attributes'Access);
255 pragma Assert (Result = 0 or else Result = ENOMEM);
257 if Result = ENOMEM then
258 raise Storage_Error;
259 end if;
261 L.Priority := Prio;
263 Result := pthread_mutex_init (L.L'Access, 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_mutexattr_destroy (Attributes'Access);
271 pragma Assert (Result = 0);
272 end Initialize_Lock;
274 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
275 pragma Unreferenced (Level);
277 Attributes : aliased pthread_mutexattr_t;
278 Result : Interfaces.C.int;
280 begin
281 Result := pthread_mutexattr_init (Attributes'Access);
282 pragma Assert (Result = 0 or else Result = ENOMEM);
284 if Result = ENOMEM then
285 raise Storage_Error;
286 end if;
288 Result := pthread_mutex_init (L, Attributes'Access);
290 pragma Assert (Result = 0 or else Result = ENOMEM);
292 if Result = ENOMEM then
293 raise Storage_Error;
294 end if;
296 Result := pthread_mutexattr_destroy (Attributes'Access);
297 pragma Assert (Result = 0);
298 end Initialize_Lock;
300 -------------------
301 -- Finalize_Lock --
302 -------------------
304 procedure Finalize_Lock (L : access Lock) is
305 Result : Interfaces.C.int;
306 begin
307 Result := pthread_mutex_destroy (L.L'Access);
308 pragma Assert (Result = 0);
309 end Finalize_Lock;
311 procedure Finalize_Lock (L : access RTS_Lock) is
312 Result : Interfaces.C.int;
313 begin
314 Result := pthread_mutex_destroy (L);
315 pragma Assert (Result = 0);
316 end Finalize_Lock;
318 ----------------
319 -- Write_Lock --
320 ----------------
322 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
323 Result : Interfaces.C.int;
325 begin
326 L.Owner_Priority := Get_Priority (Self);
328 if L.Priority < L.Owner_Priority then
329 Ceiling_Violation := True;
330 return;
331 end if;
333 Result := pthread_mutex_lock (L.L'Access);
334 pragma Assert (Result = 0);
335 Ceiling_Violation := False;
336 end Write_Lock;
338 procedure Write_Lock
339 (L : access RTS_Lock; Global_Lock : Boolean := False)
341 Result : Interfaces.C.int;
343 begin
344 if not Single_Lock or else Global_Lock then
345 Result := pthread_mutex_lock (L);
346 pragma Assert (Result = 0);
347 end if;
348 end Write_Lock;
350 procedure Write_Lock (T : Task_ID) is
351 Result : Interfaces.C.int;
353 begin
354 if not Single_Lock then
355 Result := pthread_mutex_lock (T.Common.LL.L'Access);
356 pragma Assert (Result = 0);
357 end if;
358 end Write_Lock;
360 ---------------
361 -- Read_Lock --
362 ---------------
364 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
365 begin
366 Write_Lock (L, Ceiling_Violation);
367 end Read_Lock;
369 ------------
370 -- Unlock --
371 ------------
373 procedure Unlock (L : access Lock) is
374 Result : Interfaces.C.int;
376 begin
377 Result := pthread_mutex_unlock (L.L'Access);
378 pragma Assert (Result = 0);
379 end Unlock;
381 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
382 Result : Interfaces.C.int;
383 begin
384 if not Single_Lock or else Global_Lock then
385 Result := pthread_mutex_unlock (L);
386 pragma Assert (Result = 0);
387 end if;
388 end Unlock;
390 procedure Unlock (T : Task_ID) is
391 Result : Interfaces.C.int;
393 begin
394 if not Single_Lock then
395 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
396 pragma Assert (Result = 0);
397 end if;
398 end Unlock;
400 -----------
401 -- Sleep --
402 -----------
404 procedure Sleep
405 (Self_ID : Task_ID;
406 Reason : System.Tasking.Task_States)
408 pragma Unreferenced (Reason);
410 Result : Interfaces.C.int;
411 begin
412 if Single_Lock then
413 Result := pthread_cond_wait
414 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
415 else
416 Result := pthread_cond_wait
417 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
418 end if;
420 -- EINTR is not considered a failure.
421 pragma Assert (Result = 0 or else Result = EINTR);
422 end Sleep;
424 -----------------
425 -- Timed_Sleep --
426 -----------------
428 procedure Timed_Sleep
429 (Self_ID : Task_ID;
430 Time : Duration;
431 Mode : ST.Delay_Modes;
432 Reason : System.Tasking.Task_States;
433 Timedout : out Boolean;
434 Yielded : out Boolean)
436 pragma Unreferenced (Reason);
438 Check_Time : constant Duration := Monotonic_Clock;
439 Abs_Time : Duration;
440 Request : aliased timespec;
441 Result : Interfaces.C.int;
443 begin
444 Timedout := True;
445 Yielded := False;
447 if Mode = Relative then
448 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
449 else
450 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
451 end if;
453 if Abs_Time > Check_Time then
454 Request := To_Timespec (Abs_Time);
456 loop
457 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
458 or else Self_ID.Pending_Priority_Change;
460 if Single_Lock then
461 Result := pthread_cond_timedwait
462 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
463 Request'Access);
465 else
466 Result := pthread_cond_timedwait
467 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
468 Request'Access);
469 end if;
471 exit when Abs_Time <= Monotonic_Clock;
473 if Result = 0 or Result = EINTR then
475 -- Somebody may have called Wakeup for us
477 Timedout := False;
478 exit;
479 end if;
481 pragma Assert (Result = ETIMEDOUT);
482 end loop;
483 end if;
484 end Timed_Sleep;
486 -----------------
487 -- Timed_Delay --
488 -----------------
490 procedure Timed_Delay
491 (Self_ID : Task_ID;
492 Time : Duration;
493 Mode : ST.Delay_Modes)
495 Check_Time : constant Duration := Monotonic_Clock;
496 Abs_Time : Duration;
497 Request : aliased timespec;
498 Result : Interfaces.C.int;
500 begin
501 -- Only the little window between deferring abort and
502 -- locking Self_ID is the reason we need to
503 -- check for pending abort and priority change below! :(
505 SSL.Abort_Defer.all;
507 if Single_Lock then
508 Lock_RTS;
509 end if;
511 Write_Lock (Self_ID);
513 if Mode = Relative then
514 Abs_Time := Time + Check_Time;
515 else
516 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
517 end if;
519 if Abs_Time > Check_Time then
520 Request := To_Timespec (Abs_Time);
521 Self_ID.Common.State := Delay_Sleep;
523 loop
524 if Self_ID.Pending_Priority_Change then
525 Self_ID.Pending_Priority_Change := False;
526 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
527 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
528 end if;
530 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
532 if Single_Lock then
533 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
534 Single_RTS_Lock'Access, Request'Access);
535 else
536 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
537 Self_ID.Common.LL.L'Access, Request'Access);
538 end if;
540 exit when Abs_Time <= Monotonic_Clock;
542 pragma Assert (Result = 0 or else
543 Result = ETIMEDOUT or else
544 Result = EINTR);
545 end loop;
547 Self_ID.Common.State := Runnable;
548 end if;
550 Unlock (Self_ID);
552 if Single_Lock then
553 Unlock_RTS;
554 end if;
556 Result := sched_yield;
557 SSL.Abort_Undefer.all;
558 end Timed_Delay;
560 ---------------------
561 -- Monotonic_Clock --
562 ---------------------
564 function Monotonic_Clock return Duration is
565 TS : aliased timespec;
566 Result : Interfaces.C.int;
568 begin
569 Result := Clock_Gettime (CLOCK_REALTIME, TS'Unchecked_Access);
570 pragma Assert (Result = 0);
571 return To_Duration (TS);
572 end Monotonic_Clock;
574 -------------------
575 -- RT_Resolution --
576 -------------------
578 function RT_Resolution return Duration is
579 begin
580 return 10#1.0#E-6;
581 end RT_Resolution;
583 ------------
584 -- Wakeup --
585 ------------
587 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
588 pragma Unreferenced (Reason);
590 Result : Interfaces.C.int;
592 begin
593 Result := pthread_cond_signal (T.Common.LL.CV'Access);
594 pragma Assert (Result = 0);
595 end Wakeup;
597 -----------
598 -- Yield --
599 -----------
601 procedure Yield (Do_Yield : Boolean := True) is
602 Result : Interfaces.C.int;
604 begin
605 if Do_Yield then
606 Result := sched_yield;
607 end if;
608 end Yield;
610 ------------------
611 -- Set_Priority --
612 ------------------
614 type Prio_Array_Type is array (System.Any_Priority) of Integer;
615 pragma Atomic_Components (Prio_Array_Type);
617 Prio_Array : Prio_Array_Type;
618 -- Global array containing the id of the currently running task for
619 -- each priority.
621 -- Note: we assume that we are on a single processor with run-til-blocked
622 -- scheduling.
624 procedure Set_Priority
625 (T : Task_ID;
626 Prio : System.Any_Priority;
627 Loss_Of_Inheritance : Boolean := False)
629 Result : Interfaces.C.int;
630 Array_Item : Integer;
631 Param : aliased struct_sched_param;
633 begin
634 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
636 if Time_Slice_Val > 0 then
637 Result := pthread_setschedparam
638 (T.Common.LL.Thread, SCHED_RR, Param'Access);
640 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
641 Result := pthread_setschedparam
642 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
644 else
645 Result := pthread_setschedparam
646 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
647 end if;
649 pragma Assert (Result = 0);
651 if FIFO_Within_Priorities 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);
701 Lock_RTS;
703 for J in Known_Tasks'Range loop
704 if Known_Tasks (J) = null then
705 Known_Tasks (J) := Self_ID;
706 Self_ID.Known_Tasks_Index := J;
707 exit;
708 end if;
709 end loop;
711 Unlock_RTS;
712 end Enter_Task;
714 --------------
715 -- New_ATCB --
716 --------------
718 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
719 begin
720 return new Ada_Task_Control_Block (Entry_Num);
721 end New_ATCB;
723 -------------------
724 -- Is_Valid_Task --
725 -------------------
727 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
729 -----------------------------
730 -- Register_Foreign_Thread --
731 -----------------------------
733 function Register_Foreign_Thread return Task_ID is
734 begin
735 if Is_Valid_Task then
736 return Self;
737 else
738 return Register_Foreign_Thread (pthread_self);
739 end if;
740 end Register_Foreign_Thread;
742 --------------------
743 -- Initialize_TCB --
744 --------------------
746 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
747 Mutex_Attr : aliased pthread_mutexattr_t;
748 Result : Interfaces.C.int;
749 Cond_Attr : aliased pthread_condattr_t;
751 begin
752 if not Single_Lock then
753 Result := pthread_mutexattr_init (Mutex_Attr'Access);
754 pragma Assert (Result = 0 or else Result = ENOMEM);
756 if Result = 0 then
757 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
758 Mutex_Attr'Access);
759 pragma Assert (Result = 0 or else Result = ENOMEM);
760 end if;
762 if Result /= 0 then
763 Succeeded := False;
764 return;
765 end if;
767 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
768 pragma Assert (Result = 0);
769 end if;
771 Result := pthread_condattr_init (Cond_Attr'Access);
772 pragma Assert (Result = 0 or else Result = ENOMEM);
774 if Result = 0 then
775 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
776 Cond_Attr'Access);
777 pragma Assert (Result = 0 or else Result = ENOMEM);
778 end if;
780 if Result = 0 then
781 Succeeded := True;
782 else
783 if not Single_Lock then
784 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
785 pragma Assert (Result = 0);
786 end if;
788 Succeeded := False;
789 end if;
791 Result := pthread_condattr_destroy (Cond_Attr'Access);
792 pragma Assert (Result = 0);
793 end Initialize_TCB;
795 -----------------
796 -- Create_Task --
797 -----------------
799 procedure Create_Task
800 (T : Task_ID;
801 Wrapper : System.Address;
802 Stack_Size : System.Parameters.Size_Type;
803 Priority : System.Any_Priority;
804 Succeeded : out Boolean)
806 Attributes : aliased pthread_attr_t;
807 Adjusted_Stack_Size : Interfaces.C.size_t;
808 Result : Interfaces.C.int;
810 function Thread_Body_Access is new
811 Unchecked_Conversion (System.Address, Thread_Body);
813 begin
814 if Stack_Size = Unspecified_Size then
815 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
817 elsif Stack_Size < Minimum_Stack_Size then
818 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
820 else
821 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
822 end if;
824 Result := pthread_attr_init (Attributes'Access);
825 pragma Assert (Result = 0 or else Result = ENOMEM);
827 if Result /= 0 then
828 Succeeded := False;
829 return;
830 end if;
832 Result := pthread_attr_setstacksize
833 (Attributes'Access, Adjusted_Stack_Size);
834 pragma Assert (Result = 0);
836 -- Since the initial signal mask of a thread is inherited from the
837 -- creator, and the Environment task has all its signals masked, we
838 -- do not need to manipulate caller's signal mask at this point.
839 -- All tasks in RTS will have All_Tasks_Mask initially.
841 Result := pthread_create
842 (T.Common.LL.Thread'Access,
843 Attributes'Access,
844 Thread_Body_Access (Wrapper),
845 To_Address (T));
846 pragma Assert (Result = 0 or else Result = EAGAIN);
848 Succeeded := Result = 0;
850 pthread_detach (T.Common.LL.Thread'Access);
851 -- Detach the thread using pthread_detach, sinc DCE threads do not have
852 -- pthread_attr_set_detachstate.
854 Result := pthread_attr_destroy (Attributes'Access);
855 pragma Assert (Result = 0);
857 Set_Priority (T, Priority);
858 end Create_Task;
860 ------------------
861 -- Finalize_TCB --
862 ------------------
864 procedure Finalize_TCB (T : Task_ID) is
865 Result : Interfaces.C.int;
866 Tmp : Task_ID := T;
867 Is_Self : constant Boolean := T = Self;
869 procedure Free is new
870 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
872 begin
873 if not Single_Lock then
874 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
875 pragma Assert (Result = 0);
876 end if;
878 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
879 pragma Assert (Result = 0);
881 if T.Known_Tasks_Index /= -1 then
882 Known_Tasks (T.Known_Tasks_Index) := null;
883 end if;
885 Free (Tmp);
887 if Is_Self then
888 Result := pthread_setspecific (ATCB_Key, System.Null_Address);
889 pragma Assert (Result = 0);
890 end if;
892 end Finalize_TCB;
894 ---------------
895 -- Exit_Task --
896 ---------------
898 procedure Exit_Task is
899 begin
900 Specific.Set (null);
901 end Exit_Task;
903 ----------------
904 -- Abort_Task --
905 ----------------
907 procedure Abort_Task (T : Task_ID) is
908 begin
910 -- Interrupt Server_Tasks may be waiting on an "event" flag (signal)
912 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
913 System.Interrupt_Management.Operations.Interrupt_Self_Process
914 (System.Interrupt_Management.Interrupt_ID
915 (PIO.Get_Interrupt_ID (T)));
916 end if;
917 end Abort_Task;
919 ----------------
920 -- Check_Exit --
921 ----------------
923 -- Dummy versions. The only currently working versions is for solaris
924 -- (native).
926 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
927 pragma Unreferenced (Self_ID);
928 begin
929 return True;
930 end Check_Exit;
932 --------------------
933 -- Check_No_Locks --
934 --------------------
936 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
937 pragma Unreferenced (Self_ID);
938 begin
939 return True;
940 end Check_No_Locks;
942 ----------------------
943 -- Environment_Task --
944 ----------------------
946 function Environment_Task return Task_ID is
947 begin
948 return Environment_Task_ID;
949 end Environment_Task;
951 --------------
952 -- Lock_RTS --
953 --------------
955 procedure Lock_RTS is
956 begin
957 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
958 end Lock_RTS;
960 ----------------
961 -- Unlock_RTS --
962 ----------------
964 procedure Unlock_RTS is
965 begin
966 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
967 end Unlock_RTS;
969 ------------------
970 -- Suspend_Task --
971 ------------------
973 function Suspend_Task
974 (T : ST.Task_ID;
975 Thread_Self : Thread_Id)
976 return Boolean
978 pragma Unreferenced (T);
979 pragma Unreferenced (Thread_Self);
981 begin
982 return False;
983 end Suspend_Task;
985 -----------------
986 -- Resume_Task --
987 -----------------
989 function Resume_Task
990 (T : ST.Task_ID;
991 Thread_Self : Thread_Id)
992 return Boolean
994 pragma Unreferenced (T);
995 pragma Unreferenced (Thread_Self);
997 begin
998 return False;
999 end Resume_Task;
1001 ----------------
1002 -- Initialize --
1003 ----------------
1005 procedure Initialize (Environment_Task : Task_ID) is
1006 act : aliased struct_sigaction;
1007 old_act : aliased struct_sigaction;
1008 Tmp_Set : aliased sigset_t;
1009 Result : Interfaces.C.int;
1011 function State (Int : System.Interrupt_Management.Interrupt_ID)
1012 return Character;
1013 pragma Import (C, State, "__gnat_get_interrupt_state");
1014 -- Get interrupt state. Defined in a-init.c
1015 -- The input argument is the interrupt number,
1016 -- and the result is one of the following:
1018 Default : constant Character := 's';
1019 -- 'n' this interrupt not set by any Interrupt_State pragma
1020 -- 'u' Interrupt_State pragma set state to User
1021 -- 'r' Interrupt_State pragma set state to Runtime
1022 -- 's' Interrupt_State pragma set state to System (use "default"
1023 -- system handler)
1025 begin
1026 Environment_Task_ID := Environment_Task;
1028 -- Initialize the lock used to synchronize chain of all ATCBs.
1030 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1032 Specific.Initialize (Environment_Task);
1034 Enter_Task (Environment_Task);
1036 -- Install the abort-signal handler
1038 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1039 /= Default
1040 then
1041 act.sa_flags := 0;
1042 act.sa_handler := Abort_Handler'Address;
1044 Result := sigemptyset (Tmp_Set'Access);
1045 pragma Assert (Result = 0);
1046 act.sa_mask := Tmp_Set;
1048 Result :=
1049 sigaction (
1050 Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1051 act'Unchecked_Access,
1052 old_act'Unchecked_Access);
1053 pragma Assert (Result = 0);
1054 end if;
1055 end Initialize;
1057 -- NOTE: Unlike other pthread implementations, we do *not* mask all
1058 -- signals here since we handle signals using the process-wide primitive
1059 -- signal, rather than using sigthreadmask and sigwait. The reason of
1060 -- this difference is that sigwait doesn't work when some critical
1061 -- signals (SIGABRT, SIGPIPE) are masked.
1063 end System.Task_Primitives.Operations;