Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / s-taprop-vxworks.adb
blob51e7f0cac181b7cd657b1e708884be3795050fd9
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 --------------------
118 -- Local Packages --
119 --------------------
121 package Specific is
123 procedure Initialize;
124 pragma Inline (Initialize);
125 -- Initialize task specific data
127 function Is_Valid_Task return Boolean;
128 pragma Inline (Is_Valid_Task);
129 -- Does executing thread have a TCB?
131 procedure Set (Self_Id : Task_Id);
132 pragma Inline (Set);
133 -- Set the self id for the current task
135 procedure Delete;
136 pragma Inline (Delete);
137 -- Delete the task specific data associated with the current task
139 function Self return Task_Id;
140 pragma Inline (Self);
141 -- Return a pointer to the Ada Task Control Block of the calling task
143 end Specific;
145 package body Specific is separate;
146 -- The body of this package is target specific
148 ---------------------------------
149 -- Support for foreign threads --
150 ---------------------------------
152 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
153 -- Allocate and Initialize a new ATCB for the current Thread
155 function Register_Foreign_Thread
156 (Thread : Thread_Id) return Task_Id is separate;
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 procedure Abort_Handler (signo : Signal);
163 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
165 procedure Install_Signal_Handlers;
166 -- Install the default signal handlers for the current task
168 function To_Address is
169 new Ada.Unchecked_Conversion (Task_Id, System.Address);
171 -------------------
172 -- Abort_Handler --
173 -------------------
175 procedure Abort_Handler (signo : Signal) is
176 pragma Unreferenced (signo);
178 Self_ID : constant Task_Id := Self;
179 Result : int;
180 Old_Set : aliased sigset_t;
182 begin
183 -- It is not safe to raise an exception when using ZCX and the GCC
184 -- exception handling mechanism.
186 if ZCX_By_Default and then GCC_ZCX_Support then
187 return;
188 end if;
190 if Self_ID.Deferral_Level = 0
191 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
192 and then not Self_ID.Aborting
193 then
194 Self_ID.Aborting := True;
196 -- Make sure signals used for RTS internal purpose are unmasked
198 Result :=
199 pthread_sigmask
200 (SIG_UNBLOCK,
201 Unblocked_Signal_Mask'Unchecked_Access,
202 Old_Set'Unchecked_Access);
203 pragma Assert (Result = 0);
205 raise Standard'Abort_Signal;
206 end if;
207 end Abort_Handler;
209 -----------------
210 -- Stack_Guard --
211 -----------------
213 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
214 pragma Unreferenced (T);
215 pragma Unreferenced (On);
217 begin
218 -- Nothing needed (why not???)
220 null;
221 end Stack_Guard;
223 -------------------
224 -- Get_Thread_Id --
225 -------------------
227 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
228 begin
229 return T.Common.LL.Thread;
230 end Get_Thread_Id;
232 ----------
233 -- Self --
234 ----------
236 function Self return Task_Id renames Specific.Self;
238 -----------------------------
239 -- Install_Signal_Handlers --
240 -----------------------------
242 procedure Install_Signal_Handlers is
243 act : aliased struct_sigaction;
244 old_act : aliased struct_sigaction;
245 Tmp_Set : aliased sigset_t;
246 Result : int;
248 begin
249 act.sa_flags := 0;
250 act.sa_handler := Abort_Handler'Address;
252 Result := sigemptyset (Tmp_Set'Access);
253 pragma Assert (Result = 0);
254 act.sa_mask := Tmp_Set;
256 Result :=
257 sigaction
258 (Signal (Interrupt_Management.Abort_Task_Interrupt),
259 act'Unchecked_Access,
260 old_act'Unchecked_Access);
261 pragma Assert (Result = 0);
263 Interrupt_Management.Initialize_Interrupts;
264 end Install_Signal_Handlers;
266 ---------------------
267 -- Initialize_Lock --
268 ---------------------
270 procedure Initialize_Lock
271 (Prio : System.Any_Priority;
272 L : not null access Lock)
274 begin
275 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
276 L.Prio_Ceiling := int (Prio);
277 L.Protocol := Mutex_Protocol;
278 pragma Assert (L.Mutex /= 0);
279 end Initialize_Lock;
281 procedure Initialize_Lock
282 (L : not null access RTS_Lock;
283 Level : Lock_Level)
285 pragma Unreferenced (Level);
286 begin
287 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
288 L.Prio_Ceiling := int (System.Any_Priority'Last);
289 L.Protocol := Mutex_Protocol;
290 pragma Assert (L.Mutex /= 0);
291 end Initialize_Lock;
293 -------------------
294 -- Finalize_Lock --
295 -------------------
297 procedure Finalize_Lock (L : not null access Lock) is
298 Result : int;
299 begin
300 Result := semDelete (L.Mutex);
301 pragma Assert (Result = 0);
302 end Finalize_Lock;
304 procedure Finalize_Lock (L : not null access RTS_Lock) is
305 Result : int;
306 begin
307 Result := semDelete (L.Mutex);
308 pragma Assert (Result = 0);
309 end Finalize_Lock;
311 ----------------
312 -- Write_Lock --
313 ----------------
315 procedure Write_Lock
316 (L : not null access Lock;
317 Ceiling_Violation : out Boolean)
319 Result : int;
321 begin
322 if L.Protocol = Prio_Protect
323 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
324 then
325 Ceiling_Violation := True;
326 return;
327 else
328 Ceiling_Violation := False;
329 end if;
331 Result := semTake (L.Mutex, WAIT_FOREVER);
332 pragma Assert (Result = 0);
333 end Write_Lock;
335 procedure Write_Lock
336 (L : not null access RTS_Lock;
337 Global_Lock : Boolean := False)
339 Result : int;
340 begin
341 if not Single_Lock or else Global_Lock then
342 Result := semTake (L.Mutex, WAIT_FOREVER);
343 pragma Assert (Result = 0);
344 end if;
345 end Write_Lock;
347 procedure Write_Lock (T : Task_Id) is
348 Result : int;
349 begin
350 if not Single_Lock then
351 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
352 pragma Assert (Result = 0);
353 end if;
354 end Write_Lock;
356 ---------------
357 -- Read_Lock --
358 ---------------
360 procedure Read_Lock
361 (L : not null access Lock;
362 Ceiling_Violation : out Boolean)
364 begin
365 Write_Lock (L, Ceiling_Violation);
366 end Read_Lock;
368 ------------
369 -- Unlock --
370 ------------
372 procedure Unlock (L : not null access Lock) is
373 Result : int;
374 begin
375 Result := semGive (L.Mutex);
376 pragma Assert (Result = 0);
377 end Unlock;
379 procedure Unlock
380 (L : not null access RTS_Lock;
381 Global_Lock : Boolean := False)
383 Result : int;
384 begin
385 if not Single_Lock or else Global_Lock then
386 Result := semGive (L.Mutex);
387 pragma Assert (Result = 0);
388 end if;
389 end Unlock;
391 procedure Unlock (T : Task_Id) is
392 Result : int;
393 begin
394 if not Single_Lock then
395 Result := semGive (T.Common.LL.L.Mutex);
396 pragma Assert (Result = 0);
397 end if;
398 end Unlock;
400 -----------------
401 -- Set_Ceiling --
402 -----------------
404 -- Dynamic priority ceilings are not supported by the underlying system
406 procedure Set_Ceiling
407 (L : not null access Lock;
408 Prio : System.Any_Priority)
410 pragma Unreferenced (L, Prio);
411 begin
412 null;
413 end Set_Ceiling;
415 -----------
416 -- Sleep --
417 -----------
419 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
420 pragma Unreferenced (Reason);
422 Result : int;
424 begin
425 pragma Assert (Self_ID = Self);
427 -- Release the mutex before sleeping
429 if Single_Lock then
430 Result := semGive (Single_RTS_Lock.Mutex);
431 else
432 Result := semGive (Self_ID.Common.LL.L.Mutex);
433 end if;
435 pragma Assert (Result = 0);
437 -- Perform a blocking operation to take the CV semaphore. Note that a
438 -- blocking operation in VxWorks will reenable task scheduling. When we
439 -- are no longer blocked and control is returned, task scheduling will
440 -- again be disabled.
442 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
443 pragma Assert (Result = 0);
445 -- Take the mutex back
447 if Single_Lock then
448 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
449 else
450 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
451 end if;
453 pragma Assert (Result = 0);
454 end Sleep;
456 -----------------
457 -- Timed_Sleep --
458 -----------------
460 -- This is for use within the run-time system, so abort is assumed to be
461 -- already deferred, and the caller should be holding its own ATCB lock.
463 procedure Timed_Sleep
464 (Self_ID : Task_Id;
465 Time : Duration;
466 Mode : ST.Delay_Modes;
467 Reason : System.Tasking.Task_States;
468 Timedout : out Boolean;
469 Yielded : out Boolean)
471 pragma Unreferenced (Reason);
473 Orig : constant Duration := Monotonic_Clock;
474 Absolute : Duration;
475 Ticks : int;
476 Result : int;
477 Wakeup : Boolean := False;
479 begin
480 Timedout := False;
481 Yielded := True;
483 if Mode = Relative then
484 Absolute := Orig + Time;
486 -- Systematically add one since the first tick will delay *at most*
487 -- 1 / Rate_Duration seconds, so we need to add one to be on the
488 -- safe side.
490 Ticks := To_Clock_Ticks (Time);
492 if Ticks > 0 and then Ticks < int'Last then
493 Ticks := Ticks + 1;
494 end if;
496 else
497 Absolute := Time;
498 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
499 end if;
501 if Ticks > 0 then
502 loop
503 -- Release the mutex before sleeping
505 if Single_Lock then
506 Result := semGive (Single_RTS_Lock.Mutex);
507 else
508 Result := semGive (Self_ID.Common.LL.L.Mutex);
509 end if;
511 pragma Assert (Result = 0);
513 -- Perform a blocking operation to take the CV semaphore. Note
514 -- that a blocking operation in VxWorks will reenable task
515 -- scheduling. When we are no longer blocked and control is
516 -- returned, task scheduling will again be disabled.
518 Result := semTake (Self_ID.Common.LL.CV, Ticks);
520 if Result = 0 then
522 -- Somebody may have called Wakeup for us
524 Wakeup := True;
526 else
527 if errno /= S_objLib_OBJ_TIMEOUT then
528 Wakeup := True;
530 else
531 -- If Ticks = int'last, it was most probably truncated so
532 -- let's make another round after recomputing Ticks from
533 -- the the absolute time.
535 if Ticks /= int'Last then
536 Timedout := True;
538 else
539 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
541 if Ticks < 0 then
542 Timedout := True;
543 end if;
544 end if;
545 end if;
546 end if;
548 -- Take the mutex back
550 if Single_Lock then
551 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
552 else
553 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
554 end if;
556 pragma Assert (Result = 0);
558 exit when Timedout or Wakeup;
559 end loop;
561 else
562 Timedout := True;
564 -- Should never hold a lock while yielding
566 if Single_Lock then
567 Result := semGive (Single_RTS_Lock.Mutex);
568 taskDelay (0);
569 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
571 else
572 Result := semGive (Self_ID.Common.LL.L.Mutex);
573 taskDelay (0);
574 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
575 end if;
576 end if;
577 end Timed_Sleep;
579 -----------------
580 -- Timed_Delay --
581 -----------------
583 -- This is for use in implementing delay statements, so we assume the
584 -- caller is holding no locks.
586 procedure Timed_Delay
587 (Self_ID : Task_Id;
588 Time : Duration;
589 Mode : ST.Delay_Modes)
591 Orig : constant Duration := Monotonic_Clock;
592 Absolute : Duration;
593 Ticks : int;
594 Timedout : Boolean;
595 Aborted : Boolean := False;
597 Result : int;
598 pragma Warnings (Off, Result);
600 begin
601 if Mode = Relative then
602 Absolute := Orig + Time;
603 Ticks := To_Clock_Ticks (Time);
605 if Ticks > 0 and then Ticks < int'Last then
607 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
608 -- seconds, so we need to add one to be on the safe side.
610 Ticks := Ticks + 1;
611 end if;
613 else
614 Absolute := Time;
615 Ticks := To_Clock_Ticks (Time - Orig);
616 end if;
618 if Ticks > 0 then
620 -- Modifying State, locking the TCB
622 if Single_Lock then
623 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
624 else
625 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
626 end if;
628 pragma Assert (Result = 0);
630 Self_ID.Common.State := Delay_Sleep;
631 Timedout := False;
633 loop
634 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
636 -- Release the TCB before sleeping
638 if Single_Lock then
639 Result := semGive (Single_RTS_Lock.Mutex);
640 else
641 Result := semGive (Self_ID.Common.LL.L.Mutex);
642 end if;
643 pragma Assert (Result = 0);
645 exit when Aborted;
647 Result := semTake (Self_ID.Common.LL.CV, Ticks);
649 if Result /= 0 then
651 -- If Ticks = int'last, it was most probably truncated
652 -- so let's make another round after recomputing Ticks
653 -- from the the absolute time.
655 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
656 Timedout := True;
657 else
658 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
660 if Ticks < 0 then
661 Timedout := True;
662 end if;
663 end if;
664 end if;
666 -- Take back the lock after having slept, to protect further
667 -- access to Self_ID.
669 if Single_Lock then
670 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
671 else
672 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
673 end if;
675 pragma Assert (Result = 0);
677 exit when Timedout;
678 end loop;
680 Self_ID.Common.State := Runnable;
682 if Single_Lock then
683 Result := semGive (Single_RTS_Lock.Mutex);
684 else
685 Result := semGive (Self_ID.Common.LL.L.Mutex);
686 end if;
688 else
689 taskDelay (0);
690 end if;
691 end Timed_Delay;
693 ---------------------
694 -- Monotonic_Clock --
695 ---------------------
697 function Monotonic_Clock return Duration is
698 TS : aliased timespec;
699 Result : int;
700 begin
701 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
702 pragma Assert (Result = 0);
703 return To_Duration (TS);
704 end Monotonic_Clock;
706 -------------------
707 -- RT_Resolution --
708 -------------------
710 function RT_Resolution return Duration is
711 begin
712 return 1.0 / Duration (sysClkRateGet);
713 end RT_Resolution;
715 ------------
716 -- Wakeup --
717 ------------
719 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
720 pragma Unreferenced (Reason);
721 Result : int;
722 begin
723 Result := semGive (T.Common.LL.CV);
724 pragma Assert (Result = 0);
725 end Wakeup;
727 -----------
728 -- Yield --
729 -----------
731 procedure Yield (Do_Yield : Boolean := True) is
732 pragma Unreferenced (Do_Yield);
733 Result : int;
734 pragma Unreferenced (Result);
735 begin
736 Result := taskDelay (0);
737 end Yield;
739 ------------------
740 -- Set_Priority --
741 ------------------
743 type Prio_Array_Type is array (System.Any_Priority) of Integer;
744 pragma Atomic_Components (Prio_Array_Type);
746 Prio_Array : Prio_Array_Type;
747 -- Global array containing the id of the currently running task for
748 -- each priority. Note that we assume that we are on a single processor
749 -- with run-till-blocked scheduling.
751 procedure Set_Priority
752 (T : Task_Id;
753 Prio : System.Any_Priority;
754 Loss_Of_Inheritance : Boolean := False)
756 Array_Item : Integer;
757 Result : int;
759 begin
760 Result :=
761 taskPrioritySet
762 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
763 pragma Assert (Result = 0);
765 if (Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F')
766 and then Loss_Of_Inheritance
767 and then Prio < T.Common.Current_Priority
768 then
769 -- Annex D requirement (RM D.2.2(9))
771 -- If the task drops its priority due to the loss of inherited
772 -- priority, it is added at the head of the ready queue for its
773 -- new active priority.
775 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
776 Prio_Array (T.Common.Base_Priority) := Array_Item;
778 loop
779 -- Give some processes a chance to arrive
781 taskDelay (0);
783 -- Then wait for our turn to proceed
785 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
786 or else Prio_Array (T.Common.Base_Priority) = 1;
787 end loop;
789 Prio_Array (T.Common.Base_Priority) :=
790 Prio_Array (T.Common.Base_Priority) - 1;
791 end if;
793 T.Common.Current_Priority := Prio;
794 end Set_Priority;
796 ------------------
797 -- Get_Priority --
798 ------------------
800 function Get_Priority (T : Task_Id) return System.Any_Priority is
801 begin
802 return T.Common.Current_Priority;
803 end Get_Priority;
805 ----------------
806 -- Enter_Task --
807 ----------------
809 procedure Enter_Task (Self_ID : Task_Id) is
810 procedure Init_Float;
811 pragma Import (C, Init_Float, "__gnat_init_float");
812 -- Properly initializes the FPU for PPC/MIPS systems
814 begin
815 -- Store the user-level task id in the Thread field (to be used
816 -- internally by the run-time system) and the kernel-level task id in
817 -- the LWP field (to be used by the debugger).
819 Self_ID.Common.LL.Thread := taskIdSelf;
820 Self_ID.Common.LL.LWP := getpid;
822 Specific.Set (Self_ID);
824 Init_Float;
826 -- Install the signal handlers
828 -- This is called for each task since there is no signal inheritance
829 -- between VxWorks tasks.
831 Install_Signal_Handlers;
833 Lock_RTS;
835 for J in Known_Tasks'Range loop
836 if Known_Tasks (J) = null then
837 Known_Tasks (J) := Self_ID;
838 Self_ID.Known_Tasks_Index := J;
839 exit;
840 end if;
841 end loop;
843 Unlock_RTS;
844 end Enter_Task;
846 --------------
847 -- New_ATCB --
848 --------------
850 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
851 begin
852 return new Ada_Task_Control_Block (Entry_Num);
853 end New_ATCB;
855 -------------------
856 -- Is_Valid_Task --
857 -------------------
859 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
861 -----------------------------
862 -- Register_Foreign_Thread --
863 -----------------------------
865 function Register_Foreign_Thread return Task_Id is
866 begin
867 if Is_Valid_Task then
868 return Self;
869 else
870 return Register_Foreign_Thread (taskIdSelf);
871 end if;
872 end Register_Foreign_Thread;
874 --------------------
875 -- Initialize_TCB --
876 --------------------
878 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
879 begin
880 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
881 Self_ID.Common.LL.Thread := 0;
883 if Self_ID.Common.LL.CV = 0 then
884 Succeeded := False;
886 else
887 Succeeded := True;
889 if not Single_Lock then
890 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
891 end if;
892 end if;
893 end Initialize_TCB;
895 -----------------
896 -- Create_Task --
897 -----------------
899 procedure Create_Task
900 (T : Task_Id;
901 Wrapper : System.Address;
902 Stack_Size : System.Parameters.Size_Type;
903 Priority : System.Any_Priority;
904 Succeeded : out Boolean)
906 Adjusted_Stack_Size : size_t;
907 begin
908 -- Ask for four extra bytes of stack space so that the ATCB pointer can
909 -- be stored below the stack limit, plus extra space for the frame of
910 -- Task_Wrapper. This is so the user gets the amount of stack requested
911 -- exclusive of the needs.
913 -- We also have to allocate n more bytes for the task name storage and
914 -- enough space for the Wind Task Control Block which is around 0x778
915 -- bytes. VxWorks also seems to carve out additional space, so use 2048
916 -- as a nice round number. We might want to increment to the nearest
917 -- page size in case we ever support VxVMI.
919 -- ??? - we should come back and visit this so we can set the task name
920 -- to something appropriate.
922 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
924 -- Since the initial signal mask of a thread is inherited from the
925 -- creator, and the Environment task has all its signals masked, we do
926 -- not need to manipulate caller's signal mask at this point. All tasks
927 -- in RTS will have All_Tasks_Mask initially.
929 -- We now compute the VxWorks task name and options, then spawn ...
931 declare
932 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
933 Name_Address : System.Address;
934 -- Task name we are going to hand down to VxWorks
936 function Get_Task_Options return int;
937 pragma Import (C, Get_Task_Options, "__gnat_get_task_options");
938 -- Function that returns the options to be set for the task that we
939 -- are creating. We fetch the options assigned to the current task,
940 -- so offering some user level control over the options for a task
941 -- hierarchy, and force VX_FP_TASK because it is almost always
942 -- required.
944 begin
945 -- If there is no Ada task name handy, let VxWorks choose one.
946 -- Otherwise, tell VxWorks what the Ada task name is.
948 if T.Common.Task_Image_Len = 0 then
949 Name_Address := System.Null_Address;
950 else
951 Name (1 .. Name'Last - 1) :=
952 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
953 Name (Name'Last) := ASCII.NUL;
954 Name_Address := Name'Address;
955 end if;
957 -- Now spawn the VxWorks task for real
959 T.Common.LL.Thread :=
960 taskSpawn
961 (Name_Address,
962 To_VxWorks_Priority (int (Priority)),
963 Get_Task_Options,
964 Adjusted_Stack_Size,
965 Wrapper,
966 To_Address (T));
967 end;
969 if T.Common.LL.Thread = -1 then
970 Succeeded := False;
971 else
972 Succeeded := True;
973 end if;
975 Task_Creation_Hook (T.Common.LL.Thread);
976 Set_Priority (T, Priority);
977 end Create_Task;
979 ------------------
980 -- Finalize_TCB --
981 ------------------
983 procedure Finalize_TCB (T : Task_Id) is
984 Result : int;
985 Tmp : Task_Id := T;
986 Is_Self : constant Boolean := (T = Self);
988 procedure Free is new
989 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
991 begin
992 if not Single_Lock then
993 Result := semDelete (T.Common.LL.L.Mutex);
994 pragma Assert (Result = 0);
995 end if;
997 T.Common.LL.Thread := 0;
999 Result := semDelete (T.Common.LL.CV);
1000 pragma Assert (Result = 0);
1002 if T.Known_Tasks_Index /= -1 then
1003 Known_Tasks (T.Known_Tasks_Index) := null;
1004 end if;
1006 Free (Tmp);
1008 if Is_Self then
1009 Specific.Delete;
1010 end if;
1011 end Finalize_TCB;
1013 ---------------
1014 -- Exit_Task --
1015 ---------------
1017 procedure Exit_Task is
1018 begin
1019 Specific.Set (null);
1020 end Exit_Task;
1022 ----------------
1023 -- Abort_Task --
1024 ----------------
1026 procedure Abort_Task (T : Task_Id) is
1027 Result : int;
1028 begin
1029 Result :=
1030 kill
1031 (T.Common.LL.Thread,
1032 Signal (Interrupt_Management.Abort_Task_Interrupt));
1033 pragma Assert (Result = 0);
1034 end Abort_Task;
1036 ----------------
1037 -- Initialize --
1038 ----------------
1040 procedure Initialize (S : in out Suspension_Object) is
1041 begin
1042 -- Initialize internal state (always to False (RM D.10(6)))
1044 S.State := False;
1045 S.Waiting := False;
1047 -- Initialize internal mutex
1049 -- Use simpler binary semaphore instead of VxWorks
1050 -- mutual exclusion semaphore, because we don't need
1051 -- the fancier semantics and their overhead.
1053 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1055 -- Initialize internal condition variable
1057 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1058 end Initialize;
1060 --------------
1061 -- Finalize --
1062 --------------
1064 procedure Finalize (S : in out Suspension_Object) is
1065 Result : STATUS;
1067 begin
1068 -- Destroy internal mutex
1070 Result := semDelete (S.L);
1071 pragma Assert (Result = OK);
1073 -- Destroy internal condition variable
1075 Result := semDelete (S.CV);
1076 pragma Assert (Result = OK);
1077 end Finalize;
1079 -------------------
1080 -- Current_State --
1081 -------------------
1083 function Current_State (S : Suspension_Object) return Boolean is
1084 begin
1085 -- We do not want to use lock on this read operation. State is marked
1086 -- as Atomic so that we ensure that the value retrieved is correct.
1088 return S.State;
1089 end Current_State;
1091 ---------------
1092 -- Set_False --
1093 ---------------
1095 procedure Set_False (S : in out Suspension_Object) is
1096 Result : STATUS;
1098 begin
1099 SSL.Abort_Defer.all;
1101 Result := semTake (S.L, WAIT_FOREVER);
1102 pragma Assert (Result = OK);
1104 S.State := False;
1106 Result := semGive (S.L);
1107 pragma Assert (Result = OK);
1109 SSL.Abort_Undefer.all;
1110 end Set_False;
1112 --------------
1113 -- Set_True --
1114 --------------
1116 procedure Set_True (S : in out Suspension_Object) is
1117 Result : STATUS;
1119 begin
1120 SSL.Abort_Defer.all;
1122 Result := semTake (S.L, WAIT_FOREVER);
1123 pragma Assert (Result = OK);
1125 -- If there is already a task waiting on this suspension object then
1126 -- we resume it, leaving the state of the suspension object to False,
1127 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1128 -- the state to True.
1130 if S.Waiting then
1131 S.Waiting := False;
1132 S.State := False;
1134 Result := semGive (S.CV);
1135 pragma Assert (Result = OK);
1136 else
1137 S.State := True;
1138 end if;
1140 Result := semGive (S.L);
1141 pragma Assert (Result = OK);
1143 SSL.Abort_Undefer.all;
1144 end Set_True;
1146 ------------------------
1147 -- Suspend_Until_True --
1148 ------------------------
1150 procedure Suspend_Until_True (S : in out Suspension_Object) is
1151 Result : STATUS;
1153 begin
1154 SSL.Abort_Defer.all;
1156 Result := semTake (S.L, WAIT_FOREVER);
1158 if S.Waiting then
1160 -- Program_Error must be raised upon calling Suspend_Until_True
1161 -- if another task is already waiting on that suspension object
1162 -- (ARM D.10 par. 10).
1164 Result := semGive (S.L);
1165 pragma Assert (Result = OK);
1167 SSL.Abort_Undefer.all;
1169 raise Program_Error;
1171 else
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1176 if S.State then
1177 S.State := False;
1179 Result := semGive (S.L);
1180 pragma Assert (Result = 0);
1182 SSL.Abort_Undefer.all;
1184 else
1185 S.Waiting := True;
1187 -- Release the mutex before sleeping
1189 Result := semGive (S.L);
1190 pragma Assert (Result = OK);
1192 SSL.Abort_Undefer.all;
1194 Result := semTake (S.CV, WAIT_FOREVER);
1195 pragma Assert (Result = 0);
1196 end if;
1197 end if;
1198 end Suspend_Until_True;
1200 ----------------
1201 -- Check_Exit --
1202 ----------------
1204 -- Dummy version
1206 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1207 pragma Unreferenced (Self_ID);
1208 begin
1209 return True;
1210 end Check_Exit;
1212 --------------------
1213 -- Check_No_Locks --
1214 --------------------
1216 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1217 pragma Unreferenced (Self_ID);
1218 begin
1219 return True;
1220 end Check_No_Locks;
1222 ----------------------
1223 -- Environment_Task --
1224 ----------------------
1226 function Environment_Task return Task_Id is
1227 begin
1228 return Environment_Task_Id;
1229 end Environment_Task;
1231 --------------
1232 -- Lock_RTS --
1233 --------------
1235 procedure Lock_RTS is
1236 begin
1237 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1238 end Lock_RTS;
1240 ----------------
1241 -- Unlock_RTS --
1242 ----------------
1244 procedure Unlock_RTS is
1245 begin
1246 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1247 end Unlock_RTS;
1249 ------------------
1250 -- Suspend_Task --
1251 ------------------
1253 function Suspend_Task
1254 (T : ST.Task_Id;
1255 Thread_Self : Thread_Id) return Boolean
1257 begin
1258 if T.Common.LL.Thread /= 0
1259 and then T.Common.LL.Thread /= Thread_Self
1260 then
1261 return taskSuspend (T.Common.LL.Thread) = 0;
1262 else
1263 return True;
1264 end if;
1265 end Suspend_Task;
1267 -----------------
1268 -- Resume_Task --
1269 -----------------
1271 function Resume_Task
1272 (T : ST.Task_Id;
1273 Thread_Self : Thread_Id) return Boolean
1275 begin
1276 if T.Common.LL.Thread /= 0
1277 and then T.Common.LL.Thread /= Thread_Self
1278 then
1279 return taskResume (T.Common.LL.Thread) = 0;
1280 else
1281 return True;
1282 end if;
1283 end Resume_Task;
1285 --------------------
1286 -- Stop_All_Tasks --
1287 --------------------
1289 procedure Stop_All_Tasks
1291 Thread_Self : constant Thread_Id := taskIdSelf;
1292 C : Task_Id;
1294 Dummy : int;
1295 pragma Unreferenced (Dummy);
1297 begin
1298 Dummy := Int_Lock;
1300 C := All_Tasks_List;
1301 while C /= null loop
1302 if C.Common.LL.Thread /= 0
1303 and then C.Common.LL.Thread /= Thread_Self
1304 then
1305 Dummy := Task_Stop (C.Common.LL.Thread);
1306 end if;
1308 C := C.Common.All_Tasks_Link;
1309 end loop;
1311 Dummy := Int_Unlock;
1312 end Stop_All_Tasks;
1314 ---------------
1315 -- Stop_Task --
1316 ---------------
1318 function Stop_Task (T : ST.Task_Id) return Boolean is
1319 begin
1320 if T.Common.LL.Thread /= 0 then
1321 return Task_Stop (T.Common.LL.Thread) = 0;
1322 else
1323 return True;
1324 end if;
1325 end Stop_Task;
1327 -------------------
1328 -- Continue_Task --
1329 -------------------
1331 function Continue_Task (T : ST.Task_Id) return Boolean
1333 begin
1334 if T.Common.LL.Thread /= 0 then
1335 return Task_Cont (T.Common.LL.Thread) = 0;
1336 else
1337 return True;
1338 end if;
1339 end Continue_Task;
1341 ----------------
1342 -- Initialize --
1343 ----------------
1345 procedure Initialize (Environment_Task : Task_Id) is
1346 Result : int;
1348 begin
1349 Environment_Task_Id := Environment_Task;
1351 Interrupt_Management.Initialize;
1352 Specific.Initialize;
1354 if Locking_Policy = 'C' then
1355 Mutex_Protocol := Prio_Protect;
1356 elsif Locking_Policy = 'I' then
1357 Mutex_Protocol := Prio_Inherit;
1358 else
1359 Mutex_Protocol := Prio_None;
1360 end if;
1362 if Time_Slice_Val > 0 then
1363 Result :=
1364 Set_Time_Slice
1365 (To_Clock_Ticks
1366 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1368 elsif Dispatching_Policy = 'R' then
1369 Result := Set_Time_Slice (To_Clock_Ticks (0.01));
1371 end if;
1373 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1374 pragma Assert (Result = 0);
1376 for J in Interrupt_Management.Signal_ID loop
1377 if System.Interrupt_Management.Keep_Unmasked (J) then
1378 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1379 pragma Assert (Result = 0);
1380 end if;
1381 end loop;
1383 -- Initialize the lock used to synchronize chain of all ATCBs
1385 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1387 Enter_Task (Environment_Task);
1388 end Initialize;
1390 end System.Task_Primitives.Operations;