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-2006, 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 DEC Unix 4.0d version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System
.Tasking
.Debug
;
44 -- used for Known_Tasks
46 with System
.Interrupt_Management
;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
51 with System
.OS_Primitives
;
52 -- used for Delay_Modes
54 with System
.Task_Info
;
55 -- used for Task_Info_Type
58 -- used for Shift_Left
64 with Unchecked_Deallocation
;
66 package body System
.Task_Primitives
.Operations
is
68 use System
.Tasking
.Debug
;
71 use System
.OS_Interface
;
72 use System
.Parameters
;
73 use System
.OS_Primitives
;
79 -- The followings are logically constants, but need to be initialized
82 Single_RTS_Lock
: aliased RTS_Lock
;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 ATCB_Key
: aliased pthread_key_t
;
88 -- Key used to find the Ada Task_Id associated with a thread
90 Environment_Task_Id
: Task_Id
;
91 -- A variable to hold Task_Id for the environment task
93 Unblocked_Signal_Mask
: aliased sigset_t
;
94 -- The set of signals that should unblocked in all tasks
96 Time_Slice_Val
: Integer;
97 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
99 Locking_Policy
: Character;
100 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
102 Dispatching_Policy
: Character;
103 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
107 Foreign_Task_Elaborated
: aliased Boolean := True;
108 -- Used to identified fake tasks (i.e., non-Ada Threads)
116 procedure Initialize
(Environment_Task
: Task_Id
);
117 pragma Inline
(Initialize
);
118 -- Initialize various data needed by this package
120 function Is_Valid_Task
return Boolean;
121 pragma Inline
(Is_Valid_Task
);
122 -- Does executing thread have a TCB?
124 procedure Set
(Self_Id
: Task_Id
);
126 -- Set the self id for the current task
128 function Self
return Task_Id
;
129 pragma Inline
(Self
);
130 -- Return a pointer to the Ada Task Control Block of the calling task
134 package body Specific
is separate;
135 -- The body of this package is target specific
137 ---------------------------------
138 -- Support for foreign threads --
139 ---------------------------------
141 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
142 -- Allocate and initialize a new ATCB for the current Thread
144 function Register_Foreign_Thread
145 (Thread
: Thread_Id
) return Task_Id
is separate;
147 -----------------------
148 -- Local Subprograms --
149 -----------------------
151 procedure Abort_Handler
(Sig
: Signal
);
152 -- Signal handler used to implement asynchronous abort
158 procedure Abort_Handler
(Sig
: Signal
) is
159 pragma Unreferenced
(Sig
);
161 T
: constant Task_Id
:= Self
;
162 Result
: Interfaces
.C
.int
;
163 Old_Set
: aliased sigset_t
;
166 -- It is not safe to raise an exception when using ZCX and the GCC
167 -- exception handling mechanism.
169 if ZCX_By_Default
and then GCC_ZCX_Support
then
173 if T
.Deferral_Level
= 0
174 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
179 -- Make sure signals used for RTS internal purpose are unmasked
181 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
182 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
183 pragma Assert
(Result
= 0);
185 raise Standard
'Abort_Signal;
193 -- The underlying thread system sets a guard page at the
194 -- bottom of a thread stack, so nothing is needed.
196 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
197 pragma Unreferenced
(T
);
198 pragma Unreferenced
(On
);
207 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
209 return T
.Common
.LL
.Thread
;
216 function Self
return Task_Id
renames Specific
.Self
;
218 ---------------------
219 -- Initialize_Lock --
220 ---------------------
222 -- Note: mutexes and cond_variables needed per-task basis are
223 -- initialized in Initialize_TCB and the Storage_Error is
224 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
225 -- used in RTS is initialized before any status change of RTS.
226 -- Therefore rasing Storage_Error in the following routines
227 -- should be able to be handled safely.
229 procedure Initialize_Lock
230 (Prio
: System
.Any_Priority
;
233 Attributes
: aliased pthread_mutexattr_t
;
234 Result
: Interfaces
.C
.int
;
237 Result
:= pthread_mutexattr_init
(Attributes
'Access);
238 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
240 if Result
= ENOMEM
then
244 if Locking_Policy
= 'C' then
245 L
.Ceiling
:= Interfaces
.C
.int
(Prio
);
248 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
249 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
251 if Result
= ENOMEM
then
252 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
256 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
257 pragma Assert
(Result
= 0);
260 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
261 pragma Unreferenced
(Level
);
263 Attributes
: aliased pthread_mutexattr_t
;
264 Result
: Interfaces
.C
.int
;
267 Result
:= pthread_mutexattr_init
(Attributes
'Access);
268 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
270 if Result
= ENOMEM
then
274 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
275 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
277 if Result
= ENOMEM
then
278 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
282 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
283 pragma Assert
(Result
= 0);
290 procedure Finalize_Lock
(L
: access Lock
) is
291 Result
: Interfaces
.C
.int
;
293 Result
:= pthread_mutex_destroy
(L
.L
'Access);
294 pragma Assert
(Result
= 0);
297 procedure Finalize_Lock
(L
: access RTS_Lock
) is
298 Result
: Interfaces
.C
.int
;
300 Result
:= pthread_mutex_destroy
(L
);
301 pragma Assert
(Result
= 0);
308 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
309 Result
: Interfaces
.C
.int
;
311 All_Tasks_Link
: Task_Id
;
312 Current_Prio
: System
.Any_Priority
;
315 -- Perform ceiling checks only when this is the locking policy in use
317 if Locking_Policy
= 'C' then
319 All_Tasks_Link
:= Self_ID
.Common
.All_Tasks_Link
;
320 Current_Prio
:= Get_Priority
(Self_ID
);
322 -- If there is no other task, no need to check priorities
324 if All_Tasks_Link
/= Null_Task
325 and then L
.Ceiling
< Interfaces
.C
.int
(Current_Prio
)
327 Ceiling_Violation
:= True;
332 Result
:= pthread_mutex_lock
(L
.L
'Access);
333 pragma Assert
(Result
= 0);
335 Ceiling_Violation
:= False;
339 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
341 Result
: Interfaces
.C
.int
;
343 if not Single_Lock
or else Global_Lock
then
344 Result
:= pthread_mutex_lock
(L
);
345 pragma Assert
(Result
= 0);
349 procedure Write_Lock
(T
: Task_Id
) is
350 Result
: Interfaces
.C
.int
;
352 if not Single_Lock
then
353 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
354 pragma Assert
(Result
= 0);
362 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
364 Write_Lock
(L
, Ceiling_Violation
);
371 procedure Unlock
(L
: access Lock
) is
372 Result
: Interfaces
.C
.int
;
374 Result
:= pthread_mutex_unlock
(L
.L
'Access);
375 pragma Assert
(Result
= 0);
378 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
379 Result
: Interfaces
.C
.int
;
381 if not Single_Lock
or else Global_Lock
then
382 Result
:= pthread_mutex_unlock
(L
);
383 pragma Assert
(Result
= 0);
387 procedure Unlock
(T
: Task_Id
) is
388 Result
: Interfaces
.C
.int
;
390 if not Single_Lock
then
391 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
392 pragma Assert
(Result
= 0);
402 Reason
: System
.Tasking
.Task_States
)
404 pragma Unreferenced
(Reason
);
406 Result
: Interfaces
.C
.int
;
410 Result
:= pthread_cond_wait
411 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
413 Result
:= pthread_cond_wait
414 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
417 -- EINTR is not considered a failure
419 pragma Assert
(Result
= 0 or else Result
= EINTR
);
426 -- This is for use within the run-time system, so abort is
427 -- assumed to be already deferred, and the caller should be
428 -- holding its own ATCB lock.
430 procedure Timed_Sleep
433 Mode
: ST
.Delay_Modes
;
434 Reason
: System
.Tasking
.Task_States
;
435 Timedout
: out Boolean;
436 Yielded
: out Boolean)
438 pragma Unreferenced
(Reason
);
440 Check_Time
: constant Duration := Monotonic_Clock
;
442 Request
: aliased timespec
;
443 Result
: Interfaces
.C
.int
;
449 if Mode
= Relative
then
450 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
452 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
455 if Abs_Time
> Check_Time
then
456 Request
:= To_Timespec
(Abs_Time
);
459 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
460 or else Self_ID
.Pending_Priority_Change
;
463 Result
:= pthread_cond_timedwait
464 (Self_ID
.Common
.LL
.CV
'Access,
465 Single_RTS_Lock
'Access,
469 Result
:= pthread_cond_timedwait
470 (Self_ID
.Common
.LL
.CV
'Access,
471 Self_ID
.Common
.LL
.L
'Access,
475 exit when Abs_Time
<= Monotonic_Clock
;
477 if Result
= 0 or Result
= EINTR
then
479 -- Somebody may have called Wakeup for us
485 pragma Assert
(Result
= ETIMEDOUT
);
494 -- This is for use in implementing delay statements, so
495 -- we assume the caller is abort-deferred but is holding
498 procedure Timed_Delay
501 Mode
: ST
.Delay_Modes
)
503 Check_Time
: constant Duration := Monotonic_Clock
;
505 Request
: aliased timespec
;
506 Result
: Interfaces
.C
.int
;
513 Write_Lock
(Self_ID
);
515 if Mode
= Relative
then
516 Abs_Time
:= Time
+ Check_Time
;
518 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
521 if Abs_Time
> Check_Time
then
522 Request
:= To_Timespec
(Abs_Time
);
523 Self_ID
.Common
.State
:= Delay_Sleep
;
526 if Self_ID
.Pending_Priority_Change
then
527 Self_ID
.Pending_Priority_Change
:= False;
528 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
529 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
532 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
535 Result
:= pthread_cond_timedwait
536 (Self_ID
.Common
.LL
.CV
'Access,
537 Single_RTS_Lock
'Access,
540 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
541 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
544 exit when Abs_Time
<= Monotonic_Clock
;
546 pragma Assert
(Result
= 0 or else
547 Result
= ETIMEDOUT
or else
551 Self_ID
.Common
.State
:= Runnable
;
563 ---------------------
564 -- Monotonic_Clock --
565 ---------------------
567 function Monotonic_Clock
return Duration is
568 TS
: aliased timespec
;
569 Result
: Interfaces
.C
.int
;
571 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
572 pragma Assert
(Result
= 0);
573 return To_Duration
(TS
);
580 function RT_Resolution
return Duration is
582 -- Returned value must be an integral multiple of Duration'Small (1 ns)
583 -- The following is the best approximation of 1/1024. The clock on the
584 -- DEC Alpha ticks at 1024 Hz.
586 return 0.000_976_563
;
593 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
594 pragma Unreferenced
(Reason
);
595 Result
: Interfaces
.C
.int
;
597 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
598 pragma Assert
(Result
= 0);
605 procedure Yield
(Do_Yield
: Boolean := True) is
606 Result
: Interfaces
.C
.int
;
607 pragma Unreferenced
(Result
);
610 Result
:= sched_yield
;
618 procedure Set_Priority
620 Prio
: System
.Any_Priority
;
621 Loss_Of_Inheritance
: Boolean := False)
623 pragma Unreferenced
(Loss_Of_Inheritance
);
625 Result
: Interfaces
.C
.int
;
626 Param
: aliased struct_sched_param
;
629 T
.Common
.Current_Priority
:= Prio
;
630 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
632 if Time_Slice_Val
> 0 then
633 Result
:= pthread_setschedparam
634 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
636 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
637 Result
:= pthread_setschedparam
638 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
641 Result
:= pthread_setschedparam
642 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
645 pragma Assert
(Result
= 0);
652 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
654 return T
.Common
.Current_Priority
;
661 procedure Enter_Task
(Self_ID
: Task_Id
) is
664 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
665 Specific
.Set
(Self_ID
);
669 for J
in Known_Tasks
'Range loop
670 if Known_Tasks
(J
) = null then
671 Known_Tasks
(J
) := Self_ID
;
672 Self_ID
.Known_Tasks_Index
:= J
;
684 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
686 return new Ada_Task_Control_Block
(Entry_Num
);
693 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
695 -----------------------------
696 -- Register_Foreign_Thread --
697 -----------------------------
699 function Register_Foreign_Thread
return Task_Id
is
701 if Is_Valid_Task
then
704 return Register_Foreign_Thread
(pthread_self
);
706 end Register_Foreign_Thread
;
712 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
713 Mutex_Attr
: aliased pthread_mutexattr_t
;
714 Result
: Interfaces
.C
.int
;
715 Cond_Attr
: aliased pthread_condattr_t
;
718 if not Single_Lock
then
719 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
720 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
723 Result
:= pthread_mutex_init
724 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
725 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
733 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
734 pragma Assert
(Result
= 0);
737 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
738 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
741 Result
:= pthread_cond_init
742 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
743 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
749 if not Single_Lock
then
750 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
751 pragma Assert
(Result
= 0);
757 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
758 pragma Assert
(Result
= 0);
765 procedure Create_Task
767 Wrapper
: System
.Address
;
768 Stack_Size
: System
.Parameters
.Size_Type
;
769 Priority
: System
.Any_Priority
;
770 Succeeded
: out Boolean)
772 Attributes
: aliased pthread_attr_t
;
773 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
774 Result
: Interfaces
.C
.int
;
775 Param
: aliased System
.OS_Interface
.struct_sched_param
;
777 use System
.Task_Info
;
780 -- Account for the Yellow Zone (2 pages) and the guard page
781 -- right above. See Hide_Yellow_Zone for the rationale.
783 Adjusted_Stack_Size
:=
784 Interfaces
.C
.size_t
(Stack_Size
) + 3 * Get_Page_Size
;
786 Result
:= pthread_attr_init
(Attributes
'Access);
787 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
794 Result
:= pthread_attr_setdetachstate
795 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
796 pragma Assert
(Result
= 0);
798 Result
:= pthread_attr_setstacksize
799 (Attributes
'Access, Adjusted_Stack_Size
);
800 pragma Assert
(Result
= 0);
802 Param
.sched_priority
:=
803 Interfaces
.C
.int
(Underlying_Priorities
(Priority
));
804 Result
:= pthread_attr_setschedparam
805 (Attributes
'Access, Param
'Access);
806 pragma Assert
(Result
= 0);
808 if Time_Slice_Val
> 0 then
809 Result
:= pthread_attr_setschedpolicy
810 (Attributes
'Access, System
.OS_Interface
.SCHED_RR
);
812 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
813 Result
:= pthread_attr_setschedpolicy
814 (Attributes
'Access, System
.OS_Interface
.SCHED_FIFO
);
817 Result
:= pthread_attr_setschedpolicy
818 (Attributes
'Access, System
.OS_Interface
.SCHED_OTHER
);
821 pragma Assert
(Result
= 0);
823 -- Set the scheduling parameters explicitly, since this is the
824 -- only way to force the OS to take e.g. the sched policy and scope
825 -- attributes into account.
827 Result
:= pthread_attr_setinheritsched
828 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
829 pragma Assert
(Result
= 0);
831 T
.Common
.Current_Priority
:= Priority
;
833 if T
.Common
.Task_Info
/= null then
834 case T
.Common
.Task_Info
.Contention_Scope
is
835 when System
.Task_Info
.Process_Scope
=>
836 Result
:= pthread_attr_setscope
837 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
839 when System
.Task_Info
.System_Scope
=>
840 Result
:= pthread_attr_setscope
841 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
843 when System
.Task_Info
.Default_Scope
=>
847 pragma Assert
(Result
= 0);
850 -- Since the initial signal mask of a thread is inherited from the
851 -- creator, and the Environment task has all its signals masked, we
852 -- do not need to manipulate caller's signal mask at this point.
853 -- All tasks in RTS will have All_Tasks_Mask initially.
855 Result
:= pthread_create
856 (T
.Common
.LL
.Thread
'Access,
858 Thread_Body_Access
(Wrapper
),
860 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
862 Succeeded
:= Result
= 0;
864 Result
:= pthread_attr_destroy
(Attributes
'Access);
865 pragma Assert
(Result
= 0);
867 if T
.Common
.Task_Info
/= null then
868 -- ??? We're using a process-wide function to implement a task
869 -- specific characteristic.
871 if T
.Common
.Task_Info
.Bind_To_Cpu_Number
= 0 then
872 Result
:= bind_to_cpu
(Curpid
, 0);
873 elsif T
.Common
.Task_Info
.Bind_To_Cpu_Number
> 0 then
874 Result
:= bind_to_cpu
876 Interfaces
.C
.unsigned_long
(
877 Interfaces
.Shift_Left
878 (Interfaces
.Unsigned_64
'(1),
879 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
880 pragma Assert (Result = 0);
889 procedure Finalize_TCB (T : Task_Id) is
890 Result : Interfaces.C.int;
892 Is_Self : constant Boolean := T = Self;
894 procedure Free is new
895 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
898 if not Single_Lock then
899 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
900 pragma Assert (Result = 0);
903 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
904 pragma Assert (Result = 0);
906 if T.Known_Tasks_Index /= -1 then
907 Known_Tasks (T.Known_Tasks_Index) := null;
921 procedure Exit_Task is
930 procedure Abort_Task (T : Task_Id) is
931 Result : Interfaces.C.int;
933 Result := pthread_kill (T.Common.LL.Thread,
934 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
935 pragma Assert (Result = 0);
942 procedure Initialize (S : in out Suspension_Object) is
943 Mutex_Attr : aliased pthread_mutexattr_t;
944 Cond_Attr : aliased pthread_condattr_t;
945 Result : Interfaces.C.int;
947 -- Initialize internal state. It is always initialized to False (ARM
953 -- Initialize internal mutex
955 Result := pthread_mutexattr_init (Mutex_Attr'Access);
956 pragma Assert (Result = 0 or else Result = ENOMEM);
958 if Result = ENOMEM then
962 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
963 pragma Assert (Result = 0 or else Result = ENOMEM);
965 if Result = ENOMEM then
966 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
970 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
971 pragma Assert (Result = 0);
973 -- Initialize internal condition variable
975 Result := pthread_condattr_init (Cond_Attr'Access);
976 pragma Assert (Result = 0 or else Result = ENOMEM);
978 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
980 pragma Assert (Result = 0 or else Result = ENOMEM);
983 Result := pthread_mutex_destroy (S.L'Access);
984 pragma Assert (Result = 0);
986 if Result = ENOMEM then
996 procedure Finalize (S : in out Suspension_Object) is
997 Result : Interfaces.C.int;
999 -- Destroy internal mutex
1001 Result := pthread_mutex_destroy (S.L'Access);
1002 pragma Assert (Result = 0);
1004 -- Destroy internal condition variable
1006 Result := pthread_cond_destroy (S.CV'Access);
1007 pragma Assert (Result = 0);
1014 function Current_State (S : Suspension_Object) return Boolean is
1016 -- We do not want to use lock on this read operation. State is marked
1017 -- as Atomic so that we ensure that the value retrieved is correct.
1026 procedure Set_False (S : in out Suspension_Object) is
1027 Result : Interfaces.C.int;
1029 Result := pthread_mutex_lock (S.L'Access);
1030 pragma Assert (Result = 0);
1034 Result := pthread_mutex_unlock (S.L'Access);
1035 pragma Assert (Result = 0);
1042 procedure Set_True (S : in out Suspension_Object) is
1043 Result : Interfaces.C.int;
1045 Result := pthread_mutex_lock (S.L'Access);
1046 pragma Assert (Result = 0);
1048 -- If there is already a task waiting on this suspension object then
1049 -- we resume it, leaving the state of the suspension object to False,
1050 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1051 -- the state to True.
1057 Result := pthread_cond_signal (S.CV'Access);
1058 pragma Assert (Result = 0);
1063 Result := pthread_mutex_unlock (S.L'Access);
1064 pragma Assert (Result = 0);
1067 ------------------------
1068 -- Suspend_Until_True --
1069 ------------------------
1071 procedure Suspend_Until_True (S : in out Suspension_Object) is
1072 Result : Interfaces.C.int;
1074 Result := pthread_mutex_lock (S.L'Access);
1075 pragma Assert (Result = 0);
1078 -- Program_Error must be raised upon calling Suspend_Until_True
1079 -- if another task is already waiting on that suspension object
1080 -- (ARM D.10 par. 10).
1082 Result := pthread_mutex_unlock (S.L'Access);
1083 pragma Assert (Result = 0);
1085 raise Program_Error;
1087 -- Suspend the task if the state is False. Otherwise, the task
1088 -- continues its execution, and the state of the suspension object
1089 -- is set to False (ARM D.10 par. 9).
1095 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1099 Result := pthread_mutex_unlock (S.L'Access);
1100 pragma Assert (Result = 0);
1101 end Suspend_Until_True;
1109 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1110 pragma Unreferenced (Self_ID);
1115 --------------------
1116 -- Check_No_Locks --
1117 --------------------
1119 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1120 pragma Unreferenced (Self_ID);
1125 ----------------------
1126 -- Environment_Task --
1127 ----------------------
1129 function Environment_Task return Task_Id is
1131 return Environment_Task_Id;
1132 end Environment_Task;
1138 procedure Lock_RTS is
1140 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1147 procedure Unlock_RTS is
1149 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1156 function Suspend_Task
1158 Thread_Self : Thread_Id) return Boolean
1160 pragma Warnings (Off, T);
1161 pragma Warnings (Off, Thread_Self);
1170 function Resume_Task
1172 Thread_Self : Thread_Id) return Boolean
1174 pragma Warnings (Off, T);
1175 pragma Warnings (Off, Thread_Self);
1184 procedure Initialize (Environment_Task : Task_Id) is
1185 act : aliased struct_sigaction;
1186 old_act : aliased struct_sigaction;
1187 Tmp_Set : aliased sigset_t;
1188 Result : Interfaces.C.int;
1191 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1192 pragma Import (C, State, "__gnat_get_interrupt_state");
1193 -- Get interrupt state. Defined in a-init.c. The input argument is
1194 -- the interrupt number, and the result is one of the following:
1196 Default : constant Character := 's
';
1197 -- 'n
' this interrupt not set by any Interrupt_State pragma
1198 -- 'u
' Interrupt_State pragma set state to User
1199 -- 'r
' Interrupt_State pragma set state to Runtime
1200 -- 's
' Interrupt_State pragma set state to System (use "default"
1204 Environment_Task_Id := Environment_Task;
1206 Interrupt_Management.Initialize;
1208 -- Prepare the set of signals that should unblocked in all tasks
1210 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1211 pragma Assert (Result = 0);
1213 for J in Interrupt_Management.Interrupt_ID loop
1214 if System.Interrupt_Management.Keep_Unmasked (J) then
1215 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1216 pragma Assert (Result = 0);
1222 -- Initialize the lock used to synchronize chain of all ATCBs
1224 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1226 Specific.Initialize (Environment_Task);
1228 Enter_Task (Environment_Task);
1230 -- Install the abort-signal handler
1232 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1236 act.sa_handler := Abort_Handler'Address;
1238 Result := sigemptyset (Tmp_Set'Access);
1239 pragma Assert (Result = 0);
1240 act.sa_mask := Tmp_Set;
1244 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1245 act'Unchecked_Access,
1246 old_act'Unchecked_Access);
1247 pragma Assert (Result = 0);
1251 end System.Task_Primitives.Operations;