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-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 LynxOS version of this file, adapted to make SCHED_FIFO and
33 -- ceiling locking (Annex D compliance) work properly.
35 -- This package contains all the GNULL primitives that interface directly with
39 -- Turn off polling, we do not want ATC polling to take place during tasking
40 -- operations. It causes infinite loops and other problems.
42 with Ada
.Unchecked_Deallocation
;
46 with System
.Tasking
.Debug
;
47 with System
.Interrupt_Management
;
48 with System
.OS_Primitives
;
49 with System
.Task_Info
;
51 with System
.Soft_Links
;
52 -- We use System.Soft_Links instead of System.Tasking.Initialization
53 -- because the later is a higher level package that we shouldn't depend on.
54 -- For example when using the restricted run time, it is replaced by
55 -- System.Tasking.Restricted.Stages.
57 package body System
.Task_Primitives
.Operations
is
59 package SSL
renames System
.Soft_Links
;
61 use System
.Tasking
.Debug
;
64 use System
.OS_Interface
;
65 use System
.Parameters
;
66 use System
.OS_Primitives
;
72 -- The followings are logically constants, but need to be initialized
75 Single_RTS_Lock
: aliased RTS_Lock
;
76 -- This is a lock to allow only one thread of control in the RTS at
77 -- a time; it is used to execute in mutual exclusion from all other tasks.
78 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
80 ATCB_Key
: aliased pthread_key_t
;
81 -- Key used to find the Ada Task_Id associated with a thread
83 Environment_Task_Id
: Task_Id
;
84 -- A variable to hold Task_Id for the environment task
86 Locking_Policy
: Character;
87 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
88 -- Value of the pragma Locking_Policy:
89 -- 'C' for Ceiling_Locking
90 -- 'I' for Inherit_Locking
93 Unblocked_Signal_Mask
: aliased sigset_t
;
94 -- The set of signals that should unblocked in all tasks
96 -- The followings are internal configuration constants needed
98 Next_Serial_Number
: Task_Serial_Number
:= 100;
99 -- We start at 100, to reserve some special values for
100 -- using in error checking.
102 Time_Slice_Val
: Integer;
103 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
105 Dispatching_Policy
: Character;
106 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
108 Foreign_Task_Elaborated
: aliased Boolean := True;
109 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 procedure Initialize
(Environment_Task
: Task_Id
);
118 pragma Inline
(Initialize
);
119 -- Initialize various data needed by this package
121 function Is_Valid_Task
return Boolean;
122 pragma Inline
(Is_Valid_Task
);
123 -- Does the current thread have an ATCB?
125 procedure Set
(Self_Id
: Task_Id
);
127 -- Set the self id for the current task
129 function Self
return Task_Id
;
130 pragma Inline
(Self
);
131 -- Return a pointer to the Ada Task Control Block of the calling task
135 package body Specific
is separate;
136 -- The body of this package is target specific
138 ---------------------------------
139 -- Support for foreign threads --
140 ---------------------------------
142 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
143 -- Allocate and Initialize a new ATCB for the current Thread
145 function Register_Foreign_Thread
146 (Thread
: Thread_Id
) return Task_Id
is separate;
148 -----------------------
149 -- Local Subprograms --
150 -----------------------
152 procedure Abort_Handler
(Sig
: Signal
);
153 -- Signal handler used to implement asynchronous abort
155 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
);
156 -- This procedure calls the scheduler of the OS to set thread's priority
162 procedure Abort_Handler
(Sig
: Signal
) is
163 pragma Unreferenced
(Sig
);
165 T
: constant Task_Id
:= Self
;
166 Result
: Interfaces
.C
.int
;
167 Old_Set
: aliased sigset_t
;
170 -- It is not safe to raise an exception when using ZCX and the GCC
171 -- exception handling mechanism.
173 if ZCX_By_Default
and then GCC_ZCX_Support
then
177 if T
.Deferral_Level
= 0
178 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
179 and then not T
.Aborting
183 -- Make sure signals used for RTS internal purpose are unmasked
188 Unblocked_Signal_Mask
'Access,
190 pragma Assert
(Result
= 0);
192 raise Standard
'Abort_Signal;
200 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
201 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
202 Guard_Page_Address
: Address
;
204 Res
: Interfaces
.C
.int
;
207 if Stack_Base_Available
then
209 -- Compute the guard page address
211 Guard_Page_Address
:=
212 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
215 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
217 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
220 pragma Assert
(Res
= 0);
228 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
230 return T
.Common
.LL
.Thread
;
237 function Self
return Task_Id
renames Specific
.Self
;
239 ---------------------
240 -- Initialize_Lock --
241 ---------------------
243 procedure Initialize_Lock
244 (Prio
: System
.Any_Priority
;
245 L
: not null access Lock
)
247 Attributes
: aliased pthread_mutexattr_t
;
248 Result
: Interfaces
.C
.int
;
251 Result
:= pthread_mutexattr_init
(Attributes
'Access);
252 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
254 if Result
= ENOMEM
then
258 if Locking_Policy
= 'C' then
262 Result
:= pthread_mutex_init
(L
.Mutex
'Access, Attributes
'Access);
263 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
265 if Result
= ENOMEM
then
269 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
270 pragma Assert
(Result
= 0);
273 procedure Initialize_Lock
274 (L
: not null access RTS_Lock
;
277 pragma Unreferenced
(Level
);
279 Attributes
: aliased pthread_mutexattr_t
;
280 Result
: Interfaces
.C
.int
;
283 Result
:= pthread_mutexattr_init
(Attributes
'Access);
284 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
286 if Result
= ENOMEM
then
290 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
291 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
293 if Result
= ENOMEM
then
294 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
298 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
299 pragma Assert
(Result
= 0);
306 procedure Finalize_Lock
(L
: not null access Lock
) is
307 Result
: Interfaces
.C
.int
;
309 Result
:= pthread_mutex_destroy
(L
.Mutex
'Access);
310 pragma Assert
(Result
= 0);
313 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
314 Result
: Interfaces
.C
.int
;
316 Result
:= pthread_mutex_destroy
(L
);
317 pragma Assert
(Result
= 0);
325 (L
: not null access Lock
;
326 Ceiling_Violation
: out Boolean)
328 Result
: Interfaces
.C
.int
;
329 T
: constant Task_Id
:= Self
;
332 if Locking_Policy
= 'C' then
333 if T
.Common
.Current_Priority
> L
.Ceiling
then
334 Ceiling_Violation
:= True;
338 L
.Saved_Priority
:= T
.Common
.Current_Priority
;
340 if T
.Common
.Current_Priority
< L
.Ceiling
then
341 Set_OS_Priority
(T
, L
.Ceiling
);
345 Result
:= pthread_mutex_lock
(L
.Mutex
'Access);
347 -- Assume that the cause of EINVAL is a priority ceiling violation
349 Ceiling_Violation
:= (Result
= EINVAL
);
350 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
353 -- No tricks on RTS_Locks
356 (L
: not null access RTS_Lock
;
357 Global_Lock
: Boolean := False)
359 Result
: Interfaces
.C
.int
;
361 if not Single_Lock
or else Global_Lock
then
362 Result
:= pthread_mutex_lock
(L
);
363 pragma Assert
(Result
= 0);
367 procedure Write_Lock
(T
: Task_Id
) is
368 Result
: Interfaces
.C
.int
;
370 if not Single_Lock
then
371 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
372 pragma Assert
(Result
= 0);
381 (L
: not null access Lock
;
382 Ceiling_Violation
: out Boolean)
385 Write_Lock
(L
, Ceiling_Violation
);
392 procedure Unlock
(L
: not null access Lock
) is
393 Result
: Interfaces
.C
.int
;
394 T
: constant Task_Id
:= Self
;
397 Result
:= pthread_mutex_unlock
(L
.Mutex
'Access);
398 pragma Assert
(Result
= 0);
400 if Locking_Policy
= 'C' then
401 if T
.Common
.Current_Priority
> L
.Saved_Priority
then
402 Set_OS_Priority
(T
, L
.Saved_Priority
);
408 (L
: not null access RTS_Lock
;
409 Global_Lock
: Boolean := False)
411 Result
: Interfaces
.C
.int
;
413 if not Single_Lock
or else Global_Lock
then
414 Result
:= pthread_mutex_unlock
(L
);
415 pragma Assert
(Result
= 0);
419 procedure Unlock
(T
: Task_Id
) is
420 Result
: Interfaces
.C
.int
;
422 if not Single_Lock
then
423 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
424 pragma Assert
(Result
= 0);
432 -- Dynamic priority ceilings are not supported by the underlying system
434 procedure Set_Ceiling
435 (L
: not null access Lock
;
436 Prio
: System
.Any_Priority
)
438 pragma Unreferenced
(L
, Prio
);
449 Reason
: System
.Tasking
.Task_States
)
451 pragma Unreferenced
(Reason
);
452 Result
: Interfaces
.C
.int
;
458 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
462 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
465 -- EINTR is not considered a failure
467 pragma Assert
(Result
= 0 or else Result
= EINTR
);
474 -- This is for use within the run-time system, so abort is
475 -- assumed to be already deferred, and the caller should be
476 -- holding its own ATCB lock.
478 procedure Timed_Sleep
481 Mode
: ST
.Delay_Modes
;
482 Reason
: Task_States
;
483 Timedout
: out Boolean;
484 Yielded
: out Boolean)
486 pragma Unreferenced
(Reason
);
488 Base_Time
: constant Duration := Monotonic_Clock
;
489 Check_Time
: Duration := Base_Time
;
492 Request
: aliased timespec
;
493 Result
: Interfaces
.C
.int
;
499 if Mode
= Relative
then
500 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
502 if Relative_Timed_Wait
then
503 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
507 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
509 if Relative_Timed_Wait
then
510 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
514 if Abs_Time
> Check_Time
then
515 if Relative_Timed_Wait
then
516 Request
:= To_Timespec
(Rel_Time
);
518 Request
:= To_Timespec
(Abs_Time
);
522 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
526 pthread_cond_timedwait
527 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
532 pthread_cond_timedwait
533 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
537 Check_Time
:= Monotonic_Clock
;
538 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
540 if Result
= 0 or Result
= EINTR
then
542 -- Somebody may have called Wakeup for us
548 pragma Assert
(Result
= ETIMEDOUT
);
557 -- This is for use in implementing delay statements, so we assume
558 -- the caller is abort-deferred but is holding no locks.
560 procedure Timed_Delay
563 Mode
: ST
.Delay_Modes
)
565 Base_Time
: constant Duration := Monotonic_Clock
;
566 Check_Time
: Duration := Base_Time
;
569 Request
: aliased timespec
;
571 Result
: Interfaces
.C
.int
;
572 pragma Warnings
(Off
, Result
);
579 -- Comments needed in code below ???
581 Write_Lock
(Self_ID
);
583 if Mode
= Relative
then
584 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
586 if Relative_Timed_Wait
then
587 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
591 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
593 if Relative_Timed_Wait
then
594 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
598 if Abs_Time
> Check_Time
then
599 if Relative_Timed_Wait
then
600 Request
:= To_Timespec
(Rel_Time
);
602 Request
:= To_Timespec
(Abs_Time
);
605 Self_ID
.Common
.State
:= Delay_Sleep
;
608 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
612 pthread_cond_timedwait
613 (Self_ID
.Common
.LL
.CV
'Access,
614 Single_RTS_Lock
'Access,
618 pthread_cond_timedwait
619 (Self_ID
.Common
.LL
.CV
'Access,
620 Self_ID
.Common
.LL
.L
'Access,
624 Check_Time
:= Monotonic_Clock
;
625 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
627 pragma Assert
(Result
= 0 or else
628 Result
= ETIMEDOUT
or else
632 Self_ID
.Common
.State
:= Runnable
;
641 Result
:= sched_yield
;
644 ---------------------
645 -- Monotonic_Clock --
646 ---------------------
648 function Monotonic_Clock
return Duration is
649 TS
: aliased timespec
;
650 Result
: Interfaces
.C
.int
;
654 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
655 pragma Assert
(Result
= 0);
656 return To_Duration
(TS
);
663 function RT_Resolution
return Duration is
664 Res
: aliased timespec
;
665 Result
: Interfaces
.C
.int
;
669 (clock_id
=> CLOCK_REALTIME
, res
=> Res
'Unchecked_Access);
670 pragma Assert
(Result
= 0);
671 return To_Duration
(Res
);
678 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
679 pragma Unreferenced
(Reason
);
680 Result
: Interfaces
.C
.int
;
682 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
683 pragma Assert
(Result
= 0);
690 procedure Yield
(Do_Yield
: Boolean := True) is
691 Result
: Interfaces
.C
.int
;
692 pragma Unreferenced
(Result
);
695 Result
:= sched_yield
;
703 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
) is
704 Result
: Interfaces
.C
.int
;
705 Param
: aliased struct_sched_param
;
707 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
708 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
709 -- Get priority specific dispatching policy
711 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
712 -- Upper case first character of the policy name corresponding to the
713 -- task as set by a Priority_Specific_Dispatching pragma.
716 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
718 if Time_Slice_Supported
719 and then (Dispatching_Policy
= 'R'
720 or else Priority_Specific_Policy
= 'R'
721 or else Time_Slice_Val
> 0)
724 pthread_setschedparam
725 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
727 elsif Dispatching_Policy
= 'F'
728 or else Priority_Specific_Policy
= 'F'
729 or else Time_Slice_Val
= 0
732 pthread_setschedparam
733 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
737 pthread_setschedparam
738 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
741 pragma Assert
(Result
= 0);
744 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
745 pragma Atomic_Components
(Prio_Array_Type
);
746 Prio_Array
: Prio_Array_Type
;
747 -- Comments needed for these declarations ???
749 procedure Set_Priority
751 Prio
: System
.Any_Priority
;
752 Loss_Of_Inheritance
: Boolean := False)
754 Array_Item
: Integer;
757 Set_OS_Priority
(T
, Prio
);
759 if Locking_Policy
= 'C' then
761 -- Annex D requirements: loss of inheritance puts task at the start
762 -- of the queue for that prio; copied from 5ztaprop (VxWorks).
764 if Loss_Of_Inheritance
765 and then Prio
< T
.Common
.Current_Priority
then
767 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
768 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
772 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
773 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
776 Prio_Array
(T
.Common
.Base_Priority
) :=
777 Prio_Array
(T
.Common
.Base_Priority
) - 1;
781 T
.Common
.Current_Priority
:= Prio
;
788 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
790 return T
.Common
.Current_Priority
;
797 procedure Enter_Task
(Self_ID
: Task_Id
) is
799 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
800 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
802 Specific
.Set
(Self_ID
);
806 for J
in Known_Tasks
'Range loop
807 if Known_Tasks
(J
) = null then
808 Known_Tasks
(J
) := Self_ID
;
809 Self_ID
.Known_Tasks_Index
:= J
;
821 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
823 return new Ada_Task_Control_Block
(Entry_Num
);
830 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
832 -----------------------------
833 -- Register_Foreign_Thread --
834 -----------------------------
836 function Register_Foreign_Thread
return Task_Id
is
838 if Is_Valid_Task
then
841 return Register_Foreign_Thread
(pthread_self
);
843 end Register_Foreign_Thread
;
849 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
850 Mutex_Attr
: aliased pthread_mutexattr_t
;
851 Result
: Interfaces
.C
.int
;
852 Cond_Attr
: aliased pthread_condattr_t
;
855 -- Give the task a unique serial number
857 Self_ID
.Serial_Number
:= Next_Serial_Number
;
858 Next_Serial_Number
:= Next_Serial_Number
+ 1;
859 pragma Assert
(Next_Serial_Number
/= 0);
861 if not Single_Lock
then
862 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
863 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
868 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
869 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
877 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
878 pragma Assert
(Result
= 0);
881 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
882 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
886 pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
887 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
893 if not Single_Lock
then
894 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
895 pragma Assert
(Result
= 0);
901 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
902 pragma Assert
(Result
= 0);
909 procedure Create_Task
911 Wrapper
: System
.Address
;
912 Stack_Size
: System
.Parameters
.Size_Type
;
913 Priority
: System
.Any_Priority
;
914 Succeeded
: out Boolean)
916 Attributes
: aliased pthread_attr_t
;
917 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
918 Result
: Interfaces
.C
.int
;
920 use System
.Task_Info
;
923 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
925 if Stack_Base_Available
then
927 -- If Stack Checking is supported then allocate 2 additional pages:
929 -- In the worst case, stack is allocated at something like
930 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
931 -- to be sure the effective stack size is greater than what
934 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
937 Result
:= pthread_attr_init
(Attributes
'Access);
938 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
946 pthread_attr_setdetachstate
947 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
948 pragma Assert
(Result
= 0);
951 pthread_attr_setstacksize
952 (Attributes
'Access, Adjusted_Stack_Size
);
953 pragma Assert
(Result
= 0);
955 if T
.Common
.Task_Info
/= Default_Scope
then
957 -- We are assuming that Scope_Type has the same values than the
958 -- corresponding C macros
961 pthread_attr_setscope
962 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
963 pragma Assert
(Result
= 0);
966 -- Since the initial signal mask of a thread is inherited from the
967 -- creator, and the Environment task has all its signals masked, we
968 -- do not need to manipulate caller's signal mask at this point.
969 -- All tasks in RTS will have All_Tasks_Mask initially.
973 (T
.Common
.LL
.Thread
'Access,
975 Thread_Body_Access
(Wrapper
),
977 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
979 Succeeded
:= Result
= 0;
981 Result
:= pthread_attr_destroy
(Attributes
'Access);
982 pragma Assert
(Result
= 0);
985 Set_Priority
(T
, Priority
);
993 procedure Finalize_TCB
(T
: Task_Id
) is
994 Result
: Interfaces
.C
.int
;
996 Is_Self
: constant Boolean := T
= Self
;
998 procedure Free
is new
999 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
1002 if not Single_Lock
then
1003 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
1004 pragma Assert
(Result
= 0);
1007 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
1008 pragma Assert
(Result
= 0);
1010 if T
.Known_Tasks_Index
/= -1 then
1011 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1017 Result
:= st_setspecific
(ATCB_Key
, System
.Null_Address
);
1018 pragma Assert
(Result
= 0);
1026 procedure Exit_Task
is
1028 Specific
.Set
(null);
1035 procedure Abort_Task
(T
: Task_Id
) is
1036 Result
: Interfaces
.C
.int
;
1040 (T
.Common
.LL
.Thread
,
1041 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1042 pragma Assert
(Result
= 0);
1049 procedure Initialize
(S
: in out Suspension_Object
) is
1050 Mutex_Attr
: aliased pthread_mutexattr_t
;
1051 Cond_Attr
: aliased pthread_condattr_t
;
1052 Result
: Interfaces
.C
.int
;
1055 -- Initialize internal state (always to False (RM D.10(6)))
1060 -- Initialize internal mutex
1062 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1063 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1065 if Result
= ENOMEM
then
1066 raise Storage_Error
;
1069 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1070 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1072 if Result
= ENOMEM
then
1073 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1074 pragma Assert
(Result
= 0);
1076 raise Storage_Error
;
1079 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1080 pragma Assert
(Result
= 0);
1082 -- Initialize internal condition variable
1084 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1085 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1088 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1089 pragma Assert
(Result
= 0);
1091 if Result
= ENOMEM
then
1092 raise Storage_Error
;
1096 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1097 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1100 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1101 pragma Assert
(Result
= 0);
1103 if Result
= ENOMEM
then
1104 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1105 pragma Assert
(Result
= 0);
1107 raise Storage_Error
;
1111 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1112 pragma Assert
(Result
= 0);
1119 procedure Finalize
(S
: in out Suspension_Object
) is
1120 Result
: Interfaces
.C
.int
;
1123 -- Destroy internal mutex
1125 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1126 pragma Assert
(Result
= 0);
1128 -- Destroy internal condition variable
1130 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1131 pragma Assert
(Result
= 0);
1138 function Current_State
(S
: Suspension_Object
) return Boolean is
1140 -- We do not want to use lock on this read operation. State is marked
1141 -- as Atomic so that we ensure that the value retrieved is correct.
1150 procedure Set_False
(S
: in out Suspension_Object
) is
1151 Result
: Interfaces
.C
.int
;
1154 SSL
.Abort_Defer
.all;
1156 Result
:= pthread_mutex_lock
(S
.L
'Access);
1157 pragma Assert
(Result
= 0);
1161 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1162 pragma Assert
(Result
= 0);
1164 SSL
.Abort_Undefer
.all;
1171 procedure Set_True
(S
: in out Suspension_Object
) is
1172 Result
: Interfaces
.C
.int
;
1175 SSL
.Abort_Defer
.all;
1177 Result
:= pthread_mutex_lock
(S
.L
'Access);
1178 pragma Assert
(Result
= 0);
1180 -- If there is already a task waiting on this suspension object then
1181 -- we resume it, leaving the state of the suspension object to False,
1182 -- as specified in (RM D.10(9)). Otherwise, just leave state set True.
1188 Result
:= pthread_cond_signal
(S
.CV
'Access);
1189 pragma Assert
(Result
= 0);
1195 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1196 pragma Assert
(Result
= 0);
1198 SSL
.Abort_Undefer
.all;
1201 ------------------------
1202 -- Suspend_Until_True --
1203 ------------------------
1205 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1206 Result
: Interfaces
.C
.int
;
1209 SSL
.Abort_Defer
.all;
1211 Result
:= pthread_mutex_lock
(S
.L
'Access);
1212 pragma Assert
(Result
= 0);
1216 -- Program_Error must be raised upon calling Suspend_Until_True
1217 -- if another task is already waiting on that suspension object
1220 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1221 pragma Assert
(Result
= 0);
1223 SSL
.Abort_Undefer
.all;
1225 raise Program_Error
;
1228 -- Suspend the task if the state is False. Otherwise, the task
1229 -- continues its execution, and the state of the suspension object
1230 -- is set to False (RM D.10(9)).
1236 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1239 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1240 pragma Assert
(Result
= 0);
1242 SSL
.Abort_Undefer
.all;
1244 end Suspend_Until_True
;
1252 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1253 pragma Unreferenced
(Self_ID
);
1258 --------------------
1259 -- Check_No_Locks --
1260 --------------------
1262 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1263 pragma Unreferenced
(Self_ID
);
1268 ----------------------
1269 -- Environment_Task --
1270 ----------------------
1272 function Environment_Task
return Task_Id
is
1274 return Environment_Task_Id
;
1275 end Environment_Task
;
1281 procedure Lock_RTS
is
1283 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1290 procedure Unlock_RTS
is
1292 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1299 function Suspend_Task
1301 Thread_Self
: Thread_Id
) return Boolean
1303 pragma Unreferenced
(T
);
1304 pragma Unreferenced
(Thread_Self
);
1313 function Resume_Task
1315 Thread_Self
: Thread_Id
) return Boolean
1317 pragma Unreferenced
(T
);
1318 pragma Unreferenced
(Thread_Self
);
1323 --------------------
1324 -- Stop_All_Tasks --
1325 --------------------
1327 procedure Stop_All_Tasks
is
1336 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1337 pragma Unreferenced
(T
);
1346 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1347 pragma Unreferenced
(T
);
1356 procedure Initialize
(Environment_Task
: Task_Id
) is
1357 act
: aliased struct_sigaction
;
1358 old_act
: aliased struct_sigaction
;
1359 Tmp_Set
: aliased sigset_t
;
1360 Result
: Interfaces
.C
.int
;
1363 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1364 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1365 -- Get interrupt state. Defined in a-init.c
1366 -- The input argument is the interrupt number,
1367 -- and the result is one of the following:
1369 Default
: constant Character := 's';
1370 -- 'n' this interrupt not set by any Interrupt_State pragma
1371 -- 'u' Interrupt_State pragma set state to User
1372 -- 'r' Interrupt_State pragma set state to Runtime
1373 -- 's' Interrupt_State pragma set state to System (use "default"
1377 Environment_Task_Id
:= Environment_Task
;
1379 Interrupt_Management
.Initialize
;
1381 -- Prepare the set of signals that should unblocked in all tasks
1383 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1384 pragma Assert
(Result
= 0);
1386 for J
in Interrupt_Management
.Interrupt_ID
loop
1387 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1388 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1389 pragma Assert
(Result
= 0);
1393 -- Initialize the lock used to synchronize chain of all ATCBs
1395 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1397 Specific
.Initialize
(Environment_Task
);
1399 Enter_Task
(Environment_Task
);
1401 -- Install the abort-signal handler
1404 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1407 act
.sa_handler
:= Abort_Handler
'Address;
1409 Result
:= sigemptyset
(Tmp_Set
'Access);
1410 pragma Assert
(Result
= 0);
1411 act
.sa_mask
:= Tmp_Set
;
1415 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1416 act
'Unchecked_Access,
1417 old_act
'Unchecked_Access);
1419 pragma Assert
(Result
= 0);
1423 end System
.Task_Primitives
.Operations
;