1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a LynxOS version of this file, adapted to make SCHED_FIFO and
35 -- ceiling locking (Annex D compliance) work properly.
37 -- This package contains all the GNULL primitives that interface directly with
41 -- Turn off polling, we do not want ATC polling to take place during tasking
42 -- operations. It causes infinite loops and other problems.
44 with Ada.Unchecked_Deallocation;
48 with System.Tasking.Debug;
49 with System.Interrupt_Management;
50 with System.OS_Primitives;
51 with System.Task_Info;
53 with System.Soft_Links;
54 -- We use System.Soft_Links instead of System.Tasking.Initialization
55 -- because the later is a higher level package that we shouldn't depend on.
56 -- For example when using the restricted run time, it is replaced by
57 -- System.Tasking.Restricted.Stages.
59 package body System.Task_Primitives.Operations is
61 package SSL renames System.Soft_Links;
63 use System.Tasking.Debug;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
74 -- The followings are logically constants, but need to be initialized
77 Single_RTS_Lock : aliased RTS_Lock;
78 -- This is a lock to allow only one thread of control in the RTS at
79 -- a time; it is used to execute in mutual exclusion from all other tasks.
80 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
82 ATCB_Key : aliased pthread_key_t;
83 -- Key used to find the Ada Task_Id associated with a thread
85 Environment_Task_Id : Task_Id;
86 -- A variable to hold Task_Id for the environment task
88 Locking_Policy : Character;
89 pragma Import (C, Locking_Policy, "__gl_locking_policy");
90 -- Value of the pragma Locking_Policy:
91 -- 'C' for Ceiling_Locking
92 -- 'I' for Inherit_Locking
95 Unblocked_Signal_Mask : aliased sigset_t;
96 -- The set of signals that should unblocked in all tasks
98 -- The followings are internal configuration constants needed
100 Next_Serial_Number : Task_Serial_Number := 100;
101 -- We start at 100, to reserve some special values for
102 -- using in error checking.
104 Time_Slice_Val : Integer;
105 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
107 Dispatching_Policy : Character;
108 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
110 Foreign_Task_Elaborated : aliased Boolean := True;
111 -- Used to identified fake tasks (i.e., non-Ada Threads)
119 procedure Initialize (Environment_Task : Task_Id);
120 pragma Inline (Initialize);
121 -- Initialize various data needed by this package
123 function Is_Valid_Task return Boolean;
124 pragma Inline (Is_Valid_Task);
125 -- Does the current thread have an ATCB?
127 procedure Set (Self_Id : Task_Id);
129 -- Set the self id for the current task
131 function Self return Task_Id;
132 pragma Inline (Self);
133 -- Return a pointer to the Ada Task Control Block of the calling task
137 package body Specific is separate;
138 -- The body of this package is target specific
140 ---------------------------------
141 -- Support for foreign threads --
142 ---------------------------------
144 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
145 -- Allocate and Initialize a new ATCB for the current Thread
147 function Register_Foreign_Thread
148 (Thread : Thread_Id) return Task_Id is separate;
150 -----------------------
151 -- Local Subprograms --
152 -----------------------
154 procedure Abort_Handler (Sig : Signal);
155 -- Signal handler used to implement asynchronous abort
157 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority);
158 -- This procedure calls the scheduler of the OS to set thread's priority
164 procedure Abort_Handler (Sig : Signal) is
165 pragma Unreferenced (Sig);
167 T : constant Task_Id := Self;
168 Result : Interfaces.C.int;
169 Old_Set : aliased sigset_t;
172 -- It is not safe to raise an exception when using ZCX and the GCC
173 -- exception handling mechanism.
175 if ZCX_By_Default and then GCC_ZCX_Support then
179 if T.Deferral_Level = 0
180 and then T.Pending_ATC_Level < T.ATC_Nesting_Level
181 and then not T.Aborting
185 -- Make sure signals used for RTS internal purpose are unmasked
190 Unblocked_Signal_Mask'Access,
192 pragma Assert (Result = 0);
194 raise Standard'Abort_Signal;
202 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
203 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
204 Guard_Page_Address : Address;
206 Res : Interfaces.C.int;
209 if Stack_Base_Available then
211 -- Compute the guard page address
213 Guard_Page_Address :=
214 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
217 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
219 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
222 pragma Assert (Res = 0);
230 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
232 return T.Common.LL.Thread;
239 function Self return Task_Id renames Specific.Self;
241 ---------------------
242 -- Initialize_Lock --
243 ---------------------
245 procedure Initialize_Lock
246 (Prio : System.Any_Priority;
247 L : not null access Lock)
249 Attributes : aliased pthread_mutexattr_t;
250 Result : Interfaces.C.int;
253 Result := pthread_mutexattr_init (Attributes'Access);
254 pragma Assert (Result = 0 or else Result = ENOMEM);
256 if Result = ENOMEM then
260 if Locking_Policy = 'C' then
264 Result := pthread_mutex_init (L.Mutex'Access, Attributes'Access);
265 pragma Assert (Result = 0 or else Result = ENOMEM);
267 if Result = ENOMEM then
271 Result := pthread_mutexattr_destroy (Attributes'Access);
272 pragma Assert (Result = 0);
275 procedure Initialize_Lock
276 (L : not null access RTS_Lock;
279 pragma Unreferenced (Level);
281 Attributes : aliased pthread_mutexattr_t;
282 Result : Interfaces.C.int;
285 Result := pthread_mutexattr_init (Attributes'Access);
286 pragma Assert (Result = 0 or else Result = ENOMEM);
288 if Result = ENOMEM then
292 Result := pthread_mutex_init (L, Attributes'Access);
293 pragma Assert (Result = 0 or else Result = ENOMEM);
295 if Result = ENOMEM then
296 Result := pthread_mutexattr_destroy (Attributes'Access);
300 Result := pthread_mutexattr_destroy (Attributes'Access);
301 pragma Assert (Result = 0);
308 procedure Finalize_Lock (L : not null access Lock) is
309 Result : Interfaces.C.int;
311 Result := pthread_mutex_destroy (L.Mutex'Access);
312 pragma Assert (Result = 0);
315 procedure Finalize_Lock (L : not null access RTS_Lock) is
316 Result : Interfaces.C.int;
318 Result := pthread_mutex_destroy (L);
319 pragma Assert (Result = 0);
327 (L : not null access Lock;
328 Ceiling_Violation : out Boolean)
330 Result : Interfaces.C.int;
331 T : constant Task_Id := Self;
334 if Locking_Policy = 'C' then
335 if T.Common.Current_Priority > L.Ceiling then
336 Ceiling_Violation := True;
340 L.Saved_Priority := T.Common.Current_Priority;
342 if T.Common.Current_Priority < L.Ceiling then
343 Set_OS_Priority (T, L.Ceiling);
347 Result := pthread_mutex_lock (L.Mutex'Access);
349 -- Assume that the cause of EINVAL is a priority ceiling violation
351 Ceiling_Violation := (Result = EINVAL);
352 pragma Assert (Result = 0 or else Result = EINVAL);
355 -- No tricks on RTS_Locks
358 (L : not null access RTS_Lock;
359 Global_Lock : Boolean := False)
361 Result : Interfaces.C.int;
363 if not Single_Lock or else Global_Lock then
364 Result := pthread_mutex_lock (L);
365 pragma Assert (Result = 0);
369 procedure Write_Lock (T : Task_Id) is
370 Result : Interfaces.C.int;
372 if not Single_Lock then
373 Result := pthread_mutex_lock (T.Common.LL.L'Access);
374 pragma Assert (Result = 0);
383 (L : not null access Lock;
384 Ceiling_Violation : out Boolean)
387 Write_Lock (L, Ceiling_Violation);
394 procedure Unlock (L : not null access Lock) is
395 Result : Interfaces.C.int;
396 T : constant Task_Id := Self;
399 Result := pthread_mutex_unlock (L.Mutex'Access);
400 pragma Assert (Result = 0);
402 if Locking_Policy = 'C' then
403 if T.Common.Current_Priority > L.Saved_Priority then
404 Set_OS_Priority (T, L.Saved_Priority);
410 (L : not null access RTS_Lock;
411 Global_Lock : Boolean := False)
413 Result : Interfaces.C.int;
415 if not Single_Lock or else Global_Lock then
416 Result := pthread_mutex_unlock (L);
417 pragma Assert (Result = 0);
421 procedure Unlock (T : Task_Id) is
422 Result : Interfaces.C.int;
424 if not Single_Lock then
425 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
426 pragma Assert (Result = 0);
434 -- Dynamic priority ceilings are not supported by the underlying system
436 procedure Set_Ceiling
437 (L : not null access Lock;
438 Prio : System.Any_Priority)
440 pragma Unreferenced (L, Prio);
451 Reason : System.Tasking.Task_States)
453 pragma Unreferenced (Reason);
454 Result : Interfaces.C.int;
460 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
464 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
467 -- EINTR is not considered a failure
469 pragma Assert (Result = 0 or else Result = EINTR);
476 -- This is for use within the run-time system, so abort is
477 -- assumed to be already deferred, and the caller should be
478 -- holding its own ATCB lock.
480 procedure Timed_Sleep
483 Mode : ST.Delay_Modes;
484 Reason : Task_States;
485 Timedout : out Boolean;
486 Yielded : out Boolean)
488 pragma Unreferenced (Reason);
490 Base_Time : constant Duration := Monotonic_Clock;
491 Check_Time : Duration := Base_Time;
494 Request : aliased timespec;
495 Result : Interfaces.C.int;
501 if Mode = Relative then
502 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
504 if Relative_Timed_Wait then
505 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
509 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
511 if Relative_Timed_Wait then
512 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
516 if Abs_Time > Check_Time then
517 if Relative_Timed_Wait then
518 Request := To_Timespec (Rel_Time);
520 Request := To_Timespec (Abs_Time);
524 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
528 pthread_cond_timedwait
529 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
534 pthread_cond_timedwait
535 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
539 Check_Time := Monotonic_Clock;
540 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
542 if Result = 0 or Result = EINTR then
544 -- Somebody may have called Wakeup for us
550 pragma Assert (Result = ETIMEDOUT);
559 -- This is for use in implementing delay statements, so we assume
560 -- the caller is abort-deferred but is holding no locks.
562 procedure Timed_Delay
565 Mode : ST.Delay_Modes)
567 Base_Time : constant Duration := Monotonic_Clock;
568 Check_Time : Duration := Base_Time;
571 Request : aliased timespec;
573 Result : Interfaces.C.int;
574 pragma Warnings (Off, Result);
581 -- Comments needed in code below ???
583 Write_Lock (Self_ID);
585 if Mode = Relative then
586 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
588 if Relative_Timed_Wait then
589 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
593 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
595 if Relative_Timed_Wait then
596 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
600 if Abs_Time > Check_Time then
601 if Relative_Timed_Wait then
602 Request := To_Timespec (Rel_Time);
604 Request := To_Timespec (Abs_Time);
607 Self_ID.Common.State := Delay_Sleep;
610 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
614 pthread_cond_timedwait
615 (Self_ID.Common.LL.CV'Access,
616 Single_RTS_Lock'Access,
620 pthread_cond_timedwait
621 (Self_ID.Common.LL.CV'Access,
622 Self_ID.Common.LL.L'Access,
626 Check_Time := Monotonic_Clock;
627 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
629 pragma Assert (Result = 0 or else
630 Result = ETIMEDOUT or else
634 Self_ID.Common.State := Runnable;
643 Result := sched_yield;
646 ---------------------
647 -- Monotonic_Clock --
648 ---------------------
650 function Monotonic_Clock return Duration is
651 TS : aliased timespec;
652 Result : Interfaces.C.int;
656 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
657 pragma Assert (Result = 0);
658 return To_Duration (TS);
665 function RT_Resolution return Duration is
666 Res : aliased timespec;
667 Result : Interfaces.C.int;
671 (clock_id => CLOCK_REALTIME, Res => Res'Unchecked_Access);
672 pragma Assert (Result = 0);
673 return To_Duration (Res);
680 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
681 pragma Unreferenced (Reason);
682 Result : Interfaces.C.int;
684 Result := pthread_cond_signal (T.Common.LL.CV'Access);
685 pragma Assert (Result = 0);
692 procedure Yield (Do_Yield : Boolean := True) is
693 Result : Interfaces.C.int;
694 pragma Unreferenced (Result);
697 Result := sched_yield;
705 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority) is
706 Result : Interfaces.C.int;
707 Param : aliased struct_sched_param;
709 function Get_Policy (Prio : System.Any_Priority) return Character;
710 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
711 -- Get priority specific dispatching policy
713 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
714 -- Upper case first character of the policy name corresponding to the
715 -- task as set by a Priority_Specific_Dispatching pragma.
718 Param.sched_priority := Interfaces.C.int (Prio);
720 if Time_Slice_Supported
721 and then (Dispatching_Policy = 'R'
722 or else Priority_Specific_Policy = 'R'
723 or else Time_Slice_Val > 0)
726 pthread_setschedparam
727 (T.Common.LL.Thread, SCHED_RR, Param'Access);
729 elsif Dispatching_Policy = 'F'
730 or else Priority_Specific_Policy = 'F'
731 or else Time_Slice_Val = 0
734 pthread_setschedparam
735 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
739 pthread_setschedparam
740 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
743 pragma Assert (Result = 0);
746 type Prio_Array_Type is array (System.Any_Priority) of Integer;
747 pragma Atomic_Components (Prio_Array_Type);
748 Prio_Array : Prio_Array_Type;
749 -- Comments needed for these declarations ???
751 procedure Set_Priority
753 Prio : System.Any_Priority;
754 Loss_Of_Inheritance : Boolean := False)
756 Array_Item : Integer;
759 Set_OS_Priority (T, Prio);
761 if Locking_Policy = 'C' then
763 -- Annex D requirements: loss of inheritance puts task at the start
764 -- of the queue for that prio; copied from 5ztaprop (VxWorks).
766 if Loss_Of_Inheritance
767 and then Prio < T.Common.Current_Priority then
769 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
770 Prio_Array (T.Common.Base_Priority) := Array_Item;
774 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
775 or else Prio_Array (T.Common.Base_Priority) = 1;
778 Prio_Array (T.Common.Base_Priority) :=
779 Prio_Array (T.Common.Base_Priority) - 1;
783 T.Common.Current_Priority := Prio;
790 function Get_Priority (T : Task_Id) return System.Any_Priority is
792 return T.Common.Current_Priority;
799 procedure Enter_Task (Self_ID : Task_Id) is
801 Self_ID.Common.LL.Thread := pthread_self;
802 Self_ID.Common.LL.LWP := lwp_self;
804 Specific.Set (Self_ID);
808 for J in Known_Tasks'Range loop
809 if Known_Tasks (J) = null then
810 Known_Tasks (J) := Self_ID;
811 Self_ID.Known_Tasks_Index := J;
823 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
825 return new Ada_Task_Control_Block (Entry_Num);
832 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
834 -----------------------------
835 -- Register_Foreign_Thread --
836 -----------------------------
838 function Register_Foreign_Thread return Task_Id is
840 if Is_Valid_Task then
843 return Register_Foreign_Thread (pthread_self);
845 end Register_Foreign_Thread;
851 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
852 Mutex_Attr : aliased pthread_mutexattr_t;
853 Result : Interfaces.C.int;
854 Cond_Attr : aliased pthread_condattr_t;
857 -- Give the task a unique serial number
859 Self_ID.Serial_Number := Next_Serial_Number;
860 Next_Serial_Number := Next_Serial_Number + 1;
861 pragma Assert (Next_Serial_Number /= 0);
863 if not Single_Lock then
864 Result := pthread_mutexattr_init (Mutex_Attr'Access);
865 pragma Assert (Result = 0 or else Result = ENOMEM);
870 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
871 pragma Assert (Result = 0 or else Result = ENOMEM);
879 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
880 pragma Assert (Result = 0);
883 Result := pthread_condattr_init (Cond_Attr'Access);
884 pragma Assert (Result = 0 or else Result = ENOMEM);
888 pthread_cond_init (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
889 pragma Assert (Result = 0 or else Result = ENOMEM);
895 if not Single_Lock then
896 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
897 pragma Assert (Result = 0);
903 Result := pthread_condattr_destroy (Cond_Attr'Access);
904 pragma Assert (Result = 0);
911 procedure Create_Task
913 Wrapper : System.Address;
914 Stack_Size : System.Parameters.Size_Type;
915 Priority : System.Any_Priority;
916 Succeeded : out Boolean)
918 Attributes : aliased pthread_attr_t;
919 Adjusted_Stack_Size : Interfaces.C.size_t;
920 Result : Interfaces.C.int;
922 use System.Task_Info;
925 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
927 if Stack_Base_Available then
929 -- If Stack Checking is supported then allocate 2 additional pages:
931 -- In the worst case, stack is allocated at something like
932 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
933 -- to be sure the effective stack size is greater than what
936 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
939 Result := pthread_attr_init (Attributes'Access);
940 pragma Assert (Result = 0 or else Result = ENOMEM);
948 pthread_attr_setdetachstate
949 (Attributes'Access, PTHREAD_CREATE_DETACHED);
950 pragma Assert (Result = 0);
953 pthread_attr_setstacksize
954 (Attributes'Access, Adjusted_Stack_Size);
955 pragma Assert (Result = 0);
957 if T.Common.Task_Info /= Default_Scope then
959 -- We are assuming that Scope_Type has the same values than the
960 -- corresponding C macros
963 pthread_attr_setscope
964 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
965 pragma Assert (Result = 0);
968 -- Since the initial signal mask of a thread is inherited from the
969 -- creator, and the Environment task has all its signals masked, we
970 -- do not need to manipulate caller's signal mask at this point.
971 -- All tasks in RTS will have All_Tasks_Mask initially.
975 (T.Common.LL.Thread'Access,
977 Thread_Body_Access (Wrapper),
979 pragma Assert (Result = 0 or else Result = EAGAIN);
981 Succeeded := Result = 0;
983 Result := pthread_attr_destroy (Attributes'Access);
984 pragma Assert (Result = 0);
987 Set_Priority (T, Priority);
995 procedure Finalize_TCB (T : Task_Id) is
996 Result : Interfaces.C.int;
998 Is_Self : constant Boolean := T = Self;
1000 procedure Free is new
1001 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
1004 if not Single_Lock then
1005 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
1006 pragma Assert (Result = 0);
1009 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
1010 pragma Assert (Result = 0);
1012 if T.Known_Tasks_Index /= -1 then
1013 Known_Tasks (T.Known_Tasks_Index) := null;
1019 Result := st_setspecific (ATCB_Key, System.Null_Address);
1020 pragma Assert (Result = 0);
1028 procedure Exit_Task is
1030 Specific.Set (null);
1037 procedure Abort_Task (T : Task_Id) is
1038 Result : Interfaces.C.int;
1042 (T.Common.LL.Thread,
1043 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1044 pragma Assert (Result = 0);
1051 procedure Initialize (S : in out Suspension_Object) is
1052 Mutex_Attr : aliased pthread_mutexattr_t;
1053 Cond_Attr : aliased pthread_condattr_t;
1054 Result : Interfaces.C.int;
1057 -- Initialize internal state (always to False (RM D.10(6)))
1062 -- Initialize internal mutex
1064 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1065 pragma Assert (Result = 0 or else Result = ENOMEM);
1067 if Result = ENOMEM then
1068 raise Storage_Error;
1071 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1072 pragma Assert (Result = 0 or else Result = ENOMEM);
1074 if Result = ENOMEM then
1075 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1076 pragma Assert (Result = 0);
1078 raise Storage_Error;
1081 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1082 pragma Assert (Result = 0);
1084 -- Initialize internal condition variable
1086 Result := pthread_condattr_init (Cond_Attr'Access);
1087 pragma Assert (Result = 0 or else Result = ENOMEM);
1090 Result := pthread_mutex_destroy (S.L'Access);
1091 pragma Assert (Result = 0);
1093 if Result = ENOMEM then
1094 raise Storage_Error;
1098 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1099 pragma Assert (Result = 0 or else Result = ENOMEM);
1102 Result := pthread_mutex_destroy (S.L'Access);
1103 pragma Assert (Result = 0);
1105 if Result = ENOMEM then
1106 Result := pthread_condattr_destroy (Cond_Attr'Access);
1107 pragma Assert (Result = 0);
1109 raise Storage_Error;
1113 Result := pthread_condattr_destroy (Cond_Attr'Access);
1114 pragma Assert (Result = 0);
1121 procedure Finalize (S : in out Suspension_Object) is
1122 Result : Interfaces.C.int;
1125 -- Destroy internal mutex
1127 Result := pthread_mutex_destroy (S.L'Access);
1128 pragma Assert (Result = 0);
1130 -- Destroy internal condition variable
1132 Result := pthread_cond_destroy (S.CV'Access);
1133 pragma Assert (Result = 0);
1140 function Current_State (S : Suspension_Object) return Boolean is
1142 -- We do not want to use lock on this read operation. State is marked
1143 -- as Atomic so that we ensure that the value retrieved is correct.
1152 procedure Set_False (S : in out Suspension_Object) is
1153 Result : Interfaces.C.int;
1156 SSL.Abort_Defer.all;
1158 Result := pthread_mutex_lock (S.L'Access);
1159 pragma Assert (Result = 0);
1163 Result := pthread_mutex_unlock (S.L'Access);
1164 pragma Assert (Result = 0);
1166 SSL.Abort_Undefer.all;
1173 procedure Set_True (S : in out Suspension_Object) is
1174 Result : Interfaces.C.int;
1177 SSL.Abort_Defer.all;
1179 Result := pthread_mutex_lock (S.L'Access);
1180 pragma Assert (Result = 0);
1182 -- If there is already a task waiting on this suspension object then
1183 -- we resume it, leaving the state of the suspension object to False,
1184 -- as specified in (RM D.10(9)). Otherwise, just leave state set True.
1190 Result := pthread_cond_signal (S.CV'Access);
1191 pragma Assert (Result = 0);
1197 Result := pthread_mutex_unlock (S.L'Access);
1198 pragma Assert (Result = 0);
1200 SSL.Abort_Undefer.all;
1203 ------------------------
1204 -- Suspend_Until_True --
1205 ------------------------
1207 procedure Suspend_Until_True (S : in out Suspension_Object) is
1208 Result : Interfaces.C.int;
1211 SSL.Abort_Defer.all;
1213 Result := pthread_mutex_lock (S.L'Access);
1214 pragma Assert (Result = 0);
1218 -- Program_Error must be raised upon calling Suspend_Until_True
1219 -- if another task is already waiting on that suspension object
1222 Result := pthread_mutex_unlock (S.L'Access);
1223 pragma Assert (Result = 0);
1225 SSL.Abort_Undefer.all;
1227 raise Program_Error;
1230 -- Suspend the task if the state is False. Otherwise, the task
1231 -- continues its execution, and the state of the suspension object
1232 -- is set to False (RM D.10(9)).
1238 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1241 Result := pthread_mutex_unlock (S.L'Access);
1242 pragma Assert (Result = 0);
1244 SSL.Abort_Undefer.all;
1246 end Suspend_Until_True;
1254 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1255 pragma Unreferenced (Self_ID);
1260 --------------------
1261 -- Check_No_Locks --
1262 --------------------
1264 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1265 pragma Unreferenced (Self_ID);
1270 ----------------------
1271 -- Environment_Task --
1272 ----------------------
1274 function Environment_Task return Task_Id is
1276 return Environment_Task_Id;
1277 end Environment_Task;
1283 procedure Lock_RTS is
1285 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1292 procedure Unlock_RTS is
1294 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1301 function Suspend_Task
1303 Thread_Self : Thread_Id) return Boolean
1305 pragma Unreferenced (T);
1306 pragma Unreferenced (Thread_Self);
1315 function Resume_Task
1317 Thread_Self : Thread_Id) return Boolean
1319 pragma Unreferenced (T);
1320 pragma Unreferenced (Thread_Self);
1325 --------------------
1326 -- Stop_All_Tasks --
1327 --------------------
1329 procedure Stop_All_Tasks is
1338 function Stop_Task (T : ST.Task_Id) return Boolean is
1339 pragma Unreferenced (T);
1348 function Continue_Task (T : ST.Task_Id) return Boolean is
1349 pragma Unreferenced (T);
1358 procedure Initialize (Environment_Task : Task_Id) is
1359 act : aliased struct_sigaction;
1360 old_act : aliased struct_sigaction;
1361 Tmp_Set : aliased sigset_t;
1362 Result : Interfaces.C.int;
1365 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1366 pragma Import (C, State, "__gnat_get_interrupt_state");
1367 -- Get interrupt state. Defined in a-init.c
1368 -- The input argument is the interrupt number,
1369 -- and the result is one of the following:
1371 Default : constant Character := 's';
1372 -- 'n' this interrupt not set by any Interrupt_State pragma
1373 -- 'u' Interrupt_State pragma set state to User
1374 -- 'r' Interrupt_State pragma set state to Runtime
1375 -- 's' Interrupt_State pragma set state to System (use "default"
1379 Environment_Task_Id := Environment_Task;
1381 Interrupt_Management.Initialize;
1383 -- Prepare the set of signals that should unblocked in all tasks
1385 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1386 pragma Assert (Result = 0);
1388 for J in Interrupt_Management.Interrupt_ID loop
1389 if System.Interrupt_Management.Keep_Unmasked (J) then
1390 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1391 pragma Assert (Result = 0);
1395 -- Initialize the lock used to synchronize chain of all ATCBs
1397 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1399 Specific.Initialize (Environment_Task);
1401 Enter_Task (Environment_Task);
1403 -- Install the abort-signal handler
1406 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1409 act.sa_handler := Abort_Handler'Address;
1411 Result := sigemptyset (Tmp_Set'Access);
1412 pragma Assert (Result = 0);
1413 act.sa_mask := Tmp_Set;
1417 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1418 act'Unchecked_Access,
1419 old_act'Unchecked_Access);
1421 pragma Assert (Result = 0);
1425 end System.Task_Primitives.Operations;