OSDN Git Service

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