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-2005, 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 LynxOS version of this file, adapted to make
35 -- SCHED_FIFO and ceiling locking (Annex D compliance) work properly
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 -- used for Known_Tasks
47 with System
.Interrupt_Management
;
48 -- used for Keep_Unmasked
49 -- Abort_Task_Interrupt
52 with System
.OS_Primitives
;
53 -- used for Delay_Modes
55 with System
.Task_Info
;
56 -- used for Task_Info_Type
62 with System
.Parameters
;
65 with Unchecked_Deallocation
;
67 package body System
.Task_Primitives
.Operations
is
69 use System
.Tasking
.Debug
;
72 use System
.OS_Interface
;
73 use System
.Parameters
;
74 use System
.OS_Primitives
;
80 -- The followings are logically constants, but need to be initialized
83 Single_RTS_Lock
: aliased RTS_Lock
;
84 -- This is a lock to allow only one thread of control in the RTS at
85 -- a time; it is used to execute in mutual exclusion from all other tasks.
86 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
88 ATCB_Key
: aliased pthread_key_t
;
89 -- Key used to find the Ada Task_Id associated with a thread
91 Environment_Task_Id
: Task_Id
;
92 -- A variable to hold Task_Id for the environment task
94 Locking_Policy
: Character;
95 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
96 -- Value of the pragma Locking_Policy:
97 -- 'C' for Ceiling_Locking
98 -- 'I' for Inherit_Locking
101 Unblocked_Signal_Mask
: aliased sigset_t
;
102 -- The set of signals that should unblocked in all tasks
104 -- The followings are internal configuration constants needed
106 Next_Serial_Number
: Task_Serial_Number
:= 100;
107 -- We start at 100, to reserve some special values for
108 -- using in error checking.
110 Time_Slice_Val
: Integer;
111 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
113 Dispatching_Policy
: Character;
114 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
116 Foreign_Task_Elaborated
: aliased Boolean := True;
117 -- Used to identified fake tasks (i.e., non-Ada Threads)
125 procedure Initialize
(Environment_Task
: Task_Id
);
126 pragma Inline
(Initialize
);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task
return Boolean;
130 pragma Inline
(Is_Valid_Task
);
131 -- Does the current thread have an ATCB?
133 procedure Set
(Self_Id
: Task_Id
);
135 -- Set the self id for the current task
137 function Self
return Task_Id
;
138 pragma Inline
(Self
);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific
is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread
: Thread_Id
) return Task_Id
is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 procedure Abort_Handler
(Sig
: Signal
);
161 -- Signal handler used to implement asynchronous abort
163 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
);
164 -- This procedure calls the scheduler of the OS to set thread's priority
170 procedure Abort_Handler
(Sig
: Signal
) is
171 pragma Unreferenced
(Sig
);
173 T
: constant Task_Id
:= Self
;
174 Result
: Interfaces
.C
.int
;
175 Old_Set
: aliased sigset_t
;
178 -- It is not safe to raise an exception when using ZCX and the GCC
179 -- exception handling mechanism.
181 if ZCX_By_Default
and then GCC_ZCX_Support
then
185 if T
.Deferral_Level
= 0
186 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
191 -- Make sure signals used for RTS internal purpose are unmasked
194 pthread_sigmask
(SIG_UNBLOCK
,
195 Unblocked_Signal_Mask
'Unchecked_Access,
196 Old_Set
'Unchecked_Access);
197 pragma Assert
(Result
= 0);
199 raise Standard
'Abort_Signal;
207 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
208 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
209 Guard_Page_Address
: Address
;
211 Res
: Interfaces
.C
.int
;
214 if Stack_Base_Available
then
216 -- Compute the guard page address
218 Guard_Page_Address
:=
219 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
222 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
224 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
227 pragma Assert
(Res
= 0);
235 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
237 return T
.Common
.LL
.Thread
;
244 function Self
return Task_Id
renames Specific
.Self
;
246 ---------------------
247 -- Initialize_Lock --
248 ---------------------
250 procedure Initialize_Lock
251 (Prio
: System
.Any_Priority
;
254 Attributes
: aliased pthread_mutexattr_t
;
255 Result
: Interfaces
.C
.int
;
258 Result
:= pthread_mutexattr_init
(Attributes
'Access);
259 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
261 if Result
= ENOMEM
then
265 if Locking_Policy
= 'C' then
269 Result
:= pthread_mutex_init
(L
.Mutex
'Access, Attributes
'Access);
270 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
272 if Result
= ENOMEM
then
276 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
277 pragma Assert
(Result
= 0);
280 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
281 pragma Unreferenced
(Level
);
283 Attributes
: aliased pthread_mutexattr_t
;
284 Result
: Interfaces
.C
.int
;
287 Result
:= pthread_mutexattr_init
(Attributes
'Access);
288 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
290 if Result
= ENOMEM
then
294 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
295 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
297 if Result
= ENOMEM
then
298 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
302 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
303 pragma Assert
(Result
= 0);
310 procedure Finalize_Lock
(L
: access Lock
) is
311 Result
: Interfaces
.C
.int
;
313 Result
:= pthread_mutex_destroy
(L
.Mutex
'Access);
314 pragma Assert
(Result
= 0);
317 procedure Finalize_Lock
(L
: access RTS_Lock
) is
318 Result
: Interfaces
.C
.int
;
320 Result
:= pthread_mutex_destroy
(L
);
321 pragma Assert
(Result
= 0);
328 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
329 Result
: Interfaces
.C
.int
;
330 T
: constant Task_Id
:= Self
;
333 if Locking_Policy
= 'C' then
334 if T
.Common
.Current_Priority
> L
.Ceiling
then
335 Ceiling_Violation
:= True;
339 L
.Saved_Priority
:= T
.Common
.Current_Priority
;
341 if T
.Common
.Current_Priority
< L
.Ceiling
then
342 Set_OS_Priority
(T
, L
.Ceiling
);
346 Result
:= pthread_mutex_lock
(L
.Mutex
'Access);
348 -- Assume that the cause of EINVAL is a priority ceiling violation
350 Ceiling_Violation
:= (Result
= EINVAL
);
351 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
354 -- No tricks on RTS_Locks
357 (L
: access RTS_Lock
; 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);
380 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
382 Write_Lock
(L
, Ceiling_Violation
);
389 procedure Unlock
(L
: access Lock
) is
390 Result
: Interfaces
.C
.int
;
391 T
: constant Task_Id
:= Self
;
394 Result
:= pthread_mutex_unlock
(L
.Mutex
'Access);
395 pragma Assert
(Result
= 0);
397 if Locking_Policy
= 'C' then
398 if T
.Common
.Current_Priority
> L
.Saved_Priority
then
399 Set_OS_Priority
(T
, L
.Saved_Priority
);
404 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
405 Result
: Interfaces
.C
.int
;
407 if not Single_Lock
or else Global_Lock
then
408 Result
:= pthread_mutex_unlock
(L
);
409 pragma Assert
(Result
= 0);
413 procedure Unlock
(T
: Task_Id
) is
414 Result
: Interfaces
.C
.int
;
416 if not Single_Lock
then
417 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
418 pragma Assert
(Result
= 0);
428 Reason
: System
.Tasking
.Task_States
)
430 pragma Unreferenced
(Reason
);
431 Result
: Interfaces
.C
.int
;
435 Result
:= pthread_cond_wait
436 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
438 Result
:= pthread_cond_wait
439 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
442 -- EINTR is not considered a failure
444 pragma Assert
(Result
= 0 or else Result
= EINTR
);
451 -- This is for use within the run-time system, so abort is
452 -- assumed to be already deferred, and the caller should be
453 -- holding its own ATCB lock.
455 procedure Timed_Sleep
458 Mode
: ST
.Delay_Modes
;
459 Reason
: Task_States
;
460 Timedout
: out Boolean;
461 Yielded
: out Boolean)
463 pragma Unreferenced
(Reason
);
465 Check_Time
: constant Duration := Monotonic_Clock
;
468 Request
: aliased timespec
;
469 Result
: Interfaces
.C
.int
;
475 if Mode
= Relative
then
476 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
478 if Relative_Timed_Wait
then
479 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
483 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
485 if Relative_Timed_Wait
then
486 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
490 if Abs_Time
> Check_Time
then
491 if Relative_Timed_Wait
then
492 Request
:= To_Timespec
(Rel_Time
);
494 Request
:= To_Timespec
(Abs_Time
);
498 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
499 or else Self_ID
.Pending_Priority_Change
;
502 Result
:= pthread_cond_timedwait
503 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
507 Result
:= pthread_cond_timedwait
508 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
512 exit when Abs_Time
<= Monotonic_Clock
;
514 if Result
= 0 or Result
= EINTR
then
516 -- Somebody may have called Wakeup for us
522 pragma Assert
(Result
= ETIMEDOUT
);
531 -- This is for use in implementing delay statements, so we assume
532 -- the caller is abort-deferred but is holding no locks.
534 procedure Timed_Delay
537 Mode
: ST
.Delay_Modes
)
539 Check_Time
: constant Duration := Monotonic_Clock
;
542 Request
: aliased timespec
;
543 Result
: Interfaces
.C
.int
;
550 -- Comments needed in code below ???
552 Write_Lock
(Self_ID
);
554 if Mode
= Relative
then
555 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
557 if Relative_Timed_Wait
then
558 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
562 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
564 if Relative_Timed_Wait
then
565 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
569 if Abs_Time
> Check_Time
then
570 if Relative_Timed_Wait
then
571 Request
:= To_Timespec
(Rel_Time
);
573 Request
:= To_Timespec
(Abs_Time
);
576 Self_ID
.Common
.State
:= Delay_Sleep
;
579 if Self_ID
.Pending_Priority_Change
then
580 Self_ID
.Pending_Priority_Change
:= False;
581 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
582 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
585 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
588 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
589 Single_RTS_Lock
'Access, Request
'Access);
591 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
592 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
595 exit when Abs_Time
<= Monotonic_Clock
;
597 pragma Assert
(Result
= 0
598 or else Result
= ETIMEDOUT
599 or else Result
= EINTR
);
602 Self_ID
.Common
.State
:= Runnable
;
611 Result
:= sched_yield
;
614 ---------------------
615 -- Monotonic_Clock --
616 ---------------------
618 function Monotonic_Clock
return Duration is
619 TS
: aliased timespec
;
620 Result
: Interfaces
.C
.int
;
622 Result
:= clock_gettime
623 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
624 pragma Assert
(Result
= 0);
625 return To_Duration
(TS
);
632 function RT_Resolution
return Duration is
633 Res
: aliased timespec
;
634 Result
: Interfaces
.C
.int
;
636 Result
:= clock_getres
637 (clock_id
=> CLOCK_REALTIME
, Res
=> Res
'Unchecked_Access);
638 pragma Assert
(Result
= 0);
639 return To_Duration
(Res
);
646 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
647 pragma Unreferenced
(Reason
);
648 Result
: Interfaces
.C
.int
;
650 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
651 pragma Assert
(Result
= 0);
658 procedure Yield
(Do_Yield
: Boolean := True) is
659 Result
: Interfaces
.C
.int
;
660 pragma Unreferenced
(Result
);
663 Result
:= sched_yield
;
671 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
) is
672 Result
: Interfaces
.C
.int
;
673 Param
: aliased struct_sched_param
;
676 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
678 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
679 Result
:= pthread_setschedparam
680 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
682 elsif Dispatching_Policy
= 'F' or else Time_Slice_Val
= 0 then
683 Result
:= pthread_setschedparam
684 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
687 Result
:= pthread_setschedparam
688 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
691 pragma Assert
(Result
= 0);
694 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
695 pragma Atomic_Components
(Prio_Array_Type
);
696 Prio_Array
: Prio_Array_Type
;
697 -- Comments needed for these declarations ???
699 procedure Set_Priority
701 Prio
: System
.Any_Priority
;
702 Loss_Of_Inheritance
: Boolean := False)
704 Array_Item
: Integer;
707 Set_OS_Priority
(T
, Prio
);
709 if Locking_Policy
= 'C' then
710 -- Annex D requirements: loss of inheritance puts task at the
711 -- beginning of the queue for that prio; copied from 5ztaprop
714 if Loss_Of_Inheritance
715 and then Prio
< T
.Common
.Current_Priority
then
717 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
718 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
722 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
723 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
726 Prio_Array
(T
.Common
.Base_Priority
) :=
727 Prio_Array
(T
.Common
.Base_Priority
) - 1;
731 T
.Common
.Current_Priority
:= Prio
;
738 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
740 return T
.Common
.Current_Priority
;
747 procedure Enter_Task
(Self_ID
: Task_Id
) is
749 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
750 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
752 Specific
.Set
(Self_ID
);
756 for J
in Known_Tasks
'Range loop
757 if Known_Tasks
(J
) = null then
758 Known_Tasks
(J
) := Self_ID
;
759 Self_ID
.Known_Tasks_Index
:= J
;
771 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
773 return new Ada_Task_Control_Block
(Entry_Num
);
780 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
782 -----------------------------
783 -- Register_Foreign_Thread --
784 -----------------------------
786 function Register_Foreign_Thread
return Task_Id
is
788 if Is_Valid_Task
then
791 return Register_Foreign_Thread
(pthread_self
);
793 end Register_Foreign_Thread
;
799 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
800 Mutex_Attr
: aliased pthread_mutexattr_t
;
801 Result
: Interfaces
.C
.int
;
802 Cond_Attr
: aliased pthread_condattr_t
;
805 -- Give the task a unique serial number
807 Self_ID
.Serial_Number
:= Next_Serial_Number
;
808 Next_Serial_Number
:= Next_Serial_Number
+ 1;
809 pragma Assert
(Next_Serial_Number
/= 0);
811 if not Single_Lock
then
812 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
813 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
816 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
818 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
826 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
827 pragma Assert
(Result
= 0);
830 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
831 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
834 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
836 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
842 if not Single_Lock
then
843 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
844 pragma Assert
(Result
= 0);
850 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
851 pragma Assert
(Result
= 0);
858 procedure Create_Task
860 Wrapper
: System
.Address
;
861 Stack_Size
: System
.Parameters
.Size_Type
;
862 Priority
: System
.Any_Priority
;
863 Succeeded
: out Boolean)
865 Attributes
: aliased pthread_attr_t
;
866 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
867 Result
: Interfaces
.C
.int
;
869 use System
.Task_Info
;
872 if Stack_Size
= Unspecified_Size
then
873 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
875 elsif Stack_Size
< Minimum_Stack_Size
then
876 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
879 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
882 if Stack_Base_Available
then
884 -- If Stack Checking is supported then allocate 2 additional pages:
886 -- In the worst case, stack is allocated at something like
887 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
888 -- to be sure the effective stack size is greater than what
891 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
894 Result
:= pthread_attr_init
(Attributes
'Access);
895 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
902 Result
:= pthread_attr_setdetachstate
903 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
904 pragma Assert
(Result
= 0);
906 Result
:= pthread_attr_setstacksize
907 (Attributes
'Access, Adjusted_Stack_Size
);
908 pragma Assert
(Result
= 0);
910 if T
.Common
.Task_Info
/= Default_Scope
then
912 -- We are assuming that Scope_Type has the same values than the
913 -- corresponding C macros
915 Result
:= pthread_attr_setscope
916 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
917 pragma Assert
(Result
= 0);
920 -- Since the initial signal mask of a thread is inherited from the
921 -- creator, and the Environment task has all its signals masked, we
922 -- do not need to manipulate caller's signal mask at this point.
923 -- All tasks in RTS will have All_Tasks_Mask initially.
925 Result
:= pthread_create
926 (T
.Common
.LL
.Thread
'Access,
928 Thread_Body_Access
(Wrapper
),
930 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
932 Succeeded
:= Result
= 0;
934 Result
:= pthread_attr_destroy
(Attributes
'Access);
935 pragma Assert
(Result
= 0);
937 Set_Priority
(T
, Priority
);
944 procedure Finalize_TCB
(T
: Task_Id
) is
945 Result
: Interfaces
.C
.int
;
947 Is_Self
: constant Boolean := T
= Self
;
949 procedure Free
is new
950 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
953 if not Single_Lock
then
954 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
955 pragma Assert
(Result
= 0);
958 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
959 pragma Assert
(Result
= 0);
961 if T
.Known_Tasks_Index
/= -1 then
962 Known_Tasks
(T
.Known_Tasks_Index
) := null;
968 Result
:= st_setspecific
(ATCB_Key
, System
.Null_Address
);
969 pragma Assert
(Result
= 0);
978 procedure Exit_Task
is
987 procedure Abort_Task
(T
: Task_Id
) is
988 Result
: Interfaces
.C
.int
;
990 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
991 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
992 pragma Assert
(Result
= 0);
999 procedure Initialize
(S
: in out Suspension_Object
) is
1000 Mutex_Attr
: aliased pthread_mutexattr_t
;
1001 Cond_Attr
: aliased pthread_condattr_t
;
1002 Result
: Interfaces
.C
.int
;
1005 -- Initialize internal state. It is always initialized to False (ARM
1011 -- Initialize internal mutex
1013 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
1014 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1016 if Result
= ENOMEM
then
1017 raise Storage_Error
;
1020 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
1021 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1023 if Result
= ENOMEM
then
1024 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1025 pragma Assert
(Result
= 0);
1027 raise Storage_Error
;
1030 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
1031 pragma Assert
(Result
= 0);
1033 -- Initialize internal condition variable
1035 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
1036 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1039 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1040 pragma Assert
(Result
= 0);
1042 if Result
= ENOMEM
then
1043 raise Storage_Error
;
1047 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
1048 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1051 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1052 pragma Assert
(Result
= 0);
1054 if Result
= ENOMEM
then
1055 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1056 pragma Assert
(Result
= 0);
1058 raise Storage_Error
;
1062 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1063 pragma Assert
(Result
= 0);
1070 procedure Finalize
(S
: in out Suspension_Object
) is
1071 Result
: Interfaces
.C
.int
;
1073 -- Destroy internal mutex
1075 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1076 pragma Assert
(Result
= 0);
1078 -- Destroy internal condition variable
1080 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1081 pragma Assert
(Result
= 0);
1088 function Current_State
(S
: Suspension_Object
) return Boolean is
1090 -- We do not want to use lock on this read operation. State is marked
1091 -- as Atomic so that we ensure that the value retrieved is correct.
1100 procedure Set_False
(S
: in out Suspension_Object
) is
1101 Result
: Interfaces
.C
.int
;
1103 Result
:= pthread_mutex_lock
(S
.L
'Access);
1104 pragma Assert
(Result
= 0);
1108 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1109 pragma Assert
(Result
= 0);
1116 procedure Set_True
(S
: in out Suspension_Object
) is
1117 Result
: Interfaces
.C
.int
;
1119 Result
:= pthread_mutex_lock
(S
.L
'Access);
1120 pragma Assert
(Result
= 0);
1122 -- If there is already a task waiting on this suspension object then
1123 -- we resume it, leaving the state of the suspension object to False,
1124 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1125 -- the state to True.
1131 Result
:= pthread_cond_signal
(S
.CV
'Access);
1132 pragma Assert
(Result
= 0);
1137 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1138 pragma Assert
(Result
= 0);
1141 ------------------------
1142 -- Suspend_Until_True --
1143 ------------------------
1145 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1146 Result
: Interfaces
.C
.int
;
1148 Result
:= pthread_mutex_lock
(S
.L
'Access);
1149 pragma Assert
(Result
= 0);
1152 -- Program_Error must be raised upon calling Suspend_Until_True
1153 -- if another task is already waiting on that suspension object
1154 -- (ARM D.10 par. 10).
1156 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1157 pragma Assert
(Result
= 0);
1159 raise Program_Error
;
1161 -- Suspend the task if the state is False. Otherwise, the task
1162 -- continues its execution, and the state of the suspension object
1163 -- is set to False (ARM D.10 par. 9).
1169 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1173 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1174 pragma Assert
(Result
= 0);
1175 end Suspend_Until_True
;
1183 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1184 pragma Unreferenced
(Self_ID
);
1189 --------------------
1190 -- Check_No_Locks --
1191 --------------------
1193 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1194 pragma Unreferenced
(Self_ID
);
1199 ----------------------
1200 -- Environment_Task --
1201 ----------------------
1203 function Environment_Task
return Task_Id
is
1205 return Environment_Task_Id
;
1206 end Environment_Task
;
1212 procedure Lock_RTS
is
1214 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1221 procedure Unlock_RTS
is
1223 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1230 function Suspend_Task
1232 Thread_Self
: Thread_Id
) return Boolean
1234 pragma Unreferenced
(T
);
1235 pragma Unreferenced
(Thread_Self
);
1244 function Resume_Task
1246 Thread_Self
: Thread_Id
) return Boolean
1248 pragma Unreferenced
(T
);
1249 pragma Unreferenced
(Thread_Self
);
1258 procedure Initialize
(Environment_Task
: Task_Id
) is
1259 act
: aliased struct_sigaction
;
1260 old_act
: aliased struct_sigaction
;
1261 Tmp_Set
: aliased sigset_t
;
1262 Result
: Interfaces
.C
.int
;
1265 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1266 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1267 -- Get interrupt state. Defined in a-init.c
1268 -- The input argument is the interrupt number,
1269 -- and the result is one of the following:
1271 Default
: constant Character := 's';
1272 -- 'n' this interrupt not set by any Interrupt_State pragma
1273 -- 'u' Interrupt_State pragma set state to User
1274 -- 'r' Interrupt_State pragma set state to Runtime
1275 -- 's' Interrupt_State pragma set state to System (use "default"
1279 Environment_Task_Id
:= Environment_Task
;
1281 Interrupt_Management
.Initialize
;
1283 -- Prepare the set of signals that should unblocked in all tasks
1285 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1286 pragma Assert
(Result
= 0);
1288 for J
in Interrupt_Management
.Interrupt_ID
loop
1289 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1290 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1291 pragma Assert
(Result
= 0);
1295 -- Initialize the lock used to synchronize chain of all ATCBs
1297 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1299 Specific
.Initialize
(Environment_Task
);
1301 Enter_Task
(Environment_Task
);
1303 -- Install the abort-signal handler
1305 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
1309 act
.sa_handler
:= Abort_Handler
'Address;
1311 Result
:= sigemptyset
(Tmp_Set
'Access);
1312 pragma Assert
(Result
= 0);
1313 act
.sa_mask
:= Tmp_Set
;
1317 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1318 act
'Unchecked_Access,
1319 old_act
'Unchecked_Access);
1321 pragma Assert
(Result
= 0);
1325 end System
.Task_Primitives
.Operations
;