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.
445 if Result
= 0 and then Locking_Policy
= 'C' then
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 < current priority means Ceiling violation
452 -- (otherwise the current priority == ceiling)
453 if Ceiling
< Sched
.sched_curpriority
then
454 Ceiling_Violation
:= True;
455 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
456 pragma Assert
(Result
= 0);
462 (L
: not null access RTS_Lock
;
463 Global_Lock
: Boolean := False)
465 Result
: Interfaces
.C
.int
;
467 if not Single_Lock
or else Global_Lock
then
468 Result
:= pthread_mutex_lock
(L
);
469 pragma Assert
(Result
= 0);
473 procedure Write_Lock
(T
: Task_Id
) is
474 Result
: Interfaces
.C
.int
;
476 if not Single_Lock
then
477 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
478 pragma Assert
(Result
= 0);
487 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
489 Write_Lock
(L
, Ceiling_Violation
);
496 procedure Unlock
(L
: not null access Lock
) is
497 Result
: Interfaces
.C
.int
;
499 Result
:= pthread_mutex_unlock
(L
.WO
'Access);
500 pragma Assert
(Result
= 0);
504 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False)
506 Result
: Interfaces
.C
.int
;
508 if not Single_Lock
or else Global_Lock
then
509 Result
:= pthread_mutex_unlock
(L
);
510 pragma Assert
(Result
= 0);
514 procedure Unlock
(T
: Task_Id
) is
515 Result
: Interfaces
.C
.int
;
517 if not Single_Lock
then
518 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
519 pragma Assert
(Result
= 0);
527 procedure Set_Ceiling
528 (L
: not null access Lock
;
529 Prio
: System
.Any_Priority
)
531 Result
: Interfaces
.C
.int
;
533 Result
:= pthread_mutex_setprioceiling
534 (L
.WO
'Access, To_Target_Priority
(Prio
), null);
535 pragma Assert
(Result
= 0);
544 Reason
: System
.Tasking
.Task_States
)
546 pragma Unreferenced
(Reason
);
548 Result
: Interfaces
.C
.int
;
553 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
554 mutex
=> (if Single_Lock
555 then Single_RTS_Lock
'Access
556 else Self_ID
.Common
.LL
.L
'Access));
558 -- EINTR is not considered a failure
560 pragma Assert
(Result
= 0 or else Result
= EINTR
);
567 -- This is for use within the run-time system, so abort is
568 -- assumed to be already deferred, and the caller should be
569 -- holding its own ATCB lock.
571 procedure Timed_Sleep
574 Mode
: ST
.Delay_Modes
;
575 Reason
: Task_States
;
576 Timedout
: out Boolean;
577 Yielded
: out Boolean) renames Monotonic
.Timed_Sleep
;
583 -- This is for use in implementing delay statements, so we assume the
584 -- caller is abort-deferred but is holding no locks.
586 procedure Timed_Delay
589 Mode
: ST
.Delay_Modes
) renames Monotonic
.Timed_Delay
;
591 ---------------------
592 -- Monotonic_Clock --
593 ---------------------
595 function Monotonic_Clock
return Duration renames Monotonic
.Monotonic_Clock
;
601 function RT_Resolution
return Duration renames Monotonic
.RT_Resolution
;
607 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
608 pragma Unreferenced
(Reason
);
609 Result
: Interfaces
.C
.int
;
611 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
612 pragma Assert
(Result
= 0);
619 procedure Yield
(Do_Yield
: Boolean := True) is
620 Result
: Interfaces
.C
.int
;
621 pragma Unreferenced
(Result
);
624 Result
:= sched_yield
;
632 procedure Set_Priority
634 Prio
: System
.Any_Priority
;
635 Loss_Of_Inheritance
: Boolean := False)
637 pragma Unreferenced
(Loss_Of_Inheritance
);
638 Result
: Interfaces
.C
.int
;
639 Old
: constant System
.Any_Priority
:= T
.Common
.Current_Priority
;
642 T
.Common
.Current_Priority
:= Prio
;
643 Result
:= pthread_setschedprio
644 (T
.Common
.LL
.Thread
, To_Target_Priority
(Prio
));
645 pragma Assert
(Result
= 0);
647 if T
.Common
.LL
.Thread
= pthread_self
650 -- When lowering the priority via a pthread_setschedprio, QNX ensures
651 -- that the running thread remains in the head of the FIFO for tne
652 -- new priority. Annex D expects the thread to be requeued so let's
653 -- yield to the other threads of the same priority.
654 Result
:= sched_yield
;
655 pragma Assert
(Result
= 0);
663 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
665 return T
.Common
.Current_Priority
;
672 procedure Enter_Task
(Self_ID
: Task_Id
) is
674 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
675 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
677 Specific
.Set
(Self_ID
);
679 if Use_Alternate_Stack
then
681 Stack
: aliased stack_t
;
682 Result
: Interfaces
.C
.int
;
684 Stack
.ss_sp
:= Self_ID
.Common
.Task_Alternate_Stack
;
685 Stack
.ss_size
:= Alternate_Stack_Size
;
687 Result
:= sigaltstack
(Stack
'Access, null);
688 pragma Assert
(Result
= 0);
697 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
699 -----------------------------
700 -- Register_Foreign_Thread --
701 -----------------------------
703 function Register_Foreign_Thread
return Task_Id
is
705 if Is_Valid_Task
then
708 return Register_Foreign_Thread
(pthread_self
);
710 end Register_Foreign_Thread
;
716 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean)
718 Result
: Interfaces
.C
.int
;
719 Cond_Attr
: aliased pthread_condattr_t
;
722 -- Give the task a unique serial number
724 Self_ID
.Serial_Number
:= Next_Serial_Number
;
725 Next_Serial_Number
:= Next_Serial_Number
+ 1;
726 pragma Assert
(Next_Serial_Number
/= 0);
728 if not Single_Lock
then
729 Result
:= Init_Mutex
(Self_ID
.Common
.LL
.L
'Access, Any_Priority
'Last);
730 pragma Assert
(Result
= 0);
738 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
739 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
742 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
743 pragma Assert
(Result
= 0);
747 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
748 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
754 if not Single_Lock
then
755 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
756 pragma Assert
(Result
= 0);
762 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
763 pragma Assert
(Result
= 0);
770 procedure Create_Task
772 Wrapper
: System
.Address
;
773 Stack_Size
: System
.Parameters
.Size_Type
;
774 Priority
: System
.Any_Priority
;
775 Succeeded
: out Boolean)
777 Attributes
: aliased pthread_attr_t
;
778 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
779 Page_Size
: constant Interfaces
.C
.size_t
:=
780 Interfaces
.C
.size_t
(Get_Page_Size
);
781 Sched_Param
: aliased struct_sched_param
;
782 Result
: Interfaces
.C
.int
;
784 Priority_Specific_Policy
: constant Character := Get_Policy
(Priority
);
785 -- Upper case first character of the policy name corresponding to the
786 -- task as set by a Priority_Specific_Dispatching pragma.
788 function Thread_Body_Access
is new
789 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
792 Adjusted_Stack_Size
:=
793 Interfaces
.C
.size_t
(Stack_Size
+ Alternate_Stack_Size
);
795 if Stack_Base_Available
then
797 -- If Stack Checking is supported then allocate 2 additional pages:
799 -- In the worst case, stack is allocated at something like
800 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
801 -- to be sure the effective stack size is greater than what
804 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2 * Page_Size
;
807 -- Round stack size as this is required by some OSes (Darwin)
809 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ Page_Size
- 1;
810 Adjusted_Stack_Size
:=
811 Adjusted_Stack_Size
- Adjusted_Stack_Size
mod Page_Size
;
813 Result
:= pthread_attr_init
(Attributes
'Access);
814 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
822 pthread_attr_setdetachstate
823 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
824 pragma Assert
(Result
= 0);
827 pthread_attr_setstacksize
828 (Attributes
'Access, Adjusted_Stack_Size
);
829 pragma Assert
(Result
= 0);
831 -- Set thread priority
832 T
.Common
.Current_Priority
:= Priority
;
833 Sched_Param
.sched_priority
:= To_Target_Priority
(Priority
);
835 Result
:= pthread_attr_setinheritsched
836 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
837 pragma Assert
(Result
= 0);
839 Result
:= pthread_attr_setschedparam
840 (Attributes
'Access, Sched_Param
'Access);
841 pragma Assert
(Result
= 0);
843 if Time_Slice_Supported
844 and then (Dispatching_Policy
= 'R'
845 or else Priority_Specific_Policy
= 'R'
846 or else Time_Slice_Val
> 0)
848 Result
:= pthread_attr_setschedpolicy
849 (Attributes
'Access, SCHED_RR
);
851 elsif Dispatching_Policy
= 'F'
852 or else Priority_Specific_Policy
= 'F'
853 or else Time_Slice_Val
= 0
855 Result
:= pthread_attr_setschedpolicy
856 (Attributes
'Access, SCHED_FIFO
);
859 Result
:= pthread_attr_setschedpolicy
860 (Attributes
'Access, SCHED_OTHER
);
863 pragma Assert
(Result
= 0);
865 -- Since the initial signal mask of a thread is inherited from the
866 -- creator, and the Environment task has all its signals masked, we
867 -- do not need to manipulate caller's signal mask at this point.
868 -- All tasks in RTS will have All_Tasks_Mask initially.
870 -- Note: the use of Unrestricted_Access in the following call is needed
871 -- because otherwise we have an error of getting a access-to-volatile
872 -- value which points to a non-volatile object. But in this case it is
873 -- safe to do this, since we know we have no problems with aliasing and
874 -- Unrestricted_Access bypasses this check.
876 Result
:= pthread_create
877 (T
.Common
.LL
.Thread
'Unrestricted_Access,
879 Thread_Body_Access
(Wrapper
),
881 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
883 Succeeded
:= Result
= 0;
885 Result
:= pthread_attr_destroy
(Attributes
'Access);
886 pragma Assert
(Result
= 0);
893 procedure Finalize_TCB
(T
: Task_Id
) is
894 Result
: Interfaces
.C
.int
;
897 if not Single_Lock
then
898 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
899 pragma Assert
(Result
= 0);
902 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
903 pragma Assert
(Result
= 0);
905 if T
.Known_Tasks_Index
/= -1 then
906 Known_Tasks
(T
.Known_Tasks_Index
) := null;
909 ATCB_Allocation
.Free_ATCB
(T
);
916 procedure Exit_Task
is
918 -- Mark this task as unknown, so that if Self is called, it won't
919 -- return a dangling pointer.
928 procedure Abort_Task
(T
: Task_Id
) is
929 Result
: Interfaces
.C
.int
;
931 if Abort_Handler_Installed
then
935 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
936 pragma Assert
(Result
= 0);
944 procedure Initialize
(S
: in out Suspension_Object
) is
945 Mutex_Attr
: aliased pthread_mutexattr_t
;
946 Cond_Attr
: aliased pthread_condattr_t
;
947 Result
: Interfaces
.C
.int
;
950 -- Initialize internal state (always to False (RM D.10 (6)))
955 -- Initialize internal mutex
957 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
958 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
960 if Result
= ENOMEM
then
964 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
965 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
967 if Result
= ENOMEM
then
968 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
969 pragma Assert
(Result
= 0);
974 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
975 pragma Assert
(Result
= 0);
977 -- Initialize internal condition variable
979 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
980 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
983 Result
:= pthread_mutex_destroy
(S
.L
'Access);
984 pragma Assert
(Result
= 0);
986 -- Storage_Error is propagated as intended if the allocation of the
987 -- underlying OS entities fails.
992 Result
:= GNAT_pthread_condattr_setup
(Cond_Attr
'Access);
993 pragma Assert
(Result
= 0);
996 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
997 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1000 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1001 pragma Assert
(Result
= 0);
1003 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1004 pragma Assert
(Result
= 0);
1006 -- Storage_Error is propagated as intended if the allocation of the
1007 -- underlying OS entities fails.
1009 raise Storage_Error
;
1012 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1013 pragma Assert
(Result
= 0);
1020 procedure Finalize
(S
: in out Suspension_Object
) is
1021 Result
: Interfaces
.C
.int
;
1024 -- Destroy internal mutex
1026 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1027 pragma Assert
(Result
= 0);
1029 -- Destroy internal condition variable
1031 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1032 pragma Assert
(Result
= 0);
1039 function Current_State
(S
: Suspension_Object
) return Boolean is
1041 -- We do not want to use lock on this read operation. State is marked
1042 -- as Atomic so that we ensure that the value retrieved is correct.
1051 procedure Set_False
(S
: in out Suspension_Object
) is
1052 Result
: Interfaces
.C
.int
;
1055 SSL
.Abort_Defer
.all;
1057 Result
:= pthread_mutex_lock
(S
.L
'Access);
1058 pragma Assert
(Result
= 0);
1062 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1063 pragma Assert
(Result
= 0);
1065 SSL
.Abort_Undefer
.all;
1072 procedure Set_True
(S
: in out Suspension_Object
) is
1073 Result
: Interfaces
.C
.int
;
1076 SSL
.Abort_Defer
.all;
1078 Result
:= pthread_mutex_lock
(S
.L
'Access);
1079 pragma Assert
(Result
= 0);
1081 -- If there is already a task waiting on this suspension object then
1082 -- we resume it, leaving the state of the suspension object to False,
1083 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1084 -- the state to True.
1090 Result
:= pthread_cond_signal
(S
.CV
'Access);
1091 pragma Assert
(Result
= 0);
1097 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1098 pragma Assert
(Result
= 0);
1100 SSL
.Abort_Undefer
.all;
1103 ------------------------
1104 -- Suspend_Until_True --
1105 ------------------------
1107 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1108 Result
: Interfaces
.C
.int
;
1111 SSL
.Abort_Defer
.all;
1113 Result
:= pthread_mutex_lock
(S
.L
'Access);
1114 pragma Assert
(Result
= 0);
1118 -- Program_Error must be raised upon calling Suspend_Until_True
1119 -- if another task is already waiting on that suspension object
1122 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1123 pragma Assert
(Result
= 0);
1125 SSL
.Abort_Undefer
.all;
1127 raise Program_Error
;
1130 -- Suspend the task if the state is False. Otherwise, the task
1131 -- continues its execution, and the state of the suspension object
1132 -- is set to False (ARM D.10 par. 9).
1140 -- Loop in case pthread_cond_wait returns earlier than expected
1141 -- (e.g. in case of EINTR caused by a signal).
1143 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1144 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1146 exit when not S
.Waiting
;
1150 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1151 pragma Assert
(Result
= 0);
1153 SSL
.Abort_Undefer
.all;
1155 end Suspend_Until_True
;
1163 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1164 pragma Unreferenced
(Self_ID
);
1169 --------------------
1170 -- Check_No_Locks --
1171 --------------------
1173 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1174 pragma Unreferenced
(Self_ID
);
1179 ----------------------
1180 -- Environment_Task --
1181 ----------------------
1183 function Environment_Task
return Task_Id
is
1185 return Environment_Task_Id
;
1186 end Environment_Task
;
1192 procedure Lock_RTS
is
1194 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1201 procedure Unlock_RTS
is
1203 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1210 function Suspend_Task
1212 Thread_Self
: Thread_Id
) return Boolean
1214 pragma Unreferenced
(T
, Thread_Self
);
1223 function Resume_Task
1225 Thread_Self
: Thread_Id
) return Boolean
1227 pragma Unreferenced
(T
, Thread_Self
);
1232 --------------------
1233 -- Stop_All_Tasks --
1234 --------------------
1236 procedure Stop_All_Tasks
is
1245 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1246 pragma Unreferenced
(T
);
1255 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1256 pragma Unreferenced
(T
);
1265 procedure Initialize
(Environment_Task
: Task_Id
) is
1266 act
: aliased struct_sigaction
;
1267 old_act
: aliased struct_sigaction
;
1268 Tmp_Set
: aliased sigset_t
;
1269 Result
: Interfaces
.C
.int
;
1272 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1273 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1274 -- Get interrupt state. Defined in a-init.c
1275 -- The input argument is the interrupt number,
1276 -- and the result is one of the following:
1278 Default
: constant Character := 's';
1279 -- 'n' this interrupt not set by any Interrupt_State pragma
1280 -- 'u' Interrupt_State pragma set state to User
1281 -- 'r' Interrupt_State pragma set state to Runtime
1282 -- 's' Interrupt_State pragma set state to System (use "default"
1286 Environment_Task_Id
:= Environment_Task
;
1288 Interrupt_Management
.Initialize
;
1290 -- Prepare the set of signals that should unblocked in all tasks
1292 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1293 pragma Assert
(Result
= 0);
1295 for J
in Interrupt_Management
.Interrupt_ID
loop
1296 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1297 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1298 pragma Assert
(Result
= 0);
1302 -- Initialize the lock used to synchronize chain of all ATCBs
1304 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1306 Specific
.Initialize
(Environment_Task
);
1308 if Use_Alternate_Stack
then
1309 Environment_Task
.Common
.Task_Alternate_Stack
:=
1310 Alternate_Stack
'Address;
1313 -- Make environment task known here because it doesn't go through
1314 -- Activate_Tasks, which does it for all other tasks.
1316 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1317 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1319 Enter_Task
(Environment_Task
);
1322 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
1325 act
.sa_handler
:= Abort_Handler
'Address;
1327 Result
:= sigemptyset
(Tmp_Set
'Access);
1328 pragma Assert
(Result
= 0);
1329 act
.sa_mask
:= Tmp_Set
;
1333 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1334 act
'Unchecked_Access,
1335 old_act
'Unchecked_Access);
1336 pragma Assert
(Result
= 0);
1337 Abort_Handler_Installed
:= True;
1341 -----------------------
1342 -- Set_Task_Affinity --
1343 -----------------------
1345 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1346 pragma Unreferenced
(T
);
1349 -- Setting task affinity is not supported by the underlying system
1352 end Set_Task_Affinity
;
1354 end System
.Task_Primitives
.Operations
;