OSDN Git Service

PR c++/27714
[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-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 ------------------------------------------------------------------------------
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.Debug;
44 --  used for Known_Tasks
45
46 with System.Interrupt_Management;
47 --  used for Keep_Unmasked
48 --           Abort_Task_Signal
49 --           Signal_ID
50 --           Initialize_Interrupts
51
52 with Interfaces.C;
53
54 with System.Soft_Links;
55 --  used for Abort_Defer/Undefer
56
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.
61
62 with Unchecked_Conversion;
63 with Unchecked_Deallocation;
64
65 package body System.Task_Primitives.Operations is
66
67    package SSL renames System.Soft_Links;
68
69    use System.Tasking.Debug;
70    use System.Tasking;
71    use System.OS_Interface;
72    use System.Parameters;
73    use type Interfaces.C.int;
74
75    subtype int is System.OS_Interface.int;
76
77    Relative : constant := 0;
78
79    ----------------
80    -- Local Data --
81    ----------------
82
83    --  The followings are logically constants, but need to be initialized at
84    --  run time.
85
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
90
91    Environment_Task_Id : Task_Id;
92    --  A variable to hold Task_Id for the environment task
93
94    Unblocked_Signal_Mask : aliased sigset_t;
95    --  The set of signals that should unblocked in all tasks
96
97    --  The followings are internal configuration constants needed
98
99    Time_Slice_Val : Integer;
100    pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
101
102    Locking_Policy : Character;
103    pragma Import (C, Locking_Policy, "__gl_locking_policy");
104
105    Dispatching_Policy : Character;
106    pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
107
108    Mutex_Protocol : Priority_Type;
109
110    Foreign_Task_Elaborated : aliased Boolean := True;
111    --  Used to identified fake tasks (i.e., non-Ada Threads)
112
113    --------------------
114    -- Local Packages --
115    --------------------
116
117    package Specific is
118
119       procedure Initialize;
120       pragma Inline (Initialize);
121       --  Initialize task specific data
122
123       function Is_Valid_Task return Boolean;
124       pragma Inline (Is_Valid_Task);
125       --  Does executing thread have a TCB?
126
127       procedure Set (Self_Id : Task_Id);
128       pragma Inline (Set);
129       --  Set the self id for the current task
130
131       procedure Delete;
132       pragma Inline (Delete);
133       --  Delete the task specific data associated with the current task
134
135       function Self return Task_Id;
136       pragma Inline (Self);
137       --  Return a pointer to the Ada Task Control Block of the calling task
138
139    end Specific;
140
141    package body Specific is separate;
142    --  The body of this package is target specific
143
144    ---------------------------------
145    -- Support for foreign threads --
146    ---------------------------------
147
148    function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
149    --  Allocate and Initialize a new ATCB for the current Thread
150
151    function Register_Foreign_Thread
152      (Thread : Thread_Id) return Task_Id is separate;
153
154    -----------------------
155    -- Local Subprograms --
156    -----------------------
157
158    procedure Abort_Handler (signo : Signal);
159    --  Handler for the abort (SIGABRT) signal to handle asynchronous abort
160
161    procedure Install_Signal_Handlers;
162    --  Install the default signal handlers for the current task
163
164    function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
165
166    -------------------
167    -- Abort_Handler --
168    -------------------
169
170    procedure Abort_Handler (signo : Signal) is
171       pragma Unreferenced (signo);
172
173       Self_ID : constant Task_Id := Self;
174       Result  : int;
175       Old_Set : aliased sigset_t;
176
177    begin
178       --  It is not safe to raise an exception when using ZCX and the GCC
179       --  exception handling mechanism.
180
181       if ZCX_By_Default and then GCC_ZCX_Support then
182          return;
183       end if;
184
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;
190
191          --  Make sure signals used for RTS internal purpose are unmasked
192
193          Result := pthread_sigmask (SIG_UNBLOCK,
194            Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
195          pragma Assert (Result = 0);
196
197          raise Standard'Abort_Signal;
198       end if;
199    end Abort_Handler;
200
201    -----------------
202    -- Stack_Guard --
203    -----------------
204
205    procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
206       pragma Unreferenced (T);
207       pragma Unreferenced (On);
208
209    begin
210       --  Nothing needed (why not???)
211
212       null;
213    end Stack_Guard;
214
215    -------------------
216    -- Get_Thread_Id --
217    -------------------
218
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;
223
224    ----------
225    -- Self --
226    ----------
227
228    function Self return Task_Id renames Specific.Self;
229
230    -----------------------------
231    -- Install_Signal_Handlers --
232    -----------------------------
233
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;
239
240    begin
241       act.sa_flags := 0;
242       act.sa_handler := Abort_Handler'Address;
243
244       Result := sigemptyset (Tmp_Set'Access);
245       pragma Assert (Result = 0);
246       act.sa_mask := Tmp_Set;
247
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);
254
255       Interrupt_Management.Initialize_Interrupts;
256    end Install_Signal_Handlers;
257
258    ---------------------
259    -- Initialize_Lock --
260    ---------------------
261
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;
269
270    procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
271       pragma Unreferenced (Level);
272
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;
279
280    -------------------
281    -- Finalize_Lock --
282    -------------------
283
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;
290
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;
297
298    ----------------
299    -- Write_Lock --
300    ----------------
301
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;
313
314       Result := semTake (L.Mutex, WAIT_FOREVER);
315       pragma Assert (Result = 0);
316    end Write_Lock;
317
318    procedure Write_Lock
319      (L           : access RTS_Lock;
320       Global_Lock : Boolean := False)
321    is
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;
329
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;
338
339    ---------------
340    -- Read_Lock --
341    ---------------
342
343    procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
344    begin
345       Write_Lock (L, Ceiling_Violation);
346    end Read_Lock;
347
348    ------------
349    -- Unlock --
350    ------------
351
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;
358
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;
367
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;
376
377    -----------
378    -- Sleep --
379    -----------
380
381    procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
382       pragma Unreferenced (Reason);
383
384       Result : int;
385
386    begin
387       pragma Assert (Self_ID = Self);
388
389       --  Release the mutex before sleeping
390
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;
396
397       pragma Assert (Result = 0);
398
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.
403
404       Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
405       pragma Assert (Result = 0);
406
407       --  Take the mutex back
408
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;
414
415       pragma Assert (Result = 0);
416    end Sleep;
417
418    -----------------
419    -- Timed_Sleep --
420    -----------------
421
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.
424
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)
432    is
433       pragma Unreferenced (Reason);
434
435       Orig     : constant Duration := Monotonic_Clock;
436       Absolute : Duration;
437       Ticks    : int;
438       Result   : int;
439       Wakeup   : Boolean := False;
440
441    begin
442       Timedout := False;
443       Yielded  := True;
444
445       if Mode = Relative then
446          Absolute := Orig + Time;
447
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.
451
452          Ticks := To_Clock_Ticks (Time);
453
454          if Ticks > 0 and then Ticks < int'Last then
455             Ticks := Ticks + 1;
456          end if;
457
458       else
459          Absolute := Time;
460          Ticks    := To_Clock_Ticks (Time - Monotonic_Clock);
461       end if;
462
463       if Ticks > 0 then
464          loop
465             --  Release the mutex before sleeping
466
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;
472
473             pragma Assert (Result = 0);
474
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.
479
480             Result := semTake (Self_ID.Common.LL.CV, Ticks);
481
482             if Result = 0 then
483
484                --  Somebody may have called Wakeup for us
485
486                Wakeup := True;
487
488             else
489                if errno /= S_objLib_OBJ_TIMEOUT then
490                   Wakeup := True;
491
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.
496
497                   if Ticks /= int'Last then
498                      Timedout := True;
499                   else
500                      Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
501
502                      if Ticks < 0 then
503                         Timedout := True;
504                      end if;
505                   end if;
506                end if;
507             end if;
508
509             --  Take the mutex back
510
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;
516
517             pragma Assert (Result = 0);
518
519             exit when Timedout or Wakeup;
520          end loop;
521
522       else
523          Timedout := True;
524
525          --  Should never hold a lock while yielding
526
527          if Single_Lock then
528             Result := semGive (Single_RTS_Lock.Mutex);
529             taskDelay (0);
530             Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
531
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;
539
540    -----------------
541    -- Timed_Delay --
542    -----------------
543
544    --  This is for use in implementing delay statements, so we assume the
545    --  caller is holding no locks.
546
547    procedure Timed_Delay
548      (Self_ID : Task_Id;
549       Time    : Duration;
550       Mode    : ST.Delay_Modes)
551    is
552       Orig     : constant Duration := Monotonic_Clock;
553       Absolute : Duration;
554       Ticks    : int;
555       Timedout : Boolean;
556       Result   : int;
557       Aborted  : Boolean := False;
558
559    begin
560       if Mode = Relative then
561          Absolute := Orig + Time;
562          Ticks    := To_Clock_Ticks (Time);
563
564          if Ticks > 0 and then Ticks < int'Last then
565
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.
568
569             Ticks := Ticks + 1;
570          end if;
571
572       else
573          Absolute := Time;
574          Ticks    := To_Clock_Ticks (Time - Orig);
575       end if;
576
577       if Ticks > 0 then
578
579          --  Modifying State and Pending_Priority_Change, locking the TCB
580
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;
586
587          pragma Assert (Result = 0);
588
589          Self_ID.Common.State := Delay_Sleep;
590          Timedout := False;
591
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;
598
599             Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
600
601             --  Release the TCB before sleeping
602
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);
609
610             exit when Aborted;
611
612             Result := semTake (Self_ID.Common.LL.CV, Ticks);
613
614             if Result /= 0 then
615
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.
619
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);
624
625                   if Ticks < 0 then
626                      Timedout := True;
627                   end if;
628                end if;
629             end if;
630
631             --  Take back the lock after having slept, to protect further
632             --  access to Self_ID.
633
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;
639
640             pragma Assert (Result = 0);
641
642             exit when Timedout;
643          end loop;
644
645          Self_ID.Common.State := Runnable;
646
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;
652
653       else
654          taskDelay (0);
655       end if;
656    end Timed_Delay;
657
658    ---------------------
659    -- Monotonic_Clock --
660    ---------------------
661
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;
670
671    -------------------
672    -- RT_Resolution --
673    -------------------
674
675    function RT_Resolution return Duration is
676    begin
677       return 1.0 / Duration (sysClkRateGet);
678    end RT_Resolution;
679
680    ------------
681    -- Wakeup --
682    ------------
683
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;
691
692    -----------
693    -- Yield --
694    -----------
695
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;
703
704    ------------------
705    -- Set_Priority --
706    ------------------
707
708    type Prio_Array_Type is array (System.Any_Priority) of Integer;
709    pragma Atomic_Components (Prio_Array_Type);
710
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.
715
716    procedure Set_Priority
717      (T                   : Task_Id;
718       Prio                : System.Any_Priority;
719       Loss_Of_Inheritance : Boolean := False)
720    is
721       Array_Item : Integer;
722       Result     : int;
723
724    begin
725       Result :=
726         taskPrioritySet
727           (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
728       pragma Assert (Result = 0);
729
730       if Dispatching_Policy = 'F' then
731
732          --  Annex D requirement [RM D.2.2 par. 9]:
733
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.
737
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;
743
744             loop
745                --  Give some processes a chance to arrive
746
747                taskDelay (0);
748
749                --  Then wait for our turn to proceed
750
751                exit when Array_Item = Prio_Array (T.Common.Base_Priority)
752                  or else Prio_Array (T.Common.Base_Priority) = 1;
753             end loop;
754
755             Prio_Array (T.Common.Base_Priority) :=
756               Prio_Array (T.Common.Base_Priority) - 1;
757          end if;
758       end if;
759
760       T.Common.Current_Priority := Prio;
761    end Set_Priority;
762
763    ------------------
764    -- Get_Priority --
765    ------------------
766
767    function Get_Priority (T : Task_Id) return System.Any_Priority is
768    begin
769       return T.Common.Current_Priority;
770    end Get_Priority;
771
772    ----------------
773    -- Enter_Task --
774    ----------------
775
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
780
781    begin
782       Self_ID.Common.LL.Thread := taskIdSelf;
783       Specific.Set (Self_ID);
784
785       Init_Float;
786
787       --  Install the signal handlers
788
789       --  This is called for each task since there is no signal inheritance
790       --  between VxWorks tasks.
791
792       Install_Signal_Handlers;
793
794       Lock_RTS;
795
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;
803
804       Unlock_RTS;
805    end Enter_Task;
806
807    --------------
808    -- New_ATCB --
809    --------------
810
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;
815
816    -------------------
817    -- Is_Valid_Task --
818    -------------------
819
820    function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
821
822    -----------------------------
823    -- Register_Foreign_Thread --
824    -----------------------------
825
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;
834
835    --------------------
836    -- Initialize_TCB --
837    --------------------
838
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;
843
844       if Self_ID.Common.LL.CV = 0 then
845          Succeeded := False;
846       else
847          Succeeded := True;
848
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;
854
855    -----------------
856    -- Create_Task --
857    -----------------
858
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)
865    is
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.
872
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.
878
879       --  ??? - we should come back and visit this so we can set the task name
880       --        to something appropriate.
881
882       Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
883
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.
888
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);
900
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;
905
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;
915
916       if T.Common.LL.Thread = -1 then
917          Succeeded := False;
918       else
919          Succeeded := True;
920       end if;
921
922       Task_Creation_Hook (T.Common.LL.Thread);
923       Set_Priority (T, Priority);
924    end Create_Task;
925
926    ------------------
927    -- Finalize_TCB --
928    ------------------
929
930    procedure Finalize_TCB (T : Task_Id) is
931       Result  : int;
932       Tmp     : Task_Id          := T;
933       Is_Self : constant Boolean := (T = Self);
934
935       procedure Free is new
936         Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
937
938    begin
939       if not Single_Lock then
940          Result := semDelete (T.Common.LL.L.Mutex);
941          pragma Assert (Result = 0);
942       end if;
943
944       T.Common.LL.Thread := 0;
945
946       Result := semDelete (T.Common.LL.CV);
947       pragma Assert (Result = 0);
948
949       if T.Known_Tasks_Index /= -1 then
950          Known_Tasks (T.Known_Tasks_Index) := null;
951       end if;
952
953       Free (Tmp);
954
955       if Is_Self then
956          Specific.Delete;
957       end if;
958    end Finalize_TCB;
959
960    ---------------
961    -- Exit_Task --
962    ---------------
963
964    procedure Exit_Task is
965    begin
966       Specific.Set (null);
967    end Exit_Task;
968
969    ----------------
970    -- Abort_Task --
971    ----------------
972
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;
980
981    ----------------
982    -- Initialize --
983    ----------------
984
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).
989
990       S.State := False;
991       S.Waiting := False;
992
993       --  Initialize internal mutex
994
995       --  Use simpler binary semaphore instead of VxWorks
996       --  mutual exclusion semaphore, because we don't need
997       --  the fancier semantics and their overhead.
998
999       S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1000
1001       --  Initialize internal condition variable
1002
1003       S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1004    end Initialize;
1005
1006    --------------
1007    -- Finalize --
1008    --------------
1009
1010    procedure Finalize (S : in out Suspension_Object) is
1011       Result : STATUS;
1012    begin
1013       --  Destroy internal mutex
1014
1015       Result := semDelete (S.L);
1016       pragma Assert (Result = OK);
1017
1018       --  Destroy internal condition variable
1019
1020       Result := semDelete (S.CV);
1021       pragma Assert (Result = OK);
1022    end Finalize;
1023
1024    -------------------
1025    -- Current_State --
1026    -------------------
1027
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.
1032
1033       return S.State;
1034    end Current_State;
1035
1036    ---------------
1037    -- Set_False --
1038    ---------------
1039
1040    procedure Set_False (S : in out Suspension_Object) is
1041       Result  : STATUS;
1042    begin
1043       SSL.Abort_Defer.all;
1044
1045       Result := semTake (S.L, WAIT_FOREVER);
1046       pragma Assert (Result = OK);
1047
1048       S.State := False;
1049
1050       Result := semGive (S.L);
1051       pragma Assert (Result = OK);
1052
1053       SSL.Abort_Undefer.all;
1054    end Set_False;
1055
1056    --------------
1057    -- Set_True --
1058    --------------
1059
1060    procedure Set_True (S : in out Suspension_Object) is
1061       Result : STATUS;
1062    begin
1063       SSL.Abort_Defer.all;
1064
1065       Result := semTake (S.L, WAIT_FOREVER);
1066       pragma Assert (Result = OK);
1067
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.
1072
1073       if S.Waiting then
1074          S.Waiting := False;
1075          S.State := False;
1076
1077          Result := semGive (S.CV);
1078          pragma Assert (Result = OK);
1079       else
1080          S.State := True;
1081       end if;
1082
1083       Result := semGive (S.L);
1084       pragma Assert (Result = OK);
1085
1086       SSL.Abort_Undefer.all;
1087    end Set_True;
1088
1089    ------------------------
1090    -- Suspend_Until_True --
1091    ------------------------
1092
1093    procedure Suspend_Until_True (S : in out Suspension_Object) is
1094       Result : STATUS;
1095    begin
1096       SSL.Abort_Defer.all;
1097
1098       Result := semTake (S.L, WAIT_FOREVER);
1099
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).
1104
1105          Result := semGive (S.L);
1106          pragma Assert (Result = OK);
1107
1108          SSL.Abort_Undefer.all;
1109
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).
1115
1116          if S.State then
1117             S.State := False;
1118
1119             Result := semGive (S.L);
1120             pragma Assert (Result = 0);
1121
1122             SSL.Abort_Undefer.all;
1123          else
1124             S.Waiting := True;
1125
1126             --  Release the mutex before sleeping
1127
1128             Result := semGive (S.L);
1129             pragma Assert (Result = OK);
1130
1131             SSL.Abort_Undefer.all;
1132
1133             Result := semTake (S.CV, WAIT_FOREVER);
1134             pragma Assert (Result = 0);
1135          end if;
1136       end if;
1137    end Suspend_Until_True;
1138
1139    ----------------
1140    -- Check_Exit --
1141    ----------------
1142
1143    --  Dummy version
1144
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;
1150
1151    --------------------
1152    -- Check_No_Locks --
1153    --------------------
1154
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;
1160
1161    ----------------------
1162    -- Environment_Task --
1163    ----------------------
1164
1165    function Environment_Task return Task_Id is
1166    begin
1167       return Environment_Task_Id;
1168    end Environment_Task;
1169
1170    --------------
1171    -- Lock_RTS --
1172    --------------
1173
1174    procedure Lock_RTS is
1175    begin
1176       Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1177    end Lock_RTS;
1178
1179    ----------------
1180    -- Unlock_RTS --
1181    ----------------
1182
1183    procedure Unlock_RTS is
1184    begin
1185       Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1186    end Unlock_RTS;
1187
1188    ------------------
1189    -- Suspend_Task --
1190    ------------------
1191
1192    function Suspend_Task
1193      (T           : ST.Task_Id;
1194       Thread_Self : Thread_Id) return Boolean
1195    is
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;
1205
1206    -----------------
1207    -- Resume_Task --
1208    -----------------
1209
1210    function Resume_Task
1211      (T           : ST.Task_Id;
1212       Thread_Self : Thread_Id) return Boolean
1213    is
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;
1223
1224    ----------------
1225    -- Initialize --
1226    ----------------
1227
1228    procedure Initialize (Environment_Task : Task_Id) is
1229       Result : int;
1230    begin
1231       Environment_Task_Id := Environment_Task;
1232
1233       Interrupt_Management.Initialize;
1234       Specific.Initialize;
1235
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;
1243
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;
1249
1250       Result := sigemptyset (Unblocked_Signal_Mask'Access);
1251       pragma Assert (Result = 0);
1252
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;
1259
1260       --  Initialize the lock used to synchronize chain of all ATCBs
1261
1262       Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1263
1264       Enter_Task (Environment_Task);
1265    end Initialize;
1266
1267 end System.Task_Primitives.Operations;