[AArch64] Improve scheduling model for X-Gene
[official-gcc.git] / gcc / ada / libgnarl / s-taprop__qnx.adb
blob4ec033046c5e6b5341ef22932f62d4bbe0e168fe
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-2017, 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 POSIX-like version of this package
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
37 -- Note: this file can only be used for POSIX compliant systems that implement
38 -- SCHED_FIFO and Ceiling Locking correctly.
40 -- For configurations where SCHED_FIFO and priority ceiling are not a
41 -- requirement, this file can also be used (e.g AiX threads)
43 pragma Polling (Off);
44 -- Turn off polling, we do not want ATC polling to take place during tasking
45 -- operations. It causes infinite loops and other problems.
47 with Ada.Unchecked_Conversion;
49 with Interfaces.C;
51 with System.Tasking.Debug;
52 with System.Interrupt_Management;
53 with System.OS_Constants;
54 with System.OS_Primitives;
55 with System.Task_Info;
57 with System.Soft_Links;
58 -- We use System.Soft_Links instead of System.Tasking.Initialization
59 -- because the later is a higher level package that we shouldn't depend on.
60 -- For example when using the restricted run time, it is replaced by
61 -- System.Tasking.Restricted.Stages.
63 package body System.Task_Primitives.Operations is
65 package OSC renames System.OS_Constants;
66 package SSL renames System.Soft_Links;
68 use System.Tasking.Debug;
69 use System.Tasking;
70 use Interfaces.C;
71 use System.OS_Interface;
72 use System.Parameters;
73 use System.OS_Primitives;
75 ----------------
76 -- Local Data --
77 ----------------
79 -- The followings are logically constants, but need to be initialized
80 -- at run time.
82 Single_RTS_Lock : aliased RTS_Lock;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 Environment_Task_Id : Task_Id;
88 -- A variable to hold Task_Id for the environment task
90 Locking_Policy : Character;
91 pragma Import (C, Locking_Policy, "__gl_locking_policy");
92 -- Value of the pragma Locking_Policy:
93 -- 'C' for Ceiling_Locking
94 -- 'I' for Inherit_Locking
95 -- ' ' for none.
97 Unblocked_Signal_Mask : aliased sigset_t;
98 -- The set of signals that should unblocked in all tasks
100 -- The followings are internal configuration constants needed
102 Next_Serial_Number : Task_Serial_Number := 100;
103 -- We start at 100, to reserve some special values for
104 -- using in error checking.
106 Time_Slice_Val : Integer;
107 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
109 Dispatching_Policy : Character;
110 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
112 Foreign_Task_Elaborated : aliased Boolean := True;
113 -- Used to identified fake tasks (i.e., non-Ada Threads)
115 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
116 -- Whether to use an alternate signal stack for stack overflows
118 Abort_Handler_Installed : Boolean := False;
119 -- True if a handler for the abort signal is installed
121 type RTS_Lock_Ptr is not null access all RTS_Lock;
123 function Init_Mutex (L : RTS_Lock_Ptr; Prio : Any_Priority) return int;
124 -- Initialize the mutex L. If Ceiling_Support is True, then set the ceiling
125 -- to Prio. Returns 0 for success, or ENOMEM for out-of-memory.
127 function Get_Policy (Prio : System.Any_Priority) return Character;
128 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
129 -- Get priority specific dispatching policy
131 --------------------
132 -- Local Packages --
133 --------------------
135 package Specific is
137 procedure Initialize (Environment_Task : Task_Id);
138 pragma Inline (Initialize);
139 -- Initialize various data needed by this package
141 function Is_Valid_Task return Boolean;
142 pragma Inline (Is_Valid_Task);
143 -- Does executing thread have a TCB?
145 procedure Set (Self_Id : Task_Id);
146 pragma Inline (Set);
147 -- Set the self id for the current task
149 function Self return Task_Id;
150 pragma Inline (Self);
151 -- Return a pointer to the Ada Task Control Block of the calling task
153 end Specific;
155 package body Specific is separate;
156 -- The body of this package is target specific
158 package Monotonic is
160 function Monotonic_Clock return Duration;
161 pragma Inline (Monotonic_Clock);
162 -- Returns an absolute time, represented as an offset relative to some
163 -- unspecified starting point, typically system boot time. This clock
164 -- is not affected by discontinuous jumps in the system time.
166 function RT_Resolution return Duration;
167 pragma Inline (RT_Resolution);
168 -- Returns resolution of the underlying clock used to implement RT_Clock
170 procedure Timed_Sleep
171 (Self_ID : ST.Task_Id;
172 Time : Duration;
173 Mode : ST.Delay_Modes;
174 Reason : System.Tasking.Task_States;
175 Timedout : out Boolean;
176 Yielded : out Boolean);
177 -- Combination of Sleep (above) and Timed_Delay
179 procedure Timed_Delay
180 (Self_ID : ST.Task_Id;
181 Time : Duration;
182 Mode : ST.Delay_Modes);
183 -- Implement the semantics of the delay statement.
184 -- The caller should be abort-deferred and should not hold any locks.
186 end Monotonic;
188 package body Monotonic is separate;
190 ----------------------------------
191 -- ATCB allocation/deallocation --
192 ----------------------------------
194 package body ATCB_Allocation is separate;
195 -- The body of this package is shared across several targets
197 ---------------------------------
198 -- Support for foreign threads --
199 ---------------------------------
201 function Register_Foreign_Thread
202 (Thread : Thread_Id;
203 Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id;
204 -- Allocate and initialize a new ATCB for the current Thread. The size of
205 -- the secondary stack can be optionally specified.
207 function Register_Foreign_Thread
208 (Thread : Thread_Id;
209 Sec_Stack_Size : Size_Type := Unspecified_Size)
210 return Task_Id is separate;
212 -----------------------
213 -- Local Subprograms --
214 -----------------------
216 procedure Abort_Handler (Sig : Signal);
217 -- Signal handler used to implement asynchronous abort.
218 -- See also comment before body, below.
220 function To_Address is
221 new Ada.Unchecked_Conversion (Task_Id, System.Address);
223 function GNAT_pthread_condattr_setup
224 (attr : access pthread_condattr_t) return int;
225 pragma Import (C,
226 GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup");
228 -------------------
229 -- Abort_Handler --
230 -------------------
232 -- Target-dependent binding of inter-thread Abort signal to the raising of
233 -- the Abort_Signal exception.
235 -- The technical issues and alternatives here are essentially the
236 -- same as for raising exceptions in response to other signals
237 -- (e.g. Storage_Error). See code and comments in the package body
238 -- System.Interrupt_Management.
240 -- Some implementations may not allow an exception to be propagated out of
241 -- a handler, and others might leave the signal or interrupt that invoked
242 -- this handler masked after the exceptional return to the application
243 -- code.
245 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
246 -- most UNIX systems, this will allow transfer out of a signal handler,
247 -- which is usually the only mechanism available for implementing
248 -- asynchronous handlers of this kind. However, some systems do not
249 -- restore the signal mask on longjmp(), leaving the abort signal masked.
251 procedure Abort_Handler (Sig : Signal) is
252 pragma Unreferenced (Sig);
254 T : constant Task_Id := Self;
255 Old_Set : aliased sigset_t;
257 Result : Interfaces.C.int;
258 pragma Warnings (Off, Result);
260 begin
261 -- It's not safe to raise an exception when using GCC ZCX mechanism.
262 -- Note that we still need to install a signal handler, since in some
263 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
264 -- need to send the Abort signal to a task.
266 if ZCX_By_Default then
267 return;
268 end if;
270 if T.Deferral_Level = 0
271 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
272 not T.Aborting
273 then
274 T.Aborting := True;
276 -- Make sure signals used for RTS internal purpose are unmasked
278 Result := pthread_sigmask (SIG_UNBLOCK,
279 Unblocked_Signal_Mask'Access, Old_Set'Access);
280 pragma Assert (Result = 0);
282 raise Standard'Abort_Signal;
283 end if;
284 end Abort_Handler;
286 -----------------
287 -- Stack_Guard --
288 -----------------
290 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
291 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
292 Page_Size : Address;
293 Res : Interfaces.C.int;
295 begin
296 if Stack_Base_Available then
298 -- Compute the guard page address
300 Page_Size := Address (Get_Page_Size);
301 Res :=
302 mprotect
303 (Stack_Base - (Stack_Base mod Page_Size) + Page_Size,
304 size_t (Page_Size),
305 prot => (if On then PROT_ON else PROT_OFF));
306 pragma Assert (Res = 0);
307 end if;
308 end Stack_Guard;
310 --------------------
311 -- Get_Thread_Id --
312 --------------------
314 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
315 begin
316 return T.Common.LL.Thread;
317 end Get_Thread_Id;
319 ----------
320 -- Self --
321 ----------
323 function Self return Task_Id renames Specific.Self;
325 ----------------
326 -- Init_Mutex --
327 ----------------
329 function Init_Mutex (L : RTS_Lock_Ptr; Prio : Any_Priority) return int
331 Attributes : aliased pthread_mutexattr_t;
332 Result : int;
333 Result_2 : aliased int;
335 begin
336 Result := pthread_mutexattr_init (Attributes'Access);
337 pragma Assert (Result = 0 or else Result = ENOMEM);
339 if Result = ENOMEM then
340 return Result;
341 end if;
343 if Locking_Policy = 'C' then
344 Result := pthread_mutexattr_setprotocol
345 (Attributes'Access, PTHREAD_PRIO_PROTECT);
346 pragma Assert (Result = 0);
348 Result := pthread_mutexattr_getprotocol
349 (Attributes'Access, Result_2'Access);
350 if Result_2 /= PTHREAD_PRIO_PROTECT then
351 raise Program_Error with "setprotocol failed";
352 end if;
354 Result := pthread_mutexattr_setprioceiling
355 (Attributes'Access, To_Target_Priority (Prio));
356 pragma Assert (Result = 0);
358 elsif Locking_Policy = 'I' then
359 Result := pthread_mutexattr_setprotocol
360 (Attributes'Access, PTHREAD_PRIO_INHERIT);
361 pragma Assert (Result = 0);
362 end if;
364 Result := pthread_mutex_init (L, Attributes'Access);
365 pragma Assert (Result = 0 or else Result = ENOMEM);
367 Result_2 := pthread_mutexattr_destroy (Attributes'Access);
368 pragma Assert (Result_2 = 0);
370 return Result;
371 end Init_Mutex;
373 ---------------------
374 -- Initialize_Lock --
375 ---------------------
377 -- Note: mutexes and cond_variables needed per-task basis are initialized
378 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
379 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
380 -- status change of RTS. Therefore raising Storage_Error in the following
381 -- routines should be able to be handled safely.
383 procedure Initialize_Lock
384 (Prio : System.Any_Priority;
385 L : not null access Lock)
387 begin
388 if Init_Mutex (L.WO'Access, Prio) = ENOMEM then
389 raise Storage_Error with "Failed to allocate a lock";
390 end if;
391 end Initialize_Lock;
393 procedure Initialize_Lock
394 (L : not null access RTS_Lock; Level : Lock_Level)
396 pragma Unreferenced (Level);
398 begin
399 if Init_Mutex (L.all'Access, Any_Priority'Last) = ENOMEM then
400 raise Storage_Error with "Failed to allocate a lock";
401 end if;
402 end Initialize_Lock;
404 -------------------
405 -- Finalize_Lock --
406 -------------------
408 procedure Finalize_Lock (L : not null access Lock) is
409 Result : Interfaces.C.int;
410 begin
411 Result := pthread_mutex_destroy (L.WO'Access);
412 pragma Assert (Result = 0);
413 end Finalize_Lock;
415 procedure Finalize_Lock (L : not null access RTS_Lock) is
416 Result : Interfaces.C.int;
417 begin
418 Result := pthread_mutex_destroy (L);
419 pragma Assert (Result = 0);
420 end Finalize_Lock;
422 ----------------
423 -- Write_Lock --
424 ----------------
426 procedure Write_Lock
427 (L : not null access Lock; Ceiling_Violation : out Boolean)
429 Self : constant pthread_t := pthread_self;
430 Result : int;
431 Policy : aliased int;
432 Ceiling : aliased int;
433 Sched : aliased struct_sched_param;
435 begin
436 Result := pthread_mutex_lock (L.WO'Access);
438 -- The cause of EINVAL is a priority ceiling violation
440 Ceiling_Violation := Result = EINVAL;
441 pragma Assert (Result = 0 or else Ceiling_Violation);
443 -- Workaround bug in QNX on ceiling locks: tasks with priority higher
444 -- than the ceiling priority don't receive EINVAL upon trying to lock.
445 if Result = 0 then
446 Result := pthread_getschedparam (Self, Policy'Access, Sched'Access);
447 pragma Assert (Result = 0);
448 Result := pthread_mutex_getprioceiling (L.WO'Access, Ceiling'Access);
449 pragma Assert (Result = 0);
451 -- Ceiling = 0 means no Ceiling Priority policy is set on this mutex
452 -- Else, Ceiling < current priority means Ceiling violation
453 -- (otherwise the current priority == ceiling)
454 if Ceiling > 0 and then Ceiling < Sched.sched_curpriority then
455 Ceiling_Violation := True;
456 Result := pthread_mutex_unlock (L.WO'Access);
457 pragma Assert (Result = 0);
458 end if;
459 end if;
460 end Write_Lock;
462 procedure Write_Lock
463 (L : not null access RTS_Lock;
464 Global_Lock : Boolean := False)
466 Result : Interfaces.C.int;
467 begin
468 if not Single_Lock or else Global_Lock then
469 Result := pthread_mutex_lock (L);
470 pragma Assert (Result = 0);
471 end if;
472 end Write_Lock;
474 procedure Write_Lock (T : Task_Id) is
475 Result : Interfaces.C.int;
476 begin
477 if not Single_Lock then
478 Result := pthread_mutex_lock (T.Common.LL.L'Access);
479 pragma Assert (Result = 0);
480 end if;
481 end Write_Lock;
483 ---------------
484 -- Read_Lock --
485 ---------------
487 procedure Read_Lock
488 (L : not null access Lock; Ceiling_Violation : out Boolean) is
489 begin
490 Write_Lock (L, Ceiling_Violation);
491 end Read_Lock;
493 ------------
494 -- Unlock --
495 ------------
497 procedure Unlock (L : not null access Lock) is
498 Result : Interfaces.C.int;
499 begin
500 Result := pthread_mutex_unlock (L.WO'Access);
501 pragma Assert (Result = 0);
502 end Unlock;
504 procedure Unlock
505 (L : not null access RTS_Lock; Global_Lock : Boolean := False)
507 Result : Interfaces.C.int;
508 begin
509 if not Single_Lock or else Global_Lock then
510 Result := pthread_mutex_unlock (L);
511 pragma Assert (Result = 0);
512 end if;
513 end Unlock;
515 procedure Unlock (T : Task_Id) is
516 Result : Interfaces.C.int;
517 begin
518 if not Single_Lock then
519 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
520 pragma Assert (Result = 0);
521 end if;
522 end Unlock;
524 -----------------
525 -- Set_Ceiling --
526 -----------------
528 procedure Set_Ceiling
529 (L : not null access Lock;
530 Prio : System.Any_Priority)
532 Result : Interfaces.C.int;
533 begin
534 Result := pthread_mutex_setprioceiling
535 (L.WO'Access, To_Target_Priority (Prio), null);
536 pragma Assert (Result = 0);
537 end Set_Ceiling;
539 -----------
540 -- Sleep --
541 -----------
543 procedure Sleep
544 (Self_ID : Task_Id;
545 Reason : System.Tasking.Task_States)
547 pragma Unreferenced (Reason);
549 Result : Interfaces.C.int;
551 begin
552 Result :=
553 pthread_cond_wait
554 (cond => Self_ID.Common.LL.CV'Access,
555 mutex => (if Single_Lock
556 then Single_RTS_Lock'Access
557 else Self_ID.Common.LL.L'Access));
559 -- EINTR is not considered a failure
561 pragma Assert (Result = 0 or else Result = EINTR);
562 end Sleep;
564 -----------------
565 -- Timed_Sleep --
566 -----------------
568 -- This is for use within the run-time system, so abort is
569 -- assumed to be already deferred, and the caller should be
570 -- holding its own ATCB lock.
572 procedure Timed_Sleep
573 (Self_ID : Task_Id;
574 Time : Duration;
575 Mode : ST.Delay_Modes;
576 Reason : Task_States;
577 Timedout : out Boolean;
578 Yielded : out Boolean) renames Monotonic.Timed_Sleep;
580 -----------------
581 -- Timed_Delay --
582 -----------------
584 -- This is for use in implementing delay statements, so we assume the
585 -- caller is abort-deferred but is holding no locks.
587 procedure Timed_Delay
588 (Self_ID : Task_Id;
589 Time : Duration;
590 Mode : ST.Delay_Modes) renames Monotonic.Timed_Delay;
592 ---------------------
593 -- Monotonic_Clock --
594 ---------------------
596 function Monotonic_Clock return Duration renames Monotonic.Monotonic_Clock;
598 -------------------
599 -- RT_Resolution --
600 -------------------
602 function RT_Resolution return Duration renames Monotonic.RT_Resolution;
604 ------------
605 -- Wakeup --
606 ------------
608 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
609 pragma Unreferenced (Reason);
610 Result : Interfaces.C.int;
611 begin
612 Result := pthread_cond_signal (T.Common.LL.CV'Access);
613 pragma Assert (Result = 0);
614 end Wakeup;
616 -----------
617 -- Yield --
618 -----------
620 procedure Yield (Do_Yield : Boolean := True) is
621 Result : Interfaces.C.int;
622 pragma Unreferenced (Result);
623 begin
624 if Do_Yield then
625 Result := sched_yield;
626 end if;
627 end Yield;
629 ------------------
630 -- Set_Priority --
631 ------------------
633 procedure Set_Priority
634 (T : Task_Id;
635 Prio : System.Any_Priority;
636 Loss_Of_Inheritance : Boolean := False)
638 pragma Unreferenced (Loss_Of_Inheritance);
639 Result : Interfaces.C.int;
640 Old : constant System.Any_Priority := T.Common.Current_Priority;
642 begin
643 T.Common.Current_Priority := Prio;
644 Result := pthread_setschedprio
645 (T.Common.LL.Thread, To_Target_Priority (Prio));
646 pragma Assert (Result = 0);
648 if T.Common.LL.Thread = pthread_self
649 and then Old > Prio
650 then
651 -- When lowering the priority via a pthread_setschedprio, QNX ensures
652 -- that the running thread remains in the head of the FIFO for tne
653 -- new priority. Annex D expects the thread to be requeued so let's
654 -- yield to the other threads of the same priority.
655 Result := sched_yield;
656 pragma Assert (Result = 0);
657 end if;
658 end Set_Priority;
660 ------------------
661 -- Get_Priority --
662 ------------------
664 function Get_Priority (T : Task_Id) return System.Any_Priority is
665 begin
666 return T.Common.Current_Priority;
667 end Get_Priority;
669 ----------------
670 -- Enter_Task --
671 ----------------
673 procedure Enter_Task (Self_ID : Task_Id) is
674 begin
675 Self_ID.Common.LL.Thread := pthread_self;
676 Self_ID.Common.LL.LWP := lwp_self;
678 Specific.Set (Self_ID);
680 if Use_Alternate_Stack then
681 declare
682 Stack : aliased stack_t;
683 Result : Interfaces.C.int;
684 begin
685 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
686 Stack.ss_size := Alternate_Stack_Size;
687 Stack.ss_flags := 0;
688 Result := sigaltstack (Stack'Access, null);
689 pragma Assert (Result = 0);
690 end;
691 end if;
692 end Enter_Task;
694 -------------------
695 -- Is_Valid_Task --
696 -------------------
698 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
700 -----------------------------
701 -- Register_Foreign_Thread --
702 -----------------------------
704 function Register_Foreign_Thread return Task_Id is
705 begin
706 if Is_Valid_Task then
707 return Self;
708 else
709 return Register_Foreign_Thread (pthread_self);
710 end if;
711 end Register_Foreign_Thread;
713 --------------------
714 -- Initialize_TCB --
715 --------------------
717 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean)
719 Result : Interfaces.C.int;
720 Cond_Attr : aliased pthread_condattr_t;
722 begin
723 -- Give the task a unique serial number
725 Self_ID.Serial_Number := Next_Serial_Number;
726 Next_Serial_Number := Next_Serial_Number + 1;
727 pragma Assert (Next_Serial_Number /= 0);
729 if not Single_Lock then
730 Result := Init_Mutex (Self_ID.Common.LL.L'Access, Any_Priority'Last);
731 pragma Assert (Result = 0);
733 if Result /= 0 then
734 Succeeded := False;
735 return;
736 end if;
737 end if;
739 Result := pthread_condattr_init (Cond_Attr'Access);
740 pragma Assert (Result = 0 or else Result = ENOMEM);
742 if Result = 0 then
743 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
744 pragma Assert (Result = 0);
746 Result :=
747 pthread_cond_init
748 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
749 pragma Assert (Result = 0 or else Result = ENOMEM);
750 end if;
752 if Result = 0 then
753 Succeeded := True;
754 else
755 if not Single_Lock then
756 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
757 pragma Assert (Result = 0);
758 end if;
760 Succeeded := False;
761 end if;
763 Result := pthread_condattr_destroy (Cond_Attr'Access);
764 pragma Assert (Result = 0);
765 end Initialize_TCB;
767 -----------------
768 -- Create_Task --
769 -----------------
771 procedure Create_Task
772 (T : Task_Id;
773 Wrapper : System.Address;
774 Stack_Size : System.Parameters.Size_Type;
775 Priority : System.Any_Priority;
776 Succeeded : out Boolean)
778 Attributes : aliased pthread_attr_t;
779 Adjusted_Stack_Size : Interfaces.C.size_t;
780 Page_Size : constant Interfaces.C.size_t :=
781 Interfaces.C.size_t (Get_Page_Size);
782 Sched_Param : aliased struct_sched_param;
783 Result : Interfaces.C.int;
785 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
786 -- Upper case first character of the policy name corresponding to the
787 -- task as set by a Priority_Specific_Dispatching pragma.
789 function Thread_Body_Access is new
790 Ada.Unchecked_Conversion (System.Address, Thread_Body);
792 begin
793 Adjusted_Stack_Size :=
794 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
796 if Stack_Base_Available then
798 -- If Stack Checking is supported then allocate 2 additional pages:
800 -- In the worst case, stack is allocated at something like
801 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
802 -- to be sure the effective stack size is greater than what
803 -- has been asked.
805 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
806 end if;
808 -- Round stack size as this is required by some OSes (Darwin)
810 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
811 Adjusted_Stack_Size :=
812 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
814 Result := pthread_attr_init (Attributes'Access);
815 pragma Assert (Result = 0 or else Result = ENOMEM);
817 if Result /= 0 then
818 Succeeded := False;
819 return;
820 end if;
822 Result :=
823 pthread_attr_setdetachstate
824 (Attributes'Access, PTHREAD_CREATE_DETACHED);
825 pragma Assert (Result = 0);
827 Result :=
828 pthread_attr_setstacksize
829 (Attributes'Access, Adjusted_Stack_Size);
830 pragma Assert (Result = 0);
832 -- Set thread priority
833 T.Common.Current_Priority := Priority;
834 Sched_Param.sched_priority := To_Target_Priority (Priority);
836 Result := pthread_attr_setinheritsched
837 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
838 pragma Assert (Result = 0);
840 Result := pthread_attr_setschedparam
841 (Attributes'Access, Sched_Param'Access);
842 pragma Assert (Result = 0);
844 if Time_Slice_Supported
845 and then (Dispatching_Policy = 'R'
846 or else Priority_Specific_Policy = 'R'
847 or else Time_Slice_Val > 0)
848 then
849 Result := pthread_attr_setschedpolicy
850 (Attributes'Access, SCHED_RR);
852 elsif Dispatching_Policy = 'F'
853 or else Priority_Specific_Policy = 'F'
854 or else Time_Slice_Val = 0
855 then
856 Result := pthread_attr_setschedpolicy
857 (Attributes'Access, SCHED_FIFO);
859 else
860 Result := pthread_attr_setschedpolicy
861 (Attributes'Access, SCHED_OTHER);
862 end if;
864 pragma Assert (Result = 0);
866 -- Since the initial signal mask of a thread is inherited from the
867 -- creator, and the Environment task has all its signals masked, we
868 -- do not need to manipulate caller's signal mask at this point.
869 -- All tasks in RTS will have All_Tasks_Mask initially.
871 -- Note: the use of Unrestricted_Access in the following call is needed
872 -- because otherwise we have an error of getting a access-to-volatile
873 -- value which points to a non-volatile object. But in this case it is
874 -- safe to do this, since we know we have no problems with aliasing and
875 -- Unrestricted_Access bypasses this check.
877 Result := pthread_create
878 (T.Common.LL.Thread'Unrestricted_Access,
879 Attributes'Access,
880 Thread_Body_Access (Wrapper),
881 To_Address (T));
882 pragma Assert (Result = 0 or else Result = EAGAIN);
884 Succeeded := Result = 0;
886 Result := pthread_attr_destroy (Attributes'Access);
887 pragma Assert (Result = 0);
888 end Create_Task;
890 ------------------
891 -- Finalize_TCB --
892 ------------------
894 procedure Finalize_TCB (T : Task_Id) is
895 Result : Interfaces.C.int;
897 begin
898 if not Single_Lock then
899 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
900 pragma Assert (Result = 0);
901 end if;
903 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
904 pragma Assert (Result = 0);
906 if T.Known_Tasks_Index /= -1 then
907 Known_Tasks (T.Known_Tasks_Index) := null;
908 end if;
910 ATCB_Allocation.Free_ATCB (T);
911 end Finalize_TCB;
913 ---------------
914 -- Exit_Task --
915 ---------------
917 procedure Exit_Task is
918 begin
919 -- Mark this task as unknown, so that if Self is called, it won't
920 -- return a dangling pointer.
922 Specific.Set (null);
923 end Exit_Task;
925 ----------------
926 -- Abort_Task --
927 ----------------
929 procedure Abort_Task (T : Task_Id) is
930 Result : Interfaces.C.int;
931 begin
932 if Abort_Handler_Installed then
933 Result :=
934 pthread_kill
935 (T.Common.LL.Thread,
936 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
937 pragma Assert (Result = 0);
938 end if;
939 end Abort_Task;
941 ----------------
942 -- Initialize --
943 ----------------
945 procedure Initialize (S : in out Suspension_Object) is
946 Mutex_Attr : aliased pthread_mutexattr_t;
947 Cond_Attr : aliased pthread_condattr_t;
948 Result : Interfaces.C.int;
950 begin
951 -- Initialize internal state (always to False (RM D.10 (6)))
953 S.State := False;
954 S.Waiting := False;
956 -- Initialize internal mutex
958 Result := pthread_mutexattr_init (Mutex_Attr'Access);
959 pragma Assert (Result = 0 or else Result = ENOMEM);
961 if Result = ENOMEM then
962 raise Storage_Error;
963 end if;
965 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
966 pragma Assert (Result = 0 or else Result = ENOMEM);
968 if Result = ENOMEM then
969 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
970 pragma Assert (Result = 0);
972 raise Storage_Error;
973 end if;
975 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
976 pragma Assert (Result = 0);
978 -- Initialize internal condition variable
980 Result := pthread_condattr_init (Cond_Attr'Access);
981 pragma Assert (Result = 0 or else Result = ENOMEM);
983 if Result /= 0 then
984 Result := pthread_mutex_destroy (S.L'Access);
985 pragma Assert (Result = 0);
987 -- Storage_Error is propagated as intended if the allocation of the
988 -- underlying OS entities fails.
990 raise Storage_Error;
992 else
993 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
994 pragma Assert (Result = 0);
995 end if;
997 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
998 pragma Assert (Result = 0 or else Result = ENOMEM);
1000 if Result /= 0 then
1001 Result := pthread_mutex_destroy (S.L'Access);
1002 pragma Assert (Result = 0);
1004 Result := pthread_condattr_destroy (Cond_Attr'Access);
1005 pragma Assert (Result = 0);
1007 -- Storage_Error is propagated as intended if the allocation of the
1008 -- underlying OS entities fails.
1010 raise Storage_Error;
1011 end if;
1013 Result := pthread_condattr_destroy (Cond_Attr'Access);
1014 pragma Assert (Result = 0);
1015 end Initialize;
1017 --------------
1018 -- Finalize --
1019 --------------
1021 procedure Finalize (S : in out Suspension_Object) is
1022 Result : Interfaces.C.int;
1024 begin
1025 -- Destroy internal mutex
1027 Result := pthread_mutex_destroy (S.L'Access);
1028 pragma Assert (Result = 0);
1030 -- Destroy internal condition variable
1032 Result := pthread_cond_destroy (S.CV'Access);
1033 pragma Assert (Result = 0);
1034 end Finalize;
1036 -------------------
1037 -- Current_State --
1038 -------------------
1040 function Current_State (S : Suspension_Object) return Boolean is
1041 begin
1042 -- We do not want to use lock on this read operation. State is marked
1043 -- as Atomic so that we ensure that the value retrieved is correct.
1045 return S.State;
1046 end Current_State;
1048 ---------------
1049 -- Set_False --
1050 ---------------
1052 procedure Set_False (S : in out Suspension_Object) is
1053 Result : Interfaces.C.int;
1055 begin
1056 SSL.Abort_Defer.all;
1058 Result := pthread_mutex_lock (S.L'Access);
1059 pragma Assert (Result = 0);
1061 S.State := False;
1063 Result := pthread_mutex_unlock (S.L'Access);
1064 pragma Assert (Result = 0);
1066 SSL.Abort_Undefer.all;
1067 end Set_False;
1069 --------------
1070 -- Set_True --
1071 --------------
1073 procedure Set_True (S : in out Suspension_Object) is
1074 Result : Interfaces.C.int;
1076 begin
1077 SSL.Abort_Defer.all;
1079 Result := pthread_mutex_lock (S.L'Access);
1080 pragma Assert (Result = 0);
1082 -- If there is already a task waiting on this suspension object then
1083 -- we resume it, leaving the state of the suspension object to False,
1084 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1085 -- the state to True.
1087 if S.Waiting then
1088 S.Waiting := False;
1089 S.State := False;
1091 Result := pthread_cond_signal (S.CV'Access);
1092 pragma Assert (Result = 0);
1094 else
1095 S.State := True;
1096 end if;
1098 Result := pthread_mutex_unlock (S.L'Access);
1099 pragma Assert (Result = 0);
1101 SSL.Abort_Undefer.all;
1102 end Set_True;
1104 ------------------------
1105 -- Suspend_Until_True --
1106 ------------------------
1108 procedure Suspend_Until_True (S : in out Suspension_Object) is
1109 Result : Interfaces.C.int;
1111 begin
1112 SSL.Abort_Defer.all;
1114 Result := pthread_mutex_lock (S.L'Access);
1115 pragma Assert (Result = 0);
1117 if S.Waiting then
1119 -- Program_Error must be raised upon calling Suspend_Until_True
1120 -- if another task is already waiting on that suspension object
1121 -- (RM D.10(10)).
1123 Result := pthread_mutex_unlock (S.L'Access);
1124 pragma Assert (Result = 0);
1126 SSL.Abort_Undefer.all;
1128 raise Program_Error;
1130 else
1131 -- Suspend the task if the state is False. Otherwise, the task
1132 -- continues its execution, and the state of the suspension object
1133 -- is set to False (ARM D.10 par. 9).
1135 if S.State then
1136 S.State := False;
1137 else
1138 S.Waiting := True;
1140 loop
1141 -- Loop in case pthread_cond_wait returns earlier than expected
1142 -- (e.g. in case of EINTR caused by a signal).
1144 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1145 pragma Assert (Result = 0 or else Result = EINTR);
1147 exit when not S.Waiting;
1148 end loop;
1149 end if;
1151 Result := pthread_mutex_unlock (S.L'Access);
1152 pragma Assert (Result = 0);
1154 SSL.Abort_Undefer.all;
1155 end if;
1156 end Suspend_Until_True;
1158 ----------------
1159 -- Check_Exit --
1160 ----------------
1162 -- Dummy version
1164 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1165 pragma Unreferenced (Self_ID);
1166 begin
1167 return True;
1168 end Check_Exit;
1170 --------------------
1171 -- Check_No_Locks --
1172 --------------------
1174 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1175 pragma Unreferenced (Self_ID);
1176 begin
1177 return True;
1178 end Check_No_Locks;
1180 ----------------------
1181 -- Environment_Task --
1182 ----------------------
1184 function Environment_Task return Task_Id is
1185 begin
1186 return Environment_Task_Id;
1187 end Environment_Task;
1189 --------------
1190 -- Lock_RTS --
1191 --------------
1193 procedure Lock_RTS is
1194 begin
1195 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1196 end Lock_RTS;
1198 ----------------
1199 -- Unlock_RTS --
1200 ----------------
1202 procedure Unlock_RTS is
1203 begin
1204 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1205 end Unlock_RTS;
1207 ------------------
1208 -- Suspend_Task --
1209 ------------------
1211 function Suspend_Task
1212 (T : ST.Task_Id;
1213 Thread_Self : Thread_Id) return Boolean
1215 pragma Unreferenced (T, Thread_Self);
1216 begin
1217 return False;
1218 end Suspend_Task;
1220 -----------------
1221 -- Resume_Task --
1222 -----------------
1224 function Resume_Task
1225 (T : ST.Task_Id;
1226 Thread_Self : Thread_Id) return Boolean
1228 pragma Unreferenced (T, Thread_Self);
1229 begin
1230 return False;
1231 end Resume_Task;
1233 --------------------
1234 -- Stop_All_Tasks --
1235 --------------------
1237 procedure Stop_All_Tasks is
1238 begin
1239 null;
1240 end Stop_All_Tasks;
1242 ---------------
1243 -- Stop_Task --
1244 ---------------
1246 function Stop_Task (T : ST.Task_Id) return Boolean is
1247 pragma Unreferenced (T);
1248 begin
1249 return False;
1250 end Stop_Task;
1252 -------------------
1253 -- Continue_Task --
1254 -------------------
1256 function Continue_Task (T : ST.Task_Id) return Boolean is
1257 pragma Unreferenced (T);
1258 begin
1259 return False;
1260 end Continue_Task;
1262 ----------------
1263 -- Initialize --
1264 ----------------
1266 procedure Initialize (Environment_Task : Task_Id) is
1267 act : aliased struct_sigaction;
1268 old_act : aliased struct_sigaction;
1269 Tmp_Set : aliased sigset_t;
1270 Result : Interfaces.C.int;
1272 function State
1273 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1274 pragma Import (C, State, "__gnat_get_interrupt_state");
1275 -- Get interrupt state. Defined in a-init.c
1276 -- The input argument is the interrupt number,
1277 -- and the result is one of the following:
1279 Default : constant Character := 's';
1280 -- 'n' this interrupt not set by any Interrupt_State pragma
1281 -- 'u' Interrupt_State pragma set state to User
1282 -- 'r' Interrupt_State pragma set state to Runtime
1283 -- 's' Interrupt_State pragma set state to System (use "default"
1284 -- system handler)
1286 begin
1287 Environment_Task_Id := Environment_Task;
1289 Interrupt_Management.Initialize;
1291 -- Prepare the set of signals that should unblocked in all tasks
1293 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1294 pragma Assert (Result = 0);
1296 for J in Interrupt_Management.Interrupt_ID loop
1297 if System.Interrupt_Management.Keep_Unmasked (J) then
1298 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1299 pragma Assert (Result = 0);
1300 end if;
1301 end loop;
1303 -- Initialize the lock used to synchronize chain of all ATCBs
1305 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1307 Specific.Initialize (Environment_Task);
1309 if Use_Alternate_Stack then
1310 Environment_Task.Common.Task_Alternate_Stack :=
1311 Alternate_Stack'Address;
1312 end if;
1314 -- Make environment task known here because it doesn't go through
1315 -- Activate_Tasks, which does it for all other tasks.
1317 Known_Tasks (Known_Tasks'First) := Environment_Task;
1318 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1320 Enter_Task (Environment_Task);
1322 if State
1323 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1324 then
1325 act.sa_flags := 0;
1326 act.sa_handler := Abort_Handler'Address;
1328 Result := sigemptyset (Tmp_Set'Access);
1329 pragma Assert (Result = 0);
1330 act.sa_mask := Tmp_Set;
1332 Result :=
1333 sigaction
1334 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1335 act'Unchecked_Access,
1336 old_act'Unchecked_Access);
1337 pragma Assert (Result = 0);
1338 Abort_Handler_Installed := True;
1339 end if;
1340 end Initialize;
1342 -----------------------
1343 -- Set_Task_Affinity --
1344 -----------------------
1346 procedure Set_Task_Affinity (T : ST.Task_Id) is
1347 pragma Unreferenced (T);
1349 begin
1350 -- Setting task affinity is not supported by the underlying system
1352 null;
1353 end Set_Task_Affinity;
1355 end System.Task_Primitives.Operations;