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-2005, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
35 -- SCHED_FIFO and ceiling locking (Annex D compliance) work properly
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System.Tasking.Debug;
45 -- used for Known_Tasks
47 with System.Task_Info;
48 -- used for Task_Info_Type
54 with System.Interrupt_Management;
55 -- used for Keep_Unmasked
56 -- Abort_Task_Interrupt
59 with System.Parameters;
63 -- used for Ada_Task_Control_Block
66 with System.Soft_Links;
67 -- used for Defer/Undefer_Abort
69 -- Note that we do not use System.Tasking.Initialization directly since
70 -- this is a higher level package that we shouldn't depend on. For example
71 -- when using the restricted run time, it is replaced by
72 -- System.Tasking.Restricted.Stages.
74 with System.OS_Primitives;
75 -- used for Delay_Modes
77 with Unchecked_Deallocation;
79 package body System.Task_Primitives.Operations is
81 use System.Tasking.Debug;
84 use System.OS_Interface;
85 use System.Parameters;
86 use System.OS_Primitives;
88 package SSL renames System.Soft_Links;
94 -- The followings are logically constants, but need to be initialized
97 Single_RTS_Lock : aliased RTS_Lock;
98 -- This is a lock to allow only one thread of control in the RTS at
99 -- a 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
102 ATCB_Key : aliased pthread_key_t;
103 -- Key used to find the Ada Task_Id associated with a thread
105 Environment_Task_Id : Task_Id;
106 -- A variable to hold Task_Id for the environment task
108 Locking_Policy : Character;
109 pragma Import (C, Locking_Policy, "__gl_locking_policy");
110 -- Value of the pragma Locking_Policy:
111 -- 'C' for Ceiling_Locking
112 -- 'I' for Inherit_Locking
115 Unblocked_Signal_Mask : aliased sigset_t;
116 -- The set of signals that should unblocked in all tasks
118 -- The followings are internal configuration constants needed
120 Next_Serial_Number : Task_Serial_Number := 100;
121 -- We start at 100, to reserve some special values for
122 -- using in error checking.
124 Time_Slice_Val : Integer;
125 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
127 Dispatching_Policy : Character;
128 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
130 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
131 -- Indicates whether FIFO_Within_Priorities is set
133 Foreign_Task_Elaborated : aliased Boolean := True;
134 -- Used to identified fake tasks (i.e., non-Ada Threads)
142 procedure Initialize (Environment_Task : Task_Id);
143 pragma Inline (Initialize);
144 -- Initialize various data needed by this package
146 function Is_Valid_Task return Boolean;
147 pragma Inline (Is_Valid_Task);
148 -- Does the current thread have an ATCB?
150 procedure Set (Self_Id : Task_Id);
152 -- Set the self id for the current task
154 function Self return Task_Id;
155 pragma Inline (Self);
156 -- Return a pointer to the Ada Task Control Block of the calling task
160 package body Specific is separate;
161 -- The body of this package is target specific
163 ---------------------------------
164 -- Support for foreign threads --
165 ---------------------------------
167 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
168 -- Allocate and Initialize a new ATCB for the current Thread
170 function Register_Foreign_Thread
171 (Thread : Thread_Id) return Task_Id is separate;
173 -----------------------
174 -- Local Subprograms --
175 -----------------------
177 procedure Abort_Handler (Sig : Signal);
178 -- Signal handler used to implement asynchronous abort
180 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority);
181 -- This procedure calls the scheduler of the OS to set thread's priority
187 procedure Abort_Handler (Sig : Signal) is
188 pragma Unreferenced (Sig);
190 T : constant Task_Id := Self;
191 Result : Interfaces.C.int;
192 Old_Set : aliased sigset_t;
195 -- It is not safe to raise an exception when using ZCX and the GCC
196 -- exception handling mechanism.
198 if ZCX_By_Default and then GCC_ZCX_Support then
202 if T.Deferral_Level = 0
203 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
208 -- Make sure signals used for RTS internal purpose are unmasked
211 pthread_sigmask (SIG_UNBLOCK,
212 Unblocked_Signal_Mask'Unchecked_Access,
213 Old_Set'Unchecked_Access);
214 pragma Assert (Result = 0);
216 raise Standard'Abort_Signal;
224 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
225 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
226 Guard_Page_Address : Address;
228 Res : Interfaces.C.int;
231 if Stack_Base_Available then
233 -- Compute the guard page address
235 Guard_Page_Address :=
236 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
239 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
241 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
244 pragma Assert (Res = 0);
252 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
254 return T.Common.LL.Thread;
261 function Self return Task_Id renames Specific.Self;
263 ---------------------
264 -- Initialize_Lock --
265 ---------------------
267 procedure Initialize_Lock
268 (Prio : System.Any_Priority;
271 Attributes : aliased pthread_mutexattr_t;
272 Result : Interfaces.C.int;
275 Result := pthread_mutexattr_init (Attributes'Access);
276 pragma Assert (Result = 0 or else Result = ENOMEM);
278 if Result = ENOMEM then
282 if Locking_Policy = 'C' then
286 Result := pthread_mutex_init (L.Mutex'Access, Attributes'Access);
287 pragma Assert (Result = 0 or else Result = ENOMEM);
289 if Result = ENOMEM then
293 Result := pthread_mutexattr_destroy (Attributes'Access);
294 pragma Assert (Result = 0);
297 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
298 pragma Unreferenced (Level);
300 Attributes : aliased pthread_mutexattr_t;
301 Result : Interfaces.C.int;
304 Result := pthread_mutexattr_init (Attributes'Access);
305 pragma Assert (Result = 0 or else Result = ENOMEM);
307 if Result = ENOMEM then
311 Result := pthread_mutex_init (L, Attributes'Access);
312 pragma Assert (Result = 0 or else Result = ENOMEM);
314 if Result = ENOMEM then
315 Result := pthread_mutexattr_destroy (Attributes'Access);
319 Result := pthread_mutexattr_destroy (Attributes'Access);
320 pragma Assert (Result = 0);
327 procedure Finalize_Lock (L : access Lock) is
328 Result : Interfaces.C.int;
330 Result := pthread_mutex_destroy (L.Mutex'Access);
331 pragma Assert (Result = 0);
334 procedure Finalize_Lock (L : access RTS_Lock) is
335 Result : Interfaces.C.int;
337 Result := pthread_mutex_destroy (L);
338 pragma Assert (Result = 0);
345 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
346 Result : Interfaces.C.int;
347 T : constant Task_Id := Self;
350 if Locking_Policy = 'C' then
351 if T.Common.Current_Priority > L.Ceiling then
352 Ceiling_Violation := True;
356 L.Saved_Priority := T.Common.Current_Priority;
358 if T.Common.Current_Priority < L.Ceiling then
359 Set_OS_Priority (T, L.Ceiling);
363 Result := pthread_mutex_lock (L.Mutex'Access);
365 -- Assume that the cause of EINVAL is a priority ceiling violation
367 Ceiling_Violation := (Result = EINVAL);
368 pragma Assert (Result = 0 or else Result = EINVAL);
371 -- No tricks on RTS_Locks
374 (L : access RTS_Lock; Global_Lock : Boolean := False)
376 Result : Interfaces.C.int;
378 if not Single_Lock or else Global_Lock then
379 Result := pthread_mutex_lock (L);
380 pragma Assert (Result = 0);
384 procedure Write_Lock (T : Task_Id) is
385 Result : Interfaces.C.int;
387 if not Single_Lock then
388 Result := pthread_mutex_lock (T.Common.LL.L'Access);
389 pragma Assert (Result = 0);
397 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
399 Write_Lock (L, Ceiling_Violation);
406 procedure Unlock (L : access Lock) is
407 Result : Interfaces.C.int;
408 T : constant Task_Id := Self;
411 Result := pthread_mutex_unlock (L.Mutex'Access);
412 pragma Assert (Result = 0);
414 if Locking_Policy = 'C' then
415 if T.Common.Current_Priority > L.Saved_Priority then
416 Set_OS_Priority (T, L.Saved_Priority);
421 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
422 Result : Interfaces.C.int;
424 if not Single_Lock or else Global_Lock then
425 Result := pthread_mutex_unlock (L);
426 pragma Assert (Result = 0);
430 procedure Unlock (T : Task_Id) is
431 Result : Interfaces.C.int;
433 if not Single_Lock then
434 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
435 pragma Assert (Result = 0);
445 Reason : System.Tasking.Task_States)
447 pragma Unreferenced (Reason);
448 Result : Interfaces.C.int;
452 Result := pthread_cond_wait
453 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
455 Result := pthread_cond_wait
456 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
459 -- EINTR is not considered a failure
461 pragma Assert (Result = 0 or else Result = EINTR);
468 -- This is for use within the run-time system, so abort is
469 -- assumed to be already deferred, and the caller should be
470 -- holding its own ATCB lock.
472 procedure Timed_Sleep
475 Mode : ST.Delay_Modes;
476 Reason : Task_States;
477 Timedout : out Boolean;
478 Yielded : out Boolean)
480 pragma Unreferenced (Reason);
482 Check_Time : constant Duration := Monotonic_Clock;
485 Request : aliased timespec;
486 Result : Interfaces.C.int;
492 if Mode = Relative then
493 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
495 if Relative_Timed_Wait then
496 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
500 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
502 if Relative_Timed_Wait then
503 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
507 if Abs_Time > Check_Time then
508 if Relative_Timed_Wait then
509 Request := To_Timespec (Rel_Time);
511 Request := To_Timespec (Abs_Time);
515 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
516 or else Self_ID.Pending_Priority_Change;
519 Result := pthread_cond_timedwait
520 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
524 Result := pthread_cond_timedwait
525 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
529 exit when Abs_Time <= Monotonic_Clock;
531 if Result = 0 or Result = EINTR then
533 -- Somebody may have called Wakeup for us
539 pragma Assert (Result = ETIMEDOUT);
548 -- This is for use in implementing delay statements, so we assume
549 -- the caller is abort-deferred but is holding no locks.
551 procedure Timed_Delay
554 Mode : ST.Delay_Modes)
556 Check_Time : constant Duration := Monotonic_Clock;
559 Request : aliased timespec;
560 Result : Interfaces.C.int;
563 -- Only the little window between deferring abort and
564 -- locking Self_ID is the reason we need to
565 -- check for pending abort and priority change below!
573 -- Comments needed in code below ???
575 Write_Lock (Self_ID);
577 if Mode = Relative then
578 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
580 if Relative_Timed_Wait then
581 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
585 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
587 if Relative_Timed_Wait then
588 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
592 if Abs_Time > Check_Time then
593 if Relative_Timed_Wait then
594 Request := To_Timespec (Rel_Time);
596 Request := To_Timespec (Abs_Time);
599 Self_ID.Common.State := Delay_Sleep;
602 if Self_ID.Pending_Priority_Change then
603 Self_ID.Pending_Priority_Change := False;
604 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
605 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
608 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
611 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
612 Single_RTS_Lock'Access, Request'Access);
614 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
615 Self_ID.Common.LL.L'Access, Request'Access);
618 exit when Abs_Time <= Monotonic_Clock;
620 pragma Assert (Result = 0
621 or else Result = ETIMEDOUT
622 or else Result = EINTR);
625 Self_ID.Common.State := Runnable;
634 Result := sched_yield;
635 SSL.Abort_Undefer.all;
638 ---------------------
639 -- Monotonic_Clock --
640 ---------------------
642 function Monotonic_Clock return Duration is
643 TS : aliased timespec;
644 Result : Interfaces.C.int;
646 Result := clock_gettime
647 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
648 pragma Assert (Result = 0);
649 return To_Duration (TS);
656 function RT_Resolution return Duration is
657 Res : aliased timespec;
658 Result : Interfaces.C.int;
660 Result := clock_getres
661 (clock_id => CLOCK_REALTIME, Res => Res'Unchecked_Access);
662 pragma Assert (Result = 0);
663 return To_Duration (Res);
670 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
671 pragma Unreferenced (Reason);
672 Result : Interfaces.C.int;
674 Result := pthread_cond_signal (T.Common.LL.CV'Access);
675 pragma Assert (Result = 0);
682 procedure Yield (Do_Yield : Boolean := True) is
683 Result : Interfaces.C.int;
684 pragma Unreferenced (Result);
687 Result := sched_yield;
695 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority) is
696 Result : Interfaces.C.int;
697 Param : aliased struct_sched_param;
700 Param.sched_priority := Interfaces.C.int (Prio);
702 if Time_Slice_Supported and then Time_Slice_Val > 0 then
703 Result := pthread_setschedparam
704 (T.Common.LL.Thread, SCHED_RR, Param'Access);
706 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
707 Result := pthread_setschedparam
708 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
711 Result := pthread_setschedparam
712 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
715 pragma Assert (Result = 0);
718 type Prio_Array_Type is array (System.Any_Priority) of Integer;
719 pragma Atomic_Components (Prio_Array_Type);
720 Prio_Array : Prio_Array_Type;
721 -- Comments needed for these declarations ???
723 procedure Set_Priority
725 Prio : System.Any_Priority;
726 Loss_Of_Inheritance : Boolean := False)
728 Array_Item : Integer;
731 Set_OS_Priority (T, Prio);
733 if Locking_Policy = 'C' then
734 -- Annex D requirements: loss of inheritance puts task at the
735 -- beginning of the queue for that prio; copied from 5ztaprop
738 if Loss_Of_Inheritance
739 and then Prio < T.Common.Current_Priority then
741 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
742 Prio_Array (T.Common.Base_Priority) := Array_Item;
746 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
747 or else Prio_Array (T.Common.Base_Priority) = 1;
750 Prio_Array (T.Common.Base_Priority) :=
751 Prio_Array (T.Common.Base_Priority) - 1;
755 T.Common.Current_Priority := Prio;
762 function Get_Priority (T : Task_Id) return System.Any_Priority is
764 return T.Common.Current_Priority;
771 procedure Enter_Task (Self_ID : Task_Id) is
773 Self_ID.Common.LL.Thread := pthread_self;
774 Self_ID.Common.LL.LWP := lwp_self;
776 Specific.Set (Self_ID);
780 for J in Known_Tasks'Range loop
781 if Known_Tasks (J) = null then
782 Known_Tasks (J) := Self_ID;
783 Self_ID.Known_Tasks_Index := J;
795 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
797 return new Ada_Task_Control_Block (Entry_Num);
804 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
806 -----------------------------
807 -- Register_Foreign_Thread --
808 -----------------------------
810 function Register_Foreign_Thread return Task_Id is
812 if Is_Valid_Task then
815 return Register_Foreign_Thread (pthread_self);
817 end Register_Foreign_Thread;
823 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
824 Mutex_Attr : aliased pthread_mutexattr_t;
825 Result : Interfaces.C.int;
826 Cond_Attr : aliased pthread_condattr_t;
829 -- Give the task a unique serial number
831 Self_ID.Serial_Number := Next_Serial_Number;
832 Next_Serial_Number := Next_Serial_Number + 1;
833 pragma Assert (Next_Serial_Number /= 0);
835 if not Single_Lock then
836 Result := pthread_mutexattr_init (Mutex_Attr'Access);
837 pragma Assert (Result = 0 or else Result = ENOMEM);
840 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
842 pragma Assert (Result = 0 or else Result = ENOMEM);
850 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
851 pragma Assert (Result = 0);
854 Result := pthread_condattr_init (Cond_Attr'Access);
855 pragma Assert (Result = 0 or else Result = ENOMEM);
858 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
860 pragma Assert (Result = 0 or else Result = ENOMEM);
866 if not Single_Lock then
867 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
868 pragma Assert (Result = 0);
874 Result := pthread_condattr_destroy (Cond_Attr'Access);
875 pragma Assert (Result = 0);
882 procedure Create_Task
884 Wrapper : System.Address;
885 Stack_Size : System.Parameters.Size_Type;
886 Priority : System.Any_Priority;
887 Succeeded : out Boolean)
889 Attributes : aliased pthread_attr_t;
890 Adjusted_Stack_Size : Interfaces.C.size_t;
891 Result : Interfaces.C.int;
893 use System.Task_Info;
896 if Stack_Size = Unspecified_Size then
897 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
899 elsif Stack_Size < Minimum_Stack_Size then
900 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
903 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
906 if Stack_Base_Available then
908 -- If Stack Checking is supported then allocate 2 additional pages:
910 -- In the worst case, stack is allocated at something like
911 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
912 -- to be sure the effective stack size is greater than what
915 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
918 Result := pthread_attr_init (Attributes'Access);
919 pragma Assert (Result = 0 or else Result = ENOMEM);
926 Result := pthread_attr_setdetachstate
927 (Attributes'Access, PTHREAD_CREATE_DETACHED);
928 pragma Assert (Result = 0);
930 Result := pthread_attr_setstacksize
931 (Attributes'Access, Adjusted_Stack_Size);
932 pragma Assert (Result = 0);
934 if T.Common.Task_Info /= Default_Scope then
936 -- We are assuming that Scope_Type has the same values than the
937 -- corresponding C macros
939 Result := pthread_attr_setscope
940 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
941 pragma Assert (Result = 0);
944 -- Since the initial signal mask of a thread is inherited from the
945 -- creator, and the Environment task has all its signals masked, we
946 -- do not need to manipulate caller's signal mask at this point.
947 -- All tasks in RTS will have All_Tasks_Mask initially.
949 Result := pthread_create
950 (T.Common.LL.Thread'Access,
952 Thread_Body_Access (Wrapper),
954 pragma Assert (Result = 0 or else Result = EAGAIN);
956 Succeeded := Result = 0;
958 Result := pthread_attr_destroy (Attributes'Access);
959 pragma Assert (Result = 0);
961 Set_Priority (T, Priority);
968 procedure Finalize_TCB (T : Task_Id) is
969 Result : Interfaces.C.int;
971 Is_Self : constant Boolean := T = Self;
973 procedure Free is new
974 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
977 if not Single_Lock then
978 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
979 pragma Assert (Result = 0);
982 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
983 pragma Assert (Result = 0);
985 if T.Known_Tasks_Index /= -1 then
986 Known_Tasks (T.Known_Tasks_Index) := null;
992 Result := st_setspecific (ATCB_Key, System.Null_Address);
993 pragma Assert (Result = 0);
1002 procedure Exit_Task is
1004 Specific.Set (null);
1011 procedure Abort_Task (T : Task_Id) is
1012 Result : Interfaces.C.int;
1014 Result := pthread_kill (T.Common.LL.Thread,
1015 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1016 pragma Assert (Result = 0);
1023 procedure Initialize (S : in out Suspension_Object) is
1024 Mutex_Attr : aliased pthread_mutexattr_t;
1025 Cond_Attr : aliased pthread_condattr_t;
1026 Result : Interfaces.C.int;
1029 -- Initialize internal state. It is always initialized to False (ARM
1035 -- Initialize internal mutex
1037 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1038 pragma Assert (Result = 0 or else Result = ENOMEM);
1040 if Result = ENOMEM then
1041 raise Storage_Error;
1044 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1045 pragma Assert (Result = 0 or else Result = ENOMEM);
1047 if Result = ENOMEM then
1048 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1049 pragma Assert (Result = 0);
1051 raise Storage_Error;
1054 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1055 pragma Assert (Result = 0);
1057 -- Initialize internal condition variable
1059 Result := pthread_condattr_init (Cond_Attr'Access);
1060 pragma Assert (Result = 0 or else Result = ENOMEM);
1063 Result := pthread_mutex_destroy (S.L'Access);
1064 pragma Assert (Result = 0);
1066 if Result = ENOMEM then
1067 raise Storage_Error;
1071 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1072 pragma Assert (Result = 0 or else Result = ENOMEM);
1075 Result := pthread_mutex_destroy (S.L'Access);
1076 pragma Assert (Result = 0);
1078 if Result = ENOMEM then
1079 Result := pthread_condattr_destroy (Cond_Attr'Access);
1080 pragma Assert (Result = 0);
1082 raise Storage_Error;
1086 Result := pthread_condattr_destroy (Cond_Attr'Access);
1087 pragma Assert (Result = 0);
1094 procedure Finalize (S : in out Suspension_Object) is
1095 Result : Interfaces.C.int;
1097 -- Destroy internal mutex
1099 Result := pthread_mutex_destroy (S.L'Access);
1100 pragma Assert (Result = 0);
1102 -- Destroy internal condition variable
1104 Result := pthread_cond_destroy (S.CV'Access);
1105 pragma Assert (Result = 0);
1112 function Current_State (S : Suspension_Object) return Boolean is
1114 -- We do not want to use lock on this read operation. State is marked
1115 -- as Atomic so that we ensure that the value retrieved is correct.
1124 procedure Set_False (S : in out Suspension_Object) is
1125 Result : Interfaces.C.int;
1127 Result := pthread_mutex_lock (S.L'Access);
1128 pragma Assert (Result = 0);
1132 Result := pthread_mutex_unlock (S.L'Access);
1133 pragma Assert (Result = 0);
1140 procedure Set_True (S : in out Suspension_Object) is
1141 Result : Interfaces.C.int;
1143 Result := pthread_mutex_lock (S.L'Access);
1144 pragma Assert (Result = 0);
1146 -- If there is already a task waiting on this suspension object then
1147 -- we resume it, leaving the state of the suspension object to False,
1148 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1149 -- the state to True.
1155 Result := pthread_cond_signal (S.CV'Access);
1156 pragma Assert (Result = 0);
1161 Result := pthread_mutex_unlock (S.L'Access);
1162 pragma Assert (Result = 0);
1165 ------------------------
1166 -- Suspend_Until_True --
1167 ------------------------
1169 procedure Suspend_Until_True (S : in out Suspension_Object) is
1170 Result : Interfaces.C.int;
1172 Result := pthread_mutex_lock (S.L'Access);
1173 pragma Assert (Result = 0);
1176 -- Program_Error must be raised upon calling Suspend_Until_True
1177 -- if another task is already waiting on that suspension object
1178 -- (ARM D.10 par. 10).
1180 Result := pthread_mutex_unlock (S.L'Access);
1181 pragma Assert (Result = 0);
1183 raise Program_Error;
1185 -- Suspend the task if the state is False. Otherwise, the task
1186 -- continues its execution, and the state of the suspension object
1187 -- is set to False (ARM D.10 par. 9).
1193 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1197 Result := pthread_mutex_unlock (S.L'Access);
1198 pragma Assert (Result = 0);
1199 end Suspend_Until_True;
1207 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1208 pragma Unreferenced (Self_ID);
1213 --------------------
1214 -- Check_No_Locks --
1215 --------------------
1217 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1218 pragma Unreferenced (Self_ID);
1223 ----------------------
1224 -- Environment_Task --
1225 ----------------------
1227 function Environment_Task return Task_Id is
1229 return Environment_Task_Id;
1230 end Environment_Task;
1236 procedure Lock_RTS is
1238 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1245 procedure Unlock_RTS is
1247 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1254 function Suspend_Task
1256 Thread_Self : Thread_Id) return Boolean
1258 pragma Unreferenced (T);
1259 pragma Unreferenced (Thread_Self);
1268 function Resume_Task
1270 Thread_Self : Thread_Id) return Boolean
1272 pragma Unreferenced (T);
1273 pragma Unreferenced (Thread_Self);
1282 procedure Initialize (Environment_Task : Task_Id) is
1283 act : aliased struct_sigaction;
1284 old_act : aliased struct_sigaction;
1285 Tmp_Set : aliased sigset_t;
1286 Result : Interfaces.C.int;
1289 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1290 pragma Import (C, State, "__gnat_get_interrupt_state");
1291 -- Get interrupt state. Defined in a-init.c
1292 -- The input argument is the interrupt number,
1293 -- and the result is one of the following:
1295 Default : constant Character := 's';
1296 -- 'n' this interrupt not set by any Interrupt_State pragma
1297 -- 'u' Interrupt_State pragma set state to User
1298 -- 'r' Interrupt_State pragma set state to Runtime
1299 -- 's' Interrupt_State pragma set state to System (use "default"
1303 Environment_Task_Id := Environment_Task;
1305 -- Initialize the lock used to synchronize chain of all ATCBs
1307 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1309 Specific.Initialize (Environment_Task);
1311 Enter_Task (Environment_Task);
1313 -- Install the abort-signal handler
1315 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1319 act.sa_handler := Abort_Handler'Address;
1321 Result := sigemptyset (Tmp_Set'Access);
1322 pragma Assert (Result = 0);
1323 act.sa_mask := Tmp_Set;
1327 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1328 act'Unchecked_Access,
1329 old_act'Unchecked_Access);
1331 pragma Assert (Result = 0);
1337 Result : Interfaces.C.int;
1339 -- Prepare the set of signals that should unblocked in all tasks
1341 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1342 pragma Assert (Result = 0);
1344 for J in Interrupt_Management.Interrupt_ID loop
1345 if System.Interrupt_Management.Keep_Unmasked (J) then
1346 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1347 pragma Assert (Result = 0);
1351 end System.Task_Primitives.Operations;