OSDN Git Service

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