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 LynxOS version of this file, adapted to make
35 -- SCHED_FIFO and ceiling locking (Annex D compliance) work properly
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System.Tasking.Debug;
45 -- used for Known_Tasks
47 with System.Interrupt_Management;
48 -- used for Keep_Unmasked
49 -- Abort_Task_Interrupt
52 with System.OS_Primitives;
53 -- used for Delay_Modes
55 with System.Task_Info;
56 -- used for Task_Info_Type
62 with System.Soft_Links;
63 -- used for Abort_Defer/Undefer
65 -- We use System.Soft_Links instead of System.Tasking.Initialization
66 -- because the later is a higher level package that we shouldn't depend on.
67 -- For example when using the restricted run time, it is replaced by
68 -- System.Tasking.Restricted.Stages.
70 with Unchecked_Deallocation;
72 package body System.Task_Primitives.Operations is
74 package SSL renames System.Soft_Links;
76 use System.Tasking.Debug;
79 use System.OS_Interface;
80 use System.Parameters;
81 use System.OS_Primitives;
87 -- The followings are logically constants, but need to be initialized
90 Single_RTS_Lock : aliased RTS_Lock;
91 -- This is a lock to allow only one thread of control in the RTS at
92 -- a time; it is used to execute in mutual exclusion from all other tasks.
93 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
95 ATCB_Key : aliased pthread_key_t;
96 -- Key used to find the Ada Task_Id associated with a thread
98 Environment_Task_Id : Task_Id;
99 -- A variable to hold Task_Id for the environment task
101 Locking_Policy : Character;
102 pragma Import (C, Locking_Policy, "__gl_locking_policy");
103 -- Value of the pragma Locking_Policy:
104 -- 'C' for Ceiling_Locking
105 -- 'I' for Inherit_Locking
108 Unblocked_Signal_Mask : aliased sigset_t;
109 -- The set of signals that should unblocked in all tasks
111 -- The followings are internal configuration constants needed
113 Next_Serial_Number : Task_Serial_Number := 100;
114 -- We start at 100, to reserve some special values for
115 -- using in error checking.
117 Time_Slice_Val : Integer;
118 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
120 Dispatching_Policy : Character;
121 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
123 Foreign_Task_Elaborated : aliased Boolean := True;
124 -- Used to identified fake tasks (i.e., non-Ada Threads)
132 procedure Initialize (Environment_Task : Task_Id);
133 pragma Inline (Initialize);
134 -- Initialize various data needed by this package
136 function Is_Valid_Task return Boolean;
137 pragma Inline (Is_Valid_Task);
138 -- Does the current thread have an ATCB?
140 procedure Set (Self_Id : Task_Id);
142 -- Set the self id for the current task
144 function Self return Task_Id;
145 pragma Inline (Self);
146 -- Return a pointer to the Ada Task Control Block of the calling task
150 package body Specific is separate;
151 -- The body of this package is target specific
153 ---------------------------------
154 -- Support for foreign threads --
155 ---------------------------------
157 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
158 -- Allocate and Initialize a new ATCB for the current Thread
160 function Register_Foreign_Thread
161 (Thread : Thread_Id) return Task_Id is separate;
163 -----------------------
164 -- Local Subprograms --
165 -----------------------
167 procedure Abort_Handler (Sig : Signal);
168 -- Signal handler used to implement asynchronous abort
170 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority);
171 -- This procedure calls the scheduler of the OS to set thread's priority
177 procedure Abort_Handler (Sig : Signal) is
178 pragma Unreferenced (Sig);
180 T : constant Task_Id := Self;
181 Result : Interfaces.C.int;
182 Old_Set : aliased sigset_t;
185 -- It is not safe to raise an exception when using ZCX and the GCC
186 -- exception handling mechanism.
188 if ZCX_By_Default and then GCC_ZCX_Support then
192 if T.Deferral_Level = 0
193 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
198 -- Make sure signals used for RTS internal purpose are unmasked
201 pthread_sigmask (SIG_UNBLOCK,
202 Unblocked_Signal_Mask'Unchecked_Access,
203 Old_Set'Unchecked_Access);
204 pragma Assert (Result = 0);
206 raise Standard'Abort_Signal;
214 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
215 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
216 Guard_Page_Address : Address;
218 Res : Interfaces.C.int;
221 if Stack_Base_Available then
223 -- Compute the guard page address
225 Guard_Page_Address :=
226 Stack_Base - (Stack_Base mod Get_Page_Size) + Get_Page_Size;
229 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_ON);
231 Res := mprotect (Guard_Page_Address, Get_Page_Size, PROT_OFF);
234 pragma Assert (Res = 0);
242 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
244 return T.Common.LL.Thread;
251 function Self return Task_Id renames Specific.Self;
253 ---------------------
254 -- Initialize_Lock --
255 ---------------------
257 procedure Initialize_Lock
258 (Prio : System.Any_Priority;
261 Attributes : aliased pthread_mutexattr_t;
262 Result : Interfaces.C.int;
265 Result := pthread_mutexattr_init (Attributes'Access);
266 pragma Assert (Result = 0 or else Result = ENOMEM);
268 if Result = ENOMEM then
272 if Locking_Policy = 'C' then
276 Result := pthread_mutex_init (L.Mutex'Access, Attributes'Access);
277 pragma Assert (Result = 0 or else Result = ENOMEM);
279 if Result = ENOMEM then
283 Result := pthread_mutexattr_destroy (Attributes'Access);
284 pragma Assert (Result = 0);
287 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
288 pragma Unreferenced (Level);
290 Attributes : aliased pthread_mutexattr_t;
291 Result : Interfaces.C.int;
294 Result := pthread_mutexattr_init (Attributes'Access);
295 pragma Assert (Result = 0 or else Result = ENOMEM);
297 if Result = ENOMEM then
301 Result := pthread_mutex_init (L, Attributes'Access);
302 pragma Assert (Result = 0 or else Result = ENOMEM);
304 if Result = ENOMEM then
305 Result := pthread_mutexattr_destroy (Attributes'Access);
309 Result := pthread_mutexattr_destroy (Attributes'Access);
310 pragma Assert (Result = 0);
317 procedure Finalize_Lock (L : access Lock) is
318 Result : Interfaces.C.int;
320 Result := pthread_mutex_destroy (L.Mutex'Access);
321 pragma Assert (Result = 0);
324 procedure Finalize_Lock (L : access RTS_Lock) is
325 Result : Interfaces.C.int;
327 Result := pthread_mutex_destroy (L);
328 pragma Assert (Result = 0);
335 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
336 Result : Interfaces.C.int;
337 T : constant Task_Id := Self;
340 if Locking_Policy = 'C' then
341 if T.Common.Current_Priority > L.Ceiling then
342 Ceiling_Violation := True;
346 L.Saved_Priority := T.Common.Current_Priority;
348 if T.Common.Current_Priority < L.Ceiling then
349 Set_OS_Priority (T, L.Ceiling);
353 Result := pthread_mutex_lock (L.Mutex'Access);
355 -- Assume that the cause of EINVAL is a priority ceiling violation
357 Ceiling_Violation := (Result = EINVAL);
358 pragma Assert (Result = 0 or else Result = EINVAL);
361 -- No tricks on RTS_Locks
364 (L : access RTS_Lock; Global_Lock : Boolean := False)
366 Result : Interfaces.C.int;
368 if not Single_Lock or else Global_Lock then
369 Result := pthread_mutex_lock (L);
370 pragma Assert (Result = 0);
374 procedure Write_Lock (T : Task_Id) is
375 Result : Interfaces.C.int;
377 if not Single_Lock then
378 Result := pthread_mutex_lock (T.Common.LL.L'Access);
379 pragma Assert (Result = 0);
387 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
389 Write_Lock (L, Ceiling_Violation);
396 procedure Unlock (L : access Lock) is
397 Result : Interfaces.C.int;
398 T : constant Task_Id := Self;
401 Result := pthread_mutex_unlock (L.Mutex'Access);
402 pragma Assert (Result = 0);
404 if Locking_Policy = 'C' then
405 if T.Common.Current_Priority > L.Saved_Priority then
406 Set_OS_Priority (T, L.Saved_Priority);
411 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
412 Result : Interfaces.C.int;
414 if not Single_Lock or else Global_Lock then
415 Result := pthread_mutex_unlock (L);
416 pragma Assert (Result = 0);
420 procedure Unlock (T : Task_Id) is
421 Result : Interfaces.C.int;
423 if not Single_Lock then
424 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
425 pragma Assert (Result = 0);
435 Reason : System.Tasking.Task_States)
437 pragma Unreferenced (Reason);
438 Result : Interfaces.C.int;
442 Result := pthread_cond_wait
443 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
445 Result := pthread_cond_wait
446 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
449 -- EINTR is not considered a failure
451 pragma Assert (Result = 0 or else Result = EINTR);
458 -- This is for use within the run-time system, so abort is
459 -- assumed to be already deferred, and the caller should be
460 -- holding its own ATCB lock.
462 procedure Timed_Sleep
465 Mode : ST.Delay_Modes;
466 Reason : Task_States;
467 Timedout : out Boolean;
468 Yielded : out Boolean)
470 pragma Unreferenced (Reason);
472 Check_Time : constant Duration := Monotonic_Clock;
475 Request : aliased timespec;
476 Result : Interfaces.C.int;
482 if Mode = Relative then
483 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
485 if Relative_Timed_Wait then
486 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
490 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
492 if Relative_Timed_Wait then
493 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
497 if Abs_Time > Check_Time then
498 if Relative_Timed_Wait then
499 Request := To_Timespec (Rel_Time);
501 Request := To_Timespec (Abs_Time);
505 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
506 or else Self_ID.Pending_Priority_Change;
509 Result := pthread_cond_timedwait
510 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access,
514 Result := pthread_cond_timedwait
515 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access,
519 exit when Abs_Time <= Monotonic_Clock;
521 if Result = 0 or Result = EINTR then
523 -- Somebody may have called Wakeup for us
529 pragma Assert (Result = ETIMEDOUT);
538 -- This is for use in implementing delay statements, so we assume
539 -- the caller is abort-deferred but is holding no locks.
541 procedure Timed_Delay
544 Mode : ST.Delay_Modes)
546 Check_Time : constant Duration := Monotonic_Clock;
549 Request : aliased timespec;
551 Result : Interfaces.C.int;
552 pragma Warnings (Off, Result);
559 -- Comments needed in code below ???
561 Write_Lock (Self_ID);
563 if Mode = Relative then
564 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
566 if Relative_Timed_Wait then
567 Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
571 Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);
573 if Relative_Timed_Wait then
574 Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
578 if Abs_Time > Check_Time then
579 if Relative_Timed_Wait then
580 Request := To_Timespec (Rel_Time);
582 Request := To_Timespec (Abs_Time);
585 Self_ID.Common.State := Delay_Sleep;
588 if Self_ID.Pending_Priority_Change then
589 Self_ID.Pending_Priority_Change := False;
590 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
591 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
594 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
597 Result := pthread_cond_timedwait
598 (Self_ID.Common.LL.CV'Access,
599 Single_RTS_Lock'Access,
602 Result := pthread_cond_timedwait
603 (Self_ID.Common.LL.CV'Access,
604 Self_ID.Common.LL.L'Access,
608 exit when Abs_Time <= Monotonic_Clock;
610 pragma Assert (Result = 0
611 or else Result = ETIMEDOUT
612 or else Result = EINTR);
615 Self_ID.Common.State := Runnable;
624 Result := sched_yield;
627 ---------------------
628 -- Monotonic_Clock --
629 ---------------------
631 function Monotonic_Clock return Duration is
632 TS : aliased timespec;
633 Result : Interfaces.C.int;
635 Result := clock_gettime
636 (clock_id => CLOCK_REALTIME, tp => TS'Unchecked_Access);
637 pragma Assert (Result = 0);
638 return To_Duration (TS);
645 function RT_Resolution return Duration is
646 Res : aliased timespec;
647 Result : Interfaces.C.int;
649 Result := clock_getres
650 (clock_id => CLOCK_REALTIME, Res => Res'Unchecked_Access);
651 pragma Assert (Result = 0);
652 return To_Duration (Res);
659 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
660 pragma Unreferenced (Reason);
661 Result : Interfaces.C.int;
663 Result := pthread_cond_signal (T.Common.LL.CV'Access);
664 pragma Assert (Result = 0);
671 procedure Yield (Do_Yield : Boolean := True) is
672 Result : Interfaces.C.int;
673 pragma Unreferenced (Result);
676 Result := sched_yield;
684 procedure Set_OS_Priority (T : Task_Id; Prio : System.Any_Priority) is
685 Result : Interfaces.C.int;
686 Param : aliased struct_sched_param;
688 function Get_Policy (Prio : System.Any_Priority) return Character;
689 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
690 -- Get priority specific dispatching policy
692 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
693 -- Upper case first character of the policy name corresponding to the
694 -- task as set by a Priority_Specific_Dispatching pragma.
697 Param.sched_priority := Interfaces.C.int (Prio);
699 if Time_Slice_Supported
700 and then (Dispatching_Policy = 'R'
701 or else Priority_Specific_Policy = 'R'
702 or else Time_Slice_Val > 0)
704 Result := pthread_setschedparam
705 (T.Common.LL.Thread, SCHED_RR, Param'Access);
707 elsif Dispatching_Policy = 'F'
708 or else Priority_Specific_Policy = 'F'
709 or else Time_Slice_Val = 0
711 Result := pthread_setschedparam
712 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
715 Result := pthread_setschedparam
716 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
719 pragma Assert (Result = 0);
722 type Prio_Array_Type is array (System.Any_Priority) of Integer;
723 pragma Atomic_Components (Prio_Array_Type);
724 Prio_Array : Prio_Array_Type;
725 -- Comments needed for these declarations ???
727 procedure Set_Priority
729 Prio : System.Any_Priority;
730 Loss_Of_Inheritance : Boolean := False)
732 Array_Item : Integer;
735 Set_OS_Priority (T, Prio);
737 if Locking_Policy = 'C' then
738 -- Annex D requirements: loss of inheritance puts task at the
739 -- beginning of the queue for that prio; copied from 5ztaprop
742 if Loss_Of_Inheritance
743 and then Prio < T.Common.Current_Priority then
745 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
746 Prio_Array (T.Common.Base_Priority) := Array_Item;
750 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
751 or else Prio_Array (T.Common.Base_Priority) = 1;
754 Prio_Array (T.Common.Base_Priority) :=
755 Prio_Array (T.Common.Base_Priority) - 1;
759 T.Common.Current_Priority := Prio;
766 function Get_Priority (T : Task_Id) return System.Any_Priority is
768 return T.Common.Current_Priority;
775 procedure Enter_Task (Self_ID : Task_Id) is
777 Self_ID.Common.LL.Thread := pthread_self;
778 Self_ID.Common.LL.LWP := lwp_self;
780 Specific.Set (Self_ID);
784 for J in Known_Tasks'Range loop
785 if Known_Tasks (J) = null then
786 Known_Tasks (J) := Self_ID;
787 Self_ID.Known_Tasks_Index := J;
799 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
801 return new Ada_Task_Control_Block (Entry_Num);
808 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
810 -----------------------------
811 -- Register_Foreign_Thread --
812 -----------------------------
814 function Register_Foreign_Thread return Task_Id is
816 if Is_Valid_Task then
819 return Register_Foreign_Thread (pthread_self);
821 end Register_Foreign_Thread;
827 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
828 Mutex_Attr : aliased pthread_mutexattr_t;
829 Result : Interfaces.C.int;
830 Cond_Attr : aliased pthread_condattr_t;
833 -- Give the task a unique serial number
835 Self_ID.Serial_Number := Next_Serial_Number;
836 Next_Serial_Number := Next_Serial_Number + 1;
837 pragma Assert (Next_Serial_Number /= 0);
839 if not Single_Lock then
840 Result := pthread_mutexattr_init (Mutex_Attr'Access);
841 pragma Assert (Result = 0 or else Result = ENOMEM);
844 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
846 pragma Assert (Result = 0 or else Result = ENOMEM);
854 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
855 pragma Assert (Result = 0);
858 Result := pthread_condattr_init (Cond_Attr'Access);
859 pragma Assert (Result = 0 or else Result = ENOMEM);
862 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
864 pragma Assert (Result = 0 or else Result = ENOMEM);
870 if not Single_Lock then
871 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
872 pragma Assert (Result = 0);
878 Result := pthread_condattr_destroy (Cond_Attr'Access);
879 pragma Assert (Result = 0);
886 procedure Create_Task
888 Wrapper : System.Address;
889 Stack_Size : System.Parameters.Size_Type;
890 Priority : System.Any_Priority;
891 Succeeded : out Boolean)
893 Attributes : aliased pthread_attr_t;
894 Adjusted_Stack_Size : Interfaces.C.size_t;
895 Result : Interfaces.C.int;
897 use System.Task_Info;
900 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
902 if Stack_Base_Available then
904 -- If Stack Checking is supported then allocate 2 additional pages:
906 -- In the worst case, stack is allocated at something like
907 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
908 -- to be sure the effective stack size is greater than what
911 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Get_Page_Size;
914 Result := pthread_attr_init (Attributes'Access);
915 pragma Assert (Result = 0 or else Result = ENOMEM);
922 Result := pthread_attr_setdetachstate
923 (Attributes'Access, PTHREAD_CREATE_DETACHED);
924 pragma Assert (Result = 0);
926 Result := pthread_attr_setstacksize
927 (Attributes'Access, Adjusted_Stack_Size);
928 pragma Assert (Result = 0);
930 if T.Common.Task_Info /= Default_Scope then
932 -- We are assuming that Scope_Type has the same values than the
933 -- corresponding C macros
935 Result := pthread_attr_setscope
936 (Attributes'Access, Task_Info_Type'Pos (T.Common.Task_Info));
937 pragma Assert (Result = 0);
940 -- Since the initial signal mask of a thread is inherited from the
941 -- creator, and the Environment task has all its signals masked, we
942 -- do not need to manipulate caller's signal mask at this point.
943 -- All tasks in RTS will have All_Tasks_Mask initially.
945 Result := pthread_create
946 (T.Common.LL.Thread'Access,
948 Thread_Body_Access (Wrapper),
950 pragma Assert (Result = 0 or else Result = EAGAIN);
952 Succeeded := Result = 0;
954 Result := pthread_attr_destroy (Attributes'Access);
955 pragma Assert (Result = 0);
957 Set_Priority (T, Priority);
964 procedure Finalize_TCB (T : Task_Id) is
965 Result : Interfaces.C.int;
967 Is_Self : constant Boolean := T = Self;
969 procedure Free is new
970 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
973 if not Single_Lock then
974 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
975 pragma Assert (Result = 0);
978 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
979 pragma Assert (Result = 0);
981 if T.Known_Tasks_Index /= -1 then
982 Known_Tasks (T.Known_Tasks_Index) := null;
988 Result := st_setspecific (ATCB_Key, System.Null_Address);
989 pragma Assert (Result = 0);
998 procedure Exit_Task is
1000 Specific.Set (null);
1007 procedure Abort_Task (T : Task_Id) is
1008 Result : Interfaces.C.int;
1010 Result := pthread_kill (T.Common.LL.Thread,
1011 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1012 pragma Assert (Result = 0);
1019 procedure Initialize (S : in out Suspension_Object) is
1020 Mutex_Attr : aliased pthread_mutexattr_t;
1021 Cond_Attr : aliased pthread_condattr_t;
1022 Result : Interfaces.C.int;
1025 -- Initialize internal state. It is always initialized to False (ARM
1031 -- Initialize internal mutex
1033 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1034 pragma Assert (Result = 0 or else Result = ENOMEM);
1036 if Result = ENOMEM then
1037 raise Storage_Error;
1040 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1041 pragma Assert (Result = 0 or else Result = ENOMEM);
1043 if Result = ENOMEM then
1044 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1045 pragma Assert (Result = 0);
1047 raise Storage_Error;
1050 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
1051 pragma Assert (Result = 0);
1053 -- Initialize internal condition variable
1055 Result := pthread_condattr_init (Cond_Attr'Access);
1056 pragma Assert (Result = 0 or else Result = ENOMEM);
1059 Result := pthread_mutex_destroy (S.L'Access);
1060 pragma Assert (Result = 0);
1062 if Result = ENOMEM then
1063 raise Storage_Error;
1067 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1068 pragma Assert (Result = 0 or else Result = ENOMEM);
1071 Result := pthread_mutex_destroy (S.L'Access);
1072 pragma Assert (Result = 0);
1074 if Result = ENOMEM then
1075 Result := pthread_condattr_destroy (Cond_Attr'Access);
1076 pragma Assert (Result = 0);
1078 raise Storage_Error;
1082 Result := pthread_condattr_destroy (Cond_Attr'Access);
1083 pragma Assert (Result = 0);
1090 procedure Finalize (S : in out Suspension_Object) is
1091 Result : Interfaces.C.int;
1093 -- Destroy internal mutex
1095 Result := pthread_mutex_destroy (S.L'Access);
1096 pragma Assert (Result = 0);
1098 -- Destroy internal condition variable
1100 Result := pthread_cond_destroy (S.CV'Access);
1101 pragma Assert (Result = 0);
1108 function Current_State (S : Suspension_Object) return Boolean is
1110 -- We do not want to use lock on this read operation. State is marked
1111 -- as Atomic so that we ensure that the value retrieved is correct.
1120 procedure Set_False (S : in out Suspension_Object) is
1121 Result : Interfaces.C.int;
1123 SSL.Abort_Defer.all;
1125 Result := pthread_mutex_lock (S.L'Access);
1126 pragma Assert (Result = 0);
1130 Result := pthread_mutex_unlock (S.L'Access);
1131 pragma Assert (Result = 0);
1133 SSL.Abort_Undefer.all;
1140 procedure Set_True (S : in out Suspension_Object) is
1141 Result : Interfaces.C.int;
1143 SSL.Abort_Defer.all;
1145 Result := pthread_mutex_lock (S.L'Access);
1146 pragma Assert (Result = 0);
1148 -- If there is already a task waiting on this suspension object then
1149 -- we resume it, leaving the state of the suspension object to False,
1150 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1151 -- the state to True.
1157 Result := pthread_cond_signal (S.CV'Access);
1158 pragma Assert (Result = 0);
1163 Result := pthread_mutex_unlock (S.L'Access);
1164 pragma Assert (Result = 0);
1166 SSL.Abort_Undefer.all;
1169 ------------------------
1170 -- Suspend_Until_True --
1171 ------------------------
1173 procedure Suspend_Until_True (S : in out Suspension_Object) is
1174 Result : Interfaces.C.int;
1176 SSL.Abort_Defer.all;
1178 Result := pthread_mutex_lock (S.L'Access);
1179 pragma Assert (Result = 0);
1182 -- Program_Error must be raised upon calling Suspend_Until_True
1183 -- if another task is already waiting on that suspension object
1184 -- (ARM D.10 par. 10).
1186 Result := pthread_mutex_unlock (S.L'Access);
1187 pragma Assert (Result = 0);
1189 SSL.Abort_Undefer.all;
1191 raise Program_Error;
1193 -- Suspend the task if the state is False. Otherwise, the task
1194 -- continues its execution, and the state of the suspension object
1195 -- is set to False (ARM D.10 par. 9).
1201 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1204 Result := pthread_mutex_unlock (S.L'Access);
1205 pragma Assert (Result = 0);
1207 SSL.Abort_Undefer.all;
1209 end Suspend_Until_True;
1217 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1218 pragma Unreferenced (Self_ID);
1223 --------------------
1224 -- Check_No_Locks --
1225 --------------------
1227 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1228 pragma Unreferenced (Self_ID);
1233 ----------------------
1234 -- Environment_Task --
1235 ----------------------
1237 function Environment_Task return Task_Id is
1239 return Environment_Task_Id;
1240 end Environment_Task;
1246 procedure Lock_RTS is
1248 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1255 procedure Unlock_RTS is
1257 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1264 function Suspend_Task
1266 Thread_Self : Thread_Id) return Boolean
1268 pragma Unreferenced (T);
1269 pragma Unreferenced (Thread_Self);
1278 function Resume_Task
1280 Thread_Self : Thread_Id) return Boolean
1282 pragma Unreferenced (T);
1283 pragma Unreferenced (Thread_Self);
1292 procedure Initialize (Environment_Task : Task_Id) is
1293 act : aliased struct_sigaction;
1294 old_act : aliased struct_sigaction;
1295 Tmp_Set : aliased sigset_t;
1296 Result : Interfaces.C.int;
1299 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1300 pragma Import (C, State, "__gnat_get_interrupt_state");
1301 -- Get interrupt state. Defined in a-init.c
1302 -- The input argument is the interrupt number,
1303 -- and the result is one of the following:
1305 Default : constant Character := 's';
1306 -- 'n' this interrupt not set by any Interrupt_State pragma
1307 -- 'u' Interrupt_State pragma set state to User
1308 -- 'r' Interrupt_State pragma set state to Runtime
1309 -- 's' Interrupt_State pragma set state to System (use "default"
1313 Environment_Task_Id := Environment_Task;
1315 Interrupt_Management.Initialize;
1317 -- Prepare the set of signals that should unblocked in all tasks
1319 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1320 pragma Assert (Result = 0);
1322 for J in Interrupt_Management.Interrupt_ID loop
1323 if System.Interrupt_Management.Keep_Unmasked (J) then
1324 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1325 pragma Assert (Result = 0);
1329 -- Initialize the lock used to synchronize chain of all ATCBs
1331 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1333 Specific.Initialize (Environment_Task);
1335 Enter_Task (Environment_Task);
1337 -- Install the abort-signal handler
1339 if State (System.Interrupt_Management.Abort_Task_Interrupt)
1343 act.sa_handler := Abort_Handler'Address;
1345 Result := sigemptyset (Tmp_Set'Access);
1346 pragma Assert (Result = 0);
1347 act.sa_mask := Tmp_Set;
1351 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1352 act'Unchecked_Access,
1353 old_act'Unchecked_Access);
1355 pragma Assert (Result = 0);
1359 end System.Task_Primitives.Operations;