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
;
413 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
414 mutex
=> (if Single_Lock
415 then Single_RTS_Lock
'Access
416 else Self_ID
.Common
.LL
.L
'Access));
418 -- EINTR is not considered a failure
420 pragma Assert
(Result
= 0 or else Result
= EINTR
);
422 if Self_ID
.Deferral_Level
= 0
423 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
426 raise Standard
'Abort_Signal;
434 procedure Timed_Sleep
437 Mode
: ST
.Delay_Modes
;
438 Reason
: System
.Tasking
.Task_States
;
439 Timedout
: out Boolean;
440 Yielded
: out Boolean)
442 pragma Unreferenced
(Reason
);
444 Sleep_Time
: OS_Time
;
445 Result
: Interfaces
.C
.int
;
446 Status
: Cond_Value_Type
;
448 -- The body below requires more comments ???
454 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
456 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
460 Self_ID
.Common
.LL
.AST_Pending
:= True;
463 (Status
, 0, Sleep_Time
,
464 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
466 if (Status
and 1) /= 1 then
473 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
474 pragma Assert
(Result
= 0);
479 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
480 pragma Assert
(Result
= 0);
485 if not Self_ID
.Common
.LL
.AST_Pending
then
488 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
489 pragma Assert
((Status
and 1) = 1);
497 procedure Timed_Delay
500 Mode
: ST
.Delay_Modes
)
502 Sleep_Time
: OS_Time
;
503 Result
: Interfaces
.C
.int
;
504 Status
: Cond_Value_Type
;
505 Yielded
: Boolean := False;
512 -- More comments required in body below ???
514 Write_Lock
(Self_ID
);
516 if Time
/= 0.0 or else Mode
/= Relative
then
517 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
519 if Mode
= Relative
or else OS_Clock
<= Sleep_Time
then
520 Self_ID
.Common
.State
:= Delay_Sleep
;
521 Self_ID
.Common
.LL
.AST_Pending
:= True;
524 (Status
, 0, Sleep_Time
,
525 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
527 -- Comment following test
529 if (Status
and 1) /= 1 then
534 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
535 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
536 pragma Assert
((Status
and 1) = 1);
542 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
543 mutex
=> (if Single_Lock
544 then Single_RTS_Lock
'Access
545 else Self_ID
.Common
.LL
.L
'Access));
546 pragma Assert
(Result
= 0);
550 exit when not Self_ID
.Common
.LL
.AST_Pending
;
553 Self_ID
.Common
.State
:= Runnable
;
564 Result
:= sched_yield
;
565 pragma Assert
(Result
= 0);
569 ---------------------
570 -- Monotonic_Clock --
571 ---------------------
573 function Monotonic_Clock
return Duration
574 renames System
.OS_Primitives
.Monotonic_Clock
;
580 function RT_Resolution
return Duration is
582 -- Document origin of this magic constant ???
590 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
591 pragma Unreferenced
(Reason
);
592 Result
: Interfaces
.C
.int
;
594 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
595 pragma Assert
(Result
= 0);
602 procedure Yield
(Do_Yield
: Boolean := True) is
603 Result
: Interfaces
.C
.int
;
604 pragma Unreferenced
(Result
);
607 Result
:= sched_yield
;
615 procedure Set_Priority
617 Prio
: System
.Any_Priority
;
618 Loss_Of_Inheritance
: Boolean := False)
620 pragma Unreferenced
(Loss_Of_Inheritance
);
622 Result
: Interfaces
.C
.int
;
623 Param
: aliased struct_sched_param
;
625 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
626 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
627 -- Get priority specific dispatching policy
629 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
630 -- Upper case first character of the policy name corresponding to the
631 -- task as set by a Priority_Specific_Dispatching pragma.
634 T
.Common
.Current_Priority
:= Prio
;
635 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
637 if Dispatching_Policy
= 'R'
638 or else Priority_Specific_Policy
= 'R'
639 or else Time_Slice_Val
> 0
642 pthread_setschedparam
643 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
645 elsif Dispatching_Policy
= 'F'
646 or else Priority_Specific_Policy
= 'F'
647 or else Time_Slice_Val
= 0
650 pthread_setschedparam
651 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
654 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
655 -- Since the translation from Underlying priorities results
656 -- in a range of 16 - 31, dividing by 2 gives the correct result.
658 Param
.sched_priority
:= Param
.sched_priority
/ 2;
660 pthread_setschedparam
661 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
664 pragma Assert
(Result
= 0);
671 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
673 return T
.Common
.Current_Priority
;
680 procedure Enter_Task
(Self_ID
: Task_Id
) is
682 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
683 Specific
.Set
(Self_ID
);
690 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
692 return new Ada_Task_Control_Block
(Entry_Num
);
699 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
701 -----------------------------
702 -- Register_Foreign_Thread --
703 -----------------------------
705 function Register_Foreign_Thread
return Task_Id
is
707 if Is_Valid_Task
then
710 return Register_Foreign_Thread
(pthread_self
);
712 end Register_Foreign_Thread
;
718 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
719 Mutex_Attr
: aliased pthread_mutexattr_t
;
720 Result
: Interfaces
.C
.int
;
721 Cond_Attr
: aliased pthread_condattr_t
;
724 -- More comments required in body below ???
726 if not Single_Lock
then
727 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
728 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
733 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
734 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
742 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
743 pragma Assert
(Result
= 0);
746 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
747 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
752 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
753 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
758 Self_ID
.Common
.LL
.Exc_Stack_Ptr
:= new Exc_Stack_T
;
761 if not Single_Lock
then
762 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
763 pragma Assert
(Result
= 0);
769 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
770 pragma Assert
(Result
= 0);
773 ------------------------
774 -- Get_Exc_Stack_Addr --
775 ------------------------
777 function Get_Exc_Stack_Addr
return Address
is
779 return Self
.Common
.LL
.Exc_Stack_Ptr
(Exc_Stack_T
'Last)'Address;
780 end Get_Exc_Stack_Addr
;
786 procedure Create_Task
788 Wrapper
: System
.Address
;
789 Stack_Size
: System
.Parameters
.Size_Type
;
790 Priority
: System
.Any_Priority
;
791 Succeeded
: out Boolean)
793 Attributes
: aliased pthread_attr_t
;
794 Result
: Interfaces
.C
.int
;
796 function Thread_Body_Access
is new
797 Ada
.Unchecked_Conversion
(System
.Aux_DEC
.Short_Address
, Thread_Body
);
800 -- Since the initial signal mask of a thread is inherited from the
801 -- creator, we need to set our local signal mask to mask all signals
802 -- during the creation operation, to make sure the new thread is
803 -- not disturbed by signals before it has set its own Task_Id.
805 Result
:= pthread_attr_init
(Attributes
'Access);
806 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
813 Result
:= pthread_attr_setdetachstate
814 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
815 pragma Assert
(Result
= 0);
817 Result
:= pthread_attr_setstacksize
818 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
819 pragma Assert
(Result
= 0);
821 -- This call may be unnecessary, not sure. ???
824 pthread_attr_setinheritsched
825 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
826 pragma Assert
(Result
= 0);
830 (T
.Common
.LL
.Thread
'Access,
832 Thread_Body_Access
(Wrapper
),
835 -- ENOMEM is a valid run-time error -- do not shut down
837 pragma Assert
(Result
= 0
838 or else Result
= EAGAIN
or else Result
= ENOMEM
);
840 Succeeded
:= Result
= 0;
842 Result
:= pthread_attr_destroy
(Attributes
'Access);
843 pragma Assert
(Result
= 0);
846 Set_Priority
(T
, Priority
);
854 procedure Finalize_TCB
(T
: Task_Id
) is
855 Result
: Interfaces
.C
.int
;
857 Is_Self
: constant Boolean := T
= Self
;
859 procedure Free
is new
860 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
862 procedure Free
is new Ada
.Unchecked_Deallocation
863 (Exc_Stack_T
, Exc_Stack_Ptr_T
);
866 if not Single_Lock
then
867 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
868 pragma Assert
(Result
= 0);
871 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
872 pragma Assert
(Result
= 0);
874 if T
.Known_Tasks_Index
/= -1 then
875 Known_Tasks
(T
.Known_Tasks_Index
) := null;
878 Free
(T
.Common
.LL
.Exc_Stack_Ptr
);
890 procedure Exit_Task
is
899 procedure Abort_Task
(T
: Task_Id
) is
901 -- Interrupt Server_Tasks may be waiting on an event flag
903 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
904 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
912 procedure Initialize
(S
: in out Suspension_Object
) is
913 Mutex_Attr
: aliased pthread_mutexattr_t
;
914 Cond_Attr
: aliased pthread_condattr_t
;
915 Result
: Interfaces
.C
.int
;
917 -- Initialize internal state (always to False (D.10 (6)))
922 -- Initialize internal mutex
924 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
925 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
927 if Result
= ENOMEM
then
931 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
932 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
934 if Result
= ENOMEM
then
935 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
936 pragma Assert
(Result
= 0);
941 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
942 pragma Assert
(Result
= 0);
944 -- Initialize internal condition variable
946 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
947 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
950 Result
:= pthread_mutex_destroy
(S
.L
'Access);
951 pragma Assert
(Result
= 0);
953 if Result
= ENOMEM
then
958 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
959 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
962 Result
:= pthread_mutex_destroy
(S
.L
'Access);
963 pragma Assert
(Result
= 0);
965 if Result
= ENOMEM
then
966 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
967 pragma Assert
(Result
= 0);
973 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
974 pragma Assert
(Result
= 0);
981 procedure Finalize
(S
: in out Suspension_Object
) is
982 Result
: Interfaces
.C
.int
;
985 -- Destroy internal mutex
987 Result
:= pthread_mutex_destroy
(S
.L
'Access);
988 pragma Assert
(Result
= 0);
990 -- Destroy internal condition variable
992 Result
:= pthread_cond_destroy
(S
.CV
'Access);
993 pragma Assert
(Result
= 0);
1000 function Current_State
(S
: Suspension_Object
) return Boolean is
1002 -- We do not want to use lock on this read operation. State is marked
1003 -- as Atomic so that we ensure that the value retrieved is correct.
1012 procedure Set_False
(S
: in out Suspension_Object
) is
1013 Result
: Interfaces
.C
.int
;
1016 SSL
.Abort_Defer
.all;
1018 Result
:= pthread_mutex_lock
(S
.L
'Access);
1019 pragma Assert
(Result
= 0);
1023 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1024 pragma Assert
(Result
= 0);
1026 SSL
.Abort_Undefer
.all;
1033 procedure Set_True
(S
: in out Suspension_Object
) is
1034 Result
: Interfaces
.C
.int
;
1037 SSL
.Abort_Defer
.all;
1039 Result
:= pthread_mutex_lock
(S
.L
'Access);
1040 pragma Assert
(Result
= 0);
1042 -- If there is already a task waiting on this suspension object then
1043 -- we resume it, leaving the state of the suspension object to False,
1044 -- as specified in (RM D.10(9)), otherwise leave state set to True.
1050 Result
:= pthread_cond_signal
(S
.CV
'Access);
1051 pragma Assert
(Result
= 0);
1057 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1058 pragma Assert
(Result
= 0);
1060 SSL
.Abort_Undefer
.all;
1063 ------------------------
1064 -- Suspend_Until_True --
1065 ------------------------
1067 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1068 Result
: Interfaces
.C
.int
;
1071 SSL
.Abort_Defer
.all;
1073 Result
:= pthread_mutex_lock
(S
.L
'Access);
1074 pragma Assert
(Result
= 0);
1078 -- Program_Error must be raised upon calling Suspend_Until_True
1079 -- if another task is already waiting on that suspension object
1082 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1083 pragma Assert
(Result
= 0);
1085 SSL
.Abort_Undefer
.all;
1087 raise Program_Error
;
1090 -- Suspend the task if the state is False. Otherwise, the task
1091 -- continues its execution, and the state of the suspension object
1092 -- is set to False (ARM D.10 par. 9).
1100 -- Loop in case pthread_cond_wait returns earlier than expected
1101 -- (e.g. in case of EINTR caused by a signal).
1103 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1104 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1106 exit when not S
.Waiting
;
1110 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1111 pragma Assert
(Result
= 0);
1113 SSL
.Abort_Undefer
.all;
1115 end Suspend_Until_True
;
1123 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1124 pragma Unreferenced
(Self_ID
);
1129 --------------------
1130 -- Check_No_Locks --
1131 --------------------
1133 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1134 pragma Unreferenced
(Self_ID
);
1139 ----------------------
1140 -- Environment_Task --
1141 ----------------------
1143 function Environment_Task
return Task_Id
is
1145 return Environment_Task_Id
;
1146 end Environment_Task
;
1152 procedure Lock_RTS
is
1154 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1161 procedure Unlock_RTS
is
1163 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1170 function Suspend_Task
1172 Thread_Self
: Thread_Id
) return Boolean
1174 pragma Unreferenced
(T
);
1175 pragma Unreferenced
(Thread_Self
);
1184 function Resume_Task
1186 Thread_Self
: Thread_Id
) return Boolean
1188 pragma Unreferenced
(T
);
1189 pragma Unreferenced
(Thread_Self
);
1194 --------------------
1195 -- Stop_All_Tasks --
1196 --------------------
1198 procedure Stop_All_Tasks
is
1207 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1208 pragma Unreferenced
(T
);
1217 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1218 pragma Unreferenced
(T
);
1227 procedure Initialize
(Environment_Task
: Task_Id
) is
1229 -- The DEC Ada facility code defined in Starlet
1230 Ada_Facility
: constant := 49;
1232 function DBGEXT
(Control_Block
: System
.Address
)
1233 return System
.Aux_DEC
.Unsigned_Word
;
1234 -- DBGEXT is imported from s-tasdeb.adb and its parameter re-typed
1235 -- as Address to avoid having a VMS specific s-tasdeb.ads.
1236 pragma Interface
(C
, DBGEXT
);
1237 pragma Import_Function
(DBGEXT
, "GNAT$DBGEXT");
1239 type Facility_Type
is range 0 .. 65535;
1241 procedure Debug_Register
1242 (ADBGEXT
: System
.Address
;
1243 ATCB_Key
: pthread_key_t
;
1244 Facility
: Facility_Type
;
1245 Std_Prolog
: Integer);
1246 pragma Import
(C
, Debug_Register
, "CMA$DEBUG_REGISTER");
1248 Environment_Task_Id
:= Environment_Task
;
1250 SSL
.Get_Exc_Stack_Addr
:= Get_Exc_Stack_Addr
'Access;
1252 -- Initialize the lock used to synchronize chain of all ATCBs
1254 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1256 Specific
.Initialize
(Environment_Task
);
1258 -- Pass the context key on to CMA along with the other parameters
1261 DBGEXT
'Address, -- Our DEBUG handling entry point
1262 ATCB_Key
, -- CMA context key for our Ada TCB's
1263 Ada_Facility
, -- Out facility code
1264 0 -- False, we don't have the std TCB prolog
1267 -- Make environment task known here because it doesn't go through
1268 -- Activate_Tasks, which does it for all other tasks.
1270 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1271 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1273 Enter_Task
(Environment_Task
);
1276 end System
.Task_Primitives
.Operations
;