pragma Polling (Off);
with System.Tasking.Debug;
with Ada.Exceptions;
with GNAT.OS_Lib;
with Interfaces.C;
with System.Interrupt_Management;
with System.Interrupt_Management.Operations;
pragma Elaborate_All (System.Interrupt_Management.Operations);
with System.Parameters;
with System.Tasking;
with System.Task_Info;
with System.Soft_Links;
with System.OS_Primitives;
with Unchecked_Conversion;
with Unchecked_Deallocation;
package body System.Task_Primitives.Operations is
use System.Tasking.Debug;
use System.Tasking;
use Interfaces.C;
use System.OS_Interface;
use System.Parameters;
use Ada.Exceptions;
use System.OS_Primitives;
package SSL renames System.Soft_Links;
Environment_Task_ID : Task_ID;
Unblocked_Signal_Mask : aliased sigset_t;
ATCB_Key : aliased thread_key_t;
Single_RTS_Lock : aliased RTS_Lock;
Next_Serial_Number : Task_Serial_Number := 100;
Priority_Ceiling_Emulation : constant Boolean := True;
Using_Real_Time_Class : Boolean := False;
Prio_Param : aliased struct_pcparms;
Time_Slice_Val : Interfaces.C.long;
pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
Locking_Policy : Character;
pragma Import (C, Locking_Policy, "__gl_locking_policy");
Dispatching_Policy : Character;
pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
type Fake_ATCB;
type Fake_ATCB_Ptr is access Fake_ATCB;
type Fake_ATCB is record
Stack_Base : Interfaces.C.unsigned := 0;
Next : Fake_ATCB_Ptr;
Real_ATCB : aliased Ada_Task_Control_Block (0);
end record;
Fake_ATCB_List : Fake_ATCB_Ptr;
Fake_Task_Elaborated : aliased Boolean := True;
Next_Fake_ATCB : Fake_ATCB_Ptr;
Check_Count : Integer := 0;
Old_Owner : Task_ID;
Lock_Count : Integer := 0;
Unlock_Count : Integer := 0;
function To_Lock_Ptr is
new Unchecked_Conversion (RTS_Lock_Ptr, Lock_Ptr);
function To_Task_ID is
new Unchecked_Conversion (Owner_ID, Task_ID);
function To_Owner_ID is
new Unchecked_Conversion (Task_ID, Owner_ID);
function sysconf (name : System.OS_Interface.int)
return processorid_t;
pragma Import (C, sysconf, "sysconf");
SC_NPROCESSORS_CONF : constant System.OS_Interface.int := 14;
function Num_Procs (name : System.OS_Interface.int := SC_NPROCESSORS_CONF)
return processorid_t renames sysconf;
procedure Abort_Handler
(Sig : Signal;
Code : access siginfo_t;
Context : access ucontext_t);
function To_thread_t is new Unchecked_Conversion
(Integer, System.OS_Interface.thread_t);
function To_Task_ID is new Unchecked_Conversion (System.Address, Task_ID);
function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
function Thread_Body_Access is
new Unchecked_Conversion (System.Address, Thread_Body);
function New_Fake_ATCB (Stack_Base : Interfaces.C.unsigned) return Task_ID;
pragma Warnings (Off, New_Fake_ATCB);
function Check_Initialize_Lock (L : Lock_Ptr; Level : Lock_Level)
return Boolean;
pragma Inline (Check_Initialize_Lock);
function Check_Lock (L : Lock_Ptr) return Boolean;
pragma Inline (Check_Lock);
function Record_Lock (L : Lock_Ptr) return Boolean;
pragma Inline (Record_Lock);
function Check_Sleep (Reason : Task_States) return Boolean;
pragma Inline (Check_Sleep);
function Record_Wakeup
(L : Lock_Ptr;
Reason : Task_States) return Boolean;
pragma Inline (Record_Wakeup);
function Check_Wakeup
(T : Task_ID;
Reason : Task_States) return Boolean;
pragma Inline (Check_Wakeup);
function Check_Unlock (L : Lock_Ptr) return Boolean;
pragma Inline (Check_Lock);
function Check_Finalize_Lock (L : Lock_Ptr) return Boolean;
pragma Inline (Check_Finalize_Lock);
function New_Fake_ATCB (Stack_Base : Interfaces.C.unsigned)
return Task_ID
is
Self_ID : Task_ID;
P, Q : Fake_ATCB_Ptr;
Succeeded : Boolean;
Result : Interfaces.C.int;
begin
Result := mutex_lock (Single_RTS_Lock.L'Access);
Q := null;
P := Fake_ATCB_List;
while P /= null loop
if P.Stack_Base = 0 then
Q := P;
elsif thr_kill (P.Real_ATCB.Common.LL.Thread, 0) /= 0 then
P.Stack_Base := 0;
Q := P;
end if;
P := P.Next;
end loop;
if Q = null then
Self_ID := Next_Fake_ATCB.Real_ATCB'Access;
Next_Fake_ATCB.Stack_Base := Stack_Base;
Next_Fake_ATCB.Next := Fake_ATCB_List;
Fake_ATCB_List := Next_Fake_ATCB;
Next_Fake_ATCB := null;
else
Self_ID := Q.Real_ATCB'Access;
Q.Stack_Base := Stack_Base;
end if;
System.Tasking.Initialize_ATCB
(Self_ID, null, Null_Address, Null_Task, Fake_Task_Elaborated'Access,
System.Priority'First, Task_Info.Unspecified_Task_Info, 0, Self_ID,
Succeeded);
pragma Assert (Succeeded);
Self_ID.Common.LL.Thread := thr_self;
Result := thr_setspecific (ATCB_Key, To_Address (Self_ID));
pragma Assert (Result = 0);
if Next_Fake_ATCB = null then
Next_Fake_ATCB := new Fake_ATCB;
end if;
Self_ID.Master_of_Task := 0;
Self_ID.Master_Within := Self_ID.Master_of_Task + 1;
for L in Self_ID.Entry_Calls'Range loop
Self_ID.Entry_Calls (L).Self := Self_ID;
Self_ID.Entry_Calls (L).Level := L;
end loop;
Self_ID.Common.State := Runnable;
Self_ID.Awake_Count := 1;
Self_ID.Deferral_Level := 0;
Self_ID.Serial_Number := Next_Serial_Number;
Next_Serial_Number := Next_Serial_Number + 1;
pragma Assert (Next_Serial_Number /= 0);
System.Soft_Links.Create_TSD (Self_ID.Common.Compiler_Data);
for J in Known_Tasks'Range loop
if Known_Tasks (J) = null then
Known_Tasks (J) := Self_ID;
Self_ID.Known_Tasks_Index := J;
exit;
end if;
end loop;
Result := mutex_unlock (Single_RTS_Lock.L'Access);
return Self_ID;
end New_Fake_ATCB;
procedure Abort_Handler
(Sig : Signal;
Code : access siginfo_t;
Context : access ucontext_t)
is
Self_ID : Task_ID := Self;
Result : Interfaces.C.int;
Old_Set : aliased sigset_t;
begin
if Self_ID.Deferral_Level = 0
and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
and then not Self_ID.Aborting
then
Self_ID.Aborting := True;
Result := thr_sigsetmask (SIG_UNBLOCK,
Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
pragma Assert (Result = 0);
raise Standard'Abort_Signal;
end if;
end Abort_Handler;
procedure Stack_Guard (T : ST.Task_ID; On : Boolean) is
begin
null;
end Stack_Guard;
function Get_Thread_Id (T : ST.Task_ID) return OSI.Thread_Id is
begin
return T.Common.LL.Thread;
end Get_Thread_Id;
function Self return Task_ID is separate;
procedure Initialize_Lock
(Prio : System.Any_Priority;
L : access Lock)
is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Initialize_Lock (Lock_Ptr (L), PO_Level));
if Priority_Ceiling_Emulation then
L.Ceiling := Prio;
end if;
Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
pragma Assert (Result = 0 or else Result = ENOMEM);
if Result = ENOMEM then
Raise_Exception (Storage_Error'Identity, "Failed to allocate a lock");
end if;
end Initialize_Lock;
procedure Initialize_Lock
(L : access RTS_Lock;
Level : Lock_Level)
is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Initialize_Lock
(To_Lock_Ptr (RTS_Lock_Ptr (L)), Level));
Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
pragma Assert (Result = 0 or else Result = ENOMEM);
if Result = ENOMEM then
Raise_Exception (Storage_Error'Identity, "Failed to allocate a lock");
end if;
end Initialize_Lock;
procedure Finalize_Lock (L : access Lock) is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Finalize_Lock (Lock_Ptr (L)));
Result := mutex_destroy (L.L'Access);
pragma Assert (Result = 0);
end Finalize_Lock;
procedure Finalize_Lock (L : access RTS_Lock) is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Finalize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
Result := mutex_destroy (L.L'Access);
pragma Assert (Result = 0);
end Finalize_Lock;
procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Lock (Lock_Ptr (L)));
if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
declare
Self_Id : constant Task_ID := Self;
Saved_Priority : System.Any_Priority;
begin
if Self_Id.Common.LL.Active_Priority > L.Ceiling then
Ceiling_Violation := True;
return;
end if;
Saved_Priority := Self_Id.Common.LL.Active_Priority;
if Self_Id.Common.LL.Active_Priority < L.Ceiling then
Set_Priority (Self_Id, L.Ceiling);
end if;
Result := mutex_lock (L.L'Access);
pragma Assert (Result = 0);
Ceiling_Violation := False;
L.Saved_Priority := Saved_Priority;
end;
else
Result := mutex_lock (L.L'Access);
pragma Assert (Result = 0);
Ceiling_Violation := False;
end if;
pragma Assert (Record_Lock (Lock_Ptr (L)));
end Write_Lock;
procedure Write_Lock
(L : access RTS_Lock; Global_Lock : Boolean := False)
is
Result : Interfaces.C.int;
begin
if not Single_Lock or else Global_Lock then
pragma Assert (Check_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
Result := mutex_lock (L.L'Access);
pragma Assert (Result = 0);
pragma Assert (Record_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
end if;
end Write_Lock;
procedure Write_Lock (T : Task_ID) is
Result : Interfaces.C.int;
begin
if not Single_Lock then
pragma Assert (Check_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
Result := mutex_lock (T.Common.LL.L.L'Access);
pragma Assert (Result = 0);
pragma Assert (Record_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
end if;
end Write_Lock;
procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
begin
Write_Lock (L, Ceiling_Violation);
end Read_Lock;
procedure Unlock (L : access Lock) is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Unlock (Lock_Ptr (L)));
if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
declare
Self_Id : constant Task_ID := Self;
begin
Result := mutex_unlock (L.L'Access);
pragma Assert (Result = 0);
if Self_Id.Common.LL.Active_Priority > L.Saved_Priority then
Set_Priority (Self_Id, L.Saved_Priority);
end if;
end;
else
Result := mutex_unlock (L.L'Access);
pragma Assert (Result = 0);
end if;
end Unlock;
procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
Result : Interfaces.C.int;
begin
if not Single_Lock or else Global_Lock then
pragma Assert (Check_Unlock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
Result := mutex_unlock (L.L'Access);
pragma Assert (Result = 0);
end if;
end Unlock;
procedure Unlock (T : Task_ID) is
Result : Interfaces.C.int;
begin
if not Single_Lock then
pragma Assert (Check_Unlock (To_Lock_Ptr (T.Common.LL.L'Access)));
Result := mutex_unlock (T.Common.LL.L.L'Access);
pragma Assert (Result = 0);
end if;
end Unlock;
function Monotonic_Clock return Duration is
TS : aliased timespec;
Result : Interfaces.C.int;
begin
Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
pragma Assert (Result = 0);
return To_Duration (TS);
end Monotonic_Clock;
function RT_Resolution return Duration is
begin
return 10#1.0#E-6;
end RT_Resolution;
procedure Yield (Do_Yield : Boolean := True) is
begin
if Do_Yield then
System.OS_Interface.thr_yield;
end if;
end Yield;
procedure Set_Priority
(T : Task_ID;
Prio : System.Any_Priority;
Loss_Of_Inheritance : Boolean := False)
is
Result : Interfaces.C.int;
Param : aliased struct_pcparms;
use Task_Info;
begin
T.Common.Current_Priority := Prio;
if Priority_Ceiling_Emulation then
T.Common.LL.Active_Priority := Prio;
end if;
if Using_Real_Time_Class then
Param.pc_cid := Prio_Param.pc_cid;
Param.rt_pri := pri_t (Prio);
Param.rt_tqsecs := Prio_Param.rt_tqsecs;
Param.rt_tqnsecs := Prio_Param.rt_tqnsecs;
Result := Interfaces.C.int (
priocntl (PC_VERSION, P_LWPID, T.Common.LL.LWP, PC_SETPARMS,
Param'Address));
else
if T.Common.Task_Info /= null
and then not T.Common.Task_Info.Bound_To_LWP
then
Result :=
thr_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
else
null;
end if;
end if;
end Set_Priority;
function Get_Priority (T : Task_ID) return System.Any_Priority is
begin
return T.Common.Current_Priority;
end Get_Priority;
procedure Enter_Task (Self_ID : Task_ID) is
Result : Interfaces.C.int;
Proc : processorid_t; Last_Proc : processorid_t;
use System.Task_Info;
begin
Self_ID.Common.LL.Thread := thr_self;
Self_ID.Common.LL.LWP := lwp_self;
if Self_ID.Common.Task_Info /= null then
if Self_ID.Common.Task_Info.New_LWP
and then Self_ID.Common.Task_Info.CPU /= CPU_UNCHANGED
then
Last_Proc := Num_Procs - 1;
if Self_ID.Common.Task_Info.CPU = ANY_CPU then
Result := 0;
Proc := 0;
while Proc < Last_Proc loop
Result := p_online (Proc, PR_STATUS);
exit when Result = PR_ONLINE;
Proc := Proc + 1;
end loop;
Result := processor_bind (P_LWPID, P_MYID, Proc, null);
pragma Assert (Result = 0);
else
if Self_ID.Common.Task_Info.CPU < 0
or else Self_ID.Common.Task_Info.CPU > Last_Proc
then
raise Invalid_CPU_Number;
end if;
Result := processor_bind
(P_LWPID, P_MYID, Self_ID.Common.Task_Info.CPU, null);
pragma Assert (Result = 0);
end if;
end if;
end if;
Result := thr_setspecific (ATCB_Key, To_Address (Self_ID));
pragma Assert (Result = 0);
Lock_RTS;
for J in Known_Tasks'Range loop
if Known_Tasks (J) = null then
Known_Tasks (J) := Self_ID;
Self_ID.Known_Tasks_Index := J;
exit;
end if;
end loop;
Unlock_RTS;
end Enter_Task;
function New_ATCB (Entry_Num : Task_Entry_Index) return Task_ID is
begin
return new Ada_Task_Control_Block (Entry_Num);
end New_ATCB;
procedure Initialize_TCB (Self_ID : Task_ID; Succeeded : out Boolean) is
Result : Interfaces.C.int := 0;
begin
Self_ID.Serial_Number := Next_Serial_Number;
Next_Serial_Number := Next_Serial_Number + 1;
pragma Assert (Next_Serial_Number /= 0);
Self_ID.Common.LL.Thread := To_thread_t (-1);
if not Single_Lock then
Result := mutex_init
(Self_ID.Common.LL.L.L'Access, USYNC_THREAD, System.Null_Address);
Self_ID.Common.LL.L.Level :=
Private_Task_Serial_Number (Self_ID.Serial_Number);
pragma Assert (Result = 0 or else Result = ENOMEM);
end if;
if Result = 0 then
Result := cond_init (Self_ID.Common.LL.CV'Access, USYNC_THREAD, 0);
pragma Assert (Result = 0 or else Result = ENOMEM);
end if;
if Result = 0 then
Succeeded := True;
else
if not Single_Lock then
Result := mutex_destroy (Self_ID.Common.LL.L.L'Access);
pragma Assert (Result = 0);
end if;
Succeeded := False;
end if;
end Initialize_TCB;
procedure Create_Task
(T : Task_ID;
Wrapper : System.Address;
Stack_Size : System.Parameters.Size_Type;
Priority : System.Any_Priority;
Succeeded : out Boolean)
is
Result : Interfaces.C.int;
Adjusted_Stack_Size : Interfaces.C.size_t;
Opts : Interfaces.C.int := THR_DETACHED;
Page_Size : constant System.Parameters.Size_Type := 4096;
use System.Task_Info;
begin
if Stack_Size = System.Parameters.Unspecified_Size then
Adjusted_Stack_Size :=
Interfaces.C.size_t (Default_Stack_Size + Page_Size);
elsif Stack_Size < Minimum_Stack_Size then
Adjusted_Stack_Size :=
Interfaces.C.size_t (Minimum_Stack_Size + Page_Size);
else
Adjusted_Stack_Size :=
Interfaces.C.size_t (Stack_Size + Page_Size);
end if;
if T.Common.Task_Info /= null then
if T.Common.Task_Info.New_LWP then
Opts := Opts + THR_NEW_LWP;
end if;
if T.Common.Task_Info.Bound_To_LWP then
Opts := Opts + THR_BOUND;
end if;
else
Opts := THR_DETACHED + THR_BOUND;
end if;
Result := thr_create
(System.Null_Address,
Adjusted_Stack_Size,
Thread_Body_Access (Wrapper),
To_Address (T),
Opts,
T.Common.LL.Thread'Access);
Succeeded := Result = 0;
pragma Assert
(Result = 0
or else Result = ENOMEM
or else Result = EAGAIN);
end Create_Task;
procedure Finalize_TCB (T : Task_ID) is
Result : Interfaces.C.int;
Tmp : Task_ID := T;
procedure Free is new
Unchecked_Deallocation (Ada_Task_Control_Block, Task_ID);
begin
T.Common.LL.Thread := To_thread_t (0);
if not Single_Lock then
Result := mutex_destroy (T.Common.LL.L.L'Access);
pragma Assert (Result = 0);
end if;
Result := cond_destroy (T.Common.LL.CV'Access);
pragma Assert (Result = 0);
if T.Known_Tasks_Index /= -1 then
Known_Tasks (T.Known_Tasks_Index) := null;
end if;
Free (Tmp);
end Finalize_TCB;
procedure Exit_Task is
begin
thr_exit (System.Null_Address);
end Exit_Task;
procedure Abort_Task (T : Task_ID) is
Result : Interfaces.C.int;
begin
pragma Assert (T /= Self);
Result := thr_kill (T.Common.LL.Thread,
Signal (System.Interrupt_Management.Abort_Task_Interrupt));
null;
pragma Assert (Result = 0);
end Abort_Task;
procedure Sleep
(Self_ID : Task_ID;
Reason : Task_States)
is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Sleep (Reason));
if Dynamic_Priority_Support
and then Self_ID.Pending_Priority_Change
then
Self_ID.Pending_Priority_Change := False;
Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
end if;
if Single_Lock then
Result := cond_wait
(Self_ID.Common.LL.CV'Access, Single_RTS_Lock.L'Access);
else
Result := cond_wait
(Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L.L'Access);
end if;
pragma Assert (Record_Wakeup
(To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
pragma Assert (Result = 0 or else Result = EINTR);
end Sleep;
procedure Timed_Sleep
(Self_ID : Task_ID;
Time : Duration;
Mode : ST.Delay_Modes;
Reason : System.Tasking.Task_States;
Timedout : out Boolean;
Yielded : out Boolean)
is
Check_Time : constant Duration := Monotonic_Clock;
Abs_Time : Duration;
Request : aliased timespec;
Result : Interfaces.C.int;
begin
pragma Assert (Check_Sleep (Reason));
Timedout := True;
Yielded := False;
if Mode = Relative then
Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
else
Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
end if;
if Abs_Time > Check_Time then
Request := To_Timespec (Abs_Time);
loop
exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
or else (Dynamic_Priority_Support and then
Self_ID.Pending_Priority_Change);
if Single_Lock then
Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
Single_RTS_Lock.L'Access, Request'Access);
else
Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
Self_ID.Common.LL.L.L'Access, Request'Access);
end if;
Yielded := True;
exit when Abs_Time <= Monotonic_Clock;
if Result = 0 or Result = EINTR then
Timedout := False;
exit;
end if;
pragma Assert (Result = ETIME);
end loop;
end if;
pragma Assert (Record_Wakeup
(To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
end Timed_Sleep;
procedure Timed_Delay
(Self_ID : Task_ID;
Time : Duration;
Mode : ST.Delay_Modes)
is
Check_Time : constant Duration := Monotonic_Clock;
Abs_Time : Duration;
Request : aliased timespec;
Result : Interfaces.C.int;
Yielded : Boolean := False;
begin
SSL.Abort_Defer.all;
if Single_Lock then
Lock_RTS;
end if;
Write_Lock (Self_ID);
if Mode = Relative then
Abs_Time := Time + Check_Time;
else
Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
end if;
if Abs_Time > Check_Time then
Request := To_Timespec (Abs_Time);
Self_ID.Common.State := Delay_Sleep;
pragma Assert (Check_Sleep (Delay_Sleep));
loop
if Dynamic_Priority_Support and then
Self_ID.Pending_Priority_Change then
Self_ID.Pending_Priority_Change := False;
Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
end if;
exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
if Single_Lock then
Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
Single_RTS_Lock.L'Access, Request'Access);
else
Result := cond_timedwait (Self_ID.Common.LL.CV'Access,
Self_ID.Common.LL.L.L'Access, Request'Access);
end if;
Yielded := True;
exit when Abs_Time <= Monotonic_Clock;
pragma Assert (Result = 0 or else
Result = ETIME or else
Result = EINTR);
end loop;
pragma Assert (Record_Wakeup
(To_Lock_Ptr (Self_ID.Common.LL.L'Access), Delay_Sleep));
Self_ID.Common.State := Runnable;
end if;
Unlock (Self_ID);
if Single_Lock then
Unlock_RTS;
end if;
if not Yielded then
thr_yield;
end if;
SSL.Abort_Undefer.all;
end Timed_Delay;
procedure Wakeup
(T : Task_ID;
Reason : Task_States)
is
Result : Interfaces.C.int;
begin
pragma Assert (Check_Wakeup (T, Reason));
Result := cond_signal (T.Common.LL.CV'Access);
pragma Assert (Result = 0);
end Wakeup;
function Check_Initialize_Lock
(L : Lock_Ptr;
Level : Lock_Level)
return Boolean
is
Self_ID : constant Task_ID := Self;
begin
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
if L.Level /= 0 then
return False;
end if;
L.Level := Lock_Level'Pos (Level) + 1;
return True;
end Check_Initialize_Lock;
function Check_Lock (L : Lock_Ptr) return Boolean is
Self_ID : Task_ID := Self;
P : Lock_Ptr;
begin
if L = null then
return False;
end if;
if L.Frozen then
return False;
end if;
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
if L.Owner = To_Owner_ID (Self_ID) then
return False;
end if;
if Single_Lock then
return True;
end if;
P := Self_ID.Common.LL.Locks;
if P /= null then
if P.Level >= L.Level
and then (P.Level > 2 or else L.Level > 2)
then
return False;
end if;
end if;
return True;
end Check_Lock;
function Record_Lock (L : Lock_Ptr) return Boolean is
Self_ID : Task_ID := Self;
P : Lock_Ptr;
begin
Lock_Count := Lock_Count + 1;
if L.Owner /= null then
return False;
end if;
L.Owner := To_Owner_ID (Self_ID);
if Single_Lock then
return True;
end if;
P := Self_ID.Common.LL.Locks;
if P /= null then
L.Next := P;
end if;
Self_ID.Common.LL.Locking := null;
Self_ID.Common.LL.Locks := L;
return True;
end Record_Lock;
function Check_Sleep (Reason : Task_States) return Boolean is
Self_ID : Task_ID := Self;
P : Lock_Ptr;
begin
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
if Single_Lock then
return True;
end if;
if Self_ID.Common.LL.Locks /=
To_Lock_Ptr (Self_ID.Common.LL.L'Access)
then
return False;
end if;
if Self_ID.Common.LL.Locks.Next /= null then
return False;
end if;
Self_ID.Common.LL.L.Owner := null;
P := Self_ID.Common.LL.Locks;
Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
P.Next := null;
return True;
end Check_Sleep;
function Record_Wakeup
(L : Lock_Ptr;
Reason : Task_States)
return Boolean
is
Self_ID : Task_ID := Self;
P : Lock_Ptr;
begin
L.Owner := To_Owner_ID (Self_ID);
if Single_Lock then
return True;
end if;
P := Self_ID.Common.LL.Locks;
if P /= null then
L.Next := P;
end if;
Self_ID.Common.LL.Locking := null;
Self_ID.Common.LL.Locks := L;
return True;
end Record_Wakeup;
function Check_Wakeup
(T : Task_ID;
Reason : Task_States)
return Boolean
is
Self_ID : Task_ID := Self;
begin
if T.Common.LL.L.Owner /= To_Owner_ID (Self_ID) then
return False;
end if;
if T.Common.State /= Reason then
return False;
end if;
return True;
end Check_Wakeup;
function Check_Unlock (L : Lock_Ptr) return Boolean is
Self_ID : Task_ID := Self;
P : Lock_Ptr;
begin
Unlock_Count := Unlock_Count + 1;
if L = null then
return False;
end if;
if L.Buddy /= null then
return False;
end if;
if L.Level = 4 then
Check_Count := Unlock_Count;
end if;
if Unlock_Count - Check_Count > 1000 then
Check_Count := Unlock_Count;
Old_Owner := To_Task_ID (Single_RTS_Lock.Owner);
end if;
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
if Self_ID.Common.LL.Locks /= L then
return False;
end if;
L.Owner := null;
P := Self_ID.Common.LL.Locks;
Self_ID.Common.LL.Locks := Self_ID.Common.LL.Locks.Next;
P.Next := null;
return True;
end Check_Unlock;
function Check_Finalize_Lock (L : Lock_Ptr) return Boolean is
Self_ID : Task_ID := Self;
begin
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
if L.Owner /= null then
return False;
end if;
L.Frozen := True;
return True;
end Check_Finalize_Lock;
function Check_Exit (Self_ID : Task_ID) return Boolean is
begin
if Self_ID.Common.LL.Locks = null then
return False;
end if;
if Self_ID.Common.LL.Locks.Level /= 2 then
return False;
end if;
if Self_ID.Common.LL.Locks.Next /= null then
return False;
end if;
if Self_ID.Deferral_Level <= 0 then
return False;
end if;
return True;
end Check_Exit;
function Check_No_Locks (Self_ID : Task_ID) return Boolean is
begin
return Self_ID.Common.LL.Locks = null;
end Check_No_Locks;
function Environment_Task return Task_ID is
begin
return Environment_Task_ID;
end Environment_Task;
procedure Lock_RTS is
begin
Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
end Lock_RTS;
procedure Unlock_RTS is
begin
Unlock (Single_RTS_Lock'Access, Global_Lock => True);
end Unlock_RTS;
function Suspend_Task
(T : ST.Task_ID;
Thread_Self : Thread_Id) return Boolean is
begin
if T.Common.LL.Thread /= Thread_Self then
return thr_suspend (T.Common.LL.Thread) = 0;
else
return True;
end if;
end Suspend_Task;
function Resume_Task
(T : ST.Task_ID;
Thread_Self : Thread_Id) return Boolean is
begin
if T.Common.LL.Thread /= Thread_Self then
return thr_continue (T.Common.LL.Thread) = 0;
else
return True;
end if;
end Resume_Task;
procedure Initialize (Environment_Task : ST.Task_ID) is
act : aliased struct_sigaction;
old_act : aliased struct_sigaction;
Tmp_Set : aliased sigset_t;
Result : Interfaces.C.int;
procedure Configure_Processors;
procedure Configure_Processors is
Proc_Acc : constant GNAT.OS_Lib.String_Access :=
GNAT.OS_Lib.Getenv ("GNAT_PROCESSOR");
Proc : aliased processorid_t; Last_Proc : processorid_t;
begin
if Proc_Acc.all'Length /= 0 then
Last_Proc := Num_Procs - 1;
if Last_Proc /= -1 then
Proc := processorid_t'Value (Proc_Acc.all);
if Proc <= -2 or else Proc > Last_Proc then
null;
elsif Proc = -1 then
Result := 0;
while Proc < Last_Proc loop
Proc := Proc + 1;
Result := p_online (Proc, PR_STATUS);
exit when Result = PR_ONLINE;
end loop;
pragma Assert (Result = PR_ONLINE);
Result := processor_bind (P_PID, P_MYID, Proc, null);
pragma Assert (Result = 0);
else
Result := processor_bind (P_PID, P_MYID, Proc, null);
pragma Assert (Result = 0);
end if;
end if;
end if;
exception
when Constraint_Error =>
null;
end Configure_Processors;
begin
Environment_Task_ID := Environment_Task;
Result := thr_setspecific (ATCB_Key, To_Address (Environment_Task));
pragma Assert (Result = 0);
Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
Enter_Task (Environment_Task);
act.sa_flags := 16;
act.sa_handler := Abort_Handler'Address;
Result := sigemptyset (Tmp_Set'Access);
pragma Assert (Result = 0);
act.sa_mask := Tmp_Set;
Result :=
sigaction (
Signal (System.Interrupt_Management.Abort_Task_Interrupt),
act'Unchecked_Access,
old_act'Unchecked_Access);
pragma Assert (Result = 0);
Configure_Processors;
Next_Fake_ATCB := new Fake_ATCB;
end Initialize;
begin
declare
Result : Interfaces.C.int;
begin
System.Interrupt_Management.Operations.Set_Interrupt_Mask
(System.Interrupt_Management.Operations.All_Tasks_Mask'Access);
Result := sigemptyset (Unblocked_Signal_Mask'Access);
pragma Assert (Result = 0);
for J in Interrupt_Management.Interrupt_ID loop
if System.Interrupt_Management.Keep_Unmasked (J) then
Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
pragma Assert (Result = 0);
end if;
end loop;
Result := thr_keycreate (ATCB_Key'Access, System.Null_Address);
pragma Assert (Result = 0);
end;
if Dispatching_Policy = 'F' then
declare
Result : Interfaces.C.long;
Class_Info : aliased struct_pcinfo;
Secs, Nsecs : Interfaces.C.long;
begin
if Time_Slice_Val > 0 then
Secs := Time_Slice_Val / 1_000_000;
Nsecs := (Time_Slice_Val rem 1_000_000) * 1_000;
else
Secs := RT_TQINF;
Nsecs := RT_TQINF;
end if;
Class_Info.pc_clname (1) := 'R';
Class_Info.pc_clname (2) := 'T';
Class_Info.pc_clname (3) := ASCII.NUL;
Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_GETCID,
Class_Info'Address);
Prio_Param.pc_cid := Class_Info.pc_cid;
Prio_Param.rt_pri := pri_t (Class_Info.rt_maxpri);
Prio_Param.rt_tqsecs := Secs;
Prio_Param.rt_tqnsecs := Nsecs;
Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_SETPARMS,
Prio_Param'Address);
Using_Real_Time_Class := Result /= -1;
end;
end if;
end System.Task_Primitives.Operations;