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-2009, 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 OpenVMS/Alpha version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
41 with Ada
.Unchecked_Conversion
;
42 with Ada
.Unchecked_Deallocation
;
46 with System
.Tasking
.Debug
;
47 with System
.OS_Primitives
;
48 with System
.Soft_Links
;
51 package body System
.Task_Primitives
.Operations
is
53 use System
.Tasking
.Debug
;
56 use System
.OS_Interface
;
57 use System
.Parameters
;
58 use System
.OS_Primitives
;
59 use type System
.OS_Primitives
.OS_Time
;
61 package SSL
renames System
.Soft_Links
;
67 -- The followings are logically constants, but need to be initialized
70 Single_RTS_Lock
: aliased RTS_Lock
;
71 -- This is a lock to allow only one thread of control in the RTS at
72 -- a time; it is used to execute in mutual exclusion from all other tasks.
73 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
75 ATCB_Key
: aliased pthread_key_t
;
76 -- Key used to find the Ada Task_Id associated with a thread
78 Environment_Task_Id
: Task_Id
;
79 -- A variable to hold Task_Id for the environment task
81 Time_Slice_Val
: Integer;
82 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
84 Dispatching_Policy
: Character;
85 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
87 Foreign_Task_Elaborated
: aliased Boolean := True;
88 -- Used to identified fake tasks (i.e., non-Ada Threads)
96 procedure Initialize
(Environment_Task
: Task_Id
);
97 pragma Inline
(Initialize
);
98 -- Initialize various data needed by this package
100 function Is_Valid_Task
return Boolean;
101 pragma Inline
(Is_Valid_Task
);
102 -- Does executing thread have a TCB?
104 procedure Set
(Self_Id
: Task_Id
);
106 -- Set the self id for the current task
108 function Self
return Task_Id
;
109 pragma Inline
(Self
);
110 -- Return a pointer to the Ada Task Control Block of the calling task
114 package body Specific
is separate;
115 -- The body of this package is target specific
117 ---------------------------------
118 -- Support for foreign threads --
119 ---------------------------------
121 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
122 -- Allocate and Initialize a new ATCB for the current Thread
124 function Register_Foreign_Thread
125 (Thread
: Thread_Id
) return Task_Id
is separate;
127 -----------------------
128 -- Local Subprograms --
129 -----------------------
131 function To_Task_Id
is
132 new Ada
.Unchecked_Conversion
133 (System
.Task_Primitives
.Task_Address
, Task_Id
);
135 function To_Address
is
136 new Ada
.Unchecked_Conversion
137 (Task_Id
, System
.Task_Primitives
.Task_Address
);
139 function Get_Exc_Stack_Addr
return Address
;
140 -- Replace System.Soft_Links.Get_Exc_Stack_Addr_NT
142 procedure Timer_Sleep_AST
(ID
: Address
);
143 pragma Convention
(C
, Timer_Sleep_AST
);
144 -- Signal the condition variable when AST fires
146 procedure Timer_Sleep_AST
(ID
: Address
) is
147 Result
: Interfaces
.C
.int
;
148 pragma Warnings
(Off
, Result
);
149 Self_ID
: constant Task_Id
:= To_Task_Id
(ID
);
151 Self_ID
.Common
.LL
.AST_Pending
:= False;
152 Result
:= pthread_cond_signal_int_np
(Self_ID
.Common
.LL
.CV
'Access);
153 pragma Assert
(Result
= 0);
160 -- The underlying thread system sets a guard page at the bottom of a thread
161 -- stack, so nothing is needed.
162 -- ??? Check the comment above
164 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
165 pragma Unreferenced
(T
);
166 pragma Unreferenced
(On
);
175 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
177 return T
.Common
.LL
.Thread
;
184 function Self
return Task_Id
renames Specific
.Self
;
186 ---------------------
187 -- Initialize_Lock --
188 ---------------------
190 -- Note: mutexes and cond_variables needed per-task basis are initialized
191 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
192 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
193 -- status change of RTS. Therefore raising Storage_Error in the following
194 -- routines should be able to be handled safely.
196 procedure Initialize_Lock
197 (Prio
: System
.Any_Priority
;
198 L
: not null access Lock
)
200 Attributes
: aliased pthread_mutexattr_t
;
201 Result
: Interfaces
.C
.int
;
204 Result
:= pthread_mutexattr_init
(Attributes
'Access);
205 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
207 if Result
= ENOMEM
then
212 L
.Prio
:= Interfaces
.C
.int
(Prio
);
214 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
215 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
217 if Result
= ENOMEM
then
221 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
222 pragma Assert
(Result
= 0);
225 procedure Initialize_Lock
226 (L
: not null access RTS_Lock
;
229 pragma Unreferenced
(Level
);
231 Attributes
: aliased pthread_mutexattr_t
;
232 Result
: Interfaces
.C
.int
;
235 Result
:= pthread_mutexattr_init
(Attributes
'Access);
236 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
238 if Result
= ENOMEM
then
242 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes???
243 -- Result := pthread_mutexattr_settype_np
244 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
245 -- pragma Assert (Result = 0);
247 -- Result := pthread_mutexattr_setprotocol
248 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
249 -- pragma Assert (Result = 0);
251 -- Result := pthread_mutexattr_setprioceiling
252 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
253 -- pragma Assert (Result = 0);
255 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
257 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
259 if Result
= ENOMEM
then
263 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
264 pragma Assert
(Result
= 0);
271 procedure Finalize_Lock
(L
: not null access Lock
) is
272 Result
: Interfaces
.C
.int
;
274 Result
:= pthread_mutex_destroy
(L
.L
'Access);
275 pragma Assert
(Result
= 0);
278 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
279 Result
: Interfaces
.C
.int
;
281 Result
:= pthread_mutex_destroy
(L
);
282 pragma Assert
(Result
= 0);
290 (L
: not null access Lock
;
291 Ceiling_Violation
: out Boolean)
293 Self_ID
: constant Task_Id
:= Self
;
294 All_Tasks_Link
: constant Task_Id
:= Self
.Common
.All_Tasks_Link
;
295 Current_Prio
: System
.Any_Priority
;
296 Result
: Interfaces
.C
.int
;
299 Current_Prio
:= Get_Priority
(Self_ID
);
301 -- If there is no other tasks, no need to check priorities
303 if All_Tasks_Link
/= Null_Task
304 and then L
.Prio
< Interfaces
.C
.int
(Current_Prio
)
306 Ceiling_Violation
:= True;
310 Result
:= pthread_mutex_lock
(L
.L
'Access);
311 pragma Assert
(Result
= 0);
313 Ceiling_Violation
:= False;
314 -- Why is this commented out ???
315 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
316 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
320 (L
: not null access RTS_Lock
;
321 Global_Lock
: Boolean := False)
323 Result
: Interfaces
.C
.int
;
325 if not Single_Lock
or else Global_Lock
then
326 Result
:= pthread_mutex_lock
(L
);
327 pragma Assert
(Result
= 0);
331 procedure Write_Lock
(T
: Task_Id
) is
332 Result
: Interfaces
.C
.int
;
334 if not Single_Lock
then
335 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
336 pragma Assert
(Result
= 0);
345 (L
: not null access Lock
;
346 Ceiling_Violation
: out Boolean)
349 Write_Lock
(L
, Ceiling_Violation
);
356 procedure Unlock
(L
: not null access Lock
) is
357 Result
: Interfaces
.C
.int
;
359 Result
:= pthread_mutex_unlock
(L
.L
'Access);
360 pragma Assert
(Result
= 0);
364 (L
: not null access RTS_Lock
;
365 Global_Lock
: Boolean := False)
367 Result
: Interfaces
.C
.int
;
369 if not Single_Lock
or else Global_Lock
then
370 Result
:= pthread_mutex_unlock
(L
);
371 pragma Assert
(Result
= 0);
375 procedure Unlock
(T
: Task_Id
) is
376 Result
: Interfaces
.C
.int
;
378 if not Single_Lock
then
379 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
380 pragma Assert
(Result
= 0);
388 -- Dynamic priority ceilings are not supported by the underlying system
390 procedure Set_Ceiling
391 (L
: not null access Lock
;
392 Prio
: System
.Any_Priority
)
394 pragma Unreferenced
(L
, Prio
);
405 Reason
: System
.Tasking
.Task_States
)
407 pragma Unreferenced
(Reason
);
408 Result
: Interfaces
.C
.int
;
414 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
418 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
421 -- EINTR is not considered a failure
423 pragma Assert
(Result
= 0 or else Result
= EINTR
);
425 if Self_ID
.Deferral_Level
= 0
426 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
429 raise Standard
'Abort_Signal;
437 procedure Timed_Sleep
440 Mode
: ST
.Delay_Modes
;
441 Reason
: System
.Tasking
.Task_States
;
442 Timedout
: out Boolean;
443 Yielded
: out Boolean)
445 pragma Unreferenced
(Reason
);
447 Sleep_Time
: OS_Time
;
448 Result
: Interfaces
.C
.int
;
449 Status
: Cond_Value_Type
;
451 -- The body below requires more comments ???
457 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
459 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
463 Self_ID
.Common
.LL
.AST_Pending
:= True;
466 (Status
, 0, Sleep_Time
,
467 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
469 if (Status
and 1) /= 1 then
476 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
477 pragma Assert
(Result
= 0);
482 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
483 pragma Assert
(Result
= 0);
488 if not Self_ID
.Common
.LL
.AST_Pending
then
491 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
492 pragma Assert
((Status
and 1) = 1);
500 procedure Timed_Delay
503 Mode
: ST
.Delay_Modes
)
505 Sleep_Time
: OS_Time
;
506 Result
: Interfaces
.C
.int
;
507 Status
: Cond_Value_Type
;
508 Yielded
: Boolean := False;
515 -- More comments required in body below ???
517 Write_Lock
(Self_ID
);
519 if Time
/= 0.0 or else Mode
/= Relative
then
520 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
522 if Mode
= Relative
or else OS_Clock
<= Sleep_Time
then
523 Self_ID
.Common
.State
:= Delay_Sleep
;
524 Self_ID
.Common
.LL
.AST_Pending
:= True;
527 (Status
, 0, Sleep_Time
,
528 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
530 -- Comment following test
532 if (Status
and 1) /= 1 then
537 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
538 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
539 pragma Assert
((Status
and 1) = 1);
546 (Self_ID
.Common
.LL
.CV
'Access,
547 Single_RTS_Lock
'Access);
548 pragma Assert
(Result
= 0);
552 (Self_ID
.Common
.LL
.CV
'Access,
553 Self_ID
.Common
.LL
.L
'Access);
554 pragma Assert
(Result
= 0);
559 exit when not Self_ID
.Common
.LL
.AST_Pending
;
562 Self_ID
.Common
.State
:= Runnable
;
573 Result
:= sched_yield
;
574 pragma Assert
(Result
= 0);
578 ---------------------
579 -- Monotonic_Clock --
580 ---------------------
582 function Monotonic_Clock
return Duration
583 renames System
.OS_Primitives
.Monotonic_Clock
;
589 function RT_Resolution
return Duration is
591 -- Document origin of this magic constant ???
599 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
600 pragma Unreferenced
(Reason
);
601 Result
: Interfaces
.C
.int
;
603 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
604 pragma Assert
(Result
= 0);
611 procedure Yield
(Do_Yield
: Boolean := True) is
612 Result
: Interfaces
.C
.int
;
613 pragma Unreferenced
(Result
);
616 Result
:= sched_yield
;
624 procedure Set_Priority
626 Prio
: System
.Any_Priority
;
627 Loss_Of_Inheritance
: Boolean := False)
629 pragma Unreferenced
(Loss_Of_Inheritance
);
631 Result
: Interfaces
.C
.int
;
632 Param
: aliased struct_sched_param
;
634 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
635 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
636 -- Get priority specific dispatching policy
638 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
639 -- Upper case first character of the policy name corresponding to the
640 -- task as set by a Priority_Specific_Dispatching pragma.
643 T
.Common
.Current_Priority
:= Prio
;
644 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
646 if Dispatching_Policy
= 'R'
647 or else Priority_Specific_Policy
= 'R'
648 or else Time_Slice_Val
> 0
651 pthread_setschedparam
652 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
654 elsif Dispatching_Policy
= 'F'
655 or else Priority_Specific_Policy
= 'F'
656 or else Time_Slice_Val
= 0
659 pthread_setschedparam
660 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
663 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
664 -- Since the translation from Underlying priorities results
665 -- in a range of 16 - 31, dividing by 2 gives the correct result.
667 Param
.sched_priority
:= Param
.sched_priority
/ 2;
669 pthread_setschedparam
670 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
673 pragma Assert
(Result
= 0);
680 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
682 return T
.Common
.Current_Priority
;
689 procedure Enter_Task
(Self_ID
: Task_Id
) is
691 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
692 Specific
.Set
(Self_ID
);
699 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
701 return new Ada_Task_Control_Block
(Entry_Num
);
708 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
710 -----------------------------
711 -- Register_Foreign_Thread --
712 -----------------------------
714 function Register_Foreign_Thread
return Task_Id
is
716 if Is_Valid_Task
then
719 return Register_Foreign_Thread
(pthread_self
);
721 end Register_Foreign_Thread
;
727 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
728 Mutex_Attr
: aliased pthread_mutexattr_t
;
729 Result
: Interfaces
.C
.int
;
730 Cond_Attr
: aliased pthread_condattr_t
;
733 -- More comments required in body below ???
735 if not Single_Lock
then
736 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
737 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
742 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
743 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
751 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
752 pragma Assert
(Result
= 0);
755 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
756 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
761 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
762 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
767 Self_ID
.Common
.LL
.Exc_Stack_Ptr
:= new Exc_Stack_T
;
770 if not Single_Lock
then
771 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
772 pragma Assert
(Result
= 0);
778 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
779 pragma Assert
(Result
= 0);
782 ------------------------
783 -- Get_Exc_Stack_Addr --
784 ------------------------
786 function Get_Exc_Stack_Addr
return Address
is
788 return Self
.Common
.LL
.Exc_Stack_Ptr
(Exc_Stack_T
'Last)'Address;
789 end Get_Exc_Stack_Addr
;
795 procedure Create_Task
797 Wrapper
: System
.Address
;
798 Stack_Size
: System
.Parameters
.Size_Type
;
799 Priority
: System
.Any_Priority
;
800 Succeeded
: out Boolean)
802 Attributes
: aliased pthread_attr_t
;
803 Result
: Interfaces
.C
.int
;
805 function Thread_Body_Access
is new
806 Ada
.Unchecked_Conversion
(System
.Aux_DEC
.Short_Address
, Thread_Body
);
809 -- Since the initial signal mask of a thread is inherited from the
810 -- creator, we need to set our local signal mask to mask all signals
811 -- during the creation operation, to make sure the new thread is
812 -- not disturbed by signals before it has set its own Task_Id.
814 Result
:= pthread_attr_init
(Attributes
'Access);
815 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
822 Result
:= pthread_attr_setdetachstate
823 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
824 pragma Assert
(Result
= 0);
826 Result
:= pthread_attr_setstacksize
827 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
828 pragma Assert
(Result
= 0);
830 -- This call may be unnecessary, not sure. ???
833 pthread_attr_setinheritsched
834 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
835 pragma Assert
(Result
= 0);
839 (T
.Common
.LL
.Thread
'Access,
841 Thread_Body_Access
(Wrapper
),
844 -- ENOMEM is a valid run-time error -- do not shut down
846 pragma Assert
(Result
= 0
847 or else Result
= EAGAIN
or else Result
= ENOMEM
);
849 Succeeded
:= Result
= 0;
851 Result
:= pthread_attr_destroy
(Attributes
'Access);
852 pragma Assert
(Result
= 0);
855 Set_Priority
(T
, Priority
);
863 procedure Finalize_TCB
(T
: Task_Id
) is
864 Result
: Interfaces
.C
.int
;
866 Is_Self
: constant Boolean := T
= Self
;
868 procedure Free
is new
869 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
871 procedure Free
is new Ada
.Unchecked_Deallocation
872 (Exc_Stack_T
, Exc_Stack_Ptr_T
);
875 if not Single_Lock
then
876 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
877 pragma Assert
(Result
= 0);
880 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
881 pragma Assert
(Result
= 0);
883 if T
.Known_Tasks_Index
/= -1 then
884 Known_Tasks
(T
.Known_Tasks_Index
) := null;
887 Free
(T
.Common
.LL
.Exc_Stack_Ptr
);
899 procedure Exit_Task
is
908 procedure Abort_Task
(T
: Task_Id
) is
910 -- Interrupt Server_Tasks may be waiting on an event flag
912 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
913 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
921 procedure Initialize
(S
: in out Suspension_Object
) is
922 Mutex_Attr
: aliased pthread_mutexattr_t
;
923 Cond_Attr
: aliased pthread_condattr_t
;
924 Result
: Interfaces
.C
.int
;
926 -- Initialize internal state (always to False (D.10 (6)))
931 -- Initialize internal mutex
933 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
934 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
936 if Result
= ENOMEM
then
940 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
941 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
943 if Result
= ENOMEM
then
944 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
945 pragma Assert
(Result
= 0);
950 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
951 pragma Assert
(Result
= 0);
953 -- Initialize internal condition variable
955 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
956 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
959 Result
:= pthread_mutex_destroy
(S
.L
'Access);
960 pragma Assert
(Result
= 0);
962 if Result
= ENOMEM
then
967 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
968 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
971 Result
:= pthread_mutex_destroy
(S
.L
'Access);
972 pragma Assert
(Result
= 0);
974 if Result
= ENOMEM
then
975 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
976 pragma Assert
(Result
= 0);
982 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
983 pragma Assert
(Result
= 0);
990 procedure Finalize
(S
: in out Suspension_Object
) is
991 Result
: Interfaces
.C
.int
;
994 -- Destroy internal mutex
996 Result
:= pthread_mutex_destroy
(S
.L
'Access);
997 pragma Assert
(Result
= 0);
999 -- Destroy internal condition variable
1001 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1002 pragma Assert
(Result
= 0);
1009 function Current_State
(S
: Suspension_Object
) return Boolean is
1011 -- We do not want to use lock on this read operation. State is marked
1012 -- as Atomic so that we ensure that the value retrieved is correct.
1021 procedure Set_False
(S
: in out Suspension_Object
) is
1022 Result
: Interfaces
.C
.int
;
1025 SSL
.Abort_Defer
.all;
1027 Result
:= pthread_mutex_lock
(S
.L
'Access);
1028 pragma Assert
(Result
= 0);
1032 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1033 pragma Assert
(Result
= 0);
1035 SSL
.Abort_Undefer
.all;
1042 procedure Set_True
(S
: in out Suspension_Object
) is
1043 Result
: Interfaces
.C
.int
;
1046 SSL
.Abort_Defer
.all;
1048 Result
:= pthread_mutex_lock
(S
.L
'Access);
1049 pragma Assert
(Result
= 0);
1051 -- If there is already a task waiting on this suspension object then
1052 -- we resume it, leaving the state of the suspension object to False,
1053 -- as specified in (RM D.10(9)), otherwise leave state set to True.
1059 Result
:= pthread_cond_signal
(S
.CV
'Access);
1060 pragma Assert
(Result
= 0);
1066 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1067 pragma Assert
(Result
= 0);
1069 SSL
.Abort_Undefer
.all;
1072 ------------------------
1073 -- Suspend_Until_True --
1074 ------------------------
1076 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1077 Result
: Interfaces
.C
.int
;
1080 SSL
.Abort_Defer
.all;
1082 Result
:= pthread_mutex_lock
(S
.L
'Access);
1083 pragma Assert
(Result
= 0);
1087 -- Program_Error must be raised upon calling Suspend_Until_True
1088 -- if another task is already waiting on that suspension object
1091 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1092 pragma Assert
(Result
= 0);
1094 SSL
.Abort_Undefer
.all;
1096 raise Program_Error
;
1099 -- Suspend the task if the state is False. Otherwise, the task
1100 -- continues its execution, and the state of the suspension object
1101 -- is set to False (ARM D.10 par. 9).
1109 -- Loop in case pthread_cond_wait returns earlier than expected
1110 -- (e.g. in case of EINTR caused by a signal).
1112 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1113 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1115 exit when not S
.Waiting
;
1119 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1120 pragma Assert
(Result
= 0);
1122 SSL
.Abort_Undefer
.all;
1124 end Suspend_Until_True
;
1132 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1133 pragma Unreferenced
(Self_ID
);
1138 --------------------
1139 -- Check_No_Locks --
1140 --------------------
1142 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1143 pragma Unreferenced
(Self_ID
);
1148 ----------------------
1149 -- Environment_Task --
1150 ----------------------
1152 function Environment_Task
return Task_Id
is
1154 return Environment_Task_Id
;
1155 end Environment_Task
;
1161 procedure Lock_RTS
is
1163 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1170 procedure Unlock_RTS
is
1172 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1179 function Suspend_Task
1181 Thread_Self
: Thread_Id
) return Boolean
1183 pragma Unreferenced
(T
);
1184 pragma Unreferenced
(Thread_Self
);
1193 function Resume_Task
1195 Thread_Self
: Thread_Id
) return Boolean
1197 pragma Unreferenced
(T
);
1198 pragma Unreferenced
(Thread_Self
);
1203 --------------------
1204 -- Stop_All_Tasks --
1205 --------------------
1207 procedure Stop_All_Tasks
is
1216 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1217 pragma Unreferenced
(T
);
1226 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1227 pragma Unreferenced
(T
);
1236 procedure Initialize
(Environment_Task
: Task_Id
) is
1238 -- The DEC Ada facility code defined in Starlet
1239 Ada_Facility
: constant := 49;
1241 function DBGEXT
(Control_Block
: System
.Address
)
1242 return System
.Aux_DEC
.Unsigned_Word
;
1243 -- DBGEXT is imported from s-tasdeb.adb and its parameter re-typed
1244 -- as Address to avoid having a VMS specific s-tasdeb.ads.
1245 pragma Interface
(C
, DBGEXT
);
1246 pragma Import_Function
(DBGEXT
, "GNAT$DBGEXT");
1248 type Facility_Type
is range 0 .. 65535;
1250 procedure Debug_Register
1251 (ADBGEXT
: System
.Address
;
1252 ATCB_Key
: pthread_key_t
;
1253 Facility
: Facility_Type
;
1254 Std_Prolog
: Integer);
1255 pragma Import
(C
, Debug_Register
, "CMA$DEBUG_REGISTER");
1257 Environment_Task_Id
:= Environment_Task
;
1259 SSL
.Get_Exc_Stack_Addr
:= Get_Exc_Stack_Addr
'Access;
1261 -- Initialize the lock used to synchronize chain of all ATCBs
1263 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1265 Specific
.Initialize
(Environment_Task
);
1267 -- Pass the context key on to CMA along with the other parameters
1270 DBGEXT
'Address, -- Our DEBUG handling entry point
1271 ATCB_Key
, -- CMA context key for our Ada TCB's
1272 Ada_Facility
, -- Out facility code
1273 0 -- False, we don't have the std TCB prolog
1276 -- Make environment task known here because it doesn't go through
1277 -- Activate_Tasks, which does it for all other tasks.
1279 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1280 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1282 Enter_Task
(Environment_Task
);
1285 end System
.Task_Primitives
.Operations
;