OSDN Git Service

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