* crtstuff.c (__dso_handle): Set section from
[official-gcc.git] / gcc / ada / s-taprop-vxworks.adb
blob186e8c28f409734e817bf92c4709e5a45caf9787
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 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 Unchecked_Conversion;
63 with 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 Mutex_Protocol : Priority_Type;
110 Foreign_Task_Elaborated : aliased Boolean := True;
111 -- Used to identified fake tasks (i.e., non-Ada Threads)
113 --------------------
114 -- Local Packages --
115 --------------------
117 package Specific is
119 procedure Initialize;
120 pragma Inline (Initialize);
121 -- Initialize task specific data
123 function Is_Valid_Task return Boolean;
124 pragma Inline (Is_Valid_Task);
125 -- Does executing thread have a TCB?
127 procedure Set (Self_Id : Task_Id);
128 pragma Inline (Set);
129 -- Set the self id for the current task
131 procedure Delete;
132 pragma Inline (Delete);
133 -- Delete the task specific data associated with the current task
135 function Self return Task_Id;
136 pragma Inline (Self);
137 -- Return a pointer to the Ada Task Control Block of the calling task
139 end Specific;
141 package body Specific is separate;
142 -- The body of this package is target specific
144 ---------------------------------
145 -- Support for foreign threads --
146 ---------------------------------
148 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
149 -- Allocate and Initialize a new ATCB for the current Thread
151 function Register_Foreign_Thread
152 (Thread : Thread_Id) return Task_Id is separate;
154 -----------------------
155 -- Local Subprograms --
156 -----------------------
158 procedure Abort_Handler (signo : Signal);
159 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
161 procedure Install_Signal_Handlers;
162 -- Install the default signal handlers for the current task
164 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
166 -------------------
167 -- Abort_Handler --
168 -------------------
170 procedure Abort_Handler (signo : Signal) is
171 pragma Unreferenced (signo);
173 Self_ID : constant Task_Id := Self;
174 Result : int;
175 Old_Set : aliased sigset_t;
177 begin
178 -- It is not safe to raise an exception when using ZCX and the GCC
179 -- exception handling mechanism.
181 if ZCX_By_Default and then GCC_ZCX_Support then
182 return;
183 end if;
185 if Self_ID.Deferral_Level = 0
186 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
187 and then not Self_ID.Aborting
188 then
189 Self_ID.Aborting := True;
191 -- Make sure signals used for RTS internal purpose are unmasked
193 Result := pthread_sigmask (SIG_UNBLOCK,
194 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
195 pragma Assert (Result = 0);
197 raise Standard'Abort_Signal;
198 end if;
199 end Abort_Handler;
201 -----------------
202 -- Stack_Guard --
203 -----------------
205 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
206 pragma Unreferenced (T);
207 pragma Unreferenced (On);
209 begin
210 -- Nothing needed (why not???)
212 null;
213 end Stack_Guard;
215 -------------------
216 -- Get_Thread_Id --
217 -------------------
219 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
220 begin
221 return T.Common.LL.Thread;
222 end Get_Thread_Id;
224 ----------
225 -- Self --
226 ----------
228 function Self return Task_Id renames Specific.Self;
230 -----------------------------
231 -- Install_Signal_Handlers --
232 -----------------------------
234 procedure Install_Signal_Handlers is
235 act : aliased struct_sigaction;
236 old_act : aliased struct_sigaction;
237 Tmp_Set : aliased sigset_t;
238 Result : int;
240 begin
241 act.sa_flags := 0;
242 act.sa_handler := Abort_Handler'Address;
244 Result := sigemptyset (Tmp_Set'Access);
245 pragma Assert (Result = 0);
246 act.sa_mask := Tmp_Set;
248 Result :=
249 sigaction
250 (Signal (Interrupt_Management.Abort_Task_Signal),
251 act'Unchecked_Access,
252 old_act'Unchecked_Access);
253 pragma Assert (Result = 0);
255 Interrupt_Management.Initialize_Interrupts;
256 end Install_Signal_Handlers;
258 ---------------------
259 -- Initialize_Lock --
260 ---------------------
262 procedure Initialize_Lock (Prio : System.Any_Priority; L : access Lock) is
263 begin
264 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
265 L.Prio_Ceiling := int (Prio);
266 L.Protocol := Mutex_Protocol;
267 pragma Assert (L.Mutex /= 0);
268 end Initialize_Lock;
270 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
271 pragma Unreferenced (Level);
273 begin
274 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
275 L.Prio_Ceiling := int (System.Any_Priority'Last);
276 L.Protocol := Mutex_Protocol;
277 pragma Assert (L.Mutex /= 0);
278 end Initialize_Lock;
280 -------------------
281 -- Finalize_Lock --
282 -------------------
284 procedure Finalize_Lock (L : access Lock) is
285 Result : int;
286 begin
287 Result := semDelete (L.Mutex);
288 pragma Assert (Result = 0);
289 end Finalize_Lock;
291 procedure Finalize_Lock (L : access RTS_Lock) is
292 Result : int;
293 begin
294 Result := semDelete (L.Mutex);
295 pragma Assert (Result = 0);
296 end Finalize_Lock;
298 ----------------
299 -- Write_Lock --
300 ----------------
302 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
303 Result : int;
304 begin
305 if L.Protocol = Prio_Protect
306 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
307 then
308 Ceiling_Violation := True;
309 return;
310 else
311 Ceiling_Violation := False;
312 end if;
314 Result := semTake (L.Mutex, WAIT_FOREVER);
315 pragma Assert (Result = 0);
316 end Write_Lock;
318 procedure Write_Lock
319 (L : access RTS_Lock;
320 Global_Lock : Boolean := False)
322 Result : int;
323 begin
324 if not Single_Lock or else Global_Lock then
325 Result := semTake (L.Mutex, WAIT_FOREVER);
326 pragma Assert (Result = 0);
327 end if;
328 end Write_Lock;
330 procedure Write_Lock (T : Task_Id) is
331 Result : int;
332 begin
333 if not Single_Lock then
334 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
335 pragma Assert (Result = 0);
336 end if;
337 end Write_Lock;
339 ---------------
340 -- Read_Lock --
341 ---------------
343 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
344 begin
345 Write_Lock (L, Ceiling_Violation);
346 end Read_Lock;
348 ------------
349 -- Unlock --
350 ------------
352 procedure Unlock (L : access Lock) is
353 Result : int;
354 begin
355 Result := semGive (L.Mutex);
356 pragma Assert (Result = 0);
357 end Unlock;
359 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
360 Result : int;
361 begin
362 if not Single_Lock or else Global_Lock then
363 Result := semGive (L.Mutex);
364 pragma Assert (Result = 0);
365 end if;
366 end Unlock;
368 procedure Unlock (T : Task_Id) is
369 Result : int;
370 begin
371 if not Single_Lock then
372 Result := semGive (T.Common.LL.L.Mutex);
373 pragma Assert (Result = 0);
374 end if;
375 end Unlock;
377 -----------
378 -- Sleep --
379 -----------
381 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
382 pragma Unreferenced (Reason);
384 Result : int;
386 begin
387 pragma Assert (Self_ID = Self);
389 -- Release the mutex before sleeping
391 if Single_Lock then
392 Result := semGive (Single_RTS_Lock.Mutex);
393 else
394 Result := semGive (Self_ID.Common.LL.L.Mutex);
395 end if;
397 pragma Assert (Result = 0);
399 -- Perform a blocking operation to take the CV semaphore. Note that a
400 -- blocking operation in VxWorks will reenable task scheduling. When we
401 -- are no longer blocked and control is returned, task scheduling will
402 -- again be disabled.
404 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
405 pragma Assert (Result = 0);
407 -- Take the mutex back
409 if Single_Lock then
410 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
411 else
412 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
413 end if;
415 pragma Assert (Result = 0);
416 end Sleep;
418 -----------------
419 -- Timed_Sleep --
420 -----------------
422 -- This is for use within the run-time system, so abort is assumed to be
423 -- already deferred, and the caller should be holding its own ATCB lock.
425 procedure Timed_Sleep
426 (Self_ID : Task_Id;
427 Time : Duration;
428 Mode : ST.Delay_Modes;
429 Reason : System.Tasking.Task_States;
430 Timedout : out Boolean;
431 Yielded : out Boolean)
433 pragma Unreferenced (Reason);
435 Orig : constant Duration := Monotonic_Clock;
436 Absolute : Duration;
437 Ticks : int;
438 Result : int;
439 Wakeup : Boolean := False;
441 begin
442 Timedout := False;
443 Yielded := True;
445 if Mode = Relative then
446 Absolute := Orig + Time;
448 -- Systematically add one since the first tick will delay *at most*
449 -- 1 / Rate_Duration seconds, so we need to add one to be on the
450 -- safe side.
452 Ticks := To_Clock_Ticks (Time);
454 if Ticks > 0 and then Ticks < int'Last then
455 Ticks := Ticks + 1;
456 end if;
458 else
459 Absolute := Time;
460 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
461 end if;
463 if Ticks > 0 then
464 loop
465 -- Release the mutex before sleeping
467 if Single_Lock then
468 Result := semGive (Single_RTS_Lock.Mutex);
469 else
470 Result := semGive (Self_ID.Common.LL.L.Mutex);
471 end if;
473 pragma Assert (Result = 0);
475 -- Perform a blocking operation to take the CV semaphore. Note
476 -- that a blocking operation in VxWorks will reenable task
477 -- scheduling. When we are no longer blocked and control is
478 -- returned, task scheduling will again be disabled.
480 Result := semTake (Self_ID.Common.LL.CV, Ticks);
482 if Result = 0 then
484 -- Somebody may have called Wakeup for us
486 Wakeup := True;
488 else
489 if errno /= S_objLib_OBJ_TIMEOUT then
490 Wakeup := True;
492 else
493 -- If Ticks = int'last, it was most probably truncated so
494 -- let's make another round after recomputing Ticks from
495 -- the the absolute time.
497 if Ticks /= int'Last then
498 Timedout := True;
499 else
500 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
502 if Ticks < 0 then
503 Timedout := True;
504 end if;
505 end if;
506 end if;
507 end if;
509 -- Take the mutex back
511 if Single_Lock then
512 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
513 else
514 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
515 end if;
517 pragma Assert (Result = 0);
519 exit when Timedout or Wakeup;
520 end loop;
522 else
523 Timedout := True;
525 -- Should never hold a lock while yielding
527 if Single_Lock then
528 Result := semGive (Single_RTS_Lock.Mutex);
529 taskDelay (0);
530 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
532 else
533 Result := semGive (Self_ID.Common.LL.L.Mutex);
534 taskDelay (0);
535 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
536 end if;
537 end if;
538 end Timed_Sleep;
540 -----------------
541 -- Timed_Delay --
542 -----------------
544 -- This is for use in implementing delay statements, so we assume the
545 -- caller is holding no locks.
547 procedure Timed_Delay
548 (Self_ID : Task_Id;
549 Time : Duration;
550 Mode : ST.Delay_Modes)
552 Orig : constant Duration := Monotonic_Clock;
553 Absolute : Duration;
554 Ticks : int;
555 Timedout : Boolean;
556 Result : int;
557 Aborted : Boolean := False;
559 begin
560 if Mode = Relative then
561 Absolute := Orig + Time;
562 Ticks := To_Clock_Ticks (Time);
564 if Ticks > 0 and then Ticks < int'Last then
566 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
567 -- seconds, so we need to add one to be on the safe side.
569 Ticks := Ticks + 1;
570 end if;
572 else
573 Absolute := Time;
574 Ticks := To_Clock_Ticks (Time - Orig);
575 end if;
577 if Ticks > 0 then
579 -- Modifying State and Pending_Priority_Change, locking the TCB
581 if Single_Lock then
582 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
583 else
584 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
585 end if;
587 pragma Assert (Result = 0);
589 Self_ID.Common.State := Delay_Sleep;
590 Timedout := False;
592 loop
593 if Self_ID.Pending_Priority_Change then
594 Self_ID.Pending_Priority_Change := False;
595 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
596 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
597 end if;
599 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
601 -- Release the TCB before sleeping
603 if Single_Lock then
604 Result := semGive (Single_RTS_Lock.Mutex);
605 else
606 Result := semGive (Self_ID.Common.LL.L.Mutex);
607 end if;
608 pragma Assert (Result = 0);
610 exit when Aborted;
612 Result := semTake (Self_ID.Common.LL.CV, Ticks);
614 if Result /= 0 then
616 -- If Ticks = int'last, it was most probably truncated
617 -- so let's make another round after recomputing Ticks
618 -- from the the absolute time.
620 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
621 Timedout := True;
622 else
623 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
625 if Ticks < 0 then
626 Timedout := True;
627 end if;
628 end if;
629 end if;
631 -- Take back the lock after having slept, to protect further
632 -- access to Self_ID.
634 if Single_Lock then
635 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
636 else
637 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
638 end if;
640 pragma Assert (Result = 0);
642 exit when Timedout;
643 end loop;
645 Self_ID.Common.State := Runnable;
647 if Single_Lock then
648 Result := semGive (Single_RTS_Lock.Mutex);
649 else
650 Result := semGive (Self_ID.Common.LL.L.Mutex);
651 end if;
653 else
654 taskDelay (0);
655 end if;
656 end Timed_Delay;
658 ---------------------
659 -- Monotonic_Clock --
660 ---------------------
662 function Monotonic_Clock return Duration is
663 TS : aliased timespec;
664 Result : int;
665 begin
666 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
667 pragma Assert (Result = 0);
668 return To_Duration (TS);
669 end Monotonic_Clock;
671 -------------------
672 -- RT_Resolution --
673 -------------------
675 function RT_Resolution return Duration is
676 begin
677 return 1.0 / Duration (sysClkRateGet);
678 end RT_Resolution;
680 ------------
681 -- Wakeup --
682 ------------
684 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
685 pragma Unreferenced (Reason);
686 Result : int;
687 begin
688 Result := semGive (T.Common.LL.CV);
689 pragma Assert (Result = 0);
690 end Wakeup;
692 -----------
693 -- Yield --
694 -----------
696 procedure Yield (Do_Yield : Boolean := True) is
697 pragma Unreferenced (Do_Yield);
698 Result : int;
699 pragma Unreferenced (Result);
700 begin
701 Result := taskDelay (0);
702 end Yield;
704 ------------------
705 -- Set_Priority --
706 ------------------
708 type Prio_Array_Type is array (System.Any_Priority) of Integer;
709 pragma Atomic_Components (Prio_Array_Type);
711 Prio_Array : Prio_Array_Type;
712 -- Global array containing the id of the currently running task for
713 -- each priority. Note that we assume that we are on a single processor
714 -- with run-till-blocked scheduling.
716 procedure Set_Priority
717 (T : Task_Id;
718 Prio : System.Any_Priority;
719 Loss_Of_Inheritance : Boolean := False)
721 Array_Item : Integer;
722 Result : int;
724 begin
725 Result :=
726 taskPrioritySet
727 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
728 pragma Assert (Result = 0);
730 if Dispatching_Policy = 'F' then
732 -- Annex D requirement [RM D.2.2 par. 9]:
734 -- If the task drops its priority due to the loss of inherited
735 -- priority, it is added at the head of the ready queue for its
736 -- new active priority.
738 if Loss_Of_Inheritance
739 and then Prio < T.Common.Current_Priority
740 then
741 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
742 Prio_Array (T.Common.Base_Priority) := Array_Item;
744 loop
745 -- Give some processes a chance to arrive
747 taskDelay (0);
749 -- Then wait for our turn to proceed
751 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
752 or else Prio_Array (T.Common.Base_Priority) = 1;
753 end loop;
755 Prio_Array (T.Common.Base_Priority) :=
756 Prio_Array (T.Common.Base_Priority) - 1;
757 end if;
758 end if;
760 T.Common.Current_Priority := Prio;
761 end Set_Priority;
763 ------------------
764 -- Get_Priority --
765 ------------------
767 function Get_Priority (T : Task_Id) return System.Any_Priority is
768 begin
769 return T.Common.Current_Priority;
770 end Get_Priority;
772 ----------------
773 -- Enter_Task --
774 ----------------
776 procedure Enter_Task (Self_ID : Task_Id) is
777 procedure Init_Float;
778 pragma Import (C, Init_Float, "__gnat_init_float");
779 -- Properly initializes the FPU for PPC/MIPS systems
781 begin
782 Self_ID.Common.LL.Thread := taskIdSelf;
783 Specific.Set (Self_ID);
785 Init_Float;
787 -- Install the signal handlers
789 -- This is called for each task since there is no signal inheritance
790 -- between VxWorks tasks.
792 Install_Signal_Handlers;
794 Lock_RTS;
796 for J in Known_Tasks'Range loop
797 if Known_Tasks (J) = null then
798 Known_Tasks (J) := Self_ID;
799 Self_ID.Known_Tasks_Index := J;
800 exit;
801 end if;
802 end loop;
804 Unlock_RTS;
805 end Enter_Task;
807 --------------
808 -- New_ATCB --
809 --------------
811 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
812 begin
813 return new Ada_Task_Control_Block (Entry_Num);
814 end New_ATCB;
816 -------------------
817 -- Is_Valid_Task --
818 -------------------
820 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
822 -----------------------------
823 -- Register_Foreign_Thread --
824 -----------------------------
826 function Register_Foreign_Thread return Task_Id is
827 begin
828 if Is_Valid_Task then
829 return Self;
830 else
831 return Register_Foreign_Thread (taskIdSelf);
832 end if;
833 end Register_Foreign_Thread;
835 --------------------
836 -- Initialize_TCB --
837 --------------------
839 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
840 begin
841 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
842 Self_ID.Common.LL.Thread := 0;
844 if Self_ID.Common.LL.CV = 0 then
845 Succeeded := False;
846 else
847 Succeeded := True;
849 if not Single_Lock then
850 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
851 end if;
852 end if;
853 end Initialize_TCB;
855 -----------------
856 -- Create_Task --
857 -----------------
859 procedure Create_Task
860 (T : Task_Id;
861 Wrapper : System.Address;
862 Stack_Size : System.Parameters.Size_Type;
863 Priority : System.Any_Priority;
864 Succeeded : out Boolean)
866 Adjusted_Stack_Size : size_t;
867 begin
868 -- Ask for four extra bytes of stack space so that the ATCB pointer can
869 -- be stored below the stack limit, plus extra space for the frame of
870 -- Task_Wrapper. This is so the user gets the amount of stack requested
871 -- exclusive of the needs.
873 -- We also have to allocate n more bytes for the task name storage and
874 -- enough space for the Wind Task Control Block which is around 0x778
875 -- bytes. VxWorks also seems to carve out additional space, so use 2048
876 -- as a nice round number. We might want to increment to the nearest
877 -- page size in case we ever support VxVMI.
879 -- ??? - we should come back and visit this so we can set the task name
880 -- to something appropriate.
882 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
884 -- Since the initial signal mask of a thread is inherited from the
885 -- creator, and the Environment task has all its signals masked, we do
886 -- not need to manipulate caller's signal mask at this point. All tasks
887 -- in RTS will have All_Tasks_Mask initially.
889 if T.Common.Task_Image_Len = 0 then
890 T.Common.LL.Thread := taskSpawn
891 (System.Null_Address,
892 To_VxWorks_Priority (int (Priority)),
893 VX_FP_TASK,
894 Adjusted_Stack_Size,
895 Wrapper,
896 To_Address (T));
897 else
898 declare
899 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
901 begin
902 Name (1 .. Name'Last - 1) :=
903 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
904 Name (Name'Last) := ASCII.NUL;
906 T.Common.LL.Thread := taskSpawn
907 (Name'Address,
908 To_VxWorks_Priority (int (Priority)),
909 VX_FP_TASK,
910 Adjusted_Stack_Size,
911 Wrapper,
912 To_Address (T));
913 end;
914 end if;
916 if T.Common.LL.Thread = -1 then
917 Succeeded := False;
918 else
919 Succeeded := True;
920 end if;
922 Task_Creation_Hook (T.Common.LL.Thread);
923 Set_Priority (T, Priority);
924 end Create_Task;
926 ------------------
927 -- Finalize_TCB --
928 ------------------
930 procedure Finalize_TCB (T : Task_Id) is
931 Result : int;
932 Tmp : Task_Id := T;
933 Is_Self : constant Boolean := (T = Self);
935 procedure Free is new
936 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
938 begin
939 if not Single_Lock then
940 Result := semDelete (T.Common.LL.L.Mutex);
941 pragma Assert (Result = 0);
942 end if;
944 T.Common.LL.Thread := 0;
946 Result := semDelete (T.Common.LL.CV);
947 pragma Assert (Result = 0);
949 if T.Known_Tasks_Index /= -1 then
950 Known_Tasks (T.Known_Tasks_Index) := null;
951 end if;
953 Free (Tmp);
955 if Is_Self then
956 Specific.Delete;
957 end if;
958 end Finalize_TCB;
960 ---------------
961 -- Exit_Task --
962 ---------------
964 procedure Exit_Task is
965 begin
966 Specific.Set (null);
967 end Exit_Task;
969 ----------------
970 -- Abort_Task --
971 ----------------
973 procedure Abort_Task (T : Task_Id) is
974 Result : int;
975 begin
976 Result := kill (T.Common.LL.Thread,
977 Signal (Interrupt_Management.Abort_Task_Signal));
978 pragma Assert (Result = 0);
979 end Abort_Task;
981 ----------------
982 -- Initialize --
983 ----------------
985 procedure Initialize (S : in out Suspension_Object) is
986 begin
987 -- Initialize internal state. It is always initialized to False (ARM
988 -- D.10 par. 6).
990 S.State := False;
991 S.Waiting := False;
993 -- Initialize internal mutex
995 -- Use simpler binary semaphore instead of VxWorks
996 -- mutual exclusion semaphore, because we don't need
997 -- the fancier semantics and their overhead.
999 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1001 -- Initialize internal condition variable
1003 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1004 end Initialize;
1006 --------------
1007 -- Finalize --
1008 --------------
1010 procedure Finalize (S : in out Suspension_Object) is
1011 Result : STATUS;
1012 begin
1013 -- Destroy internal mutex
1015 Result := semDelete (S.L);
1016 pragma Assert (Result = OK);
1018 -- Destroy internal condition variable
1020 Result := semDelete (S.CV);
1021 pragma Assert (Result = OK);
1022 end Finalize;
1024 -------------------
1025 -- Current_State --
1026 -------------------
1028 function Current_State (S : Suspension_Object) return Boolean is
1029 begin
1030 -- We do not want to use lock on this read operation. State is marked
1031 -- as Atomic so that we ensure that the value retrieved is correct.
1033 return S.State;
1034 end Current_State;
1036 ---------------
1037 -- Set_False --
1038 ---------------
1040 procedure Set_False (S : in out Suspension_Object) is
1041 Result : STATUS;
1042 begin
1043 SSL.Abort_Defer.all;
1045 Result := semTake (S.L, WAIT_FOREVER);
1046 pragma Assert (Result = OK);
1048 S.State := False;
1050 Result := semGive (S.L);
1051 pragma Assert (Result = OK);
1053 SSL.Abort_Undefer.all;
1054 end Set_False;
1056 --------------
1057 -- Set_True --
1058 --------------
1060 procedure Set_True (S : in out Suspension_Object) is
1061 Result : STATUS;
1062 begin
1063 SSL.Abort_Defer.all;
1065 Result := semTake (S.L, WAIT_FOREVER);
1066 pragma Assert (Result = OK);
1068 -- If there is already a task waiting on this suspension object then
1069 -- we resume it, leaving the state of the suspension object to False,
1070 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1071 -- the state to True.
1073 if S.Waiting then
1074 S.Waiting := False;
1075 S.State := False;
1077 Result := semGive (S.CV);
1078 pragma Assert (Result = OK);
1079 else
1080 S.State := True;
1081 end if;
1083 Result := semGive (S.L);
1084 pragma Assert (Result = OK);
1086 SSL.Abort_Undefer.all;
1087 end Set_True;
1089 ------------------------
1090 -- Suspend_Until_True --
1091 ------------------------
1093 procedure Suspend_Until_True (S : in out Suspension_Object) is
1094 Result : STATUS;
1095 begin
1096 SSL.Abort_Defer.all;
1098 Result := semTake (S.L, WAIT_FOREVER);
1100 if S.Waiting then
1101 -- Program_Error must be raised upon calling Suspend_Until_True
1102 -- if another task is already waiting on that suspension object
1103 -- (ARM D.10 par. 10).
1105 Result := semGive (S.L);
1106 pragma Assert (Result = OK);
1108 SSL.Abort_Undefer.all;
1110 raise Program_Error;
1111 else
1112 -- Suspend the task if the state is False. Otherwise, the task
1113 -- continues its execution, and the state of the suspension object
1114 -- is set to False (ARM D.10 par. 9).
1116 if S.State then
1117 S.State := False;
1119 Result := semGive (S.L);
1120 pragma Assert (Result = 0);
1122 SSL.Abort_Undefer.all;
1123 else
1124 S.Waiting := True;
1126 -- Release the mutex before sleeping
1128 Result := semGive (S.L);
1129 pragma Assert (Result = OK);
1131 SSL.Abort_Undefer.all;
1133 Result := semTake (S.CV, WAIT_FOREVER);
1134 pragma Assert (Result = 0);
1135 end if;
1136 end if;
1137 end Suspend_Until_True;
1139 ----------------
1140 -- Check_Exit --
1141 ----------------
1143 -- Dummy version
1145 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1146 pragma Unreferenced (Self_ID);
1147 begin
1148 return True;
1149 end Check_Exit;
1151 --------------------
1152 -- Check_No_Locks --
1153 --------------------
1155 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1156 pragma Unreferenced (Self_ID);
1157 begin
1158 return True;
1159 end Check_No_Locks;
1161 ----------------------
1162 -- Environment_Task --
1163 ----------------------
1165 function Environment_Task return Task_Id is
1166 begin
1167 return Environment_Task_Id;
1168 end Environment_Task;
1170 --------------
1171 -- Lock_RTS --
1172 --------------
1174 procedure Lock_RTS is
1175 begin
1176 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1177 end Lock_RTS;
1179 ----------------
1180 -- Unlock_RTS --
1181 ----------------
1183 procedure Unlock_RTS is
1184 begin
1185 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1186 end Unlock_RTS;
1188 ------------------
1189 -- Suspend_Task --
1190 ------------------
1192 function Suspend_Task
1193 (T : ST.Task_Id;
1194 Thread_Self : Thread_Id) return Boolean
1196 begin
1197 if T.Common.LL.Thread /= 0
1198 and then T.Common.LL.Thread /= Thread_Self
1199 then
1200 return taskSuspend (T.Common.LL.Thread) = 0;
1201 else
1202 return True;
1203 end if;
1204 end Suspend_Task;
1206 -----------------
1207 -- Resume_Task --
1208 -----------------
1210 function Resume_Task
1211 (T : ST.Task_Id;
1212 Thread_Self : Thread_Id) return Boolean
1214 begin
1215 if T.Common.LL.Thread /= 0
1216 and then T.Common.LL.Thread /= Thread_Self
1217 then
1218 return taskResume (T.Common.LL.Thread) = 0;
1219 else
1220 return True;
1221 end if;
1222 end Resume_Task;
1224 ----------------
1225 -- Initialize --
1226 ----------------
1228 procedure Initialize (Environment_Task : Task_Id) is
1229 Result : int;
1230 begin
1231 Environment_Task_Id := Environment_Task;
1233 Interrupt_Management.Initialize;
1234 Specific.Initialize;
1236 if Locking_Policy = 'C' then
1237 Mutex_Protocol := Prio_Protect;
1238 elsif Locking_Policy = 'I' then
1239 Mutex_Protocol := Prio_Inherit;
1240 else
1241 Mutex_Protocol := Prio_None;
1242 end if;
1244 if Time_Slice_Val > 0 then
1245 Result := Set_Time_Slice
1246 (To_Clock_Ticks
1247 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1248 end if;
1250 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1251 pragma Assert (Result = 0);
1253 for J in Interrupt_Management.Signal_ID loop
1254 if System.Interrupt_Management.Keep_Unmasked (J) then
1255 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1256 pragma Assert (Result = 0);
1257 end if;
1258 end loop;
1260 -- Initialize the lock used to synchronize chain of all ATCBs
1262 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1264 Enter_Task (Environment_Task);
1265 end Initialize;
1267 end System.Task_Primitives.Operations;