OSDN Git Service

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