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 --
11 -- Copyright (C) 1991-2001, Florida State University --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This is a POSIX-like version of this package
39 -- This package contains all the GNULL primitives that interface directly
40 -- with the underlying OS.
42 -- Note: this file can only be used for POSIX compliant systems that
43 -- implement SCHED_FIFO and Ceiling Locking correctly.
45 -- For configurations where SCHED_FIFO and priority ceiling are not a
46 -- requirement, this file can also be used (e.g AiX threads)
49 -- Turn off polling, we do not want ATC polling to take place during
50 -- tasking operations. It causes infinite loops and other problems.
52 with System
.Tasking
.Debug
;
53 -- used for Known_Tasks
55 with System
.Task_Info
;
56 -- used for Task_Info_Type
62 with System
.Interrupt_Management
;
63 -- used for Keep_Unmasked
64 -- Abort_Task_Interrupt
67 with System
.Interrupt_Management
.Operations
;
68 -- used for Set_Interrupt_Mask
70 pragma Elaborate_All
(System
.Interrupt_Management
.Operations
);
72 with System
.Parameters
;
76 -- used for Ada_Task_Control_Block
79 with System
.Soft_Links
;
80 -- used for Defer/Undefer_Abort
82 -- Note that we do not use System.Tasking.Initialization directly since
83 -- this is a higher level package that we shouldn't depend on. For example
84 -- when using the restricted run time, it is replaced by
85 -- System.Tasking.Restricted.Initialization
87 with System
.OS_Primitives
;
88 -- used for Delay_Modes
90 with Unchecked_Conversion
;
91 with Unchecked_Deallocation
;
93 package body System
.Task_Primitives
.Operations
is
95 use System
.Tasking
.Debug
;
98 use System
.OS_Interface
;
99 use System
.Parameters
;
100 use System
.OS_Primitives
;
102 package SSL
renames System
.Soft_Links
;
108 -- The followings are logically constants, but need to be initialized
111 All_Tasks_L
: aliased System
.Task_Primitives
.RTS_Lock
;
112 -- See comments on locking rules in System.Tasking (spec).
114 Environment_Task_ID
: Task_ID
;
115 -- A variable to hold Task_ID for the environment task.
117 Locking_Policy
: Character;
118 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
119 -- Value of the pragma Locking_Policy:
120 -- 'C' for Ceiling_Locking
121 -- 'I' for Inherit_Locking
124 Unblocked_Signal_Mask
: aliased sigset_t
;
125 -- The set of signals that should unblocked in all tasks
127 -- The followings are internal configuration constants needed.
129 Next_Serial_Number
: Task_Serial_Number
:= 100;
130 -- We start at 100, to reserve some special values for
131 -- using in error checking.
133 Time_Slice_Val
: Integer;
134 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
136 Dispatching_Policy
: Character;
137 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
139 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
140 -- Indicates whether FIFO_Within_Priorities is set.
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 procedure Abort_Handler
149 function To_Task_ID
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
151 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
159 procedure Initialize
(Environment_Task
: Task_ID
);
160 pragma Inline
(Initialize
);
161 -- Initialize various data needed by this package.
163 procedure Set
(Self_Id
: Task_ID
);
165 -- Set the self id for the current task.
167 function Self
return Task_ID
;
168 pragma Inline
(Self
);
169 -- Return a pointer to the Ada Task Control Block of the calling task.
173 package body Specific
is separate;
174 -- The body of this package is target specific.
180 -- Target-dependent binding of inter-thread Abort signal to
181 -- the raising of the Abort_Signal exception.
183 -- The technical issues and alternatives here are essentially
184 -- the same as for raising exceptions in response to other
185 -- signals (e.g. Storage_Error). See code and comments in
186 -- the package body System.Interrupt_Management.
188 -- Some implementations may not allow an exception to be propagated
189 -- out of a handler, and others might leave the signal or
190 -- interrupt that invoked this handler masked after the exceptional
191 -- return to the application code.
193 -- GNAT exceptions are originally implemented using setjmp()/longjmp().
194 -- On most UNIX systems, this will allow transfer out of a signal handler,
195 -- which is usually the only mechanism available for implementing
196 -- asynchronous handlers of this kind. However, some
197 -- systems do not restore the signal mask on longjmp(), leaving the
198 -- abort signal masked.
200 -- Alternative solutions include:
202 -- 1. Change the PC saved in the system-dependent Context
203 -- parameter to point to code that raises the exception.
204 -- Normal return from this handler will then raise
205 -- the exception after the mask and other system state has
206 -- been restored (see example below).
208 -- 2. Use siglongjmp()/sigsetjmp() to implement exceptions.
210 -- 3. Unmask the signal in the Abortion_Signal exception handler
213 -- The following procedure would be needed if we can't lonjmp out of
214 -- a signal handler (See below)
216 -- procedure Raise_Abort_Signal is
218 -- raise Standard'Abort_Signal;
221 procedure Abort_Handler
225 Result
: Interfaces
.C
.int
;
226 Old_Set
: aliased sigset_t
;
229 -- Assuming it is safe to longjmp out of a signal handler, the
230 -- following code can be used:
232 if T
.Deferral_Level
= 0
233 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
238 -- Make sure signals used for RTS internal purpose are unmasked
240 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
241 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
242 pragma Assert
(Result
= 0);
244 raise Standard
'Abort_Signal;
247 -- Otherwise, something like this is required:
248 -- if not Abort_Is_Deferred.all then
249 -- -- Overwrite the return PC address with the address of the
250 -- -- special raise routine, and "return" to that routine's
251 -- -- starting address.
252 -- Context.PC := Raise_Abort_Signal'Address;
262 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
264 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
265 Guard_Page_Address
: Address
;
267 Res
: Interfaces
.C
.int
;
270 if Stack_Base_Available
then
271 -- Compute the guard page address
273 Guard_Page_Address
:=
274 Stack_Base
- (Stack_Base
mod Get_Page_Size
) + Get_Page_Size
;
277 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_ON
);
279 Res
:= mprotect
(Guard_Page_Address
, Get_Page_Size
, PROT_OFF
);
282 pragma Assert
(Res
= 0);
290 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
292 return T
.Common
.LL
.Thread
;
299 function Self
return Task_ID
renames Specific
.Self
;
301 ---------------------
302 -- Initialize_Lock --
303 ---------------------
305 -- Note: mutexes and cond_variables needed per-task basis are
306 -- initialized in Initialize_TCB and the Storage_Error is
307 -- handled. Other mutexes (such as All_Tasks_Lock, Memory_Lock...)
308 -- used in RTS is initialized before any status change of RTS.
309 -- Therefore rasing Storage_Error in the following routines
310 -- should be able to be handled safely.
312 procedure Initialize_Lock
313 (Prio
: System
.Any_Priority
;
316 Attributes
: aliased pthread_mutexattr_t
;
317 Result
: Interfaces
.C
.int
;
320 Result
:= pthread_mutexattr_init
(Attributes
'Access);
321 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
323 if Result
= ENOMEM
then
327 if Locking_Policy
= 'C' then
328 Result
:= pthread_mutexattr_setprotocol
329 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
330 pragma Assert
(Result
= 0);
332 Result
:= pthread_mutexattr_setprioceiling
333 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
334 pragma Assert
(Result
= 0);
336 elsif Locking_Policy
= 'I' then
337 Result
:= pthread_mutexattr_setprotocol
338 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
339 pragma Assert
(Result
= 0);
342 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
343 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
345 if Result
= ENOMEM
then
349 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
350 pragma Assert
(Result
= 0);
353 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
354 Attributes
: aliased pthread_mutexattr_t
;
355 Result
: Interfaces
.C
.int
;
358 Result
:= pthread_mutexattr_init
(Attributes
'Access);
359 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
361 if Result
= ENOMEM
then
365 if Locking_Policy
= 'C' then
366 Result
:= pthread_mutexattr_setprotocol
367 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
368 pragma Assert
(Result
= 0);
370 Result
:= pthread_mutexattr_setprioceiling
371 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
372 pragma Assert
(Result
= 0);
374 elsif Locking_Policy
= 'I' then
375 Result
:= pthread_mutexattr_setprotocol
376 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
377 pragma Assert
(Result
= 0);
380 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
381 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
383 if Result
= ENOMEM
then
384 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
388 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
389 pragma Assert
(Result
= 0);
396 procedure Finalize_Lock
(L
: access Lock
) is
397 Result
: Interfaces
.C
.int
;
400 Result
:= pthread_mutex_destroy
(L
);
401 pragma Assert
(Result
= 0);
404 procedure Finalize_Lock
(L
: access RTS_Lock
) is
405 Result
: Interfaces
.C
.int
;
408 Result
:= pthread_mutex_destroy
(L
);
409 pragma Assert
(Result
= 0);
416 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
417 Result
: Interfaces
.C
.int
;
420 Result
:= pthread_mutex_lock
(L
);
422 -- Assume that the cause of EINVAL is a priority ceiling violation
424 Ceiling_Violation
:= (Result
= EINVAL
);
425 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
428 procedure Write_Lock
(L
: access RTS_Lock
) is
429 Result
: Interfaces
.C
.int
;
432 Result
:= pthread_mutex_lock
(L
);
433 pragma Assert
(Result
= 0);
436 procedure Write_Lock
(T
: Task_ID
) is
437 Result
: Interfaces
.C
.int
;
440 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
441 pragma Assert
(Result
= 0);
448 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
450 Write_Lock
(L
, Ceiling_Violation
);
457 procedure Unlock
(L
: access Lock
) is
458 Result
: Interfaces
.C
.int
;
461 Result
:= pthread_mutex_unlock
(L
);
462 pragma Assert
(Result
= 0);
465 procedure Unlock
(L
: access RTS_Lock
) is
466 Result
: Interfaces
.C
.int
;
469 Result
:= pthread_mutex_unlock
(L
);
470 pragma Assert
(Result
= 0);
473 procedure Unlock
(T
: Task_ID
) is
474 Result
: Interfaces
.C
.int
;
477 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
478 pragma Assert
(Result
= 0);
485 procedure Sleep
(Self_ID
: Task_ID
;
486 Reason
: System
.Tasking
.Task_States
) is
487 Result
: Interfaces
.C
.int
;
490 pragma Assert
(Self_ID
= Self
);
491 Result
:= pthread_cond_wait
(Self_ID
.Common
.LL
.CV
'Access,
492 Self_ID
.Common
.LL
.L
'Access);
494 -- EINTR is not considered a failure.
496 pragma Assert
(Result
= 0 or else Result
= EINTR
);
503 -- This is for use within the run-time system, so abort is
504 -- assumed to be already deferred, and the caller should be
505 -- holding its own ATCB lock.
507 procedure Timed_Sleep
510 Mode
: ST
.Delay_Modes
;
511 Reason
: Task_States
;
512 Timedout
: out Boolean;
513 Yielded
: out Boolean)
515 Check_Time
: constant Duration := Monotonic_Clock
;
518 Request
: aliased timespec
;
519 Result
: Interfaces
.C
.int
;
525 if Mode
= Relative
then
526 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
528 if Relative_Timed_Wait
then
529 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
533 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
535 if Relative_Timed_Wait
then
536 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
540 if Abs_Time
> Check_Time
then
541 if Relative_Timed_Wait
then
542 Request
:= To_Timespec
(Rel_Time
);
544 Request
:= To_Timespec
(Abs_Time
);
548 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
549 or else Self_ID
.Pending_Priority_Change
;
551 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
552 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
554 exit when Abs_Time
<= Monotonic_Clock
;
556 if Result
= 0 or Result
= EINTR
then
558 -- Somebody may have called Wakeup for us
564 pragma Assert
(Result
= ETIMEDOUT
);
573 -- This is for use in implementing delay statements, so
574 -- we assume the caller is abort-deferred but is holding
577 procedure Timed_Delay
580 Mode
: ST
.Delay_Modes
)
582 Check_Time
: constant Duration := Monotonic_Clock
;
585 Request
: aliased timespec
;
586 Result
: Interfaces
.C
.int
;
589 -- Only the little window between deferring abort and
590 -- locking Self_ID is the reason we need to
591 -- check for pending abort and priority change below! :(
594 Write_Lock
(Self_ID
);
596 if Mode
= Relative
then
597 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
599 if Relative_Timed_Wait
then
600 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
);
604 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
606 if Relative_Timed_Wait
then
607 Rel_Time
:= Duration'Min (Max_Sensible_Delay
, Time
- Check_Time
);
611 if Abs_Time
> Check_Time
then
612 if Relative_Timed_Wait
then
613 Request
:= To_Timespec
(Rel_Time
);
615 Request
:= To_Timespec
(Abs_Time
);
618 Self_ID
.Common
.State
:= Delay_Sleep
;
621 if Self_ID
.Pending_Priority_Change
then
622 Self_ID
.Pending_Priority_Change
:= False;
623 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
624 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
627 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
629 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
630 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
631 exit when Abs_Time
<= Monotonic_Clock
;
633 pragma Assert
(Result
= 0
634 or else Result
= ETIMEDOUT
635 or else Result
= EINTR
);
638 Self_ID
.Common
.State
:= Runnable
;
642 Result
:= sched_yield
;
643 SSL
.Abort_Undefer
.all;
646 ---------------------
647 -- Monotonic_Clock --
648 ---------------------
650 function Monotonic_Clock
return Duration is
651 TS
: aliased timespec
;
652 Result
: Interfaces
.C
.int
;
655 Result
:= clock_gettime
656 (clock_id
=> CLOCK_REALTIME
, tp
=> TS
'Unchecked_Access);
657 pragma Assert
(Result
= 0);
658 return To_Duration
(TS
);
665 function RT_Resolution
return Duration is
674 procedure Wakeup
(T
: Task_ID
; Reason
: System
.Tasking
.Task_States
) is
675 Result
: Interfaces
.C
.int
;
678 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
679 pragma Assert
(Result
= 0);
686 procedure Yield
(Do_Yield
: Boolean := True) is
687 Result
: Interfaces
.C
.int
;
691 Result
:= sched_yield
;
699 procedure Set_Priority
701 Prio
: System
.Any_Priority
;
702 Loss_Of_Inheritance
: Boolean := False)
704 Result
: Interfaces
.C
.int
;
705 Param
: aliased struct_sched_param
;
708 T
.Common
.Current_Priority
:= Prio
;
709 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
711 if Time_Slice_Supported
and then Time_Slice_Val
> 0 then
712 Result
:= pthread_setschedparam
713 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
715 elsif FIFO_Within_Priorities
or else Time_Slice_Val
= 0 then
716 Result
:= pthread_setschedparam
717 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
720 Result
:= pthread_setschedparam
721 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
724 pragma Assert
(Result
= 0);
731 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
733 return T
.Common
.Current_Priority
;
740 procedure Enter_Task
(Self_ID
: Task_ID
) is
742 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
743 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
745 Specific
.Set
(Self_ID
);
749 for I
in Known_Tasks
'Range loop
750 if Known_Tasks
(I
) = null then
751 Known_Tasks
(I
) := Self_ID
;
752 Self_ID
.Known_Tasks_Index
:= I
;
757 Unlock_All_Tasks_List
;
764 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
766 return new Ada_Task_Control_Block
(Entry_Num
);
769 ----------------------
771 ----------------------
773 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
774 Mutex_Attr
: aliased pthread_mutexattr_t
;
775 Result
: Interfaces
.C
.int
;
776 Cond_Attr
: aliased pthread_condattr_t
;
779 -- Give the task a unique serial number.
781 Self_ID
.Serial_Number
:= Next_Serial_Number
;
782 Next_Serial_Number
:= Next_Serial_Number
+ 1;
783 pragma Assert
(Next_Serial_Number
/= 0);
785 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
786 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
793 Result
:= pthread_mutexattr_setprotocol
794 (Mutex_Attr
'Access, PTHREAD_PRIO_PROTECT
);
795 pragma Assert
(Result
= 0);
797 Result
:= pthread_mutexattr_setprioceiling
798 (Mutex_Attr
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
799 pragma Assert
(Result
= 0);
801 Result
:= pthread_mutex_init
(Self_ID
.Common
.LL
.L
'Access,
803 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
810 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
811 pragma Assert
(Result
= 0);
813 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
814 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
817 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
818 pragma Assert
(Result
= 0);
823 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
825 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
830 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
831 pragma Assert
(Result
= 0);
835 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
836 pragma Assert
(Result
= 0);
843 procedure Create_Task
845 Wrapper
: System
.Address
;
846 Stack_Size
: System
.Parameters
.Size_Type
;
847 Priority
: System
.Any_Priority
;
848 Succeeded
: out Boolean)
850 Attributes
: aliased pthread_attr_t
;
851 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
852 Result
: Interfaces
.C
.int
;
854 function Thread_Body_Access
is new
855 Unchecked_Conversion
(System
.Address
, Thread_Body
);
857 use System
.Task_Info
;
860 if Stack_Size
= Unspecified_Size
then
861 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Default_Stack_Size
);
863 elsif Stack_Size
< Minimum_Stack_Size
then
864 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Minimum_Stack_Size
);
867 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
);
870 if Stack_Base_Available
then
871 -- If Stack Checking is supported then allocate 2 additional pages:
873 -- In the worst case, stack is allocated at something like
874 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
875 -- to be sure the effective stack size is greater than what
878 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Get_Page_Size
;
881 Result
:= pthread_attr_init
(Attributes
'Access);
882 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
889 Result
:= pthread_attr_setdetachstate
890 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
891 pragma Assert
(Result
= 0);
893 Result
:= pthread_attr_setstacksize
894 (Attributes
'Access, Adjusted_Stack_Size
);
895 pragma Assert
(Result
= 0);
897 if T
.Common
.Task_Info
/= Default_Scope
then
899 -- We are assuming that Scope_Type has the same values than the
900 -- corresponding C macros
902 Result
:= pthread_attr_setscope
903 (Attributes
'Access, Task_Info_Type
'Pos (T
.Common
.Task_Info
));
904 pragma Assert
(Result
= 0);
907 -- Since the initial signal mask of a thread is inherited from the
908 -- creator, and the Environment task has all its signals masked, we
909 -- do not need to manipulate caller's signal mask at this point.
910 -- All tasks in RTS will have All_Tasks_Mask initially.
912 Result
:= pthread_create
913 (T
.Common
.LL
.Thread
'Access,
915 Thread_Body_Access
(Wrapper
),
917 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
919 Succeeded
:= Result
= 0;
921 Result
:= pthread_attr_destroy
(Attributes
'Access);
922 pragma Assert
(Result
= 0);
924 Set_Priority
(T
, Priority
);
931 procedure Finalize_TCB
(T
: Task_ID
) is
932 Result
: Interfaces
.C
.int
;
935 procedure Free
is new
936 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
939 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
940 pragma Assert
(Result
= 0);
942 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
943 pragma Assert
(Result
= 0);
945 if T
.Known_Tasks_Index
/= -1 then
946 Known_Tasks
(T
.Known_Tasks_Index
) := null;
956 procedure Exit_Task
is
958 pthread_exit
(System
.Null_Address
);
965 procedure Abort_Task
(T
: Task_ID
) is
966 Result
: Interfaces
.C
.int
;
969 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
970 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
971 pragma Assert
(Result
= 0);
978 -- Dummy versions. The only currently working versions is for solaris
981 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
990 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
995 ----------------------
996 -- Environment_Task --
997 ----------------------
999 function Environment_Task
return Task_ID
is
1001 return Environment_Task_ID
;
1002 end Environment_Task
;
1004 -------------------------
1005 -- Lock_All_Tasks_List --
1006 -------------------------
1008 procedure Lock_All_Tasks_List
is
1010 Write_Lock
(All_Tasks_L
'Access);
1011 end Lock_All_Tasks_List
;
1013 ---------------------------
1014 -- Unlock_All_Tasks_List --
1015 ---------------------------
1017 procedure Unlock_All_Tasks_List
is
1019 Unlock
(All_Tasks_L
'Access);
1020 end Unlock_All_Tasks_List
;
1026 function Suspend_Task
1028 Thread_Self
: Thread_Id
) return Boolean is
1037 function Resume_Task
1039 Thread_Self
: Thread_Id
) return Boolean is
1048 procedure Initialize
(Environment_Task
: Task_ID
) is
1049 act
: aliased struct_sigaction
;
1050 old_act
: aliased struct_sigaction
;
1051 Tmp_Set
: aliased sigset_t
;
1052 Result
: Interfaces
.C
.int
;
1055 Environment_Task_ID
:= Environment_Task
;
1057 -- Initialize the lock used to synchronize chain of all ATCBs.
1059 Initialize_Lock
(All_Tasks_L
'Access, All_Tasks_Level
);
1061 Specific
.Initialize
(Environment_Task
);
1063 Enter_Task
(Environment_Task
);
1065 -- Install the abort-signal handler
1068 act
.sa_handler
:= Abort_Handler
'Address;
1070 Result
:= sigemptyset
(Tmp_Set
'Access);
1071 pragma Assert
(Result
= 0);
1072 act
.sa_mask
:= Tmp_Set
;
1076 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1077 act
'Unchecked_Access,
1078 old_act
'Unchecked_Access);
1080 pragma Assert
(Result
= 0);
1085 Result
: Interfaces
.C
.int
;
1088 -- Mask Environment task for all signals. The original mask of the
1089 -- Environment task will be recovered by Interrupt_Server task
1090 -- during the elaboration of s-interr.adb.
1092 System
.Interrupt_Management
.Operations
.Set_Interrupt_Mask
1093 (System
.Interrupt_Management
.Operations
.All_Tasks_Mask
'Access);
1095 -- Prepare the set of signals that should unblocked in all tasks
1097 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1098 pragma Assert
(Result
= 0);
1100 for J
in Interrupt_Management
.Interrupt_ID
loop
1101 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1102 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1103 pragma Assert
(Result
= 0);
1108 end System
.Task_Primitives
.Operations
;