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 System
.Soft_Links
;
65 -- used for Abort_Defer/Undefer
67 -- We use System.Soft_Links instead of System.Tasking.Initialization
68 -- because the later is a higher level package that we shouldn't depend on.
69 -- For example when using the restricted run time, it is replaced by
70 -- System.Tasking.Restricted.Stages.
72 with Unchecked_Deallocation
;
74 package body System
.Task_Primitives
.Operations
is
76 package SSL
renames System
.Soft_Links
;
78 use System
.Tasking
.Debug
;
81 use System
.OS_Interface
;
82 use System
.Parameters
;
83 use System
.OS_Primitives
;
89 -- The followings are logically constants, but need to be initialized
92 Single_RTS_Lock
: aliased RTS_Lock
;
93 -- This is a lock to allow only one thread of control in the RTS at
94 -- a time; it is used to execute in mutual exclusion from all other tasks.
95 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
97 ATCB_Key
: aliased pthread_key_t
;
98 -- Key used to find the Ada Task_Id associated with a thread
100 Environment_Task_Id
: Task_Id
;
101 -- A variable to hold Task_Id for the environment task
103 Unblocked_Signal_Mask
: aliased sigset_t
;
104 -- The set of signals that should unblocked in all tasks
106 Time_Slice_Val
: Integer;
107 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
109 Locking_Policy
: Character;
110 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
112 Dispatching_Policy
: Character;
113 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
117 Foreign_Task_Elaborated
: aliased Boolean := True;
118 -- Used to identified fake tasks (i.e., non-Ada Threads)
126 procedure Initialize
(Environment_Task
: Task_Id
);
127 pragma Inline
(Initialize
);
128 -- Initialize various data needed by this package
130 function Is_Valid_Task
return Boolean;
131 pragma Inline
(Is_Valid_Task
);
132 -- Does executing thread have a TCB?
134 procedure Set
(Self_Id
: Task_Id
);
136 -- Set the self id for the current task
138 function Self
return Task_Id
;
139 pragma Inline
(Self
);
140 -- Return a pointer to the Ada Task Control Block of the calling task
144 package body Specific
is separate;
145 -- The body of this package is target specific
147 ---------------------------------
148 -- Support for foreign threads --
149 ---------------------------------
151 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
152 -- Allocate and initialize a new ATCB for the current Thread
154 function Register_Foreign_Thread
155 (Thread
: Thread_Id
) return Task_Id
is separate;
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 procedure Abort_Handler
(Sig
: Signal
);
162 -- Signal handler used to implement asynchronous abort
168 procedure Abort_Handler
(Sig
: Signal
) is
169 pragma Unreferenced
(Sig
);
171 T
: constant Task_Id
:= Self
;
172 Result
: Interfaces
.C
.int
;
173 Old_Set
: aliased sigset_t
;
176 -- It is not safe to raise an exception when using ZCX and the GCC
177 -- exception handling mechanism.
179 if ZCX_By_Default
and then GCC_ZCX_Support
then
183 if T
.Deferral_Level
= 0
184 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
189 -- Make sure signals used for RTS internal purpose are unmasked
191 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
192 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
193 pragma Assert
(Result
= 0);
195 raise Standard
'Abort_Signal;
203 -- The underlying thread system sets a guard page at the
204 -- bottom of a thread stack, so nothing is needed.
206 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
207 pragma Unreferenced
(T
);
208 pragma Unreferenced
(On
);
217 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
219 return T
.Common
.LL
.Thread
;
226 function Self
return Task_Id
renames Specific
.Self
;
228 ---------------------
229 -- Initialize_Lock --
230 ---------------------
232 -- Note: mutexes and cond_variables needed per-task basis are
233 -- initialized in Initialize_TCB and the Storage_Error is
234 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
235 -- used in RTS is initialized before any status change of RTS.
236 -- Therefore rasing Storage_Error in the following routines
237 -- should be able to be handled safely.
239 procedure Initialize_Lock
240 (Prio
: System
.Any_Priority
;
243 Attributes
: aliased pthread_mutexattr_t
;
244 Result
: Interfaces
.C
.int
;
247 Result
:= pthread_mutexattr_init
(Attributes
'Access);
248 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
250 if Result
= ENOMEM
then
254 if Locking_Policy
= 'C' then
255 L
.Ceiling
:= Interfaces
.C
.int
(Prio
);
258 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
259 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
261 if Result
= ENOMEM
then
262 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
266 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
267 pragma Assert
(Result
= 0);
270 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
271 pragma Unreferenced
(Level
);
273 Attributes
: aliased pthread_mutexattr_t
;
274 Result
: Interfaces
.C
.int
;
277 Result
:= pthread_mutexattr_init
(Attributes
'Access);
278 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
280 if Result
= ENOMEM
then
284 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
285 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
287 if Result
= ENOMEM
then
288 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
292 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
293 pragma Assert
(Result
= 0);
300 procedure Finalize_Lock
(L
: access Lock
) is
301 Result
: Interfaces
.C
.int
;
303 Result
:= pthread_mutex_destroy
(L
.L
'Access);
304 pragma Assert
(Result
= 0);
307 procedure Finalize_Lock
(L
: access RTS_Lock
) is
308 Result
: Interfaces
.C
.int
;
310 Result
:= pthread_mutex_destroy
(L
);
311 pragma Assert
(Result
= 0);
318 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
319 Result
: Interfaces
.C
.int
;
321 All_Tasks_Link
: Task_Id
;
322 Current_Prio
: System
.Any_Priority
;
325 -- Perform ceiling checks only when this is the locking policy in use
327 if Locking_Policy
= 'C' then
329 All_Tasks_Link
:= Self_ID
.Common
.All_Tasks_Link
;
330 Current_Prio
:= Get_Priority
(Self_ID
);
332 -- If there is no other task, no need to check priorities
334 if All_Tasks_Link
/= Null_Task
335 and then L
.Ceiling
< Interfaces
.C
.int
(Current_Prio
)
337 Ceiling_Violation
:= True;
342 Result
:= pthread_mutex_lock
(L
.L
'Access);
343 pragma Assert
(Result
= 0);
345 Ceiling_Violation
:= False;
349 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
351 Result
: Interfaces
.C
.int
;
353 if not Single_Lock
or else Global_Lock
then
354 Result
:= pthread_mutex_lock
(L
);
355 pragma Assert
(Result
= 0);
359 procedure Write_Lock
(T
: Task_Id
) is
360 Result
: Interfaces
.C
.int
;
362 if not Single_Lock
then
363 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
364 pragma Assert
(Result
= 0);
372 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
374 Write_Lock
(L
, Ceiling_Violation
);
381 procedure Unlock
(L
: access Lock
) is
382 Result
: Interfaces
.C
.int
;
384 Result
:= pthread_mutex_unlock
(L
.L
'Access);
385 pragma Assert
(Result
= 0);
388 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
389 Result
: Interfaces
.C
.int
;
391 if not Single_Lock
or else Global_Lock
then
392 Result
:= pthread_mutex_unlock
(L
);
393 pragma Assert
(Result
= 0);
397 procedure Unlock
(T
: Task_Id
) is
398 Result
: Interfaces
.C
.int
;
400 if not Single_Lock
then
401 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
402 pragma Assert
(Result
= 0);
412 Reason
: System
.Tasking
.Task_States
)
414 pragma Unreferenced
(Reason
);
416 Result
: Interfaces
.C
.int
;
420 Result
:= pthread_cond_wait
421 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
423 Result
:= pthread_cond_wait
424 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
427 -- EINTR is not considered a failure
429 pragma Assert
(Result
= 0 or else Result
= EINTR
);
436 -- This is for use within the run-time system, so abort is
437 -- assumed to be already deferred, and the caller should be
438 -- holding its own ATCB lock.
440 procedure Timed_Sleep
443 Mode
: ST
.Delay_Modes
;
444 Reason
: System
.Tasking
.Task_States
;
445 Timedout
: out Boolean;
446 Yielded
: out Boolean)
448 pragma Unreferenced
(Reason
);
450 Check_Time
: constant Duration := Monotonic_Clock
;
452 Request
: aliased timespec
;
453 Result
: Interfaces
.C
.int
;
459 if Mode
= Relative
then
460 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
462 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
465 if Abs_Time
> Check_Time
then
466 Request
:= To_Timespec
(Abs_Time
);
469 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
470 or else Self_ID
.Pending_Priority_Change
;
473 Result
:= pthread_cond_timedwait
474 (Self_ID
.Common
.LL
.CV
'Access,
475 Single_RTS_Lock
'Access,
479 Result
:= pthread_cond_timedwait
480 (Self_ID
.Common
.LL
.CV
'Access,
481 Self_ID
.Common
.LL
.L
'Access,
485 exit when Abs_Time
<= Monotonic_Clock
;
487 if Result
= 0 or Result
= EINTR
then
489 -- Somebody may have called Wakeup for us
495 pragma Assert
(Result
= ETIMEDOUT
);
504 -- This is for use in implementing delay statements, so
505 -- we assume the caller is abort-deferred but is holding
508 procedure Timed_Delay
511 Mode
: ST
.Delay_Modes
)
513 Check_Time
: constant Duration := Monotonic_Clock
;
515 Request
: aliased timespec
;
516 Result
: Interfaces
.C
.int
;
523 Write_Lock
(Self_ID
);
525 if Mode
= Relative
then
526 Abs_Time
:= Time
+ Check_Time
;
528 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
531 if Abs_Time
> Check_Time
then
532 Request
:= To_Timespec
(Abs_Time
);
533 Self_ID
.Common
.State
:= Delay_Sleep
;
536 if Self_ID
.Pending_Priority_Change
then
537 Self_ID
.Pending_Priority_Change
:= False;
538 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
539 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
542 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
545 Result
:= pthread_cond_timedwait
546 (Self_ID
.Common
.LL
.CV
'Access,
547 Single_RTS_Lock
'Access,
550 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
551 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
554 exit when Abs_Time
<= Monotonic_Clock
;
556 pragma Assert
(Result
= 0 or else
557 Result
= ETIMEDOUT
or else
561 Self_ID
.Common
.State
:= Runnable
;
573 ---------------------
574 -- Monotonic_Clock --
575 ---------------------
577 function Monotonic_Clock
return Duration is
578 TS
: aliased timespec
;
579 Result
: Interfaces
.C
.int
;
581 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
582 pragma Assert
(Result
= 0);
583 return To_Duration
(TS
);
590 function RT_Resolution
return Duration is
592 -- Returned value must be an integral multiple of Duration'Small (1 ns)
593 -- The following is the best approximation of 1/1024. The clock on the
594 -- DEC Alpha ticks at 1024 Hz.
596 return 0.000_976_563
;
603 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
604 pragma Unreferenced
(Reason
);
605 Result
: Interfaces
.C
.int
;
607 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
608 pragma Assert
(Result
= 0);
615 procedure Yield
(Do_Yield
: Boolean := True) is
616 Result
: Interfaces
.C
.int
;
617 pragma Unreferenced
(Result
);
620 Result
:= sched_yield
;
628 procedure Set_Priority
630 Prio
: System
.Any_Priority
;
631 Loss_Of_Inheritance
: Boolean := False)
633 pragma Unreferenced
(Loss_Of_Inheritance
);
635 Result
: Interfaces
.C
.int
;
636 Param
: aliased struct_sched_param
;
639 T
.Common
.Current_Priority
:= Prio
;
640 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
642 if Time_Slice_Val
> 0 then
643 Result
:= pthread_setschedparam
644 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
646 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
647 Result
:= pthread_setschedparam
648 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
651 Result
:= pthread_setschedparam
652 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
655 pragma Assert
(Result
= 0);
662 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
664 return T
.Common
.Current_Priority
;
671 procedure Enter_Task
(Self_ID
: Task_Id
) is
674 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
675 Specific
.Set
(Self_ID
);
679 for J
in Known_Tasks
'Range loop
680 if Known_Tasks
(J
) = null then
681 Known_Tasks
(J
) := Self_ID
;
682 Self_ID
.Known_Tasks_Index
:= J
;
694 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
696 return new Ada_Task_Control_Block
(Entry_Num
);
703 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
705 -----------------------------
706 -- Register_Foreign_Thread --
707 -----------------------------
709 function Register_Foreign_Thread
return Task_Id
is
711 if Is_Valid_Task
then
714 return Register_Foreign_Thread
(pthread_self
);
716 end Register_Foreign_Thread
;
722 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
723 Mutex_Attr
: aliased pthread_mutexattr_t
;
724 Result
: Interfaces
.C
.int
;
725 Cond_Attr
: aliased pthread_condattr_t
;
728 if not Single_Lock
then
729 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
730 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
733 Result
:= pthread_mutex_init
734 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
735 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
743 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
744 pragma Assert
(Result
= 0);
747 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
748 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
751 Result
:= pthread_cond_init
752 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
753 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
759 if not Single_Lock
then
760 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
761 pragma Assert
(Result
= 0);
767 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
768 pragma Assert
(Result
= 0);
775 procedure Create_Task
777 Wrapper
: System
.Address
;
778 Stack_Size
: System
.Parameters
.Size_Type
;
779 Priority
: System
.Any_Priority
;
780 Succeeded
: out Boolean)
782 Attributes
: aliased pthread_attr_t
;
783 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
784 Result
: Interfaces
.C
.int
;
785 Param
: aliased System
.OS_Interface
.struct_sched_param
;
787 use System
.Task_Info
;
790 -- Account for the Yellow Zone (2 pages) and the guard page
791 -- right above. See Hide_Yellow_Zone for the rationale.
793 Adjusted_Stack_Size
:=
794 Interfaces
.C
.size_t
(Stack_Size
) + 3 * Get_Page_Size
;
796 Result
:= pthread_attr_init
(Attributes
'Access);
797 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
804 Result
:= pthread_attr_setdetachstate
805 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
806 pragma Assert
(Result
= 0);
808 Result
:= pthread_attr_setstacksize
809 (Attributes
'Access, Adjusted_Stack_Size
);
810 pragma Assert
(Result
= 0);
812 Param
.sched_priority
:=
813 Interfaces
.C
.int
(Underlying_Priorities
(Priority
));
814 Result
:= pthread_attr_setschedparam
815 (Attributes
'Access, Param
'Access);
816 pragma Assert
(Result
= 0);
818 if Time_Slice_Val
> 0 then
819 Result
:= pthread_attr_setschedpolicy
820 (Attributes
'Access, System
.OS_Interface
.SCHED_RR
);
822 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
823 Result
:= pthread_attr_setschedpolicy
824 (Attributes
'Access, System
.OS_Interface
.SCHED_FIFO
);
827 Result
:= pthread_attr_setschedpolicy
828 (Attributes
'Access, System
.OS_Interface
.SCHED_OTHER
);
831 pragma Assert
(Result
= 0);
833 -- Set the scheduling parameters explicitly, since this is the
834 -- only way to force the OS to take e.g. the sched policy and scope
835 -- attributes into account.
837 Result
:= pthread_attr_setinheritsched
838 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
839 pragma Assert
(Result
= 0);
841 T
.Common
.Current_Priority
:= Priority
;
843 if T
.Common
.Task_Info
/= null then
844 case T
.Common
.Task_Info
.Contention_Scope
is
845 when System
.Task_Info
.Process_Scope
=>
846 Result
:= pthread_attr_setscope
847 (Attributes
'Access, PTHREAD_SCOPE_PROCESS
);
849 when System
.Task_Info
.System_Scope
=>
850 Result
:= pthread_attr_setscope
851 (Attributes
'Access, PTHREAD_SCOPE_SYSTEM
);
853 when System
.Task_Info
.Default_Scope
=>
857 pragma Assert
(Result
= 0);
860 -- Since the initial signal mask of a thread is inherited from the
861 -- creator, and the Environment task has all its signals masked, we
862 -- do not need to manipulate caller's signal mask at this point.
863 -- All tasks in RTS will have All_Tasks_Mask initially.
865 Result
:= pthread_create
866 (T
.Common
.LL
.Thread
'Access,
868 Thread_Body_Access
(Wrapper
),
870 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
872 Succeeded
:= Result
= 0;
874 Result
:= pthread_attr_destroy
(Attributes
'Access);
875 pragma Assert
(Result
= 0);
877 if T
.Common
.Task_Info
/= null then
878 -- ??? We're using a process-wide function to implement a task
879 -- specific characteristic.
881 if T
.Common
.Task_Info
.Bind_To_Cpu_Number
= 0 then
882 Result
:= bind_to_cpu
(Curpid
, 0);
883 elsif T
.Common
.Task_Info
.Bind_To_Cpu_Number
> 0 then
884 Result
:= bind_to_cpu
886 Interfaces
.C
.unsigned_long
(
887 Interfaces
.Shift_Left
888 (Interfaces
.Unsigned_64
'(1),
889 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
890 pragma Assert (Result = 0);
899 procedure Finalize_TCB (T : Task_Id) is
900 Result : Interfaces.C.int;
902 Is_Self : constant Boolean := T = Self;
904 procedure Free is new
905 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
908 if not Single_Lock then
909 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
910 pragma Assert (Result = 0);
913 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
914 pragma Assert (Result = 0);
916 if T.Known_Tasks_Index /= -1 then
917 Known_Tasks (T.Known_Tasks_Index) := null;
931 procedure Exit_Task is
940 procedure Abort_Task (T : Task_Id) is
941 Result : Interfaces.C.int;
943 Result := pthread_kill (T.Common.LL.Thread,
944 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
945 pragma Assert (Result = 0);
952 procedure Initialize (S : in out Suspension_Object) is
953 Mutex_Attr : aliased pthread_mutexattr_t;
954 Cond_Attr : aliased pthread_condattr_t;
955 Result : Interfaces.C.int;
957 -- Initialize internal state. It is always initialized to False (ARM
963 -- Initialize internal mutex
965 Result := pthread_mutexattr_init (Mutex_Attr'Access);
966 pragma Assert (Result = 0 or else Result = ENOMEM);
968 if Result = ENOMEM then
972 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
973 pragma Assert (Result = 0 or else Result = ENOMEM);
975 if Result = ENOMEM then
976 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
980 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
981 pragma Assert (Result = 0);
983 -- Initialize internal condition variable
985 Result := pthread_condattr_init (Cond_Attr'Access);
986 pragma Assert (Result = 0 or else Result = ENOMEM);
988 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
990 pragma Assert (Result = 0 or else Result = ENOMEM);
993 Result := pthread_mutex_destroy (S.L'Access);
994 pragma Assert (Result = 0);
996 if Result = ENOMEM then
1006 procedure Finalize (S : in out Suspension_Object) is
1007 Result : Interfaces.C.int;
1009 -- Destroy internal mutex
1011 Result := pthread_mutex_destroy (S.L'Access);
1012 pragma Assert (Result = 0);
1014 -- Destroy internal condition variable
1016 Result := pthread_cond_destroy (S.CV'Access);
1017 pragma Assert (Result = 0);
1024 function Current_State (S : Suspension_Object) return Boolean is
1026 -- We do not want to use lock on this read operation. State is marked
1027 -- as Atomic so that we ensure that the value retrieved is correct.
1036 procedure Set_False (S : in out Suspension_Object) is
1037 Result : Interfaces.C.int;
1039 SSL.Abort_Defer.all;
1041 Result := pthread_mutex_lock (S.L'Access);
1042 pragma Assert (Result = 0);
1046 Result := pthread_mutex_unlock (S.L'Access);
1047 pragma Assert (Result = 0);
1049 SSL.Abort_Undefer.all;
1056 procedure Set_True (S : in out Suspension_Object) is
1057 Result : Interfaces.C.int;
1059 SSL.Abort_Defer.all;
1061 Result := pthread_mutex_lock (S.L'Access);
1062 pragma Assert (Result = 0);
1064 -- If there is already a task waiting on this suspension object then
1065 -- we resume it, leaving the state of the suspension object to False,
1066 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1067 -- the state to True.
1073 Result := pthread_cond_signal (S.CV'Access);
1074 pragma Assert (Result = 0);
1079 Result := pthread_mutex_unlock (S.L'Access);
1080 pragma Assert (Result = 0);
1082 SSL.Abort_Undefer.all;
1085 ------------------------
1086 -- Suspend_Until_True --
1087 ------------------------
1089 procedure Suspend_Until_True (S : in out Suspension_Object) is
1090 Result : Interfaces.C.int;
1092 SSL.Abort_Defer.all;
1094 Result := pthread_mutex_lock (S.L'Access);
1095 pragma Assert (Result = 0);
1098 -- Program_Error must be raised upon calling Suspend_Until_True
1099 -- if another task is already waiting on that suspension object
1100 -- (ARM D.10 par. 10).
1102 Result := pthread_mutex_unlock (S.L'Access);
1103 pragma Assert (Result = 0);
1105 SSL.Abort_Undefer.all;
1107 raise Program_Error;
1109 -- Suspend the task if the state is False. Otherwise, the task
1110 -- continues its execution, and the state of the suspension object
1111 -- is set to False (ARM D.10 par. 9).
1117 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1120 Result := pthread_mutex_unlock (S.L'Access);
1121 pragma Assert (Result = 0);
1123 SSL.Abort_Undefer.all;
1125 end Suspend_Until_True;
1133 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1134 pragma Unreferenced (Self_ID);
1139 --------------------
1140 -- Check_No_Locks --
1141 --------------------
1143 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1144 pragma Unreferenced (Self_ID);
1149 ----------------------
1150 -- Environment_Task --
1151 ----------------------
1153 function Environment_Task return Task_Id is
1155 return Environment_Task_Id;
1156 end Environment_Task;
1162 procedure Lock_RTS is
1164 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1171 procedure Unlock_RTS is
1173 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1180 function Suspend_Task
1182 Thread_Self : Thread_Id) return Boolean
1184 pragma Warnings (Off, T);
1185 pragma Warnings (Off, Thread_Self);
1194 function Resume_Task
1196 Thread_Self : Thread_Id) return Boolean
1198 pragma Warnings (Off, T);
1199 pragma Warnings (Off, Thread_Self);
1208 procedure Initialize (Environment_Task : Task_Id) is
1209 act : aliased struct_sigaction;
1210 old_act : aliased struct_sigaction;
1211 Tmp_Set : aliased sigset_t;
1212 Result : Interfaces.C.int;
1215 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1216 pragma Import (C, State, "__gnat_get_interrupt_state");
1217 -- Get interrupt state. Defined in a-init.c. The input argument is
1218 -- the interrupt number, and the result is one of the following:
1220 Default : constant Character := 's
';
1221 -- 'n
' this interrupt not set by any Interrupt_State pragma
1222 -- 'u
' Interrupt_State pragma set state to User
1223 -- 'r
' Interrupt_State pragma set state to Runtime
1224 -- 's
' Interrupt_State pragma set state to System (use "default"
1228 Environment_Task_Id := Environment_Task;
1230 Interrupt_Management.Initialize;
1232 -- Prepare the set of signals that should unblocked in all tasks
1234 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1235 pragma Assert (Result = 0);
1237 for J in Interrupt_Management.Interrupt_ID loop
1238 if System.Interrupt_Management.Keep_Unmasked (J) then
1239 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1240 pragma Assert (Result = 0);
1246 -- Initialize the lock used to synchronize chain of all ATCBs
1248 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1250 Specific.Initialize (Environment_Task);
1252 Enter_Task (Environment_Task);
1254 -- Install the abort-signal handler
1256 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1260 act.sa_handler := Abort_Handler'Address;
1262 Result := sigemptyset (Tmp_Set'Access);
1263 pragma Assert (Result = 0);
1264 act.sa_mask := Tmp_Set;
1268 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1269 act'Unchecked_Access,
1270 old_act'Unchecked_Access);
1271 pragma Assert (Result = 0);
1275 end System.Task_Primitives.Operations;