Debian Codegraph @

gcc/ada/libgnarl/s-taprop__solaris.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 Solaris (native) version of this package
33
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
36
37 with Interfaces.C;
38
39 with System.Interrupt_Management;
40 with System.Multiprocessors;
41 with System.OS_Constants;
42 with System.OS_Primitives;
43 with System.Task_Info;
44 with System.Tasking.Debug;
45
46 pragma Warnings (Off);
47 with System.OS_Lib;
48 pragma Warnings (On);
49
50 with System.Soft_Links;
51 -- We use System.Soft_Links instead of System.Tasking.Initialization
52 -- because the later is a higher level package that we shouldn't depend on.
53 -- For example when using the restricted run time, it is replaced by
54 -- System.Tasking.Restricted.Stages.
55
56 package body System.Task_Primitives.Operations is
57
58 package OSC renames System.OS_Constants;
59 package SSL renames System.Soft_Links;
60
61 use Interfaces.C;
62
63 use System.OS_Interface;
64 use System.OS_Locks;
65 use System.OS_Primitives;
66 use System.Parameters;
67 use System.Tasking;
68 use System.Tasking.Debug;
69
70 ----------------
71 -- Local Data --
72 ----------------
73
74 -- The following are logically constants, but need to be initialized
75 -- at run time.
76
77 Environment_Task_Id : Task_Id;
78 -- A variable to hold Task_Id for the environment task.
79 -- If we use this variable to get the Task_Id, we need the following
80 -- ATCB_Key only for non-Ada threads.
81
82 Unblocked_Signal_Mask : aliased sigset_t;
83 -- The set of signals that should unblocked in all tasks
84
85 ATCB_Key : aliased thread_key_t;
86 -- Key used to find the Ada Task_Id associated with a thread,
87 -- at least for C threads unknown to the Ada run-time system.
88
89 Single_RTS_Lock : aliased RTS_Lock;
90 -- This is a lock to allow only one thread of control in the RTS at
91 -- a time; it is used to execute in mutual exclusion from all other tasks.
92 -- Used to protect All_Tasks_List
93
94 Next_Serial_Number : Task_Serial_Number := 100;
95 -- We start at 100, to reserve some special values for
96 -- using in error checking.
97 -- The following are internal configuration constants needed.
98
99 Abort_Handler_Installed : Boolean := False;
100 -- True if a handler for the abort signal is installed
101
102 Null_Thread_Id : constant Thread_Id := Thread_Id'Last;
103 -- Constant to indicate that the thread identifier has not yet been
104 -- initialized.
105
106 ----------------------
107 -- Priority Support --
108 ----------------------
109
110 Priority_Ceiling_Emulation : constant Boolean := True;
111 -- controls whether we emulate priority ceiling locking
112
113 -- To get a scheduling close to annex D requirements, we use the real-time
114 -- class provided for LWPs and map each task/thread to a specific and
115 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
116
117 -- The real time class can only be set when the process has root
118 -- privileges, so in the other cases, we use the normal thread scheduling
119 -- and priority handling.
120
121 Using_Real_Time_Class : Boolean := False;
122 -- indicates whether the real time class is being used (i.e. the process
123 -- has root privileges).
124
125 Prio_Param : aliased struct_pcparms;
126 -- Hold priority info (Real_Time) initialized during the package
127 -- elaboration.
128
129 -----------------------------------
130 -- External Configuration Values --
131 -----------------------------------
132
133 Time_Slice_Val : constant Integer;
134 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
135
136 Locking_Policy : constant Character;
137 pragma Import (C, Locking_Policy, "__gl_locking_policy");
138
139 Dispatching_Policy : constant Character;
140 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
141
142 Foreign_Task_Elaborated : aliased Boolean := True;
143 -- Used to identified fake tasks (i.e., non-Ada Threads)
144
145 -----------------------
146 -- Local Subprograms --
147 -----------------------
148
149 function sysconf (name : System.OS_Interface.int) return processorid_t;
150 pragma Import (C, sysconf, "sysconf");
151
152 SC_NPROCESSORS_CONF : constant System.OS_Interface.int := 14;
153
154 function Num_Procs
155 (name : System.OS_Interface.int := SC_NPROCESSORS_CONF)
156 return processorid_t renames sysconf;
157
158 procedure Abort_Handler
159 (Sig : Signal;
160 Code : not null access siginfo_t;
161 Context : not null access ucontext_t);
162 -- Target-dependent binding of inter-thread Abort signal to
163 -- the raising of the Abort_Signal exception.
164 -- See also comments in 7staprop.adb
165
166 ------------
167 -- Checks --
168 ------------
169
170 function Check_Initialize_Lock
171 (L : Lock_Ptr;
172 Level : Lock_Level) return Boolean;
173 pragma Inline (Check_Initialize_Lock);
174
175 function Check_Lock (L : Lock_Ptr) return Boolean;
176 pragma Inline (Check_Lock);
177
178 function Record_Lock (L : Lock_Ptr) return Boolean;
179 pragma Inline (Record_Lock);
180
181 function Check_Sleep (Reason : Task_States) return Boolean;
182 pragma Inline (Check_Sleep);
183
184 function Record_Wakeup
185 (L : Lock_Ptr;
186 Reason : Task_States) return Boolean;
187 pragma Inline (Record_Wakeup);
188
189 function Check_Wakeup
190 (T : Task_Id;
191 Reason : Task_States) return Boolean;
192 pragma Inline (Check_Wakeup);
193
194 function Check_Unlock (L : Lock_Ptr) return Boolean;
195 pragma Inline (Check_Unlock);
196
197 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean;
198 pragma Inline (Check_Finalize_Lock);
199
200 --------------------
201 -- Local Packages --
202 --------------------
203
204 package Specific is
205
206 procedure Initialize (Environment_Task : Task_Id);
207 pragma Inline (Initialize);
208 -- Initialize various data needed by this package
209
210 function Is_Valid_Task return Boolean;
211 pragma Inline (Is_Valid_Task);
212 -- Does executing thread have a TCB?
213
214 procedure Set (Self_Id : Task_Id);
215 pragma Inline (Set);
216 -- Set the self id for the current task
217
218 function Self return Task_Id;
219 pragma Inline (Self);
220 -- Return a pointer to the Ada Task Control Block of the calling task
221
222 end Specific;
223
224 package body Specific is separate;
225 -- The body of this package is target specific
226
227 ----------------------------------
228 -- ATCB allocation/deallocation --
229 ----------------------------------
230
231 package body ATCB_Allocation is separate;
232 -- The body of this package is shared across several targets
233
234 ---------------------------------
235 -- Support for foreign threads --
236 ---------------------------------
237
238 function Register_Foreign_Thread
239 (Thread : Thread_Id;
240 Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id;
241 -- Allocate and initialize a new ATCB for the current Thread. The size of
242 -- the secondary stack can be optionally specified.
243
244 function Register_Foreign_Thread
245 (Thread : Thread_Id;
246 Sec_Stack_Size : Size_Type := Unspecified_Size)
247 return Task_Id is separate;
248
249 ------------
250 -- Checks --
251 ------------
252
253 Check_Count : Integer := 0;
254 Lock_Count : Integer := 0;
255 Unlock_Count : Integer := 0;
256
257 -------------------
258 -- Abort_Handler --
259 -------------------
260
261 procedure Abort_Handler
262 (Sig : Signal;
263 Code : not null access siginfo_t;
264 Context : not null access ucontext_t)
265 is
266 pragma Unreferenced (Sig);
267 pragma Unreferenced (Code);
268 pragma Unreferenced (Context);
269
270 Self_ID : constant Task_Id := Self;
271 Old_Set : aliased sigset_t;
272
273 Result : Interfaces.C.int;
274 pragma Warnings (Off, Result);
275
276 begin
277 -- It's not safe to raise an exception when using GCC ZCX mechanism.
278 -- Note that we still need to install a signal handler, since in some
279 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
280 -- need to send the Abort signal to a task.
281
282 if ZCX_By_Default then
283 return;
284 end if;
285
286 if Self_ID.Deferral_Level = 0
287 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
288 and then not Self_ID.Aborting
289 then
290 Self_ID.Aborting := True;
291
292 -- Make sure signals used for RTS internal purpose are unmasked
293
294 Result :=
295 thr_sigsetmask
296 (SIG_UNBLOCK,
297 Unblocked_Signal_Mask'Unchecked_Access,
298 Old_Set'Unchecked_Access);
299 pragma Assert (Result = 0);
300
301 raise Standard'Abort_Signal;
302 end if;
303 end Abort_Handler;
304
305 -----------------
306 -- Stack_Guard --
307 -----------------
308
309 -- The underlying thread system sets a guard page at the
310 -- bottom of a thread stack, so nothing is needed.
311
312 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
313 pragma Unreferenced (T);
314 pragma Unreferenced (On);
315 begin
316 null;
317 end Stack_Guard;
318
319 -------------------
320 -- Get_Thread_Id --
321 -------------------
322
323 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
324 begin
325 return T.Common.LL.Thread;
326 end Get_Thread_Id;
327
328 ----------------
329 -- Initialize --
330 ----------------
331
332 procedure Initialize (Environment_Task : ST.Task_Id) is
333 act : aliased struct_sigaction;
334 old_act : aliased struct_sigaction;
335 Tmp_Set : aliased sigset_t;
336 Result : Interfaces.C.int;
337
338 procedure Configure_Processors;
339 -- Processors configuration
340 -- The user can specify a processor which the program should run
341 -- on to emulate a single-processor system. This can be easily
342 -- done by setting environment variable GNAT_PROCESSOR to one of
343 -- the following :
344 --
345 -- -2 : use the default configuration (run the program on all
346 -- available processors) - this is the same as having
347 -- GNAT_PROCESSOR unset
348 -- -1 : let the RTS choose one processor and run the program on
349 -- that processor
350 -- 0 .. Last_Proc : run the program on the specified processor
351 --
352 -- Last_Proc is equal to the value of the system variable
353 -- _SC_NPROCESSORS_CONF, minus one.
354
355 procedure Configure_Processors is
356 Proc_Acc : constant System.OS_Lib.String_Access :=
357 System.OS_Lib.Getenv ("GNAT_PROCESSOR");
358 Proc : aliased processorid_t; -- User processor #
359 Last_Proc : processorid_t; -- Last processor #
360
361 begin
362 if Proc_Acc.all'Length /= 0 then
363
364 -- Environment variable is defined
365
366 Last_Proc := Num_Procs - 1;
367
368 if Last_Proc /= -1 then
369 Proc := processorid_t'Value (Proc_Acc.all);
370
371 if Proc <= -2 or else Proc > Last_Proc then
372
373 -- Use the default configuration
374
375 null;
376
377 elsif Proc = -1 then
378
379 -- Choose a processor
380
381 Result := 0;
382 while Proc < Last_Proc loop
383 Proc := Proc + 1;
384 Result := p_online (Proc, PR_STATUS);
385 exit when Result = PR_ONLINE;
386 end loop;
387
388 pragma Assert (Result = PR_ONLINE);
389 Result := processor_bind (P_PID, P_MYID, Proc, null);
390 pragma Assert (Result = 0);
391
392 else
393 -- Use user processor
394
395 Result := processor_bind (P_PID, P_MYID, Proc, null);
396 pragma Assert (Result = 0);
397 end if;
398 end if;
399 end if;
400
401 exception
402 when Constraint_Error =>
403
404 -- Illegal environment variable GNAT_PROCESSOR - ignored
405
406 null;
407 end Configure_Processors;
408
409 function State
410 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
411 pragma Import (C, State, "__gnat_get_interrupt_state");
412 -- Get interrupt state. Defined in a-init.c
413 -- The input argument is the interrupt number,
414 -- and the result is one of the following:
415
416 Default : constant Character := 's';
417 -- 'n' this interrupt not set by any Interrupt_State pragma
418 -- 'u' Interrupt_State pragma set state to User
419 -- 'r' Interrupt_State pragma set state to Runtime
420 -- 's' Interrupt_State pragma set state to System (use "default"
421 -- system handler)
422
423 -- Start of processing for Initialize
424
425 begin
426 Environment_Task_Id := Environment_Task;
427 Environment_Task.Common.LL.Thread := thr_self;
428
429 Interrupt_Management.Initialize;
430
431 -- Prepare the set of signals that should unblocked in all tasks
432
433 Result := sigemptyset (Unblocked_Signal_Mask'Access);
434 pragma Assert (Result = 0);
435
436 for J in Interrupt_Management.Interrupt_ID loop
437 if System.Interrupt_Management.Keep_Unmasked (J) then
438 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
439 pragma Assert (Result = 0);
440 end if;
441 end loop;
442
443 if Dispatching_Policy = 'F' then
444 declare
445 Result : Interfaces.C.long;
446 Class_Info : aliased struct_pcinfo;
447 Secs, Nsecs : Interfaces.C.long;
448
449 begin
450 -- If a pragma Time_Slice is specified, takes the value in account
451
452 if Time_Slice_Val > 0 then
453
454 -- Convert Time_Slice_Val (microseconds) to seconds/nanosecs
455
456 Secs := Interfaces.C.long (Time_Slice_Val / 1_000_000);
457 Nsecs :=
458 Interfaces.C.long ((Time_Slice_Val rem 1_000_000) * 1_000);
459
460 -- Otherwise, default to no time slicing (i.e run until blocked)
461
462 else
463 Secs := RT_TQINF;
464 Nsecs := RT_TQINF;
465 end if;
466
467 -- Get the real time class id
468
469 Class_Info.pc_clname (1) := 'R';
470 Class_Info.pc_clname (2) := 'T';
471 Class_Info.pc_clname (3) := ASCII.NUL;
472
473 Result := priocntl (PC_VERSION, P_LWPID, P_MYID, PC_GETCID,
474 Class_Info'Address);
475
476 -- Request the real time class
477
478 Prio_Param.pc_cid := Class_Info.pc_cid;
479 Prio_Param.rt_pri := pri_t (Class_Info.rt_maxpri);
480 Prio_Param.rt_tqsecs := Secs;
481 Prio_Param.rt_tqnsecs := Nsecs;
482
483 Result :=
484 priocntl
485 (PC_VERSION, P_LWPID, P_MYID, PC_SETPARMS, Prio_Param'Address);
486
487 Using_Real_Time_Class := Result /= -1;
488 end;
489 end if;
490
491 Specific.Initialize (Environment_Task);
492
493 -- The following is done in Enter_Task, but this is too late for the
494 -- Environment Task, since we need to call Self in Check_Locks when
495 -- the run time is compiled with assertions on.
496
497 Specific.Set (Environment_Task);
498
499 -- Initialize the lock used to synchronize chain of all ATCBs
500
501 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
502
503 -- Make environment task known here because it doesn't go through
504 -- Activate_Tasks, which does it for all other tasks.
505
506 Known_Tasks (Known_Tasks'First) := Environment_Task;
507 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
508
509 Enter_Task (Environment_Task);
510
511 Configure_Processors;
512
513 if State
514 (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
515 then
516 -- Set sa_flags to SA_NODEFER so that during the handler execution
517 -- we do not change the Signal_Mask to be masked for the Abort_Signal
518 -- This is a temporary fix to the problem that the Signal_Mask is
519 -- not restored after the exception (longjmp) from the handler.
520 -- The right fix should be made in sigsetjmp so that we save
521 -- the Signal_Set and restore it after a longjmp.
522 -- In that case, this field should be changed back to 0. ???
523
524 act.sa_flags := 16;
525
526 act.sa_handler := Abort_Handler'Address;
527 Result := sigemptyset (Tmp_Set'Access);
528 pragma Assert (Result = 0);
529 act.sa_mask := Tmp_Set;
530
531 Result :=
532 sigaction
533 (Signal (System.Interrupt_Management.Abort_Task_Interrupt),
534 act'Unchecked_Access,
535 old_act'Unchecked_Access);
536 pragma Assert (Result = 0);
537 Abort_Handler_Installed := True;
538 end if;
539 end Initialize;
540
541 ---------------------
542 -- Initialize_Lock --
543 ---------------------
544
545 -- Note: mutexes and cond_variables needed per-task basis are initialized
546 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
547 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
548 -- status change of RTS. Therefore raising Storage_Error in the following
549 -- routines should be able to be handled safely.
550
551 procedure Initialize_Lock
552 (Prio : System.Any_Priority;
553 L : not null access Lock)
554 is
555 Result : Interfaces.C.int;
556
557 begin
558 pragma Assert (Check_Initialize_Lock (Lock_Ptr (L), PO_Level));
559
560 if Priority_Ceiling_Emulation then
561 L.Ceiling := Prio;
562 end if;
563
564 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
565 pragma Assert (Result = 0 or else Result = ENOMEM);
566
567 if Result = ENOMEM then
568 raise Storage_Error with "Failed to allocate a lock";
569 end if;
570 end Initialize_Lock;
571
572 procedure Initialize_Lock
573 (L : not null access RTS_Lock;
574 Level : Lock_Level)
575 is
576 Result : Interfaces.C.int;
577
578 begin
579 pragma Assert
580 (Check_Initialize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L)), Level));
581 Result := mutex_init (L.L'Access, USYNC_THREAD, System.Null_Address);
582 pragma Assert (Result = 0 or else Result = ENOMEM);
583
584 if Result = ENOMEM then
585 raise Storage_Error with "Failed to allocate a lock";
586 end if;
587 end Initialize_Lock;
588
589 -------------------
590 -- Finalize_Lock --
591 -------------------
592
593 procedure Finalize_Lock (L : not null access Lock) is
594 Result : Interfaces.C.int;
595 begin
596 pragma Assert (Check_Finalize_Lock (Lock_Ptr (L)));
597 Result := mutex_destroy (L.L'Access);
598 pragma Assert (Result = 0);
599 end Finalize_Lock;
600
601 procedure Finalize_Lock (L : not null access RTS_Lock) is
602 Result : Interfaces.C.int;
603 begin
604 pragma Assert (Check_Finalize_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
605 Result := mutex_destroy (L.L'Access);
606 pragma Assert (Result = 0);
607 end Finalize_Lock;
608
609 ----------------
610 -- Write_Lock --
611 ----------------
612
613 procedure Write_Lock
614 (L : not null access Lock;
615 Ceiling_Violation : out Boolean)
616 is
617 Result : Interfaces.C.int;
618
619 begin
620 pragma Assert (Check_Lock (Lock_Ptr (L)));
621
622 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
623 declare
624 Self_Id : constant Task_Id := Self;
625 Saved_Priority : System.Any_Priority;
626
627 begin
628 if Self_Id.Common.LL.Active_Priority > L.Ceiling then
629 Ceiling_Violation := True;
630 return;
631 end if;
632
633 Saved_Priority := Self_Id.Common.LL.Active_Priority;
634
635 if Self_Id.Common.LL.Active_Priority < L.Ceiling then
636 Set_Priority (Self_Id, L.Ceiling);
637 end if;
638
639 Result := mutex_lock (L.L'Access);
640 pragma Assert (Result = 0);
641 Ceiling_Violation := False;
642
643 L.Saved_Priority := Saved_Priority;
644 end;
645
646 else
647 Result := mutex_lock (L.L'Access);
648 pragma Assert (Result = 0);
649 Ceiling_Violation := False;
650 end if;
651
652 pragma Assert (Record_Lock (Lock_Ptr (L)));
653 end Write_Lock;
654
655 procedure Write_Lock (L : not null access RTS_Lock) is
656 Result : Interfaces.C.int;
657 begin
658 pragma Assert (Check_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
659 Result := mutex_lock (L.L'Access);
660 pragma Assert (Result = 0);
661 pragma Assert (Record_Lock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
662 end Write_Lock;
663
664 procedure Write_Lock (T : Task_Id) is
665 Result : Interfaces.C.int;
666 begin
667 pragma Assert (Check_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
668 Result := mutex_lock (T.Common.LL.L.L'Access);
669 pragma Assert (Result = 0);
670 pragma Assert (Record_Lock (To_Lock_Ptr (T.Common.LL.L'Access)));
671 end Write_Lock;
672
673 ---------------
674 -- Read_Lock --
675 ---------------
676
677 procedure Read_Lock
678 (L : not null access Lock;
679 Ceiling_Violation : out Boolean) is
680 begin
681 Write_Lock (L, Ceiling_Violation);
682 end Read_Lock;
683
684 ------------
685 -- Unlock --
686 ------------
687
688 procedure Unlock (L : not null access Lock) is
689 Result : Interfaces.C.int;
690
691 begin
692 pragma Assert (Check_Unlock (Lock_Ptr (L)));
693
694 if Priority_Ceiling_Emulation and then Locking_Policy = 'C' then
695 declare
696 Self_Id : constant Task_Id := Self;
697
698 begin
699 Result := mutex_unlock (L.L'Access);
700 pragma Assert (Result = 0);
701
702 if Self_Id.Common.LL.Active_Priority > L.Saved_Priority then
703 Set_Priority (Self_Id, L.Saved_Priority);
704 end if;
705 end;
706 else
707 Result := mutex_unlock (L.L'Access);
708 pragma Assert (Result = 0);
709 end if;
710 end Unlock;
711
712 procedure Unlock (L : not null access RTS_Lock) is
713 Result : Interfaces.C.int;
714 begin
715 pragma Assert (Check_Unlock (To_Lock_Ptr (RTS_Lock_Ptr (L))));
716 Result := mutex_unlock (L.L'Access);
717 pragma Assert (Result = 0);
718 end Unlock;
719
720 procedure Unlock (T : Task_Id) is
721 Result : Interfaces.C.int;
722 begin
723 pragma Assert (Check_Unlock (To_Lock_Ptr (T.Common.LL.L'Access)));
724 Result := mutex_unlock (T.Common.LL.L.L'Access);
725 pragma Assert (Result = 0);
726 end Unlock;
727
728 -----------------
729 -- Set_Ceiling --
730 -----------------
731
732 -- Dynamic priority ceilings are not supported by the underlying system
733
734 procedure Set_Ceiling
735 (L : not null access Lock;
736 Prio : System.Any_Priority)
737 is
738 pragma Unreferenced (L, Prio);
739 begin
740 null;
741 end Set_Ceiling;
742
743 -- For the time delay implementation, we need to make sure we
744 -- achieve following criteria:
745
746 -- 1) We have to delay at least for the amount requested.
747 -- 2) We have to give up CPU even though the actual delay does not
748 -- result in blocking.
749 -- 3) Except for restricted run-time systems that do not support
750 -- ATC or task abort, the delay must be interrupted by the
751 -- abort_task operation.
752 -- 4) The implementation has to be efficient so that the delay overhead
753 -- is relatively cheap.
754 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
755 -- requirement we still want to provide the effect in all cases.
756 -- The reason is that users may want to use short delays to implement
757 -- their own scheduling effect in the absence of language provided
758 -- scheduling policies.
759
760 ---------------------
761 -- Monotonic_Clock --
762 ---------------------
763
764 function Monotonic_Clock return Duration is
765 TS : aliased timespec;
766 Result : Interfaces.C.int;
767 begin
768 Result := clock_gettime (OSC.CLOCK_RT_Ada, TS'Unchecked_Access);
769 pragma Assert (Result = 0);
770 return To_Duration (TS);
771 end Monotonic_Clock;
772
773 -------------------
774 -- RT_Resolution --
775 -------------------
776
777 function RT_Resolution return Duration is
778 TS : aliased timespec;
779 Result : Interfaces.C.int;
780 begin
781 Result := clock_getres (OSC.CLOCK_REALTIME, TS'Unchecked_Access);
782 pragma Assert (Result = 0);
783
784 return To_Duration (TS);
785 end RT_Resolution;
786
787 -----------
788 -- Yield --
789 -----------
790
791 procedure Yield (Do_Yield : Boolean := True) is
792 begin
793 if Do_Yield then
794 System.OS_Interface.thr_yield;
795 end if;
796 end Yield;
797
798 -----------
799 -- Self ---
800 -----------
801
802 function Self return Task_Id renames Specific.Self;
803
804 ------------------
805 -- Set_Priority --
806 ------------------
807
808 procedure Set_Priority
809 (T : Task_Id;
810 Prio : System.Any_Priority;
811 Loss_Of_Inheritance : Boolean := False)
812 is
813 pragma Unreferenced (Loss_Of_Inheritance);
814
815 Result : Interfaces.C.int;
816 pragma Unreferenced (Result);
817
818 Param : aliased struct_pcparms;
819
820 use Task_Info;
821
822 begin
823 T.Common.Current_Priority := Prio;
824
825 if Priority_Ceiling_Emulation then
826 T.Common.LL.Active_Priority := Prio;
827 end if;
828
829 if Using_Real_Time_Class then
830 Param.pc_cid := Prio_Param.pc_cid;
831 Param.rt_pri := pri_t (Prio);
832 Param.rt_tqsecs := Prio_Param.rt_tqsecs;
833 Param.rt_tqnsecs := Prio_Param.rt_tqnsecs;
834
835 Result := Interfaces.C.int (
836 priocntl (PC_VERSION, P_LWPID, T.Common.LL.LWP, PC_SETPARMS,
837 Param'Address));
838
839 else
840 if T.Common.Task_Info /= null
841 and then not T.Common.Task_Info.Bound_To_LWP
842 then
843 -- The task is not bound to a LWP, so use thr_setprio
844
845 Result :=
846 thr_setprio (T.Common.LL.Thread, Interfaces.C.int (Prio));
847
848 else
849 -- The task is bound to a LWP, use priocntl
850 -- ??? TBD
851
852 null;
853 end if;
854 end if;
855 end Set_Priority;
856
857 ------------------
858 -- Get_Priority --
859 ------------------
860
861 function Get_Priority (T : Task_Id) return System.Any_Priority is
862 begin
863 return T.Common.Current_Priority;
864 end Get_Priority;
865
866 ----------------
867 -- Enter_Task --
868 ----------------
869
870 procedure Enter_Task (Self_ID : Task_Id) is
871 begin
872 Self_ID.Common.LL.LWP := lwp_self;
873
874 Set_Task_Affinity (Self_ID);
875 Specific.Set (Self_ID);
876
877 -- We need the above code even if we do direct fetch of Task_Id in Self
878 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
879 end Enter_Task;
880
881 -------------------
882 -- Is_Valid_Task --
883 -------------------
884
885 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
886
887 -----------------------------
888 -- Register_Foreign_Thread --
889 -----------------------------
890
891 function Register_Foreign_Thread return Task_Id is
892 begin
893 if Is_Valid_Task then
894 return Self;
895 else
896 return Register_Foreign_Thread (thr_self);
897 end if;
898 end Register_Foreign_Thread;
899
900 --------------------
901 -- Initialize_TCB --
902 --------------------
903
904 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
905 Result : Interfaces.C.int := 0;
906
907 begin
908 -- Give the task a unique serial number
909
910 Self_ID.Serial_Number := Next_Serial_Number;
911 Next_Serial_Number := Next_Serial_Number + 1;
912 pragma Assert (Next_Serial_Number /= 0);
913
914 Self_ID.Common.LL.Thread := Null_Thread_Id;
915
916 Result :=
917 mutex_init
918 (Self_ID.Common.LL.L.L'Access, USYNC_THREAD, System.Null_Address);
919 Self_ID.Common.LL.L.Level :=
920 Private_Task_Serial_Number (Self_ID.Serial_Number);
921 pragma Assert (Result = 0 or else Result = ENOMEM);
922
923 if Result = 0 then
924 Result := cond_init (Self_ID.Common.LL.CV'Access, USYNC_THREAD, 0);
925 pragma Assert (Result = 0 or else Result = ENOMEM);
926 end if;
927
928 if Result = 0 then
929 Succeeded := True;
930 else
931 Result := mutex_destroy (Self_ID.Common.LL.L.L'Access);
932 pragma Assert (Result = 0);
933
934 Succeeded := False;
935 end if;
936 end Initialize_TCB;
937
938 -----------------
939 -- Create_Task --
940 -----------------
941
942 procedure Create_Task
943 (T : Task_Id;
944 Wrapper : System.Address;
945 Stack_Size : System.Parameters.Size_Type;
946 Priority : System.Any_Priority;
947 Succeeded : out Boolean)
948 is
949 pragma Unreferenced (Priority);
950
951 Result : Interfaces.C.int;
952 Adjusted_Stack_Size : Interfaces.C.size_t;
953 Opts : Interfaces.C.int := THR_DETACHED;
954
955 Page_Size : constant System.Parameters.Size_Type := 4096;
956 -- This constant is for reserving extra space at the
957 -- end of the stack, which can be used by the stack
958 -- checking as guard page. The idea is that we need
959 -- to have at least Stack_Size bytes available for
960 -- actual use.
961
962 use System.Task_Info;
963 use type System.Multiprocessors.CPU_Range;
964
965 begin
966 -- Check whether both Dispatching_Domain and CPU are specified for the
967 -- task, and the CPU value is not contained within the range of
968 -- processors for the domain.
969
970 if T.Common.Domain /= null
971 and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
972 and then
973 (T.Common.Base_CPU not in T.Common.Domain'Range
974 or else not T.Common.Domain (T.Common.Base_CPU))
975 then
976 Succeeded := False;
977 return;
978 end if;
979
980 Adjusted_Stack_Size := Interfaces.C.size_t (Stack_Size + Page_Size);
981
982 -- Since the initial signal mask of a thread is inherited from the
983 -- creator, and the Environment task has all its signals masked, we
984 -- do not need to manipulate caller's signal mask at this point.
985 -- All tasks in RTS will have All_Tasks_Mask initially.
986
987 if T.Common.Task_Info /= null then
988 if T.Common.Task_Info.New_LWP then
989 Opts := Opts + THR_NEW_LWP;
990 end if;
991
992 if T.Common.Task_Info.Bound_To_LWP then
993 Opts := Opts + THR_BOUND;
994 end if;
995
996 else
997 Opts := THR_DETACHED + THR_BOUND;
998 end if;
999
1000 -- The write to T.Common.LL.Thread is not racy with regard to the
1001 -- created thread because the created thread will not access it until
1002 -- we release the RTS lock (or the current task's lock when
1003 -- Restricted.Stages is used). One can verify that by inspecting the
1004 -- Task_Wrapper procedures.
1005
1006 Result :=
1007 thr_create
1008 (System.Null_Address,
1009 Adjusted_Stack_Size,
1010 Thread_Body_Access (Wrapper),
1011 To_Address (T),
1012 Opts,
1013 T.Common.LL.Thread'Access);
1014
1015 Succeeded := Result = 0;
1016 pragma Assert
1017 (Result = 0
1018 or else Result = ENOMEM
1019 or else Result = EAGAIN);
1020 end Create_Task;
1021
1022 ------------------
1023 -- Finalize_TCB --
1024 ------------------
1025
1026 procedure Finalize_TCB (T : Task_Id) is
1027 Result : Interfaces.C.int;
1028
1029 begin
1030 T.Common.LL.Thread := Null_Thread_Id;
1031
1032 Result := mutex_destroy (T.Common.LL.L.L'Access);
1033 pragma Assert (Result = 0);
1034
1035 Result := cond_destroy (T.Common.LL.CV'Access);
1036 pragma Assert (Result = 0);
1037
1038 if T.Known_Tasks_Index /= -1 then
1039 Known_Tasks (T.Known_Tasks_Index) := null;
1040 end if;
1041
1042 ATCB_Allocation.Free_ATCB (T);
1043 end Finalize_TCB;
1044
1045 ---------------
1046 -- Exit_Task --
1047 ---------------
1048
1049 -- This procedure must be called with abort deferred. It can no longer
1050 -- call Self or access the current task's ATCB, since the ATCB has been
1051 -- deallocated.
1052
1053 procedure Exit_Task is
1054 begin
1055 Specific.Set (null);
1056 end Exit_Task;
1057
1058 ----------------
1059 -- Abort_Task --
1060 ----------------
1061
1062 procedure Abort_Task (T : Task_Id) is
1063 Result : Interfaces.C.int;
1064 begin
1065 if Abort_Handler_Installed then
1066 pragma Assert (T /= Self);
1067 Result :=
1068 thr_kill
1069 (T.Common.LL.Thread,
1070 Signal (System.Interrupt_Management.Abort_Task_Interrupt));
1071 pragma Assert (Result = 0);
1072 end if;
1073 end Abort_Task;
1074
1075 -----------
1076 -- Sleep --
1077 -----------
1078
1079 procedure Sleep
1080 (Self_ID : Task_Id;
1081 Reason : Task_States)
1082 is
1083 Result : Interfaces.C.int;
1084
1085 begin
1086 pragma Assert (Check_Sleep (Reason));
1087
1088 Result :=
1089 cond_wait
1090 (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L.L'Access);
1091
1092 pragma Assert
1093 (Record_Wakeup (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1094 pragma Assert (Result = 0 or else Result = EINTR);
1095 end Sleep;
1096
1097 -- Note that we are relying heavily here on GNAT representing
1098 -- Calendar.Time, System.Real_Time.Time, Duration,
1099 -- System.Real_Time.Time_Span in the same way, i.e., as a 64-bit count of
1100 -- nanoseconds.
1101
1102 -- This allows us to always pass the timeout value as a Duration
1103
1104 -- ???
1105 -- We are taking liberties here with the semantics of the delays. That is,
1106 -- we make no distinction between delays on the Calendar clock and delays
1107 -- on the Real_Time clock. That is technically incorrect, if the Calendar
1108 -- clock happens to be reset or adjusted. To solve this defect will require
1109 -- modification to the compiler interface, so that it can pass through more
1110 -- information, to tell us here which clock to use.
1111
1112 -- cond_timedwait will return if any of the following happens:
1113 -- 1) some other task did cond_signal on this condition variable
1114 -- In this case, the return value is 0
1115 -- 2) the call just returned, for no good reason
1116 -- This is called a "spurious wakeup".
1117 -- In this case, the return value may also be 0.
1118 -- 3) the time delay expires
1119 -- In this case, the return value is ETIME
1120 -- 4) this task received a signal, which was handled by some
1121 -- handler procedure, and now the thread is resuming execution
1122 -- UNIX calls this an "interrupted" system call.
1123 -- In this case, the return value is EINTR
1124
1125 -- If the cond_timedwait returns 0 or EINTR, it is still possible that the
1126 -- time has actually expired, and by chance a signal or cond_signal
1127 -- occurred at around the same time.
1128
1129 -- We have also observed that on some OS's the value ETIME will be
1130 -- returned, but the clock will show that the full delay has not yet
1131 -- expired.
1132
1133 -- For these reasons, we need to check the clock after return from
1134 -- cond_timedwait. If the time has expired, we will set Timedout = True.
1135
1136 -- This check might be omitted for systems on which the cond_timedwait()
1137 -- never returns early or wakes up spuriously.
1138
1139 -- Annex D requires that completion of a delay cause the task to go to the
1140 -- end of its priority queue, regardless of whether the task actually was
1141 -- suspended by the delay. Since cond_timedwait does not do this on
1142 -- Solaris, we add a call to thr_yield at the end. We might do this at the
1143 -- beginning, instead, but then the round-robin effect would not be the
1144 -- same; the delayed task would be ahead of other tasks of the same
1145 -- priority that awoke while it was sleeping.
1146
1147 -- For Timed_Sleep, we are expecting possible cond_signals to indicate
1148 -- other events (e.g., completion of a RV or completion of the abortable
1149 -- part of an async. select), we want to always return if interrupted. The
1150 -- caller will be responsible for checking the task state to see whether
1151 -- the wakeup was spurious, and to go back to sleep again in that case. We
1152 -- don't need to check for pending abort or priority change on the way in
1153 -- our out; that is the caller's responsibility.
1154
1155 -- For Timed_Delay, we are not expecting any cond_signals or other
1156 -- interruptions, except for priority changes and aborts. Therefore, we
1157 -- don't want to return unless the delay has actually expired, or the call
1158 -- has been aborted. In this case, since we want to implement the entire
1159 -- delay statement semantics, we do need to check for pending abort and
1160 -- priority changes. We can quietly handle priority changes inside the
1161 -- procedure, since there is no entry-queue reordering involved.
1162
1163 -----------------
1164 -- Timed_Sleep --
1165 -----------------
1166
1167 procedure Timed_Sleep
1168 (Self_ID : Task_Id;
1169 Time : Duration;
1170 Mode : ST.Delay_Modes;
1171 Reason : System.Tasking.Task_States;
1172 Timedout : out Boolean;
1173 Yielded : out Boolean)
1174 is
1175 Base_Time : constant Duration := Monotonic_Clock;
1176 Check_Time : Duration := Base_Time;
1177 Abs_Time : Duration;
1178 Request : aliased timespec;
1179 Result : Interfaces.C.int;
1180
1181 begin
1182 pragma Assert (Check_Sleep (Reason));
1183 Timedout := True;
1184 Yielded := False;
1185
1186 Abs_Time :=
1187 (if Mode = Relative
1188 then Duration'Min (Time, Max_Sensible_Delay) + Check_Time
1189 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
1190
1191 if Abs_Time > Check_Time then
1192 Request := To_Timespec (Abs_Time);
1193 loop
1194 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1195
1196 Result :=
1197 cond_timedwait
1198 (Self_ID.Common.LL.CV'Access,
1199 Self_ID.Common.LL.L.L'Access, Request'Access);
1200 Yielded := True;
1201 Check_Time := Monotonic_Clock;
1202
1203 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
1204
1205 if Result = 0 or Result = EINTR then
1206
1207 -- Somebody may have called Wakeup for us
1208
1209 Timedout := False;
1210 exit;
1211 end if;
1212
1213 pragma Assert (Result = ETIME);
1214 end loop;
1215 end if;
1216
1217 pragma Assert
1218 (Record_Wakeup (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Reason));
1219 end Timed_Sleep;
1220
1221 -----------------
1222 -- Timed_Delay --
1223 -----------------
1224
1225 procedure Timed_Delay
1226 (Self_ID : Task_Id;
1227 Time : Duration;
1228 Mode : ST.Delay_Modes)
1229 is
1230 Base_Time : constant Duration := Monotonic_Clock;
1231 Check_Time : Duration := Base_Time;
1232 Abs_Time : Duration;
1233 Request : aliased timespec;
1234 Result : Interfaces.C.int;
1235 Yielded : Boolean := False;
1236
1237 begin
1238 Write_Lock (Self_ID);
1239
1240 Abs_Time :=
1241 (if Mode = Relative
1242 then Time + Check_Time
1243 else Duration'Min (Check_Time + Max_Sensible_Delay, Time));
1244
1245 if Abs_Time > Check_Time then
1246 Request := To_Timespec (Abs_Time);
1247 Self_ID.Common.State := Delay_Sleep;
1248
1249 pragma Assert (Check_Sleep (Delay_Sleep));
1250
1251 loop
1252 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
1253
1254 Result :=
1255 cond_timedwait
1256 (Self_ID.Common.LL.CV'Access,
1257 Self_ID.Common.LL.L.L'Access,
1258 Request'Access);
1259 Yielded := True;
1260 Check_Time := Monotonic_Clock;
1261
1262 exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;
1263
1264 pragma Assert
1265 (Result = 0 or else
1266 Result = ETIME or else
1267 Result = EINTR);
1268 end loop;
1269
1270 pragma Assert
1271 (Record_Wakeup
1272 (To_Lock_Ptr (Self_ID.Common.LL.L'Access), Delay_Sleep));
1273
1274 Self_ID.Common.State := Runnable;
1275 end if;
1276
1277 Unlock (Self_ID);
1278
1279 if not Yielded then
1280 thr_yield;
1281 end if;
1282 end Timed_Delay;
1283
1284 ------------
1285 -- Wakeup --
1286 ------------
1287
1288 procedure Wakeup
1289 (T : Task_Id;
1290 Reason : Task_States)
1291 is
1292 Result : Interfaces.C.int;
1293 begin
1294 pragma Assert (Check_Wakeup (T, Reason));
1295 Result := cond_signal (T.Common.LL.CV'Access);
1296 pragma Assert (Result = 0);
1297 end Wakeup;
1298
1299 ---------------------------
1300 -- Check_Initialize_Lock --
1301 ---------------------------
1302
1303 -- The following code is intended to check some of the invariant assertions
1304 -- related to lock usage, on which we depend.
1305
1306 function Check_Initialize_Lock
1307 (L : Lock_Ptr;
1308 Level : Lock_Level) return Boolean
1309 is
1310 Self_ID : constant Task_Id := Self;
1311
1312 begin
1313 -- Check that caller is abort-deferred
1314
1315 if Self_ID.Deferral_Level = 0 then
1316 return False;
1317 end if;
1318
1319 -- Check that the lock is not yet initialized
1320
1321 if L.Level /= 0 then
1322 return False;
1323 end if;
1324
1325 L.Level := Lock_Level'Pos (Level) + 1;
1326 return True;
1327 end Check_Initialize_Lock;
1328
1329 ----------------
1330 -- Check_Lock --
1331 ----------------
1332
1333 function Check_Lock (L : Lock_Ptr) return Boolean is
1334 Self_ID : constant Task_Id := Self;
1335 P : Lock_Ptr;
1336
1337 begin
1338 -- Check that the argument is not null
1339
1340 if L = null then
1341 return False;
1342 end if;
1343
1344 -- Check that L is not frozen
1345
1346 if L.Frozen then
1347 return False;
1348 end if;
1349
1350 -- Check that caller is abort-deferred
1351
1352 if Self_ID.Deferral_Level = 0 then
1353 return False;
1354 end if;
1355
1356 -- Check that caller is not holding this lock already
1357
1358 if L.Owner = To_Owner_ID (To_Address (Self_ID)) then
1359 return False;
1360 end if;
1361
1362 -- Check that TCB lock order rules are satisfied
1363
1364 P := Self_ID.Common.LL.Locks;
1365 if P /= null then
1366 if P.Level >= L.Level
1367 and then (P.Level > 2 or else L.Level > 2)
1368 then
1369 return False;
1370 end if;
1371 end if;
1372
1373 return True;
1374 end Check_Lock;
1375
1376 -----------------
1377 -- Record_Lock --
1378 -----------------
1379
1380 function Record_Lock (L : Lock_Ptr) return Boolean is
1381 Self_ID : constant Task_Id := Self;
1382 P : Lock_Ptr;
1383
1384 begin
1385 Lock_Count := Lock_Count + 1;
1386
1387 -- There should be no owner for this lock at this point
1388
1389 if L.Owner /= null then
1390 return False;
1391 end if;
1392
1393 -- Record new owner
1394
1395 L.Owner := To_Owner_ID (To_Address (Self_ID));
1396
1397 -- Check that TCB lock order rules are satisfied
1398
1399 P := Self_ID.Common.LL.Locks;
1400
1401 if P /= null then
1402 L.Next := To_RTS_Lock_Ptr (P);
1403 end if;
1404
1405 Self_ID.Common.LL.Locking := null;
1406 Self_ID.Common.LL.Locks := L;
1407 return True;
1408 end Record_Lock;
1409
1410 -----------------
1411 -- Check_Sleep --
1412 -----------------
1413
1414 function Check_Sleep (Reason : Task_States) return Boolean is
1415 pragma Unreferenced (Reason);
1416
1417 Self_ID : constant Task_Id := Self;
1418 P : Lock_Ptr;
1419
1420 begin
1421 -- Check that caller is abort-deferred
1422
1423 if Self_ID.Deferral_Level = 0 then
1424 return False;
1425 end if;
1426
1427 -- Check that caller is holding own lock, on top of list
1428
1429 if Self_ID.Common.LL.Locks /=
1430 To_Lock_Ptr (Self_ID.Common.LL.L'Access)
1431 then
1432 return False;
1433 end if;
1434
1435 -- Check that TCB lock order rules are satisfied
1436
1437 if Self_ID.Common.LL.Locks.Next /= null then
1438 return False;
1439 end if;
1440
1441 Self_ID.Common.LL.L.Owner := null;
1442 P := Self_ID.Common.LL.Locks;
1443 Self_ID.Common.LL.Locks := To_Lock_Ptr (Self_ID.Common.LL.Locks.Next);
1444 P.Next := null;
1445 return True;
1446 end Check_Sleep;
1447
1448 -------------------
1449 -- Record_Wakeup --
1450 -------------------
1451
1452 function Record_Wakeup
1453 (L : Lock_Ptr;
1454 Reason : Task_States) return Boolean
1455 is
1456 pragma Unreferenced (Reason);
1457
1458 Self_ID : constant Task_Id := Self;
1459 P : Lock_Ptr;
1460
1461 begin
1462 -- Record new owner
1463
1464 L.Owner := To_Owner_ID (To_Address (Self_ID));
1465
1466 -- Check that TCB lock order rules are satisfied
1467
1468 P := Self_ID.Common.LL.Locks;
1469
1470 if P /= null then
1471 L.Next := To_RTS_Lock_Ptr (P);
1472 end if;
1473
1474 Self_ID.Common.LL.Locking := null;
1475 Self_ID.Common.LL.Locks := L;
1476 return True;
1477 end Record_Wakeup;
1478
1479 ------------------
1480 -- Check_Wakeup --
1481 ------------------
1482
1483 function Check_Wakeup
1484 (T : Task_Id;
1485 Reason : Task_States) return Boolean
1486 is
1487 Self_ID : constant Task_Id := Self;
1488
1489 begin
1490 -- Is caller holding T's lock?
1491
1492 if T.Common.LL.L.Owner /= To_Owner_ID (To_Address (Self_ID)) then
1493 return False;
1494 end if;
1495
1496 -- Are reasons for wakeup and sleep consistent?
1497
1498 if T.Common.State /= Reason then
1499 return False;
1500 end if;
1501
1502 return True;
1503 end Check_Wakeup;
1504
1505 ------------------
1506 -- Check_Unlock --
1507 ------------------
1508
1509 function Check_Unlock (L : Lock_Ptr) return Boolean is
1510 Self_ID : constant Task_Id := Self;
1511 P : Lock_Ptr;
1512
1513 begin
1514 Unlock_Count := Unlock_Count + 1;
1515
1516 if L = null then
1517 return False;
1518 end if;
1519
1520 if L.Buddy /= null then
1521 return False;
1522 end if;
1523
1524 -- Magic constant 4???
1525
1526 if L.Level = 4 then
1527 Check_Count := Unlock_Count;
1528 end if;
1529
1530 -- Magic constant 1000???
1531
1532 if Unlock_Count - Check_Count > 1000 then
1533 Check_Count := Unlock_Count;
1534 end if;
1535
1536 -- Check that caller is abort-deferred
1537
1538 if Self_ID.Deferral_Level = 0 then
1539 return False;
1540 end if;
1541
1542 -- Check that caller is holding this lock, on top of list
1543
1544 if Self_ID.Common.LL.Locks /= L then
1545 return False;
1546 end if;
1547
1548 -- Record there is no owner now
1549
1550 L.Owner := null;
1551 P := Self_ID.Common.LL.Locks;
1552 Self_ID.Common.LL.Locks := To_Lock_Ptr (Self_ID.Common.LL.Locks.Next);
1553 P.Next := null;
1554 return True;
1555 end Check_Unlock;
1556
1557 -------------------------
1558 -- Check_Finalize_Lock --
1559 -------------------------
1560
1561 function Check_Finalize_Lock (L : Lock_Ptr) return Boolean is
1562 Self_ID : constant Task_Id := Self;
1563
1564 begin
1565 -- Check that caller is abort-deferred
1566
1567 if Self_ID.Deferral_Level = 0 then
1568 return False;
1569 end if;
1570
1571 -- Check that no one is holding this lock
1572
1573 if L.Owner /= null then
1574 return False;
1575 end if;
1576
1577 L.Frozen := True;
1578 return True;
1579 end Check_Finalize_Lock;
1580
1581 ----------------
1582 -- Initialize --
1583 ----------------
1584
1585 procedure Initialize (S : in out Suspension_Object) is
1586 Result : Interfaces.C.int;
1587
1588 begin
1589 -- Initialize internal state (always to zero (RM D.10(6)))
1590
1591 S.State := False;
1592 S.Waiting := False;
1593
1594 -- Initialize internal mutex
1595
1596 Result := mutex_init (S.L'Access, USYNC_THREAD, System.Null_Address);
1597 pragma Assert (Result = 0 or else Result = ENOMEM);
1598
1599 if Result = ENOMEM then
1600 raise Storage_Error with "Failed to allocate a lock";
1601 end if;
1602
1603 -- Initialize internal condition variable
1604
1605 Result := cond_init (S.CV'Access, USYNC_THREAD, 0);
1606 pragma Assert (Result = 0 or else Result = ENOMEM);
1607
1608 if Result /= 0 then
1609 Result := mutex_destroy (S.L'Access);
1610 pragma Assert (Result = 0);
1611
1612 if Result = ENOMEM then
1613 raise Storage_Error;
1614 end if;
1615 end if;
1616 end Initialize;
1617
1618 --------------
1619 -- Finalize --
1620 --------------
1621
1622 procedure Finalize (S : in out Suspension_Object) is
1623 Result : Interfaces.C.int;
1624
1625 begin
1626 -- Destroy internal mutex
1627
1628 Result := mutex_destroy (S.L'Access);
1629 pragma Assert (Result = 0);
1630
1631 -- Destroy internal condition variable
1632
1633 Result := cond_destroy (S.CV'Access);
1634 pragma Assert (Result = 0);
1635 end Finalize;
1636
1637 -------------------
1638 -- Current_State --
1639 -------------------
1640
1641 function Current_State (S : Suspension_Object) return Boolean is
1642 begin
1643 -- We do not want to use lock on this read operation. State is marked
1644 -- as Atomic so that we ensure that the value retrieved is correct.
1645
1646 return S.State;
1647 end Current_State;
1648
1649 ---------------
1650 -- Set_False --
1651 ---------------
1652
1653 procedure Set_False (S : in out Suspension_Object) is
1654 Result : Interfaces.C.int;
1655
1656 begin
1657 SSL.Abort_Defer.all;
1658
1659 Result := mutex_lock (S.L'Access);
1660 pragma Assert (Result = 0);
1661
1662 S.State := False;
1663
1664 Result := mutex_unlock (S.L'Access);
1665 pragma Assert (Result = 0);
1666
1667 SSL.Abort_Undefer.all;
1668 end Set_False;
1669
1670 --------------
1671 -- Set_True --
1672 --------------
1673
1674 procedure Set_True (S : in out Suspension_Object) is
1675 Result : Interfaces.C.int;
1676
1677 begin
1678 SSL.Abort_Defer.all;
1679
1680 Result := mutex_lock (S.L'Access);
1681 pragma Assert (Result = 0);
1682
1683 -- If there is already a task waiting on this suspension object then
1684 -- we resume it, leaving the state of the suspension object to False,
1685 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1686 -- the state to True.
1687
1688 if S.Waiting then
1689 S.Waiting := False;
1690 S.State := False;
1691
1692 Result := cond_signal (S.CV'Access);
1693 pragma Assert (Result = 0);
1694
1695 else
1696 S.State := True;
1697 end if;
1698
1699 Result := mutex_unlock (S.L'Access);
1700 pragma Assert (Result = 0);
1701
1702 SSL.Abort_Undefer.all;
1703 end Set_True;
1704
1705 ------------------------
1706 -- Suspend_Until_True --
1707 ------------------------
1708
1709 procedure Suspend_Until_True (S : in out Suspension_Object) is
1710 Result : Interfaces.C.int;
1711
1712 begin
1713 SSL.Abort_Defer.all;
1714
1715 Result := mutex_lock (S.L'Access);
1716 pragma Assert (Result = 0);
1717
1718 if S.Waiting then
1719
1720 -- Program_Error must be raised upon calling Suspend_Until_True
1721 -- if another task is already waiting on that suspension object
1722 -- (RM D.10(10)).
1723
1724 Result := mutex_unlock (S.L'Access);
1725 pragma Assert (Result = 0);
1726
1727 SSL.Abort_Undefer.all;
1728
1729 raise Program_Error;
1730
1731 else
1732 -- Suspend the task if the state is False. Otherwise, the task
1733 -- continues its execution, and the state of the suspension object
1734 -- is set to False (ARM D.10 par. 9).
1735
1736 if S.State then
1737 S.State := False;
1738 else
1739 S.Waiting := True;
1740
1741 loop
1742 -- Loop in case pthread_cond_wait returns earlier than expected
1743 -- (e.g. in case of EINTR caused by a signal).
1744
1745 Result := cond_wait (S.CV'Access, S.L'Access);
1746 pragma Assert (Result = 0 or else Result = EINTR);
1747
1748 exit when not S.Waiting;
1749 end loop;
1750 end if;
1751
1752 Result := mutex_unlock (S.L'Access);
1753 pragma Assert (Result = 0);
1754
1755 SSL.Abort_Undefer.all;
1756 end if;
1757 end Suspend_Until_True;
1758
1759 ----------------
1760 -- Check_Exit --
1761 ----------------
1762
1763 function Check_Exit (Self_ID : Task_Id) return Boolean is
1764 begin
1765 -- Check that caller is just holding Global_Task_Lock and no other locks
1766
1767 if Self_ID.Common.LL.Locks = null then
1768 return False;
1769 end if;
1770
1771 -- 2 = Global_Task_Level
1772
1773 if Self_ID.Common.LL.Locks.Level /= 2 then
1774 return False;
1775 end if;
1776
1777 if Self_ID.Common.LL.Locks.Next /= null then
1778 return False;
1779 end if;
1780
1781 -- Check that caller is abort-deferred
1782
1783 if Self_ID.Deferral_Level = 0 then
1784 return False;
1785 end if;
1786
1787 return True;
1788 end Check_Exit;
1789
1790 --------------------
1791 -- Check_No_Locks --
1792 --------------------
1793
1794 function Check_No_Locks (Self_ID : Task_Id) return Boolean is
1795 begin
1796 return Self_ID.Common.LL.Locks = null;
1797 end Check_No_Locks;
1798
1799 ----------------------
1800 -- Environment_Task --
1801 ----------------------
1802
1803 function Environment_Task return Task_Id is
1804 begin
1805 return Environment_Task_Id;
1806 end Environment_Task;
1807
1808 --------------
1809 -- Lock_RTS --
1810 --------------
1811
1812 procedure Lock_RTS is
1813 begin
1814 Write_Lock (Single_RTS_Lock'Access);
1815 end Lock_RTS;
1816
1817 ----------------
1818 -- Unlock_RTS --
1819 ----------------
1820
1821 procedure Unlock_RTS is
1822 begin
1823 Unlock (Single_RTS_Lock'Access);
1824 end Unlock_RTS;
1825
1826 ------------------
1827 -- Suspend_Task --
1828 ------------------
1829
1830 function Suspend_Task
1831 (T : ST.Task_Id;
1832 Thread_Self : Thread_Id) return Boolean
1833 is
1834 begin
1835 if T.Common.LL.Thread /= Thread_Self then
1836 return thr_suspend (T.Common.LL.Thread) = 0;
1837 else
1838 return True;
1839 end if;
1840 end Suspend_Task;
1841
1842 -----------------
1843 -- Resume_Task --
1844 -----------------
1845
1846 function Resume_Task
1847 (T : ST.Task_Id;
1848 Thread_Self : Thread_Id) return Boolean
1849 is
1850 begin
1851 if T.Common.LL.Thread /= Thread_Self then
1852 return thr_continue (T.Common.LL.Thread) = 0;
1853 else
1854 return True;
1855 end if;
1856 end Resume_Task;
1857
1858 --------------------
1859 -- Stop_All_Tasks --
1860 --------------------
1861
1862 procedure Stop_All_Tasks is
1863 begin
1864 null;
1865 end Stop_All_Tasks;
1866
1867 ---------------
1868 -- Stop_Task --
1869 ---------------
1870
1871 function Stop_Task (T : ST.Task_Id) return Boolean is
1872 pragma Unreferenced (T);
1873 begin
1874 return False;
1875 end Stop_Task;
1876
1877 -------------------
1878 -- Continue_Task --
1879 -------------------
1880
1881 function Continue_Task (T : ST.Task_Id) return Boolean is
1882 pragma Unreferenced (T);
1883 begin
1884 return False;
1885 end Continue_Task;
1886
1887 -----------------------
1888 -- Set_Task_Affinity --
1889 -----------------------
1890
1891 procedure Set_Task_Affinity (T : ST.Task_Id) is
1892 Result : Interfaces.C.int;
1893 Proc : processorid_t; -- User processor #
1894 Last_Proc : processorid_t; -- Last processor #
1895
1896 use System.Task_Info;
1897 use type System.Multiprocessors.CPU_Range;
1898
1899 begin
1900 -- Do nothing if the underlying thread has not yet been created. If the
1901 -- thread has not yet been created then the proper affinity will be set
1902 -- during its creation.
1903
1904 if T.Common.LL.Thread = Null_Thread_Id then
1905 null;
1906
1907 -- pragma CPU
1908
1909 elsif T.Common.Base_CPU /=
1910 System.Multiprocessors.Not_A_Specific_CPU
1911 then
1912 -- The CPU numbering in pragma CPU starts at 1 while the subprogram
1913 -- to set the affinity starts at 0, therefore we must substract 1.
1914
1915 Result :=
1916 processor_bind
1917 (P_LWPID, id_t (T.Common.LL.LWP),
1918 processorid_t (T.Common.Base_CPU) - 1, null);
1919 pragma Assert (Result = 0);
1920
1921 -- Task_Info
1922
1923 elsif T.Common.Task_Info /= null then
1924 if T.Common.Task_Info.New_LWP
1925 and then T.Common.Task_Info.CPU /= CPU_UNCHANGED
1926 then
1927 Last_Proc := Num_Procs - 1;
1928
1929 if T.Common.Task_Info.CPU = ANY_CPU then
1930 Result := 0;
1931
1932 Proc := 0;
1933 while Proc < Last_Proc loop
1934 Result := p_online (Proc, PR_STATUS);
1935 exit when Result = PR_ONLINE;
1936 Proc := Proc + 1;
1937 end loop;
1938
1939 Result :=
1940 processor_bind
1941 (P_LWPID, id_t (T.Common.LL.LWP), Proc, null);
1942 pragma Assert (Result = 0);
1943
1944 else
1945 -- Use specified processor
1946
1947 if T.Common.Task_Info.CPU < 0
1948 or else T.Common.Task_Info.CPU > Last_Proc
1949 then
1950 raise Invalid_CPU_Number;
1951 end if;
1952
1953 Result :=
1954 processor_bind
1955 (P_LWPID, id_t (T.Common.LL.LWP),
1956 T.Common.Task_Info.CPU, null);
1957 pragma Assert (Result = 0);
1958 end if;
1959 end if;
1960
1961 -- Handle dispatching domains
1962
1963 elsif T.Common.Domain /= null
1964 and then (T.Common.Domain /= ST.System_Domain
1965 or else T.Common.Domain.all /=
1966 (Multiprocessors.CPU'First ..
1967 Multiprocessors.Number_Of_CPUs => True))
1968 then
1969 declare
1970 CPU_Set : aliased psetid_t;
1971 Result : Interfaces.C.int;
1972
1973 begin
1974 Result := pset_create (CPU_Set'Access);
1975 pragma Assert (Result = 0);
1976
1977 -- Set the affinity to all the processors belonging to the
1978 -- dispatching domain.
1979
1980 for Proc in T.Common.Domain'Range loop
1981
1982 -- The Ada CPU numbering starts at 1 while the subprogram to
1983 -- set the affinity starts at 0, therefore we must substract 1.
1984
1985 if T.Common.Domain (Proc) then
1986 Result :=
1987 pset_assign (CPU_Set, processorid_t (Proc) - 1, null);
1988 pragma Assert (Result = 0);
1989 end if;
1990 end loop;
1991
1992 Result :=
1993 pset_bind (CPU_Set, P_LWPID, id_t (T.Common.LL.LWP), null);
1994 pragma Assert (Result = 0);
1995 end;
1996 end if;
1997 end Set_Task_Affinity;
1998
1999 end System.Task_Primitives.Operations;