Dead
[official-gcc.git] / gomp-20050608-branch / gcc / ada / s-taprop-vxworks.adb
blob56a02dda238137827d8f28b4cc704f42f64debb2
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-2006, 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_Signal
49 -- Signal_ID
50 -- Initialize_Interrupts
52 with Interfaces.C;
54 with Unchecked_Conversion;
55 with Unchecked_Deallocation;
57 package body System.Task_Primitives.Operations is
59 use System.Tasking.Debug;
60 use System.Tasking;
61 use System.OS_Interface;
62 use System.Parameters;
63 use type Interfaces.C.int;
65 subtype int is System.OS_Interface.int;
67 Relative : constant := 0;
69 ----------------
70 -- Local Data --
71 ----------------
73 -- The followings are logically constants, but need to be initialized at
74 -- run time.
76 Single_RTS_Lock : aliased RTS_Lock;
77 -- This is a lock to allow only one thread of control in the RTS at a
78 -- time; it is used to execute in mutual exclusion from all other tasks.
79 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
81 Environment_Task_Id : Task_Id;
82 -- A variable to hold Task_Id for the environment task
84 Unblocked_Signal_Mask : aliased sigset_t;
85 -- The set of signals that should unblocked in all tasks
87 -- The followings are internal configuration constants needed
89 Time_Slice_Val : Integer;
90 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
92 Locking_Policy : Character;
93 pragma Import (C, Locking_Policy, "__gl_locking_policy");
95 Dispatching_Policy : Character;
96 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
98 Mutex_Protocol : Priority_Type;
100 Foreign_Task_Elaborated : aliased Boolean := True;
101 -- Used to identified fake tasks (i.e., non-Ada Threads)
103 --------------------
104 -- Local Packages --
105 --------------------
107 package Specific is
109 procedure Initialize;
110 pragma Inline (Initialize);
111 -- Initialize task specific data
113 function Is_Valid_Task return Boolean;
114 pragma Inline (Is_Valid_Task);
115 -- Does executing thread have a TCB?
117 procedure Set (Self_Id : Task_Id);
118 pragma Inline (Set);
119 -- Set the self id for the current task
121 procedure Delete;
122 pragma Inline (Delete);
123 -- Delete the task specific data associated with the current task
125 function Self return Task_Id;
126 pragma Inline (Self);
127 -- Return a pointer to the Ada Task Control Block of the calling task
129 end Specific;
131 package body Specific is separate;
132 -- The body of this package is target specific
134 ---------------------------------
135 -- Support for foreign threads --
136 ---------------------------------
138 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
139 -- Allocate and Initialize a new ATCB for the current Thread
141 function Register_Foreign_Thread
142 (Thread : Thread_Id) return Task_Id is separate;
144 -----------------------
145 -- Local Subprograms --
146 -----------------------
148 procedure Abort_Handler (signo : Signal);
149 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
151 procedure Install_Signal_Handlers;
152 -- Install the default signal handlers for the current task
154 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
156 -------------------
157 -- Abort_Handler --
158 -------------------
160 procedure Abort_Handler (signo : Signal) is
161 pragma Unreferenced (signo);
163 Self_ID : constant Task_Id := Self;
164 Result : int;
165 Old_Set : aliased sigset_t;
167 begin
168 -- It is not safe to raise an exception when using ZCX and the GCC
169 -- exception handling mechanism.
171 if ZCX_By_Default and then GCC_ZCX_Support then
172 return;
173 end if;
175 if Self_ID.Deferral_Level = 0
176 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
177 and then not Self_ID.Aborting
178 then
179 Self_ID.Aborting := True;
181 -- Make sure signals used for RTS internal purpose are unmasked
183 Result := pthread_sigmask (SIG_UNBLOCK,
184 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
185 pragma Assert (Result = 0);
187 raise Standard'Abort_Signal;
188 end if;
189 end Abort_Handler;
191 -----------------
192 -- Stack_Guard --
193 -----------------
195 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
196 pragma Unreferenced (T);
197 pragma Unreferenced (On);
199 begin
200 -- Nothing needed (why not???)
202 null;
203 end Stack_Guard;
205 -------------------
206 -- Get_Thread_Id --
207 -------------------
209 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
210 begin
211 return T.Common.LL.Thread;
212 end Get_Thread_Id;
214 ----------
215 -- Self --
216 ----------
218 function Self return Task_Id renames Specific.Self;
220 -----------------------------
221 -- Install_Signal_Handlers --
222 -----------------------------
224 procedure Install_Signal_Handlers is
225 act : aliased struct_sigaction;
226 old_act : aliased struct_sigaction;
227 Tmp_Set : aliased sigset_t;
228 Result : int;
230 begin
231 act.sa_flags := 0;
232 act.sa_handler := Abort_Handler'Address;
234 Result := sigemptyset (Tmp_Set'Access);
235 pragma Assert (Result = 0);
236 act.sa_mask := Tmp_Set;
238 Result :=
239 sigaction
240 (Signal (Interrupt_Management.Abort_Task_Signal),
241 act'Unchecked_Access,
242 old_act'Unchecked_Access);
243 pragma Assert (Result = 0);
245 Interrupt_Management.Initialize_Interrupts;
246 end Install_Signal_Handlers;
248 ---------------------
249 -- Initialize_Lock --
250 ---------------------
252 procedure Initialize_Lock (Prio : System.Any_Priority; L : access Lock) is
253 begin
254 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
255 L.Prio_Ceiling := int (Prio);
256 L.Protocol := Mutex_Protocol;
257 pragma Assert (L.Mutex /= 0);
258 end Initialize_Lock;
260 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
261 pragma Unreferenced (Level);
263 begin
264 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
265 L.Prio_Ceiling := int (System.Any_Priority'Last);
266 L.Protocol := Mutex_Protocol;
267 pragma Assert (L.Mutex /= 0);
268 end Initialize_Lock;
270 -------------------
271 -- Finalize_Lock --
272 -------------------
274 procedure Finalize_Lock (L : access Lock) is
275 Result : int;
276 begin
277 Result := semDelete (L.Mutex);
278 pragma Assert (Result = 0);
279 end Finalize_Lock;
281 procedure Finalize_Lock (L : access RTS_Lock) is
282 Result : int;
283 begin
284 Result := semDelete (L.Mutex);
285 pragma Assert (Result = 0);
286 end Finalize_Lock;
288 ----------------
289 -- Write_Lock --
290 ----------------
292 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
293 Result : int;
294 begin
295 if L.Protocol = Prio_Protect
296 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
297 then
298 Ceiling_Violation := True;
299 return;
300 else
301 Ceiling_Violation := False;
302 end if;
304 Result := semTake (L.Mutex, WAIT_FOREVER);
305 pragma Assert (Result = 0);
306 end Write_Lock;
308 procedure Write_Lock
309 (L : access RTS_Lock;
310 Global_Lock : Boolean := False)
312 Result : int;
313 begin
314 if not Single_Lock or else Global_Lock then
315 Result := semTake (L.Mutex, WAIT_FOREVER);
316 pragma Assert (Result = 0);
317 end if;
318 end Write_Lock;
320 procedure Write_Lock (T : Task_Id) is
321 Result : int;
322 begin
323 if not Single_Lock then
324 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
325 pragma Assert (Result = 0);
326 end if;
327 end Write_Lock;
329 ---------------
330 -- Read_Lock --
331 ---------------
333 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
334 begin
335 Write_Lock (L, Ceiling_Violation);
336 end Read_Lock;
338 ------------
339 -- Unlock --
340 ------------
342 procedure Unlock (L : access Lock) is
343 Result : int;
344 begin
345 Result := semGive (L.Mutex);
346 pragma Assert (Result = 0);
347 end Unlock;
349 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
350 Result : int;
351 begin
352 if not Single_Lock or else Global_Lock then
353 Result := semGive (L.Mutex);
354 pragma Assert (Result = 0);
355 end if;
356 end Unlock;
358 procedure Unlock (T : Task_Id) is
359 Result : int;
360 begin
361 if not Single_Lock then
362 Result := semGive (T.Common.LL.L.Mutex);
363 pragma Assert (Result = 0);
364 end if;
365 end Unlock;
367 -----------
368 -- Sleep --
369 -----------
371 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
372 pragma Unreferenced (Reason);
374 Result : int;
376 begin
377 pragma Assert (Self_ID = Self);
379 -- Release the mutex before sleeping
381 if Single_Lock then
382 Result := semGive (Single_RTS_Lock.Mutex);
383 else
384 Result := semGive (Self_ID.Common.LL.L.Mutex);
385 end if;
387 pragma Assert (Result = 0);
389 -- Perform a blocking operation to take the CV semaphore. Note that a
390 -- blocking operation in VxWorks will reenable task scheduling. When we
391 -- are no longer blocked and control is returned, task scheduling will
392 -- again be disabled.
394 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
395 pragma Assert (Result = 0);
397 -- Take the mutex back
399 if Single_Lock then
400 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
401 else
402 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
403 end if;
405 pragma Assert (Result = 0);
406 end Sleep;
408 -----------------
409 -- Timed_Sleep --
410 -----------------
412 -- This is for use within the run-time system, so abort is assumed to be
413 -- already deferred, and the caller should be holding its own ATCB lock.
415 procedure Timed_Sleep
416 (Self_ID : Task_Id;
417 Time : Duration;
418 Mode : ST.Delay_Modes;
419 Reason : System.Tasking.Task_States;
420 Timedout : out Boolean;
421 Yielded : out Boolean)
423 pragma Unreferenced (Reason);
425 Orig : constant Duration := Monotonic_Clock;
426 Absolute : Duration;
427 Ticks : int;
428 Result : int;
429 Wakeup : Boolean := False;
431 begin
432 Timedout := False;
433 Yielded := True;
435 if Mode = Relative then
436 Absolute := Orig + Time;
438 -- Systematically add one since the first tick will delay *at most*
439 -- 1 / Rate_Duration seconds, so we need to add one to be on the
440 -- safe side.
442 Ticks := To_Clock_Ticks (Time);
444 if Ticks > 0 and then Ticks < int'Last then
445 Ticks := Ticks + 1;
446 end if;
448 else
449 Absolute := Time;
450 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
451 end if;
453 if Ticks > 0 then
454 loop
455 -- Release the mutex before sleeping
457 if Single_Lock then
458 Result := semGive (Single_RTS_Lock.Mutex);
459 else
460 Result := semGive (Self_ID.Common.LL.L.Mutex);
461 end if;
463 pragma Assert (Result = 0);
465 -- Perform a blocking operation to take the CV semaphore. Note
466 -- that a blocking operation in VxWorks will reenable task
467 -- scheduling. When we are no longer blocked and control is
468 -- returned, task scheduling will again be disabled.
470 Result := semTake (Self_ID.Common.LL.CV, Ticks);
472 if Result = 0 then
474 -- Somebody may have called Wakeup for us
476 Wakeup := True;
478 else
479 if errno /= S_objLib_OBJ_TIMEOUT then
480 Wakeup := True;
482 else
483 -- If Ticks = int'last, it was most probably truncated so
484 -- let's make another round after recomputing Ticks from
485 -- the the absolute time.
487 if Ticks /= int'Last then
488 Timedout := True;
489 else
490 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
492 if Ticks < 0 then
493 Timedout := True;
494 end if;
495 end if;
496 end if;
497 end if;
499 -- Take the mutex back
501 if Single_Lock then
502 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
503 else
504 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
505 end if;
507 pragma Assert (Result = 0);
509 exit when Timedout or Wakeup;
510 end loop;
512 else
513 Timedout := True;
515 -- Should never hold a lock while yielding
517 if Single_Lock then
518 Result := semGive (Single_RTS_Lock.Mutex);
519 taskDelay (0);
520 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
522 else
523 Result := semGive (Self_ID.Common.LL.L.Mutex);
524 taskDelay (0);
525 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
526 end if;
527 end if;
528 end Timed_Sleep;
530 -----------------
531 -- Timed_Delay --
532 -----------------
534 -- This is for use in implementing delay statements, so we assume the
535 -- caller is holding no locks.
537 procedure Timed_Delay
538 (Self_ID : Task_Id;
539 Time : Duration;
540 Mode : ST.Delay_Modes)
542 Orig : constant Duration := Monotonic_Clock;
543 Absolute : Duration;
544 Ticks : int;
545 Timedout : Boolean;
546 Result : int;
547 Aborted : Boolean := False;
549 begin
550 if Mode = Relative then
551 Absolute := Orig + Time;
552 Ticks := To_Clock_Ticks (Time);
554 if Ticks > 0 and then Ticks < int'Last then
556 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
557 -- seconds, so we need to add one to be on the safe side.
559 Ticks := Ticks + 1;
560 end if;
562 else
563 Absolute := Time;
564 Ticks := To_Clock_Ticks (Time - Orig);
565 end if;
567 if Ticks > 0 then
569 -- Modifying State and Pending_Priority_Change, locking the TCB
571 if Single_Lock then
572 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
573 else
574 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
575 end if;
577 pragma Assert (Result = 0);
579 Self_ID.Common.State := Delay_Sleep;
580 Timedout := False;
582 loop
583 if Self_ID.Pending_Priority_Change then
584 Self_ID.Pending_Priority_Change := False;
585 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
586 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
587 end if;
589 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
591 -- Release the TCB before sleeping
593 if Single_Lock then
594 Result := semGive (Single_RTS_Lock.Mutex);
595 else
596 Result := semGive (Self_ID.Common.LL.L.Mutex);
597 end if;
598 pragma Assert (Result = 0);
600 exit when Aborted;
602 Result := semTake (Self_ID.Common.LL.CV, Ticks);
604 if Result /= 0 then
606 -- If Ticks = int'last, it was most probably truncated
607 -- so let's make another round after recomputing Ticks
608 -- from the the absolute time.
610 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
611 Timedout := True;
612 else
613 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
615 if Ticks < 0 then
616 Timedout := True;
617 end if;
618 end if;
619 end if;
621 -- Take back the lock after having slept, to protect further
622 -- access to Self_ID.
624 if Single_Lock then
625 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
626 else
627 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
628 end if;
630 pragma Assert (Result = 0);
632 exit when Timedout;
633 end loop;
635 Self_ID.Common.State := Runnable;
637 if Single_Lock then
638 Result := semGive (Single_RTS_Lock.Mutex);
639 else
640 Result := semGive (Self_ID.Common.LL.L.Mutex);
641 end if;
643 else
644 taskDelay (0);
645 end if;
646 end Timed_Delay;
648 ---------------------
649 -- Monotonic_Clock --
650 ---------------------
652 function Monotonic_Clock return Duration is
653 TS : aliased timespec;
654 Result : int;
655 begin
656 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
657 pragma Assert (Result = 0);
658 return To_Duration (TS);
659 end Monotonic_Clock;
661 -------------------
662 -- RT_Resolution --
663 -------------------
665 function RT_Resolution return Duration is
666 begin
667 return 1.0 / Duration (sysClkRateGet);
668 end RT_Resolution;
670 ------------
671 -- Wakeup --
672 ------------
674 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
675 pragma Unreferenced (Reason);
676 Result : int;
677 begin
678 Result := semGive (T.Common.LL.CV);
679 pragma Assert (Result = 0);
680 end Wakeup;
682 -----------
683 -- Yield --
684 -----------
686 procedure Yield (Do_Yield : Boolean := True) is
687 pragma Unreferenced (Do_Yield);
688 Result : int;
689 pragma Unreferenced (Result);
690 begin
691 Result := taskDelay (0);
692 end Yield;
694 ------------------
695 -- Set_Priority --
696 ------------------
698 type Prio_Array_Type is array (System.Any_Priority) of Integer;
699 pragma Atomic_Components (Prio_Array_Type);
701 Prio_Array : Prio_Array_Type;
702 -- Global array containing the id of the currently running task for
703 -- each priority. Note that we assume that we are on a single processor
704 -- with run-till-blocked scheduling.
706 procedure Set_Priority
707 (T : Task_Id;
708 Prio : System.Any_Priority;
709 Loss_Of_Inheritance : Boolean := False)
711 Array_Item : Integer;
712 Result : int;
714 begin
715 Result :=
716 taskPrioritySet
717 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
718 pragma Assert (Result = 0);
720 if Dispatching_Policy = 'F' then
722 -- Annex D requirement [RM D.2.2 par. 9]:
724 -- If the task drops its priority due to the loss of inherited
725 -- priority, it is added at the head of the ready queue for its
726 -- new active priority.
728 if Loss_Of_Inheritance
729 and then Prio < T.Common.Current_Priority
730 then
731 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
732 Prio_Array (T.Common.Base_Priority) := Array_Item;
734 loop
735 -- Give some processes a chance to arrive
737 taskDelay (0);
739 -- Then wait for our turn to proceed
741 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
742 or else Prio_Array (T.Common.Base_Priority) = 1;
743 end loop;
745 Prio_Array (T.Common.Base_Priority) :=
746 Prio_Array (T.Common.Base_Priority) - 1;
747 end if;
748 end if;
750 T.Common.Current_Priority := Prio;
751 end Set_Priority;
753 ------------------
754 -- Get_Priority --
755 ------------------
757 function Get_Priority (T : Task_Id) return System.Any_Priority is
758 begin
759 return T.Common.Current_Priority;
760 end Get_Priority;
762 ----------------
763 -- Enter_Task --
764 ----------------
766 procedure Enter_Task (Self_ID : Task_Id) is
767 procedure Init_Float;
768 pragma Import (C, Init_Float, "__gnat_init_float");
769 -- Properly initializes the FPU for PPC/MIPS systems
771 begin
772 Self_ID.Common.LL.Thread := taskIdSelf;
773 Specific.Set (Self_ID);
775 Init_Float;
777 -- Install the signal handlers
779 -- This is called for each task since there is no signal inheritance
780 -- between VxWorks tasks.
782 Install_Signal_Handlers;
784 Lock_RTS;
786 for J in Known_Tasks'Range loop
787 if Known_Tasks (J) = null then
788 Known_Tasks (J) := Self_ID;
789 Self_ID.Known_Tasks_Index := J;
790 exit;
791 end if;
792 end loop;
794 Unlock_RTS;
795 end Enter_Task;
797 --------------
798 -- New_ATCB --
799 --------------
801 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
802 begin
803 return new Ada_Task_Control_Block (Entry_Num);
804 end New_ATCB;
806 -------------------
807 -- Is_Valid_Task --
808 -------------------
810 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
812 -----------------------------
813 -- Register_Foreign_Thread --
814 -----------------------------
816 function Register_Foreign_Thread return Task_Id is
817 begin
818 if Is_Valid_Task then
819 return Self;
820 else
821 return Register_Foreign_Thread (taskIdSelf);
822 end if;
823 end Register_Foreign_Thread;
825 --------------------
826 -- Initialize_TCB --
827 --------------------
829 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
830 begin
831 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
832 Self_ID.Common.LL.Thread := 0;
834 if Self_ID.Common.LL.CV = 0 then
835 Succeeded := False;
836 else
837 Succeeded := True;
839 if not Single_Lock then
840 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
841 end if;
842 end if;
843 end Initialize_TCB;
845 -----------------
846 -- Create_Task --
847 -----------------
849 procedure Create_Task
850 (T : Task_Id;
851 Wrapper : System.Address;
852 Stack_Size : System.Parameters.Size_Type;
853 Priority : System.Any_Priority;
854 Succeeded : out Boolean)
856 Adjusted_Stack_Size : size_t;
857 begin
858 -- Ask for four extra bytes of stack space so that the ATCB pointer can
859 -- be stored below the stack limit, plus extra space for the frame of
860 -- Task_Wrapper. This is so the user gets the amount of stack requested
861 -- exclusive of the needs.
863 -- We also have to allocate n more bytes for the task name storage and
864 -- enough space for the Wind Task Control Block which is around 0x778
865 -- bytes. VxWorks also seems to carve out additional space, so use 2048
866 -- as a nice round number. We might want to increment to the nearest
867 -- page size in case we ever support VxVMI.
869 -- ??? - we should come back and visit this so we can set the task name
870 -- to something appropriate.
872 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
874 -- Since the initial signal mask of a thread is inherited from the
875 -- creator, and the Environment task has all its signals masked, we do
876 -- not need to manipulate caller's signal mask at this point. All tasks
877 -- in RTS will have All_Tasks_Mask initially.
879 if T.Common.Task_Image_Len = 0 then
880 T.Common.LL.Thread := taskSpawn
881 (System.Null_Address,
882 To_VxWorks_Priority (int (Priority)),
883 VX_FP_TASK,
884 Adjusted_Stack_Size,
885 Wrapper,
886 To_Address (T));
887 else
888 declare
889 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
891 begin
892 Name (1 .. Name'Last - 1) :=
893 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
894 Name (Name'Last) := ASCII.NUL;
896 T.Common.LL.Thread := taskSpawn
897 (Name'Address,
898 To_VxWorks_Priority (int (Priority)),
899 VX_FP_TASK,
900 Adjusted_Stack_Size,
901 Wrapper,
902 To_Address (T));
903 end;
904 end if;
906 if T.Common.LL.Thread = -1 then
907 Succeeded := False;
908 else
909 Succeeded := True;
910 end if;
912 Task_Creation_Hook (T.Common.LL.Thread);
913 Set_Priority (T, Priority);
914 end Create_Task;
916 ------------------
917 -- Finalize_TCB --
918 ------------------
920 procedure Finalize_TCB (T : Task_Id) is
921 Result : int;
922 Tmp : Task_Id := T;
923 Is_Self : constant Boolean := (T = Self);
925 procedure Free is new
926 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
928 begin
929 if not Single_Lock then
930 Result := semDelete (T.Common.LL.L.Mutex);
931 pragma Assert (Result = 0);
932 end if;
934 T.Common.LL.Thread := 0;
936 Result := semDelete (T.Common.LL.CV);
937 pragma Assert (Result = 0);
939 if T.Known_Tasks_Index /= -1 then
940 Known_Tasks (T.Known_Tasks_Index) := null;
941 end if;
943 Free (Tmp);
945 if Is_Self then
946 Specific.Delete;
947 end if;
948 end Finalize_TCB;
950 ---------------
951 -- Exit_Task --
952 ---------------
954 procedure Exit_Task is
955 begin
956 Specific.Set (null);
957 end Exit_Task;
959 ----------------
960 -- Abort_Task --
961 ----------------
963 procedure Abort_Task (T : Task_Id) is
964 Result : int;
965 begin
966 Result := kill (T.Common.LL.Thread,
967 Signal (Interrupt_Management.Abort_Task_Signal));
968 pragma Assert (Result = 0);
969 end Abort_Task;
971 ----------------
972 -- Initialize --
973 ----------------
975 procedure Initialize (S : in out Suspension_Object) is
976 begin
977 -- Initialize internal state. It is always initialized to False (ARM
978 -- D.10 par. 6).
980 S.State := False;
981 S.Waiting := False;
983 -- Initialize internal mutex
985 -- Use simpler binary semaphore instead of VxWorks
986 -- mutual exclusion semaphore, because we don't need
987 -- the fancier semantics and their overhead.
989 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
991 -- Initialize internal condition variable
993 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
994 end Initialize;
996 --------------
997 -- Finalize --
998 --------------
1000 procedure Finalize (S : in out Suspension_Object) is
1001 Result : STATUS;
1002 begin
1003 -- Destroy internal mutex
1005 Result := semDelete (S.L);
1006 pragma Assert (Result = OK);
1008 -- Destroy internal condition variable
1010 Result := semDelete (S.CV);
1011 pragma Assert (Result = OK);
1012 end Finalize;
1014 -------------------
1015 -- Current_State --
1016 -------------------
1018 function Current_State (S : Suspension_Object) return Boolean is
1019 begin
1020 -- We do not want to use lock on this read operation. State is marked
1021 -- as Atomic so that we ensure that the value retrieved is correct.
1023 return S.State;
1024 end Current_State;
1026 ---------------
1027 -- Set_False --
1028 ---------------
1030 procedure Set_False (S : in out Suspension_Object) is
1031 Result : STATUS;
1032 begin
1033 Result := semTake (S.L, WAIT_FOREVER);
1034 pragma Assert (Result = OK);
1036 S.State := False;
1038 Result := semGive (S.L);
1039 pragma Assert (Result = OK);
1040 end Set_False;
1042 --------------
1043 -- Set_True --
1044 --------------
1046 procedure Set_True (S : in out Suspension_Object) is
1047 Result : STATUS;
1048 begin
1049 Result := semTake (S.L, WAIT_FOREVER);
1050 pragma Assert (Result = OK);
1052 -- If there is already a task waiting on this suspension object then
1053 -- we resume it, leaving the state of the suspension object to False,
1054 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1055 -- the state to True.
1057 if S.Waiting then
1058 S.Waiting := False;
1059 S.State := False;
1061 Result := semGive (S.CV);
1062 pragma Assert (Result = OK);
1063 else
1064 S.State := True;
1065 end if;
1067 Result := semGive (S.L);
1068 pragma Assert (Result = OK);
1069 end Set_True;
1071 ------------------------
1072 -- Suspend_Until_True --
1073 ------------------------
1075 procedure Suspend_Until_True (S : in out Suspension_Object) is
1076 Result : STATUS;
1077 begin
1078 Result := semTake (S.L, WAIT_FOREVER);
1080 if S.Waiting then
1081 -- Program_Error must be raised upon calling Suspend_Until_True
1082 -- if another task is already waiting on that suspension object
1083 -- (ARM D.10 par. 10).
1085 Result := semGive (S.L);
1086 pragma Assert (Result = OK);
1088 raise Program_Error;
1089 else
1090 -- Suspend the task if the state is False. Otherwise, the task
1091 -- continues its execution, and the state of the suspension object
1092 -- is set to False (ARM D.10 par. 9).
1094 if S.State then
1095 S.State := False;
1097 Result := semGive (S.L);
1098 pragma Assert (Result = 0);
1099 else
1100 S.Waiting := True;
1102 -- Release the mutex before sleeping
1104 Result := semGive (S.L);
1105 pragma Assert (Result = OK);
1107 Result := semTake (S.CV, WAIT_FOREVER);
1108 pragma Assert (Result = 0);
1109 end if;
1110 end if;
1111 end Suspend_Until_True;
1113 ----------------
1114 -- Check_Exit --
1115 ----------------
1117 -- Dummy version
1119 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1120 pragma Unreferenced (Self_ID);
1121 begin
1122 return True;
1123 end Check_Exit;
1125 --------------------
1126 -- Check_No_Locks --
1127 --------------------
1129 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1130 pragma Unreferenced (Self_ID);
1131 begin
1132 return True;
1133 end Check_No_Locks;
1135 ----------------------
1136 -- Environment_Task --
1137 ----------------------
1139 function Environment_Task return Task_Id is
1140 begin
1141 return Environment_Task_Id;
1142 end Environment_Task;
1144 --------------
1145 -- Lock_RTS --
1146 --------------
1148 procedure Lock_RTS is
1149 begin
1150 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1151 end Lock_RTS;
1153 ----------------
1154 -- Unlock_RTS --
1155 ----------------
1157 procedure Unlock_RTS is
1158 begin
1159 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1160 end Unlock_RTS;
1162 ------------------
1163 -- Suspend_Task --
1164 ------------------
1166 function Suspend_Task
1167 (T : ST.Task_Id;
1168 Thread_Self : Thread_Id) return Boolean
1170 begin
1171 if T.Common.LL.Thread /= 0
1172 and then T.Common.LL.Thread /= Thread_Self
1173 then
1174 return taskSuspend (T.Common.LL.Thread) = 0;
1175 else
1176 return True;
1177 end if;
1178 end Suspend_Task;
1180 -----------------
1181 -- Resume_Task --
1182 -----------------
1184 function Resume_Task
1185 (T : ST.Task_Id;
1186 Thread_Self : Thread_Id) return Boolean
1188 begin
1189 if T.Common.LL.Thread /= 0
1190 and then T.Common.LL.Thread /= Thread_Self
1191 then
1192 return taskResume (T.Common.LL.Thread) = 0;
1193 else
1194 return True;
1195 end if;
1196 end Resume_Task;
1198 ----------------
1199 -- Initialize --
1200 ----------------
1202 procedure Initialize (Environment_Task : Task_Id) is
1203 Result : int;
1204 begin
1205 Environment_Task_Id := Environment_Task;
1207 Interrupt_Management.Initialize;
1208 Specific.Initialize;
1210 if Locking_Policy = 'C' then
1211 Mutex_Protocol := Prio_Protect;
1212 elsif Locking_Policy = 'I' then
1213 Mutex_Protocol := Prio_Inherit;
1214 else
1215 Mutex_Protocol := Prio_None;
1216 end if;
1218 if Time_Slice_Val > 0 then
1219 Result := Set_Time_Slice
1220 (To_Clock_Ticks
1221 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1222 end if;
1224 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1225 pragma Assert (Result = 0);
1227 for J in Interrupt_Management.Signal_ID loop
1228 if System.Interrupt_Management.Keep_Unmasked (J) then
1229 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1230 pragma Assert (Result = 0);
1231 end if;
1232 end loop;
1234 -- Initialize the lock used to synchronize chain of all ATCBs
1236 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1238 Enter_Task (Environment_Task);
1239 end Initialize;
1241 end System.Task_Primitives.Operations;