2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / 5ataprop.adb
blobfbadd9b6aa105a72f04938c4ff312cecb82fa45c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a DEC Unix 4.0d version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 pragma Polling (Off);
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.Task_Info;
47 -- used for Task_Info_Type
49 with Interfaces;
50 -- used for Shift_Left
52 with Interfaces.C;
53 -- used for int
54 -- size_t
56 with System.Interrupt_Management;
57 -- used for Keep_Unmasked
58 -- Abort_Task_Interrupt
59 -- Interrupt_ID
61 with System.Interrupt_Management.Operations;
62 -- used for Set_Interrupt_Mask
63 -- All_Tasks_Mask
64 pragma Elaborate_All (System.Interrupt_Management.Operations);
66 with System.Parameters;
67 -- used for Size_Type
69 with System.Tasking;
70 -- used for Ada_Task_Control_Block
71 -- Task_ID
72 -- ATCB components and types
74 with System.Soft_Links;
75 -- used for Defer/Undefer_Abort
77 -- Note that we do not use System.Tasking.Initialization directly since
78 -- this is a higher level package that we shouldn't depend on. For example
79 -- when using the restricted run time, it is replaced by
80 -- System.Tasking.Restricted.Initialization
82 with System.OS_Primitives;
83 -- used for Delay_Modes
85 with Unchecked_Conversion;
86 with Unchecked_Deallocation;
88 package body System.Task_Primitives.Operations is
90 use System.Tasking.Debug;
91 use System.Tasking;
92 use Interfaces.C;
93 use System.OS_Interface;
94 use System.Parameters;
95 use System.OS_Primitives;
97 package SSL renames System.Soft_Links;
99 ----------------
100 -- Local Data --
101 ----------------
103 -- The followings are logically constants, but need to be initialized
104 -- at run time.
106 Single_RTS_Lock : aliased RTS_Lock;
107 -- This is a lock to allow only one thread of control in the RTS at
108 -- a time; it is used to execute in mutual exclusion from all other tasks.
109 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
111 Environment_Task_ID : Task_ID;
112 -- A variable to hold Task_ID for the environment task.
114 Unblocked_Signal_Mask : aliased sigset_t;
115 -- The set of signals that should unblocked in all tasks
117 Time_Slice_Val : Integer;
118 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
120 Locking_Policy : Character;
121 pragma Import (C, Locking_Policy, "__gl_locking_policy");
123 Dispatching_Policy : Character;
124 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
126 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
127 -- Indicates whether FIFO_Within_Priorities is set.
129 Curpid : pid_t;
131 -----------------------
132 -- Local Subprograms --
133 -----------------------
135 procedure Abort_Handler (Sig : Signal);
137 function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
139 function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
141 --------------------
142 -- Local Packages --
143 --------------------
145 package Specific is
147 procedure Initialize (Environment_Task : Task_ID);
148 pragma Inline (Initialize);
149 -- Initialize various data needed by this package.
151 procedure Set (Self_Id : Task_ID);
152 pragma Inline (Set);
153 -- Set the self id for the current task.
155 function Self return Task_ID;
156 pragma Inline (Self);
157 -- Return a pointer to the Ada Task Control Block of the calling task.
159 end Specific;
161 package body Specific is separate;
162 -- The body of this package is target specific.
164 -------------------
165 -- Abort_Handler --
166 -------------------
168 procedure Abort_Handler (Sig : Signal) is
169 T : constant Task_ID := Self;
170 Result : Interfaces.C.int;
171 Old_Set : aliased sigset_t;
173 begin
174 if T.Deferral_Level = 0
175 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
176 not T.Aborting
177 then
178 T.Aborting := True;
180 -- Make sure signals used for RTS internal purpose are unmasked
182 Result := pthread_sigmask (SIG_UNBLOCK,
183 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
184 pragma Assert (Result = 0);
186 raise Standard'Abort_Signal;
187 end if;
188 end Abort_Handler;
190 ------------------
191 -- Stack_Guard --
192 ------------------
194 -- The underlying thread system sets a guard page at the
195 -- bottom of a thread stack, so nothing is needed.
197 procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
198 begin
199 null;
200 end Stack_Guard;
202 --------------------
203 -- Get_Thread_Id --
204 --------------------
206 function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
207 begin
208 return T.Common.LL.Thread;
209 end Get_Thread_Id;
211 ----------
212 -- Self --
213 ----------
215 function Self return Task_ID renames Specific.Self;
217 ---------------------
218 -- Initialize_Lock --
219 ---------------------
221 -- Note: mutexes and cond_variables needed per-task basis are
222 -- initialized in Initialize_TCB and the Storage_Error is
223 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
224 -- used in RTS is initialized before any status change of RTS.
225 -- Therefore rasing Storage_Error in the following routines
226 -- should be able to be handled safely.
228 procedure Initialize_Lock
229 (Prio : System.Any_Priority;
230 L : access Lock)
232 Attributes : aliased pthread_mutexattr_t;
233 Result : Interfaces.C.int;
235 begin
236 Result := pthread_mutexattr_init (Attributes'Access);
237 pragma Assert (Result = 0 or else Result = ENOMEM);
239 if Result = ENOMEM then
240 raise Storage_Error;
241 end if;
243 if Locking_Policy = 'C' then
244 L.Ceiling := Interfaces.C.int (Prio);
245 end if;
247 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
248 pragma Assert (Result = 0 or else Result = ENOMEM);
250 if Result = ENOMEM then
251 Result := pthread_mutexattr_destroy (Attributes'Access);
252 raise Storage_Error;
253 end if;
255 Result := pthread_mutexattr_destroy (Attributes'Access);
256 pragma Assert (Result = 0);
257 end Initialize_Lock;
259 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
260 Attributes : aliased pthread_mutexattr_t;
261 Result : Interfaces.C.int;
263 begin
264 Result := pthread_mutexattr_init (Attributes'Access);
265 pragma Assert (Result = 0 or else Result = ENOMEM);
267 if Result = ENOMEM then
268 raise Storage_Error;
269 end if;
271 Result := pthread_mutex_init (L, Attributes'Access);
272 pragma Assert (Result = 0 or else Result = ENOMEM);
274 if Result = ENOMEM then
275 Result := pthread_mutexattr_destroy (Attributes'Access);
276 raise Storage_Error;
277 end if;
279 Result := pthread_mutexattr_destroy (Attributes'Access);
280 pragma Assert (Result = 0);
281 end Initialize_Lock;
283 -------------------
284 -- Finalize_Lock --
285 -------------------
287 procedure Finalize_Lock (L : access Lock) is
288 Result : Interfaces.C.int;
289 begin
290 Result := pthread_mutex_destroy (L.L'Access);
291 pragma Assert (Result = 0);
292 end Finalize_Lock;
294 procedure Finalize_Lock (L : access RTS_Lock) is
295 Result : Interfaces.C.int;
296 begin
297 Result := pthread_mutex_destroy (L);
298 pragma Assert (Result = 0);
299 end Finalize_Lock;
301 ----------------
302 -- Write_Lock --
303 ----------------
305 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
306 Result : Interfaces.C.int;
307 Self_ID : Task_ID;
308 All_Tasks_Link : Task_ID;
309 Current_Prio : System.Any_Priority;
311 begin
312 -- Perform ceiling checks only when this is the locking policy in use.
314 if Locking_Policy = 'C' then
315 Self_ID := Self;
316 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
317 Current_Prio := Get_Priority (Self_ID);
319 -- If there is no other task, no need to check priorities
321 if All_Tasks_Link /= Null_Task
322 and then L.Ceiling < Interfaces.C.int (Current_Prio)
323 then
324 Ceiling_Violation := True;
325 return;
326 end if;
327 end if;
329 Result := pthread_mutex_lock (L.L'Access);
330 pragma Assert (Result = 0);
332 Ceiling_Violation := False;
333 end Write_Lock;
335 procedure Write_Lock
336 (L : access RTS_Lock; Global_Lock : Boolean := False)
338 Result : Interfaces.C.int;
339 begin
340 if not Single_Lock or else Global_Lock then
341 Result := pthread_mutex_lock (L);
342 pragma Assert (Result = 0);
343 end if;
344 end Write_Lock;
346 procedure Write_Lock (T : Task_ID) is
347 Result : Interfaces.C.int;
348 begin
349 if not Single_Lock then
350 Result := pthread_mutex_lock (T.Common.LL.L'Access);
351 pragma Assert (Result = 0);
352 end if;
353 end Write_Lock;
355 ---------------
356 -- Read_Lock --
357 ---------------
359 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
360 begin
361 Write_Lock (L, Ceiling_Violation);
362 end Read_Lock;
364 ------------
365 -- Unlock --
366 ------------
368 procedure Unlock (L : access Lock) is
369 Result : Interfaces.C.int;
370 begin
371 Result := pthread_mutex_unlock (L.L'Access);
372 pragma Assert (Result = 0);
373 end Unlock;
375 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
376 Result : Interfaces.C.int;
377 begin
378 if not Single_Lock or else Global_Lock then
379 Result := pthread_mutex_unlock (L);
380 pragma Assert (Result = 0);
381 end if;
382 end Unlock;
384 procedure Unlock (T : Task_ID) is
385 Result : Interfaces.C.int;
386 begin
387 if not Single_Lock then
388 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
389 pragma Assert (Result = 0);
390 end if;
391 end Unlock;
393 -----------
394 -- Sleep --
395 -----------
397 procedure Sleep
398 (Self_ID : Task_ID;
399 Reason : System.Tasking.Task_States)
401 Result : Interfaces.C.int;
402 begin
403 if Single_Lock then
404 Result := pthread_cond_wait
405 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
406 else
407 Result := pthread_cond_wait
408 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
409 end if;
411 -- EINTR is not considered a failure.
413 pragma Assert (Result = 0 or else Result = EINTR);
414 end Sleep;
416 -----------------
417 -- Timed_Sleep --
418 -----------------
420 -- This is for use within the run-time system, so abort is
421 -- assumed to be already deferred, and the caller should be
422 -- holding its own ATCB lock.
424 procedure Timed_Sleep
425 (Self_ID : Task_ID;
426 Time : Duration;
427 Mode : ST.Delay_Modes;
428 Reason : System.Tasking.Task_States;
429 Timedout : out Boolean;
430 Yielded : out Boolean)
432 Check_Time : constant Duration := Monotonic_Clock;
433 Abs_Time : Duration;
434 Request : aliased timespec;
435 Result : Interfaces.C.int;
437 begin
438 Timedout := True;
439 Yielded := False;
441 if Mode = Relative then
442 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
443 else
444 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
445 end if;
447 if Abs_Time > Check_Time then
448 Request := To_Timespec (Abs_Time);
450 loop
451 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
452 or else Self_ID.Pending_Priority_Change;
454 if Single_Lock then
455 Result := pthread_cond_timedwait
456 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
457 Request'Access);
459 else
460 Result := pthread_cond_timedwait
461 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
462 Request'Access);
463 end if;
465 exit when Abs_Time <= Monotonic_Clock;
467 if Result = 0 or Result = EINTR then
468 -- somebody may have called Wakeup for us
469 Timedout := False;
470 exit;
471 end if;
473 pragma Assert (Result = ETIMEDOUT);
474 end loop;
475 end if;
476 end Timed_Sleep;
478 -----------------
479 -- Timed_Delay --
480 -----------------
482 -- This is for use in implementing delay statements, so
483 -- we assume the caller is abort-deferred but is holding
484 -- no locks.
486 procedure Timed_Delay
487 (Self_ID : Task_ID;
488 Time : Duration;
489 Mode : ST.Delay_Modes)
491 Check_Time : constant Duration := Monotonic_Clock;
492 Abs_Time : Duration;
493 Request : aliased timespec;
494 Result : Interfaces.C.int;
496 begin
497 -- Only the little window between deferring abort and
498 -- locking Self_ID is the reason we need to
499 -- check for pending abort and priority change below! :(
501 SSL.Abort_Defer.all;
503 if Single_Lock then
504 Lock_RTS;
505 end if;
507 Write_Lock (Self_ID);
509 if Mode = Relative then
510 Abs_Time := Time + Check_Time;
511 else
512 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
513 end if;
515 if Abs_Time > Check_Time then
516 Request := To_Timespec (Abs_Time);
517 Self_ID.Common.State := Delay_Sleep;
519 loop
520 if Self_ID.Pending_Priority_Change then
521 Self_ID.Pending_Priority_Change := False;
522 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
523 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
524 end if;
526 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
528 if Single_Lock then
529 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
530 Single_RTS_Lock'Access, Request'Access);
531 else
532 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
533 Self_ID.Common.LL.L'Access, Request'Access);
534 end if;
536 exit when Abs_Time <= Monotonic_Clock;
538 pragma Assert (Result = 0 or else
539 Result = ETIMEDOUT or else
540 Result = EINTR);
541 end loop;
543 Self_ID.Common.State := Runnable;
544 end if;
546 Unlock (Self_ID);
548 if Single_Lock then
549 Unlock_RTS;
550 end if;
552 Yield;
553 SSL.Abort_Undefer.all;
554 end Timed_Delay;
556 ---------------------
557 -- Monotonic_Clock --
558 ---------------------
560 function Monotonic_Clock return Duration is
561 TS : aliased timespec;
562 Result : Interfaces.C.int;
564 begin
565 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
566 pragma Assert (Result = 0);
567 return To_Duration (TS);
568 end Monotonic_Clock;
570 -------------------
571 -- RT_Resolution --
572 -------------------
574 function RT_Resolution return Duration is
575 begin
576 return 1.0 / 1024.0; -- Clock on DEC Alpha ticks at 1024 Hz
577 end RT_Resolution;
579 ------------
580 -- Wakeup --
581 ------------
583 procedure Wakeup (T : Task_ID; Reason : System.Tasking.Task_States) is
584 Result : Interfaces.C.int;
585 begin
586 Result := pthread_cond_signal (T.Common.LL.CV'Access);
587 pragma Assert (Result = 0);
588 end Wakeup;
590 -----------
591 -- Yield --
592 -----------
594 procedure Yield (Do_Yield : Boolean := True) is
595 Result : Interfaces.C.int;
596 begin
597 if Do_Yield then
598 Result := sched_yield;
599 end if;
600 end Yield;
602 ------------------
603 -- Set_Priority --
604 ------------------
606 procedure Set_Priority
607 (T : Task_ID;
608 Prio : System.Any_Priority;
609 Loss_Of_Inheritance : Boolean := False)
611 Result : Interfaces.C.int;
612 Param : aliased struct_sched_param;
614 begin
615 T.Common.Current_Priority := Prio;
616 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
618 if Time_Slice_Val > 0 then
619 Result := pthread_setschedparam
620 (T.Common.LL.Thread, SCHED_RR, Param'Access);
622 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
623 Result := pthread_setschedparam
624 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
626 else
627 Result := pthread_setschedparam
628 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
629 end if;
631 pragma Assert (Result = 0);
632 end Set_Priority;
634 ------------------
635 -- Get_Priority --
636 ------------------
638 function Get_Priority (T : Task_ID) return System.Any_Priority is
639 begin
640 return T.Common.Current_Priority;
641 end Get_Priority;
643 ----------------
644 -- Enter_Task --
645 ----------------
647 procedure Enter_Task (Self_ID : Task_ID) is
648 begin
649 Self_ID.Common.LL.Thread := pthread_self;
650 Specific.Set (Self_ID);
652 Lock_RTS;
654 for J in Known_Tasks'Range loop
655 if Known_Tasks (J) = null then
656 Known_Tasks (J) := Self_ID;
657 Self_ID.Known_Tasks_Index := J;
658 exit;
659 end if;
660 end loop;
662 Unlock_RTS;
663 end Enter_Task;
665 --------------
666 -- New_ATCB --
667 --------------
669 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
670 begin
671 return new Ada_Task_Control_Block (Entry_Num);
672 end New_ATCB;
674 --------------------
675 -- Initialize_TCB --
676 --------------------
678 procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
679 Mutex_Attr : aliased pthread_mutexattr_t;
680 Result : Interfaces.C.int;
681 Cond_Attr : aliased pthread_condattr_t;
683 begin
684 if not Single_Lock then
685 Result := pthread_mutexattr_init (Mutex_Attr'Access);
686 pragma Assert (Result = 0 or else Result = ENOMEM);
688 if Result = 0 then
689 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
690 Mutex_Attr'Access);
691 pragma Assert (Result = 0 or else Result = ENOMEM);
692 end if;
694 if Result /= 0 then
695 Succeeded := False;
696 return;
697 end if;
699 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
700 pragma Assert (Result = 0);
701 end if;
703 Result := pthread_condattr_init (Cond_Attr'Access);
704 pragma Assert (Result = 0 or else Result = ENOMEM);
706 if Result = 0 then
707 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
708 Cond_Attr'Access);
709 pragma Assert (Result = 0 or else Result = ENOMEM);
710 end if;
712 if Result = 0 then
713 Succeeded := True;
714 else
715 if not Single_Lock then
716 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
717 pragma Assert (Result = 0);
718 end if;
720 Succeeded := False;
721 end if;
723 Result := pthread_condattr_destroy (Cond_Attr'Access);
724 pragma Assert (Result = 0);
725 end Initialize_TCB;
727 -----------------
728 -- Create_Task --
729 -----------------
731 procedure Create_Task
732 (T : Task_ID;
733 Wrapper : System.Address;
734 Stack_Size : System.Parameters.Size_Type;
735 Priority : System.Any_Priority;
736 Succeeded : out Boolean)
738 Attributes : aliased pthread_attr_t;
739 Adjusted_Stack_Size : Interfaces.C.size_t;
740 Result : Interfaces.C.int;
741 Param : aliased System.OS_Interface.struct_sched_param;
743 function Thread_Body_Access is new
744 Unchecked_Conversion (System.Address, Thread_Body);
746 use System.Task_Info;
748 begin
749 if Stack_Size = Unspecified_Size then
750 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
752 elsif Stack_Size < Minimum_Stack_Size then
753 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
755 else
756 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
757 end if;
759 Result := pthread_attr_init (Attributes'Access);
760 pragma Assert (Result = 0 or else Result = ENOMEM);
762 if Result /= 0 then
763 Succeeded := False;
764 return;
765 end if;
767 Result := pthread_attr_setdetachstate
768 (Attributes'Access, PTHREAD_CREATE_DETACHED);
769 pragma Assert (Result = 0);
771 Result := pthread_attr_setstacksize
772 (Attributes'Access, Adjusted_Stack_Size);
773 pragma Assert (Result = 0);
775 -- Set the scheduling parameters explicitly, since this is the only
776 -- way to force the OS to take the scope attribute into account
778 Result := pthread_attr_setinheritsched
779 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
780 pragma Assert (Result = 0);
782 Param.sched_priority :=
783 Interfaces.C.int (Underlying_Priorities (Priority));
784 Result := pthread_attr_setschedparam
785 (Attributes'Access, Param'Access);
786 pragma Assert (Result = 0);
788 if Time_Slice_Val > 0 then
789 Result := pthread_attr_setschedpolicy
790 (Attributes'Access, System.OS_Interface.SCHED_RR);
792 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
793 Result := pthread_attr_setschedpolicy
794 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
796 else
797 Result := pthread_attr_setschedpolicy
798 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
799 end if;
801 pragma Assert (Result = 0);
803 T.Common.Current_Priority := Priority;
805 if T.Common.Task_Info /= null then
806 case T.Common.Task_Info.Contention_Scope is
807 when System.Task_Info.Process_Scope =>
808 Result := pthread_attr_setscope
809 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
811 when System.Task_Info.System_Scope =>
812 Result := pthread_attr_setscope
813 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
815 when System.Task_Info.Default_Scope =>
816 Result := 0;
817 end case;
819 pragma Assert (Result = 0);
820 end if;
822 -- Since the initial signal mask of a thread is inherited from the
823 -- creator, and the Environment task has all its signals masked, we
824 -- do not need to manipulate caller's signal mask at this point.
825 -- All tasks in RTS will have All_Tasks_Mask initially.
827 Result := pthread_create
828 (T.Common.LL.Thread'Access,
829 Attributes'Access,
830 Thread_Body_Access (Wrapper),
831 To_Address (T));
832 pragma Assert (Result = 0 or else Result = EAGAIN);
834 Succeeded := Result = 0;
836 Result := pthread_attr_destroy (Attributes'Access);
837 pragma Assert (Result = 0);
839 if T.Common.Task_Info /= null then
840 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
841 Result := bind_to_cpu (Curpid, 0);
842 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
843 Result := bind_to_cpu
844 (Curpid,
845 Interfaces.C.unsigned_long (
846 Interfaces.Shift_Left
847 (Interfaces.Unsigned_64'(1),
848 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
849 pragma Assert (Result = 0);
850 end if;
851 end if;
852 end Create_Task;
854 ------------------
855 -- Finalize_TCB --
856 ------------------
858 procedure Finalize_TCB (T : Task_ID) is
859 Result : Interfaces.C.int;
860 Tmp : Task_ID := T;
862 procedure Free is new
863 Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
865 begin
866 if not Single_Lock then
867 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
868 pragma Assert (Result = 0);
869 end if;
871 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
872 pragma Assert (Result = 0);
874 if T.Known_Tasks_Index /= -1 then
875 Known_Tasks (T.Known_Tasks_Index) := null;
876 end if;
878 Free (Tmp);
879 end Finalize_TCB;
881 ---------------
882 -- Exit_Task --
883 ---------------
885 procedure Exit_Task is
886 begin
887 pthread_exit (System.Null_Address);
888 end Exit_Task;
890 ----------------
891 -- Abort_Task --
892 ----------------
894 procedure Abort_Task (T : Task_ID) is
895 Result : Interfaces.C.int;
897 begin
898 Result := pthread_kill (T.Common.LL.Thread,
899 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
900 pragma Assert (Result = 0);
901 end Abort_Task;
903 ----------------
904 -- Check_Exit --
905 ----------------
907 -- Dummy versions. The only currently working versions is for solaris
908 -- (native).
910 function Check_Exit (Self_ID : ST.Task_ID) return Boolean is
911 begin
912 return True;
913 end Check_Exit;
915 --------------------
916 -- Check_No_Locks --
917 --------------------
919 function Check_No_Locks (Self_ID : ST.Task_ID) return Boolean is
920 begin
921 return True;
922 end Check_No_Locks;
924 ----------------------
925 -- Environment_Task --
926 ----------------------
928 function Environment_Task return Task_ID is
929 begin
930 return Environment_Task_ID;
931 end Environment_Task;
933 --------------
934 -- Lock_RTS --
935 --------------
937 procedure Lock_RTS is
938 begin
939 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
940 end Lock_RTS;
942 ----------------
943 -- Unlock_RTS --
944 ----------------
946 procedure Unlock_RTS is
947 begin
948 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
949 end Unlock_RTS;
951 ------------------
952 -- Suspend_Task --
953 ------------------
955 function Suspend_Task
956 (T : ST.Task_ID;
957 Thread_Self : Thread_Id) return Boolean is
958 begin
959 return False;
960 end Suspend_Task;
962 -----------------
963 -- Resume_Task --
964 -----------------
966 function Resume_Task
967 (T : ST.Task_ID;
968 Thread_Self : Thread_Id) return Boolean is
969 begin
970 return False;
971 end Resume_Task;
973 ----------------
974 -- Initialize --
975 ----------------
977 procedure Initialize (Environment_Task : Task_ID) is
978 act : aliased struct_sigaction;
979 old_act : aliased struct_sigaction;
980 Tmp_Set : aliased sigset_t;
981 Result : Interfaces.C.int;
983 begin
984 Environment_Task_ID := Environment_Task;
986 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
987 -- Initialize the lock used to synchronize chain of all ATCBs.
989 Specific.Initialize (Environment_Task);
991 Enter_Task (Environment_Task);
993 -- Install the abort-signal handler
995 act.sa_flags := 0;
996 act.sa_handler := Abort_Handler'Address;
998 Result := sigemptyset (Tmp_Set'Access);
999 pragma Assert (Result = 0);
1000 act.sa_mask := Tmp_Set;
1002 Result :=
1003 sigaction
1004 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1005 act'Unchecked_Access,
1006 old_act'Unchecked_Access);
1007 pragma Assert (Result = 0);
1008 end Initialize;
1010 begin
1011 declare
1012 Result : Interfaces.C.int;
1013 begin
1014 -- Mask Environment task for all signals. The original mask of the
1015 -- Environment task will be recovered by Interrupt_Server task
1016 -- during the elaboration of s-interr.adb.
1018 System.Interrupt_Management.Operations.Set_Interrupt_Mask
1019 (System.Interrupt_Management.Operations.All_Tasks_Mask'Access);
1021 -- Prepare the set of signals that should unblocked in all tasks
1023 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1024 pragma Assert (Result = 0);
1026 for J in Interrupt_Management.Interrupt_ID loop
1027 if System.Interrupt_Management.Keep_Unmasked (J) then
1028 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1029 pragma Assert (Result = 0);
1030 end if;
1031 end loop;
1032 end;
1034 Curpid := getpid;
1035 end System.Task_Primitives.Operations;