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-2007, 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 GNU/Linux (GNU/LinuxThreads) 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.
47 with System
.Task_Info
;
48 -- used for Unspecified_Task_Info
50 with System
.Tasking
.Debug
;
51 -- used for Known_Tasks
53 with System
.Interrupt_Management
;
54 -- used for Keep_Unmasked
55 -- Abort_Task_Interrupt
58 with System
.OS_Primitives
;
59 -- used for Delay_Modes
61 with System
.Soft_Links
;
62 -- used for Abort_Defer/Undefer
64 -- We use System.Soft_Links instead of System.Tasking.Initialization
65 -- because the later is a higher level package that we shouldn't depend on.
66 -- For example when using the restricted run time, it is replaced by
67 -- System.Tasking.Restricted.Stages.
69 with System
.Storage_Elements
;
70 with System
.Stack_Checking
.Operations
;
71 -- Used for Invalidate_Stack_Cache and Notify_Stack_Attributes;
74 -- used for Raise_Exception
75 -- Raise_From_Signal_Handler
78 with Ada
.Unchecked_Conversion
;
79 with Ada
.Unchecked_Deallocation
;
81 package body System
.Task_Primitives
.Operations
is
83 package SSL
renames System
.Soft_Links
;
84 package SC
renames System
.Stack_Checking
.Operations
;
86 use System
.Tasking
.Debug
;
89 use System
.OS_Interface
;
90 use System
.Parameters
;
91 use System
.OS_Primitives
;
92 use System
.Storage_Elements
;
99 -- The followings are logically constants, but need to be initialized
102 Single_RTS_Lock
: aliased RTS_Lock
;
103 -- This is a lock to allow only one thread of control in the RTS at
104 -- a time; it is used to execute in mutual exclusion from all other tasks.
105 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
107 ATCB_Key
: aliased pthread_key_t
;
108 -- Key used to find the Ada Task_Id associated with a thread
110 Environment_Task_Id
: Task_Id
;
111 -- A variable to hold Task_Id for the environment task
113 Unblocked_Signal_Mask
: aliased sigset_t
;
114 -- The set of signals that should be unblocked in all tasks
116 -- The followings are internal configuration constants needed
118 Next_Serial_Number
: Task_Serial_Number
:= 100;
119 -- We start at 100 (reserve some special values for using in error checks)
121 Time_Slice_Val
: Integer;
122 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
124 Dispatching_Policy
: Character;
125 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
127 -- The following are effectively constants, but they need to be initialized
128 -- by calling a pthread_ function.
130 Mutex_Attr
: aliased pthread_mutexattr_t
;
131 Cond_Attr
: aliased pthread_condattr_t
;
133 Foreign_Task_Elaborated
: aliased Boolean := True;
134 -- Used to identified fake tasks (i.e., non-Ada Threads)
142 procedure Initialize
(Environment_Task
: Task_Id
);
143 pragma Inline
(Initialize
);
144 -- Initialize various data needed by this package
146 function Is_Valid_Task
return Boolean;
147 pragma Inline
(Is_Valid_Task
);
148 -- Does executing thread have a TCB?
150 procedure Set
(Self_Id
: Task_Id
);
152 -- Set the self id for the current task
154 function Self
return Task_Id
;
155 pragma Inline
(Self
);
156 -- Return a pointer to the Ada Task Control Block of the calling task
160 package body Specific
is separate;
161 -- The body of this package is target specific
163 ---------------------------------
164 -- Support for foreign threads --
165 ---------------------------------
167 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
168 -- Allocate and Initialize a new ATCB for the current Thread
170 function Register_Foreign_Thread
171 (Thread
: Thread_Id
) return Task_Id
is separate;
173 -----------------------
174 -- Local Subprograms --
175 -----------------------
177 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
179 procedure Abort_Handler
(signo
: Signal
);
181 function To_pthread_t
is new Ada
.Unchecked_Conversion
182 (unsigned_long
, System
.OS_Interface
.pthread_t
);
184 procedure Get_Stack_Attributes
186 ISP
: out System
.Address
;
187 Size
: out Storage_Offset
);
188 -- Fill ISP and Size with the Initial Stack Pointer value and the
189 -- thread stack size for task T.
195 procedure Abort_Handler
(signo
: Signal
) is
196 pragma Unreferenced
(signo
);
198 Self_Id
: constant Task_Id
:= Self
;
199 Result
: Interfaces
.C
.int
;
200 Old_Set
: aliased sigset_t
;
203 if ZCX_By_Default
and then GCC_ZCX_Support
then
207 if Self_Id
.Deferral_Level
= 0
208 and then Self_Id
.Pending_ATC_Level
< Self_Id
.ATC_Nesting_Level
209 and then not Self_Id
.Aborting
211 Self_Id
.Aborting
:= True;
213 -- Make sure signals used for RTS internal purpose are unmasked
218 Unblocked_Signal_Mask
'Access,
220 pragma Assert
(Result
= 0);
222 raise Standard
'Abort_Signal;
230 procedure Lock_RTS
is
232 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
239 procedure Unlock_RTS
is
241 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
248 -- The underlying thread system extends the memory (up to 2MB) when needed
250 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
251 pragma Unreferenced
(T
);
252 pragma Unreferenced
(On
);
261 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
263 return T
.Common
.LL
.Thread
;
270 function Self
return Task_Id
renames Specific
.Self
;
272 ---------------------
273 -- Initialize_Lock --
274 ---------------------
276 -- Note: mutexes and cond_variables needed per-task basis are
277 -- initialized in Initialize_TCB and the Storage_Error is
278 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
279 -- used in RTS is initialized before any status change of RTS.
280 -- Therefore rasing Storage_Error in the following routines
281 -- should be able to be handled safely.
283 procedure Initialize_Lock
284 (Prio
: System
.Any_Priority
;
285 L
: not null access Lock
)
287 pragma Unreferenced
(Prio
);
289 Result
: Interfaces
.C
.int
;
292 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
294 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
296 if Result
= ENOMEM
then
297 Ada
.Exceptions
.Raise_Exception
(Storage_Error
'Identity,
298 "Failed to allocate a lock");
302 procedure Initialize_Lock
303 (L
: not null access RTS_Lock
;
306 pragma Unreferenced
(Level
);
308 Result
: Interfaces
.C
.int
;
311 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
313 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
315 if Result
= ENOMEM
then
324 procedure Finalize_Lock
(L
: not null access Lock
) is
325 Result
: Interfaces
.C
.int
;
327 Result
:= pthread_mutex_destroy
(L
);
328 pragma Assert
(Result
= 0);
331 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
332 Result
: Interfaces
.C
.int
;
334 Result
:= pthread_mutex_destroy
(L
);
335 pragma Assert
(Result
= 0);
343 (L
: not null access Lock
;
344 Ceiling_Violation
: out Boolean)
346 Result
: Interfaces
.C
.int
;
348 Result
:= pthread_mutex_lock
(L
);
349 Ceiling_Violation
:= Result
= EINVAL
;
351 -- Assume the cause of EINVAL is a priority ceiling violation
353 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
357 (L
: not null access RTS_Lock
;
358 Global_Lock
: Boolean := False)
360 Result
: Interfaces
.C
.int
;
362 if not Single_Lock
or else Global_Lock
then
363 Result
:= pthread_mutex_lock
(L
);
364 pragma Assert
(Result
= 0);
368 procedure Write_Lock
(T
: Task_Id
) is
369 Result
: Interfaces
.C
.int
;
371 if not Single_Lock
then
372 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
373 pragma Assert
(Result
= 0);
382 (L
: not null access Lock
;
383 Ceiling_Violation
: out Boolean)
386 Write_Lock
(L
, Ceiling_Violation
);
393 procedure Unlock
(L
: not null access Lock
) is
394 Result
: Interfaces
.C
.int
;
396 Result
:= pthread_mutex_unlock
(L
);
397 pragma Assert
(Result
= 0);
401 (L
: not null access RTS_Lock
;
402 Global_Lock
: Boolean := False)
404 Result
: Interfaces
.C
.int
;
406 if not Single_Lock
or else Global_Lock
then
407 Result
:= pthread_mutex_unlock
(L
);
408 pragma Assert
(Result
= 0);
412 procedure Unlock
(T
: Task_Id
) is
413 Result
: Interfaces
.C
.int
;
415 if not Single_Lock
then
416 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
417 pragma Assert
(Result
= 0);
425 -- Dynamic priority ceilings are not supported by the underlying system
427 procedure Set_Ceiling
428 (L
: not null access Lock
;
429 Prio
: System
.Any_Priority
)
431 pragma Unreferenced
(L
, Prio
);
442 Reason
: System
.Tasking
.Task_States
)
444 pragma Unreferenced
(Reason
);
446 Result
: Interfaces
.C
.int
;
449 pragma Assert
(Self_ID
= Self
);
454 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
458 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
461 -- EINTR is not considered a failure
463 pragma Assert
(Result
= 0 or else Result
= EINTR
);
470 -- This is for use within the run-time system, so abort is
471 -- assumed to be already deferred, and the caller should be
472 -- holding its own ATCB lock.
474 procedure Timed_Sleep
477 Mode
: ST
.Delay_Modes
;
478 Reason
: System
.Tasking
.Task_States
;
479 Timedout
: out Boolean;
480 Yielded
: out Boolean)
482 pragma Unreferenced
(Reason
);
484 Base_Time
: constant Duration := Monotonic_Clock
;
485 Check_Time
: Duration := Base_Time
;
487 Request
: aliased timespec
;
488 Result
: Interfaces
.C
.int
;
494 if Mode
= Relative
then
495 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
497 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
500 if Abs_Time
> Check_Time
then
501 Request
:= To_Timespec
(Abs_Time
);
504 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
508 pthread_cond_timedwait
509 (Self_ID
.Common
.LL
.CV
'Access,
510 Single_RTS_Lock
'Access,
515 pthread_cond_timedwait
516 (Self_ID
.Common
.LL
.CV
'Access,
517 Self_ID
.Common
.LL
.L
'Access,
521 Check_Time
:= Monotonic_Clock
;
522 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
524 if Result
= 0 or else Result
= EINTR
then
526 -- Somebody may have called Wakeup for us
532 pragma Assert
(Result
= ETIMEDOUT
);
541 -- This is for use in implementing delay statements, so we assume the
542 -- caller is abort-deferred but is holding no locks.
544 procedure Timed_Delay
547 Mode
: ST
.Delay_Modes
)
549 Base_Time
: constant Duration := Monotonic_Clock
;
550 Check_Time
: Duration := Base_Time
;
552 Request
: aliased timespec
;
554 Result
: Interfaces
.C
.int
;
555 pragma Warnings
(Off
, Result
);
562 Write_Lock
(Self_ID
);
564 if Mode
= Relative
then
565 Abs_Time
:= Time
+ Check_Time
;
567 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
570 if Abs_Time
> Check_Time
then
571 Request
:= To_Timespec
(Abs_Time
);
572 Self_ID
.Common
.State
:= Delay_Sleep
;
575 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
578 Result
:= pthread_cond_timedwait
579 (Self_ID
.Common
.LL
.CV
'Access,
580 Single_RTS_Lock
'Access,
583 Result
:= pthread_cond_timedwait
584 (Self_ID
.Common
.LL
.CV
'Access,
585 Self_ID
.Common
.LL
.L
'Access,
589 Check_Time
:= Monotonic_Clock
;
590 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
592 pragma Assert
(Result
= 0 or else
593 Result
= ETIMEDOUT
or else
597 Self_ID
.Common
.State
:= Runnable
;
606 Result
:= sched_yield
;
609 ---------------------
610 -- Monotonic_Clock --
611 ---------------------
613 function Monotonic_Clock
return Duration is
614 TV
: aliased struct_timeval
;
615 Result
: Interfaces
.C
.int
;
617 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
618 pragma Assert
(Result
= 0);
619 return To_Duration
(TV
);
626 function RT_Resolution
return Duration is
635 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
636 pragma Unreferenced
(Reason
);
637 Result
: Interfaces
.C
.int
;
639 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
640 pragma Assert
(Result
= 0);
647 procedure Yield
(Do_Yield
: Boolean := True) is
648 Result
: Interfaces
.C
.int
;
649 pragma Unreferenced
(Result
);
652 Result
:= sched_yield
;
660 procedure Set_Priority
662 Prio
: System
.Any_Priority
;
663 Loss_Of_Inheritance
: Boolean := False)
665 pragma Unreferenced
(Loss_Of_Inheritance
);
667 Result
: Interfaces
.C
.int
;
668 Param
: aliased struct_sched_param
;
670 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
671 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
672 -- Get priority specific dispatching policy
674 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
675 -- Upper case first character of the policy name corresponding to the
676 -- task as set by a Priority_Specific_Dispatching pragma.
679 T
.Common
.Current_Priority
:= Prio
;
681 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
683 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
) + 1;
685 if Dispatching_Policy
= 'R'
686 or else Priority_Specific_Policy
= 'R'
687 or else Time_Slice_Val
> 0
690 pthread_setschedparam
691 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
693 elsif Dispatching_Policy
= 'F'
694 or else Priority_Specific_Policy
= 'F'
695 or else Time_Slice_Val
= 0
698 pthread_setschedparam
699 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
702 Param
.sched_priority
:= 0;
704 pthread_setschedparam
706 SCHED_OTHER
, Param
'Access);
709 pragma Assert
(Result
= 0 or else Result
= EPERM
);
716 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
718 return T
.Common
.Current_Priority
;
721 --------------------------
722 -- Get_Stack_Attributes --
723 --------------------------
725 procedure Get_Stack_Attributes
727 ISP
: out System
.Address
;
728 Size
: out Storage_Offset
)
730 function pthread_getattr_np
732 attr
: System
.Address
) return Interfaces
.C
.int
;
733 pragma Import
(C
, pthread_getattr_np
, "pthread_getattr_np");
735 function pthread_attr_getstack
736 (attr
: System
.Address
;
737 base
: System
.Address
;
738 size
: System
.Address
) return Interfaces
.C
.int
;
739 pragma Import
(C
, pthread_attr_getstack
, "pthread_attr_getstack");
741 Result
: Interfaces
.C
.int
;
743 Attributes
: aliased pthread_attr_t
;
744 Stack_Base
: aliased System
.Address
;
745 Stack_Size
: aliased Storage_Offset
;
750 (T
.Common
.LL
.Thread
, Attributes
'Address);
751 pragma Assert
(Result
= 0);
754 pthread_attr_getstack
755 (Attributes
'Address, Stack_Base
'Address, Stack_Size
'Address);
756 pragma Assert
(Result
= 0);
758 Result
:= pthread_attr_destroy
(Attributes
'Access);
759 pragma Assert
(Result
= 0);
761 ISP
:= Stack_Base
+ Stack_Size
;
763 end Get_Stack_Attributes
;
769 procedure Enter_Task
(Self_ID
: Task_Id
) is
771 if Self_ID
.Common
.Task_Info
/= null
773 Self_ID
.Common
.Task_Info
.CPU_Affinity
= No_CPU
775 raise Invalid_CPU_Number
;
778 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
780 Specific
.Set
(Self_ID
);
784 for J
in Known_Tasks
'Range loop
785 if Known_Tasks
(J
) = null then
786 Known_Tasks
(J
) := Self_ID
;
787 Self_ID
.Known_Tasks_Index
:= J
;
794 -- Determine where the task stack starts, how large it is, and let the
795 -- stack checking engine know about it.
798 Initial_SP
: System
.Address
;
799 Stack_Size
: Storage_Offset
;
801 Get_Stack_Attributes
(Self_ID
, Initial_SP
, Stack_Size
);
802 System
.Stack_Checking
.Operations
.Notify_Stack_Attributes
803 (Initial_SP
, Stack_Size
);
811 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
813 return new Ada_Task_Control_Block
(Entry_Num
);
820 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
822 -----------------------------
823 -- Register_Foreign_Thread --
824 -----------------------------
826 function Register_Foreign_Thread
return Task_Id
is
828 if Is_Valid_Task
then
831 return Register_Foreign_Thread
(pthread_self
);
833 end Register_Foreign_Thread
;
839 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
840 Result
: Interfaces
.C
.int
;
843 -- Give the task a unique serial number
845 Self_ID
.Serial_Number
:= Next_Serial_Number
;
846 Next_Serial_Number
:= Next_Serial_Number
+ 1;
847 pragma Assert
(Next_Serial_Number
/= 0);
849 Self_ID
.Common
.LL
.Thread
:= To_pthread_t
(-1);
851 if not Single_Lock
then
852 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
854 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
862 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
864 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
869 if not Single_Lock
then
870 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
871 pragma Assert
(Result
= 0);
882 procedure Create_Task
884 Wrapper
: System
.Address
;
885 Stack_Size
: System
.Parameters
.Size_Type
;
886 Priority
: System
.Any_Priority
;
887 Succeeded
: out Boolean)
889 Attributes
: aliased pthread_attr_t
;
890 Result
: Interfaces
.C
.int
;
893 Result
:= pthread_attr_init
(Attributes
'Access);
894 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
902 pthread_attr_setstacksize
903 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
904 pragma Assert
(Result
= 0);
907 pthread_attr_setdetachstate
908 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
909 pragma Assert
(Result
= 0);
911 -- Since the initial signal mask of a thread is inherited from the
912 -- creator, and the Environment task has all its signals masked, we
913 -- do not need to manipulate caller's signal mask at this point.
914 -- All tasks in RTS will have All_Tasks_Mask initially.
916 Result
:= pthread_create
917 (T
.Common
.LL
.Thread
'Access,
919 Thread_Body_Access
(Wrapper
),
921 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
923 Succeeded
:= Result
= 0;
927 if T
.Common
.Task_Info
/= null then
928 if T
.Common
.Task_Info
.CPU_Affinity
/= Task_Info
.Any_CPU
then
930 pthread_setaffinity_np
933 T
.Common
.Task_Info
.CPU_Affinity
'Access);
934 pragma Assert
(Result
= 0);
938 Result
:= pthread_attr_destroy
(Attributes
'Access);
939 pragma Assert
(Result
= 0);
941 Set_Priority
(T
, Priority
);
948 procedure Finalize_TCB
(T
: Task_Id
) is
949 Result
: Interfaces
.C
.int
;
951 Is_Self
: constant Boolean := T
= Self
;
953 procedure Free
is new
954 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
957 if not Single_Lock
then
958 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
959 pragma Assert
(Result
= 0);
962 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
963 pragma Assert
(Result
= 0);
965 if T
.Known_Tasks_Index
/= -1 then
966 Known_Tasks
(T
.Known_Tasks_Index
) := null;
968 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
980 procedure Exit_Task
is
989 procedure Abort_Task
(T
: Task_Id
) is
990 Result
: Interfaces
.C
.int
;
995 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
996 pragma Assert
(Result
= 0);
1003 procedure Initialize
(S
: in out Suspension_Object
) is
1004 Result
: Interfaces
.C
.int
;
1007 -- Initialize internal state (always to False (RM D.10(6)))
1012 -- Initialize internal mutex
1014 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1016 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1018 if Result
= ENOMEM
then
1019 raise Storage_Error
;
1022 -- Initialize internal condition variable
1024 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1026 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1029 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1030 pragma Assert
(Result
= 0);
1032 if Result
= ENOMEM
then
1033 raise Storage_Error
;
1042 procedure Finalize
(S
: in out Suspension_Object
) is
1043 Result
: Interfaces
.C
.int
;
1046 -- Destroy internal mutex
1048 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1049 pragma Assert
(Result
= 0);
1051 -- Destroy internal condition variable
1053 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1054 pragma Assert
(Result
= 0);
1061 function Current_State
(S
: Suspension_Object
) return Boolean is
1063 -- We do not want to use lock on this read operation. State is marked
1064 -- as Atomic so that we ensure that the value retrieved is correct.
1073 procedure Set_False
(S
: in out Suspension_Object
) is
1074 Result
: Interfaces
.C
.int
;
1077 SSL
.Abort_Defer
.all;
1079 Result
:= pthread_mutex_lock
(S
.L
'Access);
1080 pragma Assert
(Result
= 0);
1084 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1085 pragma Assert
(Result
= 0);
1087 SSL
.Abort_Undefer
.all;
1094 procedure Set_True
(S
: in out Suspension_Object
) is
1095 Result
: Interfaces
.C
.int
;
1098 SSL
.Abort_Defer
.all;
1100 Result
:= pthread_mutex_lock
(S
.L
'Access);
1101 pragma Assert
(Result
= 0);
1103 -- If there is already a task waiting on this suspension object then
1104 -- we resume it, leaving the state of the suspension object to False,
1105 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1106 -- the state to True.
1112 Result
:= pthread_cond_signal
(S
.CV
'Access);
1113 pragma Assert
(Result
= 0);
1119 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1120 pragma Assert
(Result
= 0);
1122 SSL
.Abort_Undefer
.all;
1125 ------------------------
1126 -- Suspend_Until_True --
1127 ------------------------
1129 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1130 Result
: Interfaces
.C
.int
;
1133 SSL
.Abort_Defer
.all;
1135 Result
:= pthread_mutex_lock
(S
.L
'Access);
1136 pragma Assert
(Result
= 0);
1140 -- Program_Error must be raised upon calling Suspend_Until_True
1141 -- if another task is already waiting on that suspension object
1144 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1145 pragma Assert
(Result
= 0);
1147 SSL
.Abort_Undefer
.all;
1149 raise Program_Error
;
1151 -- Suspend the task if the state is False. Otherwise, the task
1152 -- continues its execution, and the state of the suspension object
1153 -- is set to False (ARM D.10 par. 9).
1159 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1162 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1163 pragma Assert
(Result
= 0);
1165 SSL
.Abort_Undefer
.all;
1168 end Suspend_Until_True
;
1176 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1177 pragma Unreferenced
(Self_ID
);
1182 --------------------
1183 -- Check_No_Locks --
1184 --------------------
1186 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1187 pragma Unreferenced
(Self_ID
);
1192 ----------------------
1193 -- Environment_Task --
1194 ----------------------
1196 function Environment_Task
return Task_Id
is
1198 return Environment_Task_Id
;
1199 end Environment_Task
;
1205 function Suspend_Task
1207 Thread_Self
: Thread_Id
) return Boolean
1210 if T
.Common
.LL
.Thread
/= Thread_Self
then
1211 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1221 function Resume_Task
1223 Thread_Self
: Thread_Id
) return Boolean
1226 if T
.Common
.LL
.Thread
/= Thread_Self
then
1227 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1233 --------------------
1234 -- Stop_All_Tasks --
1235 --------------------
1237 procedure Stop_All_Tasks
is
1246 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1247 pragma Unreferenced
(T
);
1256 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1257 pragma Unreferenced
(T
);
1266 procedure Initialize
(Environment_Task
: Task_Id
) is
1267 act
: aliased struct_sigaction
;
1268 old_act
: aliased struct_sigaction
;
1269 Tmp_Set
: aliased sigset_t
;
1270 Result
: Interfaces
.C
.int
;
1273 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1274 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1275 -- Get interrupt state. Defined in a-init.c
1276 -- The input argument is the interrupt number,
1277 -- and the result is one of the following:
1279 Default
: constant Character := 's';
1280 -- 'n' this interrupt not set by any Interrupt_State pragma
1281 -- 'u' Interrupt_State pragma set state to User
1282 -- 'r' Interrupt_State pragma set state to Runtime
1283 -- 's' Interrupt_State pragma set state to System (use "default"
1287 Environment_Task_Id
:= Environment_Task
;
1289 Interrupt_Management
.Initialize
;
1291 -- Prepare the set of signals that should be unblocked in all tasks
1293 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1294 pragma Assert
(Result
= 0);
1296 for J
in Interrupt_Management
.Interrupt_ID
loop
1297 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1298 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1299 pragma Assert
(Result
= 0);
1303 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1304 pragma Assert
(Result
= 0);
1306 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1307 pragma Assert
(Result
= 0);
1309 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1311 -- Initialize the global RTS lock
1313 Specific
.Initialize
(Environment_Task
);
1315 Enter_Task
(Environment_Task
);
1317 -- Install the abort-signal handler
1320 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1323 act
.sa_handler
:= Abort_Handler
'Address;
1325 Result
:= sigemptyset
(Tmp_Set
'Access);
1326 pragma Assert
(Result
= 0);
1327 act
.sa_mask
:= Tmp_Set
;
1331 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1332 act
'Unchecked_Access,
1333 old_act
'Unchecked_Access);
1334 pragma Assert
(Result
= 0);
1338 end System
.Task_Primitives
.Operations
;