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-2007, 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 OpenVMS/Alpha 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.
43 with System
.Tasking
.Debug
;
44 -- used for Known_Tasks
46 with System
.OS_Primitives
;
47 -- used for Delay_Modes
53 with System
.Soft_Links
;
54 -- used for Get_Exc_Stack_Addr
55 -- Abort_Defer/Undefer
58 -- used for Short_Address
60 with Ada
.Unchecked_Conversion
;
61 with Ada
.Unchecked_Deallocation
;
63 package body System
.Task_Primitives
.Operations
is
65 use System
.Tasking
.Debug
;
68 use System
.OS_Interface
;
69 use System
.Parameters
;
70 use System
.OS_Primitives
;
71 use type System
.OS_Primitives
.OS_Time
;
73 package SSL
renames System
.Soft_Links
;
79 -- The followings are logically constants, but need to be initialized
82 Single_RTS_Lock
: aliased RTS_Lock
;
83 -- This is a lock to allow only one thread of control in the RTS at
84 -- a time; it is used to execute in mutual exclusion from all other tasks.
85 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
87 ATCB_Key
: aliased pthread_key_t
;
88 -- Key used to find the Ada Task_Id associated with a thread
90 Environment_Task_Id
: Task_Id
;
91 -- A variable to hold Task_Id for the environment task
93 Time_Slice_Val
: Integer;
94 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
96 Dispatching_Policy
: Character;
97 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
99 Foreign_Task_Elaborated
: aliased Boolean := True;
100 -- Used to identified fake tasks (i.e., non-Ada Threads)
108 procedure Initialize
(Environment_Task
: Task_Id
);
109 pragma Inline
(Initialize
);
110 -- Initialize various data needed by this package
112 function Is_Valid_Task
return Boolean;
113 pragma Inline
(Is_Valid_Task
);
114 -- Does executing thread have a TCB?
116 procedure Set
(Self_Id
: Task_Id
);
118 -- Set the self id for the current task
120 function Self
return Task_Id
;
121 pragma Inline
(Self
);
122 -- Return a pointer to the Ada Task Control Block of the calling task
126 package body Specific
is separate;
127 -- The body of this package is target specific
129 ---------------------------------
130 -- Support for foreign threads --
131 ---------------------------------
133 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
134 -- Allocate and Initialize a new ATCB for the current Thread
136 function Register_Foreign_Thread
137 (Thread
: Thread_Id
) return Task_Id
is separate;
139 -----------------------
140 -- Local Subprograms --
141 -----------------------
143 function To_Task_Id
is
144 new Ada
.Unchecked_Conversion
(System
.Address
, Task_Id
);
146 function To_Address
is
147 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
149 function Get_Exc_Stack_Addr
return Address
;
150 -- Replace System.Soft_Links.Get_Exc_Stack_Addr_NT
152 procedure Timer_Sleep_AST
(ID
: Address
);
153 pragma Convention
(C
, Timer_Sleep_AST
);
154 -- Signal the condition variable when AST fires
156 procedure Timer_Sleep_AST
(ID
: Address
) is
157 Result
: Interfaces
.C
.int
;
158 pragma Warnings
(Off
, Result
);
159 Self_ID
: constant Task_Id
:= To_Task_Id
(ID
);
161 Self_ID
.Common
.LL
.AST_Pending
:= False;
162 Result
:= pthread_cond_signal_int_np
(Self_ID
.Common
.LL
.CV
'Access);
163 pragma Assert
(Result
= 0);
170 -- The underlying thread system sets a guard page at the bottom of a thread
171 -- stack, so nothing is needed.
172 -- ??? Check the comment above
174 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
175 pragma Unreferenced
(T
);
176 pragma Unreferenced
(On
);
185 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
187 return T
.Common
.LL
.Thread
;
194 function Self
return Task_Id
renames Specific
.Self
;
196 ---------------------
197 -- Initialize_Lock --
198 ---------------------
200 -- Note: mutexes and cond_variables needed per-task basis are initialized
201 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
202 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
203 -- status change of RTS. Therefore rasing Storage_Error in the following
204 -- routines should be able to be handled safely.
206 procedure Initialize_Lock
207 (Prio
: System
.Any_Priority
;
208 L
: not null access Lock
)
210 Attributes
: aliased pthread_mutexattr_t
;
211 Result
: Interfaces
.C
.int
;
214 Result
:= pthread_mutexattr_init
(Attributes
'Access);
215 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
217 if Result
= ENOMEM
then
222 L
.Prio
:= Interfaces
.C
.int
(Prio
);
224 Result
:= pthread_mutex_init
(L
.L
'Access, Attributes
'Access);
225 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
227 if Result
= ENOMEM
then
231 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
232 pragma Assert
(Result
= 0);
235 procedure Initialize_Lock
236 (L
: not null access RTS_Lock
;
239 pragma Unreferenced
(Level
);
241 Attributes
: aliased pthread_mutexattr_t
;
242 Result
: Interfaces
.C
.int
;
245 Result
:= pthread_mutexattr_init
(Attributes
'Access);
246 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
248 if Result
= ENOMEM
then
252 -- Don't use, see comment in s-osinte.ads about ERRORCHECK mutexes???
253 -- Result := pthread_mutexattr_settype_np
254 -- (Attributes'Access, PTHREAD_MUTEX_ERRORCHECK_NP);
255 -- pragma Assert (Result = 0);
257 -- Result := pthread_mutexattr_setprotocol
258 -- (Attributes'Access, PTHREAD_PRIO_PROTECT);
259 -- pragma Assert (Result = 0);
261 -- Result := pthread_mutexattr_setprioceiling
262 -- (Attributes'Access, Interfaces.C.int (System.Any_Priority'Last));
263 -- pragma Assert (Result = 0);
265 Result
:= pthread_mutex_init
(L
, Attributes
'Access);
267 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
269 if Result
= ENOMEM
then
273 Result
:= pthread_mutexattr_destroy
(Attributes
'Access);
274 pragma Assert
(Result
= 0);
281 procedure Finalize_Lock
(L
: not null access Lock
) is
282 Result
: Interfaces
.C
.int
;
284 Result
:= pthread_mutex_destroy
(L
.L
'Access);
285 pragma Assert
(Result
= 0);
288 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
289 Result
: Interfaces
.C
.int
;
291 Result
:= pthread_mutex_destroy
(L
);
292 pragma Assert
(Result
= 0);
300 (L
: not null access Lock
;
301 Ceiling_Violation
: out Boolean)
303 Self_ID
: constant Task_Id
:= Self
;
304 All_Tasks_Link
: constant Task_Id
:= Self
.Common
.All_Tasks_Link
;
305 Current_Prio
: System
.Any_Priority
;
306 Result
: Interfaces
.C
.int
;
309 Current_Prio
:= Get_Priority
(Self_ID
);
311 -- If there is no other tasks, no need to check priorities
313 if All_Tasks_Link
/= Null_Task
314 and then L
.Prio
< Interfaces
.C
.int
(Current_Prio
)
316 Ceiling_Violation
:= True;
320 Result
:= pthread_mutex_lock
(L
.L
'Access);
321 pragma Assert
(Result
= 0);
323 Ceiling_Violation
:= False;
324 -- Why is this commented out ???
325 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
326 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
330 (L
: not null access RTS_Lock
;
331 Global_Lock
: Boolean := False)
333 Result
: Interfaces
.C
.int
;
335 if not Single_Lock
or else Global_Lock
then
336 Result
:= pthread_mutex_lock
(L
);
337 pragma Assert
(Result
= 0);
341 procedure Write_Lock
(T
: Task_Id
) is
342 Result
: Interfaces
.C
.int
;
344 if not Single_Lock
then
345 Result
:= pthread_mutex_lock
(T
.Common
.LL
.L
'Access);
346 pragma Assert
(Result
= 0);
355 (L
: not null access Lock
;
356 Ceiling_Violation
: out Boolean)
359 Write_Lock
(L
, Ceiling_Violation
);
366 procedure Unlock
(L
: not null access Lock
) is
367 Result
: Interfaces
.C
.int
;
369 Result
:= pthread_mutex_unlock
(L
.L
'Access);
370 pragma Assert
(Result
= 0);
374 (L
: not null access RTS_Lock
;
375 Global_Lock
: Boolean := False)
377 Result
: Interfaces
.C
.int
;
379 if not Single_Lock
or else Global_Lock
then
380 Result
:= pthread_mutex_unlock
(L
);
381 pragma Assert
(Result
= 0);
385 procedure Unlock
(T
: Task_Id
) is
386 Result
: Interfaces
.C
.int
;
388 if not Single_Lock
then
389 Result
:= pthread_mutex_unlock
(T
.Common
.LL
.L
'Access);
390 pragma Assert
(Result
= 0);
398 -- Dynamic priority ceilings are not supported by the underlying system
400 procedure Set_Ceiling
401 (L
: not null access Lock
;
402 Prio
: System
.Any_Priority
)
404 pragma Unreferenced
(L
, Prio
);
415 Reason
: System
.Tasking
.Task_States
)
417 pragma Unreferenced
(Reason
);
418 Result
: Interfaces
.C
.int
;
424 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
428 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
431 -- EINTR is not considered a failure
433 pragma Assert
(Result
= 0 or else Result
= EINTR
);
435 if Self_ID
.Deferral_Level
= 0
436 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
439 raise Standard
'Abort_Signal;
447 procedure Timed_Sleep
450 Mode
: ST
.Delay_Modes
;
451 Reason
: System
.Tasking
.Task_States
;
452 Timedout
: out Boolean;
453 Yielded
: out Boolean)
455 pragma Unreferenced
(Reason
);
457 Sleep_Time
: OS_Time
;
458 Result
: Interfaces
.C
.int
;
459 Status
: Cond_Value_Type
;
461 -- The body below requires more comments ???
467 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
469 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
473 Self_ID
.Common
.LL
.AST_Pending
:= True;
476 (Status
, 0, Sleep_Time
,
477 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
479 if (Status
and 1) /= 1 then
486 (Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
487 pragma Assert
(Result
= 0);
492 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
493 pragma Assert
(Result
= 0);
498 if not Self_ID
.Common
.LL
.AST_Pending
then
501 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
502 pragma Assert
((Status
and 1) = 1);
510 procedure Timed_Delay
513 Mode
: ST
.Delay_Modes
)
515 Sleep_Time
: OS_Time
;
516 Result
: Interfaces
.C
.int
;
517 Status
: Cond_Value_Type
;
518 Yielded
: Boolean := False;
525 -- More comments required in body below ???
527 Write_Lock
(Self_ID
);
529 if Time
/= 0.0 or else Mode
/= Relative
then
530 Sleep_Time
:= To_OS_Time
(Time
, Mode
);
532 if Mode
= Relative
or else OS_Clock
< Sleep_Time
then
533 Self_ID
.Common
.State
:= Delay_Sleep
;
534 Self_ID
.Common
.LL
.AST_Pending
:= True;
537 (Status
, 0, Sleep_Time
,
538 Timer_Sleep_AST
'Access, To_Address
(Self_ID
), 0);
540 -- Comment following test
542 if (Status
and 1) /= 1 then
547 if Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
then
548 Sys_Cantim
(Status
, To_Address
(Self_ID
), 0);
549 pragma Assert
((Status
and 1) = 1);
556 (Self_ID
.Common
.LL
.CV
'Access,
557 Single_RTS_Lock
'Access);
558 pragma Assert
(Result
= 0);
562 (Self_ID
.Common
.LL
.CV
'Access,
563 Self_ID
.Common
.LL
.L
'Access);
564 pragma Assert
(Result
= 0);
569 exit when not Self_ID
.Common
.LL
.AST_Pending
;
572 Self_ID
.Common
.State
:= Runnable
;
583 Result
:= sched_yield
;
584 pragma Assert
(Result
= 0);
588 ---------------------
589 -- Monotonic_Clock --
590 ---------------------
592 function Monotonic_Clock
return Duration
593 renames System
.OS_Primitives
.Monotonic_Clock
;
599 function RT_Resolution
return Duration is
601 -- Document origin of this magic constant ???
609 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
610 pragma Unreferenced
(Reason
);
611 Result
: Interfaces
.C
.int
;
613 Result
:= pthread_cond_signal
(T
.Common
.LL
.CV
'Access);
614 pragma Assert
(Result
= 0);
621 procedure Yield
(Do_Yield
: Boolean := True) is
622 Result
: Interfaces
.C
.int
;
623 pragma Unreferenced
(Result
);
626 Result
:= sched_yield
;
634 procedure Set_Priority
636 Prio
: System
.Any_Priority
;
637 Loss_Of_Inheritance
: Boolean := False)
639 pragma Unreferenced
(Loss_Of_Inheritance
);
641 Result
: Interfaces
.C
.int
;
642 Param
: aliased struct_sched_param
;
644 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
645 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
646 -- Get priority specific dispatching policy
648 Priority_Specific_Policy
: constant Character := Get_Policy
(Prio
);
649 -- Upper case first character of the policy name corresponding to the
650 -- task as set by a Priority_Specific_Dispatching pragma.
653 T
.Common
.Current_Priority
:= Prio
;
654 Param
.sched_priority
:= Interfaces
.C
.int
(Underlying_Priorities
(Prio
));
656 if Dispatching_Policy
= 'R'
657 or else Priority_Specific_Policy
= 'R'
658 or else Time_Slice_Val
> 0
661 pthread_setschedparam
662 (T
.Common
.LL
.Thread
, SCHED_RR
, Param
'Access);
664 elsif Dispatching_Policy
= 'F'
665 or else Priority_Specific_Policy
= 'F'
666 or else Time_Slice_Val
= 0
669 pthread_setschedparam
670 (T
.Common
.LL
.Thread
, SCHED_FIFO
, Param
'Access);
673 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
674 -- Since the translation from Underlying priorities results
675 -- in a range of 16 - 31, dividing by 2 gives the correct result.
677 Param
.sched_priority
:= Param
.sched_priority
/ 2;
679 pthread_setschedparam
680 (T
.Common
.LL
.Thread
, SCHED_OTHER
, Param
'Access);
683 pragma Assert
(Result
= 0);
690 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
692 return T
.Common
.Current_Priority
;
699 procedure Enter_Task
(Self_ID
: Task_Id
) is
701 Self_ID
.Common
.LL
.Thread
:= pthread_self
;
703 Specific
.Set
(Self_ID
);
707 for J
in Known_Tasks
'Range loop
708 if Known_Tasks
(J
) = null then
709 Known_Tasks
(J
) := Self_ID
;
710 Self_ID
.Known_Tasks_Index
:= J
;
722 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
724 return new Ada_Task_Control_Block
(Entry_Num
);
731 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
733 -----------------------------
734 -- Register_Foreign_Thread --
735 -----------------------------
737 function Register_Foreign_Thread
return Task_Id
is
739 if Is_Valid_Task
then
742 return Register_Foreign_Thread
(pthread_self
);
744 end Register_Foreign_Thread
;
750 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
751 Mutex_Attr
: aliased pthread_mutexattr_t
;
752 Result
: Interfaces
.C
.int
;
753 Cond_Attr
: aliased pthread_condattr_t
;
756 -- More comments required in body below ???
758 if not Single_Lock
then
759 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
760 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
765 (Self_ID
.Common
.LL
.L
'Access, Mutex_Attr
'Access);
766 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
774 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
775 pragma Assert
(Result
= 0);
778 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
779 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
784 (Self_ID
.Common
.LL
.CV
'Access, Cond_Attr
'Access);
785 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
790 Self_ID
.Common
.LL
.Exc_Stack_Ptr
:= new Exc_Stack_T
;
793 if not Single_Lock
then
794 Result
:= pthread_mutex_destroy
(Self_ID
.Common
.LL
.L
'Access);
795 pragma Assert
(Result
= 0);
801 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
802 pragma Assert
(Result
= 0);
805 ------------------------
806 -- Get_Exc_Stack_Addr --
807 ------------------------
809 function Get_Exc_Stack_Addr
return Address
is
811 return Self
.Common
.LL
.Exc_Stack_Ptr
(Exc_Stack_T
'Last)'Address;
812 end Get_Exc_Stack_Addr
;
818 procedure Create_Task
820 Wrapper
: System
.Address
;
821 Stack_Size
: System
.Parameters
.Size_Type
;
822 Priority
: System
.Any_Priority
;
823 Succeeded
: out Boolean)
825 Attributes
: aliased pthread_attr_t
;
826 Result
: Interfaces
.C
.int
;
828 function Thread_Body_Access
is new
829 Ada
.Unchecked_Conversion
(System
.Aux_DEC
.Short_Address
, Thread_Body
);
832 -- Since the initial signal mask of a thread is inherited from the
833 -- creator, we need to set our local signal mask mask all signals
834 -- during the creation operation, to make sure the new thread is
835 -- not disturbed by signals before it has set its own Task_Id.
837 Result
:= pthread_attr_init
(Attributes
'Access);
838 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
845 Result
:= pthread_attr_setdetachstate
846 (Attributes
'Access, PTHREAD_CREATE_DETACHED
);
847 pragma Assert
(Result
= 0);
849 Result
:= pthread_attr_setstacksize
850 (Attributes
'Access, Interfaces
.C
.size_t
(Stack_Size
));
851 pragma Assert
(Result
= 0);
853 -- This call may be unnecessary, not sure. ???
856 pthread_attr_setinheritsched
857 (Attributes
'Access, PTHREAD_EXPLICIT_SCHED
);
858 pragma Assert
(Result
= 0);
862 (T
.Common
.LL
.Thread
'Access,
864 Thread_Body_Access
(Wrapper
),
867 -- ENOMEM is a valid run-time error -- do not shut down
869 pragma Assert
(Result
= 0
870 or else Result
= EAGAIN
or else Result
= ENOMEM
);
872 Succeeded
:= Result
= 0;
874 Result
:= pthread_attr_destroy
(Attributes
'Access);
875 pragma Assert
(Result
= 0);
878 Set_Priority
(T
, Priority
);
886 procedure Finalize_TCB
(T
: Task_Id
) is
887 Result
: Interfaces
.C
.int
;
889 Is_Self
: constant Boolean := T
= Self
;
891 procedure Free
is new
892 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
894 procedure Free
is new Ada
.Unchecked_Deallocation
895 (Exc_Stack_T
, Exc_Stack_Ptr_T
);
898 if not Single_Lock
then
899 Result
:= pthread_mutex_destroy
(T
.Common
.LL
.L
'Access);
900 pragma Assert
(Result
= 0);
903 Result
:= pthread_cond_destroy
(T
.Common
.LL
.CV
'Access);
904 pragma Assert
(Result
= 0);
906 if T
.Known_Tasks_Index
/= -1 then
907 Known_Tasks
(T
.Known_Tasks_Index
) := null;
910 Free
(T
.Common
.LL
.Exc_Stack_Ptr
);
922 procedure Exit_Task
is
931 procedure Abort_Task
(T
: Task_Id
) is
933 -- Interrupt Server_Tasks may be waiting on an event flag
935 if T
.Common
.State
= Interrupt_Server_Blocked_On_Event_Flag
then
936 Wakeup
(T
, Interrupt_Server_Blocked_On_Event_Flag
);
944 procedure Initialize
(S
: in out Suspension_Object
) is
945 Mutex_Attr
: aliased pthread_mutexattr_t
;
946 Cond_Attr
: aliased pthread_condattr_t
;
947 Result
: Interfaces
.C
.int
;
949 -- Initialize internal state (always to False (D.10 (6)))
954 -- Initialize internal mutex
956 Result
:= pthread_mutexattr_init
(Mutex_Attr
'Access);
957 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
959 if Result
= ENOMEM
then
963 Result
:= pthread_mutex_init
(S
.L
'Access, Mutex_Attr
'Access);
964 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
966 if Result
= ENOMEM
then
967 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
968 pragma Assert
(Result
= 0);
973 Result
:= pthread_mutexattr_destroy
(Mutex_Attr
'Access);
974 pragma Assert
(Result
= 0);
976 -- Initialize internal condition variable
978 Result
:= pthread_condattr_init
(Cond_Attr
'Access);
979 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
982 Result
:= pthread_mutex_destroy
(S
.L
'Access);
983 pragma Assert
(Result
= 0);
985 if Result
= ENOMEM
then
990 Result
:= pthread_cond_init
(S
.CV
'Access, Cond_Attr
'Access);
991 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
994 Result
:= pthread_mutex_destroy
(S
.L
'Access);
995 pragma Assert
(Result
= 0);
997 if Result
= ENOMEM
then
998 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
999 pragma Assert
(Result
= 0);
1001 raise Storage_Error
;
1005 Result
:= pthread_condattr_destroy
(Cond_Attr
'Access);
1006 pragma Assert
(Result
= 0);
1013 procedure Finalize
(S
: in out Suspension_Object
) is
1014 Result
: Interfaces
.C
.int
;
1017 -- Destroy internal mutex
1019 Result
:= pthread_mutex_destroy
(S
.L
'Access);
1020 pragma Assert
(Result
= 0);
1022 -- Destroy internal condition variable
1024 Result
:= pthread_cond_destroy
(S
.CV
'Access);
1025 pragma Assert
(Result
= 0);
1032 function Current_State
(S
: Suspension_Object
) return Boolean is
1034 -- We do not want to use lock on this read operation. State is marked
1035 -- as Atomic so that we ensure that the value retrieved is correct.
1044 procedure Set_False
(S
: in out Suspension_Object
) is
1045 Result
: Interfaces
.C
.int
;
1048 SSL
.Abort_Defer
.all;
1050 Result
:= pthread_mutex_lock
(S
.L
'Access);
1051 pragma Assert
(Result
= 0);
1055 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1056 pragma Assert
(Result
= 0);
1058 SSL
.Abort_Undefer
.all;
1065 procedure Set_True
(S
: in out Suspension_Object
) is
1066 Result
: Interfaces
.C
.int
;
1069 SSL
.Abort_Defer
.all;
1071 Result
:= pthread_mutex_lock
(S
.L
'Access);
1072 pragma Assert
(Result
= 0);
1074 -- If there is already a task waiting on this suspension object then
1075 -- we resume it, leaving the state of the suspension object to False,
1076 -- as specified in (RM D.10(9)), otherwise leave state set to True.
1082 Result
:= pthread_cond_signal
(S
.CV
'Access);
1083 pragma Assert
(Result
= 0);
1089 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1090 pragma Assert
(Result
= 0);
1092 SSL
.Abort_Undefer
.all;
1095 ------------------------
1096 -- Suspend_Until_True --
1097 ------------------------
1099 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1100 Result
: Interfaces
.C
.int
;
1103 SSL
.Abort_Defer
.all;
1105 Result
:= pthread_mutex_lock
(S
.L
'Access);
1106 pragma Assert
(Result
= 0);
1110 -- Program_Error must be raised upon calling Suspend_Until_True
1111 -- if another task is already waiting on that suspension object
1114 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1115 pragma Assert
(Result
= 0);
1117 SSL
.Abort_Undefer
.all;
1119 raise Program_Error
;
1122 -- Suspend the task if the state is False. Otherwise, the task
1123 -- continues its execution, and the state of the suspension object
1124 -- is set to False (ARM D.10 par. 9).
1130 Result
:= pthread_cond_wait
(S
.CV
'Access, S
.L
'Access);
1133 Result
:= pthread_mutex_unlock
(S
.L
'Access);
1134 pragma Assert
(Result
= 0);
1136 SSL
.Abort_Undefer
.all;
1138 end Suspend_Until_True
;
1146 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1147 pragma Unreferenced
(Self_ID
);
1152 --------------------
1153 -- Check_No_Locks --
1154 --------------------
1156 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1157 pragma Unreferenced
(Self_ID
);
1162 ----------------------
1163 -- Environment_Task --
1164 ----------------------
1166 function Environment_Task
return Task_Id
is
1168 return Environment_Task_Id
;
1169 end Environment_Task
;
1175 procedure Lock_RTS
is
1177 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1184 procedure Unlock_RTS
is
1186 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1193 function Suspend_Task
1195 Thread_Self
: Thread_Id
) return Boolean
1197 pragma Unreferenced
(T
);
1198 pragma Unreferenced
(Thread_Self
);
1207 function Resume_Task
1209 Thread_Self
: Thread_Id
) return Boolean
1211 pragma Unreferenced
(T
);
1212 pragma Unreferenced
(Thread_Self
);
1217 --------------------
1218 -- Stop_All_Tasks --
1219 --------------------
1221 procedure Stop_All_Tasks
is
1230 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1231 pragma Unreferenced
(T
);
1240 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1241 pragma Unreferenced
(T
);
1250 procedure Initialize
(Environment_Task
: Task_Id
) is
1252 Environment_Task_Id
:= Environment_Task
;
1254 SSL
.Get_Exc_Stack_Addr
:= Get_Exc_Stack_Addr
'Access;
1256 -- Initialize the lock used to synchronize chain of all ATCBs
1258 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1260 Specific
.Initialize
(Environment_Task
);
1262 Enter_Task
(Environment_Task
);
1265 end System
.Task_Primitives
.Operations
;