objc/
[official-gcc.git] / gcc / ada / s-taprop-vms.adb
bloba627d7c07ff474ad1ec58a6890c4f864346c76ad
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
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. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
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.
39 pragma Polling (Off);
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 Interfaces.C;
47 -- used for int
48 -- size_t
50 with System.Parameters;
51 -- used for Size_Type
53 with System.Tasking;
54 -- used for Ada_Task_Control_Block
55 -- Task_Id
57 with System.Soft_Links;
58 -- used for Defer/Undefer_Abort
59 -- Set_Exc_Stack_Addr
61 -- Note that we do not use System.Tasking.Initialization directly since
62 -- this is a higher level package that we shouldn't depend on. For example
63 -- when using the restricted run time, it is replaced by
64 -- System.Tasking.Restricted.Stages.
66 with System.OS_Primitives;
67 -- used for Delay_Modes
69 with Unchecked_Conversion;
70 with Unchecked_Deallocation;
72 package body System.Task_Primitives.Operations is
74 use System.Tasking.Debug;
75 use System.Tasking;
76 use Interfaces.C;
77 use System.OS_Interface;
78 use System.Parameters;
79 use System.OS_Primitives;
80 use type System.OS_Primitives.OS_Time;
82 package SSL renames System.Soft_Links;
84 ----------------
85 -- Local Data --
86 ----------------
88 -- The followings are logically constants, but need to be initialized
89 -- at run time.
91 Single_RTS_Lock : aliased RTS_Lock;
92 -- This is a lock to allow only one thread of control in the RTS at
93 -- a time; it is used to execute in mutual exclusion from all other tasks.
94 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
96 ATCB_Key : aliased pthread_key_t;
97 -- Key used to find the Ada Task_Id associated with a thread
99 Environment_Task_Id : Task_Id;
100 -- A variable to hold Task_Id for the environment task.
102 Time_Slice_Val : Integer;
103 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
105 Dispatching_Policy : Character;
106 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
108 FIFO_Within_Priorities : constant Boolean := Dispatching_Policy = 'F';
109 -- Indicates whether FIFO_Within_Priorities is set.
111 Foreign_Task_Elaborated : aliased Boolean := True;
112 -- Used to identified fake tasks (i.e., non-Ada Threads).
114 --------------------
115 -- Local Packages --
116 --------------------
118 package Specific is
120 procedure Initialize (Environment_Task : Task_Id);
121 pragma Inline (Initialize);
122 -- Initialize various data needed by this package.
124 function Is_Valid_Task return Boolean;
125 pragma Inline (Is_Valid_Task);
126 -- Does executing thread have a TCB?
128 procedure Set (Self_Id : Task_Id);
129 pragma Inline (Set);
130 -- Set the self id for the current task
132 function Self return Task_Id;
133 pragma Inline (Self);
134 -- Return a pointer to the Ada Task Control Block of the calling task
136 end Specific;
138 package body Specific is separate;
139 -- The body of this package is target specific.
141 ---------------------------------
142 -- Support for foreign threads --
143 ---------------------------------
145 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
146 -- Allocate and Initialize a new ATCB for the current Thread
148 function Register_Foreign_Thread
149 (Thread : Thread_Id) return Task_Id is separate;
151 -----------------------
152 -- Local Subprograms --
153 -----------------------
155 function To_Task_Id is new Unchecked_Conversion (System.Address, Task_Id);
157 function To_Address is new Unchecked_Conversion (Task_Id, System.Address);
159 procedure Timer_Sleep_AST (ID : Address);
160 -- Signal the condition variable when AST fires.
162 procedure Timer_Sleep_AST (ID : Address) is
163 Result : Interfaces.C.int;
164 Self_ID : constant Task_Id := To_Task_Id (ID);
165 begin
166 Self_ID.Common.LL.AST_Pending := False;
167 Result := pthread_cond_signal_int_np (Self_ID.Common.LL.CV'Access);
168 pragma Assert (Result = 0);
169 end Timer_Sleep_AST;
171 -----------------
172 -- Stack_Guard --
173 -----------------
175 -- The underlying thread system sets a guard page at the
176 -- bottom of a thread stack, so nothing is needed.
177 -- ??? Check the comment above
179 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
180 pragma Unreferenced (T);
181 pragma Unreferenced (On);
182 begin
183 null;
184 end Stack_Guard;
186 --------------------
187 -- Get_Thread_Id --
188 --------------------
190 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
191 begin
192 return T.Common.LL.Thread;
193 end Get_Thread_Id;
195 ----------
196 -- Self --
197 ----------
199 function Self return Task_Id renames Specific.Self;
201 ---------------------
202 -- Initialize_Lock --
203 ---------------------
205 -- Note: mutexes and cond_variables needed per-task basis are
206 -- initialized in Initialize_TCB and the Storage_Error is
207 -- handled. Other mutexes (such as RTS_Lock, Memory_Lock...)
208 -- used in RTS is initialized before any status change of RTS.
209 -- Therefore rasing Storage_Error in the following routines
210 -- should be able to be handled safely.
212 procedure Initialize_Lock (Prio : System.Any_Priority; L : access Lock) is
213 Attributes : aliased pthread_mutexattr_t;
214 Result : Interfaces.C.int;
216 begin
217 Result := pthread_mutexattr_init (Attributes'Access);
218 pragma Assert (Result = 0 or else Result = ENOMEM);
220 if Result = ENOMEM then
221 raise Storage_Error;
222 end if;
224 L.Prio_Save := 0;
225 L.Prio := Interfaces.C.int (Prio);
227 Result := pthread_mutex_init (L.L'Access, Attributes'Access);
228 pragma Assert (Result = 0 or else Result = ENOMEM);
230 if Result = ENOMEM then
231 raise Storage_Error;
232 end if;
234 Result := pthread_mutexattr_destroy (Attributes'Access);
235 pragma Assert (Result = 0);
236 end Initialize_Lock;
238 procedure Initialize_Lock (L : access RTS_Lock; Level : Lock_Level) is
239 pragma Unreferenced (Level);
241 Attributes : aliased pthread_mutexattr_t;
242 Result : Interfaces.C.int;
244 begin
245 Result := pthread_mutexattr_init (Attributes'Access);
246 pragma Assert (Result = 0 or else Result = ENOMEM);
248 if Result = ENOMEM then
249 raise Storage_Error;
250 end if;
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
270 raise Storage_Error;
271 end if;
273 Result := pthread_mutexattr_destroy (Attributes'Access);
274 pragma Assert (Result = 0);
275 end Initialize_Lock;
277 -------------------
278 -- Finalize_Lock --
279 -------------------
281 procedure Finalize_Lock (L : access Lock) is
282 Result : Interfaces.C.int;
283 begin
284 Result := pthread_mutex_destroy (L.L'Access);
285 pragma Assert (Result = 0);
286 end Finalize_Lock;
288 procedure Finalize_Lock (L : access RTS_Lock) is
289 Result : Interfaces.C.int;
290 begin
291 Result := pthread_mutex_destroy (L);
292 pragma Assert (Result = 0);
293 end Finalize_Lock;
295 ----------------
296 -- Write_Lock --
297 ----------------
299 procedure Write_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
300 Self_ID : constant Task_Id := Self;
301 All_Tasks_Link : constant Task_Id := Self.Common.All_Tasks_Link;
302 Current_Prio : System.Any_Priority;
303 Result : Interfaces.C.int;
305 begin
306 Current_Prio := Get_Priority (Self_ID);
308 -- If there is no other tasks, no need to check priorities
310 if All_Tasks_Link /= Null_Task
311 and then L.Prio < Interfaces.C.int (Current_Prio)
312 then
313 Ceiling_Violation := True;
314 return;
315 end if;
317 Result := pthread_mutex_lock (L.L'Access);
318 pragma Assert (Result = 0);
320 Ceiling_Violation := False;
321 -- Why is this commented out ???
322 -- L.Prio_Save := Interfaces.C.int (Current_Prio);
323 -- Set_Priority (Self_ID, System.Any_Priority (L.Prio));
324 end Write_Lock;
326 procedure Write_Lock
327 (L : access RTS_Lock;
328 Global_Lock : Boolean := False)
330 Result : Interfaces.C.int;
331 begin
332 if not Single_Lock or else Global_Lock then
333 Result := pthread_mutex_lock (L);
334 pragma Assert (Result = 0);
335 end if;
336 end Write_Lock;
338 procedure Write_Lock (T : Task_Id) is
339 Result : Interfaces.C.int;
340 begin
341 if not Single_Lock then
342 Result := pthread_mutex_lock (T.Common.LL.L'Access);
343 pragma Assert (Result = 0);
344 end if;
345 end Write_Lock;
347 ---------------
348 -- Read_Lock --
349 ---------------
351 procedure Read_Lock (L : access Lock; Ceiling_Violation : out Boolean) is
352 begin
353 Write_Lock (L, Ceiling_Violation);
354 end Read_Lock;
356 ------------
357 -- Unlock --
358 ------------
360 procedure Unlock (L : access Lock) is
361 Result : Interfaces.C.int;
362 begin
363 Result := pthread_mutex_unlock (L.L'Access);
364 pragma Assert (Result = 0);
365 end Unlock;
367 procedure Unlock (L : access RTS_Lock; Global_Lock : Boolean := False) is
368 Result : Interfaces.C.int;
369 begin
370 if not Single_Lock or else Global_Lock then
371 Result := pthread_mutex_unlock (L);
372 pragma Assert (Result = 0);
373 end if;
374 end Unlock;
376 procedure Unlock (T : Task_Id) is
377 Result : Interfaces.C.int;
378 begin
379 if not Single_Lock then
380 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
381 pragma Assert (Result = 0);
382 end if;
383 end Unlock;
385 -----------
386 -- Sleep --
387 -----------
389 procedure Sleep
390 (Self_ID : Task_Id;
391 Reason : System.Tasking.Task_States)
393 pragma Unreferenced (Reason);
394 Result : Interfaces.C.int;
396 begin
397 if Single_Lock then
398 Result := pthread_cond_wait
399 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
400 else
401 Result := pthread_cond_wait
402 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
403 end if;
405 -- EINTR is not considered a failure
407 pragma Assert (Result = 0 or else Result = EINTR);
409 if Self_ID.Deferral_Level = 0
410 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
411 then
412 Unlock (Self_ID);
413 raise Standard'Abort_Signal;
414 end if;
415 end Sleep;
417 -----------------
418 -- Timed_Sleep --
419 -----------------
421 procedure Timed_Sleep
422 (Self_ID : Task_Id;
423 Time : Duration;
424 Mode : ST.Delay_Modes;
425 Reason : System.Tasking.Task_States;
426 Timedout : out Boolean;
427 Yielded : out Boolean)
429 pragma Unreferenced (Reason);
431 Sleep_Time : OS_Time;
432 Result : Interfaces.C.int;
433 Status : Cond_Value_Type;
435 -- The body below requires more comments ???
437 begin
438 Timedout := False;
439 Yielded := False;
441 Sleep_Time := To_OS_Time (Time, Mode);
443 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
444 or else Self_ID.Pending_Priority_Change
445 then
446 return;
447 end if;
449 Self_ID.Common.LL.AST_Pending := True;
451 Sys_Setimr
452 (Status, 0, Sleep_Time,
453 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
455 if (Status and 1) /= 1 then
456 raise Storage_Error;
457 end if;
459 if Single_Lock then
460 Result := pthread_cond_wait
461 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
462 pragma Assert (Result = 0);
464 else
465 Result := pthread_cond_wait
466 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
467 pragma Assert (Result = 0);
468 end if;
470 Yielded := True;
472 if not Self_ID.Common.LL.AST_Pending then
473 Timedout := True;
474 else
475 Sys_Cantim (Status, To_Address (Self_ID), 0);
476 pragma Assert ((Status and 1) = 1);
477 end if;
478 end Timed_Sleep;
480 -----------------
481 -- Timed_Delay --
482 -----------------
484 procedure Timed_Delay
485 (Self_ID : Task_Id;
486 Time : Duration;
487 Mode : ST.Delay_Modes)
489 Sleep_Time : OS_Time;
490 Result : Interfaces.C.int;
491 Status : Cond_Value_Type;
492 Yielded : Boolean := False;
494 begin
495 -- Only the little window between deferring abort and
496 -- locking Self_ID is the reason we need to
497 -- check for pending abort and priority change below!
499 if Single_Lock then
500 Lock_RTS;
501 end if;
503 -- More comments required in body below ???
505 SSL.Abort_Defer.all;
506 Write_Lock (Self_ID);
508 if Time /= 0.0 or else Mode /= Relative then
509 Sleep_Time := To_OS_Time (Time, Mode);
511 if Mode = Relative or else OS_Clock < Sleep_Time then
512 Self_ID.Common.State := Delay_Sleep;
513 Self_ID.Common.LL.AST_Pending := True;
515 Sys_Setimr
516 (Status, 0, Sleep_Time,
517 Timer_Sleep_AST'Access, To_Address (Self_ID), 0);
519 if (Status and 1) /= 1 then
520 raise Storage_Error;
521 end if;
523 loop
524 if Self_ID.Pending_Priority_Change then
525 Self_ID.Pending_Priority_Change := False;
526 Self_ID.Common.Base_Priority := Self_ID.New_Base_Priority;
527 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
528 end if;
530 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then
531 Sys_Cantim (Status, To_Address (Self_ID), 0);
532 pragma Assert ((Status and 1) = 1);
533 exit;
534 end if;
536 if Single_Lock then
537 Result := pthread_cond_wait
538 (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
539 pragma Assert (Result = 0);
540 else
541 Result := pthread_cond_wait
542 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
543 pragma Assert (Result = 0);
544 end if;
546 Yielded := True;
548 exit when not Self_ID.Common.LL.AST_Pending;
549 end loop;
551 Self_ID.Common.State := Runnable;
552 end if;
553 end if;
555 Unlock (Self_ID);
557 if Single_Lock then
558 Unlock_RTS;
559 end if;
561 if not Yielded then
562 Result := sched_yield;
563 pragma Assert (Result = 0);
564 end if;
566 SSL.Abort_Undefer.all;
567 end Timed_Delay;
569 ---------------------
570 -- Monotonic_Clock --
571 ---------------------
573 function Monotonic_Clock return Duration
574 renames System.OS_Primitives.Monotonic_Clock;
576 -------------------
577 -- RT_Resolution --
578 -------------------
580 function RT_Resolution return Duration is
581 begin
582 return 10#1.0#E-3;
583 end RT_Resolution;
585 ------------
586 -- Wakeup --
587 ------------
589 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
590 pragma Unreferenced (Reason);
591 Result : Interfaces.C.int;
592 begin
593 Result := pthread_cond_signal (T.Common.LL.CV'Access);
594 pragma Assert (Result = 0);
595 end Wakeup;
597 -----------
598 -- Yield --
599 -----------
601 procedure Yield (Do_Yield : Boolean := True) is
602 Result : Interfaces.C.int;
603 pragma Unreferenced (Result);
604 begin
605 if Do_Yield then
606 Result := sched_yield;
607 end if;
608 end Yield;
610 ------------------
611 -- Set_Priority --
612 ------------------
614 procedure Set_Priority
615 (T : Task_Id;
616 Prio : System.Any_Priority;
617 Loss_Of_Inheritance : Boolean := False)
619 pragma Unreferenced (Loss_Of_Inheritance);
621 Result : Interfaces.C.int;
622 Param : aliased struct_sched_param;
624 begin
625 T.Common.Current_Priority := Prio;
626 Param.sched_priority := Interfaces.C.int (Underlying_Priorities (Prio));
628 if Time_Slice_Val > 0 then
629 Result := pthread_setschedparam
630 (T.Common.LL.Thread, SCHED_RR, Param'Access);
632 elsif FIFO_Within_Priorities or else Time_Slice_Val = 0 then
633 Result := pthread_setschedparam
634 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
636 else
637 -- SCHED_OTHER priorities are restricted to the range 8 - 15.
638 -- Since the translation from Underlying priorities results
639 -- in a range of 16 - 31, dividing by 2 gives the correct result.
641 Param.sched_priority := Param.sched_priority / 2;
642 Result := pthread_setschedparam
643 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
644 end if;
646 pragma Assert (Result = 0);
647 end Set_Priority;
649 ------------------
650 -- Get_Priority --
651 ------------------
653 function Get_Priority (T : Task_Id) return System.Any_Priority is
654 begin
655 return T.Common.Current_Priority;
656 end Get_Priority;
658 ----------------
659 -- Enter_Task --
660 ----------------
662 procedure Enter_Task (Self_ID : Task_Id) is
663 begin
664 Self_ID.Common.LL.Thread := pthread_self;
666 Specific.Set (Self_ID);
668 Lock_RTS;
670 for J in Known_Tasks'Range loop
671 if Known_Tasks (J) = null then
672 Known_Tasks (J) := Self_ID;
673 Self_ID.Known_Tasks_Index := J;
674 exit;
675 end if;
676 end loop;
678 Unlock_RTS;
679 end Enter_Task;
681 --------------
682 -- New_ATCB --
683 --------------
685 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
686 begin
687 return new Ada_Task_Control_Block (Entry_Num);
688 end New_ATCB;
690 -------------------
691 -- Is_Valid_Task --
692 -------------------
694 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
696 -----------------------------
697 -- Register_Foreign_Thread --
698 -----------------------------
700 function Register_Foreign_Thread return Task_Id is
701 begin
702 if Is_Valid_Task then
703 return Self;
704 else
705 return Register_Foreign_Thread (pthread_self);
706 end if;
707 end Register_Foreign_Thread;
709 --------------------
710 -- Initialize_TCB --
711 --------------------
713 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
714 Mutex_Attr : aliased pthread_mutexattr_t;
715 Result : Interfaces.C.int;
716 Cond_Attr : aliased pthread_condattr_t;
718 begin
719 -- More comments required in body below ???
721 if not Single_Lock then
722 Result := pthread_mutexattr_init (Mutex_Attr'Access);
723 pragma Assert (Result = 0 or else Result = ENOMEM);
725 if Result = 0 then
726 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
727 Mutex_Attr'Access);
728 pragma Assert (Result = 0 or else Result = ENOMEM);
729 end if;
731 if Result /= 0 then
732 Succeeded := False;
733 return;
734 end if;
736 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
737 pragma Assert (Result = 0);
738 end if;
740 Result := pthread_condattr_init (Cond_Attr'Access);
741 pragma Assert (Result = 0 or else Result = ENOMEM);
743 if Result = 0 then
744 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
745 Cond_Attr'Access);
746 pragma Assert (Result = 0 or else Result = ENOMEM);
747 end if;
749 if Result = 0 then
750 Succeeded := True;
751 Self_ID.Common.LL.Exc_Stack_Ptr := new Exc_Stack_T;
752 SSL.Set_Exc_Stack_Addr
753 (To_Address (Self_ID),
754 Self_ID.Common.LL.Exc_Stack_Ptr (Exc_Stack_T'Last)'Address);
756 else
757 if not Single_Lock then
758 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
759 pragma Assert (Result = 0);
760 end if;
762 Succeeded := False;
763 end if;
765 Result := pthread_condattr_destroy (Cond_Attr'Access);
766 pragma Assert (Result = 0);
767 end Initialize_TCB;
769 -----------------
770 -- Create_Task --
771 -----------------
773 procedure Create_Task
774 (T : Task_Id;
775 Wrapper : System.Address;
776 Stack_Size : System.Parameters.Size_Type;
777 Priority : System.Any_Priority;
778 Succeeded : out Boolean)
780 Attributes : aliased pthread_attr_t;
781 Adjusted_Stack_Size : Interfaces.C.size_t;
782 Result : Interfaces.C.int;
784 function Thread_Body_Access is new
785 Unchecked_Conversion (System.Address, Thread_Body);
787 begin
788 if Stack_Size = Unspecified_Size then
789 Adjusted_Stack_Size := Interfaces.C.size_t (Default_Stack_Size);
791 elsif Stack_Size < Minimum_Stack_Size then
792 Adjusted_Stack_Size := Interfaces.C.size_t (Minimum_Stack_Size);
794 else
795 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size);
796 end if;
798 -- Since the initial signal mask of a thread is inherited from the
799 -- creator, we need to set our local signal mask mask all signals
800 -- during the creation operation, to make sure the new thread is
801 -- not disturbed by signals before it has set its own Task_Id.
803 Result := pthread_attr_init (Attributes'Access);
804 pragma Assert (Result = 0 or else Result = ENOMEM);
806 if Result /= 0 then
807 Succeeded := False;
808 return;
809 end if;
811 Result := pthread_attr_setdetachstate
812 (Attributes'Access, PTHREAD_CREATE_DETACHED);
813 pragma Assert (Result = 0);
815 Result := pthread_attr_setstacksize
816 (Attributes'Access, Adjusted_Stack_Size);
817 pragma Assert (Result = 0);
819 -- This call may be unnecessary, not sure. ???
821 Result :=
822 pthread_attr_setinheritsched
823 (Attributes'Access, PTHREAD_EXPLICIT_SCHED);
824 pragma Assert (Result = 0);
826 Result := pthread_create
827 (T.Common.LL.Thread'Access,
828 Attributes'Access,
829 Thread_Body_Access (Wrapper),
830 To_Address (T));
832 -- ENOMEM is a valid run-time error. Don't shut down.
834 pragma Assert (Result = 0
835 or else Result = EAGAIN or else Result = ENOMEM);
837 Succeeded := Result = 0;
839 Result := pthread_attr_destroy (Attributes'Access);
840 pragma Assert (Result = 0);
842 if Succeeded then
843 Set_Priority (T, Priority);
844 end if;
845 end Create_Task;
847 ------------------
848 -- Finalize_TCB --
849 ------------------
851 procedure Finalize_TCB (T : Task_Id) is
852 Result : Interfaces.C.int;
853 Tmp : Task_Id := T;
854 Is_Self : constant Boolean := T = Self;
856 procedure Free is new
857 Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
859 procedure Free is new Unchecked_Deallocation
860 (Exc_Stack_T, Exc_Stack_Ptr_T);
862 begin
863 if not Single_Lock then
864 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
865 pragma Assert (Result = 0);
866 end if;
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;
873 end if;
875 Free (T.Common.LL.Exc_Stack_Ptr);
877 Free (Tmp);
879 if Is_Self then
880 Specific.Set (null);
881 end if;
882 end Finalize_TCB;
884 ---------------
885 -- Exit_Task --
886 ---------------
888 procedure Exit_Task is
889 begin
890 null;
891 end Exit_Task;
893 ----------------
894 -- Abort_Task --
895 ----------------
897 procedure Abort_Task (T : Task_Id) is
898 begin
899 -- Interrupt Server_Tasks may be waiting on an event flag
901 if T.Common.State = Interrupt_Server_Blocked_On_Event_Flag then
902 Wakeup (T, Interrupt_Server_Blocked_On_Event_Flag);
903 end if;
904 end Abort_Task;
906 ----------------
907 -- Initialize --
908 ----------------
910 procedure Initialize (S : in out Suspension_Object) is
911 Mutex_Attr : aliased pthread_mutexattr_t;
912 Cond_Attr : aliased pthread_condattr_t;
913 Result : Interfaces.C.int;
914 begin
915 -- Initialize internal state. It is always initialized to False (ARM
916 -- D.10 par. 6).
918 S.State := False;
919 S.Waiting := False;
921 -- Initialize internal mutex
923 Result := pthread_mutexattr_init (Mutex_Attr'Access);
924 pragma Assert (Result = 0 or else Result = ENOMEM);
926 if Result = ENOMEM then
927 raise Storage_Error;
928 end if;
930 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
931 pragma Assert (Result = 0 or else Result = ENOMEM);
933 if Result = ENOMEM then
934 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
935 pragma Assert (Result = 0);
937 raise Storage_Error;
938 end if;
940 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
941 pragma Assert (Result = 0);
943 -- Initialize internal condition variable
945 Result := pthread_condattr_init (Cond_Attr'Access);
946 pragma Assert (Result = 0 or else Result = ENOMEM);
948 if Result /= 0 then
949 Result := pthread_mutex_destroy (S.L'Access);
950 pragma Assert (Result = 0);
952 if Result = ENOMEM then
953 raise Storage_Error;
954 end if;
955 end if;
957 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
958 pragma Assert (Result = 0 or else Result = ENOMEM);
960 if Result /= 0 then
961 Result := pthread_mutex_destroy (S.L'Access);
962 pragma Assert (Result = 0);
964 if Result = ENOMEM then
965 Result := pthread_condattr_destroy (Cond_Attr'Access);
966 pragma Assert (Result = 0);
968 raise Storage_Error;
969 end if;
970 end if;
972 Result := pthread_condattr_destroy (Cond_Attr'Access);
973 pragma Assert (Result = 0);
974 end Initialize;
976 --------------
977 -- Finalize --
978 --------------
980 procedure Finalize (S : in out Suspension_Object) is
981 Result : Interfaces.C.int;
982 begin
983 -- Destroy internal mutex
985 Result := pthread_mutex_destroy (S.L'Access);
986 pragma Assert (Result = 0);
988 -- Destroy internal condition variable
990 Result := pthread_cond_destroy (S.CV'Access);
991 pragma Assert (Result = 0);
992 end Finalize;
994 -------------------
995 -- Current_State --
996 -------------------
998 function Current_State (S : Suspension_Object) return Boolean is
999 begin
1000 -- We do not want to use lock on this read operation. State is marked
1001 -- as Atomic so that we ensure that the value retrieved is correct.
1003 return S.State;
1004 end Current_State;
1006 ---------------
1007 -- Set_False --
1008 ---------------
1010 procedure Set_False (S : in out Suspension_Object) is
1011 Result : Interfaces.C.int;
1012 begin
1013 Result := pthread_mutex_lock (S.L'Access);
1014 pragma Assert (Result = 0);
1016 S.State := False;
1018 Result := pthread_mutex_unlock (S.L'Access);
1019 pragma Assert (Result = 0);
1020 end Set_False;
1022 --------------
1023 -- Set_True --
1024 --------------
1026 procedure Set_True (S : in out Suspension_Object) is
1027 Result : Interfaces.C.int;
1028 begin
1029 Result := pthread_mutex_lock (S.L'Access);
1030 pragma Assert (Result = 0);
1032 -- If there is already a task waiting on this suspension object then
1033 -- we resume it, leaving the state of the suspension object to False,
1034 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1035 -- the state to True.
1037 if S.Waiting then
1038 S.Waiting := False;
1039 S.State := False;
1041 Result := pthread_cond_signal (S.CV'Access);
1042 pragma Assert (Result = 0);
1043 else
1044 S.State := True;
1045 end if;
1047 Result := pthread_mutex_unlock (S.L'Access);
1048 pragma Assert (Result = 0);
1049 end Set_True;
1051 ------------------------
1052 -- Suspend_Until_True --
1053 ------------------------
1055 procedure Suspend_Until_True (S : in out Suspension_Object) is
1056 Result : Interfaces.C.int;
1057 begin
1058 Result := pthread_mutex_lock (S.L'Access);
1059 pragma Assert (Result = 0);
1061 if S.Waiting then
1062 -- Program_Error must be raised upon calling Suspend_Until_True
1063 -- if another task is already waiting on that suspension object
1064 -- (ARM D.10 par. 10).
1066 Result := pthread_mutex_unlock (S.L'Access);
1067 pragma Assert (Result = 0);
1069 raise Program_Error;
1070 else
1071 -- Suspend the task if the state is False. Otherwise, the task
1072 -- continues its execution, and the state of the suspension object
1073 -- is set to False (ARM D.10 par. 9).
1075 if S.State then
1076 S.State := False;
1077 else
1078 S.Waiting := True;
1079 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1080 end if;
1081 end if;
1083 Result := pthread_mutex_unlock (S.L'Access);
1084 pragma Assert (Result = 0);
1085 end Suspend_Until_True;
1087 ----------------
1088 -- Check_Exit --
1089 ----------------
1091 -- Dummy version
1093 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1094 pragma Unreferenced (Self_ID);
1095 begin
1096 return True;
1097 end Check_Exit;
1099 --------------------
1100 -- Check_No_Locks --
1101 --------------------
1103 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1104 pragma Unreferenced (Self_ID);
1105 begin
1106 return True;
1107 end Check_No_Locks;
1109 ----------------------
1110 -- Environment_Task --
1111 ----------------------
1113 function Environment_Task return Task_Id is
1114 begin
1115 return Environment_Task_Id;
1116 end Environment_Task;
1118 --------------
1119 -- Lock_RTS --
1120 --------------
1122 procedure Lock_RTS is
1123 begin
1124 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1125 end Lock_RTS;
1127 ----------------
1128 -- Unlock_RTS --
1129 ----------------
1131 procedure Unlock_RTS is
1132 begin
1133 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1134 end Unlock_RTS;
1136 ------------------
1137 -- Suspend_Task --
1138 ------------------
1140 function Suspend_Task
1141 (T : ST.Task_Id;
1142 Thread_Self : Thread_Id) return Boolean
1144 pragma Unreferenced (T);
1145 pragma Unreferenced (Thread_Self);
1146 begin
1147 return False;
1148 end Suspend_Task;
1150 -----------------
1151 -- Resume_Task --
1152 -----------------
1154 function Resume_Task
1155 (T : ST.Task_Id;
1156 Thread_Self : Thread_Id) return Boolean
1158 pragma Unreferenced (T);
1159 pragma Unreferenced (Thread_Self);
1160 begin
1161 return False;
1162 end Resume_Task;
1164 ----------------
1165 -- Initialize --
1166 ----------------
1168 procedure Initialize (Environment_Task : Task_Id) is
1169 begin
1170 Environment_Task_Id := Environment_Task;
1172 -- Initialize the lock used to synchronize chain of all ATCBs
1174 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1176 Specific.Initialize (Environment_Task);
1178 Enter_Task (Environment_Task);
1179 end Initialize;
1181 end System.Task_Primitives.Operations;