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
.Tasking
.Debug
;
48 -- used for Known_Tasks
50 with System
.Interrupt_Management
;
51 -- used for Keep_Unmasked
52 -- Abort_Task_Interrupt
55 with System
.OS_Primitives
;
56 -- used for Delay_Modes
58 with System
.Soft_Links
;
59 -- used for Abort_Defer/Undefer
61 -- We use System.Soft_Links instead of System.Tasking.Initialization
62 -- because the later is a higher level package that we shouldn't depend on.
63 -- For example when using the restricted run time, it is replaced by
64 -- System.Tasking.Restricted.Stages.
66 with System
.Storage_Elements
;
67 with System
.Stack_Checking
.Operations
;
68 -- Used for Invalidate_Stack_Cache and Notify_Stack_Attributes;
71 -- used for Raise_Exception
72 -- Raise_From_Signal_Handler
75 with Ada
.Unchecked_Conversion
;
76 with Ada
.Unchecked_Deallocation
;
78 package body System
.Task_Primitives
.Operations
is
80 package SSL
renames System
.Soft_Links
;
81 package SC
renames System
.Stack_Checking
.Operations
;
83 use System
.Tasking
.Debug
;
86 use System
.OS_Interface
;
87 use System
.Parameters
;
88 use System
.OS_Primitives
;
89 use System
.Storage_Elements
;
95 -- The followings are logically constants, but need to be initialized
98 Single_RTS_Lock
: aliased RTS_Lock
;
99 -- This is a lock to allow only one thread of control in the RTS at
100 -- a time; it is used to execute in mutual exclusion from all other tasks.
101 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
103 ATCB_Key
: aliased pthread_key_t
;
104 -- Key used to find the Ada Task_Id associated with a thread
106 Environment_Task_Id
: Task_Id
;
107 -- A variable to hold Task_Id for the environment task
109 Unblocked_Signal_Mask
: aliased sigset_t
;
110 -- The set of signals that should be unblocked in all tasks
112 -- The followings are internal configuration constants needed
114 Next_Serial_Number
: Task_Serial_Number
:= 100;
115 -- We start at 100 (reserve some special values for using in error checks)
117 Time_Slice_Val
: Integer;
118 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
120 Dispatching_Policy
: Character;
121 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
123 -- The following are effectively constants, but they need to be initialized
124 -- by calling a pthread_ function.
126 Mutex_Attr
: aliased pthread_mutexattr_t
;
127 Cond_Attr
: aliased pthread_condattr_t
;
129 Foreign_Task_Elaborated
: aliased Boolean := True;
130 -- Used to identified fake tasks (i.e., non-Ada Threads)
138 procedure Initialize
(Environment_Task
: Task_Id
);
139 pragma Inline
(Initialize
);
140 -- Initialize various data needed by this package
142 function Is_Valid_Task
return Boolean;
143 pragma Inline
(Is_Valid_Task
);
144 -- Does executing thread have a TCB?
146 procedure Set
(Self_Id
: Task_Id
);
148 -- Set the self id for the current task
150 function Self
return Task_Id
;
151 pragma Inline
(Self
);
152 -- Return a pointer to the Ada Task Control Block of the calling task
156 package body Specific
is separate;
157 -- The body of this package is target specific
159 ---------------------------------
160 -- Support for foreign threads --
161 ---------------------------------
163 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
164 -- Allocate and Initialize a new ATCB for the current Thread
166 function Register_Foreign_Thread
167 (Thread
: Thread_Id
) return Task_Id
is separate;
169 -----------------------
170 -- Local Subprograms --
171 -----------------------
173 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
175 procedure Abort_Handler
(signo
: Signal
);
177 function To_pthread_t
is new Ada
.Unchecked_Conversion
178 (unsigned_long
, System
.OS_Interface
.pthread_t
);
180 procedure Get_Stack_Attributes
182 ISP
: out System
.Address
;
183 Size
: out Storage_Offset
);
184 -- Fill ISP and Size with the Initial Stack Pointer value and the
185 -- thread stack size for task T.
191 procedure Abort_Handler
(signo
: Signal
) is
192 pragma Unreferenced
(signo
);
194 Self_Id
: constant Task_Id
:= Self
;
195 Result
: Interfaces
.C
.int
;
196 Old_Set
: aliased sigset_t
;
199 if ZCX_By_Default
and then GCC_ZCX_Support
then
203 if Self_Id
.Deferral_Level
= 0
204 and then Self_Id
.Pending_ATC_Level
< Self_Id
.ATC_Nesting_Level
205 and then not Self_Id
.Aborting
207 Self_Id
.Aborting
:= True;
209 -- Make sure signals used for RTS internal purpose are unmasked
214 Unblocked_Signal_Mask
'Unchecked_Access,
215 Old_Set
'Unchecked_Access);
216 pragma Assert
(Result
= 0);
218 raise Standard
'Abort_Signal;
226 procedure Lock_RTS
is
228 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
235 procedure Unlock_RTS
is
237 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
244 -- The underlying thread system extends the memory (up to 2MB) when needed
246 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
247 pragma Unreferenced
(T
);
248 pragma Unreferenced
(On
);
257 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
259 return T
.Common
.LL
.Thread
;
266 function Self
return Task_Id
renames Specific
.Self
;
268 ---------------------
269 -- Initialize_Lock --
270 ---------------------
272 -- Note: mutexes and cond_variables needed per-task basis are
273 -- initialized in Initialize_TCB and the Storage_Error is
274 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
275 -- used in RTS is initialized before any status change of RTS.
276 -- Therefore rasing Storage_Error in the following routines
277 -- should be able to be handled safely.
279 procedure Initialize_Lock
280 (Prio
: System
.Any_Priority
;
281 L
: not null access Lock
)
283 pragma Unreferenced
(Prio
);
285 Result
: Interfaces
.C
.int
;
288 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
290 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
292 if Result
= ENOMEM
then
293 Ada
.Exceptions
.Raise_Exception
(Storage_Error
'Identity,
294 "Failed to allocate a lock");
298 procedure Initialize_Lock
299 (L
: not null access RTS_Lock
;
302 pragma Unreferenced
(Level
);
304 Result
: Interfaces
.C
.int
;
307 Result
:= pthread_mutex_init
(L
, Mutex_Attr
'Access);
309 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
311 if Result
= ENOMEM
then
320 procedure Finalize_Lock
(L
: not null access Lock
) is
321 Result
: Interfaces
.C
.int
;
323 Result
:= pthread_mutex_destroy
(L
);
324 pragma Assert
(Result
= 0);
327 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
328 Result
: Interfaces
.C
.int
;
330 Result
:= pthread_mutex_destroy
(L
);
331 pragma Assert
(Result
= 0);
339 (L
: not null access Lock
;
340 Ceiling_Violation
: out Boolean)
342 Result
: Interfaces
.C
.int
;
344 Result
:= pthread_mutex_lock
(L
);
345 Ceiling_Violation
:= Result
= EINVAL
;
347 -- Assume the cause of EINVAL is a priority ceiling violation
349 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
353 (L
: not null access RTS_Lock
;
354 Global_Lock
: Boolean := False)
356 Result
: Interfaces
.C
.int
;
358 if not Single_Lock
or else Global_Lock
then
359 Result
:= pthread_mutex_lock
(L
);
360 pragma Assert
(Result
= 0);
364 procedure Write_Lock
(T
: Task_Id
) is
365 Result
: Interfaces
.C
.int
;
367 if not Single_Lock
then
368 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
369 pragma Assert
(Result
= 0);
378 (L
: not null access Lock
;
379 Ceiling_Violation
: out Boolean)
382 Write_Lock
(L
, Ceiling_Violation
);
389 procedure Unlock
(L
: not null access Lock
) is
390 Result
: Interfaces
.C
.int
;
392 Result
:= pthread_mutex_unlock
(L
);
393 pragma Assert
(Result
= 0);
397 (L
: not null access RTS_Lock
;
398 Global_Lock
: Boolean := False)
400 Result
: Interfaces
.C
.int
;
402 if not Single_Lock
or else Global_Lock
then
403 Result
:= pthread_mutex_unlock
(L
);
404 pragma Assert
(Result
= 0);
408 procedure Unlock
(T
: Task_Id
) is
409 Result
: Interfaces
.C
.int
;
411 if not Single_Lock
then
412 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
413 pragma Assert
(Result
= 0);
421 -- Dynamic priority ceilings are not supported by the underlying system
423 procedure Set_Ceiling
424 (L
: not null access Lock
;
425 Prio
: System
.Any_Priority
)
427 pragma Unreferenced
(L
, Prio
);
438 Reason
: System
.Tasking
.Task_States
)
440 pragma Unreferenced
(Reason
);
442 Result
: Interfaces
.C
.int
;
445 pragma Assert
(Self_ID
= Self
);
450 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
454 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
457 -- EINTR is not considered a failure
459 pragma Assert
(Result
= 0 or else Result
= EINTR
);
466 -- This is for use within the run-time system, so abort is
467 -- assumed to be already deferred, and the caller should be
468 -- holding its own ATCB lock.
470 procedure Timed_Sleep
473 Mode
: ST
.Delay_Modes
;
474 Reason
: System
.Tasking
.Task_States
;
475 Timedout
: out Boolean;
476 Yielded
: out Boolean)
478 pragma Unreferenced
(Reason
);
480 Base_Time
: constant Duration := Monotonic_Clock
;
481 Check_Time
: Duration := Base_Time
;
483 Request
: aliased timespec
;
484 Result
: Interfaces
.C
.int
;
490 if Mode
= Relative
then
491 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
493 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
496 if Abs_Time
> Check_Time
then
497 Request
:= To_Timespec
(Abs_Time
);
500 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
504 pthread_cond_timedwait
505 (Self_ID
.Common
.LL
.CV
'Access,
506 Single_RTS_Lock
'Access,
511 pthread_cond_timedwait
512 (Self_ID
.Common
.LL
.CV
'Access,
513 Self_ID
.Common
.LL
.L
'Access,
517 Check_Time
:= Monotonic_Clock
;
518 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
520 if Result
= 0 or else Result
= EINTR
then
522 -- Somebody may have called Wakeup for us
528 pragma Assert
(Result
= ETIMEDOUT
);
537 -- This is for use in implementing delay statements, so we assume the
538 -- caller is abort-deferred but is holding no locks.
540 procedure Timed_Delay
543 Mode
: ST
.Delay_Modes
)
545 Base_Time
: constant Duration := Monotonic_Clock
;
546 Check_Time
: Duration := Base_Time
;
548 Request
: aliased timespec
;
550 Result
: Interfaces
.C
.int
;
551 pragma Warnings
(Off
, Result
);
558 Write_Lock
(Self_ID
);
560 if Mode
= Relative
then
561 Abs_Time
:= Time
+ Check_Time
;
563 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
566 if Abs_Time
> Check_Time
then
567 Request
:= To_Timespec
(Abs_Time
);
568 Self_ID
.Common
.State
:= Delay_Sleep
;
571 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
574 Result
:= pthread_cond_timedwait
575 (Self_ID
.Common
.LL
.CV
'Access,
576 Single_RTS_Lock
'Access,
579 Result
:= pthread_cond_timedwait
580 (Self_ID
.Common
.LL
.CV
'Access,
581 Self_ID
.Common
.LL
.L
'Access,
585 Check_Time
:= Monotonic_Clock
;
586 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
588 pragma Assert
(Result
= 0 or else
589 Result
= ETIMEDOUT
or else
593 Self_ID
.Common
.State
:= Runnable
;
602 Result
:= sched_yield
;
605 ---------------------
606 -- Monotonic_Clock --
607 ---------------------
609 function Monotonic_Clock
return Duration is
610 TV
: aliased struct_timeval
;
611 Result
: Interfaces
.C
.int
;
613 Result
:= gettimeofday
(TV
'Access, System
.Null_Address
);
614 pragma Assert
(Result
= 0);
615 return To_Duration
(TV
);
622 function RT_Resolution
return Duration is
631 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
632 pragma Unreferenced
(Reason
);
633 Result
: Interfaces
.C
.int
;
635 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
636 pragma Assert
(Result
= 0);
643 procedure Yield
(Do_Yield
: Boolean := True) is
644 Result
: Interfaces
.C
.int
;
645 pragma Unreferenced
(Result
);
648 Result
:= sched_yield
;
656 procedure Set_Priority
658 Prio
: System
.Any_Priority
;
659 Loss_Of_Inheritance
: Boolean := False)
661 pragma Unreferenced
(Loss_Of_Inheritance
);
663 Result
: Interfaces
.C
.int
;
664 Param
: aliased struct_sched_param
;
666 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
667 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
668 -- Get priority specific dispatching policy
670 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
671 -- Upper case first character of the policy name corresponding to the
672 -- task as set by a Priority_Specific_Dispatching pragma.
675 T
.Common
.Current_Priority
:= Prio
;
677 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
679 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
) + 1;
681 if Dispatching_Policy
= 'R'
682 or else Priority_Specific_Policy
= 'R'
683 or else Time_Slice_Val
> 0
686 pthread_setschedparam
687 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
689 elsif Dispatching_Policy
= 'F'
690 or else Priority_Specific_Policy
= 'F'
691 or else Time_Slice_Val
= 0
694 pthread_setschedparam
695 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
698 Param
.sched_priority
:= 0;
700 pthread_setschedparam
702 SCHED_OTHER
, Param
'Access);
705 pragma Assert
(Result
= 0 or else Result
= EPERM
);
712 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
714 return T
.Common
.Current_Priority
;
717 --------------------------
718 -- Get_Stack_Attributes --
719 --------------------------
721 procedure Get_Stack_Attributes
723 ISP
: out System
.Address
;
724 Size
: out Storage_Offset
)
726 function pthread_getattr_np
728 attr
: System
.Address
) return Interfaces
.C
.int
;
729 pragma Import
(C
, pthread_getattr_np
, "pthread_getattr_np");
731 function pthread_attr_getstack
732 (attr
: System
.Address
;
733 base
: System
.Address
;
734 size
: System
.Address
) return Interfaces
.C
.int
;
735 pragma Import
(C
, pthread_attr_getstack
, "pthread_attr_getstack");
737 Result
: Interfaces
.C
.int
;
739 Attributes
: aliased pthread_attr_t
;
740 Stack_Base
: aliased System
.Address
;
741 Stack_Size
: aliased Storage_Offset
;
746 (T
.Common
.LL
.Thread
, Attributes
'Address);
747 pragma Assert
(Result
= 0);
750 pthread_attr_getstack
751 (Attributes
'Address, Stack_Base
'Address, Stack_Size
'Address);
752 pragma Assert
(Result
= 0);
754 Result
:= pthread_attr_destroy
(Attributes
'Access);
755 pragma Assert
(Result
= 0);
757 ISP
:= Stack_Base
+ Stack_Size
;
759 end Get_Stack_Attributes
;
765 procedure Enter_Task
(Self_ID
: Task_Id
) is
767 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
769 Specific
.Set
(Self_ID
);
773 for J
in Known_Tasks
'Range loop
774 if Known_Tasks
(J
) = null then
775 Known_Tasks
(J
) := Self_ID
;
776 Self_ID
.Known_Tasks_Index
:= J
;
783 -- Determine where the task stack starts, how large it is, and let the
784 -- stack checking engine know about it.
787 Initial_SP
: System
.Address
;
788 Stack_Size
: Storage_Offset
;
790 Get_Stack_Attributes
(Self_ID
, Initial_SP
, Stack_Size
);
791 System
.Stack_Checking
.Operations
.Notify_Stack_Attributes
792 (Initial_SP
, Stack_Size
);
800 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
802 return new Ada_Task_Control_Block
(Entry_Num
);
809 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
811 -----------------------------
812 -- Register_Foreign_Thread --
813 -----------------------------
815 function Register_Foreign_Thread
return Task_Id
is
817 if Is_Valid_Task
then
820 return Register_Foreign_Thread
(pthread_self
);
822 end Register_Foreign_Thread
;
828 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
829 Result
: Interfaces
.C
.int
;
832 -- Give the task a unique serial number
834 Self_ID
.Serial_Number
:= Next_Serial_Number
;
835 Next_Serial_Number
:= Next_Serial_Number
+ 1;
836 pragma Assert
(Next_Serial_Number
/= 0);
838 Self_ID
.Common
.LL
.Thread
:= To_pthread_t
(-1);
840 if not Single_Lock
then
841 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
843 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
851 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
853 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
858 if not Single_Lock
then
859 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
860 pragma Assert
(Result
= 0);
871 procedure Create_Task
873 Wrapper
: System
.Address
;
874 Stack_Size
: System
.Parameters
.Size_Type
;
875 Priority
: System
.Any_Priority
;
876 Succeeded
: out Boolean)
878 Attributes
: aliased pthread_attr_t
;
879 Result
: Interfaces
.C
.int
;
882 Result
:= pthread_attr_init
(Attributes
'Access);
883 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
891 pthread_attr_setstacksize
892 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
893 pragma Assert
(Result
= 0);
896 pthread_attr_setdetachstate
897 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
898 pragma Assert
(Result
= 0);
900 -- Since the initial signal mask of a thread is inherited from the
901 -- creator, and the Environment task has all its signals masked, we
902 -- do not need to manipulate caller's signal mask at this point.
903 -- All tasks in RTS will have All_Tasks_Mask initially.
905 Result
:= pthread_create
906 (T
.Common
.LL
.Thread
'Access,
908 Thread_Body_Access
(Wrapper
),
910 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
912 Succeeded
:= Result
= 0;
914 Result
:= pthread_attr_destroy
(Attributes
'Access);
915 pragma Assert
(Result
= 0);
917 Set_Priority
(T
, Priority
);
924 procedure Finalize_TCB
(T
: Task_Id
) is
925 Result
: Interfaces
.C
.int
;
927 Is_Self
: constant Boolean := T
= Self
;
929 procedure Free
is new
930 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
933 if not Single_Lock
then
934 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
935 pragma Assert
(Result
= 0);
938 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
939 pragma Assert
(Result
= 0);
941 if T
.Known_Tasks_Index
/= -1 then
942 Known_Tasks
(T
.Known_Tasks_Index
) := null;
944 SC
.Invalidate_Stack_Cache
(T
.Common
.Compiler_Data
.Pri_Stack_Info
'Access);
956 procedure Exit_Task
is
965 procedure Abort_Task
(T
: Task_Id
) is
966 Result
: Interfaces
.C
.int
;
971 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
972 pragma Assert
(Result
= 0);
979 procedure Initialize
(S
: in out Suspension_Object
) is
980 Result
: Interfaces
.C
.int
;
983 -- Initialize internal state (always to False (RM D.10(6)))
988 -- Initialize internal mutex
990 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
992 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
994 if Result
= ENOMEM
then
998 -- Initialize internal condition variable
1000 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1002 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1005 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1006 pragma Assert
(Result
= 0);
1008 if Result
= ENOMEM
then
1009 raise Storage_Error
;
1018 procedure Finalize
(S
: in out Suspension_Object
) is
1019 Result
: Interfaces
.C
.int
;
1022 -- Destroy internal mutex
1024 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1025 pragma Assert
(Result
= 0);
1027 -- Destroy internal condition variable
1029 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1030 pragma Assert
(Result
= 0);
1037 function Current_State
(S
: Suspension_Object
) return Boolean is
1039 -- We do not want to use lock on this read operation. State is marked
1040 -- as Atomic so that we ensure that the value retrieved is correct.
1049 procedure Set_False
(S
: in out Suspension_Object
) is
1050 Result
: Interfaces
.C
.int
;
1053 SSL
.Abort_Defer
.all;
1055 Result
:= pthread_mutex_lock
(S
.L
'Access);
1056 pragma Assert
(Result
= 0);
1060 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1061 pragma Assert
(Result
= 0);
1063 SSL
.Abort_Undefer
.all;
1070 procedure Set_True
(S
: in out Suspension_Object
) is
1071 Result
: Interfaces
.C
.int
;
1074 SSL
.Abort_Defer
.all;
1076 Result
:= pthread_mutex_lock
(S
.L
'Access);
1077 pragma Assert
(Result
= 0);
1079 -- If there is already a task waiting on this suspension object then
1080 -- we resume it, leaving the state of the suspension object to False,
1081 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1082 -- the state to True.
1088 Result
:= pthread_cond_signal
(S
.CV
'Access);
1089 pragma Assert
(Result
= 0);
1095 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1096 pragma Assert
(Result
= 0);
1098 SSL
.Abort_Undefer
.all;
1101 ------------------------
1102 -- Suspend_Until_True --
1103 ------------------------
1105 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1106 Result
: Interfaces
.C
.int
;
1109 SSL
.Abort_Defer
.all;
1111 Result
:= pthread_mutex_lock
(S
.L
'Access);
1112 pragma Assert
(Result
= 0);
1116 -- Program_Error must be raised upon calling Suspend_Until_True
1117 -- if another task is already waiting on that suspension object
1120 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1121 pragma Assert
(Result
= 0);
1123 SSL
.Abort_Undefer
.all;
1125 raise Program_Error
;
1127 -- Suspend the task if the state is False. Otherwise, the task
1128 -- continues its execution, and the state of the suspension object
1129 -- is set to False (ARM D.10 par. 9).
1135 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1138 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1139 pragma Assert
(Result
= 0);
1141 SSL
.Abort_Undefer
.all;
1144 end Suspend_Until_True
;
1152 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1153 pragma Unreferenced
(Self_ID
);
1158 --------------------
1159 -- Check_No_Locks --
1160 --------------------
1162 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1163 pragma Unreferenced
(Self_ID
);
1168 ----------------------
1169 -- Environment_Task --
1170 ----------------------
1172 function Environment_Task
return Task_Id
is
1174 return Environment_Task_Id
;
1175 end Environment_Task
;
1181 function Suspend_Task
1183 Thread_Self
: Thread_Id
) return Boolean
1186 if T
.Common
.LL
.Thread
/= Thread_Self
then
1187 return pthread_kill
(T
.Common
.LL
.Thread
, SIGSTOP
) = 0;
1197 function Resume_Task
1199 Thread_Self
: Thread_Id
) return Boolean
1202 if T
.Common
.LL
.Thread
/= Thread_Self
then
1203 return pthread_kill
(T
.Common
.LL
.Thread
, SIGCONT
) = 0;
1209 --------------------
1210 -- Stop_All_Tasks --
1211 --------------------
1213 procedure Stop_All_Tasks
is
1222 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1223 pragma Unreferenced
(T
);
1232 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1233 pragma Unreferenced
(T
);
1242 procedure Initialize
(Environment_Task
: Task_Id
) is
1243 act
: aliased struct_sigaction
;
1244 old_act
: aliased struct_sigaction
;
1245 Tmp_Set
: aliased sigset_t
;
1246 Result
: Interfaces
.C
.int
;
1249 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1250 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1251 -- Get interrupt state. Defined in a-init.c
1252 -- The input argument is the interrupt number,
1253 -- and the result is one of the following:
1255 Default
: constant Character := 's';
1256 -- 'n' this interrupt not set by any Interrupt_State pragma
1257 -- 'u' Interrupt_State pragma set state to User
1258 -- 'r' Interrupt_State pragma set state to Runtime
1259 -- 's' Interrupt_State pragma set state to System (use "default"
1263 Environment_Task_Id
:= Environment_Task
;
1265 Interrupt_Management
.Initialize
;
1267 -- Prepare the set of signals that should be unblocked in all tasks
1269 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1270 pragma Assert
(Result
= 0);
1272 for J
in Interrupt_Management
.Interrupt_ID
loop
1273 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1274 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1275 pragma Assert
(Result
= 0);
1279 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1280 pragma Assert
(Result
= 0);
1282 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1283 pragma Assert
(Result
= 0);
1285 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1287 -- Initialize the global RTS lock
1289 Specific
.Initialize
(Environment_Task
);
1291 Enter_Task
(Environment_Task
);
1293 -- Install the abort-signal handler
1296 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1299 act
.sa_handler
:= Abort_Handler
'Address;
1301 Result
:= sigemptyset
(Tmp_Set
'Access);
1302 pragma Assert
(Result
= 0);
1303 act
.sa_mask
:= Tmp_Set
;
1307 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
1308 act
'Unchecked_Access,
1309 old_act
'Unchecked_Access);
1310 pragma Assert
(Result
= 0);
1314 end System
.Task_Primitives
.Operations
;