1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2009, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
41 with Ada
.Unchecked_Conversion
;
42 with Ada
.Unchecked_Deallocation
;
46 with System
.Task_Info
;
47 with System
.Tasking
.Debug
;
48 with System
.Interrupt_Management
;
49 with System
.OS_Primitives
;
50 with System
.Stack_Checking
.Operations
;
52 with System
.Soft_Links
;
53 -- We use System.Soft_Links instead of System.Tasking.Initialization
54 -- because the later is a higher level package that we shouldn't depend on.
55 -- For example when using the restricted run time, it is replaced by
56 -- System.Tasking.Restricted.Stages.
58 package body System
.Task_Primitives
.Operations
is
60 package SSL
renames System
.Soft_Links
;
61 package SC
renames System
.Stack_Checking
.Operations
;
63 use System
.Tasking
.Debug
;
66 use System
.OS_Interface
;
67 use System
.Parameters
;
68 use System
.OS_Primitives
;
71 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
72 -- Whether to use an alternate signal stack for stack overflows
78 -- The followings are logically constants, but need to be initialized
81 Single_RTS_Lock
: aliased RTS_Lock
;
82 -- This is a lock to allow only one thread of control in the RTS at
83 -- a time; it is used to execute in mutual exclusion from all other tasks.
84 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
86 ATCB_Key
: aliased pthread_key_t
;
87 -- Key used to find the Ada Task_Id associated with a thread
89 Environment_Task_Id
: Task_Id
;
90 -- A variable to hold Task_Id for the environment task
92 Unblocked_Signal_Mask
: aliased sigset_t
;
93 -- The set of signals that should be unblocked in all tasks
95 -- The followings are internal configuration constants needed
97 Next_Serial_Number
: Task_Serial_Number
:= 100;
98 -- We start at 100 (reserve some special values for using in error checks)
100 Time_Slice_Val
: Integer;
101 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
103 Dispatching_Policy
: Character;
104 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
106 -- The following are effectively constants, but they need to be initialized
107 -- by calling a pthread_ function.
109 Mutex_Attr
: aliased pthread_mutexattr_t
;
110 Cond_Attr
: aliased pthread_condattr_t
;
112 Foreign_Task_Elaborated
: aliased Boolean := True;
113 -- Used to identified fake tasks (i.e., non-Ada Threads)
121 procedure Initialize
(Environment_Task
: Task_Id
);
122 pragma Inline
(Initialize
);
123 -- Initialize various data needed by this package
125 function Is_Valid_Task
return Boolean;
126 pragma Inline
(Is_Valid_Task
);
127 -- Does executing thread have a TCB?
129 procedure Set
(Self_Id
: Task_Id
);
131 -- Set the self id for the current task
133 function Self
return Task_Id
;
134 pragma Inline
(Self
);
135 -- Return a pointer to the Ada Task Control Block of the calling task
139 package body Specific
is separate;
140 -- The body of this package is target specific
142 ---------------------------------
143 -- Support for foreign threads --
144 ---------------------------------
146 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
147 -- Allocate and Initialize a new ATCB for the current Thread
149 function Register_Foreign_Thread
150 (Thread
: Thread_Id
) return Task_Id
is separate;
152 -----------------------
153 -- Local Subprograms --
154 -----------------------
156 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
158 procedure Abort_Handler
(signo
: Signal
);
160 function To_pthread_t
is new Ada
.Unchecked_Conversion
161 (unsigned_long
, System
.OS_Interface
.pthread_t
);
167 procedure Abort_Handler
(signo
: Signal
) is
168 pragma Unreferenced
(signo
);
170 Self_Id
: constant Task_Id
:= Self
;
171 Result
: Interfaces
.C
.int
;
172 Old_Set
: aliased sigset_t
;
175 if ZCX_By_Default
and then GCC_ZCX_Support
then
179 if Self_Id
.Deferral_Level
= 0
180 and then Self_Id
.Pending_ATC_Level
< Self_Id
.ATC_Nesting_Level
181 and then not Self_Id
.Aborting
183 Self_Id
.Aborting
:= True;
185 -- Make sure signals used for RTS internal purpose are unmasked
190 Unblocked_Signal_Mask
'Access,
192 pragma Assert
(Result
= 0);
194 raise Standard
'Abort_Signal;
202 procedure Lock_RTS
is
204 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
211 procedure Unlock_RTS
is
213 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
220 -- The underlying thread system extends the memory (up to 2MB) when needed
222 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
223 pragma Unreferenced
(T
);
224 pragma Unreferenced
(On
);
233 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
235 return T
.Common
.LL
.Thread
;
242 function Self
return Task_Id
renames Specific
.Self
;
244 ---------------------
245 -- Initialize_Lock --
246 ---------------------
248 -- Note: mutexes and cond_variables needed per-task basis are initialized
249 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
250 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
251 -- status change of RTS. Therefore raising Storage_Error in the following
252 -- routines should be able to be handled safely.
254 procedure Initialize_Lock
255 (Prio
: System
.Any_Priority
;
256 L
: not null access Lock
)
258 pragma Unreferenced
(Prio
);
260 Result
: Interfaces
.C
.int
;
263 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
265 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
267 if Result
= ENOMEM
then
268 raise Storage_Error
with "Failed to allocate a lock";
272 procedure Initialize_Lock
273 (L
: not null access RTS_Lock
;
276 pragma Unreferenced
(Level
);
278 Result
: Interfaces
.C
.int
;
281 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
283 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
285 if Result
= ENOMEM
then
294 procedure Finalize_Lock
(L
: not null access Lock
) is
295 Result
: Interfaces
.C
.int
;
297 Result
:= pthread_mutex_destroy
(L
);
298 pragma Assert
(Result
= 0);
301 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
302 Result
: Interfaces
.C
.int
;
304 Result
:= pthread_mutex_destroy
(L
);
305 pragma Assert
(Result
= 0);
313 (L
: not null access Lock
;
314 Ceiling_Violation
: out Boolean)
316 Result
: Interfaces
.C
.int
;
318 Result
:= pthread_mutex_lock
(L
);
319 Ceiling_Violation
:= Result
= EINVAL
;
321 -- Assume the cause of EINVAL is a priority ceiling violation
323 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
327 (L
: not null access RTS_Lock
;
328 Global_Lock
: Boolean := False)
330 Result
: Interfaces
.C
.int
;
332 if not Single_Lock
or else Global_Lock
then
333 Result
:= pthread_mutex_lock
(L
);
334 pragma Assert
(Result
= 0);
338 procedure Write_Lock
(T
: Task_Id
) is
339 Result
: Interfaces
.C
.int
;
341 if not Single_Lock
then
342 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
343 pragma Assert
(Result
= 0);
352 (L
: not null access Lock
;
353 Ceiling_Violation
: out Boolean)
356 Write_Lock
(L
, Ceiling_Violation
);
363 procedure Unlock
(L
: not null access Lock
) is
364 Result
: Interfaces
.C
.int
;
366 Result
:= pthread_mutex_unlock
(L
);
367 pragma Assert
(Result
= 0);
371 (L
: not null access RTS_Lock
;
372 Global_Lock
: Boolean := False)
374 Result
: Interfaces
.C
.int
;
376 if not Single_Lock
or else Global_Lock
then
377 Result
:= pthread_mutex_unlock
(L
);
378 pragma Assert
(Result
= 0);
382 procedure Unlock
(T
: Task_Id
) is
383 Result
: Interfaces
.C
.int
;
385 if not Single_Lock
then
386 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
387 pragma Assert
(Result
= 0);
395 -- Dynamic priority ceilings are not supported by the underlying system
397 procedure Set_Ceiling
398 (L
: not null access Lock
;
399 Prio
: System
.Any_Priority
)
401 pragma Unreferenced
(L
, Prio
);
412 Reason
: System
.Tasking
.Task_States
)
414 pragma Unreferenced
(Reason
);
416 Result
: Interfaces
.C
.int
;
419 pragma Assert
(Self_ID
= Self
);
424 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
428 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
431 -- EINTR is not considered a failure
433 pragma Assert
(Result
= 0 or else Result
= EINTR
);
440 -- This is for use within the run-time system, so abort is
441 -- assumed to be already deferred, and the caller should be
442 -- holding its own ATCB lock.
444 procedure Timed_Sleep
447 Mode
: ST
.Delay_Modes
;
448 Reason
: System
.Tasking
.Task_States
;
449 Timedout
: out Boolean;
450 Yielded
: out Boolean)
452 pragma Unreferenced
(Reason
);
454 Base_Time
: constant Duration := Monotonic_Clock
;
455 Check_Time
: Duration := Base_Time
;
457 Request
: aliased timespec
;
458 Result
: Interfaces
.C
.int
;
464 if Mode
= Relative
then
465 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
467 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
470 if Abs_Time
> Check_Time
then
471 Request
:= To_Timespec
(Abs_Time
);
474 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
478 pthread_cond_timedwait
479 (Self_ID
.Common
.LL
.CV
'Access,
480 Single_RTS_Lock
'Access,
485 pthread_cond_timedwait
486 (Self_ID
.Common
.LL
.CV
'Access,
487 Self_ID
.Common
.LL
.L
'Access,
491 Check_Time
:= Monotonic_Clock
;
492 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
494 if Result
= 0 or else Result
= EINTR
then
496 -- Somebody may have called Wakeup for us
502 pragma Assert
(Result
= ETIMEDOUT
);
511 -- This is for use in implementing delay statements, so we assume the
512 -- caller is abort-deferred but is holding no locks.
514 procedure Timed_Delay
517 Mode
: ST
.Delay_Modes
)
519 Base_Time
: constant Duration := Monotonic_Clock
;
520 Check_Time
: Duration := Base_Time
;
522 Request
: aliased timespec
;
524 Result
: Interfaces
.C
.int
;
525 pragma Warnings
(Off
, Result
);
532 Write_Lock
(Self_ID
);
534 if Mode
= Relative
then
535 Abs_Time
:= Time
+ Check_Time
;
537 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
540 if Abs_Time
> Check_Time
then
541 Request
:= To_Timespec
(Abs_Time
);
542 Self_ID
.Common
.State
:= Delay_Sleep
;
545 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
548 Result
:= pthread_cond_timedwait
549 (Self_ID
.Common
.LL
.CV
'Access,
550 Single_RTS_Lock
'Access,
553 Result
:= pthread_cond_timedwait
554 (Self_ID
.Common
.LL
.CV
'Access,
555 Self_ID
.Common
.LL
.L
'Access,
559 Check_Time
:= Monotonic_Clock
;
560 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
562 pragma Assert
(Result
= 0 or else
563 Result
= ETIMEDOUT
or else
567 Self_ID
.Common
.State
:= Runnable
;
576 Result
:= sched_yield
;
579 ---------------------
580 -- Monotonic_Clock --
581 ---------------------
583 function Monotonic_Clock
return Duration is
584 TV
: aliased struct_timeval
;
585 Result
: Interfaces
.C
.int
;
587 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
588 pragma Assert
(Result
= 0);
589 return To_Duration
(TV
);
596 function RT_Resolution
return Duration is
605 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
606 pragma Unreferenced
(Reason
);
607 Result
: Interfaces
.C
.int
;
609 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
610 pragma Assert
(Result
= 0);
617 procedure Yield
(Do_Yield
: Boolean := True) is
618 Result
: Interfaces
.C
.int
;
619 pragma Unreferenced
(Result
);
622 Result
:= sched_yield
;
630 procedure Set_Priority
632 Prio
: System
.Any_Priority
;
633 Loss_Of_Inheritance
: Boolean := False)
635 pragma Unreferenced
(Loss_Of_Inheritance
);
637 Result
: Interfaces
.C
.int
;
638 Param
: aliased struct_sched_param
;
640 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
641 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
642 -- Get priority specific dispatching policy
644 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
645 -- Upper case first character of the policy name corresponding to the
646 -- task as set by a Priority_Specific_Dispatching pragma.
649 T
.Common
.Current_Priority
:= Prio
;
651 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
653 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
) + 1;
655 if Dispatching_Policy
= 'R'
656 or else Priority_Specific_Policy
= 'R'
657 or else Time_Slice_Val
> 0
660 pthread_setschedparam
661 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
663 elsif Dispatching_Policy
= 'F'
664 or else Priority_Specific_Policy
= 'F'
665 or else Time_Slice_Val
= 0
668 pthread_setschedparam
669 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
672 Param
.sched_priority
:= 0;
674 pthread_setschedparam
676 SCHED_OTHER
, Param
'Access);
679 pragma Assert
(Result
= 0 or else Result
= EPERM
);
686 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
688 return T
.Common
.Current_Priority
;
695 procedure Enter_Task
(Self_ID
: Task_Id
) is
697 if Self_ID
.Common
.Task_Info
/= null
698 and then Self_ID
.Common
.Task_Info
.CPU_Affinity
= No_CPU
700 raise Invalid_CPU_Number
;
703 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
704 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
706 Specific
.Set
(Self_ID
);
708 if Use_Alternate_Stack
then
710 Stack
: aliased stack_t
;
711 Result
: Interfaces
.C
.int
;
713 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
714 Stack
.ss_size
:= Alternate_Stack_Size
;
716 Result
:= sigaltstack
(Stack
'Access, null);
717 pragma Assert
(Result
= 0);
726 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
728 return new Ada_Task_Control_Block
(Entry_Num
);
735 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
737 -----------------------------
738 -- Register_Foreign_Thread --
739 -----------------------------
741 function Register_Foreign_Thread
return Task_Id
is
743 if Is_Valid_Task
then
746 return Register_Foreign_Thread
(pthread_self
);
748 end Register_Foreign_Thread
;
754 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
755 Result
: Interfaces
.C
.int
;
758 -- Give the task a unique serial number
760 Self_ID
.Serial_Number
:= Next_Serial_Number
;
761 Next_Serial_Number
:= Next_Serial_Number
+ 1;
762 pragma Assert
(Next_Serial_Number
/= 0);
764 Self_ID
.Common
.LL
.Thread
:= To_pthread_t
(-1);
766 if not Single_Lock
then
767 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
769 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
777 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
779 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
784 if not Single_Lock
then
785 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
786 pragma Assert
(Result
= 0);
797 procedure Create_Task
799 Wrapper
: System
.Address
;
800 Stack_Size
: System
.Parameters
.Size_Type
;
801 Priority
: System
.Any_Priority
;
802 Succeeded
: out Boolean)
804 Attributes
: aliased pthread_attr_t
;
805 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
806 Result
: Interfaces
.C
.int
;
809 Adjusted_Stack_Size
:=
810 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
812 Result
:= pthread_attr_init
(Attributes
'Access);
813 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
821 pthread_attr_setstacksize
822 (Attributes
'Access, Adjusted_Stack_Size
);
823 pragma Assert
(Result
= 0);
826 pthread_attr_setdetachstate
827 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
828 pragma Assert
(Result
= 0);
830 -- Since the initial signal mask of a thread is inherited from the
831 -- creator, and the Environment task has all its signals masked, we
832 -- do not need to manipulate caller's signal mask at this point.
833 -- All tasks in RTS will have All_Tasks_Mask initially.
835 Result
:= pthread_create
836 (T
.Common
.LL
.Thread
'Access,
838 Thread_Body_Access
(Wrapper
),
841 (Result
= 0 or else Result
= EAGAIN
or else Result
= ENOMEM
);
845 Result
:= pthread_attr_destroy
(Attributes
'Access);
846 pragma Assert
(Result
= 0);
854 if T
.Common
.Task_Info
/= null then
855 if T
.Common
.Task_Info
.CPU_Affinity
/= Task_Info
.Any_CPU
then
857 pthread_setaffinity_np
860 T
.Common
.Task_Info
.CPU_Affinity
'Access);
861 pragma Assert
(Result
= 0);
865 Result
:= pthread_attr_destroy
(Attributes
'Access);
866 pragma Assert
(Result
= 0);
868 Set_Priority
(T
, Priority
);
875 procedure Finalize_TCB
(T
: Task_Id
) is
876 Result
: Interfaces
.C
.int
;
878 Is_Self
: constant Boolean := T
= Self
;
880 procedure Free
is new
881 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
884 if not Single_Lock
then
885 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
886 pragma Assert
(Result
= 0);
889 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
890 pragma Assert
(Result
= 0);
892 if T
.Known_Tasks_Index
/= -1 then
893 Known_Tasks
(T
.Known_Tasks_Index
) := null;
895 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
907 procedure Exit_Task
is
916 procedure Abort_Task
(T
: Task_Id
) is
917 Result
: Interfaces
.C
.int
;
922 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
923 pragma Assert
(Result
= 0);
930 procedure Initialize
(S
: in out Suspension_Object
) is
931 Result
: Interfaces
.C
.int
;
934 -- Initialize internal state (always to False (RM D.10(6)))
939 -- Initialize internal mutex
941 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
943 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
945 if Result
= ENOMEM
then
949 -- Initialize internal condition variable
951 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
953 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
956 Result
:= pthread_mutex_destroy
(S
.L
'Access);
957 pragma Assert
(Result
= 0);
959 if Result
= ENOMEM
then
969 procedure Finalize
(S
: in out Suspension_Object
) is
970 Result
: Interfaces
.C
.int
;
973 -- Destroy internal mutex
975 Result
:= pthread_mutex_destroy
(S
.L
'Access);
976 pragma Assert
(Result
= 0);
978 -- Destroy internal condition variable
980 Result
:= pthread_cond_destroy
(S
.CV
'Access);
981 pragma Assert
(Result
= 0);
988 function Current_State
(S
: Suspension_Object
) return Boolean is
990 -- We do not want to use lock on this read operation. State is marked
991 -- as Atomic so that we ensure that the value retrieved is correct.
1000 procedure Set_False
(S
: in out Suspension_Object
) is
1001 Result
: Interfaces
.C
.int
;
1004 SSL
.Abort_Defer
.all;
1006 Result
:= pthread_mutex_lock
(S
.L
'Access);
1007 pragma Assert
(Result
= 0);
1011 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1012 pragma Assert
(Result
= 0);
1014 SSL
.Abort_Undefer
.all;
1021 procedure Set_True
(S
: in out Suspension_Object
) is
1022 Result
: Interfaces
.C
.int
;
1025 SSL
.Abort_Defer
.all;
1027 Result
:= pthread_mutex_lock
(S
.L
'Access);
1028 pragma Assert
(Result
= 0);
1030 -- If there is already a task waiting on this suspension object then
1031 -- we resume it, leaving the state of the suspension object to False,
1032 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1033 -- the state to True.
1039 Result
:= pthread_cond_signal
(S
.CV
'Access);
1040 pragma Assert
(Result
= 0);
1046 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1047 pragma Assert
(Result
= 0);
1049 SSL
.Abort_Undefer
.all;
1052 ------------------------
1053 -- Suspend_Until_True --
1054 ------------------------
1056 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1057 Result
: Interfaces
.C
.int
;
1060 SSL
.Abort_Defer
.all;
1062 Result
:= pthread_mutex_lock
(S
.L
'Access);
1063 pragma Assert
(Result
= 0);
1067 -- Program_Error must be raised upon calling Suspend_Until_True
1068 -- if another task is already waiting on that suspension object
1071 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1072 pragma Assert
(Result
= 0);
1074 SSL
.Abort_Undefer
.all;
1076 raise Program_Error
;
1078 -- Suspend the task if the state is False. Otherwise, the task
1079 -- continues its execution, and the state of the suspension object
1080 -- is set to False (ARM D.10 par. 9).
1088 -- Loop in case pthread_cond_wait returns earlier than expected
1089 -- (e.g. in case of EINTR caused by a signal). This should not
1090 -- happen with the current Linux implementation of pthread, but
1091 -- POSIX does not guarantee it, so this may change in the
1094 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1095 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1097 exit when not S
.Waiting
;
1101 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1102 pragma Assert
(Result
= 0);
1104 SSL
.Abort_Undefer
.all;
1106 end Suspend_Until_True
;
1114 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1115 pragma Unreferenced
(Self_ID
);
1120 --------------------
1121 -- Check_No_Locks --
1122 --------------------
1124 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1125 pragma Unreferenced
(Self_ID
);
1130 ----------------------
1131 -- Environment_Task --
1132 ----------------------
1134 function Environment_Task
return Task_Id
is
1136 return Environment_Task_Id
;
1137 end Environment_Task
;
1143 function Suspend_Task
1145 Thread_Self
: Thread_Id
) return Boolean
1148 if T
.Common
.LL
.Thread
/= Thread_Self
then
1149 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1159 function Resume_Task
1161 Thread_Self
: Thread_Id
) return Boolean
1164 if T
.Common
.LL
.Thread
/= Thread_Self
then
1165 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1171 --------------------
1172 -- Stop_All_Tasks --
1173 --------------------
1175 procedure Stop_All_Tasks
is
1184 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1185 pragma Unreferenced
(T
);
1194 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1195 pragma Unreferenced
(T
);
1204 procedure Initialize
(Environment_Task
: Task_Id
) is
1205 act
: aliased struct_sigaction
;
1206 old_act
: aliased struct_sigaction
;
1207 Tmp_Set
: aliased sigset_t
;
1208 Result
: Interfaces
.C
.int
;
1209 -- Whether to use an alternate signal stack for stack overflows
1212 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1213 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1214 -- Get interrupt state. Defined in a-init.c
1215 -- The input argument is the interrupt number,
1216 -- and the result is one of the following:
1218 Default
: constant Character := 's';
1219 -- 'n' this interrupt not set by any Interrupt_State pragma
1220 -- 'u' Interrupt_State pragma set state to User
1221 -- 'r' Interrupt_State pragma set state to Runtime
1222 -- 's' Interrupt_State pragma set state to System (use "default"
1226 Environment_Task_Id
:= Environment_Task
;
1228 Interrupt_Management
.Initialize
;
1230 -- Prepare the set of signals that should be unblocked in all tasks
1232 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1233 pragma Assert
(Result
= 0);
1235 for J
in Interrupt_Management
.Interrupt_ID
loop
1236 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1237 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1238 pragma Assert
(Result
= 0);
1242 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1243 pragma Assert
(Result
= 0);
1245 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1246 pragma Assert
(Result
= 0);
1248 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1250 -- Initialize the global RTS lock
1252 Specific
.Initialize
(Environment_Task
);
1254 if Use_Alternate_Stack
then
1255 Environment_Task
.Common
.Task_Alternate_Stack
:=
1256 Alternate_Stack
'Address;
1259 -- Make environment task known here because it doesn't go through
1260 -- Activate_Tasks, which does it for all other tasks.
1262 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1263 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1265 Enter_Task
(Environment_Task
);
1267 -- Install the abort-signal handler
1270 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1273 act
.sa_handler
:= Abort_Handler
'Address;
1275 Result
:= sigemptyset
(Tmp_Set
'Access);
1276 pragma Assert
(Result
= 0);
1277 act
.sa_mask
:= Tmp_Set
;
1281 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1282 act
'Unchecked_Access,
1283 old_act
'Unchecked_Access);
1284 pragma Assert
(Result
= 0);
1288 end System
.Task_Primitives
.Operations
;