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-2001, 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 POSIX-like version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
39 -- Note: this file can only be used for POSIX compliant systems that
40 -- implement SCHED_FIFO and Ceiling Locking correctly.
42 -- For configurations where SCHED_FIFO and priority ceiling are not a
43 -- requirement, this file can also be used (e.g AiX threads)
46 -- Turn off polling, we do not want ATC polling to take place during
47 -- tasking operations. It causes infinite loops and other problems.
49 with System
.Tasking
.Debug
;
50 -- used for Known_Tasks
52 with System
.Task_Info
;
53 -- used for Task_Info_Type
59 with System
.Interrupt_Management
;
60 -- used for Keep_Unmasked
61 -- Abort_Task_Interrupt
64 with System
.Interrupt_Management
.Operations
;
65 -- used for Set_Interrupt_Mask
67 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
69 with System
.Parameters
;
73 -- used for Ada_Task_Control_Block
76 with System
.Soft_Links
;
77 -- used for Defer/Undefer_Abort
79 -- Note that we do not use System.Tasking.Initialization directly since
80 -- this is a higher level package that we shouldn't depend on. For example
81 -- when using the restricted run time, it is replaced by
82 -- System.Tasking.Restricted.Initialization
84 with System
.OS_Primitives
;
85 -- used for Delay_Modes
87 with Unchecked_Conversion
;
88 with Unchecked_Deallocation
;
90 package body System
.Task_Primitives
.Operations
is
92 use System
.Tasking
.Debug
;
95 use System
.OS_Interface
;
96 use System
.Parameters
;
97 use System
.OS_Primitives
;
99 package SSL
renames System
.Soft_Links
;
105 -- The followings are logically constants, but need to be initialized
108 Single_RTS_Lock
: aliased RTS_Lock
;
109 -- This is a lock to allow only one thread of control in the RTS at
110 -- a time; it is used to execute in mutual exclusion from all other tasks.
111 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
113 Environment_Task_ID
: Task_ID
;
114 -- A variable to hold Task_ID for the environment task.
116 Locking_Policy
: Character;
117 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
118 -- Value of the pragma Locking_Policy:
119 -- 'C' for Ceiling_Locking
120 -- 'I' for Inherit_Locking
123 Unblocked_Signal_Mask
: aliased sigset_t
;
124 -- The set of signals that should unblocked in all tasks
126 -- The followings are internal configuration constants needed.
128 Next_Serial_Number
: Task_Serial_Number
:= 100;
129 -- We start at 100, to reserve some special values for
130 -- using in error checking.
132 Time_Slice_Val
: Integer;
133 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
135 Dispatching_Policy
: Character;
136 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
138 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
139 -- Indicates whether FIFO_Within_Priorities is set.
141 -----------------------
142 -- Local Subprograms --
143 -----------------------
145 procedure Abort_Handler
(Sig
: Signal
);
147 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
149 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
157 procedure Initialize
(Environment_Task
: Task_ID
);
158 pragma Inline
(Initialize
);
159 -- Initialize various data needed by this package.
161 procedure Set
(Self_Id
: Task_ID
);
163 -- Set the self id for the current task.
165 function Self
return Task_ID
;
166 pragma Inline
(Self
);
167 -- Return a pointer to the Ada Task Control Block of the calling task.
171 package body Specific
is separate;
172 -- The body of this package is target specific.
178 -- Target-dependent binding of inter-thread Abort signal to
179 -- the raising of the Abort_Signal exception.
181 -- The technical issues and alternatives here are essentially
182 -- the same as for raising exceptions in response to other
183 -- signals (e.g. Storage_Error). See code and comments in
184 -- the package body System.Interrupt_Management.
186 -- Some implementations may not allow an exception to be propagated
187 -- out of a handler, and others might leave the signal or
188 -- interrupt that invoked this handler masked after the exceptional
189 -- return to the application code.
191 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
192 -- On most UNIX systems, this will allow transfer out of a signal handler,
193 -- which is usually the only mechanism available for implementing
194 -- asynchronous handlers of this kind. However, some
195 -- systems do not restore the signal mask on longjmp(), leaving the
196 -- abort signal masked.
198 -- Alternative solutions include:
200 -- 1. Change the PC saved in the system-dependent Context
201 -- parameter to point to code that raises the exception.
202 -- Normal return from this handler will then raise
203 -- the exception after the mask and other system state has
204 -- been restored (see example below).
206 -- 2. Use siglongjmp()/sigsetjmp() to implement exceptions.
208 -- 3. Unmask the signal in the Abortion_Signal exception handler
211 -- The following procedure would be needed if we can't lonjmp out of
212 -- a signal handler (See below)
214 -- procedure Raise_Abort_Signal is
216 -- raise Standard'Abort_Signal;
219 procedure Abort_Handler
223 Result
: Interfaces
.C
.int
;
224 Old_Set
: aliased sigset_t
;
227 -- Assuming it is safe to longjmp out of a signal handler, the
228 -- following code can be used:
230 if T
.Deferral_Level
= 0
231 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
236 -- Make sure signals used for RTS internal purpose are unmasked
238 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
239 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
240 pragma Assert
(Result
= 0);
242 raise Standard
'Abort_Signal;
245 -- Otherwise, something like this is required:
246 -- if not Abort_Is_Deferred.all then
247 -- -- Overwrite the return PC address with the address of the
248 -- -- special raise routine, and "return" to that routine's
249 -- -- starting address.
250 -- Context.PC := Raise_Abort_Signal'Address;
259 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
260 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
261 Guard_Page_Address
: Address
;
263 Res
: Interfaces
.C
.int
;
266 if Stack_Base_Available
then
267 -- Compute the guard page address
269 Guard_Page_Address
:=
270 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
273 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
275 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
278 pragma Assert
(Res
= 0);
286 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
288 return T
.Common
.LL
.Thread
;
295 function Self
return Task_ID
renames Specific
.Self
;
297 ---------------------
298 -- Initialize_Lock --
299 ---------------------
301 -- Note: mutexes and cond_variables needed per-task basis are
302 -- initialized in Initialize_TCB and the Storage_Error is
303 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
304 -- used in RTS is initialized before any status change of RTS.
305 -- Therefore rasing Storage_Error in the following routines
306 -- should be able to be handled safely.
308 procedure Initialize_Lock
309 (Prio
: System
.Any_Priority
;
312 Attributes
: aliased pthread_mutexattr_t
;
313 Result
: Interfaces
.C
.int
;
316 Result
:= pthread_mutexattr_init
(Attributes
'Access);
317 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
319 if Result
= ENOMEM
then
323 if Locking_Policy
= 'C' then
324 Result
:= pthread_mutexattr_setprotocol
325 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
326 pragma Assert
(Result
= 0);
328 Result
:= pthread_mutexattr_setprioceiling
329 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
330 pragma Assert
(Result
= 0);
332 elsif Locking_Policy
= 'I' then
333 Result
:= pthread_mutexattr_setprotocol
334 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
335 pragma Assert
(Result
= 0);
338 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
339 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
341 if Result
= ENOMEM
then
345 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
346 pragma Assert
(Result
= 0);
349 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
350 Attributes
: aliased pthread_mutexattr_t
;
351 Result
: Interfaces
.C
.int
;
354 Result
:= pthread_mutexattr_init
(Attributes
'Access);
355 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
357 if Result
= ENOMEM
then
361 if Locking_Policy
= 'C' then
362 Result
:= pthread_mutexattr_setprotocol
363 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
364 pragma Assert
(Result
= 0);
366 Result
:= pthread_mutexattr_setprioceiling
367 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
368 pragma Assert
(Result
= 0);
370 elsif Locking_Policy
= 'I' then
371 Result
:= pthread_mutexattr_setprotocol
372 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
373 pragma Assert
(Result
= 0);
376 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
377 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
379 if Result
= ENOMEM
then
380 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
384 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
385 pragma Assert
(Result
= 0);
392 procedure Finalize_Lock
(L
: access Lock
) is
393 Result
: Interfaces
.C
.int
;
395 Result
:= pthread_mutex_destroy
(L
);
396 pragma Assert
(Result
= 0);
399 procedure Finalize_Lock
(L
: access RTS_Lock
) is
400 Result
: Interfaces
.C
.int
;
402 Result
:= pthread_mutex_destroy
(L
);
403 pragma Assert
(Result
= 0);
410 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
411 Result
: Interfaces
.C
.int
;
413 Result
:= pthread_mutex_lock
(L
);
415 -- Assume that the cause of EINVAL is a priority ceiling violation
417 Ceiling_Violation
:= (Result
= EINVAL
);
418 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
422 (L
: access RTS_Lock
; Global_Lock
: Boolean := False)
424 Result
: Interfaces
.C
.int
;
426 if not Single_Lock
or else Global_Lock
then
427 Result
:= pthread_mutex_lock
(L
);
428 pragma Assert
(Result
= 0);
432 procedure Write_Lock
(T
: Task_ID
) is
433 Result
: Interfaces
.C
.int
;
435 if not Single_Lock
then
436 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
437 pragma Assert
(Result
= 0);
445 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
447 Write_Lock
(L
, Ceiling_Violation
);
454 procedure Unlock
(L
: access Lock
) is
455 Result
: Interfaces
.C
.int
;
457 Result
:= pthread_mutex_unlock
(L
);
458 pragma Assert
(Result
= 0);
461 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
462 Result
: Interfaces
.C
.int
;
464 if not Single_Lock
or else Global_Lock
then
465 Result
:= pthread_mutex_unlock
(L
);
466 pragma Assert
(Result
= 0);
470 procedure Unlock
(T
: Task_ID
) is
471 Result
: Interfaces
.C
.int
;
473 if not Single_Lock
then
474 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
475 pragma Assert
(Result
= 0);
485 Reason
: System
.Tasking
.Task_States
)
487 Result
: Interfaces
.C
.int
;
490 Result
:= pthread_cond_wait
491 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
493 Result
:= pthread_cond_wait
494 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
497 -- EINTR is not considered a failure.
499 pragma Assert
(Result
= 0 or else Result
= EINTR
);
506 -- This is for use within the run-time system, so abort is
507 -- assumed to be already deferred, and the caller should be
508 -- holding its own ATCB lock.
510 procedure Timed_Sleep
513 Mode
: ST
.Delay_Modes
;
514 Reason
: Task_States
;
515 Timedout
: out Boolean;
516 Yielded
: out Boolean)
518 Check_Time
: constant Duration := Monotonic_Clock
;
521 Request
: aliased timespec
;
522 Result
: Interfaces
.C
.int
;
528 if Mode
= Relative
then
529 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
531 if Relative_Timed_Wait
then
532 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
536 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
538 if Relative_Timed_Wait
then
539 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
543 if Abs_Time
> Check_Time
then
544 if Relative_Timed_Wait
then
545 Request
:= To_Timespec
(Rel_Time
);
547 Request
:= To_Timespec
(Abs_Time
);
551 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
552 or else Self_ID
.Pending_Priority_Change
;
555 Result
:= pthread_cond_timedwait
556 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
560 Result
:= pthread_cond_timedwait
561 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
565 exit when Abs_Time
<= Monotonic_Clock
;
567 if Result
= 0 or Result
= EINTR
then
569 -- Somebody may have called Wakeup for us
575 pragma Assert
(Result
= ETIMEDOUT
);
584 -- This is for use in implementing delay statements, so
585 -- we assume the caller is abort-deferred but is holding
588 procedure Timed_Delay
591 Mode
: ST
.Delay_Modes
)
593 Check_Time
: constant Duration := Monotonic_Clock
;
596 Request
: aliased timespec
;
597 Result
: Interfaces
.C
.int
;
600 -- Only the little window between deferring abort and
601 -- locking Self_ID is the reason we need to
602 -- check for pending abort and priority change below! :(
610 Write_Lock
(Self_ID
);
612 if Mode
= Relative
then
613 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
615 if Relative_Timed_Wait
then
616 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
620 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
622 if Relative_Timed_Wait
then
623 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
627 if Abs_Time
> Check_Time
then
628 if Relative_Timed_Wait
then
629 Request
:= To_Timespec
(Rel_Time
);
631 Request
:= To_Timespec
(Abs_Time
);
634 Self_ID
.Common
.State
:= Delay_Sleep
;
637 if Self_ID
.Pending_Priority_Change
then
638 Self_ID
.Pending_Priority_Change
:= False;
639 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
640 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
643 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
646 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
647 Single_RTS_Lock
'Access, Request
'Access);
649 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
650 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
653 exit when Abs_Time
<= Monotonic_Clock
;
655 pragma Assert
(Result
= 0
656 or else Result
= ETIMEDOUT
657 or else Result
= EINTR
);
660 Self_ID
.Common
.State
:= Runnable
;
669 Result
:= sched_yield
;
670 SSL
.Abort_Undefer
.all;
673 ---------------------
674 -- Monotonic_Clock --
675 ---------------------
677 function Monotonic_Clock
return Duration is
678 TS
: aliased timespec
;
679 Result
: Interfaces
.C
.int
;
682 Result
:= clock_gettime
683 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
684 pragma Assert
(Result
= 0);
685 return To_Duration
(TS
);
692 function RT_Resolution
return Duration is
701 procedure Wakeup
(T
: Task_ID
; Reason
: System
.Tasking
.Task_States
) is
702 Result
: Interfaces
.C
.int
;
704 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
705 pragma Assert
(Result
= 0);
712 procedure Yield
(Do_Yield
: Boolean := True) is
713 Result
: Interfaces
.C
.int
;
716 Result
:= sched_yield
;
724 procedure Set_Priority
726 Prio
: System
.Any_Priority
;
727 Loss_Of_Inheritance
: Boolean := False)
729 Result
: Interfaces
.C
.int
;
730 Param
: aliased struct_sched_param
;
733 T
.Common
.Current_Priority
:= Prio
;
734 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
736 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
737 Result
:= pthread_setschedparam
738 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
740 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
741 Result
:= pthread_setschedparam
742 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
745 Result
:= pthread_setschedparam
746 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
749 pragma Assert
(Result
= 0);
756 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
758 return T
.Common
.Current_Priority
;
765 procedure Enter_Task
(Self_ID
: Task_ID
) is
767 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
768 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
770 Specific
.Set
(Self_ID
);
774 for J
in Known_Tasks
'Range loop
775 if Known_Tasks
(J
) = null then
776 Known_Tasks
(J
) := Self_ID
;
777 Self_ID
.Known_Tasks_Index
:= J
;
789 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
791 return new Ada_Task_Control_Block
(Entry_Num
);
794 ----------------------
796 ----------------------
798 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
799 Mutex_Attr
: aliased pthread_mutexattr_t
;
800 Result
: Interfaces
.C
.int
;
801 Cond_Attr
: aliased pthread_condattr_t
;
804 -- Give the task a unique serial number.
806 Self_ID
.Serial_Number
:= Next_Serial_Number
;
807 Next_Serial_Number
:= Next_Serial_Number
+ 1;
808 pragma Assert
(Next_Serial_Number
/= 0);
810 if not Single_Lock
then
811 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
812 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
815 Result
:= pthread_mutexattr_setprotocol
816 (Mutex_Attr
'Access, PTHREAD_PRIO_PROTECT
);
817 pragma Assert
(Result
= 0);
819 Result
:= pthread_mutexattr_setprioceiling
820 (Mutex_Attr
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
821 pragma Assert
(Result
= 0);
823 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
825 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
833 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
834 pragma Assert
(Result
= 0);
837 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
838 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
841 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
843 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
849 if not Single_Lock
then
850 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
851 pragma Assert
(Result
= 0);
857 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
858 pragma Assert
(Result
= 0);
865 procedure Create_Task
867 Wrapper
: System
.Address
;
868 Stack_Size
: System
.Parameters
.Size_Type
;
869 Priority
: System
.Any_Priority
;
870 Succeeded
: out Boolean)
872 Attributes
: aliased pthread_attr_t
;
873 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
874 Result
: Interfaces
.C
.int
;
876 function Thread_Body_Access
is new
877 Unchecked_Conversion
(System
.Address
, Thread_Body
);
879 use System
.Task_Info
;
882 if Stack_Size
= Unspecified_Size
then
883 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
885 elsif Stack_Size
< Minimum_Stack_Size
then
886 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
889 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
892 if Stack_Base_Available
then
893 -- If Stack Checking is supported then allocate 2 additional pages:
895 -- In the worst case, stack is allocated at something like
896 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
897 -- to be sure the effective stack size is greater than what
900 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
903 Result
:= pthread_attr_init
(Attributes
'Access);
904 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
911 Result
:= pthread_attr_setdetachstate
912 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
913 pragma Assert
(Result
= 0);
915 Result
:= pthread_attr_setstacksize
916 (Attributes
'Access, Adjusted_Stack_Size
);
917 pragma Assert
(Result
= 0);
919 if T
.Common
.Task_Info
/= Default_Scope
then
921 -- We are assuming that Scope_Type has the same values than the
922 -- corresponding C macros
924 Result
:= pthread_attr_setscope
925 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
926 pragma Assert
(Result
= 0);
929 -- Since the initial signal mask of a thread is inherited from the
930 -- creator, and the Environment task has all its signals masked, we
931 -- do not need to manipulate caller's signal mask at this point.
932 -- All tasks in RTS will have All_Tasks_Mask initially.
934 Result
:= pthread_create
935 (T
.Common
.LL
.Thread
'Access,
937 Thread_Body_Access
(Wrapper
),
939 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
941 Succeeded
:= Result
= 0;
943 Result
:= pthread_attr_destroy
(Attributes
'Access);
944 pragma Assert
(Result
= 0);
946 Set_Priority
(T
, Priority
);
953 procedure Finalize_TCB
(T
: Task_ID
) is
954 Result
: Interfaces
.C
.int
;
957 procedure Free
is new
958 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
961 if not Single_Lock
then
962 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
963 pragma Assert
(Result
= 0);
966 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
967 pragma Assert
(Result
= 0);
969 if T
.Known_Tasks_Index
/= -1 then
970 Known_Tasks
(T
.Known_Tasks_Index
) := null;
980 procedure Exit_Task
is
982 pthread_exit
(System
.Null_Address
);
989 procedure Abort_Task
(T
: Task_ID
) is
990 Result
: Interfaces
.C
.int
;
993 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
994 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
995 pragma Assert
(Result
= 0);
1002 -- Dummy versions. The only currently working versions is for solaris
1005 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
1010 --------------------
1011 -- Check_No_Locks --
1012 --------------------
1014 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
1019 ----------------------
1020 -- Environment_Task --
1021 ----------------------
1023 function Environment_Task
return Task_ID
is
1025 return Environment_Task_ID
;
1026 end Environment_Task
;
1032 procedure Lock_RTS
is
1034 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1041 procedure Unlock_RTS
is
1043 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1050 function Suspend_Task
1052 Thread_Self
: Thread_Id
) return Boolean is
1061 function Resume_Task
1063 Thread_Self
: Thread_Id
) return Boolean is
1072 procedure Initialize
(Environment_Task
: Task_ID
) is
1073 act
: aliased struct_sigaction
;
1074 old_act
: aliased struct_sigaction
;
1075 Tmp_Set
: aliased sigset_t
;
1076 Result
: Interfaces
.C
.int
;
1079 Environment_Task_ID
:= Environment_Task
;
1081 -- Initialize the lock used to synchronize chain of all ATCBs.
1083 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1085 Specific
.Initialize
(Environment_Task
);
1087 Enter_Task
(Environment_Task
);
1089 -- Install the abort-signal handler
1092 act
.sa_handler
:= Abort_Handler
'Address;
1094 Result
:= sigemptyset
(Tmp_Set
'Access);
1095 pragma Assert
(Result
= 0);
1096 act
.sa_mask
:= Tmp_Set
;
1100 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1101 act
'Unchecked_Access,
1102 old_act
'Unchecked_Access);
1104 pragma Assert
(Result
= 0);
1109 Result
: Interfaces
.C
.int
;
1111 -- Mask Environment task for all signals. The original mask of the
1112 -- Environment task will be recovered by Interrupt_Server task
1113 -- during the elaboration of s-interr.adb.
1115 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1116 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1118 -- Prepare the set of signals that should unblocked in all tasks
1120 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1121 pragma Assert
(Result
= 0);
1123 for J
in Interrupt_Management
.Interrupt_ID
loop
1124 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1125 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1126 pragma Assert
(Result
= 0);
1130 end System
.Task_Primitives
.Operations
;