Debian Codegraph @

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