Debian Codegraph @

gcc/ada/libgnarl/s-taprop__rtems.adb

raw
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-2025, 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 the RTEMS version of this package
33
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
36
37 with Ada.Unchecked_Conversion;
38
39 with Interfaces.C;
40
41 with System.Interrupt_Management;
42 with System.OS_Constants;
43 with System.OS_Primitives;
44 with System.Task_Info;
45 with System.Tasking.Debug;
46
47 with System.Soft_Links;
48 -- We use System.Soft_Links instead of System.Tasking.Initialization
49 -- because the later is a higher level package that we shouldn't depend on.
50 -- For example when using the restricted run time, it is replaced by
51 -- System.Tasking.Restricted.Stages.
52
53 package body System.Task_Primitives.Operations is
54
55 package OSC renames System.OS_Constants;
56 package SSL renames System.Soft_Links;
57
58 use Interfaces.C;
59
60 use System.OS_Interface;
61 use System.OS_Locks;
62 use System.OS_Primitives;
63 use System.Parameters;
64 use System.Tasking;
65 use System.Tasking.Debug;
66
67 ----------------
68 -- Local Data --
69 ----------------
70
71 -- The followings are logically constants, but need to be initialized
72 -- at run time.
73
74 Single_RTS_Lock : aliased RTS_Lock;
75 -- This is a lock to allow only one thread of control in the RTS at
76 -- a time; it is used to execute in mutual exclusion from all other tasks.
77 -- Used to protect All_Tasks_List
78
79 Environment_Task_Id : Task_Id;
80 -- A variable to hold Task_Id for the environment task
81
82 Locking_Policy : constant Character;
83 pragma Import (C, Locking_Policy, "__gl_locking_policy");
84 -- Value of the pragma Locking_Policy:
85 -- 'C' for Ceiling_Locking
86 -- 'I' for Inherit_Locking
87 -- ' ' for none.
88
89 -- The followings are internal configuration constants needed
90
91 Next_Serial_Number : Task_Serial_Number := 100;
92 -- We start at 100, to reserve some special values for
93 -- using in error checking.
94
95 Time_Slice_Val : constant Integer;
96 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
97
98 Dispatching_Policy : constant Character;
99 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
100
101 Foreign_Task_Elaborated : aliased Boolean := True;
102 -- Used to identified fake tasks (i.e., non-Ada Threads)
103
104 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
105 -- Whether to use an alternate signal stack for stack overflows
106
107 Abort_Handler_Installed : Boolean := False;
108 -- True if a handler for the abort signal is installed
109
110 --------------------
111 -- Local Packages --
112 --------------------
113
114 package Specific is
115
116 procedure Initialize (Environment_Task : Task_Id);
117 pragma Inline (Initialize);
118 -- Initialize various data needed by this package
119
120 function Is_Valid_Task return Boolean;
121 pragma Inline (Is_Valid_Task);
122 -- Does executing thread have a TCB?
123
124 procedure Set (Self_Id : Task_Id);
125 pragma Inline (Set);
126 -- Set the self id for the current task
127
128 function Self return Task_Id;
129 pragma Inline (Self);
130 -- Return a pointer to the Ada Task Control Block of the calling task
131
132 end Specific;
133
134 package body Specific is separate;
135 -- The body of this package is target specific
136
137 package Monotonic is
138
139 function Monotonic_Clock return Duration;
140 pragma Inline (Monotonic_Clock);
141 -- Returns an absolute time, represented as an offset relative to some
142 -- unspecified starting point, typically system boot time. This clock
143 -- is not affected by discontinuous jumps in the system time.
144
145 function RT_Resolution return Duration;
146 pragma Inline (RT_Resolution);
147 -- Returns resolution of the underlying clock used to implement RT_Clock
148
149 procedure Timed_Sleep
150 (Self_ID : ST.Task_Id;
151 Time : Duration;
152 Mode : ST.Delay_Modes;
153 Reason : System.Tasking.Task_States;
154 Timedout : out Boolean;
155 Yielded : out Boolean);
156 -- Combination of Sleep (above) and Timed_Delay
157
158 procedure Timed_Delay
159 (Self_ID : ST.Task_Id;
160 Time : Duration;
161 Mode : ST.Delay_Modes);
162 -- Implement the semantics of the delay statement.
163 -- The caller should be abort-deferred and should not hold any locks.
164
165 end Monotonic;
166
167 package body Monotonic is separate;
168
169 ----------------------------------
170 -- ATCB allocation/deallocation --
171 ----------------------------------
172
173 package body ATCB_Allocation is separate;
174 -- The body of this package is shared across several targets
175
176 ---------------------------------
177 -- Support for foreign threads --
178 ---------------------------------
179
180 function Register_Foreign_Thread
181 (Thread : Thread_Id;
182 Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id;
183 -- Allocate and initialize a new ATCB for the current Thread. The size of
184 -- the secondary stack can be optionally specified.
185
186 function Register_Foreign_Thread
187 (Thread : Thread_Id;
188 Sec_Stack_Size : Size_Type := Unspecified_Size)
189 return Task_Id is separate;
190
191 -----------------------
192 -- Local Subprograms --
193 -----------------------
194
195 procedure Abort_Handler (Sig : Signal);
196 -- Signal handler used to implement asynchronous abort.
197 -- See also comment before body, below.
198
199 function To_Address is
200 new Ada.Unchecked_Conversion (Task_Id, System.Address);
201
202 function GNAT_pthread_condattr_setup
203 (attr : access pthread_condattr_t) return Interfaces.C.int;
204 pragma Import (C,
205 GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup");
206
207 procedure Initialize_Lock
208 (L : not null access RTS_Lock;
209 Prio : System.Any_Priority);
210 -- Initialize an RTS_Lock with the specified priority
211
212 -------------------
213 -- Abort_Handler --
214 -------------------
215
216 -- Target-dependent binding of inter-thread Abort signal to the raising of
217 -- the Abort_Signal exception.
218
219 -- The technical issues and alternatives here are essentially the
220 -- same as for raising exceptions in response to other signals
221 -- (e.g. Storage_Error). See code and comments in the package body
222 -- System.Interrupt_Management.
223
224 -- Some implementations may not allow an exception to be propagated out of
225 -- a handler, and others might leave the signal or interrupt that invoked
226 -- this handler masked after the exceptional return to the application
227 -- code.
228
229 -- GNAT exceptions are originally implemented using setjmp()/longjmp(). On
230 -- most UNIX systems, this will allow transfer out of a signal handler,
231 -- which is usually the only mechanism available for implementing
232 -- asynchronous handlers of this kind. However, some systems do not
233 -- restore the signal mask on longjmp(), leaving the abort signal masked.
234
235 procedure Abort_Handler (Sig : Signal) is
236 pragma Unreferenced (Sig);
237
238 T : constant Task_Id := Self;
239 Old_Set : aliased sigset_t;
240 Unblocked_Mask : aliased sigset_t;
241 Result : Interfaces.C.int;
242 pragma Warnings (Off, Result);
243
244 begin
245 -- It's not safe to raise an exception when using GCC ZCX mechanism.
246 -- Note that we still need to install a signal handler, since in some
247 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
248 -- need to send the Abort signal to a task.
249
250 if ZCX_By_Default then
251 return;
252 end if;
253
254 if T.Deferral_Level = 0
255 and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then
256 not T.Aborting
257 then
258 T.Aborting := True;
259
260 -- Make sure signals used for RTS internal purpose are unmasked
261
262 Result := sigemptyset (Unblocked_Mask'Access);
263 pragma Assert (Result = 0);
264 Result :=
265 sigaddset
266 (Unblocked_Mask'Access,
267 Signal (Interrupt_Management.Abort_Task_Interrupt));
268 pragma Assert (Result = 0);
269 Result := sigaddset (Unblocked_Mask'Access, SIGBUS);
270 pragma Assert (Result = 0);
271 Result := sigaddset (Unblocked_Mask'Access, SIGFPE);
272 pragma Assert (Result = 0);
273 Result := sigaddset (Unblocked_Mask'Access, SIGILL);
274 pragma Assert (Result = 0);
275 Result := sigaddset (Unblocked_Mask'Access, SIGSEGV);
276 pragma Assert (Result = 0);
277
278 Result :=
279 pthread_sigmask
280 (SIG_UNBLOCK,
281 Unblocked_Mask'Access,
282 Old_Set'Access);
283 pragma Assert (Result = 0);
284
285 raise Standard'Abort_Signal;
286 end if;
287 end Abort_Handler;
288
289 -----------------
290 -- Stack_Guard --
291 -----------------
292
293 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
294 Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread);
295 Page_Size : Address;
296 Res : Interfaces.C.int;
297
298 begin
299 if Stack_Base_Available then
300
301 -- Compute the guard page address
302
303 Page_Size := Address (Get_Page_Size);
304 Res :=
305 mprotect
306 (Stack_Base - (Stack_Base mod Page_Size) + Page_Size,
307 Interfaces.C.size_t (Page_Size),
308 prot => (if On then PROT_ON else PROT_OFF));
309 pragma Assert (Res = 0);
310 end if;
311 end Stack_Guard;
312
313 --------------------
314 -- Get_Thread_Id --
315 --------------------
316
317 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
318 begin
319 return T.Common.LL.Thread;
320 end Get_Thread_Id;
321
322 ----------
323 -- Self --
324 ----------
325
326 function Self return Task_Id renames Specific.Self;
327
328 ---------------------
329 -- Initialize_Lock --
330 ---------------------
331
332 -- Note: mutexes and cond_variables needed per-task basis are initialized
333 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
334 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
335 -- status change of RTS. Therefore raising Storage_Error in the following
336 -- routines should be able to be handled safely.
337
338 procedure Initialize_Lock
339 (L : not null access RTS_Lock;
340 Prio : System.Any_Priority)
341 is
342 Attributes : aliased pthread_mutexattr_t;
343 Result : Interfaces.C.int;
344
345 begin
346 Result := pthread_mutexattr_init (Attributes'Access);
347 pragma Assert (Result = 0 or else Result = ENOMEM);
348
349 if Result = ENOMEM then
350 raise Storage_Error;
351 end if;
352
353 if Locking_Policy = 'C' then
354 Result := pthread_mutexattr_setprotocol
355 (Attributes'Access, PTHREAD_PRIO_PROTECT);
356 pragma Assert (Result = 0);
357
358 Result := pthread_mutexattr_setprioceiling
359 (Attributes'Access, Interfaces.C.int (Prio));
360 pragma Assert (Result = 0);
361
362 elsif Locking_Policy = 'I' then
363 Result := pthread_mutexattr_setprotocol
364 (Attributes'Access, PTHREAD_PRIO_INHERIT);
365 pragma Assert (Result = 0);
366 end if;
367
368 Result := pthread_mutex_init (L, Attributes'Access);
369 pragma Assert (Result = 0 or else Result = ENOMEM);
370
371 if Result = ENOMEM then
372 Result := pthread_mutexattr_destroy (Attributes'Access);
373 raise Storage_Error;
374 end if;
375
376 Result := pthread_mutexattr_destroy (Attributes'Access);
377 pragma Assert (Result = 0);
378 end Initialize_Lock;
379
380 procedure Initialize_Lock
381 (Prio : System.Any_Priority;
382 L : not null access Lock)
383 is
384 begin
385 Initialize_Lock (L.WO'Access, Prio);
386 end Initialize_Lock;
387
388 procedure Initialize_Lock
389 (L : not null access RTS_Lock;
390 Level : Lock_Level)
391 is
392 pragma Unreferenced (Level);
393 begin
394 Initialize_Lock (L, System.Any_Priority'Last);
395 end Initialize_Lock;
396
397 -------------------
398 -- Finalize_Lock --
399 -------------------
400
401 procedure Finalize_Lock (L : not null access Lock) is
402 Result : Interfaces.C.int;
403 begin
404 Result := pthread_mutex_destroy (L.WO'Access);
405 pragma Assert (Result = 0);
406 end Finalize_Lock;
407
408 procedure Finalize_Lock (L : not null access RTS_Lock) is
409 Result : Interfaces.C.int;
410 begin
411 Result := pthread_mutex_destroy (L);
412 pragma Assert (Result = 0);
413 end Finalize_Lock;
414
415 ----------------
416 -- Write_Lock --
417 ----------------
418
419 procedure Write_Lock
420 (L : not null access Lock; Ceiling_Violation : out Boolean)
421 is
422 Result : Interfaces.C.int;
423
424 begin
425 Result := pthread_mutex_lock (L.WO'Access);
426
427 -- The cause of EINVAL is a priority ceiling violation
428
429 Ceiling_Violation := Result = EINVAL;
430 pragma Assert (Result = 0 or else Ceiling_Violation);
431 end Write_Lock;
432
433 procedure Write_Lock (L : not null access RTS_Lock) is
434 Result : Interfaces.C.int;
435 begin
436 Result := pthread_mutex_lock (L);
437 pragma Assert (Result = 0);
438 end Write_Lock;
439
440 procedure Write_Lock (T : Task_Id) is
441 Result : Interfaces.C.int;
442 begin
443 Result := pthread_mutex_lock (T.Common.LL.L'Access);
444 pragma Assert (Result = 0);
445 end Write_Lock;
446
447 ---------------
448 -- Read_Lock --
449 ---------------
450
451 procedure Read_Lock
452 (L : not null access Lock; Ceiling_Violation : out Boolean) is
453 begin
454 Write_Lock (L, Ceiling_Violation);
455 end Read_Lock;
456
457 ------------
458 -- Unlock --
459 ------------
460
461 procedure Unlock (L : not null access Lock) is
462 Result : Interfaces.C.int;
463 begin
464 Result := pthread_mutex_unlock (L.WO'Access);
465 pragma Assert (Result = 0);
466 end Unlock;
467
468 procedure Unlock (L : not null access RTS_Lock) is
469 Result : Interfaces.C.int;
470 begin
471 Result := pthread_mutex_unlock (L);
472 pragma Assert (Result = 0);
473 end Unlock;
474
475 procedure Unlock (T : Task_Id) is
476 Result : Interfaces.C.int;
477 begin
478 Result := pthread_mutex_unlock (T.Common.LL.L'Access);
479 pragma Assert (Result = 0);
480 end Unlock;
481
482 -----------------
483 -- Set_Ceiling --
484 -----------------
485
486 -- Dynamic priority ceilings are not supported by the underlying system
487
488 procedure Set_Ceiling
489 (L : not null access Lock;
490 Prio : System.Any_Priority)
491 is
492 pragma Unreferenced (L, Prio);
493 begin
494 null;
495 end Set_Ceiling;
496
497 -----------
498 -- Sleep --
499 -----------
500
501 procedure Sleep
502 (Self_ID : Task_Id;
503 Reason : System.Tasking.Task_States)
504 is
505 pragma Unreferenced (Reason);
506
507 Result : Interfaces.C.int;
508
509 begin
510 Result :=
511 pthread_cond_wait
512 (cond => Self_ID.Common.LL.CV'Access,
513 mutex => Self_ID.Common.LL.L'Access);
514
515 -- EINTR is not considered a failure
516
517 pragma Assert (Result = 0 or else Result = EINTR);
518 end Sleep;
519
520 -----------------
521 -- Timed_Sleep --
522 -----------------
523
524 -- This is for use within the run-time system, so abort is
525 -- assumed to be already deferred, and the caller should be
526 -- holding its own ATCB lock.
527
528 procedure Timed_Sleep
529 (Self_ID : Task_Id;
530 Time : Duration;
531 Mode : ST.Delay_Modes;
532 Reason : Task_States;
533 Timedout : out Boolean;
534 Yielded : out Boolean) renames Monotonic.Timed_Sleep;
535
536 -----------------
537 -- Timed_Delay --
538 -----------------
539
540 -- This is for use in implementing delay statements, so we assume the
541 -- caller is abort-deferred but is holding no locks.
542
543 procedure Timed_Delay
544 (Self_ID : Task_Id;
545 Time : Duration;
546 Mode : ST.Delay_Modes) renames Monotonic.Timed_Delay;
547
548 ---------------------
549 -- Monotonic_Clock --
550 ---------------------
551
552 function Monotonic_Clock return Duration renames Monotonic.Monotonic_Clock;
553
554 -------------------
555 -- RT_Resolution --
556 -------------------
557
558 function RT_Resolution return Duration renames Monotonic.RT_Resolution;
559
560 ------------
561 -- Wakeup --
562 ------------
563
564 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
565 pragma Unreferenced (Reason);
566 Result : Interfaces.C.int;
567 begin
568 Result := pthread_cond_signal (T.Common.LL.CV'Access);
569 pragma Assert (Result = 0);
570 end Wakeup;
571
572 -----------
573 -- Yield --
574 -----------
575
576 procedure Yield (Do_Yield : Boolean := True) is
577 Result : Interfaces.C.int;
578 pragma Unreferenced (Result);
579 begin
580 if Do_Yield then
581 Result := sched_yield;
582 end if;
583 end Yield;
584
585 ------------------
586 -- Set_Priority --
587 ------------------
588
589 procedure Set_Priority
590 (T : Task_Id;
591 Prio : System.Any_Priority;
592 Loss_Of_Inheritance : Boolean := False)
593 is
594 pragma Unreferenced (Loss_Of_Inheritance);
595
596 Result : Interfaces.C.int;
597 Param : aliased struct_sched_param;
598
599 function Get_Policy (Prio : System.Any_Priority) return Character;
600 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
601 -- Get priority specific dispatching policy
602
603 Priority_Specific_Policy : constant Character := Get_Policy (Prio);
604 -- Upper case first character of the policy name corresponding to the
605 -- task as set by a Priority_Specific_Dispatching pragma.
606
607 begin
608 T.Common.Current_Priority := Prio;
609 Param.sched_priority := To_Target_Priority (Prio);
610
611 if Time_Slice_Supported
612 and then (Dispatching_Policy = 'R'
613 or else Priority_Specific_Policy = 'R'
614 or else Time_Slice_Val > 0)
615 then
616 Result := pthread_setschedparam
617 (T.Common.LL.Thread, SCHED_RR, Param'Access);
618
619 elsif Dispatching_Policy = 'F'
620 or else Priority_Specific_Policy = 'F'
621 or else Time_Slice_Val = 0
622 then
623 Result := pthread_setschedparam
624 (T.Common.LL.Thread, SCHED_FIFO, Param'Access);
625
626 else
627 Result := pthread_setschedparam
628 (T.Common.LL.Thread, SCHED_OTHER, Param'Access);
629 end if;
630
631 pragma Assert (Result = 0);
632 end Set_Priority;
633
634 ------------------
635 -- Get_Priority --
636 ------------------
637
638 function Get_Priority (T : Task_Id) return System.Any_Priority is
639 begin
640 return T.Common.Current_Priority;
641 end Get_Priority;
642
643 ----------------
644 -- Enter_Task --
645 ----------------
646
647 procedure Enter_Task (Self_ID : Task_Id) is
648 begin
649 Self_ID.Common.LL.LWP := lwp_self;
650
651 Specific.Set (Self_ID);
652
653 if Use_Alternate_Stack then
654 declare
655 Stack : aliased stack_t;
656 Result : Interfaces.C.int;
657 begin
658 Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack;
659 Stack.ss_size := Alternate_Stack_Size;
660 Stack.ss_flags := 0;
661 Result := sigaltstack (Stack'Access, null);
662 pragma Assert (Result = 0);
663 end;
664 end if;
665 end Enter_Task;
666
667 -------------------
668 -- Is_Valid_Task --
669 -------------------
670
671 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
672
673 -----------------------------
674 -- Register_Foreign_Thread --
675 -----------------------------
676
677 function Register_Foreign_Thread return Task_Id is
678 begin
679 if Is_Valid_Task then
680 return Self;
681 else
682 return Register_Foreign_Thread (pthread_self);
683 end if;
684 end Register_Foreign_Thread;
685
686 --------------------
687 -- Initialize_TCB --
688 --------------------
689
690 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
691 Mutex_Attr : aliased pthread_mutexattr_t;
692 Result : Interfaces.C.int;
693 Cond_Attr : aliased pthread_condattr_t;
694
695 begin
696 -- Give the task a unique serial number
697
698 Self_ID.Serial_Number := Next_Serial_Number;
699 Next_Serial_Number := Next_Serial_Number + 1;
700 pragma Assert (Next_Serial_Number /= 0);
701
702 Result := pthread_mutexattr_init (Mutex_Attr'Access);
703 pragma Assert (Result = 0 or else Result = ENOMEM);
704
705 if Result = 0 then
706 if Locking_Policy = 'C' then
707 Result :=
708 pthread_mutexattr_setprotocol
709 (Mutex_Attr'Access,
710 PTHREAD_PRIO_PROTECT);
711 pragma Assert (Result = 0);
712
713 Result :=
714 pthread_mutexattr_setprioceiling
715 (Mutex_Attr'Access,
716 Interfaces.C.int (System.Any_Priority'Last));
717 pragma Assert (Result = 0);
718
719 elsif Locking_Policy = 'I' then
720 Result :=
721 pthread_mutexattr_setprotocol
722 (Mutex_Attr'Access,
723 PTHREAD_PRIO_INHERIT);
724 pragma Assert (Result = 0);
725 end if;
726
727 Result :=
728 pthread_mutex_init
729 (Self_ID.Common.LL.L'Access,
730 Mutex_Attr'Access);
731 pragma Assert (Result = 0 or else Result = ENOMEM);
732 end if;
733
734 if Result /= 0 then
735 Succeeded := False;
736 return;
737 end if;
738
739 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
740 pragma Assert (Result = 0);
741
742 Result := pthread_condattr_init (Cond_Attr'Access);
743 pragma Assert (Result = 0 or else Result = ENOMEM);
744
745 if Result = 0 then
746 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
747 pragma Assert (Result = 0);
748
749 Result :=
750 pthread_cond_init
751 (Self_ID.Common.LL.CV'Access, Cond_Attr'Access);
752 pragma Assert (Result = 0 or else Result = ENOMEM);
753 end if;
754
755 if Result = 0 then
756 Succeeded := True;
757 else
758 Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access);
759 pragma Assert (Result = 0);
760 Succeeded := False;
761 end if;
762
763 Result := pthread_condattr_destroy (Cond_Attr'Access);
764 pragma Assert (Result = 0);
765 end Initialize_TCB;
766
767 -----------------
768 -- Create_Task --
769 -----------------
770
771 procedure Create_Task
772 (T : Task_Id;
773 Wrapper : System.Address;
774 Stack_Size : System.Parameters.Size_Type;
775 Priority : System.Any_Priority;
776 Succeeded : out Boolean)
777 is
778 Attributes : aliased pthread_attr_t;
779 Adjusted_Stack_Size : Interfaces.C.size_t;
780 Page_Size : constant Interfaces.C.size_t :=
781 Interfaces.C.size_t (Get_Page_Size);
782 Result : Interfaces.C.int;
783
784 function Thread_Body_Access is new
785 Ada.Unchecked_Conversion (System.Address, Thread_Body);
786
787 use System.Task_Info;
788
789 begin
790 Adjusted_Stack_Size :=
791 Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size);
792
793 if Stack_Base_Available then
794
795 -- If Stack Checking is supported then allocate 2 additional pages:
796
797 -- In the worst case, stack is allocated at something like
798 -- N * Get_Page_Size - epsilon, we need to add the size for 2 pages
799 -- to be sure the effective stack size is greater than what
800 -- has been asked.
801
802 Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size;
803 end if;
804
805 -- Round stack size as this is required by some OSes (Darwin)
806
807 Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1;
808 Adjusted_Stack_Size :=
809 Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size;
810
811 Result := pthread_attr_init (Attributes'Access);
812 pragma Assert (Result = 0 or else Result = ENOMEM);
813
814 if Result /= 0 then
815 Succeeded := False;
816 return;
817 end if;
818
819 Result :=
820 pthread_attr_setdetachstate
821 (Attributes'Access, PTHREAD_CREATE_DETACHED);
822 pragma Assert (Result = 0);
823
824 Result :=
825 pthread_attr_setstacksize
826 (Attributes'Access, Adjusted_Stack_Size);
827 pragma Assert (Result = 0);
828
829 if T.Common.Task_Info /= Default_Scope then
830 case T.Common.Task_Info is
831 when System.Task_Info.Process_Scope =>
832 Result :=
833 pthread_attr_setscope
834 (Attributes'Access, PTHREAD_SCOPE_PROCESS);
835
836 when System.Task_Info.System_Scope =>
837 Result :=
838 pthread_attr_setscope
839 (Attributes'Access, PTHREAD_SCOPE_SYSTEM);
840
841 when System.Task_Info.Default_Scope =>
842 Result := 0;
843 end case;
844
845 pragma Assert (Result = 0);
846 end if;
847
848 -- Since the initial signal mask of a thread is inherited from the
849 -- creator, and the Environment task has all its signals masked, we
850 -- do not need to manipulate caller's signal mask at this point.
851 -- All tasks in RTS will have All_Tasks_Mask initially.
852
853 -- The write to T.Common.LL.Thread is not racy with regard to the
854 -- created thread because the created thread will not access it until
855 -- we release the RTS lock (or the current task's lock when
856 -- Restricted.Stages is used). One can verify that by inspecting the
857 -- Task_Wrapper procedures.
858
859 Result := pthread_create
860 (T.Common.LL.Thread'Access,
861 Attributes'Access,
862 Thread_Body_Access (Wrapper),
863 To_Address (T));
864 pragma Assert (Result = 0 or else Result = EAGAIN);
865
866 Succeeded := Result = 0;
867
868 Result := pthread_attr_destroy (Attributes'Access);
869 pragma Assert (Result = 0);
870
871 if Succeeded then
872 Set_Priority (T, Priority);
873 end if;
874 end Create_Task;
875
876 ------------------
877 -- Finalize_TCB --
878 ------------------
879
880 procedure Finalize_TCB (T : Task_Id) is
881 Result : Interfaces.C.int;
882
883 begin
884 Result := pthread_mutex_destroy (T.Common.LL.L'Access);
885 pragma Assert (Result = 0);
886
887 Result := pthread_cond_destroy (T.Common.LL.CV'Access);
888 pragma Assert (Result = 0);
889
890 if T.Known_Tasks_Index /= -1 then
891 Known_Tasks (T.Known_Tasks_Index) := null;
892 end if;
893
894 ATCB_Allocation.Free_ATCB (T);
895 end Finalize_TCB;
896
897 ---------------
898 -- Exit_Task --
899 ---------------
900
901 procedure Exit_Task is
902 begin
903 -- Mark this task as unknown, so that if Self is called, it won't
904 -- return a dangling pointer.
905
906 Specific.Set (null);
907 end Exit_Task;
908
909 ----------------
910 -- Abort_Task --
911 ----------------
912
913 procedure Abort_Task (T : Task_Id) is
914 Result : Interfaces.C.int;
915 begin
916 if Abort_Handler_Installed then
917 Result :=
918 pthread_kill
919 (T.Common.LL.Thread,
920 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
921 pragma Assert (Result = 0);
922 end if;
923 end Abort_Task;
924
925 ----------------
926 -- Initialize --
927 ----------------
928
929 procedure Initialize (S : in out Suspension_Object) is
930 Mutex_Attr : aliased pthread_mutexattr_t;
931 Cond_Attr : aliased pthread_condattr_t;
932 Result : Interfaces.C.int;
933
934 begin
935 -- Initialize internal state (always to False (RM D.10 (6)))
936
937 S.State := False;
938 S.Waiting := False;
939
940 -- Initialize internal mutex
941
942 Result := pthread_mutexattr_init (Mutex_Attr'Access);
943 pragma Assert (Result = 0 or else Result = ENOMEM);
944
945 if Result = ENOMEM then
946 raise Storage_Error;
947 end if;
948
949 Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access);
950 pragma Assert (Result = 0 or else Result = ENOMEM);
951
952 if Result = ENOMEM then
953 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
954 pragma Assert (Result = 0);
955
956 raise Storage_Error;
957 end if;
958
959 Result := pthread_mutexattr_destroy (Mutex_Attr'Access);
960 pragma Assert (Result = 0);
961
962 -- Initialize internal condition variable
963
964 Result := pthread_condattr_init (Cond_Attr'Access);
965 pragma Assert (Result = 0 or else Result = ENOMEM);
966
967 if Result /= 0 then
968 Result := pthread_mutex_destroy (S.L'Access);
969 pragma Assert (Result = 0);
970
971 -- Storage_Error is propagated as intended if the allocation of the
972 -- underlying OS entities fails.
973
974 raise Storage_Error;
975
976 else
977 Result := GNAT_pthread_condattr_setup (Cond_Attr'Access);
978 pragma Assert (Result = 0);
979 end if;
980
981 Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access);
982 pragma Assert (Result = 0 or else Result = ENOMEM);
983
984 if Result /= 0 then
985 Result := pthread_mutex_destroy (S.L'Access);
986 pragma Assert (Result = 0);
987
988 Result := pthread_condattr_destroy (Cond_Attr'Access);
989 pragma Assert (Result = 0);
990
991 -- Storage_Error is propagated as intended if the allocation of the
992 -- underlying OS entities fails.
993
994 raise Storage_Error;
995 end if;
996
997 Result := pthread_condattr_destroy (Cond_Attr'Access);
998 pragma Assert (Result = 0);
999 end Initialize;
1000
1001 --------------
1002 -- Finalize --
1003 --------------
1004
1005 procedure Finalize (S : in out Suspension_Object) is
1006 Result : Interfaces.C.int;
1007
1008 begin
1009 -- Destroy internal mutex
1010
1011 Result := pthread_mutex_destroy (S.L'Access);
1012 pragma Assert (Result = 0);
1013
1014 -- Destroy internal condition variable
1015
1016 Result := pthread_cond_destroy (S.CV'Access);
1017 pragma Assert (Result = 0);
1018 end Finalize;
1019
1020 -------------------
1021 -- Current_State --
1022 -------------------
1023
1024 function Current_State (S : Suspension_Object) return Boolean is
1025 begin
1026 -- We do not want to use lock on this read operation. State is marked
1027 -- as Atomic so that we ensure that the value retrieved is correct.
1028
1029 return S.State;
1030 end Current_State;
1031
1032 ---------------
1033 -- Set_False --
1034 ---------------
1035
1036 procedure Set_False (S : in out Suspension_Object) is
1037 Result : Interfaces.C.int;
1038
1039 begin
1040 SSL.Abort_Defer.all;
1041
1042 Result := pthread_mutex_lock (S.L'Access);
1043 pragma Assert (Result = 0);
1044
1045 S.State := False;
1046
1047 Result := pthread_mutex_unlock (S.L'Access);
1048 pragma Assert (Result = 0);
1049
1050 SSL.Abort_Undefer.all;
1051 end Set_False;
1052
1053 --------------
1054 -- Set_True --
1055 --------------
1056
1057 procedure Set_True (S : in out Suspension_Object) is
1058 Result : Interfaces.C.int;
1059
1060 begin
1061 SSL.Abort_Defer.all;
1062
1063 Result := pthread_mutex_lock (S.L'Access);
1064 pragma Assert (Result = 0);
1065
1066 -- If there is already a task waiting on this suspension object then
1067 -- we resume it, leaving the state of the suspension object to False,
1068 -- as it is specified in (RM D.10(9)). Otherwise, it just leaves
1069 -- the state to True.
1070
1071 if S.Waiting then
1072 S.Waiting := False;
1073 S.State := False;
1074
1075 Result := pthread_cond_signal (S.CV'Access);
1076 pragma Assert (Result = 0);
1077
1078 else
1079 S.State := True;
1080 end if;
1081
1082 Result := pthread_mutex_unlock (S.L'Access);
1083 pragma Assert (Result = 0);
1084
1085 SSL.Abort_Undefer.all;
1086 end Set_True;
1087
1088 ------------------------
1089 -- Suspend_Until_True --
1090 ------------------------
1091
1092 procedure Suspend_Until_True (S : in out Suspension_Object) is
1093 Result : Interfaces.C.int;
1094
1095 begin
1096 SSL.Abort_Defer.all;
1097
1098 Result := pthread_mutex_lock (S.L'Access);
1099 pragma Assert (Result = 0);
1100
1101 if S.Waiting then
1102
1103 -- Program_Error must be raised upon calling Suspend_Until_True
1104 -- if another task is already waiting on that suspension object
1105 -- (RM D.10(10)).
1106
1107 Result := pthread_mutex_unlock (S.L'Access);
1108 pragma Assert (Result = 0);
1109
1110 SSL.Abort_Undefer.all;
1111
1112 raise Program_Error;
1113
1114 else
1115 -- Suspend the task if the state is False. Otherwise, the task
1116 -- continues its execution, and the state of the suspension object
1117 -- is set to False (ARM D.10 par. 9).
1118
1119 if S.State then
1120 S.State := False;
1121 else
1122 S.Waiting := True;
1123
1124 loop
1125 -- Loop in case pthread_cond_wait returns earlier than expected
1126 -- (e.g. in case of EINTR caused by a signal).
1127
1128 Result := pthread_cond_wait (S.CV'Access, S.L'Access);
1129 pragma Assert (Result = 0 or else Result = EINTR);
1130
1131 exit when not S.Waiting;
1132 end loop;
1133 end if;
1134
1135 Result := pthread_mutex_unlock (S.L'Access);
1136 pragma Assert (Result = 0);
1137
1138 SSL.Abort_Undefer.all;
1139 end if;
1140 end Suspend_Until_True;
1141
1142 ----------------
1143 -- Check_Exit --
1144 ----------------
1145
1146 -- Dummy version
1147
1148 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1149 pragma Unreferenced (Self_ID);
1150 begin
1151 return True;
1152 end Check_Exit;
1153
1154 --------------------
1155 -- Check_No_Locks --
1156 --------------------
1157
1158 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1159 pragma Unreferenced (Self_ID);
1160 begin
1161 return True;
1162 end Check_No_Locks;
1163
1164 ----------------------
1165 -- Environment_Task --
1166 ----------------------
1167
1168 function Environment_Task return Task_Id is
1169 begin
1170 return Environment_Task_Id;
1171 end Environment_Task;
1172
1173 --------------
1174 -- Lock_RTS --
1175 --------------
1176
1177 procedure Lock_RTS is
1178 begin
1179 Write_Lock (Single_RTS_Lock'Access);
1180 end Lock_RTS;
1181
1182 ----------------
1183 -- Unlock_RTS --
1184 ----------------
1185
1186 procedure Unlock_RTS is
1187 begin
1188 Unlock (Single_RTS_Lock'Access);
1189 end Unlock_RTS;
1190
1191 ------------------
1192 -- Suspend_Task --
1193 ------------------
1194
1195 function Suspend_Task
1196 (T : ST.Task_Id;
1197 Thread_Self : Thread_Id) return Boolean
1198 is
1199 pragma Unreferenced (T, Thread_Self);
1200 begin
1201 return False;
1202 end Suspend_Task;
1203
1204 -----------------
1205 -- Resume_Task --
1206 -----------------
1207
1208 function Resume_Task
1209 (T : ST.Task_Id;
1210 Thread_Self : Thread_Id) return Boolean
1211 is
1212 pragma Unreferenced (T, Thread_Self);
1213 begin
1214 return False;
1215 end Resume_Task;
1216
1217 --------------------
1218 -- Stop_All_Tasks --
1219 --------------------
1220
1221 procedure Stop_All_Tasks is
1222 begin
1223 null;
1224 end Stop_All_Tasks;
1225
1226 ---------------
1227 -- Stop_Task --
1228 ---------------
1229
1230 function Stop_Task (T : ST.Task_Id) return Boolean is
1231 pragma Unreferenced (T);
1232 begin
1233 return False;
1234 end Stop_Task;
1235
1236 -------------------
1237 -- Continue_Task --
1238 -------------------
1239
1240 function Continue_Task (T : ST.Task_Id) return Boolean is
1241 pragma Unreferenced (T);
1242 begin
1243 return False;
1244 end Continue_Task;
1245
1246 ----------------
1247 -- Initialize --
1248 ----------------
1249
1250 procedure Initialize (Environment_Task : Task_Id) is
1251 act : aliased struct_sigaction;
1252 old_act : aliased struct_sigaction;
1253 Tmp_Set : aliased sigset_t;
1254 Result : Interfaces.C.int;
1255
1256 function State
1257 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
1258 pragma Import (C, State, "__gnat_get_interrupt_state");
1259 -- Get interrupt state. Defined in a-init.c
1260 -- The input argument is the interrupt number,
1261 -- and the result is one of the following:
1262
1263 Default : constant Character := 's';
1264 -- 'n' this interrupt not set by any Interrupt_State pragma
1265 -- 'u' Interrupt_State pragma set state to User
1266 -- 'r' Interrupt_State pragma set state to Runtime
1267 -- 's' Interrupt_State pragma set state to System (use "default"
1268 -- system handler)
1269
1270 begin
1271 Environment_Task_Id := Environment_Task;
1272 Environment_Task.Common.LL.Thread := pthread_self;
1273
1274 Interrupt_Management.Initialize;
1275
1276 -- Initialize the lock used to synchronize chain of all ATCBs
1277
1278 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1279
1280 Specific.Initialize (Environment_Task);
1281
1282 if Use_Alternate_Stack then
1283 Environment_Task.Common.Task_Alternate_Stack :=
1284 Alternate_Stack'Address;
1285 end if;
1286
1287 -- Make environment task known here because it doesn't go through
1288 -- Activate_Tasks, which does it for all other tasks.
1289
1290 Known_Tasks (Known_Tasks'First) := Environment_Task;
1291 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1292
1293 Enter_Task (Environment_Task);
1294
1295 if State
1296 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
1297 then
1298 act.sa_flags := 0;
1299 act.sa_handler := Abort_Handler'Address;
1300
1301 Result := sigemptyset (Tmp_Set'Access);
1302 pragma Assert (Result = 0);
1303 act.sa_mask := Tmp_Set;
1304
1305 Result :=
1306 sigaction
1307 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
1308 act'Unchecked_Access,
1309 old_act'Unchecked_Access);
1310 pragma Assert (Result = 0);
1311 Abort_Handler_Installed := True;
1312 end if;
1313 end Initialize;
1314
1315 -----------------------
1316 -- Set_Task_Affinity --
1317 -----------------------
1318
1319 procedure Set_Task_Affinity (T : ST.Task_Id) is
1320 pragma Unreferenced (T);
1321
1322 begin
1323 -- Setting task affinity is not supported by the underlying system
1324
1325 null;
1326 end Set_Task_Affinity;
1327
1328 end System.Task_Primitives.Operations;