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-2017, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a POSIX-like version of this package
34 -- This package contains all the GNULL primitives that interface directly with
37 -- Note: this file can only be used for POSIX compliant systems that implement
38 -- SCHED_FIFO and Ceiling Locking correctly.
40 -- For configurations where SCHED_FIFO and priority ceiling are not a
41 -- requirement, this file can also be used (e.g AiX threads)
44 -- Turn off polling, we do not want ATC polling to take place during tasking
45 -- operations. It causes infinite loops and other problems.
47 with Ada
.Unchecked_Conversion
;
51 with System
.Tasking
.Debug
;
52 with System
.Interrupt_Management
;
53 with System
.OS_Constants
;
54 with System
.OS_Primitives
;
55 with System
.Task_Info
;
57 with System
.Soft_Links
;
58 -- We use System.Soft_Links instead of System.Tasking.Initialization
59 -- because the later is a higher level package that we shouldn't depend on.
60 -- For example when using the restricted run time, it is replaced by
61 -- System.Tasking.Restricted.Stages.
63 package body System
.Task_Primitives
.Operations
is
65 package OSC
renames System
.OS_Constants
;
66 package SSL
renames System
.Soft_Links
;
68 use System
.Tasking
.Debug
;
71 use System
.OS_Interface
;
72 use System
.Parameters
;
73 use System
.OS_Primitives
;
79 -- The followings are logically constants, but need to be initialized
82 Single_RTS_Lock
: aliased RTS_Lock
;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 Environment_Task_Id
: Task_Id
;
88 -- A variable to hold Task_Id for the environment task
90 Locking_Policy
: Character;
91 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
92 -- Value of the pragma Locking_Policy:
93 -- 'C' for Ceiling_Locking
94 -- 'I' for Inherit_Locking
97 Unblocked_Signal_Mask
: aliased sigset_t
;
98 -- The set of signals that should unblocked in all tasks
100 -- The followings are internal configuration constants needed
102 Next_Serial_Number
: Task_Serial_Number
:= 100;
103 -- We start at 100, to reserve some special values for
104 -- using in error checking.
106 Time_Slice_Val
: Integer;
107 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
109 Dispatching_Policy
: Character;
110 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
112 Foreign_Task_Elaborated
: aliased Boolean := True;
113 -- Used to identified fake tasks (i.e., non-Ada Threads)
115 Use_Alternate_Stack
: constant Boolean := Alternate_Stack_Size
/= 0;
116 -- Whether to use an alternate signal stack for stack overflows
118 Abort_Handler_Installed
: Boolean := False;
119 -- True if a handler for the abort signal is installed
121 type RTS_Lock_Ptr
is not null access all RTS_Lock
;
123 function Init_Mutex
(L
: RTS_Lock_Ptr
; Prio
: Any_Priority
) return int
;
124 -- Initialize the mutex L. If Ceiling_Support is True, then set the ceiling
125 -- to Prio. Returns 0 for success, or ENOMEM for out-of-memory.
127 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
128 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
129 -- Get priority specific dispatching policy
137 procedure Initialize
(Environment_Task
: Task_Id
);
138 pragma Inline
(Initialize
);
139 -- Initialize various data needed by this package
141 function Is_Valid_Task
return Boolean;
142 pragma Inline
(Is_Valid_Task
);
143 -- Does executing thread have a TCB?
145 procedure Set
(Self_Id
: Task_Id
);
147 -- Set the self id for the current task
149 function Self
return Task_Id
;
150 pragma Inline
(Self
);
151 -- Return a pointer to the Ada Task Control Block of the calling task
155 package body Specific
is separate;
156 -- The body of this package is target specific
160 function Monotonic_Clock
return Duration;
161 pragma Inline
(Monotonic_Clock
);
162 -- Returns an absolute time, represented as an offset relative to some
163 -- unspecified starting point, typically system boot time. This clock
164 -- is not affected by discontinuous jumps in the system time.
166 function RT_Resolution
return Duration;
167 pragma Inline
(RT_Resolution
);
168 -- Returns resolution of the underlying clock used to implement RT_Clock
170 procedure Timed_Sleep
171 (Self_ID
: ST
.Task_Id
;
173 Mode
: ST
.Delay_Modes
;
174 Reason
: System
.Tasking
.Task_States
;
175 Timedout
: out Boolean;
176 Yielded
: out Boolean);
177 -- Combination of Sleep (above) and Timed_Delay
179 procedure Timed_Delay
180 (Self_ID
: ST
.Task_Id
;
182 Mode
: ST
.Delay_Modes
);
183 -- Implement the semantics of the delay statement.
184 -- The caller should be abort-deferred and should not hold any locks.
188 package body Monotonic
is separate;
190 ----------------------------------
191 -- ATCB allocation/deallocation --
192 ----------------------------------
194 package body ATCB_Allocation
is separate;
195 -- The body of this package is shared across several targets
197 ---------------------------------
198 -- Support for foreign threads --
199 ---------------------------------
201 function Register_Foreign_Thread
203 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
204 -- Allocate and initialize a new ATCB for the current Thread. The size of
205 -- the secondary stack can be optionally specified.
207 function Register_Foreign_Thread
209 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
210 return Task_Id
is separate;
212 -----------------------
213 -- Local Subprograms --
214 -----------------------
216 procedure Abort_Handler
(Sig
: Signal
);
217 -- Signal handler used to implement asynchronous abort.
218 -- See also comment before body, below.
220 function To_Address
is
221 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
223 function GNAT_pthread_condattr_setup
224 (attr
: access pthread_condattr_t
) return int
;
226 GNAT_pthread_condattr_setup
, "__gnat_pthread_condattr_setup");
232 -- Target-dependent binding of inter-thread Abort signal to the raising of
233 -- the Abort_Signal exception.
235 -- The technical issues and alternatives here are essentially the
236 -- same as for raising exceptions in response to other signals
237 -- (e.g. Storage_Error). See code and comments in the package body
238 -- System.Interrupt_Management.
240 -- Some implementations may not allow an exception to be propagated out of
241 -- a handler, and others might leave the signal or interrupt that invoked
242 -- this handler masked after the exceptional return to the application
245 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
246 -- most UNIX systems, this will allow transfer out of a signal handler,
247 -- which is usually the only mechanism available for implementing
248 -- asynchronous handlers of this kind. However, some systems do not
249 -- restore the signal mask on longjmp(), leaving the abort signal masked.
251 procedure Abort_Handler
(Sig
: Signal
) is
252 pragma Unreferenced
(Sig
);
254 T
: constant Task_Id
:= Self
;
255 Old_Set
: aliased sigset_t
;
257 Result
: Interfaces
.C
.int
;
258 pragma Warnings
(Off
, Result
);
261 -- It's not safe to raise an exception when using GCC ZCX mechanism.
262 -- Note that we still need to install a signal handler, since in some
263 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
264 -- need to send the Abort signal to a task.
266 if ZCX_By_Default
then
270 if T
.Deferral_Level
= 0
271 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
and then
276 -- Make sure signals used for RTS internal purpose are unmasked
278 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
279 Unblocked_Signal_Mask
'Access, Old_Set
'Access);
280 pragma Assert
(Result
= 0);
282 raise Standard
'Abort_Signal;
290 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
291 Stack_Base
: constant Address
:= Get_Stack_Base
(T
.Common
.LL
.Thread
);
293 Res
: Interfaces
.C
.int
;
296 if Stack_Base_Available
then
298 -- Compute the guard page address
300 Page_Size
:= Address
(Get_Page_Size
);
303 (Stack_Base
- (Stack_Base
mod Page_Size
) + Page_Size
,
305 prot
=> (if On
then PROT_ON
else PROT_OFF
));
306 pragma Assert
(Res
= 0);
314 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
316 return T
.Common
.LL
.Thread
;
323 function Self
return Task_Id
renames Specific
.Self
;
329 function Init_Mutex
(L
: RTS_Lock_Ptr
; Prio
: Any_Priority
) return int
331 Attributes
: aliased pthread_mutexattr_t
;
333 Result_2
: aliased int
;
336 Result
:= pthread_mutexattr_init
(Attributes
'Access);
337 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
339 if Result
= ENOMEM
then
343 if Locking_Policy
= 'C' then
344 Result
:= pthread_mutexattr_setprotocol
345 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
346 pragma Assert
(Result
= 0);
348 Result
:= pthread_mutexattr_getprotocol
349 (Attributes
'Access, Result_2
'Access);
350 if Result_2
/= PTHREAD_PRIO_PROTECT
then
351 raise Program_Error
with "setprotocol failed";
354 Result
:= pthread_mutexattr_setprioceiling
355 (Attributes
'Access, To_Target_Priority
(Prio
));
356 pragma Assert
(Result
= 0);
358 elsif Locking_Policy
= 'I' then
359 Result
:= pthread_mutexattr_setprotocol
360 (Attributes
'Access, PTHREAD_PRIO_INHERIT
);
361 pragma Assert
(Result
= 0);
364 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
365 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
367 Result_2
:= pthread_mutexattr_destroy
(Attributes
'Access);
368 pragma Assert
(Result_2
= 0);
373 ---------------------
374 -- Initialize_Lock --
375 ---------------------
377 -- Note: mutexes and cond_variables needed per-task basis are initialized
378 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
379 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
380 -- status change of RTS. Therefore raising Storage_Error in the following
381 -- routines should be able to be handled safely.
383 procedure Initialize_Lock
384 (Prio
: System
.Any_Priority
;
385 L
: not null access Lock
)
388 if Init_Mutex
(L
.WO
'Access, Prio
) = ENOMEM
then
389 raise Storage_Error
with "Failed to allocate a lock";
393 procedure Initialize_Lock
394 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
396 pragma Unreferenced
(Level
);
399 if Init_Mutex
(L
.all'Access, Any_Priority
'Last) = ENOMEM
then
400 raise Storage_Error
with "Failed to allocate a lock";
408 procedure Finalize_Lock
(L
: not null access Lock
) is
409 Result
: Interfaces
.C
.int
;
411 Result
:= pthread_mutex_destroy
(L
.WO
'Access);
412 pragma Assert
(Result
= 0);
415 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
416 Result
: Interfaces
.C
.int
;
418 Result
:= pthread_mutex_destroy
(L
);
419 pragma Assert
(Result
= 0);
427 (L
: not null access Lock
; Ceiling_Violation
: out Boolean)
429 Self
: constant pthread_t
:= pthread_self
;
431 Policy
: aliased int
;
432 Ceiling
: aliased int
;
433 Sched
: aliased struct_sched_param
;
436 Result
:= pthread_mutex_lock
(L
.WO
'Access);
438 -- The cause of EINVAL is a priority ceiling violation
440 Ceiling_Violation
:= Result
= EINVAL
;
441 pragma Assert
(Result
= 0 or else Ceiling_Violation
);
443 -- Workaround bug in QNX on ceiling locks: tasks with priority higher
444 -- than the ceiling priority don't receive EINVAL upon trying to lock.
446 Result
:= pthread_getschedparam
(Self
, Policy
'Access, Sched
'Access);
447 pragma Assert
(Result
= 0);
448 Result
:= pthread_mutex_getprioceiling
(L
.WO
'Access, Ceiling
'Access);
449 pragma Assert
(Result
= 0);
451 -- Ceiling = 0 means no Ceiling Priority policy is set on this mutex
452 -- Else, Ceiling < current priority means Ceiling violation
453 -- (otherwise the current priority == ceiling)
454 if Ceiling
> 0 and then Ceiling
< Sched
.sched_curpriority
then
455 Ceiling_Violation
:= True;
456 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
457 pragma Assert
(Result
= 0);
463 (L
: not null access RTS_Lock
;
464 Global_Lock
: Boolean := False)
466 Result
: Interfaces
.C
.int
;
468 if not Single_Lock
or else Global_Lock
then
469 Result
:= pthread_mutex_lock
(L
);
470 pragma Assert
(Result
= 0);
474 procedure Write_Lock
(T
: Task_Id
) is
475 Result
: Interfaces
.C
.int
;
477 if not Single_Lock
then
478 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
479 pragma Assert
(Result
= 0);
488 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
490 Write_Lock
(L
, Ceiling_Violation
);
497 procedure Unlock
(L
: not null access Lock
) is
498 Result
: Interfaces
.C
.int
;
500 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
501 pragma Assert
(Result
= 0);
505 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False)
507 Result
: Interfaces
.C
.int
;
509 if not Single_Lock
or else Global_Lock
then
510 Result
:= pthread_mutex_unlock
(L
);
511 pragma Assert
(Result
= 0);
515 procedure Unlock
(T
: Task_Id
) is
516 Result
: Interfaces
.C
.int
;
518 if not Single_Lock
then
519 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
520 pragma Assert
(Result
= 0);
528 procedure Set_Ceiling
529 (L
: not null access Lock
;
530 Prio
: System
.Any_Priority
)
532 Result
: Interfaces
.C
.int
;
534 Result
:= pthread_mutex_setprioceiling
535 (L
.WO
'Access, To_Target_Priority
(Prio
), null);
536 pragma Assert
(Result
= 0);
545 Reason
: System
.Tasking
.Task_States
)
547 pragma Unreferenced
(Reason
);
549 Result
: Interfaces
.C
.int
;
554 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
555 mutex
=> (if Single_Lock
556 then Single_RTS_Lock
'Access
557 else Self_ID
.Common
.LL
.L
'Access));
559 -- EINTR is not considered a failure
561 pragma Assert
(Result
= 0 or else Result
= EINTR
);
568 -- This is for use within the run-time system, so abort is
569 -- assumed to be already deferred, and the caller should be
570 -- holding its own ATCB lock.
572 procedure Timed_Sleep
575 Mode
: ST
.Delay_Modes
;
576 Reason
: Task_States
;
577 Timedout
: out Boolean;
578 Yielded
: out Boolean) renames Monotonic
.Timed_Sleep
;
584 -- This is for use in implementing delay statements, so we assume the
585 -- caller is abort-deferred but is holding no locks.
587 procedure Timed_Delay
590 Mode
: ST
.Delay_Modes
) renames Monotonic
.Timed_Delay
;
592 ---------------------
593 -- Monotonic_Clock --
594 ---------------------
596 function Monotonic_Clock
return Duration renames Monotonic
.Monotonic_Clock
;
602 function RT_Resolution
return Duration renames Monotonic
.RT_Resolution
;
608 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
609 pragma Unreferenced
(Reason
);
610 Result
: Interfaces
.C
.int
;
612 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
613 pragma Assert
(Result
= 0);
620 procedure Yield
(Do_Yield
: Boolean := True) is
621 Result
: Interfaces
.C
.int
;
622 pragma Unreferenced
(Result
);
625 Result
:= sched_yield
;
633 procedure Set_Priority
635 Prio
: System
.Any_Priority
;
636 Loss_Of_Inheritance
: Boolean := False)
638 pragma Unreferenced
(Loss_Of_Inheritance
);
639 Result
: Interfaces
.C
.int
;
640 Old
: constant System
.Any_Priority
:= T
.Common
.Current_Priority
;
643 T
.Common
.Current_Priority
:= Prio
;
644 Result
:= pthread_setschedprio
645 (T
.Common
.LL
.Thread
, To_Target_Priority
(Prio
));
646 pragma Assert
(Result
= 0);
648 if T
.Common
.LL
.Thread
= pthread_self
651 -- When lowering the priority via a pthread_setschedprio, QNX ensures
652 -- that the running thread remains in the head of the FIFO for tne
653 -- new priority. Annex D expects the thread to be requeued so let's
654 -- yield to the other threads of the same priority.
655 Result
:= sched_yield
;
656 pragma Assert
(Result
= 0);
664 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
666 return T
.Common
.Current_Priority
;
673 procedure Enter_Task
(Self_ID
: Task_Id
) is
675 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
676 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
678 Specific
.Set
(Self_ID
);
680 if Use_Alternate_Stack
then
682 Stack
: aliased stack_t
;
683 Result
: Interfaces
.C
.int
;
685 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
686 Stack
.ss_size
:= Alternate_Stack_Size
;
688 Result
:= sigaltstack
(Stack
'Access, null);
689 pragma Assert
(Result
= 0);
698 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
700 -----------------------------
701 -- Register_Foreign_Thread --
702 -----------------------------
704 function Register_Foreign_Thread
return Task_Id
is
706 if Is_Valid_Task
then
709 return Register_Foreign_Thread
(pthread_self
);
711 end Register_Foreign_Thread
;
717 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean)
719 Result
: Interfaces
.C
.int
;
720 Cond_Attr
: aliased pthread_condattr_t
;
723 -- Give the task a unique serial number
725 Self_ID
.Serial_Number
:= Next_Serial_Number
;
726 Next_Serial_Number
:= Next_Serial_Number
+ 1;
727 pragma Assert
(Next_Serial_Number
/= 0);
729 if not Single_Lock
then
730 Result
:= Init_Mutex
(Self_ID
.Common
.LL
.L
'Access, Any_Priority
'Last);
731 pragma Assert
(Result
= 0);
739 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
740 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
743 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
744 pragma Assert
(Result
= 0);
748 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
749 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
755 if not Single_Lock
then
756 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
757 pragma Assert
(Result
= 0);
763 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
764 pragma Assert
(Result
= 0);
771 procedure Create_Task
773 Wrapper
: System
.Address
;
774 Stack_Size
: System
.Parameters
.Size_Type
;
775 Priority
: System
.Any_Priority
;
776 Succeeded
: out Boolean)
778 Attributes
: aliased pthread_attr_t
;
779 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
780 Page_Size
: constant Interfaces
.C
.size_t
:=
781 Interfaces
.C
.size_t
(Get_Page_Size
);
782 Sched_Param
: aliased struct_sched_param
;
783 Result
: Interfaces
.C
.int
;
785 Priority_Specific_Policy
: constant Character := Get_Policy
(Priority
);
786 -- Upper case first character of the policy name corresponding to the
787 -- task as set by a Priority_Specific_Dispatching pragma.
789 function Thread_Body_Access
is new
790 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
793 Adjusted_Stack_Size
:=
794 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
796 if Stack_Base_Available
then
798 -- If Stack Checking is supported then allocate 2 additional pages:
800 -- In the worst case, stack is allocated at something like
801 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
802 -- to be sure the effective stack size is greater than what
805 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
808 -- Round stack size as this is required by some OSes (Darwin)
810 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
811 Adjusted_Stack_Size
:=
812 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
814 Result
:= pthread_attr_init
(Attributes
'Access);
815 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
823 pthread_attr_setdetachstate
824 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
825 pragma Assert
(Result
= 0);
828 pthread_attr_setstacksize
829 (Attributes
'Access, Adjusted_Stack_Size
);
830 pragma Assert
(Result
= 0);
832 -- Set thread priority
833 T
.Common
.Current_Priority
:= Priority
;
834 Sched_Param
.sched_priority
:= To_Target_Priority
(Priority
);
836 Result
:= pthread_attr_setinheritsched
837 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
838 pragma Assert
(Result
= 0);
840 Result
:= pthread_attr_setschedparam
841 (Attributes
'Access, Sched_Param
'Access);
842 pragma Assert
(Result
= 0);
844 if Time_Slice_Supported
845 and then (Dispatching_Policy
= 'R'
846 or else Priority_Specific_Policy
= 'R'
847 or else Time_Slice_Val
> 0)
849 Result
:= pthread_attr_setschedpolicy
850 (Attributes
'Access, SCHED_RR
);
852 elsif Dispatching_Policy
= 'F'
853 or else Priority_Specific_Policy
= 'F'
854 or else Time_Slice_Val
= 0
856 Result
:= pthread_attr_setschedpolicy
857 (Attributes
'Access, SCHED_FIFO
);
860 Result
:= pthread_attr_setschedpolicy
861 (Attributes
'Access, SCHED_OTHER
);
864 pragma Assert
(Result
= 0);
866 -- Since the initial signal mask of a thread is inherited from the
867 -- creator, and the Environment task has all its signals masked, we
868 -- do not need to manipulate caller's signal mask at this point.
869 -- All tasks in RTS will have All_Tasks_Mask initially.
871 -- Note: the use of Unrestricted_Access in the following call is needed
872 -- because otherwise we have an error of getting a access-to-volatile
873 -- value which points to a non-volatile object. But in this case it is
874 -- safe to do this, since we know we have no problems with aliasing and
875 -- Unrestricted_Access bypasses this check.
877 Result
:= pthread_create
878 (T
.Common
.LL
.Thread
'Unrestricted_Access,
880 Thread_Body_Access
(Wrapper
),
882 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
884 Succeeded
:= Result
= 0;
886 Result
:= pthread_attr_destroy
(Attributes
'Access);
887 pragma Assert
(Result
= 0);
894 procedure Finalize_TCB
(T
: Task_Id
) is
895 Result
: Interfaces
.C
.int
;
898 if not Single_Lock
then
899 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
900 pragma Assert
(Result
= 0);
903 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
904 pragma Assert
(Result
= 0);
906 if T
.Known_Tasks_Index
/= -1 then
907 Known_Tasks
(T
.Known_Tasks_Index
) := null;
910 ATCB_Allocation
.Free_ATCB
(T
);
917 procedure Exit_Task
is
919 -- Mark this task as unknown, so that if Self is called, it won't
920 -- return a dangling pointer.
929 procedure Abort_Task
(T
: Task_Id
) is
930 Result
: Interfaces
.C
.int
;
932 if Abort_Handler_Installed
then
936 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
937 pragma Assert
(Result
= 0);
945 procedure Initialize
(S
: in out Suspension_Object
) is
946 Mutex_Attr
: aliased pthread_mutexattr_t
;
947 Cond_Attr
: aliased pthread_condattr_t
;
948 Result
: Interfaces
.C
.int
;
951 -- Initialize internal state (always to False (RM D.10 (6)))
956 -- Initialize internal mutex
958 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
959 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
961 if Result
= ENOMEM
then
965 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
966 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
968 if Result
= ENOMEM
then
969 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
970 pragma Assert
(Result
= 0);
975 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
976 pragma Assert
(Result
= 0);
978 -- Initialize internal condition variable
980 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
981 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
984 Result
:= pthread_mutex_destroy
(S
.L
'Access);
985 pragma Assert
(Result
= 0);
987 -- Storage_Error is propagated as intended if the allocation of the
988 -- underlying OS entities fails.
993 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
994 pragma Assert
(Result
= 0);
997 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
998 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1001 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1002 pragma Assert
(Result
= 0);
1004 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1005 pragma Assert
(Result
= 0);
1007 -- Storage_Error is propagated as intended if the allocation of the
1008 -- underlying OS entities fails.
1010 raise Storage_Error
;
1013 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1014 pragma Assert
(Result
= 0);
1021 procedure Finalize
(S
: in out Suspension_Object
) is
1022 Result
: Interfaces
.C
.int
;
1025 -- Destroy internal mutex
1027 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1028 pragma Assert
(Result
= 0);
1030 -- Destroy internal condition variable
1032 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1033 pragma Assert
(Result
= 0);
1040 function Current_State
(S
: Suspension_Object
) return Boolean is
1042 -- We do not want to use lock on this read operation. State is marked
1043 -- as Atomic so that we ensure that the value retrieved is correct.
1052 procedure Set_False
(S
: in out Suspension_Object
) is
1053 Result
: Interfaces
.C
.int
;
1056 SSL
.Abort_Defer
.all;
1058 Result
:= pthread_mutex_lock
(S
.L
'Access);
1059 pragma Assert
(Result
= 0);
1063 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1064 pragma Assert
(Result
= 0);
1066 SSL
.Abort_Undefer
.all;
1073 procedure Set_True
(S
: in out Suspension_Object
) is
1074 Result
: Interfaces
.C
.int
;
1077 SSL
.Abort_Defer
.all;
1079 Result
:= pthread_mutex_lock
(S
.L
'Access);
1080 pragma Assert
(Result
= 0);
1082 -- If there is already a task waiting on this suspension object then
1083 -- we resume it, leaving the state of the suspension object to False,
1084 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1085 -- the state to True.
1091 Result
:= pthread_cond_signal
(S
.CV
'Access);
1092 pragma Assert
(Result
= 0);
1098 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1099 pragma Assert
(Result
= 0);
1101 SSL
.Abort_Undefer
.all;
1104 ------------------------
1105 -- Suspend_Until_True --
1106 ------------------------
1108 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1109 Result
: Interfaces
.C
.int
;
1112 SSL
.Abort_Defer
.all;
1114 Result
:= pthread_mutex_lock
(S
.L
'Access);
1115 pragma Assert
(Result
= 0);
1119 -- Program_Error must be raised upon calling Suspend_Until_True
1120 -- if another task is already waiting on that suspension object
1123 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1124 pragma Assert
(Result
= 0);
1126 SSL
.Abort_Undefer
.all;
1128 raise Program_Error
;
1131 -- Suspend the task if the state is False. Otherwise, the task
1132 -- continues its execution, and the state of the suspension object
1133 -- is set to False (ARM D.10 par. 9).
1141 -- Loop in case pthread_cond_wait returns earlier than expected
1142 -- (e.g. in case of EINTR caused by a signal).
1144 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1145 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1147 exit when not S
.Waiting
;
1151 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1152 pragma Assert
(Result
= 0);
1154 SSL
.Abort_Undefer
.all;
1156 end Suspend_Until_True
;
1164 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1165 pragma Unreferenced
(Self_ID
);
1170 --------------------
1171 -- Check_No_Locks --
1172 --------------------
1174 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1175 pragma Unreferenced
(Self_ID
);
1180 ----------------------
1181 -- Environment_Task --
1182 ----------------------
1184 function Environment_Task
return Task_Id
is
1186 return Environment_Task_Id
;
1187 end Environment_Task
;
1193 procedure Lock_RTS
is
1195 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1202 procedure Unlock_RTS
is
1204 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1211 function Suspend_Task
1213 Thread_Self
: Thread_Id
) return Boolean
1215 pragma Unreferenced
(T
, Thread_Self
);
1224 function Resume_Task
1226 Thread_Self
: Thread_Id
) return Boolean
1228 pragma Unreferenced
(T
, Thread_Self
);
1233 --------------------
1234 -- Stop_All_Tasks --
1235 --------------------
1237 procedure Stop_All_Tasks
is
1246 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1247 pragma Unreferenced
(T
);
1256 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1257 pragma Unreferenced
(T
);
1266 procedure Initialize
(Environment_Task
: Task_Id
) is
1267 act
: aliased struct_sigaction
;
1268 old_act
: aliased struct_sigaction
;
1269 Tmp_Set
: aliased sigset_t
;
1270 Result
: Interfaces
.C
.int
;
1273 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1274 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1275 -- Get interrupt state. Defined in a-init.c
1276 -- The input argument is the interrupt number,
1277 -- and the result is one of the following:
1279 Default
: constant Character := 's';
1280 -- 'n' this interrupt not set by any Interrupt_State pragma
1281 -- 'u' Interrupt_State pragma set state to User
1282 -- 'r' Interrupt_State pragma set state to Runtime
1283 -- 's' Interrupt_State pragma set state to System (use "default"
1287 Environment_Task_Id
:= Environment_Task
;
1289 Interrupt_Management
.Initialize
;
1291 -- Prepare the set of signals that should unblocked in all tasks
1293 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1294 pragma Assert
(Result
= 0);
1296 for J
in Interrupt_Management
.Interrupt_ID
loop
1297 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1298 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1299 pragma Assert
(Result
= 0);
1303 -- Initialize the lock used to synchronize chain of all ATCBs
1305 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1307 Specific
.Initialize
(Environment_Task
);
1309 if Use_Alternate_Stack
then
1310 Environment_Task
.Common
.Task_Alternate_Stack
:=
1311 Alternate_Stack
'Address;
1314 -- Make environment task known here because it doesn't go through
1315 -- Activate_Tasks, which does it for all other tasks.
1317 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1318 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1320 Enter_Task
(Environment_Task
);
1323 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1326 act
.sa_handler
:= Abort_Handler
'Address;
1328 Result
:= sigemptyset
(Tmp_Set
'Access);
1329 pragma Assert
(Result
= 0);
1330 act
.sa_mask
:= Tmp_Set
;
1334 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1335 act
'Unchecked_Access,
1336 old_act
'Unchecked_Access);
1337 pragma Assert
(Result
= 0);
1338 Abort_Handler_Installed
:= True;
1342 -----------------------
1343 -- Set_Task_Affinity --
1344 -----------------------
1346 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1347 pragma Unreferenced
(T
);
1350 -- Setting task affinity is not supported by the underlying system
1353 end Set_Task_Affinity
;
1355 end System
.Task_Primitives
.Operations
;