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-2004, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
.Task_Info
;
48 -- used for Task_Info_Type
54 with System
.Interrupt_Management
;
55 -- used for Keep_Unmasked
56 -- Abort_Task_Interrupt
59 with System
.Interrupt_Management
.Operations
;
60 -- used for Set_Interrupt_Mask
62 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
64 with System
.Parameters
;
68 -- used for Ada_Task_Control_Block
71 with System
.Soft_Links
;
72 -- used for Defer/Undefer_Abort
74 -- Note that we do not use System.Tasking.Initialization directly since
75 -- this is a higher level package that we shouldn't depend on. For example
76 -- when using the restricted run time, it is replaced by
77 -- System.Tasking.Restricted.Stages.
79 with System
.OS_Primitives
;
80 -- used for Delay_Modes
82 with Unchecked_Deallocation
;
84 package body System
.Task_Primitives
.Operations
is
86 use System
.Tasking
.Debug
;
89 use System
.OS_Interface
;
90 use System
.Parameters
;
91 use System
.OS_Primitives
;
93 package SSL
renames System
.Soft_Links
;
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 Locking_Policy
: Character;
114 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
115 -- Value of the pragma Locking_Policy:
116 -- 'C' for Ceiling_Locking
117 -- 'I' for Inherit_Locking
120 Unblocked_Signal_Mask
: aliased sigset_t
;
121 -- The set of signals that should unblocked in all tasks
123 -- The followings are internal configuration constants needed.
125 Next_Serial_Number
: Task_Serial_Number
:= 100;
126 -- We start at 100, to reserve some special values for
127 -- using in error checking.
129 Time_Slice_Val
: Integer;
130 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
132 Dispatching_Policy
: Character;
133 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
135 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
136 -- Indicates whether FIFO_Within_Priorities is set.
138 Foreign_Task_Elaborated
: aliased Boolean := True;
139 -- Used to identified fake tasks (i.e., non-Ada Threads).
147 procedure Initialize
(Environment_Task
: Task_Id
);
148 pragma Inline
(Initialize
);
149 -- Initialize various data needed by this package.
151 function Is_Valid_Task
return Boolean;
152 pragma Inline
(Is_Valid_Task
);
153 -- Does the current thread have an ATCB?
155 procedure Set
(Self_Id
: Task_Id
);
157 -- Set the self id for the current task.
159 function Self
return Task_Id
;
160 pragma Inline
(Self
);
161 -- Return a pointer to the Ada Task Control Block of the calling task.
165 package body Specific
is separate;
166 -- The body of this package is target specific.
168 ---------------------------------
169 -- Support for foreign threads --
170 ---------------------------------
172 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
173 -- Allocate and Initialize a new ATCB for the current Thread.
175 function Register_Foreign_Thread
176 (Thread
: Thread_Id
) return Task_Id
is separate;
178 -----------------------
179 -- Local Subprograms --
180 -----------------------
182 procedure Abort_Handler
(Sig
: Signal
);
183 -- Signal handler used to implement asynchronous abort.
185 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
);
186 -- This procedure calls the scheduler of the OS to set thread's priority
192 procedure Abort_Handler
(Sig
: Signal
) is
193 pragma Unreferenced
(Sig
);
195 T
: constant Task_Id
:= Self
;
196 Result
: Interfaces
.C
.int
;
197 Old_Set
: aliased sigset_t
;
200 -- It is not safe to raise an exception when using ZCX and the GCC
201 -- exception handling mechanism.
203 if ZCX_By_Default
and then GCC_ZCX_Support
then
207 if T
.Deferral_Level
= 0
208 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
213 -- Make sure signals used for RTS internal purpose are unmasked
216 pthread_sigmask
(SIG_UNBLOCK
,
217 Unblocked_Signal_Mask
'Unchecked_Access,
218 Old_Set
'Unchecked_Access);
219 pragma Assert
(Result
= 0);
221 raise Standard
'Abort_Signal;
229 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
230 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
231 Guard_Page_Address
: Address
;
233 Res
: Interfaces
.C
.int
;
236 if Stack_Base_Available
then
238 -- Compute the guard page address
240 Guard_Page_Address
:=
241 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
244 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
246 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
249 pragma Assert
(Res
= 0);
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 procedure Initialize_Lock
273 (Prio
: System
.Any_Priority
;
276 Attributes
: aliased pthread_mutexattr_t
;
277 Result
: Interfaces
.C
.int
;
280 Result
:= pthread_mutexattr_init
(Attributes
'Access);
281 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
283 if Result
= ENOMEM
then
287 if Locking_Policy
= 'C' then
291 Result
:= pthread_mutex_init
(L
.Mutex
'Access, Attributes
'Access);
292 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
294 if Result
= ENOMEM
then
298 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
299 pragma Assert
(Result
= 0);
302 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
303 pragma Unreferenced
(Level
);
305 Attributes
: aliased pthread_mutexattr_t
;
306 Result
: Interfaces
.C
.int
;
309 Result
:= pthread_mutexattr_init
(Attributes
'Access);
310 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
312 if Result
= ENOMEM
then
316 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
317 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
319 if Result
= ENOMEM
then
320 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
324 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
325 pragma Assert
(Result
= 0);
332 procedure Finalize_Lock
(L
: access Lock
) is
333 Result
: Interfaces
.C
.int
;
335 Result
:= pthread_mutex_destroy
(L
.Mutex
'Access);
336 pragma Assert
(Result
= 0);
339 procedure Finalize_Lock
(L
: access RTS_Lock
) is
340 Result
: Interfaces
.C
.int
;
342 Result
:= pthread_mutex_destroy
(L
);
343 pragma Assert
(Result
= 0);
350 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
351 Result
: Interfaces
.C
.int
;
352 T
: constant Task_Id
:= Self
;
355 if Locking_Policy
= 'C' then
356 if T
.Common
.Current_Priority
> L
.Ceiling
then
357 Ceiling_Violation
:= True;
361 L
.Saved_Priority
:= T
.Common
.Current_Priority
;
363 if T
.Common
.Current_Priority
< L
.Ceiling
then
364 Set_OS_Priority
(T
, L
.Ceiling
);
368 Result
:= pthread_mutex_lock
(L
.Mutex
'Access);
370 -- Assume that the cause of EINVAL is a priority ceiling violation
372 Ceiling_Violation
:= (Result
= EINVAL
);
373 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
376 -- No tricks on RTS_Locks
379 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
381 Result
: Interfaces
.C
.int
;
383 if not Single_Lock
or else Global_Lock
then
384 Result
:= pthread_mutex_lock
(L
);
385 pragma Assert
(Result
= 0);
389 procedure Write_Lock
(T
: Task_Id
) is
390 Result
: Interfaces
.C
.int
;
392 if not Single_Lock
then
393 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
394 pragma Assert
(Result
= 0);
402 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
404 Write_Lock
(L
, Ceiling_Violation
);
411 procedure Unlock
(L
: access Lock
) is
412 Result
: Interfaces
.C
.int
;
413 T
: constant Task_Id
:= Self
;
416 Result
:= pthread_mutex_unlock
(L
.Mutex
'Access);
417 pragma Assert
(Result
= 0);
419 if Locking_Policy
= 'C' then
420 if T
.Common
.Current_Priority
> L
.Saved_Priority
then
421 Set_OS_Priority
(T
, L
.Saved_Priority
);
426 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
427 Result
: Interfaces
.C
.int
;
429 if not Single_Lock
or else Global_Lock
then
430 Result
:= pthread_mutex_unlock
(L
);
431 pragma Assert
(Result
= 0);
435 procedure Unlock
(T
: Task_Id
) is
436 Result
: Interfaces
.C
.int
;
438 if not Single_Lock
then
439 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
440 pragma Assert
(Result
= 0);
450 Reason
: System
.Tasking
.Task_States
)
452 pragma Unreferenced
(Reason
);
453 Result
: Interfaces
.C
.int
;
457 Result
:= pthread_cond_wait
458 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
460 Result
:= pthread_cond_wait
461 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
464 -- EINTR is not considered a failure
466 pragma Assert
(Result
= 0 or else Result
= EINTR
);
473 -- This is for use within the run-time system, so abort is
474 -- assumed to be already deferred, and the caller should be
475 -- holding its own ATCB lock.
477 procedure Timed_Sleep
480 Mode
: ST
.Delay_Modes
;
481 Reason
: Task_States
;
482 Timedout
: out Boolean;
483 Yielded
: out Boolean)
485 pragma Unreferenced
(Reason
);
487 Check_Time
: constant Duration := Monotonic_Clock
;
490 Request
: aliased timespec
;
491 Result
: Interfaces
.C
.int
;
497 if Mode
= Relative
then
498 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
500 if Relative_Timed_Wait
then
501 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
505 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
507 if Relative_Timed_Wait
then
508 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
512 if Abs_Time
> Check_Time
then
513 if Relative_Timed_Wait
then
514 Request
:= To_Timespec
(Rel_Time
);
516 Request
:= To_Timespec
(Abs_Time
);
520 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
521 or else Self_ID
.Pending_Priority_Change
;
524 Result
:= pthread_cond_timedwait
525 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
529 Result
:= pthread_cond_timedwait
530 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
534 exit when Abs_Time
<= Monotonic_Clock
;
536 if Result
= 0 or Result
= EINTR
then
538 -- Somebody may have called Wakeup for us
544 pragma Assert
(Result
= ETIMEDOUT
);
553 -- This is for use in implementing delay statements, so we assume
554 -- the caller is abort-deferred but is holding no locks.
556 procedure Timed_Delay
559 Mode
: ST
.Delay_Modes
)
561 Check_Time
: constant Duration := Monotonic_Clock
;
564 Request
: aliased timespec
;
565 Result
: Interfaces
.C
.int
;
568 -- Only the little window between deferring abort and
569 -- locking Self_ID is the reason we need to
570 -- check for pending abort and priority change below!
578 -- Comments needed in code below ???
580 Write_Lock
(Self_ID
);
582 if Mode
= Relative
then
583 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
585 if Relative_Timed_Wait
then
586 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
590 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
592 if Relative_Timed_Wait
then
593 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
597 if Abs_Time
> Check_Time
then
598 if Relative_Timed_Wait
then
599 Request
:= To_Timespec
(Rel_Time
);
601 Request
:= To_Timespec
(Abs_Time
);
604 Self_ID
.Common
.State
:= Delay_Sleep
;
607 if Self_ID
.Pending_Priority_Change
then
608 Self_ID
.Pending_Priority_Change
:= False;
609 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
610 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
613 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
616 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
617 Single_RTS_Lock
'Access, Request
'Access);
619 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
620 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
623 exit when Abs_Time
<= Monotonic_Clock
;
625 pragma Assert
(Result
= 0
626 or else Result
= ETIMEDOUT
627 or else Result
= EINTR
);
630 Self_ID
.Common
.State
:= Runnable
;
639 Result
:= sched_yield
;
640 SSL
.Abort_Undefer
.all;
643 ---------------------
644 -- Monotonic_Clock --
645 ---------------------
647 function Monotonic_Clock
return Duration is
648 TS
: aliased timespec
;
649 Result
: Interfaces
.C
.int
;
651 Result
:= clock_gettime
652 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
653 pragma Assert
(Result
= 0);
654 return To_Duration
(TS
);
661 function RT_Resolution
return Duration is
662 Res
: aliased timespec
;
663 Result
: Interfaces
.C
.int
;
665 Result
:= clock_getres
666 (clock_id
=> CLOCK_REALTIME
, Res
=> Res
'Unchecked_Access);
667 pragma Assert
(Result
= 0);
668 return To_Duration
(Res
);
675 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
676 pragma Unreferenced
(Reason
);
677 Result
: Interfaces
.C
.int
;
679 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
680 pragma Assert
(Result
= 0);
687 procedure Yield
(Do_Yield
: Boolean := True) is
688 Result
: Interfaces
.C
.int
;
689 pragma Unreferenced
(Result
);
692 Result
:= sched_yield
;
700 procedure Set_OS_Priority
(T
: Task_Id
; Prio
: System
.Any_Priority
) is
701 Result
: Interfaces
.C
.int
;
702 Param
: aliased struct_sched_param
;
705 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
707 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
708 Result
:= pthread_setschedparam
709 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
711 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
712 Result
:= pthread_setschedparam
713 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
716 Result
:= pthread_setschedparam
717 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
720 pragma Assert
(Result
= 0);
723 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
724 pragma Atomic_Components
(Prio_Array_Type
);
725 Prio_Array
: Prio_Array_Type
;
726 -- Comments needed for these declarations ???
728 procedure Set_Priority
730 Prio
: System
.Any_Priority
;
731 Loss_Of_Inheritance
: Boolean := False)
733 Array_Item
: Integer;
736 Set_OS_Priority
(T
, Prio
);
738 if Locking_Policy
= 'C' then
739 -- Annex D requirements: loss of inheritance puts task at the
740 -- beginning of the queue for that prio; copied from 5ztaprop
743 if Loss_Of_Inheritance
744 and then Prio
< T
.Common
.Current_Priority
then
746 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
747 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
751 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
752 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
755 Prio_Array
(T
.Common
.Base_Priority
) :=
756 Prio_Array
(T
.Common
.Base_Priority
) - 1;
760 T
.Common
.Current_Priority
:= Prio
;
767 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
769 return T
.Common
.Current_Priority
;
776 procedure Enter_Task
(Self_ID
: Task_Id
) is
778 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
779 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
781 Specific
.Set
(Self_ID
);
785 for J
in Known_Tasks
'Range loop
786 if Known_Tasks
(J
) = null then
787 Known_Tasks
(J
) := Self_ID
;
788 Self_ID
.Known_Tasks_Index
:= J
;
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 Mutex_Attr
: aliased pthread_mutexattr_t
;
830 Result
: Interfaces
.C
.int
;
831 Cond_Attr
: aliased pthread_condattr_t
;
834 -- Give the task a unique serial number
836 Self_ID
.Serial_Number
:= Next_Serial_Number
;
837 Next_Serial_Number
:= Next_Serial_Number
+ 1;
838 pragma Assert
(Next_Serial_Number
/= 0);
840 if not Single_Lock
then
841 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
842 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
845 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
847 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
855 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
856 pragma Assert
(Result
= 0);
859 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
860 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
863 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
865 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
871 if not Single_Lock
then
872 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
873 pragma Assert
(Result
= 0);
879 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
880 pragma Assert
(Result
= 0);
887 procedure Create_Task
889 Wrapper
: System
.Address
;
890 Stack_Size
: System
.Parameters
.Size_Type
;
891 Priority
: System
.Any_Priority
;
892 Succeeded
: out Boolean)
894 Attributes
: aliased pthread_attr_t
;
895 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
896 Result
: Interfaces
.C
.int
;
898 use System
.Task_Info
;
901 if Stack_Size
= Unspecified_Size
then
902 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
904 elsif Stack_Size
< Minimum_Stack_Size
then
905 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
908 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
911 if Stack_Base_Available
then
913 -- If Stack Checking is supported then allocate 2 additional pages:
915 -- In the worst case, stack is allocated at something like
916 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
917 -- to be sure the effective stack size is greater than what
920 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
923 Result
:= pthread_attr_init
(Attributes
'Access);
924 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
931 Result
:= pthread_attr_setdetachstate
932 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
933 pragma Assert
(Result
= 0);
935 Result
:= pthread_attr_setstacksize
936 (Attributes
'Access, Adjusted_Stack_Size
);
937 pragma Assert
(Result
= 0);
939 if T
.Common
.Task_Info
/= Default_Scope
then
941 -- We are assuming that Scope_Type has the same values than the
942 -- corresponding C macros
944 Result
:= pthread_attr_setscope
945 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
946 pragma Assert
(Result
= 0);
949 -- Since the initial signal mask of a thread is inherited from the
950 -- creator, and the Environment task has all its signals masked, we
951 -- do not need to manipulate caller's signal mask at this point.
952 -- All tasks in RTS will have All_Tasks_Mask initially.
954 Result
:= pthread_create
955 (T
.Common
.LL
.Thread
'Access,
957 Thread_Body_Access
(Wrapper
),
959 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
961 Succeeded
:= Result
= 0;
963 Result
:= pthread_attr_destroy
(Attributes
'Access);
964 pragma Assert
(Result
= 0);
966 Set_Priority
(T
, Priority
);
973 procedure Finalize_TCB
(T
: Task_Id
) is
974 Result
: Interfaces
.C
.int
;
976 Is_Self
: constant Boolean := T
= Self
;
978 procedure Free
is new
979 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
982 if not Single_Lock
then
983 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
984 pragma Assert
(Result
= 0);
987 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
988 pragma Assert
(Result
= 0);
990 if T
.Known_Tasks_Index
/= -1 then
991 Known_Tasks
(T
.Known_Tasks_Index
) := null;
997 Result
:= st_setspecific
(ATCB_Key
, System
.Null_Address
);
998 pragma Assert
(Result
= 0);
1007 procedure Exit_Task
is
1009 Specific
.Set
(null);
1016 procedure Abort_Task
(T
: Task_Id
) is
1017 Result
: Interfaces
.C
.int
;
1021 (T
.Common
.LL
.Thread
,
1022 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1023 pragma Assert
(Result
= 0);
1032 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1033 pragma Unreferenced
(Self_ID
);
1038 --------------------
1039 -- Check_No_Locks --
1040 --------------------
1042 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1043 pragma Unreferenced
(Self_ID
);
1048 ----------------------
1049 -- Environment_Task --
1050 ----------------------
1052 function Environment_Task
return Task_Id
is
1054 return Environment_Task_Id
;
1055 end Environment_Task
;
1061 procedure Lock_RTS
is
1063 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1070 procedure Unlock_RTS
is
1072 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1079 function Suspend_Task
1081 Thread_Self
: Thread_Id
) return Boolean
1083 pragma Unreferenced
(T
);
1084 pragma Unreferenced
(Thread_Self
);
1093 function Resume_Task
1095 Thread_Self
: Thread_Id
) return Boolean
1097 pragma Unreferenced
(T
);
1098 pragma Unreferenced
(Thread_Self
);
1107 procedure Initialize
(Environment_Task
: Task_Id
) is
1108 act
: aliased struct_sigaction
;
1109 old_act
: aliased struct_sigaction
;
1110 Tmp_Set
: aliased sigset_t
;
1111 Result
: Interfaces
.C
.int
;
1114 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1115 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1116 -- Get interrupt state. Defined in a-init.c
1117 -- The input argument is the interrupt number,
1118 -- and the result is one of the following:
1120 Default
: constant Character := 's';
1121 -- 'n' this interrupt not set by any Interrupt_State pragma
1122 -- 'u' Interrupt_State pragma set state to User
1123 -- 'r' Interrupt_State pragma set state to Runtime
1124 -- 's' Interrupt_State pragma set state to System (use "default"
1128 Environment_Task_Id
:= Environment_Task
;
1130 -- Initialize the lock used to synchronize chain of all ATCBs.
1132 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1134 Specific
.Initialize
(Environment_Task
);
1136 Enter_Task
(Environment_Task
);
1138 -- Install the abort-signal handler
1140 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
1144 act
.sa_handler
:= Abort_Handler
'Address;
1146 Result
:= sigemptyset
(Tmp_Set
'Access);
1147 pragma Assert
(Result
= 0);
1148 act
.sa_mask
:= Tmp_Set
;
1152 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1153 act
'Unchecked_Access,
1154 old_act
'Unchecked_Access);
1156 pragma Assert
(Result
= 0);
1162 Result
: Interfaces
.C
.int
;
1165 -- Mask Environment task for all signals. The original mask of the
1166 -- Environment task will be recovered by Interrupt_Server task
1167 -- during the elaboration of s-interr.adb.
1169 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1170 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1172 -- Prepare the set of signals that should unblocked in all tasks
1174 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1175 pragma Assert
(Result
= 0);
1177 for J
in Interrupt_Management
.Interrupt_ID
loop
1178 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1179 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1180 pragma Assert
(Result
= 0);
1184 end System
.Task_Primitives
.Operations
;