1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a DEC Unix 4.0d 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.Interrupt_Management;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
51 with System.OS_Primitives;
52 -- used for Delay_Modes
54 with System.Task_Info;
55 -- used for Task_Info_Type
58 -- used for Shift_Left
64 with System.Soft_Links;
65 -- used for Abort_Defer/Undefer
67 -- We use System.Soft_Links instead of System.Tasking.Initialization
68 -- because the later is a higher level package that we shouldn't depend on.
69 -- For example when using the restricted run time, it is replaced by
70 -- System.Tasking.Restricted.Stages.
72 with Unchecked_Deallocation;
74 package body System.Task_Primitives.Operations is
76 package SSL renames System.Soft_Links;
78 use System.Tasking.Debug;
81 use System.OS_Interface;
82 use System.Parameters;
83 use System.OS_Primitives;
89 -- The followings are logically constants, but need to be initialized
92 Single_RTS_Lock : aliased RTS_Lock;
93 -- This is a lock to allow only one thread of control in the RTS at
94 -- a time; it is used to execute in mutual exclusion from all other tasks.
95 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
97 ATCB_Key : aliased pthread_key_t;
98 -- Key used to find the Ada Task_Id associated with a thread
100 Environment_Task_Id : Task_Id;
101 -- A variable to hold Task_Id for the environment task
103 Unblocked_Signal_Mask : aliased sigset_t;
104 -- The set of signals that should unblocked in all tasks
106 Time_Slice_Val : Integer;
107 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
109 Locking_Policy : Character;
110 pragma Import (C, Locking_Policy, "__gl_locking_policy");
112 Dispatching_Policy : Character;
113 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
117 Foreign_Task_Elaborated : aliased Boolean := True;
118 -- Used to identified fake tasks (i.e., non-Ada Threads)
126 procedure Initialize (Environment_Task : Task_Id);
127 pragma Inline (Initialize);
128 -- Initialize various data needed by this package
130 function Is_Valid_Task return Boolean;
131 pragma Inline (Is_Valid_Task);
132 -- Does executing thread have a TCB?
134 procedure Set (Self_Id : Task_Id);
136 -- Set the self id for the current task
138 function Self return Task_Id;
139 pragma Inline (Self);
140 -- Return a pointer to the Ada Task Control Block of the calling task
144 package body Specific is separate;
145 -- The body of this package is target specific
147 ---------------------------------
148 -- Support for foreign threads --
149 ---------------------------------
151 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
152 -- Allocate and initialize a new ATCB for the current Thread
154 function Register_Foreign_Thread
155 (Thread : Thread_Id) return Task_Id is separate;
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 procedure Abort_Handler (Sig : Signal);
162 -- Signal handler used to implement asynchronous abort
164 function Get_Policy (Prio : System.Any_Priority) return Character;
165 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
166 -- Get priority specific dispatching policy
172 procedure Abort_Handler (Sig : Signal) is
173 pragma Unreferenced (Sig);
175 T : constant Task_Id := Self;
176 Result : Interfaces.C.int;
177 Old_Set : aliased sigset_t;
180 -- It is not safe to raise an exception when using ZCX and the GCC
181 -- exception handling mechanism.
183 if ZCX_By_Default and then GCC_ZCX_Support then
187 if T.Deferral_Level = 0
188 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
193 -- Make sure signals used for RTS internal purpose are unmasked
195 Result := pthread_sigmask (SIG_UNBLOCK,
196 Unblocked_Signal_Mask'Unchecked_Access, Old_Set'Unchecked_Access);
197 pragma Assert (Result = 0);
199 raise Standard'Abort_Signal;
207 -- The underlying thread system sets a guard page at the
208 -- bottom of a thread stack, so nothing is needed.
210 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
211 pragma Unreferenced (T);
212 pragma Unreferenced (On);
221 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
223 return T.Common.LL.Thread;
230 function Self return Task_Id renames Specific.Self;
232 ---------------------
233 -- Initialize_Lock --
234 ---------------------
236 -- Note: mutexes and cond_variables needed per-task basis are
237 -- initialized in Initialize_TCB and the Storage_Error is
238 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
239 -- used in RTS is initialized before any status change of RTS.
240 -- Therefore rasing Storage_Error in the following routines
241 -- should be able to be handled safely.
243 procedure Initialize_Lock
244 (Prio : System.Any_Priority;
247 Attributes : aliased pthread_mutexattr_t;
248 Result : Interfaces.C.int;
251 Result := pthread_mutexattr_init (Attributes'Access);
252 pragma Assert (Result = 0 or else Result = ENOMEM);
254 if Result = ENOMEM then
258 if Locking_Policy = 'C' then
259 L.Ceiling := Interfaces.C.int (Prio);
262 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
263 pragma Assert (Result = 0 or else Result = ENOMEM);
265 if Result = ENOMEM then
266 Result := pthread_mutexattr_destroy (Attributes'Access);
270 Result := pthread_mutexattr_destroy (Attributes'Access);
271 pragma Assert (Result = 0);
274 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
275 pragma Unreferenced (Level);
277 Attributes : aliased pthread_mutexattr_t;
278 Result : Interfaces.C.int;
281 Result := pthread_mutexattr_init (Attributes'Access);
282 pragma Assert (Result = 0 or else Result = ENOMEM);
284 if Result = ENOMEM then
288 Result := pthread_mutex_init (L, Attributes'Access);
289 pragma Assert (Result = 0 or else Result = ENOMEM);
291 if Result = ENOMEM then
292 Result := pthread_mutexattr_destroy (Attributes'Access);
296 Result := pthread_mutexattr_destroy (Attributes'Access);
297 pragma Assert (Result = 0);
304 procedure Finalize_Lock (L : access Lock) is
305 Result : Interfaces.C.int;
307 Result := pthread_mutex_destroy (L.L'Access);
308 pragma Assert (Result = 0);
311 procedure Finalize_Lock (L : access RTS_Lock) is
312 Result : Interfaces.C.int;
314 Result := pthread_mutex_destroy (L);
315 pragma Assert (Result = 0);
322 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
323 Result : Interfaces.C.int;
325 All_Tasks_Link : Task_Id;
326 Current_Prio : System.Any_Priority;
329 -- Perform ceiling checks only when this is the locking policy in use
331 if Locking_Policy = 'C' then
333 All_Tasks_Link := Self_ID.Common.All_Tasks_Link;
334 Current_Prio := Get_Priority (Self_ID);
336 -- If there is no other task, no need to check priorities
338 if All_Tasks_Link /= Null_Task
339 and then L.Ceiling < Interfaces.C.int (Current_Prio)
341 Ceiling_Violation := True;
346 Result := pthread_mutex_lock (L.L'Access);
347 pragma Assert (Result = 0);
349 Ceiling_Violation := False;
353 (L : access RTS_Lock; Global_Lock : Boolean := False)
355 Result : Interfaces.C.int;
357 if not Single_Lock or else Global_Lock then
358 Result := pthread_mutex_lock (L);
359 pragma Assert (Result = 0);
363 procedure Write_Lock (T : Task_Id) is
364 Result : Interfaces.C.int;
366 if not Single_Lock then
367 Result := pthread_mutex_lock (T.Common.LL.L'Access);
368 pragma Assert (Result = 0);
376 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
378 Write_Lock (L, Ceiling_Violation);
385 procedure Unlock (L : access Lock) is
386 Result : Interfaces.C.int;
388 Result := pthread_mutex_unlock (L.L'Access);
389 pragma Assert (Result = 0);
392 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
393 Result : Interfaces.C.int;
395 if not Single_Lock or else Global_Lock then
396 Result := pthread_mutex_unlock (L);
397 pragma Assert (Result = 0);
401 procedure Unlock (T : Task_Id) is
402 Result : Interfaces.C.int;
404 if not Single_Lock then
405 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
406 pragma Assert (Result = 0);
416 Reason : System.Tasking.Task_States)
418 pragma Unreferenced (Reason);
420 Result : Interfaces.C.int;
424 Result := pthread_cond_wait
425 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
427 Result := pthread_cond_wait
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);
440 -- This is for use within the run-time system, so abort is
441 -- assumed to be already deferred, and the caller should be
442 -- holding its own ATCB lock.
444 procedure Timed_Sleep
447 Mode : ST.Delay_Modes;
448 Reason : System.Tasking.Task_States;
449 Timedout : out Boolean;
450 Yielded : out Boolean)
452 pragma Unreferenced (Reason);
454 Check_Time : constant Duration := Monotonic_Clock;
456 Request : aliased timespec;
457 Result : Interfaces.C.int;
463 if Mode = Relative then
464 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
466 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
469 if Abs_Time > Check_Time then
470 Request := To_Timespec (Abs_Time);
473 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
474 or else Self_ID.Pending_Priority_Change;
477 Result := pthread_cond_timedwait
478 (Self_ID.Common.LL.CV'Access,
479 Single_RTS_Lock'Access,
483 Result := pthread_cond_timedwait
484 (Self_ID.Common.LL.CV'Access,
485 Self_ID.Common.LL.L'Access,
489 exit when Abs_Time <= Monotonic_Clock;
491 if Result = 0 or Result = EINTR then
493 -- Somebody may have called Wakeup for us
499 pragma Assert (Result = ETIMEDOUT);
508 -- This is for use in implementing delay statements, so
509 -- we assume the caller is abort-deferred but is holding
512 procedure Timed_Delay
515 Mode : ST.Delay_Modes)
517 Check_Time : constant Duration := Monotonic_Clock;
519 Request : aliased timespec;
520 Result : Interfaces.C.int;
527 Write_Lock (Self_ID);
529 if Mode = Relative then
530 Abs_Time := Time + Check_Time;
532 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
535 if Abs_Time > Check_Time then
536 Request := To_Timespec (Abs_Time);
537 Self_ID.Common.State := Delay_Sleep;
540 if Self_ID.Pending_Priority_Change then
541 Self_ID.Pending_Priority_Change := False;
542 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
543 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
546 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
549 Result := pthread_cond_timedwait
550 (Self_ID.Common.LL.CV'Access,
551 Single_RTS_Lock'Access,
554 Result := pthread_cond_timedwait (Self_ID.Common.LL.CV'Access,
555 Self_ID.Common.LL.L'Access, Request'Access);
558 exit when Abs_Time <= Monotonic_Clock;
560 pragma Assert (Result = 0 or else
561 Result = ETIMEDOUT or else
565 Self_ID.Common.State := Runnable;
577 ---------------------
578 -- Monotonic_Clock --
579 ---------------------
581 function Monotonic_Clock return Duration is
582 TS : aliased timespec;
583 Result : Interfaces.C.int;
585 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
586 pragma Assert (Result = 0);
587 return To_Duration (TS);
594 function RT_Resolution return Duration is
596 -- Returned value must be an integral multiple of Duration'Small (1 ns)
597 -- The following is the best approximation of 1/1024. The clock on the
598 -- DEC Alpha ticks at 1024 Hz.
600 return 0.000_976_563;
607 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
608 pragma Unreferenced (Reason);
609 Result : Interfaces.C.int;
611 Result := pthread_cond_signal (T.Common.LL.CV'Access);
612 pragma Assert (Result = 0);
619 procedure Yield (Do_Yield : Boolean := True) is
620 Result : Interfaces.C.int;
621 pragma Unreferenced (Result);
624 Result := sched_yield;
632 procedure Set_Priority
634 Prio : System.Any_Priority;
635 Loss_Of_Inheritance : Boolean := False)
637 pragma Unreferenced (Loss_Of_Inheritance);
639 Result : Interfaces.C.int;
640 Param : aliased struct_sched_param;
642 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
643 -- Upper case first character of the policy name corresponding to the
644 -- task as set by a Priority_Specific_Dispatching pragma.
647 T.Common.Current_Priority := Prio;
648 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
650 if Dispatching_Policy = 'R'
651 or else Priority_Specific_Policy = 'R'
652 or else Time_Slice_Val > 0
654 Result := pthread_setschedparam
655 (T.Common.LL.Thread, SCHED_RR, Param'Access);
657 elsif Dispatching_Policy = 'F'
658 or else Priority_Specific_Policy = 'F'
659 or else Time_Slice_Val = 0
661 Result := pthread_setschedparam
662 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
665 Result := pthread_setschedparam
666 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
669 pragma Assert (Result = 0);
676 function Get_Priority (T : Task_Id) return System.Any_Priority is
678 return T.Common.Current_Priority;
685 procedure Enter_Task (Self_ID : Task_Id) is
688 Self_ID.Common.LL.Thread := pthread_self;
689 Specific.Set (Self_ID);
693 for J in Known_Tasks'Range loop
694 if Known_Tasks (J) = null then
695 Known_Tasks (J) := Self_ID;
696 Self_ID.Known_Tasks_Index := J;
708 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
710 return new Ada_Task_Control_Block (Entry_Num);
717 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
719 -----------------------------
720 -- Register_Foreign_Thread --
721 -----------------------------
723 function Register_Foreign_Thread return Task_Id is
725 if Is_Valid_Task then
728 return Register_Foreign_Thread (pthread_self);
730 end Register_Foreign_Thread;
736 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
737 Mutex_Attr : aliased pthread_mutexattr_t;
738 Result : Interfaces.C.int;
739 Cond_Attr : aliased pthread_condattr_t;
742 if not Single_Lock then
743 Result := pthread_mutexattr_init (Mutex_Attr'Access);
744 pragma Assert (Result = 0 or else Result = ENOMEM);
747 Result := pthread_mutex_init
748 (Self_ID.Common.LL.L'Access, Mutex_Attr'Access);
749 pragma Assert (Result = 0 or else Result = ENOMEM);
757 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
758 pragma Assert (Result = 0);
761 Result := pthread_condattr_init (Cond_Attr'Access);
762 pragma Assert (Result = 0 or else Result = ENOMEM);
765 Result := pthread_cond_init
766 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
767 pragma Assert (Result = 0 or else Result = ENOMEM);
773 if not Single_Lock then
774 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
775 pragma Assert (Result = 0);
781 Result := pthread_condattr_destroy (Cond_Attr'Access);
782 pragma Assert (Result = 0);
789 procedure Create_Task
791 Wrapper : System.Address;
792 Stack_Size : System.Parameters.Size_Type;
793 Priority : System.Any_Priority;
794 Succeeded : out Boolean)
796 Attributes : aliased pthread_attr_t;
797 Adjusted_Stack_Size : Interfaces.C.size_t;
798 Result : Interfaces.C.int;
799 Param : aliased System.OS_Interface.struct_sched_param;
801 Priority_Specific_Policy : constant Character := Get_Policy (Priority);
802 -- Upper case first character of the policy name corresponding to the
803 -- task as set by a Priority_Specific_Dispatching pragma.
805 use System.Task_Info;
808 -- Account for the Yellow Zone (2 pages) and the guard page
809 -- right above. See Hide_Yellow_Zone for the rationale.
811 Adjusted_Stack_Size :=
812 Interfaces.C.size_t (Stack_Size) + 3 * Get_Page_Size;
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, Adjusted_Stack_Size);
828 pragma Assert (Result = 0);
830 Param.sched_priority :=
831 Interfaces.C.int (Underlying_Priorities (Priority));
832 Result := pthread_attr_setschedparam
833 (Attributes'Access, Param'Access);
834 pragma Assert (Result = 0);
836 if Dispatching_Policy = 'R'
837 or else Priority_Specific_Policy = 'R'
838 or else Time_Slice_Val > 0
840 Result := pthread_attr_setschedpolicy
841 (Attributes'Access, System.OS_Interface.SCHED_RR);
843 elsif Dispatching_Policy = 'F'
844 or else Priority_Specific_Policy = 'F'
845 or else Time_Slice_Val = 0
847 Result := pthread_attr_setschedpolicy
848 (Attributes'Access, System.OS_Interface.SCHED_FIFO);
851 Result := pthread_attr_setschedpolicy
852 (Attributes'Access, System.OS_Interface.SCHED_OTHER);
855 pragma Assert (Result = 0);
857 -- Set the scheduling parameters explicitly, since this is the
858 -- only way to force the OS to take e.g. the sched policy and scope
859 -- attributes into account.
861 Result := pthread_attr_setinheritsched
862 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
863 pragma Assert (Result = 0);
865 T.Common.Current_Priority := Priority;
867 if T.Common.Task_Info /= null then
868 case T.Common.Task_Info.Contention_Scope is
869 when System.Task_Info.Process_Scope =>
870 Result := pthread_attr_setscope
871 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
873 when System.Task_Info.System_Scope =>
874 Result := pthread_attr_setscope
875 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
877 when System.Task_Info.Default_Scope =>
881 pragma Assert (Result = 0);
884 -- Since the initial signal mask of a thread is inherited from the
885 -- creator, and the Environment task has all its signals masked, we
886 -- do not need to manipulate caller's signal mask at this point.
887 -- All tasks in RTS will have All_Tasks_Mask initially.
889 Result := pthread_create
890 (T.Common.LL.Thread'Access,
892 Thread_Body_Access (Wrapper),
894 pragma Assert (Result = 0 or else Result = EAGAIN);
896 Succeeded := Result = 0;
898 Result := pthread_attr_destroy (Attributes'Access);
899 pragma Assert (Result = 0);
901 if T.Common.Task_Info /= null then
902 -- ??? We're using a process-wide function to implement a task
903 -- specific characteristic.
905 if T.Common.Task_Info.Bind_To_Cpu_Number = 0 then
906 Result := bind_to_cpu (Curpid, 0);
907 elsif T.Common.Task_Info.Bind_To_Cpu_Number > 0 then
908 Result := bind_to_cpu
910 Interfaces.C.unsigned_long (
911 Interfaces.Shift_Left
912 (Interfaces.Unsigned_64'(1),
913 T.Common.Task_Info.Bind_To_Cpu_Number - 1)));
914 pragma Assert (Result = 0);
923 procedure Finalize_TCB (T : Task_Id) is
924 Result : Interfaces.C.int;
926 Is_Self : constant Boolean := T = Self;
928 procedure Free is new
929 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
932 if not Single_Lock then
933 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
934 pragma Assert (Result = 0);
937 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
938 pragma Assert (Result = 0);
940 if T.Known_Tasks_Index /= -1 then
941 Known_Tasks (T.Known_Tasks_Index) := null;
955 procedure Exit_Task is
964 procedure Abort_Task (T : Task_Id) is
965 Result : Interfaces.C.int;
967 Result := pthread_kill (T.Common.LL.Thread,
968 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
969 pragma Assert (Result = 0);
976 procedure Initialize (S : in out Suspension_Object) is
977 Mutex_Attr : aliased pthread_mutexattr_t;
978 Cond_Attr : aliased pthread_condattr_t;
979 Result : Interfaces.C.int;
981 -- Initialize internal state. It is always initialized to False (ARM
987 -- Initialize internal mutex
989 Result := pthread_mutexattr_init (Mutex_Attr'Access);
990 pragma Assert (Result = 0 or else Result = ENOMEM);
992 if Result = ENOMEM then
996 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
997 pragma Assert (Result = 0 or else Result = ENOMEM);
999 if Result = ENOMEM then
1000 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1001 raise Storage_Error;
1004 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1005 pragma Assert (Result = 0);
1007 -- Initialize internal condition variable
1009 Result := pthread_condattr_init (Cond_Attr'Access);
1010 pragma Assert (Result = 0 or else Result = ENOMEM);
1012 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1014 pragma Assert (Result = 0 or else Result = ENOMEM);
1017 Result := pthread_mutex_destroy (S.L'Access);
1018 pragma Assert (Result = 0);
1020 if Result = ENOMEM then
1021 raise Storage_Error;
1030 procedure Finalize (S : in out Suspension_Object) is
1031 Result : Interfaces.C.int;
1033 -- Destroy internal mutex
1035 Result := pthread_mutex_destroy (S.L'Access);
1036 pragma Assert (Result = 0);
1038 -- Destroy internal condition variable
1040 Result := pthread_cond_destroy (S.CV'Access);
1041 pragma Assert (Result = 0);
1048 function Current_State (S : Suspension_Object) return Boolean is
1050 -- We do not want to use lock on this read operation. State is marked
1051 -- as Atomic so that we ensure that the value retrieved is correct.
1060 procedure Set_False (S : in out Suspension_Object) is
1061 Result : Interfaces.C.int;
1063 SSL.Abort_Defer.all;
1065 Result := pthread_mutex_lock (S.L'Access);
1066 pragma Assert (Result = 0);
1070 Result := pthread_mutex_unlock (S.L'Access);
1071 pragma Assert (Result = 0);
1073 SSL.Abort_Undefer.all;
1080 procedure Set_True (S : in out Suspension_Object) is
1081 Result : Interfaces.C.int;
1083 SSL.Abort_Defer.all;
1085 Result := pthread_mutex_lock (S.L'Access);
1086 pragma Assert (Result = 0);
1088 -- If there is already a task waiting on this suspension object then
1089 -- we resume it, leaving the state of the suspension object to False,
1090 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1091 -- the state to True.
1097 Result := pthread_cond_signal (S.CV'Access);
1098 pragma Assert (Result = 0);
1103 Result := pthread_mutex_unlock (S.L'Access);
1104 pragma Assert (Result = 0);
1106 SSL.Abort_Undefer.all;
1109 ------------------------
1110 -- Suspend_Until_True --
1111 ------------------------
1113 procedure Suspend_Until_True (S : in out Suspension_Object) is
1114 Result : Interfaces.C.int;
1116 SSL.Abort_Defer.all;
1118 Result := pthread_mutex_lock (S.L'Access);
1119 pragma Assert (Result = 0);
1122 -- Program_Error must be raised upon calling Suspend_Until_True
1123 -- if another task is already waiting on that suspension object
1124 -- (ARM D.10 par. 10).
1126 Result := pthread_mutex_unlock (S.L'Access);
1127 pragma Assert (Result = 0);
1129 SSL.Abort_Undefer.all;
1131 raise Program_Error;
1133 -- Suspend the task if the state is False. Otherwise, the task
1134 -- continues its execution, and the state of the suspension object
1135 -- is set to False (ARM D.10 par. 9).
1141 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1144 Result := pthread_mutex_unlock (S.L'Access);
1145 pragma Assert (Result = 0);
1147 SSL.Abort_Undefer.all;
1149 end Suspend_Until_True;
1157 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1158 pragma Unreferenced (Self_ID);
1163 --------------------
1164 -- Check_No_Locks --
1165 --------------------
1167 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1168 pragma Unreferenced (Self_ID);
1173 ----------------------
1174 -- Environment_Task --
1175 ----------------------
1177 function Environment_Task return Task_Id is
1179 return Environment_Task_Id;
1180 end Environment_Task;
1186 procedure Lock_RTS is
1188 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1195 procedure Unlock_RTS is
1197 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1204 function Suspend_Task
1206 Thread_Self : Thread_Id) return Boolean
1208 pragma Warnings (Off, T);
1209 pragma Warnings (Off, Thread_Self);
1218 function Resume_Task
1220 Thread_Self : Thread_Id) return Boolean
1222 pragma Warnings (Off, T);
1223 pragma Warnings (Off, Thread_Self);
1232 procedure Initialize (Environment_Task : Task_Id) is
1233 act : aliased struct_sigaction;
1234 old_act : aliased struct_sigaction;
1235 Tmp_Set : aliased sigset_t;
1236 Result : Interfaces.C.int;
1239 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1240 pragma Import (C, State, "__gnat_get_interrupt_state");
1241 -- Get interrupt state. Defined in a-init.c. The input argument is
1242 -- the interrupt number, and the result is one of the following:
1244 Default : constant Character := 's';
1245 -- 'n' this interrupt not set by any Interrupt_State pragma
1246 -- 'u' Interrupt_State pragma set state to User
1247 -- 'r' Interrupt_State pragma set state to Runtime
1248 -- 's' Interrupt_State pragma set state to System (use "default"
1252 Environment_Task_Id := Environment_Task;
1254 Interrupt_Management.Initialize;
1256 -- Prepare the set of signals that should unblocked in all tasks
1258 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1259 pragma Assert (Result = 0);
1261 for J in Interrupt_Management.Interrupt_ID loop
1262 if System.Interrupt_Management.Keep_Unmasked (J) then
1263 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1264 pragma Assert (Result = 0);
1270 -- Initialize the lock used to synchronize chain of all ATCBs
1272 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1274 Specific.Initialize (Environment_Task);
1276 Enter_Task (Environment_Task);
1278 -- Install the abort-signal handler
1280 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1284 act.sa_handler := Abort_Handler'Address;
1286 Result := sigemptyset (Tmp_Set'Access);
1287 pragma Assert (Result = 0);
1288 act.sa_mask := Tmp_Set;
1292 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1293 act'Unchecked_Access,
1294 old_act'Unchecked_Access);
1295 pragma Assert (Result = 0);
1299 end System.Task_Primitives.Operations;