* xcoffout.c (xcoff_tls_data_section_name): Define.
[official-gcc.git] / gcc / ada / s-taprop-lynxos.adb
blobd553f1e69ab15997dbbd5366210be5bcc4cc4cb4
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 LynxOS version of this file, adapted to make SCHED_FIFO and
33 -- ceiling locking (Annex D compliance) work properly.
35 -- This package contains all the GNULL primitives that interface directly with
36 -- the underlying OS.
38 pragma Polling (Off);
39 -- Turn off polling, we do not want ATC polling to take place during tasking
40 -- operations. It causes infinite loops and other problems.
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_Info;
51 with System.Soft_Links;
52 -- We use System.Soft_Links instead of System.Tasking.Initialization
53 -- because the later is a higher level package that we shouldn't depend on.
54 -- For example when using the restricted run time, it is replaced by
55 -- System.Tasking.Restricted.Stages.
57 package body System.Task_Primitives.Operations is
59 package SSL renames System.Soft_Links;
61 use System.Tasking.Debug;
62 use System.Tasking;
63 use Interfaces.C;
64 use System.OS_Interface;
65 use System.Parameters;
66 use System.OS_Primitives;
68 ----------------
69 -- Local Data --
70 ----------------
72 -- The followings are logically constants, but need to be initialized
73 -- at run time.
75 Single_RTS_Lock : aliased RTS_Lock;
76 -- This is a lock to allow only one thread of control in the RTS at
77 -- a time; it is used to execute in mutual exclusion from all other tasks.
78 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
80 ATCB_Key : aliased pthread_key_t;
81 -- Key used to find the Ada Task_Id associated with a thread
83 Environment_Task_Id : Task_Id;
84 -- A variable to hold Task_Id for the environment task
86 Locking_Policy : Character;
87 pragma Import (C, Locking_Policy, "__gl_locking_policy");
88 -- Value of the pragma Locking_Policy:
89 -- 'C' for Ceiling_Locking
90 -- 'I' for Inherit_Locking
91 -- ' ' for none.
93 Unblocked_Signal_Mask : aliased sigset_t;
94 -- The set of signals that should unblocked in all tasks
96 -- The followings are internal configuration constants needed
98 Next_Serial_Number : Task_Serial_Number := 100;
99 -- We start at 100, to reserve some special values for
100 -- using in error checking.
102 Time_Slice_Val : Integer;
103 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
105 Dispatching_Policy : Character;
106 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
108 Foreign_Task_Elaborated : aliased Boolean := True;
109 -- Used to identified fake tasks (i.e., non-Ada Threads)
111 --------------------
112 -- Local Packages --
113 --------------------
115 package Specific is
117 procedure Initialize (Environment_Task : Task_Id);
118 pragma Inline (Initialize);
119 -- Initialize various data needed by this package
121 function Is_Valid_Task return Boolean;
122 pragma Inline (Is_Valid_Task);
123 -- Does the current thread have an ATCB?
125 procedure Set (Self_Id : Task_Id);
126 pragma Inline (Set);
127 -- Set the self id for the current task
129 function Self return Task_Id;
130 pragma Inline (Self);
131 -- Return a pointer to the Ada Task Control Block of the calling task
133 end Specific;
135 package body Specific is separate;
136 -- The body of this package is target specific
138 ---------------------------------
139 -- Support for foreign threads --
140 ---------------------------------
142 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
143 -- Allocate and Initialize a new ATCB for the current Thread
145 function Register_Foreign_Thread
146 (Thread : Thread_Id) return Task_Id is separate;
148 -----------------------
149 -- Local Subprograms --
150 -----------------------
152 procedure Abort_Handler (Sig : Signal);
153 -- Signal handler used to implement asynchronous abort
155 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority);
156 -- This procedure calls the scheduler of the OS to set thread's priority
158 -------------------
159 -- Abort_Handler --
160 -------------------
162 procedure Abort_Handler (Sig : Signal) is
163 pragma Unreferenced (Sig);
165 T : constant Task_Id := Self;
166 Result : Interfaces.C.int;
167 Old_Set : aliased sigset_t;
169 begin
170 -- It is not safe to raise an exception when using ZCX and the GCC
171 -- exception handling mechanism.
173 if ZCX_By_Default and then GCC_ZCX_Support then
174 return;
175 end if;
177 if T.Deferral_Level = 0
178 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
179 and then not T.Aborting
180 then
181 T.Aborting := True;
183 -- Make sure signals used for RTS internal purpose are unmasked
185 Result :=
186 pthread_sigmask
187 (SIG_UNBLOCK,
188 Unblocked_Signal_Mask'Access,
189 Old_Set'Access);
190 pragma Assert (Result = 0);
192 raise Standard'Abort_Signal;
193 end if;
194 end Abort_Handler;
196 -----------------
197 -- Stack_Guard --
198 -----------------
200 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
201 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
202 Guard_Page_Address : Address;
204 Res : Interfaces.C.int;
206 begin
207 if Stack_Base_Available then
209 -- Compute the guard page address
211 Guard_Page_Address :=
212 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
214 if On then
215 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
216 else
217 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
218 end if;
220 pragma Assert (Res = 0);
221 end if;
222 end Stack_Guard;
224 --------------------
225 -- Get_Thread_Id --
226 --------------------
228 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
229 begin
230 return T.Common.LL.Thread;
231 end Get_Thread_Id;
233 ----------
234 -- Self --
235 ----------
237 function Self return Task_Id renames Specific.Self;
239 ---------------------
240 -- Initialize_Lock --
241 ---------------------
243 procedure Initialize_Lock
244 (Prio : System.Any_Priority;
245 L : not null access Lock)
247 Attributes : aliased pthread_mutexattr_t;
248 Result : Interfaces.C.int;
250 begin
251 Result := pthread_mutexattr_init (Attributes'Access);
252 pragma Assert (Result = 0 or else Result = ENOMEM);
254 if Result = ENOMEM then
255 raise Storage_Error;
256 end if;
258 if Locking_Policy = 'C' then
259 L.Ceiling := Prio;
260 end if;
262 Result := pthread_mutex_init (L.Mutex'Access, Attributes'Access);
263 pragma Assert (Result = 0 or else Result = ENOMEM);
265 if Result = ENOMEM then
266 raise Storage_Error;
267 end if;
269 Result := pthread_mutexattr_destroy (Attributes'Access);
270 pragma Assert (Result = 0);
271 end Initialize_Lock;
273 procedure Initialize_Lock
274 (L : not null access RTS_Lock;
275 Level : Lock_Level)
277 pragma Unreferenced (Level);
279 Attributes : aliased pthread_mutexattr_t;
280 Result : Interfaces.C.int;
282 begin
283 Result := pthread_mutexattr_init (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_mutex_init (L, Attributes'Access);
291 pragma Assert (Result = 0 or else Result = ENOMEM);
293 if Result = ENOMEM then
294 Result := pthread_mutexattr_destroy (Attributes'Access);
295 raise Storage_Error;
296 end if;
298 Result := pthread_mutexattr_destroy (Attributes'Access);
299 pragma Assert (Result = 0);
300 end Initialize_Lock;
302 -------------------
303 -- Finalize_Lock --
304 -------------------
306 procedure Finalize_Lock (L : not null access Lock) is
307 Result : Interfaces.C.int;
308 begin
309 Result := pthread_mutex_destroy (L.Mutex'Access);
310 pragma Assert (Result = 0);
311 end Finalize_Lock;
313 procedure Finalize_Lock (L : not null access RTS_Lock) is
314 Result : Interfaces.C.int;
315 begin
316 Result := pthread_mutex_destroy (L);
317 pragma Assert (Result = 0);
318 end Finalize_Lock;
320 ----------------
321 -- Write_Lock --
322 ----------------
324 procedure Write_Lock
325 (L : not null access Lock;
326 Ceiling_Violation : out Boolean)
328 Result : Interfaces.C.int;
329 T : constant Task_Id := Self;
331 begin
332 if Locking_Policy = 'C' then
333 if T.Common.Current_Priority > L.Ceiling then
334 Ceiling_Violation := True;
335 return;
336 end if;
338 L.Saved_Priority := T.Common.Current_Priority;
340 if T.Common.Current_Priority < L.Ceiling then
341 Set_OS_Priority (T, L.Ceiling);
342 end if;
343 end if;
345 Result := pthread_mutex_lock (L.Mutex'Access);
347 -- Assume that the cause of EINVAL is a priority ceiling violation
349 Ceiling_Violation := (Result = EINVAL);
350 pragma Assert (Result = 0 or else Result = EINVAL);
351 end Write_Lock;
353 -- No tricks on RTS_Locks
355 procedure Write_Lock
356 (L : not null access RTS_Lock;
357 Global_Lock : Boolean := False)
359 Result : Interfaces.C.int;
360 begin
361 if not Single_Lock or else Global_Lock then
362 Result := pthread_mutex_lock (L);
363 pragma Assert (Result = 0);
364 end if;
365 end Write_Lock;
367 procedure Write_Lock (T : Task_Id) is
368 Result : Interfaces.C.int;
369 begin
370 if not Single_Lock then
371 Result := pthread_mutex_lock (T.Common.LL.L'Access);
372 pragma Assert (Result = 0);
373 end if;
374 end Write_Lock;
376 ---------------
377 -- Read_Lock --
378 ---------------
380 procedure Read_Lock
381 (L : not null access Lock;
382 Ceiling_Violation : out Boolean)
384 begin
385 Write_Lock (L, Ceiling_Violation);
386 end Read_Lock;
388 ------------
389 -- Unlock --
390 ------------
392 procedure Unlock (L : not null access Lock) is
393 Result : Interfaces.C.int;
394 T : constant Task_Id := Self;
396 begin
397 Result := pthread_mutex_unlock (L.Mutex'Access);
398 pragma Assert (Result = 0);
400 if Locking_Policy = 'C' then
401 if T.Common.Current_Priority > L.Saved_Priority then
402 Set_OS_Priority (T, L.Saved_Priority);
403 end if;
404 end if;
405 end Unlock;
407 procedure Unlock
408 (L : not null access RTS_Lock;
409 Global_Lock : Boolean := False)
411 Result : Interfaces.C.int;
412 begin
413 if not Single_Lock or else Global_Lock then
414 Result := pthread_mutex_unlock (L);
415 pragma Assert (Result = 0);
416 end if;
417 end Unlock;
419 procedure Unlock (T : Task_Id) is
420 Result : Interfaces.C.int;
421 begin
422 if not Single_Lock then
423 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
424 pragma Assert (Result = 0);
425 end if;
426 end Unlock;
428 -----------------
429 -- Set_Ceiling --
430 -----------------
432 -- Dynamic priority ceilings are not supported by the underlying system
434 procedure Set_Ceiling
435 (L : not null access Lock;
436 Prio : System.Any_Priority)
438 pragma Unreferenced (L, Prio);
439 begin
440 null;
441 end Set_Ceiling;
443 -----------
444 -- Sleep --
445 -----------
447 procedure Sleep
448 (Self_ID : Task_Id;
449 Reason : System.Tasking.Task_States)
451 pragma Unreferenced (Reason);
452 Result : Interfaces.C.int;
454 begin
455 if Single_Lock then
456 Result :=
457 pthread_cond_wait
458 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
459 else
460 Result :=
461 pthread_cond_wait
462 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
463 end if;
465 -- EINTR is not considered a failure
467 pragma Assert (Result = 0 or else Result = EINTR);
468 end Sleep;
470 -----------------
471 -- Timed_Sleep --
472 -----------------
474 -- This is for use within the run-time system, so abort is
475 -- assumed to be already deferred, and the caller should be
476 -- holding its own ATCB lock.
478 procedure Timed_Sleep
479 (Self_ID : Task_Id;
480 Time : Duration;
481 Mode : ST.Delay_Modes;
482 Reason : Task_States;
483 Timedout : out Boolean;
484 Yielded : out Boolean)
486 pragma Unreferenced (Reason);
488 Base_Time : constant Duration := Monotonic_Clock;
489 Check_Time : Duration := Base_Time;
490 Rel_Time : Duration;
491 Abs_Time : Duration;
492 Request : aliased timespec;
493 Result : Interfaces.C.int;
495 begin
496 Timedout := True;
497 Yielded := False;
499 if Mode = Relative then
500 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
502 if Relative_Timed_Wait then
503 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
504 end if;
506 else
507 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
509 if Relative_Timed_Wait then
510 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
511 end if;
512 end if;
514 if Abs_Time > Check_Time then
515 if Relative_Timed_Wait then
516 Request := To_Timespec (Rel_Time);
517 else
518 Request := To_Timespec (Abs_Time);
519 end if;
521 loop
522 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
524 if Single_Lock then
525 Result :=
526 pthread_cond_timedwait
527 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
528 Request'Access);
530 else
531 Result :=
532 pthread_cond_timedwait
533 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
534 Request'Access);
535 end if;
537 Check_Time := Monotonic_Clock;
538 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
540 if Result = 0 or Result = EINTR then
542 -- Somebody may have called Wakeup for us
544 Timedout := False;
545 exit;
546 end if;
548 pragma Assert (Result = ETIMEDOUT);
549 end loop;
550 end if;
551 end Timed_Sleep;
553 -----------------
554 -- Timed_Delay --
555 -----------------
557 -- This is for use in implementing delay statements, so we assume
558 -- the caller is abort-deferred but is holding no locks.
560 procedure Timed_Delay
561 (Self_ID : Task_Id;
562 Time : Duration;
563 Mode : ST.Delay_Modes)
565 Base_Time : constant Duration := Monotonic_Clock;
566 Check_Time : Duration := Base_Time;
567 Abs_Time : Duration;
568 Rel_Time : Duration;
569 Request : aliased timespec;
571 Result : Interfaces.C.int;
572 pragma Warnings (Off, Result);
574 begin
575 if Single_Lock then
576 Lock_RTS;
577 end if;
579 -- Comments needed in code below ???
581 Write_Lock (Self_ID);
583 if Mode = Relative then
584 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
586 if Relative_Timed_Wait then
587 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
588 end if;
590 else
591 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
593 if Relative_Timed_Wait then
594 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
595 end if;
596 end if;
598 if Abs_Time > Check_Time then
599 if Relative_Timed_Wait then
600 Request := To_Timespec (Rel_Time);
601 else
602 Request := To_Timespec (Abs_Time);
603 end if;
605 Self_ID.Common.State := Delay_Sleep;
607 loop
608 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
610 if Single_Lock then
611 Result :=
612 pthread_cond_timedwait
613 (Self_ID.Common.LL.CV'Access,
614 Single_RTS_Lock'Access,
615 Request'Access);
616 else
617 Result :=
618 pthread_cond_timedwait
619 (Self_ID.Common.LL.CV'Access,
620 Self_ID.Common.LL.L'Access,
621 Request'Access);
622 end if;
624 Check_Time := Monotonic_Clock;
625 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
627 pragma Assert (Result = 0 or else
628 Result = ETIMEDOUT or else
629 Result = EINTR);
630 end loop;
632 Self_ID.Common.State := Runnable;
633 end if;
635 Unlock (Self_ID);
637 if Single_Lock then
638 Unlock_RTS;
639 end if;
641 Result := sched_yield;
642 end Timed_Delay;
644 ---------------------
645 -- Monotonic_Clock --
646 ---------------------
648 function Monotonic_Clock return Duration is
649 TS : aliased timespec;
650 Result : Interfaces.C.int;
651 begin
652 Result :=
653 clock_gettime
654 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
655 pragma Assert (Result = 0);
656 return To_Duration (TS);
657 end Monotonic_Clock;
659 -------------------
660 -- RT_Resolution --
661 -------------------
663 function RT_Resolution return Duration is
664 Res : aliased timespec;
665 Result : Interfaces.C.int;
666 begin
667 Result :=
668 clock_getres
669 (clock_id => CLOCK_REALTIME, res => Res'Unchecked_Access);
670 pragma Assert (Result = 0);
671 return To_Duration (Res);
672 end RT_Resolution;
674 ------------
675 -- Wakeup --
676 ------------
678 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
679 pragma Unreferenced (Reason);
680 Result : Interfaces.C.int;
681 begin
682 Result := pthread_cond_signal (T.Common.LL.CV'Access);
683 pragma Assert (Result = 0);
684 end Wakeup;
686 -----------
687 -- Yield --
688 -----------
690 procedure Yield (Do_Yield : Boolean := True) is
691 Result : Interfaces.C.int;
692 pragma Unreferenced (Result);
693 begin
694 if Do_Yield then
695 Result := sched_yield;
696 end if;
697 end Yield;
699 ------------------
700 -- Set_Priority --
701 ------------------
703 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority) is
704 Result : Interfaces.C.int;
705 Param : aliased struct_sched_param;
707 function Get_Policy (Prio : System.Any_Priority) return Character;
708 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
709 -- Get priority specific dispatching policy
711 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
712 -- Upper case first character of the policy name corresponding to the
713 -- task as set by a Priority_Specific_Dispatching pragma.
715 begin
716 Param.sched_priority := Interfaces.C.int (Prio);
718 if Time_Slice_Supported
719 and then (Dispatching_Policy = 'R'
720 or else Priority_Specific_Policy = 'R'
721 or else Time_Slice_Val > 0)
722 then
723 Result :=
724 pthread_setschedparam
725 (T.Common.LL.Thread, SCHED_RR, Param'Access);
727 elsif Dispatching_Policy = 'F'
728 or else Priority_Specific_Policy = 'F'
729 or else Time_Slice_Val = 0
730 then
731 Result :=
732 pthread_setschedparam
733 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
735 else
736 Result :=
737 pthread_setschedparam
738 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
739 end if;
741 pragma Assert (Result = 0);
742 end Set_OS_Priority;
744 type Prio_Array_Type is array (System.Any_Priority) of Integer;
745 pragma Atomic_Components (Prio_Array_Type);
746 Prio_Array : Prio_Array_Type;
747 -- Comments needed for these declarations ???
749 procedure Set_Priority
750 (T : Task_Id;
751 Prio : System.Any_Priority;
752 Loss_Of_Inheritance : Boolean := False)
754 Array_Item : Integer;
756 begin
757 Set_OS_Priority (T, Prio);
759 if Locking_Policy = 'C' then
761 -- Annex D requirements: loss of inheritance puts task at the start
762 -- of the queue for that prio; copied from 5ztaprop (VxWorks).
764 if Loss_Of_Inheritance
765 and then Prio < T.Common.Current_Priority then
767 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
768 Prio_Array (T.Common.Base_Priority) := Array_Item;
770 loop
771 Yield;
772 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
773 or else Prio_Array (T.Common.Base_Priority) = 1;
774 end loop;
776 Prio_Array (T.Common.Base_Priority) :=
777 Prio_Array (T.Common.Base_Priority) - 1;
778 end if;
779 end if;
781 T.Common.Current_Priority := Prio;
782 end Set_Priority;
784 ------------------
785 -- Get_Priority --
786 ------------------
788 function Get_Priority (T : Task_Id) return System.Any_Priority is
789 begin
790 return T.Common.Current_Priority;
791 end Get_Priority;
793 ----------------
794 -- Enter_Task --
795 ----------------
797 procedure Enter_Task (Self_ID : Task_Id) is
798 begin
799 Self_ID.Common.LL.Thread := pthread_self;
800 Self_ID.Common.LL.LWP := lwp_self;
802 Specific.Set (Self_ID);
804 Lock_RTS;
806 for J in Known_Tasks'Range loop
807 if Known_Tasks (J) = null then
808 Known_Tasks (J) := Self_ID;
809 Self_ID.Known_Tasks_Index := J;
810 exit;
811 end if;
812 end loop;
814 Unlock_RTS;
815 end Enter_Task;
817 --------------
818 -- New_ATCB --
819 --------------
821 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
822 begin
823 return new Ada_Task_Control_Block (Entry_Num);
824 end New_ATCB;
826 -------------------
827 -- Is_Valid_Task --
828 -------------------
830 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
832 -----------------------------
833 -- Register_Foreign_Thread --
834 -----------------------------
836 function Register_Foreign_Thread return Task_Id is
837 begin
838 if Is_Valid_Task then
839 return Self;
840 else
841 return Register_Foreign_Thread (pthread_self);
842 end if;
843 end Register_Foreign_Thread;
845 --------------------
846 -- Initialize_TCB --
847 --------------------
849 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
850 Mutex_Attr : aliased pthread_mutexattr_t;
851 Result : Interfaces.C.int;
852 Cond_Attr : aliased pthread_condattr_t;
854 begin
855 -- Give the task a unique serial number
857 Self_ID.Serial_Number := Next_Serial_Number;
858 Next_Serial_Number := Next_Serial_Number + 1;
859 pragma Assert (Next_Serial_Number /= 0);
861 if not Single_Lock then
862 Result := pthread_mutexattr_init (Mutex_Attr'Access);
863 pragma Assert (Result = 0 or else Result = ENOMEM);
865 if Result = 0 then
866 Result :=
867 pthread_mutex_init
868 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
869 pragma Assert (Result = 0 or else Result = ENOMEM);
870 end if;
872 if Result /= 0 then
873 Succeeded := False;
874 return;
875 end if;
877 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
878 pragma Assert (Result = 0);
879 end if;
881 Result := pthread_condattr_init (Cond_Attr'Access);
882 pragma Assert (Result = 0 or else Result = ENOMEM);
884 if Result = 0 then
885 Result :=
886 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
887 pragma Assert (Result = 0 or else Result = ENOMEM);
888 end if;
890 if Result = 0 then
891 Succeeded := True;
892 else
893 if not Single_Lock then
894 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
895 pragma Assert (Result = 0);
896 end if;
898 Succeeded := False;
899 end if;
901 Result := pthread_condattr_destroy (Cond_Attr'Access);
902 pragma Assert (Result = 0);
903 end Initialize_TCB;
905 -----------------
906 -- Create_Task --
907 -----------------
909 procedure Create_Task
910 (T : Task_Id;
911 Wrapper : System.Address;
912 Stack_Size : System.Parameters.Size_Type;
913 Priority : System.Any_Priority;
914 Succeeded : out Boolean)
916 Attributes : aliased pthread_attr_t;
917 Adjusted_Stack_Size : Interfaces.C.size_t;
918 Result : Interfaces.C.int;
920 use System.Task_Info;
922 begin
923 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
925 if Stack_Base_Available then
927 -- If Stack Checking is supported then allocate 2 additional pages:
929 -- In the worst case, stack is allocated at something like
930 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
931 -- to be sure the effective stack size is greater than what
932 -- has been asked.
934 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
935 end if;
937 Result := pthread_attr_init (Attributes'Access);
938 pragma Assert (Result = 0 or else Result = ENOMEM);
940 if Result /= 0 then
941 Succeeded := False;
942 return;
943 end if;
945 Result :=
946 pthread_attr_setdetachstate
947 (Attributes'Access, PTHREAD_CREATE_DETACHED);
948 pragma Assert (Result = 0);
950 Result :=
951 pthread_attr_setstacksize
952 (Attributes'Access, Adjusted_Stack_Size);
953 pragma Assert (Result = 0);
955 if T.Common.Task_Info /= Default_Scope then
957 -- We are assuming that Scope_Type has the same values than the
958 -- corresponding C macros
960 Result :=
961 pthread_attr_setscope
962 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
963 pragma Assert (Result = 0);
964 end if;
966 -- Since the initial signal mask of a thread is inherited from the
967 -- creator, and the Environment task has all its signals masked, we
968 -- do not need to manipulate caller's signal mask at this point.
969 -- All tasks in RTS will have All_Tasks_Mask initially.
971 Result :=
972 pthread_create
973 (T.Common.LL.Thread'Access,
974 Attributes'Access,
975 Thread_Body_Access (Wrapper),
976 To_Address (T));
977 pragma Assert (Result = 0 or else Result = EAGAIN);
979 Succeeded := Result = 0;
981 Result := pthread_attr_destroy (Attributes'Access);
982 pragma Assert (Result = 0);
984 if Succeeded then
985 Set_Priority (T, Priority);
986 end if;
987 end Create_Task;
989 ------------------
990 -- Finalize_TCB --
991 ------------------
993 procedure Finalize_TCB (T : Task_Id) is
994 Result : Interfaces.C.int;
995 Tmp : Task_Id := T;
996 Is_Self : constant Boolean := T = Self;
998 procedure Free is new
999 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1001 begin
1002 if not Single_Lock then
1003 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1004 pragma Assert (Result = 0);
1005 end if;
1007 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1008 pragma Assert (Result = 0);
1010 if T.Known_Tasks_Index /= -1 then
1011 Known_Tasks (T.Known_Tasks_Index) := null;
1012 end if;
1014 Free (Tmp);
1016 if Is_Self then
1017 Result := st_setspecific (ATCB_Key, System.Null_Address);
1018 pragma Assert (Result = 0);
1019 end if;
1020 end Finalize_TCB;
1022 ---------------
1023 -- Exit_Task --
1024 ---------------
1026 procedure Exit_Task is
1027 begin
1028 Specific.Set (null);
1029 end Exit_Task;
1031 ----------------
1032 -- Abort_Task --
1033 ----------------
1035 procedure Abort_Task (T : Task_Id) is
1036 Result : Interfaces.C.int;
1037 begin
1038 Result :=
1039 pthread_kill
1040 (T.Common.LL.Thread,
1041 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1042 pragma Assert (Result = 0);
1043 end Abort_Task;
1045 ----------------
1046 -- Initialize --
1047 ----------------
1049 procedure Initialize (S : in out Suspension_Object) is
1050 Mutex_Attr : aliased pthread_mutexattr_t;
1051 Cond_Attr : aliased pthread_condattr_t;
1052 Result : Interfaces.C.int;
1054 begin
1055 -- Initialize internal state (always to False (RM D.10(6)))
1057 S.State := False;
1058 S.Waiting := False;
1060 -- Initialize internal mutex
1062 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1063 pragma Assert (Result = 0 or else Result = ENOMEM);
1065 if Result = ENOMEM then
1066 raise Storage_Error;
1067 end if;
1069 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1070 pragma Assert (Result = 0 or else Result = ENOMEM);
1072 if Result = ENOMEM then
1073 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1074 pragma Assert (Result = 0);
1076 raise Storage_Error;
1077 end if;
1079 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1080 pragma Assert (Result = 0);
1082 -- Initialize internal condition variable
1084 Result := pthread_condattr_init (Cond_Attr'Access);
1085 pragma Assert (Result = 0 or else Result = ENOMEM);
1087 if Result /= 0 then
1088 Result := pthread_mutex_destroy (S.L'Access);
1089 pragma Assert (Result = 0);
1091 if Result = ENOMEM then
1092 raise Storage_Error;
1093 end if;
1094 end if;
1096 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1097 pragma Assert (Result = 0 or else Result = ENOMEM);
1099 if Result /= 0 then
1100 Result := pthread_mutex_destroy (S.L'Access);
1101 pragma Assert (Result = 0);
1103 if Result = ENOMEM then
1104 Result := pthread_condattr_destroy (Cond_Attr'Access);
1105 pragma Assert (Result = 0);
1107 raise Storage_Error;
1108 end if;
1109 end if;
1111 Result := pthread_condattr_destroy (Cond_Attr'Access);
1112 pragma Assert (Result = 0);
1113 end Initialize;
1115 --------------
1116 -- Finalize --
1117 --------------
1119 procedure Finalize (S : in out Suspension_Object) is
1120 Result : Interfaces.C.int;
1122 begin
1123 -- Destroy internal mutex
1125 Result := pthread_mutex_destroy (S.L'Access);
1126 pragma Assert (Result = 0);
1128 -- Destroy internal condition variable
1130 Result := pthread_cond_destroy (S.CV'Access);
1131 pragma Assert (Result = 0);
1132 end Finalize;
1134 -------------------
1135 -- Current_State --
1136 -------------------
1138 function Current_State (S : Suspension_Object) return Boolean is
1139 begin
1140 -- We do not want to use lock on this read operation. State is marked
1141 -- as Atomic so that we ensure that the value retrieved is correct.
1143 return S.State;
1144 end Current_State;
1146 ---------------
1147 -- Set_False --
1148 ---------------
1150 procedure Set_False (S : in out Suspension_Object) is
1151 Result : Interfaces.C.int;
1153 begin
1154 SSL.Abort_Defer.all;
1156 Result := pthread_mutex_lock (S.L'Access);
1157 pragma Assert (Result = 0);
1159 S.State := False;
1161 Result := pthread_mutex_unlock (S.L'Access);
1162 pragma Assert (Result = 0);
1164 SSL.Abort_Undefer.all;
1165 end Set_False;
1167 --------------
1168 -- Set_True --
1169 --------------
1171 procedure Set_True (S : in out Suspension_Object) is
1172 Result : Interfaces.C.int;
1174 begin
1175 SSL.Abort_Defer.all;
1177 Result := pthread_mutex_lock (S.L'Access);
1178 pragma Assert (Result = 0);
1180 -- If there is already a task waiting on this suspension object then
1181 -- we resume it, leaving the state of the suspension object to False,
1182 -- as specified in (RM D.10(9)). Otherwise, just leave state set True.
1184 if S.Waiting then
1185 S.Waiting := False;
1186 S.State := False;
1188 Result := pthread_cond_signal (S.CV'Access);
1189 pragma Assert (Result = 0);
1191 else
1192 S.State := True;
1193 end if;
1195 Result := pthread_mutex_unlock (S.L'Access);
1196 pragma Assert (Result = 0);
1198 SSL.Abort_Undefer.all;
1199 end Set_True;
1201 ------------------------
1202 -- Suspend_Until_True --
1203 ------------------------
1205 procedure Suspend_Until_True (S : in out Suspension_Object) is
1206 Result : Interfaces.C.int;
1208 begin
1209 SSL.Abort_Defer.all;
1211 Result := pthread_mutex_lock (S.L'Access);
1212 pragma Assert (Result = 0);
1214 if S.Waiting then
1216 -- Program_Error must be raised upon calling Suspend_Until_True
1217 -- if another task is already waiting on that suspension object
1218 -- (RM D.10 (10)).
1220 Result := pthread_mutex_unlock (S.L'Access);
1221 pragma Assert (Result = 0);
1223 SSL.Abort_Undefer.all;
1225 raise Program_Error;
1227 else
1228 -- Suspend the task if the state is False. Otherwise, the task
1229 -- continues its execution, and the state of the suspension object
1230 -- is set to False (RM D.10(9)).
1232 if S.State then
1233 S.State := False;
1234 else
1235 S.Waiting := True;
1236 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1237 end if;
1239 Result := pthread_mutex_unlock (S.L'Access);
1240 pragma Assert (Result = 0);
1242 SSL.Abort_Undefer.all;
1243 end if;
1244 end Suspend_Until_True;
1246 ----------------
1247 -- Check_Exit --
1248 ----------------
1250 -- Dummy version
1252 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1253 pragma Unreferenced (Self_ID);
1254 begin
1255 return True;
1256 end Check_Exit;
1258 --------------------
1259 -- Check_No_Locks --
1260 --------------------
1262 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1263 pragma Unreferenced (Self_ID);
1264 begin
1265 return True;
1266 end Check_No_Locks;
1268 ----------------------
1269 -- Environment_Task --
1270 ----------------------
1272 function Environment_Task return Task_Id is
1273 begin
1274 return Environment_Task_Id;
1275 end Environment_Task;
1277 --------------
1278 -- Lock_RTS --
1279 --------------
1281 procedure Lock_RTS is
1282 begin
1283 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1284 end Lock_RTS;
1286 ----------------
1287 -- Unlock_RTS --
1288 ----------------
1290 procedure Unlock_RTS is
1291 begin
1292 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1293 end Unlock_RTS;
1295 ------------------
1296 -- Suspend_Task --
1297 ------------------
1299 function Suspend_Task
1300 (T : ST.Task_Id;
1301 Thread_Self : Thread_Id) return Boolean
1303 pragma Unreferenced (T);
1304 pragma Unreferenced (Thread_Self);
1305 begin
1306 return False;
1307 end Suspend_Task;
1309 -----------------
1310 -- Resume_Task --
1311 -----------------
1313 function Resume_Task
1314 (T : ST.Task_Id;
1315 Thread_Self : Thread_Id) return Boolean
1317 pragma Unreferenced (T);
1318 pragma Unreferenced (Thread_Self);
1319 begin
1320 return False;
1321 end Resume_Task;
1323 --------------------
1324 -- Stop_All_Tasks --
1325 --------------------
1327 procedure Stop_All_Tasks is
1328 begin
1329 null;
1330 end Stop_All_Tasks;
1332 ---------------
1333 -- Stop_Task --
1334 ---------------
1336 function Stop_Task (T : ST.Task_Id) return Boolean is
1337 pragma Unreferenced (T);
1338 begin
1339 return False;
1340 end Stop_Task;
1342 -------------------
1343 -- Continue_Task --
1344 -------------------
1346 function Continue_Task (T : ST.Task_Id) return Boolean is
1347 pragma Unreferenced (T);
1348 begin
1349 return False;
1350 end Continue_Task;
1352 ----------------
1353 -- Initialize --
1354 ----------------
1356 procedure Initialize (Environment_Task : Task_Id) is
1357 act : aliased struct_sigaction;
1358 old_act : aliased struct_sigaction;
1359 Tmp_Set : aliased sigset_t;
1360 Result : Interfaces.C.int;
1362 function State
1363 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1364 pragma Import (C, State, "__gnat_get_interrupt_state");
1365 -- Get interrupt state. Defined in a-init.c
1366 -- The input argument is the interrupt number,
1367 -- and the result is one of the following:
1369 Default : constant Character := 's';
1370 -- 'n' this interrupt not set by any Interrupt_State pragma
1371 -- 'u' Interrupt_State pragma set state to User
1372 -- 'r' Interrupt_State pragma set state to Runtime
1373 -- 's' Interrupt_State pragma set state to System (use "default"
1374 -- system handler)
1376 begin
1377 Environment_Task_Id := Environment_Task;
1379 Interrupt_Management.Initialize;
1381 -- Prepare the set of signals that should unblocked in all tasks
1383 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1384 pragma Assert (Result = 0);
1386 for J in Interrupt_Management.Interrupt_ID loop
1387 if System.Interrupt_Management.Keep_Unmasked (J) then
1388 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1389 pragma Assert (Result = 0);
1390 end if;
1391 end loop;
1393 -- Initialize the lock used to synchronize chain of all ATCBs
1395 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1397 Specific.Initialize (Environment_Task);
1399 Enter_Task (Environment_Task);
1401 -- Install the abort-signal handler
1403 if State
1404 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1405 then
1406 act.sa_flags := 0;
1407 act.sa_handler := Abort_Handler'Address;
1409 Result := sigemptyset (Tmp_Set'Access);
1410 pragma Assert (Result = 0);
1411 act.sa_mask := Tmp_Set;
1413 Result :=
1414 sigaction
1415 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1416 act'Unchecked_Access,
1417 old_act'Unchecked_Access);
1419 pragma Assert (Result = 0);
1420 end if;
1421 end Initialize;
1423 end System.Task_Primitives.Operations;