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-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 IRIX (pthread library) version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
47 with System
.Task_Info
;
49 with System
.Tasking
.Debug
;
50 -- used for Known_Tasks
52 with System
.Interrupt_Management
;
53 -- used for Keep_Unmasked
54 -- Abort_Task_Interrupt
57 with System
.OS_Primitives
;
58 -- used for Delay_Modes
63 with System
.Soft_Links
;
64 -- used for Abort_Defer/Undefer
66 -- We use System.Soft_Links instead of System.Tasking.Initialization
67 -- because the later is a higher level package that we shouldn't depend on.
68 -- For example when using the restricted run time, it is replaced by
69 -- System.Tasking.Restricted.Stages.
71 with Unchecked_Conversion
;
72 with Unchecked_Deallocation
;
74 package body System
.Task_Primitives
.Operations
is
76 package SSL
renames System
.Soft_Links
;
79 use System
.Tasking
.Debug
;
81 use System
.OS_Interface
;
82 use System
.OS_Primitives
;
83 use System
.Parameters
;
89 -- The followings are logically constants, but need to be initialized
92 Single_RTS_Lock
: aliased RTS_Lock
;
93 -- This is a lock to allow only one thread of control in the RTS at
94 -- a time; it is used to execute in mutual exclusion from all other tasks.
95 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
97 ATCB_Key
: aliased pthread_key_t
;
98 -- Key used to find the Ada Task_Id associated with a thread
100 Environment_Task_Id
: Task_Id
;
101 -- A variable to hold Task_Id for the environment task
103 Locking_Policy
: Character;
104 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
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 Real_Time_Clock_Id
: constant clockid_t
:= CLOCK_REALTIME
;
114 Unblocked_Signal_Mask
: aliased sigset_t
;
116 Foreign_Task_Elaborated
: aliased Boolean := True;
117 -- Used to identified fake tasks (i.e., non-Ada Threads)
125 procedure Initialize
(Environment_Task
: Task_Id
);
126 pragma Inline
(Initialize
);
127 -- Initialize various data needed by this package
129 function Is_Valid_Task
return Boolean;
130 pragma Inline
(Is_Valid_Task
);
131 -- Does executing thread have a TCB?
133 procedure Set
(Self_Id
: Task_Id
);
135 -- Set the self id for the current task
137 function Self
return Task_Id
;
138 pragma Inline
(Self
);
139 -- Return a pointer to the Ada Task Control Block of the calling task
143 package body Specific
is separate;
144 -- The body of this package is target specific
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
150 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
151 -- Allocate and Initialize a new ATCB for the current Thread
153 function Register_Foreign_Thread
154 (Thread
: Thread_Id
) return Task_Id
is separate;
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
162 procedure Abort_Handler
(Sig
: Signal
);
163 -- Signal handler used to implement asynchronous abort
169 procedure Abort_Handler
(Sig
: Signal
) is
170 pragma Unreferenced
(Sig
);
172 T
: constant Task_Id
:= Self
;
173 Result
: Interfaces
.C
.int
;
174 Old_Set
: aliased sigset_t
;
177 -- It is not safe to raise an exception when using ZCX and the GCC
178 -- exception handling mechanism.
180 if ZCX_By_Default
and then GCC_ZCX_Support
then
184 if T
.Deferral_Level
= 0
185 and then T
.Pending_ATC_Level
< T
.ATC_Nesting_Level
187 -- Make sure signals used for RTS internal purpose are unmasked
189 Result
:= pthread_sigmask
191 Unblocked_Signal_Mask
'Unchecked_Access,
192 Old_Set
'Unchecked_Access);
193 pragma Assert
(Result
= 0);
195 raise Standard
'Abort_Signal;
203 -- The underlying thread system sets a guard page at the
204 -- bottom of a thread stack, so nothing is needed.
206 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
207 pragma Unreferenced
(On
);
208 pragma Unreferenced
(T
);
217 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
219 return T
.Common
.LL
.Thread
;
226 function Self
return Task_Id
renames Specific
.Self
;
228 ---------------------
229 -- Initialize_Lock --
230 ---------------------
232 -- Note: mutexes and cond_variables needed per-task basis are
233 -- initialized in Initialize_TCB and the Storage_Error is
234 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
235 -- used in RTS is initialized before any status change of RTS.
236 -- Therefore rasing Storage_Error in the following routines
237 -- should be able to be handled safely.
239 procedure Initialize_Lock
240 (Prio
: System
.Any_Priority
;
243 Attributes
: aliased pthread_mutexattr_t
;
244 Result
: Interfaces
.C
.int
;
247 Result
:= pthread_mutexattr_init
(Attributes
'Access);
248 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
250 if Result
= ENOMEM
then
254 if Locking_Policy
= 'C' then
255 Result
:= pthread_mutexattr_setprotocol
256 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
257 pragma Assert
(Result
= 0);
259 Result
:= pthread_mutexattr_setprioceiling
260 (Attributes
'Access, Interfaces
.C
.int
(Prio
));
261 pragma Assert
(Result
= 0);
264 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
265 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
267 if Result
= ENOMEM
then
268 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
272 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
273 pragma Assert
(Result
= 0);
276 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
277 pragma Unreferenced
(Level
);
279 Attributes
: aliased pthread_mutexattr_t
;
280 Result
: Interfaces
.C
.int
;
283 Result
:= pthread_mutexattr_init
(Attributes
'Access);
284 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
286 if Result
= ENOMEM
then
290 if Locking_Policy
= 'C' then
291 Result
:= pthread_mutexattr_setprotocol
292 (Attributes
'Access, PTHREAD_PRIO_PROTECT
);
293 pragma Assert
(Result
= 0);
295 Result
:= pthread_mutexattr_setprioceiling
296 (Attributes
'Access, Interfaces
.C
.int
(System
.Any_Priority
'Last));
297 pragma Assert
(Result
= 0);
300 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
302 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
304 if Result
= ENOMEM
then
305 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
309 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
310 pragma Assert
(Result
= 0);
317 procedure Finalize_Lock
(L
: access Lock
) is
318 Result
: Interfaces
.C
.int
;
320 Result
:= pthread_mutex_destroy
(L
);
321 pragma Assert
(Result
= 0);
324 procedure Finalize_Lock
(L
: access RTS_Lock
) is
325 Result
: Interfaces
.C
.int
;
327 Result
:= pthread_mutex_destroy
(L
);
328 pragma Assert
(Result
= 0);
335 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
336 Result
: Interfaces
.C
.int
;
338 Result
:= pthread_mutex_lock
(L
);
339 Ceiling_Violation
:= Result
= EINVAL
;
341 -- Assumes the cause of EINVAL is a priority ceiling violation
343 pragma Assert
(Result
= 0 or else Result
= EINVAL
);
347 (L
: access RTS_Lock
;
348 Global_Lock
: Boolean := False)
350 Result
: Interfaces
.C
.int
;
352 if not Single_Lock
or else Global_Lock
then
353 Result
:= pthread_mutex_lock
(L
);
354 pragma Assert
(Result
= 0);
358 procedure Write_Lock
(T
: Task_Id
) is
359 Result
: Interfaces
.C
.int
;
361 if not Single_Lock
then
362 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
363 pragma Assert
(Result
= 0);
371 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
373 Write_Lock
(L
, Ceiling_Violation
);
380 procedure Unlock
(L
: access Lock
) is
381 Result
: Interfaces
.C
.int
;
383 Result
:= pthread_mutex_unlock
(L
);
384 pragma Assert
(Result
= 0);
387 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
388 Result
: Interfaces
.C
.int
;
391 if not Single_Lock
or else Global_Lock
then
392 Result
:= pthread_mutex_unlock
(L
);
393 pragma Assert
(Result
= 0);
397 procedure Unlock
(T
: Task_Id
) is
398 Result
: Interfaces
.C
.int
;
401 if not Single_Lock
then
402 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
403 pragma Assert
(Result
= 0);
412 (Self_ID
: ST
.Task_Id
;
413 Reason
: System
.Tasking
.Task_States
)
415 pragma Unreferenced
(Reason
);
417 Result
: Interfaces
.C
.int
;
421 Result
:= pthread_cond_wait
422 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
424 Result
:= pthread_cond_wait
425 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
428 -- EINTR is not considered a failure
430 pragma Assert
(Result
= 0 or else Result
= EINTR
);
437 procedure Timed_Sleep
440 Mode
: ST
.Delay_Modes
;
441 Reason
: Task_States
;
442 Timedout
: out Boolean;
443 Yielded
: out Boolean)
445 pragma Unreferenced
(Reason
);
447 Check_Time
: constant Duration := Monotonic_Clock
;
449 Request
: aliased timespec
;
450 Result
: Interfaces
.C
.int
;
456 if Mode
= Relative
then
457 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
459 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
462 if Abs_Time
> Check_Time
then
463 Request
:= To_Timespec
(Abs_Time
);
466 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
467 or else Self_ID
.Pending_Priority_Change
;
470 Result
:= pthread_cond_timedwait
471 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access,
475 Result
:= pthread_cond_timedwait
476 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access,
480 exit when Abs_Time
<= Monotonic_Clock
;
482 if Result
= 0 or else errno
= EINTR
then
494 -- This is for use in implementing delay statements, so we assume
495 -- the caller is abort-deferred but is holding no locks.
497 procedure Timed_Delay
500 Mode
: ST
.Delay_Modes
)
502 Check_Time
: constant Duration := Monotonic_Clock
;
504 Request
: aliased timespec
;
505 Result
: Interfaces
.C
.int
;
512 Write_Lock
(Self_ID
);
514 if Mode
= Relative
then
515 Abs_Time
:= Time
+ Check_Time
;
517 Abs_Time
:= Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
);
520 if Abs_Time
> Check_Time
then
521 Request
:= To_Timespec
(Abs_Time
);
522 Self_ID
.Common
.State
:= Delay_Sleep
;
525 if Self_ID
.Pending_Priority_Change
then
526 Self_ID
.Pending_Priority_Change
:= False;
527 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
528 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
531 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
533 Result
:= pthread_cond_timedwait
(Self_ID
.Common
.LL
.CV
'Access,
534 Self_ID
.Common
.LL
.L
'Access, Request
'Access);
535 exit when Abs_Time
<= Monotonic_Clock
;
537 pragma Assert
(Result
= 0
538 or else Result
= ETIMEDOUT
539 or else Result
= EINTR
);
542 Self_ID
.Common
.State
:= Runnable
;
554 ---------------------
555 -- Monotonic_Clock --
556 ---------------------
558 function Monotonic_Clock
return Duration is
559 TS
: aliased timespec
;
560 Result
: Interfaces
.C
.int
;
562 Result
:= clock_gettime
(Real_Time_Clock_Id
, TS
'Unchecked_Access);
563 pragma Assert
(Result
= 0);
564 return To_Duration
(TS
);
571 function RT_Resolution
return Duration is
573 -- The clock_getres (Real_Time_Clock_Id) function appears to return
574 -- the interrupt resolution of the realtime clock and not the actual
575 -- resolution of reading the clock. Even though this last value is
576 -- only guaranteed to be 100 Hz, at least the Origin 200 appears to
577 -- have a microsecond resolution or better.
579 -- ??? We should figure out a method to return the right value on
589 procedure Wakeup
(T
: ST
.Task_Id
; Reason
: System
.Tasking
.Task_States
) is
590 pragma Unreferenced
(Reason
);
591 Result
: Interfaces
.C
.int
;
593 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
594 pragma Assert
(Result
= 0);
601 procedure Yield
(Do_Yield
: Boolean := True) is
602 Result
: Interfaces
.C
.int
;
603 pragma Unreferenced
(Result
);
606 Result
:= sched_yield
;
614 procedure Set_Priority
616 Prio
: System
.Any_Priority
;
617 Loss_Of_Inheritance
: Boolean := False)
619 pragma Unreferenced
(Loss_Of_Inheritance
);
621 Result
: Interfaces
.C
.int
;
622 Param
: aliased struct_sched_param
;
623 Sched_Policy
: Interfaces
.C
.int
;
625 use type System
.Task_Info
.Task_Info_Type
;
627 function To_Int
is new Unchecked_Conversion
628 (System
.Task_Info
.Thread_Scheduling_Policy
, Interfaces
.C
.int
);
630 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
631 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
632 -- Get priority specific dispatching policy
634 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
635 -- Upper case first character of the policy name corresponding to the
636 -- task as set by a Priority_Specific_Dispatching pragma.
639 T
.Common
.Current_Priority
:= Prio
;
640 Param
.sched_priority
:= Interfaces
.C
.int
(Prio
);
642 if T
.Common
.Task_Info
/= null then
643 Sched_Policy
:= To_Int
(T
.Common
.Task_Info
.Policy
);
645 elsif Dispatching_Policy
= 'R'
646 or else Priority_Specific_Policy
= 'R'
647 or else Time_Slice_Val
> 0
649 Sched_Policy
:= SCHED_RR
;
652 Sched_Policy
:= SCHED_FIFO
;
655 Result
:= pthread_setschedparam
(T
.Common
.LL
.Thread
, Sched_Policy
,
657 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
674 Result
: Interfaces
.C
.int
;
676 function To_Int
is new Unchecked_Conversion
677 (System
.Task_Info
.CPU_Number
, Interfaces
.C
.int
);
679 use System
.Task_Info
;
682 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
683 Specific
.Set
(Self_ID
);
685 if Self_ID
.Common
.Task_Info
/= null
686 and then Self_ID
.Common
.Task_Info
.Scope
= PTHREAD_SCOPE_SYSTEM
687 and then Self_ID
.Common
.Task_Info
.Runon_CPU
/= ANY_CPU
689 Result
:= pthread_setrunon_np
690 (To_Int
(Self_ID
.Common
.Task_Info
.Runon_CPU
));
691 pragma Assert
(Result
= 0);
696 for J
in Known_Tasks
'Range loop
697 if Known_Tasks
(J
) = null then
698 Known_Tasks
(J
) := Self_ID
;
699 Self_ID
.Known_Tasks_Index
:= J
;
711 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
713 return new Ada_Task_Control_Block
(Entry_Num
);
720 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
722 -----------------------------
723 -- Register_Foreign_Thread --
724 -----------------------------
726 function Register_Foreign_Thread
return Task_Id
is
728 if Is_Valid_Task
then
731 return Register_Foreign_Thread
(pthread_self
);
733 end Register_Foreign_Thread
;
739 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
740 Result
: Interfaces
.C
.int
;
741 Cond_Attr
: aliased pthread_condattr_t
;
744 if not Single_Lock
then
745 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
748 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
749 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
752 Result
:= pthread_cond_init
(Self_ID
.Common
.LL
.CV
'Access,
754 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
760 if not Single_Lock
then
761 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
762 pragma Assert
(Result
= 0);
768 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
769 pragma Assert
(Result
= 0);
776 procedure Create_Task
778 Wrapper
: System
.Address
;
779 Stack_Size
: System
.Parameters
.Size_Type
;
780 Priority
: System
.Any_Priority
;
781 Succeeded
: out Boolean)
783 use System
.Task_Info
;
785 Attributes
: aliased pthread_attr_t
;
786 Sched_Param
: aliased struct_sched_param
;
787 Result
: Interfaces
.C
.int
;
789 function Thread_Body_Access
is new
790 Unchecked_Conversion
(System
.Address
, Thread_Body
);
792 function To_Int
is new Unchecked_Conversion
793 (System
.Task_Info
.Thread_Scheduling_Scope
, Interfaces
.C
.int
);
794 function To_Int
is new Unchecked_Conversion
795 (System
.Task_Info
.Thread_Scheduling_Inheritance
, Interfaces
.C
.int
);
796 function To_Int
is new Unchecked_Conversion
797 (System
.Task_Info
.Thread_Scheduling_Policy
, Interfaces
.C
.int
);
800 Result
:= pthread_attr_init
(Attributes
'Access);
801 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
808 Result
:= pthread_attr_setdetachstate
809 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
810 pragma Assert
(Result
= 0);
812 Result
:= pthread_attr_setstacksize
813 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
814 pragma Assert
(Result
= 0);
816 if T
.Common
.Task_Info
/= null then
817 Result
:= pthread_attr_setscope
818 (Attributes
'Access, To_Int
(T
.Common
.Task_Info
.Scope
));
819 pragma Assert
(Result
= 0);
821 Result
:= pthread_attr_setinheritsched
822 (Attributes
'Access, To_Int
(T
.Common
.Task_Info
.Inheritance
));
823 pragma Assert
(Result
= 0);
825 Result
:= pthread_attr_setschedpolicy
826 (Attributes
'Access, To_Int
(T
.Common
.Task_Info
.Policy
));
827 pragma Assert
(Result
= 0);
829 Sched_Param
.sched_priority
:=
830 Interfaces
.C
.int
(T
.Common
.Task_Info
.Priority
);
832 Result
:= pthread_attr_setschedparam
833 (Attributes
'Access, Sched_Param
'Access);
834 pragma Assert
(Result
= 0);
837 -- Since the initial signal mask of a thread is inherited from the
838 -- creator, and the Environment task has all its signals masked, we
839 -- do not need to manipulate caller's signal mask at this point.
840 -- All tasks in RTS will have All_Tasks_Mask initially.
842 Result
:= pthread_create
843 (T
.Common
.LL
.Thread
'Access,
845 Thread_Body_Access
(Wrapper
),
849 and then T
.Common
.Task_Info
/= null
850 and then T
.Common
.Task_Info
.Scope
= PTHREAD_SCOPE_SYSTEM
852 -- The pthread_create call may have failed because we
853 -- asked for a system scope pthread and none were
854 -- available (probably because the program was not executed
855 -- by the superuser). Let's try for a process scope pthread
856 -- instead of raising Tasking_Error.
859 ("Request for PTHREAD_SCOPE_SYSTEM in Task_Info pragma for task");
860 System
.IO
.Put
("""");
861 System
.IO
.Put
(T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
));
862 System
.IO
.Put_Line
(""" could not be honored. ");
863 System
.IO
.Put_Line
("Scope changed to PTHREAD_SCOPE_PROCESS");
865 T
.Common
.Task_Info
.Scope
:= PTHREAD_SCOPE_PROCESS
;
866 Result
:= pthread_attr_setscope
867 (Attributes
'Access, To_Int
(T
.Common
.Task_Info
.Scope
));
868 pragma Assert
(Result
= 0);
870 Result
:= pthread_create
871 (T
.Common
.LL
.Thread
'Access,
873 Thread_Body_Access
(Wrapper
),
877 pragma Assert
(Result
= 0 or else Result
= EAGAIN
);
879 Succeeded
:= Result
= 0;
881 -- The following needs significant commenting ???
883 if T
.Common
.Task_Info
/= null then
884 T
.Common
.Base_Priority
:= T
.Common
.Task_Info
.Priority
;
885 Set_Priority
(T
, T
.Common
.Task_Info
.Priority
);
887 Set_Priority
(T
, Priority
);
890 Result
:= pthread_attr_destroy
(Attributes
'Access);
891 pragma Assert
(Result
= 0);
898 procedure Finalize_TCB
(T
: Task_Id
) is
899 Result
: Interfaces
.C
.int
;
901 Is_Self
: constant Boolean := T
= Self
;
903 procedure Free
is new
904 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
907 if not Single_Lock
then
908 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
909 pragma Assert
(Result
= 0);
912 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
913 pragma Assert
(Result
= 0);
915 if T
.Known_Tasks_Index
/= -1 then
916 Known_Tasks
(T
.Known_Tasks_Index
) := null;
930 procedure Exit_Task
is
939 procedure Abort_Task
(T
: Task_Id
) is
940 Result
: Interfaces
.C
.int
;
942 Result
:= pthread_kill
(T
.Common
.LL
.Thread
,
943 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
944 pragma Assert
(Result
= 0);
951 procedure Initialize
(S
: in out Suspension_Object
) is
952 Mutex_Attr
: aliased pthread_mutexattr_t
;
953 Cond_Attr
: aliased pthread_condattr_t
;
954 Result
: Interfaces
.C
.int
;
956 -- Initialize internal state. It is always initialized to False (ARM
962 -- Initialize internal mutex
964 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
965 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
967 if Result
= ENOMEM
then
971 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
972 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
974 if Result
= ENOMEM
then
975 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
976 pragma Assert
(Result
= 0);
981 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
982 pragma Assert
(Result
= 0);
984 -- Initialize internal condition variable
986 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
987 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
990 Result
:= pthread_mutex_destroy
(S
.L
'Access);
991 pragma Assert
(Result
= 0);
993 if Result
= ENOMEM
then
998 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
999 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1002 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1003 pragma Assert
(Result
= 0);
1005 if Result
= ENOMEM
then
1006 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1007 pragma Assert
(Result
= 0);
1009 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
;
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
;
1054 SSL
.Abort_Defer
.all;
1056 Result
:= pthread_mutex_lock
(S
.L
'Access);
1057 pragma Assert
(Result
= 0);
1061 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1062 pragma Assert
(Result
= 0);
1064 SSL
.Abort_Undefer
.all;
1071 procedure Set_True
(S
: in out Suspension_Object
) is
1072 Result
: Interfaces
.C
.int
;
1074 SSL
.Abort_Defer
.all;
1076 Result
:= pthread_mutex_lock
(S
.L
'Access);
1077 pragma Assert
(Result
= 0);
1079 -- If there is already a task waiting on this suspension object then
1080 -- we resume it, leaving the state of the suspension object to False,
1081 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1082 -- the state to True.
1088 Result
:= pthread_cond_signal
(S
.CV
'Access);
1089 pragma Assert
(Result
= 0);
1094 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1095 pragma Assert
(Result
= 0);
1097 SSL
.Abort_Undefer
.all;
1100 ------------------------
1101 -- Suspend_Until_True --
1102 ------------------------
1104 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1105 Result
: Interfaces
.C
.int
;
1107 SSL
.Abort_Defer
.all;
1109 Result
:= pthread_mutex_lock
(S
.L
'Access);
1110 pragma Assert
(Result
= 0);
1113 -- Program_Error must be raised upon calling Suspend_Until_True
1114 -- if another task is already waiting on that suspension object
1115 -- (ARM D.10 par. 10).
1117 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1118 pragma Assert
(Result
= 0);
1120 SSL
.Abort_Undefer
.all;
1122 raise Program_Error
;
1124 -- Suspend the task if the state is False. Otherwise, the task
1125 -- continues its execution, and the state of the suspension object
1126 -- is set to False (ARM D.10 par. 9).
1132 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1135 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1136 pragma Assert
(Result
= 0);
1138 SSL
.Abort_Undefer
.all;
1140 end Suspend_Until_True
;
1148 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1149 pragma Unreferenced
(Self_ID
);
1154 --------------------
1155 -- Check_No_Locks --
1156 --------------------
1158 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1159 pragma Unreferenced
(Self_ID
);
1164 ----------------------
1165 -- Environment_Task --
1166 ----------------------
1168 function Environment_Task
return Task_Id
is
1170 return Environment_Task_Id
;
1171 end Environment_Task
;
1177 procedure Lock_RTS
is
1179 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1186 procedure Unlock_RTS
is
1188 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1195 function Suspend_Task
1197 Thread_Self
: Thread_Id
) return Boolean
1199 pragma Unreferenced
(T
);
1200 pragma Unreferenced
(Thread_Self
);
1209 function Resume_Task
1211 Thread_Self
: Thread_Id
) return Boolean
1213 pragma Unreferenced
(T
);
1214 pragma Unreferenced
(Thread_Self
);
1223 procedure Initialize
(Environment_Task
: Task_Id
) is
1224 act
: aliased struct_sigaction
;
1225 old_act
: aliased struct_sigaction
;
1226 Tmp_Set
: aliased sigset_t
;
1227 Result
: Interfaces
.C
.int
;
1230 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
1231 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
1232 -- Get interrupt state. Defined in a-init.c. The input argument is
1233 -- the interrupt number, and the result is one of the following:
1235 Default
: constant Character := 's';
1236 -- 'n' this interrupt not set by any Interrupt_State pragma
1237 -- 'u' Interrupt_State pragma set state to User
1238 -- 'r' Interrupt_State pragma set state to Runtime
1239 -- 's' Interrupt_State pragma set state to System (use "default"
1243 Environment_Task_Id
:= Environment_Task
;
1245 Interrupt_Management
.Initialize
;
1247 -- Initialize the lock used to synchronize chain of all ATCBs
1249 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1251 Specific
.Initialize
(Environment_Task
);
1253 Enter_Task
(Environment_Task
);
1255 -- Prepare the set of signals that should unblocked in all tasks
1257 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1258 pragma Assert
(Result
= 0);
1260 for J
in Interrupt_Management
.Interrupt_ID
loop
1261 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1262 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1263 pragma Assert
(Result
= 0);
1267 -- Install the abort-signal handler
1269 if State
(System
.Interrupt_Management
.Abort_Task_Interrupt
)
1273 act
.sa_handler
:= Abort_Handler
'Address;
1275 Result
:= sigemptyset
(Tmp_Set
'Access);
1276 pragma Assert
(Result
= 0);
1277 act
.sa_mask
:= Tmp_Set
;
1281 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
1282 act
'Unchecked_Access,
1283 old_act
'Unchecked_Access);
1284 pragma Assert
(Result
= 0);
1288 end System
.Task_Primitives
.Operations
;