Daily bump.
[official-gcc.git] / gcc / ada / s-taprop-vxworks.adb
blob5eb4bc99a6842066c503e0149e9a5c53105898ee
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-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 the VxWorks 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.Interrupt_Management;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
49 -- Signal_ID
50 -- Initialize_Interrupts
52 with Interfaces.C;
54 with System.Soft_Links;
55 -- used for Abort_Defer/Undefer
57 -- We use System.Soft_Links instead of System.Tasking.Initialization
58 -- because the later is a higher level package that we shouldn't depend on.
59 -- For example when using the restricted run time, it is replaced by
60 -- System.Tasking.Restricted.Stages.
62 with Ada.Unchecked_Conversion;
63 with Ada.Unchecked_Deallocation;
65 package body System.Task_Primitives.Operations is
67 package SSL renames System.Soft_Links;
69 use System.Tasking.Debug;
70 use System.Tasking;
71 use System.OS_Interface;
72 use System.Parameters;
73 use type Interfaces.C.int;
75 subtype int is System.OS_Interface.int;
77 Relative : constant := 0;
79 ----------------
80 -- Local Data --
81 ----------------
83 -- The followings are logically constants, but need to be initialized at
84 -- run time.
86 Single_RTS_Lock : aliased RTS_Lock;
87 -- This is a lock to allow only one thread of control in the RTS at a
88 -- time; it is used to execute in mutual exclusion from all other tasks.
89 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
91 Environment_Task_Id : Task_Id;
92 -- A variable to hold Task_Id for the environment task
94 Unblocked_Signal_Mask : aliased sigset_t;
95 -- The set of signals that should unblocked in all tasks
97 -- The followings are internal configuration constants needed
99 Time_Slice_Val : Integer;
100 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
102 Locking_Policy : Character;
103 pragma Import (C, Locking_Policy, "__gl_locking_policy");
105 Dispatching_Policy : Character;
106 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
108 function Get_Policy (Prio : System.Any_Priority) return Character;
109 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
110 -- Get priority specific dispatching policy
112 Mutex_Protocol : Priority_Type;
114 Foreign_Task_Elaborated : aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 type Set_Stack_Limit_Proc_Acc is access procedure;
118 pragma Convention (C, Set_Stack_Limit_Proc_Acc);
120 Set_Stack_Limit_Hook : Set_Stack_Limit_Proc_Acc;
121 pragma Import (C, Set_Stack_Limit_Hook, "__gnat_set_stack_limit_hook");
122 -- Procedure to be called when a task is created to set stack
123 -- limit.
125 --------------------
126 -- Local Packages --
127 --------------------
129 package Specific is
131 procedure Initialize;
132 pragma Inline (Initialize);
133 -- Initialize task specific data
135 function Is_Valid_Task return Boolean;
136 pragma Inline (Is_Valid_Task);
137 -- Does executing thread have a TCB?
139 procedure Set (Self_Id : Task_Id);
140 pragma Inline (Set);
141 -- Set the self id for the current task
143 procedure Delete;
144 pragma Inline (Delete);
145 -- Delete the task specific data associated with the current task
147 function Self return Task_Id;
148 pragma Inline (Self);
149 -- Return a pointer to the Ada Task Control Block of the calling task
151 end Specific;
153 package body Specific is separate;
154 -- The body of this package is target specific
156 ---------------------------------
157 -- Support for foreign threads --
158 ---------------------------------
160 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
161 -- Allocate and Initialize a new ATCB for the current Thread
163 function Register_Foreign_Thread
164 (Thread : Thread_Id) return Task_Id is separate;
166 -----------------------
167 -- Local Subprograms --
168 -----------------------
170 procedure Abort_Handler (signo : Signal);
171 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
173 procedure Install_Signal_Handlers;
174 -- Install the default signal handlers for the current task
176 function To_Address is
177 new Ada.Unchecked_Conversion (Task_Id, System.Address);
179 -------------------
180 -- Abort_Handler --
181 -------------------
183 procedure Abort_Handler (signo : Signal) is
184 pragma Unreferenced (signo);
186 Self_ID : constant Task_Id := Self;
187 Old_Set : aliased sigset_t;
189 Result : int;
190 pragma Warnings (Off, Result);
192 begin
193 -- It is not safe to raise an exception when using ZCX and the GCC
194 -- exception handling mechanism.
196 if ZCX_By_Default and then GCC_ZCX_Support then
197 return;
198 end if;
200 if Self_ID.Deferral_Level = 0
201 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
202 and then not Self_ID.Aborting
203 then
204 Self_ID.Aborting := True;
206 -- Make sure signals used for RTS internal purpose are unmasked
208 Result :=
209 pthread_sigmask
210 (SIG_UNBLOCK,
211 Unblocked_Signal_Mask'Access,
212 Old_Set'Access);
213 pragma Assert (Result = 0);
215 raise Standard'Abort_Signal;
216 end if;
217 end Abort_Handler;
219 -----------------
220 -- Stack_Guard --
221 -----------------
223 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
224 pragma Unreferenced (T);
225 pragma Unreferenced (On);
227 begin
228 -- Nothing needed (why not???)
230 null;
231 end Stack_Guard;
233 -------------------
234 -- Get_Thread_Id --
235 -------------------
237 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
238 begin
239 return T.Common.LL.Thread;
240 end Get_Thread_Id;
242 ----------
243 -- Self --
244 ----------
246 function Self return Task_Id renames Specific.Self;
248 -----------------------------
249 -- Install_Signal_Handlers --
250 -----------------------------
252 procedure Install_Signal_Handlers is
253 act : aliased struct_sigaction;
254 old_act : aliased struct_sigaction;
255 Tmp_Set : aliased sigset_t;
256 Result : int;
258 begin
259 act.sa_flags := 0;
260 act.sa_handler := Abort_Handler'Address;
262 Result := sigemptyset (Tmp_Set'Access);
263 pragma Assert (Result = 0);
264 act.sa_mask := Tmp_Set;
266 Result :=
267 sigaction
268 (Signal (Interrupt_Management.Abort_Task_Interrupt),
269 act'Unchecked_Access,
270 old_act'Unchecked_Access);
271 pragma Assert (Result = 0);
273 Interrupt_Management.Initialize_Interrupts;
274 end Install_Signal_Handlers;
276 ---------------------
277 -- Initialize_Lock --
278 ---------------------
280 procedure Initialize_Lock
281 (Prio : System.Any_Priority;
282 L : not null access Lock)
284 begin
285 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
286 L.Prio_Ceiling := int (Prio);
287 L.Protocol := Mutex_Protocol;
288 pragma Assert (L.Mutex /= 0);
289 end Initialize_Lock;
291 procedure Initialize_Lock
292 (L : not null access RTS_Lock;
293 Level : Lock_Level)
295 pragma Unreferenced (Level);
296 begin
297 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
298 L.Prio_Ceiling := int (System.Any_Priority'Last);
299 L.Protocol := Mutex_Protocol;
300 pragma Assert (L.Mutex /= 0);
301 end Initialize_Lock;
303 -------------------
304 -- Finalize_Lock --
305 -------------------
307 procedure Finalize_Lock (L : not null access Lock) is
308 Result : int;
309 begin
310 Result := semDelete (L.Mutex);
311 pragma Assert (Result = 0);
312 end Finalize_Lock;
314 procedure Finalize_Lock (L : not null access RTS_Lock) is
315 Result : int;
316 begin
317 Result := semDelete (L.Mutex);
318 pragma Assert (Result = 0);
319 end Finalize_Lock;
321 ----------------
322 -- Write_Lock --
323 ----------------
325 procedure Write_Lock
326 (L : not null access Lock;
327 Ceiling_Violation : out Boolean)
329 Result : int;
331 begin
332 if L.Protocol = Prio_Protect
333 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
334 then
335 Ceiling_Violation := True;
336 return;
337 else
338 Ceiling_Violation := False;
339 end if;
341 Result := semTake (L.Mutex, WAIT_FOREVER);
342 pragma Assert (Result = 0);
343 end Write_Lock;
345 procedure Write_Lock
346 (L : not null access RTS_Lock;
347 Global_Lock : Boolean := False)
349 Result : int;
350 begin
351 if not Single_Lock or else Global_Lock then
352 Result := semTake (L.Mutex, WAIT_FOREVER);
353 pragma Assert (Result = 0);
354 end if;
355 end Write_Lock;
357 procedure Write_Lock (T : Task_Id) is
358 Result : int;
359 begin
360 if not Single_Lock then
361 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
362 pragma Assert (Result = 0);
363 end if;
364 end Write_Lock;
366 ---------------
367 -- Read_Lock --
368 ---------------
370 procedure Read_Lock
371 (L : not null access Lock;
372 Ceiling_Violation : out Boolean)
374 begin
375 Write_Lock (L, Ceiling_Violation);
376 end Read_Lock;
378 ------------
379 -- Unlock --
380 ------------
382 procedure Unlock (L : not null access Lock) is
383 Result : int;
384 begin
385 Result := semGive (L.Mutex);
386 pragma Assert (Result = 0);
387 end Unlock;
389 procedure Unlock
390 (L : not null access RTS_Lock;
391 Global_Lock : Boolean := False)
393 Result : int;
394 begin
395 if not Single_Lock or else Global_Lock then
396 Result := semGive (L.Mutex);
397 pragma Assert (Result = 0);
398 end if;
399 end Unlock;
401 procedure Unlock (T : Task_Id) is
402 Result : int;
403 begin
404 if not Single_Lock then
405 Result := semGive (T.Common.LL.L.Mutex);
406 pragma Assert (Result = 0);
407 end if;
408 end Unlock;
410 -----------------
411 -- Set_Ceiling --
412 -----------------
414 -- Dynamic priority ceilings are not supported by the underlying system
416 procedure Set_Ceiling
417 (L : not null access Lock;
418 Prio : System.Any_Priority)
420 pragma Unreferenced (L, Prio);
421 begin
422 null;
423 end Set_Ceiling;
425 -----------
426 -- Sleep --
427 -----------
429 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
430 pragma Unreferenced (Reason);
432 Result : int;
434 begin
435 pragma Assert (Self_ID = Self);
437 -- Release the mutex before sleeping
439 if Single_Lock then
440 Result := semGive (Single_RTS_Lock.Mutex);
441 else
442 Result := semGive (Self_ID.Common.LL.L.Mutex);
443 end if;
445 pragma Assert (Result = 0);
447 -- Perform a blocking operation to take the CV semaphore. Note that a
448 -- blocking operation in VxWorks will reenable task scheduling. When we
449 -- are no longer blocked and control is returned, task scheduling will
450 -- again be disabled.
452 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
453 pragma Assert (Result = 0);
455 -- Take the mutex back
457 if Single_Lock then
458 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
459 else
460 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
461 end if;
463 pragma Assert (Result = 0);
464 end Sleep;
466 -----------------
467 -- Timed_Sleep --
468 -----------------
470 -- This is for use within the run-time system, so abort is assumed to be
471 -- already deferred, and the caller should be holding its own ATCB lock.
473 procedure Timed_Sleep
474 (Self_ID : Task_Id;
475 Time : Duration;
476 Mode : ST.Delay_Modes;
477 Reason : System.Tasking.Task_States;
478 Timedout : out Boolean;
479 Yielded : out Boolean)
481 pragma Unreferenced (Reason);
483 Orig : constant Duration := Monotonic_Clock;
484 Absolute : Duration;
485 Ticks : int;
486 Result : int;
487 Wakeup : Boolean := False;
489 begin
490 Timedout := False;
491 Yielded := True;
493 if Mode = Relative then
494 Absolute := Orig + Time;
496 -- Systematically add one since the first tick will delay *at most*
497 -- 1 / Rate_Duration seconds, so we need to add one to be on the
498 -- safe side.
500 Ticks := To_Clock_Ticks (Time);
502 if Ticks > 0 and then Ticks < int'Last then
503 Ticks := Ticks + 1;
504 end if;
506 else
507 Absolute := Time;
508 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
509 end if;
511 if Ticks > 0 then
512 loop
513 -- Release the mutex before sleeping
515 if Single_Lock then
516 Result := semGive (Single_RTS_Lock.Mutex);
517 else
518 Result := semGive (Self_ID.Common.LL.L.Mutex);
519 end if;
521 pragma Assert (Result = 0);
523 -- Perform a blocking operation to take the CV semaphore. Note
524 -- that a blocking operation in VxWorks will reenable task
525 -- scheduling. When we are no longer blocked and control is
526 -- returned, task scheduling will again be disabled.
528 Result := semTake (Self_ID.Common.LL.CV, Ticks);
530 if Result = 0 then
532 -- Somebody may have called Wakeup for us
534 Wakeup := True;
536 else
537 if errno /= S_objLib_OBJ_TIMEOUT then
538 Wakeup := True;
540 else
541 -- If Ticks = int'last, it was most probably truncated so
542 -- let's make another round after recomputing Ticks from
543 -- the the absolute time.
545 if Ticks /= int'Last then
546 Timedout := True;
548 else
549 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
551 if Ticks < 0 then
552 Timedout := True;
553 end if;
554 end if;
555 end if;
556 end if;
558 -- Take the mutex back
560 if Single_Lock then
561 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
562 else
563 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
564 end if;
566 pragma Assert (Result = 0);
568 exit when Timedout or Wakeup;
569 end loop;
571 else
572 Timedout := True;
574 -- Should never hold a lock while yielding
576 if Single_Lock then
577 Result := semGive (Single_RTS_Lock.Mutex);
578 taskDelay (0);
579 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
581 else
582 Result := semGive (Self_ID.Common.LL.L.Mutex);
583 taskDelay (0);
584 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
585 end if;
586 end if;
587 end Timed_Sleep;
589 -----------------
590 -- Timed_Delay --
591 -----------------
593 -- This is for use in implementing delay statements, so we assume the
594 -- caller is holding no locks.
596 procedure Timed_Delay
597 (Self_ID : Task_Id;
598 Time : Duration;
599 Mode : ST.Delay_Modes)
601 Orig : constant Duration := Monotonic_Clock;
602 Absolute : Duration;
603 Ticks : int;
604 Timedout : Boolean;
605 Aborted : Boolean := False;
607 Result : int;
608 pragma Warnings (Off, Result);
610 begin
611 if Mode = Relative then
612 Absolute := Orig + Time;
613 Ticks := To_Clock_Ticks (Time);
615 if Ticks > 0 and then Ticks < int'Last then
617 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
618 -- seconds, so we need to add one to be on the safe side.
620 Ticks := Ticks + 1;
621 end if;
623 else
624 Absolute := Time;
625 Ticks := To_Clock_Ticks (Time - Orig);
626 end if;
628 if Ticks > 0 then
630 -- Modifying State, locking the TCB
632 if Single_Lock then
633 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
634 else
635 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
636 end if;
638 pragma Assert (Result = 0);
640 Self_ID.Common.State := Delay_Sleep;
641 Timedout := False;
643 loop
644 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
646 -- Release the TCB before sleeping
648 if Single_Lock then
649 Result := semGive (Single_RTS_Lock.Mutex);
650 else
651 Result := semGive (Self_ID.Common.LL.L.Mutex);
652 end if;
653 pragma Assert (Result = 0);
655 exit when Aborted;
657 Result := semTake (Self_ID.Common.LL.CV, Ticks);
659 if Result /= 0 then
661 -- If Ticks = int'last, it was most probably truncated
662 -- so let's make another round after recomputing Ticks
663 -- from the the absolute time.
665 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
666 Timedout := True;
667 else
668 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
670 if Ticks < 0 then
671 Timedout := True;
672 end if;
673 end if;
674 end if;
676 -- Take back the lock after having slept, to protect further
677 -- access to Self_ID.
679 if Single_Lock then
680 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
681 else
682 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
683 end if;
685 pragma Assert (Result = 0);
687 exit when Timedout;
688 end loop;
690 Self_ID.Common.State := Runnable;
692 if Single_Lock then
693 Result := semGive (Single_RTS_Lock.Mutex);
694 else
695 Result := semGive (Self_ID.Common.LL.L.Mutex);
696 end if;
698 else
699 taskDelay (0);
700 end if;
701 end Timed_Delay;
703 ---------------------
704 -- Monotonic_Clock --
705 ---------------------
707 function Monotonic_Clock return Duration is
708 TS : aliased timespec;
709 Result : int;
710 begin
711 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
712 pragma Assert (Result = 0);
713 return To_Duration (TS);
714 end Monotonic_Clock;
716 -------------------
717 -- RT_Resolution --
718 -------------------
720 function RT_Resolution return Duration is
721 begin
722 return 1.0 / Duration (sysClkRateGet);
723 end RT_Resolution;
725 ------------
726 -- Wakeup --
727 ------------
729 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
730 pragma Unreferenced (Reason);
731 Result : int;
732 begin
733 Result := semGive (T.Common.LL.CV);
734 pragma Assert (Result = 0);
735 end Wakeup;
737 -----------
738 -- Yield --
739 -----------
741 procedure Yield (Do_Yield : Boolean := True) is
742 pragma Unreferenced (Do_Yield);
743 Result : int;
744 pragma Unreferenced (Result);
745 begin
746 Result := taskDelay (0);
747 end Yield;
749 ------------------
750 -- Set_Priority --
751 ------------------
753 type Prio_Array_Type is array (System.Any_Priority) of Integer;
754 pragma Atomic_Components (Prio_Array_Type);
756 Prio_Array : Prio_Array_Type;
757 -- Global array containing the id of the currently running task for
758 -- each priority. Note that we assume that we are on a single processor
759 -- with run-till-blocked scheduling.
761 procedure Set_Priority
762 (T : Task_Id;
763 Prio : System.Any_Priority;
764 Loss_Of_Inheritance : Boolean := False)
766 Array_Item : Integer;
767 Result : int;
769 begin
770 Result :=
771 taskPrioritySet
772 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
773 pragma Assert (Result = 0);
775 if (Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F')
776 and then Loss_Of_Inheritance
777 and then Prio < T.Common.Current_Priority
778 then
779 -- Annex D requirement (RM D.2.2(9))
781 -- If the task drops its priority due to the loss of inherited
782 -- priority, it is added at the head of the ready queue for its
783 -- new active priority.
785 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
786 Prio_Array (T.Common.Base_Priority) := Array_Item;
788 loop
789 -- Give some processes a chance to arrive
791 taskDelay (0);
793 -- Then wait for our turn to proceed
795 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
796 or else Prio_Array (T.Common.Base_Priority) = 1;
797 end loop;
799 Prio_Array (T.Common.Base_Priority) :=
800 Prio_Array (T.Common.Base_Priority) - 1;
801 end if;
803 T.Common.Current_Priority := Prio;
804 end Set_Priority;
806 ------------------
807 -- Get_Priority --
808 ------------------
810 function Get_Priority (T : Task_Id) return System.Any_Priority is
811 begin
812 return T.Common.Current_Priority;
813 end Get_Priority;
815 ----------------
816 -- Enter_Task --
817 ----------------
819 procedure Enter_Task (Self_ID : Task_Id) is
820 procedure Init_Float;
821 pragma Import (C, Init_Float, "__gnat_init_float");
822 -- Properly initializes the FPU for PPC/MIPS systems
824 begin
825 -- Store the user-level task id in the Thread field (to be used
826 -- internally by the run-time system) and the kernel-level task id in
827 -- the LWP field (to be used by the debugger).
829 Self_ID.Common.LL.Thread := taskIdSelf;
830 Self_ID.Common.LL.LWP := getpid;
832 Specific.Set (Self_ID);
834 Init_Float;
836 -- Install the signal handlers
838 -- This is called for each task since there is no signal inheritance
839 -- between VxWorks tasks.
841 Install_Signal_Handlers;
843 Lock_RTS;
845 for J in Known_Tasks'Range loop
846 if Known_Tasks (J) = null then
847 Known_Tasks (J) := Self_ID;
848 Self_ID.Known_Tasks_Index := J;
849 exit;
850 end if;
851 end loop;
853 Unlock_RTS;
855 -- If stack checking is enabled set the stack limit for this task.
856 if Set_Stack_Limit_Hook /= null then
857 Set_Stack_Limit_Hook.all;
858 end if;
859 end Enter_Task;
861 --------------
862 -- New_ATCB --
863 --------------
865 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
866 begin
867 return new Ada_Task_Control_Block (Entry_Num);
868 end New_ATCB;
870 -------------------
871 -- Is_Valid_Task --
872 -------------------
874 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
876 -----------------------------
877 -- Register_Foreign_Thread --
878 -----------------------------
880 function Register_Foreign_Thread return Task_Id is
881 begin
882 if Is_Valid_Task then
883 return Self;
884 else
885 return Register_Foreign_Thread (taskIdSelf);
886 end if;
887 end Register_Foreign_Thread;
889 --------------------
890 -- Initialize_TCB --
891 --------------------
893 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
894 begin
895 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
896 Self_ID.Common.LL.Thread := 0;
898 if Self_ID.Common.LL.CV = 0 then
899 Succeeded := False;
901 else
902 Succeeded := True;
904 if not Single_Lock then
905 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
906 end if;
907 end if;
908 end Initialize_TCB;
910 -----------------
911 -- Create_Task --
912 -----------------
914 procedure Create_Task
915 (T : Task_Id;
916 Wrapper : System.Address;
917 Stack_Size : System.Parameters.Size_Type;
918 Priority : System.Any_Priority;
919 Succeeded : out Boolean)
921 Adjusted_Stack_Size : size_t;
922 begin
923 -- Ask for four extra bytes of stack space so that the ATCB pointer can
924 -- be stored below the stack limit, plus extra space for the frame of
925 -- Task_Wrapper. This is so the user gets the amount of stack requested
926 -- exclusive of the needs.
928 -- We also have to allocate n more bytes for the task name storage and
929 -- enough space for the Wind Task Control Block which is around 0x778
930 -- bytes. VxWorks also seems to carve out additional space, so use 2048
931 -- as a nice round number. We might want to increment to the nearest
932 -- page size in case we ever support VxVMI.
934 -- ??? - we should come back and visit this so we can set the task name
935 -- to something appropriate.
937 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
939 -- Since the initial signal mask of a thread is inherited from the
940 -- creator, and the Environment task has all its signals masked, we do
941 -- not need to manipulate caller's signal mask at this point. All tasks
942 -- in RTS will have All_Tasks_Mask initially.
944 -- We now compute the VxWorks task name and options, then spawn ...
946 declare
947 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
948 Name_Address : System.Address;
949 -- Task name we are going to hand down to VxWorks
951 function Get_Task_Options return int;
952 pragma Import (C, Get_Task_Options, "__gnat_get_task_options");
953 -- Function that returns the options to be set for the task that we
954 -- are creating. We fetch the options assigned to the current task,
955 -- so offering some user level control over the options for a task
956 -- hierarchy, and force VX_FP_TASK because it is almost always
957 -- required.
959 begin
960 -- If there is no Ada task name handy, let VxWorks choose one.
961 -- Otherwise, tell VxWorks what the Ada task name is.
963 if T.Common.Task_Image_Len = 0 then
964 Name_Address := System.Null_Address;
965 else
966 Name (1 .. Name'Last - 1) :=
967 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
968 Name (Name'Last) := ASCII.NUL;
969 Name_Address := Name'Address;
970 end if;
972 -- Now spawn the VxWorks task for real
974 T.Common.LL.Thread :=
975 taskSpawn
976 (Name_Address,
977 To_VxWorks_Priority (int (Priority)),
978 Get_Task_Options,
979 Adjusted_Stack_Size,
980 Wrapper,
981 To_Address (T));
982 end;
984 if T.Common.LL.Thread = -1 then
985 Succeeded := False;
986 else
987 Succeeded := True;
988 end if;
990 Task_Creation_Hook (T.Common.LL.Thread);
991 Set_Priority (T, Priority);
992 end Create_Task;
994 ------------------
995 -- Finalize_TCB --
996 ------------------
998 procedure Finalize_TCB (T : Task_Id) is
999 Result : int;
1000 Tmp : Task_Id := T;
1001 Is_Self : constant Boolean := (T = Self);
1003 procedure Free is new
1004 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1006 begin
1007 if not Single_Lock then
1008 Result := semDelete (T.Common.LL.L.Mutex);
1009 pragma Assert (Result = 0);
1010 end if;
1012 T.Common.LL.Thread := 0;
1014 Result := semDelete (T.Common.LL.CV);
1015 pragma Assert (Result = 0);
1017 if T.Known_Tasks_Index /= -1 then
1018 Known_Tasks (T.Known_Tasks_Index) := null;
1019 end if;
1021 Free (Tmp);
1023 if Is_Self then
1024 Specific.Delete;
1025 end if;
1026 end Finalize_TCB;
1028 ---------------
1029 -- Exit_Task --
1030 ---------------
1032 procedure Exit_Task is
1033 begin
1034 Specific.Set (null);
1035 end Exit_Task;
1037 ----------------
1038 -- Abort_Task --
1039 ----------------
1041 procedure Abort_Task (T : Task_Id) is
1042 Result : int;
1043 begin
1044 Result :=
1045 kill
1046 (T.Common.LL.Thread,
1047 Signal (Interrupt_Management.Abort_Task_Interrupt));
1048 pragma Assert (Result = 0);
1049 end Abort_Task;
1051 ----------------
1052 -- Initialize --
1053 ----------------
1055 procedure Initialize (S : in out Suspension_Object) is
1056 begin
1057 -- Initialize internal state (always to False (RM D.10(6)))
1059 S.State := False;
1060 S.Waiting := False;
1062 -- Initialize internal mutex
1064 -- Use simpler binary semaphore instead of VxWorks
1065 -- mutual exclusion semaphore, because we don't need
1066 -- the fancier semantics and their overhead.
1068 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1070 -- Initialize internal condition variable
1072 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1073 end Initialize;
1075 --------------
1076 -- Finalize --
1077 --------------
1079 procedure Finalize (S : in out Suspension_Object) is
1080 Result : STATUS;
1082 begin
1083 -- Destroy internal mutex
1085 Result := semDelete (S.L);
1086 pragma Assert (Result = OK);
1088 -- Destroy internal condition variable
1090 Result := semDelete (S.CV);
1091 pragma Assert (Result = OK);
1092 end Finalize;
1094 -------------------
1095 -- Current_State --
1096 -------------------
1098 function Current_State (S : Suspension_Object) return Boolean is
1099 begin
1100 -- We do not want to use lock on this read operation. State is marked
1101 -- as Atomic so that we ensure that the value retrieved is correct.
1103 return S.State;
1104 end Current_State;
1106 ---------------
1107 -- Set_False --
1108 ---------------
1110 procedure Set_False (S : in out Suspension_Object) is
1111 Result : STATUS;
1113 begin
1114 SSL.Abort_Defer.all;
1116 Result := semTake (S.L, WAIT_FOREVER);
1117 pragma Assert (Result = OK);
1119 S.State := False;
1121 Result := semGive (S.L);
1122 pragma Assert (Result = OK);
1124 SSL.Abort_Undefer.all;
1125 end Set_False;
1127 --------------
1128 -- Set_True --
1129 --------------
1131 procedure Set_True (S : in out Suspension_Object) is
1132 Result : STATUS;
1134 begin
1135 SSL.Abort_Defer.all;
1137 Result := semTake (S.L, WAIT_FOREVER);
1138 pragma Assert (Result = OK);
1140 -- If there is already a task waiting on this suspension object then
1141 -- we resume it, leaving the state of the suspension object to False,
1142 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1143 -- the state to True.
1145 if S.Waiting then
1146 S.Waiting := False;
1147 S.State := False;
1149 Result := semGive (S.CV);
1150 pragma Assert (Result = OK);
1151 else
1152 S.State := True;
1153 end if;
1155 Result := semGive (S.L);
1156 pragma Assert (Result = OK);
1158 SSL.Abort_Undefer.all;
1159 end Set_True;
1161 ------------------------
1162 -- Suspend_Until_True --
1163 ------------------------
1165 procedure Suspend_Until_True (S : in out Suspension_Object) is
1166 Result : STATUS;
1168 begin
1169 SSL.Abort_Defer.all;
1171 Result := semTake (S.L, WAIT_FOREVER);
1173 if S.Waiting then
1175 -- Program_Error must be raised upon calling Suspend_Until_True
1176 -- if another task is already waiting on that suspension object
1177 -- (ARM D.10 par. 10).
1179 Result := semGive (S.L);
1180 pragma Assert (Result = OK);
1182 SSL.Abort_Undefer.all;
1184 raise Program_Error;
1186 else
1187 -- Suspend the task if the state is False. Otherwise, the task
1188 -- continues its execution, and the state of the suspension object
1189 -- is set to False (ARM D.10 par. 9).
1191 if S.State then
1192 S.State := False;
1194 Result := semGive (S.L);
1195 pragma Assert (Result = 0);
1197 SSL.Abort_Undefer.all;
1199 else
1200 S.Waiting := True;
1202 -- Release the mutex before sleeping
1204 Result := semGive (S.L);
1205 pragma Assert (Result = OK);
1207 SSL.Abort_Undefer.all;
1209 Result := semTake (S.CV, WAIT_FOREVER);
1210 pragma Assert (Result = 0);
1211 end if;
1212 end if;
1213 end Suspend_Until_True;
1215 ----------------
1216 -- Check_Exit --
1217 ----------------
1219 -- Dummy version
1221 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1222 pragma Unreferenced (Self_ID);
1223 begin
1224 return True;
1225 end Check_Exit;
1227 --------------------
1228 -- Check_No_Locks --
1229 --------------------
1231 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1232 pragma Unreferenced (Self_ID);
1233 begin
1234 return True;
1235 end Check_No_Locks;
1237 ----------------------
1238 -- Environment_Task --
1239 ----------------------
1241 function Environment_Task return Task_Id is
1242 begin
1243 return Environment_Task_Id;
1244 end Environment_Task;
1246 --------------
1247 -- Lock_RTS --
1248 --------------
1250 procedure Lock_RTS is
1251 begin
1252 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1253 end Lock_RTS;
1255 ----------------
1256 -- Unlock_RTS --
1257 ----------------
1259 procedure Unlock_RTS is
1260 begin
1261 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1262 end Unlock_RTS;
1264 ------------------
1265 -- Suspend_Task --
1266 ------------------
1268 function Suspend_Task
1269 (T : ST.Task_Id;
1270 Thread_Self : Thread_Id) return Boolean
1272 begin
1273 if T.Common.LL.Thread /= 0
1274 and then T.Common.LL.Thread /= Thread_Self
1275 then
1276 return taskSuspend (T.Common.LL.Thread) = 0;
1277 else
1278 return True;
1279 end if;
1280 end Suspend_Task;
1282 -----------------
1283 -- Resume_Task --
1284 -----------------
1286 function Resume_Task
1287 (T : ST.Task_Id;
1288 Thread_Self : Thread_Id) return Boolean
1290 begin
1291 if T.Common.LL.Thread /= 0
1292 and then T.Common.LL.Thread /= Thread_Self
1293 then
1294 return taskResume (T.Common.LL.Thread) = 0;
1295 else
1296 return True;
1297 end if;
1298 end Resume_Task;
1300 --------------------
1301 -- Stop_All_Tasks --
1302 --------------------
1304 procedure Stop_All_Tasks
1306 Thread_Self : constant Thread_Id := taskIdSelf;
1307 C : Task_Id;
1309 Dummy : int;
1310 pragma Unreferenced (Dummy);
1312 begin
1313 Dummy := Int_Lock;
1315 C := All_Tasks_List;
1316 while C /= null loop
1317 if C.Common.LL.Thread /= 0
1318 and then C.Common.LL.Thread /= Thread_Self
1319 then
1320 Dummy := Task_Stop (C.Common.LL.Thread);
1321 end if;
1323 C := C.Common.All_Tasks_Link;
1324 end loop;
1326 Dummy := Int_Unlock;
1327 end Stop_All_Tasks;
1329 ---------------
1330 -- Stop_Task --
1331 ---------------
1333 function Stop_Task (T : ST.Task_Id) return Boolean is
1334 begin
1335 if T.Common.LL.Thread /= 0 then
1336 return Task_Stop (T.Common.LL.Thread) = 0;
1337 else
1338 return True;
1339 end if;
1340 end Stop_Task;
1342 -------------------
1343 -- Continue_Task --
1344 -------------------
1346 function Continue_Task (T : ST.Task_Id) return Boolean
1348 begin
1349 if T.Common.LL.Thread /= 0 then
1350 return Task_Cont (T.Common.LL.Thread) = 0;
1351 else
1352 return True;
1353 end if;
1354 end Continue_Task;
1356 ----------------
1357 -- Initialize --
1358 ----------------
1360 procedure Initialize (Environment_Task : Task_Id) is
1361 Result : int;
1363 begin
1364 Environment_Task_Id := Environment_Task;
1366 Interrupt_Management.Initialize;
1367 Specific.Initialize;
1369 if Locking_Policy = 'C' then
1370 Mutex_Protocol := Prio_Protect;
1371 elsif Locking_Policy = 'I' then
1372 Mutex_Protocol := Prio_Inherit;
1373 else
1374 Mutex_Protocol := Prio_None;
1375 end if;
1377 if Time_Slice_Val > 0 then
1378 Result :=
1379 Set_Time_Slice
1380 (To_Clock_Ticks
1381 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1383 elsif Dispatching_Policy = 'R' then
1384 Result := Set_Time_Slice (To_Clock_Ticks (0.01));
1386 end if;
1388 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1389 pragma Assert (Result = 0);
1391 for J in Interrupt_Management.Signal_ID loop
1392 if System.Interrupt_Management.Keep_Unmasked (J) then
1393 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1394 pragma Assert (Result = 0);
1395 end if;
1396 end loop;
1398 -- Initialize the lock used to synchronize chain of all ATCBs
1400 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1402 Enter_Task (Environment_Task);
1403 end Initialize;
1405 end System.Task_Primitives.Operations;