[multiple changes]
[gcc.git] / gcc / ada / s-taprop-linux.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA 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-2011, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
33
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
36
37 pragma Polling (Off);
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
40
41 with Ada.Unchecked_Deallocation;
42
43 with Interfaces.C;
44
45 with System.Task_Info;
46 with System.Tasking.Debug;
47 with System.Interrupt_Management;
48 with System.OS_Primitives;
49 with System.Stack_Checking.Operations;
50 with System.Multiprocessors;
51
52 with System.Soft_Links;
53 -- We use System.Soft_Links instead of System.Tasking.Initialization
54 -- because the later is a higher level package that we shouldn't depend on.
55 -- For example when using the restricted run time, it is replaced by
56 -- System.Tasking.Restricted.Stages.
57
58 package body System.Task_Primitives.Operations is
59
60 package SSL renames System.Soft_Links;
61 package SC renames System.Stack_Checking.Operations;
62
63 use System.Tasking.Debug;
64 use System.Tasking;
65 use Interfaces.C;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
69 use System.Task_Info;
70
71 ----------------
72 -- Local Data --
73 ----------------
74
75 -- The followings are logically constants, but need to be initialized
76 -- at run time.
77
78 Single_RTS_Lock : aliased RTS_Lock;
79 -- This is a lock to allow only one thread of control in the RTS at
80 -- a time; it is used to execute in mutual exclusion from all other tasks.
81 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
82
83 Environment_Task_Id : Task_Id;
84 -- A variable to hold Task_Id for the environment task
85
86 Unblocked_Signal_Mask : aliased sigset_t;
87 -- The set of signals that should be unblocked in all tasks
88
89 -- The followings are internal configuration constants needed
90
91 Next_Serial_Number : Task_Serial_Number := 100;
92 -- We start at 100 (reserve some special values for using in error checks)
93
94 Time_Slice_Val : Integer;
95 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
96
97 Dispatching_Policy : Character;
98 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
99
100 -- The following are effectively constants, but they need to be initialized
101 -- by calling a pthread_ function.
102
103 Mutex_Attr : aliased pthread_mutexattr_t;
104 Cond_Attr : aliased pthread_condattr_t;
105
106 Foreign_Task_Elaborated : aliased Boolean := True;
107 -- Used to identified fake tasks (i.e., non-Ada Threads)
108
109 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
110 -- Whether to use an alternate signal stack for stack overflows
111
112 Abort_Handler_Installed : Boolean := False;
113 -- True if a handler for the abort signal is installed
114
115 Null_Thread_Id : constant pthread_t := pthread_t'Last;
116 -- Constant to indicate that the thread identifier has not yet been
117 -- initialized.
118
119 --------------------
120 -- Local Packages --
121 --------------------
122
123 package Specific is
124
125 procedure Initialize (Environment_Task : Task_Id);
126 pragma Inline (Initialize);
127 -- Initialize various data needed by this package
128
129 function Is_Valid_Task return Boolean;
130 pragma Inline (Is_Valid_Task);
131 -- Does executing thread have a TCB?
132
133 procedure Set (Self_Id : Task_Id);
134 pragma Inline (Set);
135 -- Set the self id for the current task
136
137 function Self return Task_Id;
138 pragma Inline (Self);
139 -- Return a pointer to the Ada Task Control Block of the calling task
140
141 end Specific;
142
143 package body Specific is separate;
144 -- The body of this package is target specific
145
146 ---------------------------------
147 -- Support for foreign threads --
148 ---------------------------------
149
150 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
151 -- Allocate and Initialize a new ATCB for the current Thread
152
153 function Register_Foreign_Thread
154 (Thread : Thread_Id) return Task_Id is separate;
155
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
159
160 procedure Abort_Handler (signo : Signal);
161
162 -------------------
163 -- Abort_Handler --
164 -------------------
165
166 procedure Abort_Handler (signo : Signal) is
167 pragma Unreferenced (signo);
168
169 Self_Id : constant Task_Id := Self;
170 Result : Interfaces.C.int;
171 Old_Set : aliased sigset_t;
172
173 begin
174 -- It's not safe to raise an exception when using GCC ZCX mechanism.
175 -- Note that we still need to install a signal handler, since in some
176 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
177 -- need to send the Abort signal to a task.
178
179 if ZCX_By_Default then
180 return;
181 end if;
182
183 if Self_Id.Deferral_Level = 0
184 and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level
185 and then not Self_Id.Aborting
186 then
187 Self_Id.Aborting := True;
188
189 -- Make sure signals used for RTS internal purpose are unmasked
190
191 Result :=
192 pthread_sigmask
193 (SIG_UNBLOCK,
194 Unblocked_Signal_Mask'Access,
195 Old_Set'Access);
196 pragma Assert (Result = 0);
197
198 raise Standard'Abort_Signal;
199 end if;
200 end Abort_Handler;
201
202 --------------
203 -- Lock_RTS --
204 --------------
205
206 procedure Lock_RTS is
207 begin
208 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
209 end Lock_RTS;
210
211 ----------------
212 -- Unlock_RTS --
213 ----------------
214
215 procedure Unlock_RTS is
216 begin
217 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
218 end Unlock_RTS;
219
220 -----------------
221 -- Stack_Guard --
222 -----------------
223
224 -- The underlying thread system extends the memory (up to 2MB) when needed
225
226 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
227 pragma Unreferenced (T);
228 pragma Unreferenced (On);
229 begin
230 null;
231 end Stack_Guard;
232
233 --------------------
234 -- Get_Thread_Id --
235 --------------------
236
237 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
238 begin
239 return T.Common.LL.Thread;
240 end Get_Thread_Id;
241
242 ----------
243 -- Self --
244 ----------
245
246 function Self return Task_Id renames Specific.Self;
247
248 ---------------------
249 -- Initialize_Lock --
250 ---------------------
251
252 -- Note: mutexes and cond_variables needed per-task basis are initialized
253 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
254 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
255 -- status change of RTS. Therefore raising Storage_Error in the following
256 -- routines should be able to be handled safely.
257
258 procedure Initialize_Lock
259 (Prio : System.Any_Priority;
260 L : not null access Lock)
261 is
262 pragma Unreferenced (Prio);
263
264 Result : Interfaces.C.int;
265
266 begin
267 Result := pthread_mutex_init (L, Mutex_Attr'Access);
268
269 pragma Assert (Result = 0 or else Result = ENOMEM);
270
271 if Result = ENOMEM then
272 raise Storage_Error with "Failed to allocate a lock";
273 end if;
274 end Initialize_Lock;
275
276 procedure Initialize_Lock
277 (L : not null access RTS_Lock;
278 Level : Lock_Level)
279 is
280 pragma Unreferenced (Level);
281
282 Result : Interfaces.C.int;
283
284 begin
285 Result := pthread_mutex_init (L, Mutex_Attr'Access);
286
287 pragma Assert (Result = 0 or else Result = ENOMEM);
288
289 if Result = ENOMEM then
290 raise Storage_Error;
291 end if;
292 end Initialize_Lock;
293
294 -------------------
295 -- Finalize_Lock --
296 -------------------
297
298 procedure Finalize_Lock (L : not null access Lock) is
299 Result : Interfaces.C.int;
300 begin
301 Result := pthread_mutex_destroy (L);
302 pragma Assert (Result = 0);
303 end Finalize_Lock;
304
305 procedure Finalize_Lock (L : not null access RTS_Lock) is
306 Result : Interfaces.C.int;
307 begin
308 Result := pthread_mutex_destroy (L);
309 pragma Assert (Result = 0);
310 end Finalize_Lock;
311
312 ----------------
313 -- Write_Lock --
314 ----------------
315
316 procedure Write_Lock
317 (L : not null access Lock;
318 Ceiling_Violation : out Boolean)
319 is
320 Result : Interfaces.C.int;
321 begin
322 Result := pthread_mutex_lock (L);
323 Ceiling_Violation := Result = EINVAL;
324
325 -- Assume the cause of EINVAL is a priority ceiling violation
326
327 pragma Assert (Result = 0 or else Result = EINVAL);
328 end Write_Lock;
329
330 procedure Write_Lock
331 (L : not null access RTS_Lock;
332 Global_Lock : Boolean := False)
333 is
334 Result : Interfaces.C.int;
335 begin
336 if not Single_Lock or else Global_Lock then
337 Result := pthread_mutex_lock (L);
338 pragma Assert (Result = 0);
339 end if;
340 end Write_Lock;
341
342 procedure Write_Lock (T : Task_Id) is
343 Result : Interfaces.C.int;
344 begin
345 if not Single_Lock then
346 Result := pthread_mutex_lock (T.Common.LL.L'Access);
347 pragma Assert (Result = 0);
348 end if;
349 end Write_Lock;
350
351 ---------------
352 -- Read_Lock --
353 ---------------
354
355 procedure Read_Lock
356 (L : not null access Lock;
357 Ceiling_Violation : out Boolean)
358 is
359 begin
360 Write_Lock (L, Ceiling_Violation);
361 end Read_Lock;
362
363 ------------
364 -- Unlock --
365 ------------
366
367 procedure Unlock (L : not null access Lock) is
368 Result : Interfaces.C.int;
369 begin
370 Result := pthread_mutex_unlock (L);
371 pragma Assert (Result = 0);
372 end Unlock;
373
374 procedure Unlock
375 (L : not null access RTS_Lock;
376 Global_Lock : Boolean := False)
377 is
378 Result : Interfaces.C.int;
379 begin
380 if not Single_Lock or else Global_Lock then
381 Result := pthread_mutex_unlock (L);
382 pragma Assert (Result = 0);
383 end if;
384 end Unlock;
385
386 procedure Unlock (T : Task_Id) is
387 Result : Interfaces.C.int;
388 begin
389 if not Single_Lock then
390 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
391 pragma Assert (Result = 0);
392 end if;
393 end Unlock;
394
395 -----------------
396 -- Set_Ceiling --
397 -----------------
398
399 -- Dynamic priority ceilings are not supported by the underlying system
400
401 procedure Set_Ceiling
402 (L : not null access Lock;
403 Prio : System.Any_Priority)
404 is
405 pragma Unreferenced (L, Prio);
406 begin
407 null;
408 end Set_Ceiling;
409
410 -----------
411 -- Sleep --
412 -----------
413
414 procedure Sleep
415 (Self_ID : Task_Id;
416 Reason : System.Tasking.Task_States)
417 is
418 pragma Unreferenced (Reason);
419
420 Result : Interfaces.C.int;
421
422 begin
423 pragma Assert (Self_ID = Self);
424
425 Result :=
426 pthread_cond_wait
427 (cond => Self_ID.Common.LL.CV'Access,
428 mutex => (if Single_Lock
429 then Single_RTS_Lock'Access
430 else Self_ID.Common.LL.L'Access));
431
432 -- EINTR is not considered a failure
433
434 pragma Assert (Result = 0 or else Result = EINTR);
435 end Sleep;
436
437 -----------------
438 -- Timed_Sleep --
439 -----------------
440
441 -- This is for use within the run-time system, so abort is
442 -- assumed to be already deferred, and the caller should be
443 -- holding its own ATCB lock.
444
445 procedure Timed_Sleep
446 (Self_ID : Task_Id;
447 Time : Duration;
448 Mode : ST.Delay_Modes;
449 Reason : System.Tasking.Task_States;
450 Timedout : out Boolean;
451 Yielded : out Boolean)
452 is
453 pragma Unreferenced (Reason);
454
455 Base_Time : constant Duration := Monotonic_Clock;
456 Check_Time : Duration := Base_Time;
457 Abs_Time : Duration;
458 Request : aliased timespec;
459 Result : Interfaces.C.int;
460
461 begin
462 Timedout := True;
463 Yielded := False;
464
465 Abs_Time :=
466 (if Mode = Relative
467 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
468 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
469
470 if Abs_Time > Check_Time then
471 Request := To_Timespec (Abs_Time);
472
473 loop
474 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
475
476 Result :=
477 pthread_cond_timedwait
478 (cond => Self_ID.Common.LL.CV'Access,
479 mutex => (if Single_Lock
480 then Single_RTS_Lock'Access
481 else Self_ID.Common.LL.L'Access),
482 abstime => Request'Access);
483
484 Check_Time := Monotonic_Clock;
485 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
486
487 if Result = 0 or else Result = EINTR then
488
489 -- Somebody may have called Wakeup for us
490
491 Timedout := False;
492 exit;
493 end if;
494
495 pragma Assert (Result = ETIMEDOUT);
496 end loop;
497 end if;
498 end Timed_Sleep;
499
500 -----------------
501 -- Timed_Delay --
502 -----------------
503
504 -- This is for use in implementing delay statements, so we assume the
505 -- caller is abort-deferred but is holding no locks.
506
507 procedure Timed_Delay
508 (Self_ID : Task_Id;
509 Time : Duration;
510 Mode : ST.Delay_Modes)
511 is
512 Base_Time : constant Duration := Monotonic_Clock;
513 Check_Time : Duration := Base_Time;
514 Abs_Time : Duration;
515 Request : aliased timespec;
516
517 Result : Interfaces.C.int;
518 pragma Warnings (Off, Result);
519
520 begin
521 if Single_Lock then
522 Lock_RTS;
523 end if;
524
525 Write_Lock (Self_ID);
526
527 Abs_Time :=
528 (if Mode = Relative
529 then Time + Check_Time
530 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
531
532 if Abs_Time > Check_Time then
533 Request := To_Timespec (Abs_Time);
534 Self_ID.Common.State := Delay_Sleep;
535
536 loop
537 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
538
539 Result :=
540 pthread_cond_timedwait
541 (cond => Self_ID.Common.LL.CV'Access,
542 mutex => (if Single_Lock
543 then Single_RTS_Lock'Access
544 else Self_ID.Common.LL.L'Access),
545 abstime => Request'Access);
546
547 Check_Time := Monotonic_Clock;
548 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
549
550 pragma Assert (Result = 0 or else
551 Result = ETIMEDOUT or else
552 Result = EINTR);
553 end loop;
554
555 Self_ID.Common.State := Runnable;
556 end if;
557
558 Unlock (Self_ID);
559
560 if Single_Lock then
561 Unlock_RTS;
562 end if;
563
564 Result := sched_yield;
565 end Timed_Delay;
566
567 ---------------------
568 -- Monotonic_Clock --
569 ---------------------
570
571 function Monotonic_Clock return Duration is
572 use Interfaces;
573
574 type timeval is array (1 .. 2) of C.long;
575
576 procedure timeval_to_duration
577 (T : not null access timeval;
578 sec : not null access C.long;
579 usec : not null access C.long);
580 pragma Import (C, timeval_to_duration, "__gnat_timeval_to_duration");
581
582 Micro : constant := 10**6;
583 sec : aliased C.long;
584 usec : aliased C.long;
585 TV : aliased timeval;
586 Result : int;
587
588 function gettimeofday
589 (Tv : access timeval;
590 Tz : System.Address := System.Null_Address) return int;
591 pragma Import (C, gettimeofday, "gettimeofday");
592
593 begin
594 Result := gettimeofday (TV'Access, System.Null_Address);
595 pragma Assert (Result = 0);
596 timeval_to_duration (TV'Access, sec'Access, usec'Access);
597 return Duration (sec) + Duration (usec) / Micro;
598 end Monotonic_Clock;
599
600 -------------------
601 -- RT_Resolution --
602 -------------------
603
604 function RT_Resolution return Duration is
605 begin
606 return 10#1.0#E-6;
607 end RT_Resolution;
608
609 ------------
610 -- Wakeup --
611 ------------
612
613 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
614 pragma Unreferenced (Reason);
615 Result : Interfaces.C.int;
616 begin
617 Result := pthread_cond_signal (T.Common.LL.CV'Access);
618 pragma Assert (Result = 0);
619 end Wakeup;
620
621 -----------
622 -- Yield --
623 -----------
624
625 procedure Yield (Do_Yield : Boolean := True) is
626 Result : Interfaces.C.int;
627 pragma Unreferenced (Result);
628 begin
629 if Do_Yield then
630 Result := sched_yield;
631 end if;
632 end Yield;
633
634 ------------------
635 -- Set_Priority --
636 ------------------
637
638 procedure Set_Priority
639 (T : Task_Id;
640 Prio : System.Any_Priority;
641 Loss_Of_Inheritance : Boolean := False)
642 is
643 pragma Unreferenced (Loss_Of_Inheritance);
644
645 Result : Interfaces.C.int;
646 Param : aliased struct_sched_param;
647
648 function Get_Policy (Prio : System.Any_Priority) return Character;
649 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
650 -- Get priority specific dispatching policy
651
652 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
653 -- Upper case first character of the policy name corresponding to the
654 -- task as set by a Priority_Specific_Dispatching pragma.
655
656 begin
657 T.Common.Current_Priority := Prio;
658
659 -- Priorities are 1 .. 99 on GNU/Linux, so we map 0 .. 98 to 1 .. 99
660
661 Param.sched_priority := Interfaces.C.int (Prio) + 1;
662
663 if Dispatching_Policy = 'R'
664 or else Priority_Specific_Policy = 'R'
665 or else Time_Slice_Val > 0
666 then
667 Result :=
668 pthread_setschedparam
669 (T.Common.LL.Thread, SCHED_RR, Param'Access);
670
671 elsif Dispatching_Policy = 'F'
672 or else Priority_Specific_Policy = 'F'
673 or else Time_Slice_Val = 0
674 then
675 Result :=
676 pthread_setschedparam
677 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
678
679 else
680 Param.sched_priority := 0;
681 Result :=
682 pthread_setschedparam
683 (T.Common.LL.Thread,
684 SCHED_OTHER, Param'Access);
685 end if;
686
687 pragma Assert (Result = 0 or else Result = EPERM);
688 end Set_Priority;
689
690 ------------------
691 -- Get_Priority --
692 ------------------
693
694 function Get_Priority (T : Task_Id) return System.Any_Priority is
695 begin
696 return T.Common.Current_Priority;
697 end Get_Priority;
698
699 ----------------
700 -- Enter_Task --
701 ----------------
702
703 procedure Enter_Task (Self_ID : Task_Id) is
704 begin
705 if Self_ID.Common.Task_Info /= null
706 and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU
707 then
708 raise Invalid_CPU_Number;
709 end if;
710
711 Self_ID.Common.LL.Thread := pthread_self;
712 Self_ID.Common.LL.LWP := lwp_self;
713
714 Specific.Set (Self_ID);
715
716 if Use_Alternate_Stack
717 and then Self_ID.Common.Task_Alternate_Stack /= Null_Address
718 then
719 declare
720 Stack : aliased stack_t;
721 Result : Interfaces.C.int;
722 begin
723 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
724 Stack.ss_size := Alternate_Stack_Size;
725 Stack.ss_flags := 0;
726 Result := sigaltstack (Stack'Access, null);
727 pragma Assert (Result = 0);
728 end;
729 end if;
730 end Enter_Task;
731
732 --------------
733 -- New_ATCB --
734 --------------
735
736 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
737 begin
738 return new Ada_Task_Control_Block (Entry_Num);
739 end New_ATCB;
740
741 -------------------
742 -- Is_Valid_Task --
743 -------------------
744
745 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
746
747 -----------------------------
748 -- Register_Foreign_Thread --
749 -----------------------------
750
751 function Register_Foreign_Thread return Task_Id is
752 begin
753 if Is_Valid_Task then
754 return Self;
755 else
756 return Register_Foreign_Thread (pthread_self);
757 end if;
758 end Register_Foreign_Thread;
759
760 --------------------
761 -- Initialize_TCB --
762 --------------------
763
764 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
765 Result : Interfaces.C.int;
766
767 begin
768 -- Give the task a unique serial number
769
770 Self_ID.Serial_Number := Next_Serial_Number;
771 Next_Serial_Number := Next_Serial_Number + 1;
772 pragma Assert (Next_Serial_Number /= 0);
773
774 Self_ID.Common.LL.Thread := Null_Thread_Id;
775
776 if not Single_Lock then
777 Result := pthread_mutex_init (Self_ID.Common.LL.L'Access,
778 Mutex_Attr'Access);
779 pragma Assert (Result = 0 or else Result = ENOMEM);
780
781 if Result /= 0 then
782 Succeeded := False;
783 return;
784 end if;
785 end if;
786
787 Result := pthread_cond_init (Self_ID.Common.LL.CV'Access,
788 Cond_Attr'Access);
789 pragma Assert (Result = 0 or else Result = ENOMEM);
790
791 if Result = 0 then
792 Succeeded := True;
793 else
794 if not Single_Lock then
795 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
796 pragma Assert (Result = 0);
797 end if;
798
799 Succeeded := False;
800 end if;
801 end Initialize_TCB;
802
803 -----------------
804 -- Create_Task --
805 -----------------
806
807 procedure Create_Task
808 (T : Task_Id;
809 Wrapper : System.Address;
810 Stack_Size : System.Parameters.Size_Type;
811 Priority : System.Any_Priority;
812 Succeeded : out Boolean)
813 is
814 Attributes : aliased pthread_attr_t;
815 Adjusted_Stack_Size : Interfaces.C.size_t;
816 Result : Interfaces.C.int;
817
818 use type System.Multiprocessors.CPU_Range;
819
820 begin
821 -- Check whether both Dispatching_Domain and CPU are specified for the
822 -- task, and the CPU value is not contained within the range of
823 -- processors for the domain.
824
825 if T.Common.Domain /= null
826 and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
827 and then
828 (T.Common.Base_CPU not in T.Common.Domain'Range
829 or else not T.Common.Domain (T.Common.Base_CPU))
830 then
831 Succeeded := False;
832 return;
833 end if;
834
835 Adjusted_Stack_Size :=
836 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
837
838 Result := pthread_attr_init (Attributes'Access);
839 pragma Assert (Result = 0 or else Result = ENOMEM);
840
841 if Result /= 0 then
842 Succeeded := False;
843 return;
844 end if;
845
846 Result :=
847 pthread_attr_setstacksize
848 (Attributes'Access, Adjusted_Stack_Size);
849 pragma Assert (Result = 0);
850
851 Result :=
852 pthread_attr_setdetachstate
853 (Attributes'Access, PTHREAD_CREATE_DETACHED);
854 pragma Assert (Result = 0);
855
856 -- Set the required attributes for the creation of the thread
857
858 -- Note: Previously, we called pthread_setaffinity_np (after thread
859 -- creation but before thread activation) to set the affinity but it was
860 -- not behaving as expected. Setting the required attributes for the
861 -- creation of the thread works correctly and it is more appropriate.
862
863 -- Do nothing if required support not provided by the operating system
864
865 if pthread_attr_setaffinity_np'Address = System.Null_Address then
866 null;
867
868 -- Support is available
869
870 elsif T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
871 declare
872 CPU_Set : aliased cpu_set_t;
873
874 begin
875 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
876 System.OS_Interface.CPU_SET
877 (int (T.Common.Base_CPU), CPU_Set'Access);
878 Result :=
879 pthread_attr_setaffinity_np
880 (Attributes'Access,
881 CPU_SETSIZE / 8,
882 CPU_Set'Access);
883 pragma Assert (Result = 0);
884 end;
885
886 -- Handle Task_Info
887
888 elsif T.Common.Task_Info /= null
889 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
890 then
891 Result :=
892 pthread_attr_setaffinity_np
893 (Attributes'Access,
894 CPU_SETSIZE / 8,
895 T.Common.Task_Info.CPU_Affinity'Access);
896 pragma Assert (Result = 0);
897
898 -- Handle dispatching domains
899
900 -- To avoid changing CPU affinities when not needed, we set the
901 -- affinity only when assigning to a domain other than the default
902 -- one, or when the default one has been modified.
903
904 elsif T.Common.Domain /= null and then
905 (T.Common.Domain /= ST.System_Domain
906 or else T.Common.Domain.all /=
907 (Multiprocessors.CPU'First ..
908 Multiprocessors.Number_Of_CPUs => True))
909 then
910 declare
911 CPU_Set : aliased cpu_set_t;
912
913 begin
914 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
915
916 -- Set the affinity to all the processors belonging to the
917 -- dispatching domain.
918
919 for Proc in T.Common.Domain'Range loop
920 if T.Common.Domain (Proc) then
921 System.OS_Interface.CPU_SET (int (Proc), CPU_Set'Access);
922 end if;
923 end loop;
924
925 Result :=
926 pthread_attr_setaffinity_np
927 (Attributes'Access,
928 CPU_SETSIZE / 8,
929 CPU_Set'Access);
930 pragma Assert (Result = 0);
931 end;
932 end if;
933
934 -- Since the initial signal mask of a thread is inherited from the
935 -- creator, and the Environment task has all its signals masked, we
936 -- do not need to manipulate caller's signal mask at this point.
937 -- All tasks in RTS will have All_Tasks_Mask initially.
938
939 Result := pthread_create
940 (T.Common.LL.Thread'Access,
941 Attributes'Access,
942 Thread_Body_Access (Wrapper),
943 To_Address (T));
944 pragma Assert
945 (Result = 0 or else Result = EAGAIN or else Result = ENOMEM);
946
947 if Result /= 0 then
948 Succeeded := False;
949 Result := pthread_attr_destroy (Attributes'Access);
950 pragma Assert (Result = 0);
951 return;
952 end if;
953
954 Succeeded := True;
955
956 Result := pthread_attr_destroy (Attributes'Access);
957 pragma Assert (Result = 0);
958
959 Set_Priority (T, Priority);
960 end Create_Task;
961
962 ------------------
963 -- Finalize_TCB --
964 ------------------
965
966 procedure Finalize_TCB (T : Task_Id) is
967 Result : Interfaces.C.int;
968 Tmp : Task_Id := T;
969 Is_Self : constant Boolean := T = Self;
970
971 procedure Free is new
972 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
973
974 begin
975 if not Single_Lock then
976 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
977 pragma Assert (Result = 0);
978 end if;
979
980 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
981 pragma Assert (Result = 0);
982
983 if T.Known_Tasks_Index /= -1 then
984 Known_Tasks (T.Known_Tasks_Index) := null;
985 end if;
986 SC.Invalidate_Stack_Cache (T.Common.Compiler_Data.Pri_Stack_Info'Access);
987 Free (Tmp);
988
989 if Is_Self then
990 Specific.Set (null);
991 end if;
992 end Finalize_TCB;
993
994 ---------------
995 -- Exit_Task --
996 ---------------
997
998 procedure Exit_Task is
999 begin
1000 Specific.Set (null);
1001 end Exit_Task;
1002
1003 ----------------
1004 -- Abort_Task --
1005 ----------------
1006
1007 procedure Abort_Task (T : Task_Id) is
1008 Result : Interfaces.C.int;
1009 begin
1010 if Abort_Handler_Installed then
1011 Result :=
1012 pthread_kill
1013 (T.Common.LL.Thread,
1014 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1015 pragma Assert (Result = 0);
1016 end if;
1017 end Abort_Task;
1018
1019 ----------------
1020 -- Initialize --
1021 ----------------
1022
1023 procedure Initialize (S : in out Suspension_Object) is
1024 Result : Interfaces.C.int;
1025
1026 begin
1027 -- Initialize internal state (always to False (RM D.10(6)))
1028
1029 S.State := False;
1030 S.Waiting := False;
1031
1032 -- Initialize internal mutex
1033
1034 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
1035
1036 pragma Assert (Result = 0 or else Result = ENOMEM);
1037
1038 if Result = ENOMEM then
1039 raise Storage_Error;
1040 end if;
1041
1042 -- Initialize internal condition variable
1043
1044 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
1045
1046 pragma Assert (Result = 0 or else Result = ENOMEM);
1047
1048 if Result /= 0 then
1049 Result := pthread_mutex_destroy (S.L'Access);
1050 pragma Assert (Result = 0);
1051
1052 if Result = ENOMEM then
1053 raise Storage_Error;
1054 end if;
1055 end if;
1056 end Initialize;
1057
1058 --------------
1059 -- Finalize --
1060 --------------
1061
1062 procedure Finalize (S : in out Suspension_Object) is
1063 Result : Interfaces.C.int;
1064
1065 begin
1066 -- Destroy internal mutex
1067
1068 Result := pthread_mutex_destroy (S.L'Access);
1069 pragma Assert (Result = 0);
1070
1071 -- Destroy internal condition variable
1072
1073 Result := pthread_cond_destroy (S.CV'Access);
1074 pragma Assert (Result = 0);
1075 end Finalize;
1076
1077 -------------------
1078 -- Current_State --
1079 -------------------
1080
1081 function Current_State (S : Suspension_Object) return Boolean is
1082 begin
1083 -- We do not want to use lock on this read operation. State is marked
1084 -- as Atomic so that we ensure that the value retrieved is correct.
1085
1086 return S.State;
1087 end Current_State;
1088
1089 ---------------
1090 -- Set_False --
1091 ---------------
1092
1093 procedure Set_False (S : in out Suspension_Object) is
1094 Result : Interfaces.C.int;
1095
1096 begin
1097 SSL.Abort_Defer.all;
1098
1099 Result := pthread_mutex_lock (S.L'Access);
1100 pragma Assert (Result = 0);
1101
1102 S.State := False;
1103
1104 Result := pthread_mutex_unlock (S.L'Access);
1105 pragma Assert (Result = 0);
1106
1107 SSL.Abort_Undefer.all;
1108 end Set_False;
1109
1110 --------------
1111 -- Set_True --
1112 --------------
1113
1114 procedure Set_True (S : in out Suspension_Object) is
1115 Result : Interfaces.C.int;
1116
1117 begin
1118 SSL.Abort_Defer.all;
1119
1120 Result := pthread_mutex_lock (S.L'Access);
1121 pragma Assert (Result = 0);
1122
1123 -- If there is already a task waiting on this suspension object then
1124 -- we resume it, leaving the state of the suspension object to False,
1125 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1126 -- the state to True.
1127
1128 if S.Waiting then
1129 S.Waiting := False;
1130 S.State := False;
1131
1132 Result := pthread_cond_signal (S.CV'Access);
1133 pragma Assert (Result = 0);
1134
1135 else
1136 S.State := True;
1137 end if;
1138
1139 Result := pthread_mutex_unlock (S.L'Access);
1140 pragma Assert (Result = 0);
1141
1142 SSL.Abort_Undefer.all;
1143 end Set_True;
1144
1145 ------------------------
1146 -- Suspend_Until_True --
1147 ------------------------
1148
1149 procedure Suspend_Until_True (S : in out Suspension_Object) is
1150 Result : Interfaces.C.int;
1151
1152 begin
1153 SSL.Abort_Defer.all;
1154
1155 Result := pthread_mutex_lock (S.L'Access);
1156 pragma Assert (Result = 0);
1157
1158 if S.Waiting then
1159
1160 -- Program_Error must be raised upon calling Suspend_Until_True
1161 -- if another task is already waiting on that suspension object
1162 -- (RM D.10(10)).
1163
1164 Result := pthread_mutex_unlock (S.L'Access);
1165 pragma Assert (Result = 0);
1166
1167 SSL.Abort_Undefer.all;
1168
1169 raise Program_Error;
1170
1171 else
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1175
1176 if S.State then
1177 S.State := False;
1178 else
1179 S.Waiting := True;
1180
1181 loop
1182 -- Loop in case pthread_cond_wait returns earlier than expected
1183 -- (e.g. in case of EINTR caused by a signal). This should not
1184 -- happen with the current Linux implementation of pthread, but
1185 -- POSIX does not guarantee it so this may change in future.
1186
1187 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1188 pragma Assert (Result = 0 or else Result = EINTR);
1189
1190 exit when not S.Waiting;
1191 end loop;
1192 end if;
1193
1194 Result := pthread_mutex_unlock (S.L'Access);
1195 pragma Assert (Result = 0);
1196
1197 SSL.Abort_Undefer.all;
1198 end if;
1199 end Suspend_Until_True;
1200
1201 ----------------
1202 -- Check_Exit --
1203 ----------------
1204
1205 -- Dummy version
1206
1207 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1208 pragma Unreferenced (Self_ID);
1209 begin
1210 return True;
1211 end Check_Exit;
1212
1213 --------------------
1214 -- Check_No_Locks --
1215 --------------------
1216
1217 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1218 pragma Unreferenced (Self_ID);
1219 begin
1220 return True;
1221 end Check_No_Locks;
1222
1223 ----------------------
1224 -- Environment_Task --
1225 ----------------------
1226
1227 function Environment_Task return Task_Id is
1228 begin
1229 return Environment_Task_Id;
1230 end Environment_Task;
1231
1232 ------------------
1233 -- Suspend_Task --
1234 ------------------
1235
1236 function Suspend_Task
1237 (T : ST.Task_Id;
1238 Thread_Self : Thread_Id) return Boolean
1239 is
1240 begin
1241 if T.Common.LL.Thread /= Thread_Self then
1242 return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0;
1243 else
1244 return True;
1245 end if;
1246 end Suspend_Task;
1247
1248 -----------------
1249 -- Resume_Task --
1250 -----------------
1251
1252 function Resume_Task
1253 (T : ST.Task_Id;
1254 Thread_Self : Thread_Id) return Boolean
1255 is
1256 begin
1257 if T.Common.LL.Thread /= Thread_Self then
1258 return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0;
1259 else
1260 return True;
1261 end if;
1262 end Resume_Task;
1263
1264 --------------------
1265 -- Stop_All_Tasks --
1266 --------------------
1267
1268 procedure Stop_All_Tasks is
1269 begin
1270 null;
1271 end Stop_All_Tasks;
1272
1273 ---------------
1274 -- Stop_Task --
1275 ---------------
1276
1277 function Stop_Task (T : ST.Task_Id) return Boolean is
1278 pragma Unreferenced (T);
1279 begin
1280 return False;
1281 end Stop_Task;
1282
1283 -------------------
1284 -- Continue_Task --
1285 -------------------
1286
1287 function Continue_Task (T : ST.Task_Id) return Boolean is
1288 pragma Unreferenced (T);
1289 begin
1290 return False;
1291 end Continue_Task;
1292
1293 ----------------
1294 -- Initialize --
1295 ----------------
1296
1297 procedure Initialize (Environment_Task : Task_Id) is
1298 act : aliased struct_sigaction;
1299 old_act : aliased struct_sigaction;
1300 Tmp_Set : aliased sigset_t;
1301 Result : Interfaces.C.int;
1302 -- Whether to use an alternate signal stack for stack overflows
1303
1304 function State
1305 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1306 pragma Import (C, State, "__gnat_get_interrupt_state");
1307 -- Get interrupt state. Defined in a-init.c
1308 -- The input argument is the interrupt number,
1309 -- and the result is one of the following:
1310
1311 Default : constant Character := 's';
1312 -- 'n' this interrupt not set by any Interrupt_State pragma
1313 -- 'u' Interrupt_State pragma set state to User
1314 -- 'r' Interrupt_State pragma set state to Runtime
1315 -- 's' Interrupt_State pragma set state to System (use "default"
1316 -- system handler)
1317
1318 use type System.Multiprocessors.CPU_Range;
1319
1320 begin
1321 Environment_Task_Id := Environment_Task;
1322
1323 Interrupt_Management.Initialize;
1324
1325 -- Prepare the set of signals that should be unblocked in all tasks
1326
1327 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1328 pragma Assert (Result = 0);
1329
1330 for J in Interrupt_Management.Interrupt_ID loop
1331 if System.Interrupt_Management.Keep_Unmasked (J) then
1332 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1333 pragma Assert (Result = 0);
1334 end if;
1335 end loop;
1336
1337 Result := pthread_mutexattr_init (Mutex_Attr'Access);
1338 pragma Assert (Result = 0);
1339
1340 Result := pthread_condattr_init (Cond_Attr'Access);
1341 pragma Assert (Result = 0);
1342
1343 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1344
1345 -- Initialize the global RTS lock
1346
1347 Specific.Initialize (Environment_Task);
1348
1349 if Use_Alternate_Stack then
1350 Environment_Task.Common.Task_Alternate_Stack :=
1351 Alternate_Stack'Address;
1352 end if;
1353
1354 -- Make environment task known here because it doesn't go through
1355 -- Activate_Tasks, which does it for all other tasks.
1356
1357 Known_Tasks (Known_Tasks'First) := Environment_Task;
1358 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1359
1360 Enter_Task (Environment_Task);
1361
1362 if State
1363 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1364 then
1365 act.sa_flags := 0;
1366 act.sa_handler := Abort_Handler'Address;
1367
1368 Result := sigemptyset (Tmp_Set'Access);
1369 pragma Assert (Result = 0);
1370 act.sa_mask := Tmp_Set;
1371
1372 Result :=
1373 sigaction
1374 (Signal (Interrupt_Management.Abort_Task_Interrupt),
1375 act'Unchecked_Access,
1376 old_act'Unchecked_Access);
1377 pragma Assert (Result = 0);
1378 Abort_Handler_Installed := True;
1379 end if;
1380
1381 -- pragma CPU and dispatching domains for the environment task
1382
1383 Set_Task_Affinity (Environment_Task);
1384 end Initialize;
1385
1386 -----------------------
1387 -- Set_Task_Affinity --
1388 -----------------------
1389
1390 procedure Set_Task_Affinity (T : ST.Task_Id) is
1391 use type System.Multiprocessors.CPU_Range;
1392
1393 begin
1394 -- Do nothing if there is no support for setting affinities or the
1395 -- underlying thread has not yet been created. If the thread has not
1396 -- yet been created then the proper affinity will be set during its
1397 -- creation.
1398
1399 if pthread_setaffinity_np'Address /= System.Null_Address
1400 and then T.Common.LL.Thread /= Null_Thread_Id
1401 then
1402 declare
1403 type cpu_set_t_ptr is access all cpu_set_t;
1404 CPU_Set : aliased cpu_set_t;
1405 CPU_Set_Ptr : cpu_set_t_ptr := null;
1406
1407 Result : Interfaces.C.int;
1408
1409 begin
1410 -- We look at the specific CPU (Base_CPU) first, then at the
1411 -- Task_Info field, and finally at the assigned dispatching
1412 -- domain, if any.
1413
1414 if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1415
1416 -- Set the affinity to an unique CPU
1417 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
1418 System.OS_Interface.CPU_SET
1419 (int (T.Common.Base_CPU), CPU_Set'Access);
1420 CPU_Set_Ptr := CPU_Set'Access;
1421
1422 -- Handle Task_Info
1423
1424 elsif T.Common.Task_Info /= null
1425 and then T.Common.Task_Info.CPU_Affinity /= Task_Info.Any_CPU
1426 then
1427 CPU_Set_Ptr := T.Common.Task_Info.CPU_Affinity'Access;
1428
1429 -- Handle dispatching domains
1430
1431 elsif T.Common.Domain /= null and then
1432 (T.Common.Domain /= ST.System_Domain
1433 or else T.Common.Domain.all /=
1434 (Multiprocessors.CPU'First ..
1435 Multiprocessors.Number_Of_CPUs => True))
1436 then
1437 -- Set the affinity to all the processors belonging to the
1438 -- dispatching domain. To avoid changing CPU affinities when
1439 -- not needed, we set the affinity only when assigning to a
1440 -- domain other than the default one, or when the default one
1441 -- has been modified.
1442
1443 System.OS_Interface.CPU_ZERO (CPU_Set'Access);
1444
1445 for Proc in T.Common.Domain'Range loop
1446 System.OS_Interface.CPU_SET (int (Proc), CPU_Set'Access);
1447 end loop;
1448
1449 CPU_Set_Ptr := CPU_Set'Access;
1450 end if;
1451
1452 -- We set the new affinity if needed. Otherwise, the new task
1453 -- will inherit its creator's CPU affinity mask (according to
1454 -- the documentation of pthread_setaffinity_np), which is
1455 -- consistent with Ada's required semantics.
1456
1457 if CPU_Set_Ptr /= null then
1458 Result :=
1459 pthread_setaffinity_np
1460 (T.Common.LL.Thread, CPU_SETSIZE / 8, CPU_Set_Ptr);
1461 pragma Assert (Result = 0);
1462 end if;
1463 end;
1464 end if;
1465 end Set_Task_Affinity;
1466
1467 end System.Task_Primitives.Operations;