hppa: Fix LO_SUM DLTIND14R address support in PRINT_OPERAND_ADDRESS
[official-gcc.git] / gcc / ada / libgnarl / s-taprop__rtems.adb
blob68a956e5c06a2580a778c3df35d4c85837946a94
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-2024, 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 the RTEMS version of this package
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
37 with Ada.Unchecked_Conversion;
39 with Interfaces.C;
41 with System.Tasking.Debug;
42 with System.Interrupt_Management;
43 with System.OS_Constants;
44 with System.OS_Primitives;
45 with System.Task_Info;
47 with System.Soft_Links;
48 -- We use System.Soft_Links instead of System.Tasking.Initialization
49 -- because the later is a higher level package that we shouldn't depend on.
50 -- For example when using the restricted run time, it is replaced by
51 -- System.Tasking.Restricted.Stages.
53 package body System.Task_Primitives.Operations is
55 package OSC renames System.OS_Constants;
56 package SSL renames System.Soft_Links;
58 use System.Tasking.Debug;
59 use System.Tasking;
60 use Interfaces.C;
61 use System.OS_Interface;
62 use System.Parameters;
63 use System.OS_Primitives;
65 ----------------
66 -- Local Data --
67 ----------------
69 -- The followings are logically constants, but need to be initialized
70 -- at run time.
72 Single_RTS_Lock : aliased RTS_Lock;
73 -- This is a lock to allow only one thread of control in the RTS at
74 -- a time; it is used to execute in mutual exclusion from all other tasks.
75 -- Used to protect All_Tasks_List
77 Environment_Task_Id : Task_Id;
78 -- A variable to hold Task_Id for the environment task
80 Locking_Policy : constant Character;
81 pragma Import (C, Locking_Policy, "__gl_locking_policy");
82 -- Value of the pragma Locking_Policy:
83 -- 'C' for Ceiling_Locking
84 -- 'I' for Inherit_Locking
85 -- ' ' for none.
87 -- The followings are internal configuration constants needed
89 Next_Serial_Number : Task_Serial_Number := 100;
90 -- We start at 100, to reserve some special values for
91 -- using in error checking.
93 Time_Slice_Val : constant Integer;
94 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
96 Dispatching_Policy : constant Character;
97 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
99 Foreign_Task_Elaborated : aliased Boolean := True;
100 -- Used to identified fake tasks (i.e., non-Ada Threads)
102 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
103 -- Whether to use an alternate signal stack for stack overflows
105 Abort_Handler_Installed : Boolean := False;
106 -- True if a handler for the abort signal is installed
108 --------------------
109 -- Local Packages --
110 --------------------
112 package Specific is
114 procedure Initialize (Environment_Task : Task_Id);
115 pragma Inline (Initialize);
116 -- Initialize various data needed by this package
118 function Is_Valid_Task return Boolean;
119 pragma Inline (Is_Valid_Task);
120 -- Does executing thread have a TCB?
122 procedure Set (Self_Id : Task_Id);
123 pragma Inline (Set);
124 -- Set the self id for the current task
126 function Self return Task_Id;
127 pragma Inline (Self);
128 -- Return a pointer to the Ada Task Control Block of the calling task
130 end Specific;
132 package body Specific is separate;
133 -- The body of this package is target specific
135 package Monotonic is
137 function Monotonic_Clock return Duration;
138 pragma Inline (Monotonic_Clock);
139 -- Returns an absolute time, represented as an offset relative to some
140 -- unspecified starting point, typically system boot time. This clock
141 -- is not affected by discontinuous jumps in the system time.
143 function RT_Resolution return Duration;
144 pragma Inline (RT_Resolution);
145 -- Returns resolution of the underlying clock used to implement RT_Clock
147 procedure Timed_Sleep
148 (Self_ID : ST.Task_Id;
149 Time : Duration;
150 Mode : ST.Delay_Modes;
151 Reason : System.Tasking.Task_States;
152 Timedout : out Boolean;
153 Yielded : out Boolean);
154 -- Combination of Sleep (above) and Timed_Delay
156 procedure Timed_Delay
157 (Self_ID : ST.Task_Id;
158 Time : Duration;
159 Mode : ST.Delay_Modes);
160 -- Implement the semantics of the delay statement.
161 -- The caller should be abort-deferred and should not hold any locks.
163 end Monotonic;
165 package body Monotonic is separate;
167 ----------------------------------
168 -- ATCB allocation/deallocation --
169 ----------------------------------
171 package body ATCB_Allocation is separate;
172 -- The body of this package is shared across several targets
174 ---------------------------------
175 -- Support for foreign threads --
176 ---------------------------------
178 function Register_Foreign_Thread
179 (Thread : Thread_Id;
180 Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id;
181 -- Allocate and initialize a new ATCB for the current Thread. The size of
182 -- the secondary stack can be optionally specified.
184 function Register_Foreign_Thread
185 (Thread : Thread_Id;
186 Sec_Stack_Size : Size_Type := Unspecified_Size)
187 return Task_Id is separate;
189 -----------------------
190 -- Local Subprograms --
191 -----------------------
193 procedure Abort_Handler (Sig : Signal);
194 -- Signal handler used to implement asynchronous abort.
195 -- See also comment before body, below.
197 function To_Address is
198 new Ada.Unchecked_Conversion (Task_Id, System.Address);
200 function GNAT_pthread_condattr_setup
201 (attr : access pthread_condattr_t) return int;
202 pragma Import (C,
203 GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup");
205 -------------------
206 -- Abort_Handler --
207 -------------------
209 -- Target-dependent binding of inter-thread Abort signal to the raising of
210 -- the Abort_Signal exception.
212 -- The technical issues and alternatives here are essentially the
213 -- same as for raising exceptions in response to other signals
214 -- (e.g. Storage_Error). See code and comments in the package body
215 -- System.Interrupt_Management.
217 -- Some implementations may not allow an exception to be propagated out of
218 -- a handler, and others might leave the signal or interrupt that invoked
219 -- this handler masked after the exceptional return to the application
220 -- code.
222 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
223 -- most UNIX systems, this will allow transfer out of a signal handler,
224 -- which is usually the only mechanism available for implementing
225 -- asynchronous handlers of this kind. However, some systems do not
226 -- restore the signal mask on longjmp(), leaving the abort signal masked.
228 procedure Abort_Handler (Sig : Signal) is
229 pragma Unreferenced (Sig);
231 T : constant Task_Id := Self;
232 Old_Set : aliased sigset_t;
233 Unblocked_Mask : aliased sigset_t;
234 Result : Interfaces.C.int;
235 pragma Warnings (Off, Result);
237 begin
238 -- It's not safe to raise an exception when using GCC ZCX mechanism.
239 -- Note that we still need to install a signal handler, since in some
240 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
241 -- need to send the Abort signal to a task.
243 if ZCX_By_Default then
244 return;
245 end if;
247 if T.Deferral_Level = 0
248 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
249 not T.Aborting
250 then
251 T.Aborting := True;
253 -- Make sure signals used for RTS internal purpose are unmasked
255 Result := sigemptyset (Unblocked_Mask'Access);
256 pragma Assert (Result = 0);
257 Result :=
258 sigaddset
259 (Unblocked_Mask'Access,
260 Signal (Interrupt_Management.Abort_Task_Interrupt));
261 pragma Assert (Result = 0);
262 Result := sigaddset (Unblocked_Mask'Access, SIGBUS);
263 pragma Assert (Result = 0);
264 Result := sigaddset (Unblocked_Mask'Access, SIGFPE);
265 pragma Assert (Result = 0);
266 Result := sigaddset (Unblocked_Mask'Access, SIGILL);
267 pragma Assert (Result = 0);
268 Result := sigaddset (Unblocked_Mask'Access, SIGSEGV);
269 pragma Assert (Result = 0);
271 Result :=
272 pthread_sigmask
273 (SIG_UNBLOCK,
274 Unblocked_Mask'Access,
275 Old_Set'Access);
276 pragma Assert (Result = 0);
278 raise Standard'Abort_Signal;
279 end if;
280 end Abort_Handler;
282 -----------------
283 -- Stack_Guard --
284 -----------------
286 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
287 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
288 Page_Size : Address;
289 Res : Interfaces.C.int;
291 begin
292 if Stack_Base_Available then
294 -- Compute the guard page address
296 Page_Size := Address (Get_Page_Size);
297 Res :=
298 mprotect
299 (Stack_Base - (Stack_Base mod Page_Size) + Page_Size,
300 size_t (Page_Size),
301 prot => (if On then PROT_ON else PROT_OFF));
302 pragma Assert (Res = 0);
303 end if;
304 end Stack_Guard;
306 --------------------
307 -- Get_Thread_Id --
308 --------------------
310 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
311 begin
312 return T.Common.LL.Thread;
313 end Get_Thread_Id;
315 ----------
316 -- Self --
317 ----------
319 function Self return Task_Id renames Specific.Self;
321 ---------------------
322 -- Initialize_Lock --
323 ---------------------
325 -- Note: mutexes and cond_variables needed per-task basis are initialized
326 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
327 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
328 -- status change of RTS. Therefore raising Storage_Error in the following
329 -- routines should be able to be handled safely.
331 procedure Initialize_Lock
332 (Prio : System.Any_Priority;
333 L : not null access Lock)
335 Attributes : aliased pthread_mutexattr_t;
336 Result : Interfaces.C.int;
338 begin
339 Result := pthread_mutexattr_init (Attributes'Access);
340 pragma Assert (Result = 0 or else Result = ENOMEM);
342 if Result = ENOMEM then
343 raise Storage_Error;
344 end if;
346 if Locking_Policy = 'C' then
347 Result := pthread_mutexattr_setprotocol
348 (Attributes'Access, PTHREAD_PRIO_PROTECT);
349 pragma Assert (Result = 0);
351 Result := pthread_mutexattr_setprioceiling
352 (Attributes'Access, Interfaces.C.int (Prio));
353 pragma Assert (Result = 0);
355 elsif Locking_Policy = 'I' then
356 Result := pthread_mutexattr_setprotocol
357 (Attributes'Access, PTHREAD_PRIO_INHERIT);
358 pragma Assert (Result = 0);
359 end if;
361 Result := pthread_mutex_init (L.WO'Access, Attributes'Access);
362 pragma Assert (Result = 0 or else Result = ENOMEM);
364 if Result = ENOMEM then
365 Result := pthread_mutexattr_destroy (Attributes'Access);
366 raise Storage_Error;
367 end if;
369 Result := pthread_mutexattr_destroy (Attributes'Access);
370 pragma Assert (Result = 0);
371 end Initialize_Lock;
373 procedure Initialize_Lock
374 (L : not null access RTS_Lock; Level : Lock_Level)
376 pragma Unreferenced (Level);
378 Attributes : aliased pthread_mutexattr_t;
379 Result : Interfaces.C.int;
381 begin
382 Result := pthread_mutexattr_init (Attributes'Access);
383 pragma Assert (Result = 0 or else Result = ENOMEM);
385 if Result = ENOMEM then
386 raise Storage_Error;
387 end if;
389 if Locking_Policy = 'C' then
390 Result := pthread_mutexattr_setprotocol
391 (Attributes'Access, PTHREAD_PRIO_PROTECT);
392 pragma Assert (Result = 0);
394 Result := pthread_mutexattr_setprioceiling
395 (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
396 pragma Assert (Result = 0);
398 elsif Locking_Policy = 'I' then
399 Result := pthread_mutexattr_setprotocol
400 (Attributes'Access, PTHREAD_PRIO_INHERIT);
401 pragma Assert (Result = 0);
402 end if;
404 Result := pthread_mutex_init (L, Attributes'Access);
405 pragma Assert (Result = 0 or else Result = ENOMEM);
407 if Result = ENOMEM then
408 Result := pthread_mutexattr_destroy (Attributes'Access);
409 raise Storage_Error;
410 end if;
412 Result := pthread_mutexattr_destroy (Attributes'Access);
413 pragma Assert (Result = 0);
414 end Initialize_Lock;
416 -------------------
417 -- Finalize_Lock --
418 -------------------
420 procedure Finalize_Lock (L : not null access Lock) is
421 Result : Interfaces.C.int;
422 begin
423 Result := pthread_mutex_destroy (L.WO'Access);
424 pragma Assert (Result = 0);
425 end Finalize_Lock;
427 procedure Finalize_Lock (L : not null access RTS_Lock) is
428 Result : Interfaces.C.int;
429 begin
430 Result := pthread_mutex_destroy (L);
431 pragma Assert (Result = 0);
432 end Finalize_Lock;
434 ----------------
435 -- Write_Lock --
436 ----------------
438 procedure Write_Lock
439 (L : not null access Lock; Ceiling_Violation : out Boolean)
441 Result : Interfaces.C.int;
443 begin
444 Result := pthread_mutex_lock (L.WO'Access);
446 -- The cause of EINVAL is a priority ceiling violation
448 Ceiling_Violation := Result = EINVAL;
449 pragma Assert (Result = 0 or else Ceiling_Violation);
450 end Write_Lock;
452 procedure Write_Lock (L : not null access RTS_Lock) is
453 Result : Interfaces.C.int;
454 begin
455 Result := pthread_mutex_lock (L);
456 pragma Assert (Result = 0);
457 end Write_Lock;
459 procedure Write_Lock (T : Task_Id) is
460 Result : Interfaces.C.int;
461 begin
462 Result := pthread_mutex_lock (T.Common.LL.L'Access);
463 pragma Assert (Result = 0);
464 end Write_Lock;
466 ---------------
467 -- Read_Lock --
468 ---------------
470 procedure Read_Lock
471 (L : not null access Lock; Ceiling_Violation : out Boolean) is
472 begin
473 Write_Lock (L, Ceiling_Violation);
474 end Read_Lock;
476 ------------
477 -- Unlock --
478 ------------
480 procedure Unlock (L : not null access Lock) is
481 Result : Interfaces.C.int;
482 begin
483 Result := pthread_mutex_unlock (L.WO'Access);
484 pragma Assert (Result = 0);
485 end Unlock;
487 procedure Unlock (L : not null access RTS_Lock) is
488 Result : Interfaces.C.int;
489 begin
490 Result := pthread_mutex_unlock (L);
491 pragma Assert (Result = 0);
492 end Unlock;
494 procedure Unlock (T : Task_Id) is
495 Result : Interfaces.C.int;
496 begin
497 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
498 pragma Assert (Result = 0);
499 end Unlock;
501 -----------------
502 -- Set_Ceiling --
503 -----------------
505 -- Dynamic priority ceilings are not supported by the underlying system
507 procedure Set_Ceiling
508 (L : not null access Lock;
509 Prio : System.Any_Priority)
511 pragma Unreferenced (L, Prio);
512 begin
513 null;
514 end Set_Ceiling;
516 -----------
517 -- Sleep --
518 -----------
520 procedure Sleep
521 (Self_ID : Task_Id;
522 Reason : System.Tasking.Task_States)
524 pragma Unreferenced (Reason);
526 Result : Interfaces.C.int;
528 begin
529 Result :=
530 pthread_cond_wait
531 (cond => Self_ID.Common.LL.CV'Access,
532 mutex => Self_ID.Common.LL.L'Access);
534 -- EINTR is not considered a failure
536 pragma Assert (Result = 0 or else Result = EINTR);
537 end Sleep;
539 -----------------
540 -- Timed_Sleep --
541 -----------------
543 -- This is for use within the run-time system, so abort is
544 -- assumed to be already deferred, and the caller should be
545 -- holding its own ATCB lock.
547 procedure Timed_Sleep
548 (Self_ID : Task_Id;
549 Time : Duration;
550 Mode : ST.Delay_Modes;
551 Reason : Task_States;
552 Timedout : out Boolean;
553 Yielded : out Boolean) renames Monotonic.Timed_Sleep;
555 -----------------
556 -- Timed_Delay --
557 -----------------
559 -- This is for use in implementing delay statements, so we assume the
560 -- caller is abort-deferred but is holding no locks.
562 procedure Timed_Delay
563 (Self_ID : Task_Id;
564 Time : Duration;
565 Mode : ST.Delay_Modes) renames Monotonic.Timed_Delay;
567 ---------------------
568 -- Monotonic_Clock --
569 ---------------------
571 function Monotonic_Clock return Duration renames Monotonic.Monotonic_Clock;
573 -------------------
574 -- RT_Resolution --
575 -------------------
577 function RT_Resolution return Duration renames Monotonic.RT_Resolution;
579 ------------
580 -- Wakeup --
581 ------------
583 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
584 pragma Unreferenced (Reason);
585 Result : Interfaces.C.int;
586 begin
587 Result := pthread_cond_signal (T.Common.LL.CV'Access);
588 pragma Assert (Result = 0);
589 end Wakeup;
591 -----------
592 -- Yield --
593 -----------
595 procedure Yield (Do_Yield : Boolean := True) is
596 Result : Interfaces.C.int;
597 pragma Unreferenced (Result);
598 begin
599 if Do_Yield then
600 Result := sched_yield;
601 end if;
602 end Yield;
604 ------------------
605 -- Set_Priority --
606 ------------------
608 procedure Set_Priority
609 (T : Task_Id;
610 Prio : System.Any_Priority;
611 Loss_Of_Inheritance : Boolean := False)
613 pragma Unreferenced (Loss_Of_Inheritance);
615 Result : Interfaces.C.int;
616 Param : aliased struct_sched_param;
618 function Get_Policy (Prio : System.Any_Priority) return Character;
619 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
620 -- Get priority specific dispatching policy
622 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
623 -- Upper case first character of the policy name corresponding to the
624 -- task as set by a Priority_Specific_Dispatching pragma.
626 begin
627 T.Common.Current_Priority := Prio;
628 Param.sched_priority := To_Target_Priority (Prio);
630 if Time_Slice_Supported
631 and then (Dispatching_Policy = 'R'
632 or else Priority_Specific_Policy = 'R'
633 or else Time_Slice_Val > 0)
634 then
635 Result := pthread_setschedparam
636 (T.Common.LL.Thread, SCHED_RR, Param'Access);
638 elsif Dispatching_Policy = 'F'
639 or else Priority_Specific_Policy = 'F'
640 or else Time_Slice_Val = 0
641 then
642 Result := pthread_setschedparam
643 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
645 else
646 Result := pthread_setschedparam
647 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
648 end if;
650 pragma Assert (Result = 0);
651 end Set_Priority;
653 ------------------
654 -- Get_Priority --
655 ------------------
657 function Get_Priority (T : Task_Id) return System.Any_Priority is
658 begin
659 return T.Common.Current_Priority;
660 end Get_Priority;
662 ----------------
663 -- Enter_Task --
664 ----------------
666 procedure Enter_Task (Self_ID : Task_Id) is
667 begin
668 Self_ID.Common.LL.Thread := pthread_self;
669 Self_ID.Common.LL.LWP := lwp_self;
671 Specific.Set (Self_ID);
673 if Use_Alternate_Stack then
674 declare
675 Stack : aliased stack_t;
676 Result : Interfaces.C.int;
677 begin
678 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
679 Stack.ss_size := Alternate_Stack_Size;
680 Stack.ss_flags := 0;
681 Result := sigaltstack (Stack'Access, null);
682 pragma Assert (Result = 0);
683 end;
684 end if;
685 end Enter_Task;
687 -------------------
688 -- Is_Valid_Task --
689 -------------------
691 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
693 -----------------------------
694 -- Register_Foreign_Thread --
695 -----------------------------
697 function Register_Foreign_Thread return Task_Id is
698 begin
699 if Is_Valid_Task then
700 return Self;
701 else
702 return Register_Foreign_Thread (pthread_self);
703 end if;
704 end Register_Foreign_Thread;
706 --------------------
707 -- Initialize_TCB --
708 --------------------
710 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
711 Mutex_Attr : aliased pthread_mutexattr_t;
712 Result : Interfaces.C.int;
713 Cond_Attr : aliased pthread_condattr_t;
715 begin
716 -- Give the task a unique serial number
718 Self_ID.Serial_Number := Next_Serial_Number;
719 Next_Serial_Number := Next_Serial_Number + 1;
720 pragma Assert (Next_Serial_Number /= 0);
722 Result := pthread_mutexattr_init (Mutex_Attr'Access);
723 pragma Assert (Result = 0 or else Result = ENOMEM);
725 if Result = 0 then
726 if Locking_Policy = 'C' then
727 Result :=
728 pthread_mutexattr_setprotocol
729 (Mutex_Attr'Access,
730 PTHREAD_PRIO_PROTECT);
731 pragma Assert (Result = 0);
733 Result :=
734 pthread_mutexattr_setprioceiling
735 (Mutex_Attr'Access,
736 Interfaces.C.int (System.Any_Priority'Last));
737 pragma Assert (Result = 0);
739 elsif Locking_Policy = 'I' then
740 Result :=
741 pthread_mutexattr_setprotocol
742 (Mutex_Attr'Access,
743 PTHREAD_PRIO_INHERIT);
744 pragma Assert (Result = 0);
745 end if;
747 Result :=
748 pthread_mutex_init
749 (Self_ID.Common.LL.L'Access,
750 Mutex_Attr'Access);
751 pragma Assert (Result = 0 or else Result = ENOMEM);
752 end if;
754 if Result /= 0 then
755 Succeeded := False;
756 return;
757 end if;
759 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
760 pragma Assert (Result = 0);
762 Result := pthread_condattr_init (Cond_Attr'Access);
763 pragma Assert (Result = 0 or else Result = ENOMEM);
765 if Result = 0 then
766 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
767 pragma Assert (Result = 0);
769 Result :=
770 pthread_cond_init
771 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
772 pragma Assert (Result = 0 or else Result = ENOMEM);
773 end if;
775 if Result = 0 then
776 Succeeded := True;
777 else
778 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
779 pragma Assert (Result = 0);
780 Succeeded := False;
781 end if;
783 Result := pthread_condattr_destroy (Cond_Attr'Access);
784 pragma Assert (Result = 0);
785 end Initialize_TCB;
787 -----------------
788 -- Create_Task --
789 -----------------
791 procedure Create_Task
792 (T : Task_Id;
793 Wrapper : System.Address;
794 Stack_Size : System.Parameters.Size_Type;
795 Priority : System.Any_Priority;
796 Succeeded : out Boolean)
798 Attributes : aliased pthread_attr_t;
799 Adjusted_Stack_Size : Interfaces.C.size_t;
800 Page_Size : constant Interfaces.C.size_t :=
801 Interfaces.C.size_t (Get_Page_Size);
802 Result : Interfaces.C.int;
804 function Thread_Body_Access is new
805 Ada.Unchecked_Conversion (System.Address, Thread_Body);
807 use System.Task_Info;
809 begin
810 Adjusted_Stack_Size :=
811 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
813 if Stack_Base_Available then
815 -- If Stack Checking is supported then allocate 2 additional pages:
817 -- In the worst case, stack is allocated at something like
818 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
819 -- to be sure the effective stack size is greater than what
820 -- has been asked.
822 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
823 end if;
825 -- Round stack size as this is required by some OSes (Darwin)
827 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
828 Adjusted_Stack_Size :=
829 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
831 Result := pthread_attr_init (Attributes'Access);
832 pragma Assert (Result = 0 or else Result = ENOMEM);
834 if Result /= 0 then
835 Succeeded := False;
836 return;
837 end if;
839 Result :=
840 pthread_attr_setdetachstate
841 (Attributes'Access, PTHREAD_CREATE_DETACHED);
842 pragma Assert (Result = 0);
844 Result :=
845 pthread_attr_setstacksize
846 (Attributes'Access, Adjusted_Stack_Size);
847 pragma Assert (Result = 0);
849 if T.Common.Task_Info /= Default_Scope then
850 case T.Common.Task_Info is
851 when System.Task_Info.Process_Scope =>
852 Result :=
853 pthread_attr_setscope
854 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
856 when System.Task_Info.System_Scope =>
857 Result :=
858 pthread_attr_setscope
859 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
861 when System.Task_Info.Default_Scope =>
862 Result := 0;
863 end case;
865 pragma Assert (Result = 0);
866 end if;
868 -- Since the initial signal mask of a thread is inherited from the
869 -- creator, and the Environment task has all its signals masked, we
870 -- do not need to manipulate caller's signal mask at this point.
871 -- All tasks in RTS will have All_Tasks_Mask initially.
873 -- Note: the use of Unrestricted_Access in the following call is needed
874 -- because otherwise we have an error of getting a access-to-volatile
875 -- value which points to a non-volatile object. But in this case it is
876 -- safe to do this, since we know we have no problems with aliasing and
877 -- Unrestricted_Access bypasses this check.
879 Result := pthread_create
880 (T.Common.LL.Thread'Unrestricted_Access,
881 Attributes'Access,
882 Thread_Body_Access (Wrapper),
883 To_Address (T));
884 pragma Assert (Result = 0 or else Result = EAGAIN);
886 Succeeded := Result = 0;
888 Result := pthread_attr_destroy (Attributes'Access);
889 pragma Assert (Result = 0);
891 if Succeeded then
892 Set_Priority (T, Priority);
893 end if;
894 end Create_Task;
896 ------------------
897 -- Finalize_TCB --
898 ------------------
900 procedure Finalize_TCB (T : Task_Id) is
901 Result : Interfaces.C.int;
903 begin
904 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
905 pragma Assert (Result = 0);
907 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
908 pragma Assert (Result = 0);
910 if T.Known_Tasks_Index /= -1 then
911 Known_Tasks (T.Known_Tasks_Index) := null;
912 end if;
914 ATCB_Allocation.Free_ATCB (T);
915 end Finalize_TCB;
917 ---------------
918 -- Exit_Task --
919 ---------------
921 procedure Exit_Task is
922 begin
923 -- Mark this task as unknown, so that if Self is called, it won't
924 -- return a dangling pointer.
926 Specific.Set (null);
927 end Exit_Task;
929 ----------------
930 -- Abort_Task --
931 ----------------
933 procedure Abort_Task (T : Task_Id) is
934 Result : Interfaces.C.int;
935 begin
936 if Abort_Handler_Installed then
937 Result :=
938 pthread_kill
939 (T.Common.LL.Thread,
940 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
941 pragma Assert (Result = 0);
942 end if;
943 end Abort_Task;
945 ----------------
946 -- Initialize --
947 ----------------
949 procedure Initialize (S : in out Suspension_Object) is
950 Mutex_Attr : aliased pthread_mutexattr_t;
951 Cond_Attr : aliased pthread_condattr_t;
952 Result : Interfaces.C.int;
954 begin
955 -- Initialize internal state (always to False (RM D.10 (6)))
957 S.State := False;
958 S.Waiting := False;
960 -- Initialize internal mutex
962 Result := pthread_mutexattr_init (Mutex_Attr'Access);
963 pragma Assert (Result = 0 or else Result = ENOMEM);
965 if Result = ENOMEM then
966 raise Storage_Error;
967 end if;
969 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
970 pragma Assert (Result = 0 or else Result = ENOMEM);
972 if Result = ENOMEM then
973 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
974 pragma Assert (Result = 0);
976 raise Storage_Error;
977 end if;
979 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
980 pragma Assert (Result = 0);
982 -- Initialize internal condition variable
984 Result := pthread_condattr_init (Cond_Attr'Access);
985 pragma Assert (Result = 0 or else Result = ENOMEM);
987 if Result /= 0 then
988 Result := pthread_mutex_destroy (S.L'Access);
989 pragma Assert (Result = 0);
991 -- Storage_Error is propagated as intended if the allocation of the
992 -- underlying OS entities fails.
994 raise Storage_Error;
996 else
997 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
998 pragma Assert (Result = 0);
999 end if;
1001 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1002 pragma Assert (Result = 0 or else Result = ENOMEM);
1004 if Result /= 0 then
1005 Result := pthread_mutex_destroy (S.L'Access);
1006 pragma Assert (Result = 0);
1008 Result := pthread_condattr_destroy (Cond_Attr'Access);
1009 pragma Assert (Result = 0);
1011 -- Storage_Error is propagated as intended if the allocation of the
1012 -- underlying OS entities fails.
1014 raise Storage_Error;
1015 end if;
1017 Result := pthread_condattr_destroy (Cond_Attr'Access);
1018 pragma Assert (Result = 0);
1019 end Initialize;
1021 --------------
1022 -- Finalize --
1023 --------------
1025 procedure Finalize (S : in out Suspension_Object) is
1026 Result : Interfaces.C.int;
1028 begin
1029 -- Destroy internal mutex
1031 Result := pthread_mutex_destroy (S.L'Access);
1032 pragma Assert (Result = 0);
1034 -- Destroy internal condition variable
1036 Result := pthread_cond_destroy (S.CV'Access);
1037 pragma Assert (Result = 0);
1038 end Finalize;
1040 -------------------
1041 -- Current_State --
1042 -------------------
1044 function Current_State (S : Suspension_Object) return Boolean is
1045 begin
1046 -- We do not want to use lock on this read operation. State is marked
1047 -- as Atomic so that we ensure that the value retrieved is correct.
1049 return S.State;
1050 end Current_State;
1052 ---------------
1053 -- Set_False --
1054 ---------------
1056 procedure Set_False (S : in out Suspension_Object) is
1057 Result : Interfaces.C.int;
1059 begin
1060 SSL.Abort_Defer.all;
1062 Result := pthread_mutex_lock (S.L'Access);
1063 pragma Assert (Result = 0);
1065 S.State := False;
1067 Result := pthread_mutex_unlock (S.L'Access);
1068 pragma Assert (Result = 0);
1070 SSL.Abort_Undefer.all;
1071 end Set_False;
1073 --------------
1074 -- Set_True --
1075 --------------
1077 procedure Set_True (S : in out Suspension_Object) is
1078 Result : Interfaces.C.int;
1080 begin
1081 SSL.Abort_Defer.all;
1083 Result := pthread_mutex_lock (S.L'Access);
1084 pragma Assert (Result = 0);
1086 -- If there is already a task waiting on this suspension object then
1087 -- we resume it, leaving the state of the suspension object to False,
1088 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1089 -- the state to True.
1091 if S.Waiting then
1092 S.Waiting := False;
1093 S.State := False;
1095 Result := pthread_cond_signal (S.CV'Access);
1096 pragma Assert (Result = 0);
1098 else
1099 S.State := True;
1100 end if;
1102 Result := pthread_mutex_unlock (S.L'Access);
1103 pragma Assert (Result = 0);
1105 SSL.Abort_Undefer.all;
1106 end Set_True;
1108 ------------------------
1109 -- Suspend_Until_True --
1110 ------------------------
1112 procedure Suspend_Until_True (S : in out Suspension_Object) is
1113 Result : Interfaces.C.int;
1115 begin
1116 SSL.Abort_Defer.all;
1118 Result := pthread_mutex_lock (S.L'Access);
1119 pragma Assert (Result = 0);
1121 if S.Waiting then
1123 -- Program_Error must be raised upon calling Suspend_Until_True
1124 -- if another task is already waiting on that suspension object
1125 -- (RM D.10(10)).
1127 Result := pthread_mutex_unlock (S.L'Access);
1128 pragma Assert (Result = 0);
1130 SSL.Abort_Undefer.all;
1132 raise Program_Error;
1134 else
1135 -- Suspend the task if the state is False. Otherwise, the task
1136 -- continues its execution, and the state of the suspension object
1137 -- is set to False (ARM D.10 par. 9).
1139 if S.State then
1140 S.State := False;
1141 else
1142 S.Waiting := True;
1144 loop
1145 -- Loop in case pthread_cond_wait returns earlier than expected
1146 -- (e.g. in case of EINTR caused by a signal).
1148 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1149 pragma Assert (Result = 0 or else Result = EINTR);
1151 exit when not S.Waiting;
1152 end loop;
1153 end if;
1155 Result := pthread_mutex_unlock (S.L'Access);
1156 pragma Assert (Result = 0);
1158 SSL.Abort_Undefer.all;
1159 end if;
1160 end Suspend_Until_True;
1162 ----------------
1163 -- Check_Exit --
1164 ----------------
1166 -- Dummy version
1168 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1169 pragma Unreferenced (Self_ID);
1170 begin
1171 return True;
1172 end Check_Exit;
1174 --------------------
1175 -- Check_No_Locks --
1176 --------------------
1178 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1179 pragma Unreferenced (Self_ID);
1180 begin
1181 return True;
1182 end Check_No_Locks;
1184 ----------------------
1185 -- Environment_Task --
1186 ----------------------
1188 function Environment_Task return Task_Id is
1189 begin
1190 return Environment_Task_Id;
1191 end Environment_Task;
1193 --------------
1194 -- Lock_RTS --
1195 --------------
1197 procedure Lock_RTS is
1198 begin
1199 Write_Lock (Single_RTS_Lock'Access);
1200 end Lock_RTS;
1202 ----------------
1203 -- Unlock_RTS --
1204 ----------------
1206 procedure Unlock_RTS is
1207 begin
1208 Unlock (Single_RTS_Lock'Access);
1209 end Unlock_RTS;
1211 ------------------
1212 -- Suspend_Task --
1213 ------------------
1215 function Suspend_Task
1216 (T : ST.Task_Id;
1217 Thread_Self : Thread_Id) return Boolean
1219 pragma Unreferenced (T, Thread_Self);
1220 begin
1221 return False;
1222 end Suspend_Task;
1224 -----------------
1225 -- Resume_Task --
1226 -----------------
1228 function Resume_Task
1229 (T : ST.Task_Id;
1230 Thread_Self : Thread_Id) return Boolean
1232 pragma Unreferenced (T, Thread_Self);
1233 begin
1234 return False;
1235 end Resume_Task;
1237 --------------------
1238 -- Stop_All_Tasks --
1239 --------------------
1241 procedure Stop_All_Tasks is
1242 begin
1243 null;
1244 end Stop_All_Tasks;
1246 ---------------
1247 -- Stop_Task --
1248 ---------------
1250 function Stop_Task (T : ST.Task_Id) return Boolean is
1251 pragma Unreferenced (T);
1252 begin
1253 return False;
1254 end Stop_Task;
1256 -------------------
1257 -- Continue_Task --
1258 -------------------
1260 function Continue_Task (T : ST.Task_Id) return Boolean is
1261 pragma Unreferenced (T);
1262 begin
1263 return False;
1264 end Continue_Task;
1266 ----------------
1267 -- Initialize --
1268 ----------------
1270 procedure Initialize (Environment_Task : Task_Id) is
1271 act : aliased struct_sigaction;
1272 old_act : aliased struct_sigaction;
1273 Tmp_Set : aliased sigset_t;
1274 Result : Interfaces.C.int;
1276 function State
1277 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1278 pragma Import (C, State, "__gnat_get_interrupt_state");
1279 -- Get interrupt state. Defined in a-init.c
1280 -- The input argument is the interrupt number,
1281 -- and the result is one of the following:
1283 Default : constant Character := 's';
1284 -- 'n' this interrupt not set by any Interrupt_State pragma
1285 -- 'u' Interrupt_State pragma set state to User
1286 -- 'r' Interrupt_State pragma set state to Runtime
1287 -- 's' Interrupt_State pragma set state to System (use "default"
1288 -- system handler)
1290 begin
1291 Environment_Task_Id := Environment_Task;
1293 Interrupt_Management.Initialize;
1295 -- Initialize the lock used to synchronize chain of all ATCBs
1297 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1299 Specific.Initialize (Environment_Task);
1301 if Use_Alternate_Stack then
1302 Environment_Task.Common.Task_Alternate_Stack :=
1303 Alternate_Stack'Address;
1304 end if;
1306 -- Make environment task known here because it doesn't go through
1307 -- Activate_Tasks, which does it for all other tasks.
1309 Known_Tasks (Known_Tasks'First) := Environment_Task;
1310 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1312 Enter_Task (Environment_Task);
1314 if State
1315 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1316 then
1317 act.sa_flags := 0;
1318 act.sa_handler := Abort_Handler'Address;
1320 Result := sigemptyset (Tmp_Set'Access);
1321 pragma Assert (Result = 0);
1322 act.sa_mask := Tmp_Set;
1324 Result :=
1325 sigaction
1326 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1327 act'Unchecked_Access,
1328 old_act'Unchecked_Access);
1329 pragma Assert (Result = 0);
1330 Abort_Handler_Installed := True;
1331 end if;
1332 end Initialize;
1334 -----------------------
1335 -- Set_Task_Affinity --
1336 -----------------------
1338 procedure Set_Task_Affinity (T : ST.Task_Id) is
1339 pragma Unreferenced (T);
1341 begin
1342 -- Setting task affinity is not supported by the underlying system
1344 null;
1345 end Set_Task_Affinity;
1347 end System.Task_Primitives.Operations;