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-2012, 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
;
45 with System
.Tasking
.Debug
;
46 with System
.OS_Primitives
;
47 with System
.Soft_Links
;
50 package body System
.Task_Primitives
.Operations
is
52 use System
.Tasking
.Debug
;
55 use System
.OS_Interface
;
56 use System
.Parameters
;
57 use System
.OS_Primitives
;
58 use type System
.OS_Primitives
.OS_Time
;
60 package SSL
renames System
.Soft_Links
;
66 -- The followings are logically constants, but need to be initialized
69 Single_RTS_Lock
: aliased RTS_Lock
;
70 -- This is a lock to allow only one thread of control in the RTS at
71 -- a time; it is used to execute in mutual exclusion from all other tasks.
72 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
74 ATCB_Key
: aliased pthread_key_t
;
75 -- Key used to find the Ada Task_Id associated with a thread
77 Environment_Task_Id
: Task_Id
;
78 -- A variable to hold Task_Id for the environment task
80 Time_Slice_Val
: Integer;
81 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
83 Dispatching_Policy
: Character;
84 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
86 Foreign_Task_Elaborated
: aliased Boolean := True;
87 -- Used to identified fake tasks (i.e., non-Ada Threads)
95 procedure Initialize
(Environment_Task
: Task_Id
);
96 pragma Inline
(Initialize
);
97 -- Initialize various data needed by this package
99 function Is_Valid_Task
return Boolean;
100 pragma Inline
(Is_Valid_Task
);
101 -- Does executing thread have a TCB?
103 procedure Set
(Self_Id
: Task_Id
);
105 -- Set the self id for the current task
107 function Self
return Task_Id
;
108 pragma Inline
(Self
);
109 -- Return a pointer to the Ada Task Control Block of the calling task
113 package body Specific
is separate;
114 -- The body of this package is target specific
116 ----------------------------------
117 -- ATCB allocation/deallocation --
118 ----------------------------------
120 package body ATCB_Allocation
is separate;
121 -- The body of this package is shared across several targets
123 ---------------------------------
124 -- Support for foreign threads --
125 ---------------------------------
127 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
128 -- Allocate and Initialize a new ATCB for the current Thread
130 function Register_Foreign_Thread
131 (Thread
: Thread_Id
) return Task_Id
is separate;
133 -----------------------
134 -- Local Subprograms --
135 -----------------------
137 function To_Task_Id
is
138 new Ada
.Unchecked_Conversion
139 (System
.Task_Primitives
.Task_Address
, Task_Id
);
141 function To_Address
is
142 new Ada
.Unchecked_Conversion
143 (Task_Id
, System
.Task_Primitives
.Task_Address
);
145 procedure Timer_Sleep_AST
(ID
: Address
);
146 pragma Convention
(C
, Timer_Sleep_AST
);
147 -- Signal the condition variable when AST fires
149 procedure Timer_Sleep_AST
(ID
: Address
) is
150 Result
: Interfaces
.C
.int
;
151 pragma Warnings
(Off
, Result
);
152 Self_ID
: constant Task_Id
:= To_Task_Id
(ID
);
154 Self_ID
.Common
.LL
.AST_Pending
:= False;
155 Result
:= pthread_cond_signal_int_np
(Self_ID
.Common
.LL
.CV
'Access);
156 pragma Assert
(Result
= 0);
163 -- The underlying thread system sets a guard page at the bottom of a thread
164 -- stack, so nothing is needed.
165 -- ??? Check the comment above
167 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
168 pragma Unreferenced
(T
);
169 pragma Unreferenced
(On
);
178 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
180 return T
.Common
.LL
.Thread
;
187 function Self
return Task_Id
renames Specific
.Self
;
189 ---------------------
190 -- Initialize_Lock --
191 ---------------------
193 -- Note: mutexes and cond_variables needed per-task basis are initialized
194 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
195 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
196 -- status change of RTS. Therefore raising Storage_Error in the following
197 -- routines should be able to be handled safely.
199 procedure Initialize_Lock
200 (Prio
: System
.Any_Priority
;
201 L
: not null access Lock
)
203 Attributes
: aliased pthread_mutexattr_t
;
204 Result
: Interfaces
.C
.int
;
207 Result
:= pthread_mutexattr_init
(Attributes
'Access);
208 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
210 if Result
= ENOMEM
then
215 L
.Prio
:= Interfaces
.C
.int
(Prio
);
217 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
218 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
220 if Result
= ENOMEM
then
224 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
225 pragma Assert
(Result
= 0);
228 procedure Initialize_Lock
229 (L
: not null access RTS_Lock
;
232 pragma Unreferenced
(Level
);
234 Attributes
: aliased pthread_mutexattr_t
;
235 Result
: Interfaces
.C
.int
;
238 Result
:= pthread_mutexattr_init
(Attributes
'Access);
239 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
241 if Result
= ENOMEM
then
245 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes???
246 -- Result := pthread_mutexattr_settype_np
247 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
248 -- pragma Assert (Result = 0);
250 -- Result := pthread_mutexattr_setprotocol
251 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
252 -- pragma Assert (Result = 0);
254 -- Result := pthread_mutexattr_setprioceiling
255 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
256 -- pragma Assert (Result = 0);
258 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
260 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
262 if Result
= ENOMEM
then
266 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
267 pragma Assert
(Result
= 0);
274 procedure Finalize_Lock
(L
: not null access Lock
) is
275 Result
: Interfaces
.C
.int
;
277 Result
:= pthread_mutex_destroy
(L
.L
'Access);
278 pragma Assert
(Result
= 0);
281 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
282 Result
: Interfaces
.C
.int
;
284 Result
:= pthread_mutex_destroy
(L
);
285 pragma Assert
(Result
= 0);
293 (L
: not null access Lock
;
294 Ceiling_Violation
: out Boolean)
296 Self_ID
: constant Task_Id
:= Self
;
297 All_Tasks_Link
: constant Task_Id
:= Self
.Common
.All_Tasks_Link
;
298 Current_Prio
: System
.Any_Priority
;
299 Result
: Interfaces
.C
.int
;
302 Current_Prio
:= Get_Priority
(Self_ID
);
304 -- If there is no other tasks, no need to check priorities
306 if All_Tasks_Link
/= Null_Task
307 and then L
.Prio
< Interfaces
.C
.int
(Current_Prio
)
309 Ceiling_Violation
:= True;
313 Result
:= pthread_mutex_lock
(L
.L
'Access);
314 pragma Assert
(Result
= 0);
316 Ceiling_Violation
:= False;
317 -- Why is this commented out ???
318 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
319 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
323 (L
: not null access RTS_Lock
;
324 Global_Lock
: Boolean := False)
326 Result
: Interfaces
.C
.int
;
328 if not Single_Lock
or else Global_Lock
then
329 Result
:= pthread_mutex_lock
(L
);
330 pragma Assert
(Result
= 0);
334 procedure Write_Lock
(T
: Task_Id
) is
335 Result
: Interfaces
.C
.int
;
337 if not Single_Lock
then
338 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
339 pragma Assert
(Result
= 0);
348 (L
: not null access Lock
;
349 Ceiling_Violation
: out Boolean)
352 Write_Lock
(L
, Ceiling_Violation
);
359 procedure Unlock
(L
: not null access Lock
) is
360 Result
: Interfaces
.C
.int
;
362 Result
:= pthread_mutex_unlock
(L
.L
'Access);
363 pragma Assert
(Result
= 0);
367 (L
: not null access RTS_Lock
;
368 Global_Lock
: Boolean := False)
370 Result
: Interfaces
.C
.int
;
372 if not Single_Lock
or else Global_Lock
then
373 Result
:= pthread_mutex_unlock
(L
);
374 pragma Assert
(Result
= 0);
378 procedure Unlock
(T
: Task_Id
) is
379 Result
: Interfaces
.C
.int
;
381 if not Single_Lock
then
382 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
383 pragma Assert
(Result
= 0);
391 -- Dynamic priority ceilings are not supported by the underlying system
393 procedure Set_Ceiling
394 (L
: not null access Lock
;
395 Prio
: System
.Any_Priority
)
397 pragma Unreferenced
(L
, Prio
);
408 Reason
: System
.Tasking
.Task_States
)
410 pragma Unreferenced
(Reason
);
411 Result
: Interfaces
.C
.int
;
416 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
417 mutex
=> (if Single_Lock
418 then Single_RTS_Lock
'Access
419 else 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);
545 (cond
=> Self_ID
.Common
.LL
.CV
'Access,
546 mutex
=> (if Single_Lock
547 then Single_RTS_Lock
'Access
548 else Self_ID
.Common
.LL
.L
'Access));
549 pragma Assert
(Result
= 0);
553 exit when not Self_ID
.Common
.LL
.AST_Pending
;
556 Self_ID
.Common
.State
:= Runnable
;
567 Result
:= sched_yield
;
568 pragma Assert
(Result
= 0);
572 ---------------------
573 -- Monotonic_Clock --
574 ---------------------
576 function Monotonic_Clock
return Duration
577 renames System
.OS_Primitives
.Monotonic_Clock
;
583 function RT_Resolution
return Duration is
585 -- Document origin of this magic constant ???
593 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
594 pragma Unreferenced
(Reason
);
595 Result
: Interfaces
.C
.int
;
597 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
598 pragma Assert
(Result
= 0);
605 procedure Yield
(Do_Yield
: Boolean := True) is
606 Result
: Interfaces
.C
.int
;
607 pragma Unreferenced
(Result
);
610 Result
:= sched_yield
;
618 procedure Set_Priority
620 Prio
: System
.Any_Priority
;
621 Loss_Of_Inheritance
: Boolean := False)
623 pragma Unreferenced
(Loss_Of_Inheritance
);
625 Result
: Interfaces
.C
.int
;
626 Param
: aliased struct_sched_param
;
628 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
629 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
630 -- Get priority specific dispatching policy
632 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
633 -- Upper case first character of the policy name corresponding to the
634 -- task as set by a Priority_Specific_Dispatching pragma.
637 T
.Common
.Current_Priority
:= Prio
;
638 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
640 if Dispatching_Policy
= 'R'
641 or else Priority_Specific_Policy
= 'R'
642 or else Time_Slice_Val
> 0
645 pthread_setschedparam
646 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
648 elsif Dispatching_Policy
= 'F'
649 or else Priority_Specific_Policy
= 'F'
650 or else Time_Slice_Val
= 0
653 pthread_setschedparam
654 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
657 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
658 -- Since the translation from Underlying priorities results
659 -- in a range of 16 - 31, dividing by 2 gives the correct result.
661 Param
.sched_priority
:= Param
.sched_priority
/ 2;
663 pthread_setschedparam
664 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
667 pragma Assert
(Result
= 0);
674 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
676 return T
.Common
.Current_Priority
;
683 procedure Enter_Task
(Self_ID
: Task_Id
) is
685 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
686 Specific
.Set
(Self_ID
);
693 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
695 -----------------------------
696 -- Register_Foreign_Thread --
697 -----------------------------
699 function Register_Foreign_Thread
return Task_Id
is
701 if Is_Valid_Task
then
704 return Register_Foreign_Thread
(pthread_self
);
706 end Register_Foreign_Thread
;
712 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
713 Mutex_Attr
: aliased pthread_mutexattr_t
;
714 Result
: Interfaces
.C
.int
;
715 Cond_Attr
: aliased pthread_condattr_t
;
718 -- More comments required in body below ???
720 if not Single_Lock
then
721 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
722 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
727 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
728 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
736 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
737 pragma Assert
(Result
= 0);
740 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
741 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
746 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
747 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 Result
: Interfaces
.C
.int
;
780 function Thread_Body_Access
is new
781 Ada
.Unchecked_Conversion
(System
.Aux_DEC
.Short_Address
, Thread_Body
);
783 Task_Name
: String (1 .. System
.Parameters
.Max_Task_Image_Length
+ 1);
786 -- Since the initial signal mask of a thread is inherited from the
787 -- creator, we need to set our local signal mask to mask all signals
788 -- during the creation operation, to make sure the new thread is
789 -- not disturbed by signals before it has set its own Task_Id.
791 Result
:= pthread_attr_init
(Attributes
'Access);
792 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
799 Result
:= pthread_attr_setdetachstate
800 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
801 pragma Assert
(Result
= 0);
803 Result
:= pthread_attr_setstacksize
804 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
805 pragma Assert
(Result
= 0);
807 -- This call may be unnecessary, not sure. ???
810 pthread_attr_setinheritsched
811 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
812 pragma Assert
(Result
= 0);
814 if T
.Common
.Task_Image_Len
> 0 then
816 -- Set thread name to ease debugging
818 Task_Name
(1 .. T
.Common
.Task_Image_Len
) :=
819 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
820 Task_Name
(T
.Common
.Task_Image_Len
+ 1) := ASCII
.NUL
;
822 Result
:= pthread_attr_setname_np
823 (Attributes
'Access, Task_Name
'Address, Null_Address
);
824 pragma Assert
(Result
= 0);
827 -- Note: the use of Unrestricted_Access in the following call is needed
828 -- because otherwise we have an error of getting a access-to-volatile
829 -- value which points to a non-volatile object. But in this case it is
830 -- safe to do this, since we know we have no problems with aliasing and
831 -- Unrestricted_Access bypasses this check.
835 (T
.Common
.LL
.Thread
'Unrestricted_Access,
837 Thread_Body_Access
(Wrapper
),
840 -- ENOMEM is a valid run-time error -- do not shut down
842 pragma Assert
(Result
= 0
843 or else Result
= EAGAIN
or else Result
= ENOMEM
);
845 Succeeded
:= Result
= 0;
847 Result
:= pthread_attr_destroy
(Attributes
'Access);
848 pragma Assert
(Result
= 0);
851 Set_Priority
(T
, Priority
);
859 procedure Finalize_TCB
(T
: Task_Id
) is
860 Result
: Interfaces
.C
.int
;
863 if not Single_Lock
then
864 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
865 pragma Assert
(Result
= 0);
868 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
869 pragma Assert
(Result
= 0);
871 if T
.Known_Tasks_Index
/= -1 then
872 Known_Tasks
(T
.Known_Tasks_Index
) := null;
875 ATCB_Allocation
.Free_ATCB
(T
);
882 procedure Exit_Task
is
891 procedure Abort_Task
(T
: Task_Id
) is
893 -- Interrupt Server_Tasks may be waiting on an event flag
895 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
896 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
904 procedure Initialize
(S
: in out Suspension_Object
) is
905 Mutex_Attr
: aliased pthread_mutexattr_t
;
906 Cond_Attr
: aliased pthread_condattr_t
;
907 Result
: Interfaces
.C
.int
;
909 -- Initialize internal state (always to False (D.10 (6)))
914 -- Initialize internal mutex
916 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
917 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
919 if Result
= ENOMEM
then
923 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
924 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
926 if Result
= ENOMEM
then
927 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
928 pragma Assert
(Result
= 0);
933 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
934 pragma Assert
(Result
= 0);
936 -- Initialize internal condition variable
938 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
939 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
942 Result
:= pthread_mutex_destroy
(S
.L
'Access);
943 pragma Assert
(Result
= 0);
945 if Result
= ENOMEM
then
950 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
951 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
954 Result
:= pthread_mutex_destroy
(S
.L
'Access);
955 pragma Assert
(Result
= 0);
957 if Result
= ENOMEM
then
958 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
959 pragma Assert
(Result
= 0);
965 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
966 pragma Assert
(Result
= 0);
973 procedure Finalize
(S
: in out Suspension_Object
) is
974 Result
: Interfaces
.C
.int
;
977 -- Destroy internal mutex
979 Result
:= pthread_mutex_destroy
(S
.L
'Access);
980 pragma Assert
(Result
= 0);
982 -- Destroy internal condition variable
984 Result
:= pthread_cond_destroy
(S
.CV
'Access);
985 pragma Assert
(Result
= 0);
992 function Current_State
(S
: Suspension_Object
) return Boolean is
994 -- We do not want to use lock on this read operation. State is marked
995 -- as Atomic so that we ensure that the value retrieved is correct.
1004 procedure Set_False
(S
: in out Suspension_Object
) is
1005 Result
: Interfaces
.C
.int
;
1008 SSL
.Abort_Defer
.all;
1010 Result
:= pthread_mutex_lock
(S
.L
'Access);
1011 pragma Assert
(Result
= 0);
1015 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1016 pragma Assert
(Result
= 0);
1018 SSL
.Abort_Undefer
.all;
1025 procedure Set_True
(S
: in out Suspension_Object
) is
1026 Result
: Interfaces
.C
.int
;
1029 SSL
.Abort_Defer
.all;
1031 Result
:= pthread_mutex_lock
(S
.L
'Access);
1032 pragma Assert
(Result
= 0);
1034 -- If there is already a task waiting on this suspension object then
1035 -- we resume it, leaving the state of the suspension object to False,
1036 -- as specified in (RM D.10(9)), otherwise leave state set to True.
1042 Result
:= pthread_cond_signal
(S
.CV
'Access);
1043 pragma Assert
(Result
= 0);
1049 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1050 pragma Assert
(Result
= 0);
1052 SSL
.Abort_Undefer
.all;
1055 ------------------------
1056 -- Suspend_Until_True --
1057 ------------------------
1059 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1060 Result
: Interfaces
.C
.int
;
1063 SSL
.Abort_Defer
.all;
1065 Result
:= pthread_mutex_lock
(S
.L
'Access);
1066 pragma Assert
(Result
= 0);
1070 -- Program_Error must be raised upon calling Suspend_Until_True
1071 -- if another task is already waiting on that suspension object
1074 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1075 pragma Assert
(Result
= 0);
1077 SSL
.Abort_Undefer
.all;
1079 raise Program_Error
;
1082 -- Suspend the task if the state is False. Otherwise, the task
1083 -- continues its execution, and the state of the suspension object
1084 -- is set to False (ARM D.10 par. 9).
1092 -- Loop in case pthread_cond_wait returns earlier than expected
1093 -- (e.g. in case of EINTR caused by a signal).
1095 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1096 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1098 exit when not S
.Waiting
;
1102 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1103 pragma Assert
(Result
= 0);
1105 SSL
.Abort_Undefer
.all;
1107 end Suspend_Until_True
;
1115 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1116 pragma Unreferenced
(Self_ID
);
1121 --------------------
1122 -- Check_No_Locks --
1123 --------------------
1125 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1126 pragma Unreferenced
(Self_ID
);
1131 ----------------------
1132 -- Environment_Task --
1133 ----------------------
1135 function Environment_Task
return Task_Id
is
1137 return Environment_Task_Id
;
1138 end Environment_Task
;
1144 procedure Lock_RTS
is
1146 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1153 procedure Unlock_RTS
is
1155 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1162 function Suspend_Task
1164 Thread_Self
: Thread_Id
) return Boolean
1166 pragma Unreferenced
(T
);
1167 pragma Unreferenced
(Thread_Self
);
1176 function Resume_Task
1178 Thread_Self
: Thread_Id
) return Boolean
1180 pragma Unreferenced
(T
);
1181 pragma Unreferenced
(Thread_Self
);
1186 --------------------
1187 -- Stop_All_Tasks --
1188 --------------------
1190 procedure Stop_All_Tasks
is
1199 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1200 pragma Unreferenced
(T
);
1209 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1210 pragma Unreferenced
(T
);
1219 procedure Initialize
(Environment_Task
: Task_Id
) is
1221 -- The DEC Ada facility code defined in Starlet
1222 Ada_Facility
: constant := 49;
1224 function DBGEXT
(Control_Block
: System
.Address
)
1225 return System
.Aux_DEC
.Unsigned_Word
;
1226 -- DBGEXT is imported from s-tasdeb.adb and its parameter re-typed
1227 -- as Address to avoid having a VMS specific s-tasdeb.ads.
1228 pragma Import
(C
, DBGEXT
);
1229 pragma Import_Function
(DBGEXT
, "GNAT$DBGEXT");
1231 type Facility_Type
is range 0 .. 65535;
1233 procedure Debug_Register
1234 (ADBGEXT
: System
.Address
;
1235 ATCB_Key
: pthread_key_t
;
1236 Facility
: Facility_Type
;
1237 Std_Prolog
: Integer);
1238 pragma Import
(C
, Debug_Register
, "CMA$DEBUG_REGISTER");
1240 Environment_Task_Id
:= Environment_Task
;
1242 -- Initialize the lock used to synchronize chain of all ATCBs
1244 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1246 Specific
.Initialize
(Environment_Task
);
1248 -- Pass the context key on to CMA along with the other parameters
1251 DBGEXT
'Address, -- Our DEBUG handling entry point
1252 ATCB_Key
, -- CMA context key for our Ada TCB's
1253 Ada_Facility
, -- Out facility code
1254 0 -- False, we don't have the std TCB prolog
1257 -- Make environment task known here because it doesn't go through
1258 -- Activate_Tasks, which does it for all other tasks.
1260 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1261 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1263 Enter_Task
(Environment_Task
);
1266 -----------------------
1267 -- Set_Task_Affinity --
1268 -----------------------
1270 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1271 pragma Unreferenced
(T
);
1274 -- Setting task affinity is not supported by the underlying system
1277 end Set_Task_Affinity
;
1278 end System
.Task_Primitives
.Operations
;