OSDN Git Service

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