1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
| | ------------------------------------------------------------------------------
-- --
-- GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS --
-- --
-- S Y S T E M . T A S K _ P R I M I T I V E S --
-- --
-- B o d y --
-- --
-- $Revision: 1.1 $ --
-- --
-- Copyright (C) 1991,1992,1993,1994,1996 Florida State University --
-- --
-- GNARL is free software; you can redistribute it and/or modify it under --
-- terms of the GNU Library General Public License as published by the --
-- Free Software Foundation; either version 2, or (at your option) any --
-- later version. GNARL is distributed in the hope that it will be use- --
-- ful, but but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Gen- --
-- eral Library Public License for more details. You should have received --
-- a copy of the GNU Library General Public License along with GNARL; see --
-- file COPYING.LIB. If not, write to the Free Software Foundation, 675 --
-- Mass Ave, Cambridge, MA 02139, USA. --
-- --
------------------------------------------------------------------------------
with GNAT.IO;
with Interfaces.C.POSIX_timers;
with Interfaces.C.POSIX_Error;
use Interfaces.C.POSIX_Error;
with Interfaces.C.POSIX_RTE;
use Interfaces.C.POSIX_RTE;
with Interfaces.C.Pthreads;
use Interfaces.C.Pthreads;
with Interfaces.C;
use Interfaces.C;
with System.Tasking;
use System.Tasking;
with System.Storage_Elements;
use System.Storage_Elements;
with System.Compiler_Exceptions;
use System.Compiler_Exceptions;
with System.Task_Specific_Data;
use System.Task_Specific_Data;
with System.Secondary_Stack;
use System.Secondary_Stack;
with System.Tasking_Soft_Links;
with System.Task_Clock;
use System.Task_Clock;
with Unchecked_Conversion;
with Interfaces.C.System_Constants;
package body System.Task_Primitives is
use Interfaces.C.Pthreads;
use Interfaces.C.System_Constants;
package RTE renames Interfaces.C.POSIX_RTE;
package TSL renames System.Tasking_Soft_Links;
Test_And_Set_Mutex : Lock;
Abort_Signal : constant := 6;
Abort_Handler : Abort_Handler_Pointer;
ATCB_Key : aliased pthread_key_t;
Unblocked_Signal_Mask : aliased RTE.Signal_Set;
-- The set of signals that should be unblocked in a task.
-- This is in general the signals that can be generated synchronously,
-- and which should therefore be converted into Ada exceptions.
-- It also includes the Abort_Signal, to allow asynchronous abortion.
function To_void_ptr is new
Unchecked_Conversion (TCB_Ptr, void_ptr);
function To_TCB_Ptr is new
Unchecked_Conversion (void_ptr, TCB_Ptr);
function pthread_mutexattr_setprotocol
(attr : access pthread_attr_t; priority : integer) return int;
pragma Import (C,
pthread_mutexattr_setprotocol,
"pthread_mutexattr_setprotocol",
"pthread_mutexattr_setprotocol");
function pthread_mutexattr_setprio_ceiling
(attr : access pthread_attr_t; priority : int) return int;
pragma Import (C,
pthread_mutexattr_setprio_ceiling,
"pthread_mutexattr_setprio_ceiling",
"pthread_mutexattr_setprio_ceiling");
pthread_mutexattr_default : pthread_mutexattr_t;
pragma Import (C, pthread_mutexattr_default,
"pthread_mutexattr_default",
"pthread_mutexattr_default");
-----------------------
-- Local Subprograms --
-----------------------
procedure Abort_Wrapper
(signo : Integer;
info : RTE.siginfo_ptr;
context : System.Address);
-- This is a signal handler procedure which calls the user-specified
-- abort handler procedure.
procedure LL_Wrapper (T : TCB_Ptr);
-- A wrapper procedure that is called from a new low-level task.
-- It performs initializations for the new task and calls the
-- user-specified startup procedure.
-------------------------
-- Initialize_LL_Tasks --
-------------------------
procedure Initialize_LL_Tasks (T : TCB_Ptr) is
Result : int;
begin
T.LL_Entry_Point := null;
T.Thread := pthread_self;
Result := pthread_key_create (ATCB_Key'Access, null);
if Result = FUNC_ERR then
raise Storage_Error; -- Insufficient resources.
end if;
T.Thread := pthread_self;
Result := pthread_setspecific (ATCB_Key, To_void_ptr (T));
if Result = FUNC_ERR then
GNAT.IO.Put_Line ("Get specific failed");
raise Storage_Error; -- Insufficient resources.
end if;
pragma Assert (Result /= FUNC_ERR,
"GNULLI failure---pthread_setspecific");
end Initialize_LL_Tasks;
----------
-- Self --
----------
function Self return TCB_Ptr is
Temp : aliased void_ptr;
Result : int;
begin
Result := pthread_getspecific (ATCB_Key, Temp'Access);
pragma Assert (Result /= FUNC_ERR,
"GNULLI failure---pthread_getspecific");
return To_TCB_Ptr (Temp);
end Self;
---------------------
-- Initialize_Lock --
---------------------
procedure Initialize_Lock
(Prio : System.Any_Priority;
L : in out Lock)
is
Attributes : aliased pthread_mutexattr_t;
Result : int;
MUTEX_NONRECURSIVE_NP : constant := 2;
begin
Result := pthread_mutexattr_init (Attributes'Access);
if Result = FUNC_ERR then
raise STORAGE_ERROR; -- should be ENOMEM
end if;
Result := pthread_mutexattr_setkind
(Attributes'Access, MUTEX_NONRECURSIVE_NP);
if Result = FUNC_ERR then
raise STORAGE_ERROR; -- should be ENOMEM
end if;
Result := pthread_mutex_init (L.mutex'Access, Attributes);
if Result = FUNC_ERR then
Result := pthread_mutexattr_destroy (Attributes'Access);
raise STORAGE_ERROR; -- should be ENOMEM ???
end if;
Result := pthread_mutexattr_destroy (Attributes'Access);
end Initialize_Lock;
-------------------
-- Finalize_Lock --
-------------------
procedure Finalize_Lock (L : in out Lock) is
Result : int;
begin
Result := pthread_mutex_destroy (L.mutex'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_mutex_destroy");
end Finalize_Lock;
----------------
-- Write_Lock --
----------------
--
-- The current pthreads implementation does not check for Ceiling
-- violations.
--
procedure Write_Lock (L : in out Lock; Ceiling_Violation : out Boolean) is
Result : int;
begin
Ceiling_Violation := False;
Result := pthread_mutex_lock (L.mutex'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI FUNC_ERR---pthread_mutex_lock");
end Write_Lock;
---------------
-- Read_Lock --
---------------
procedure Read_Lock (L : in out Lock; Ceiling_Violation : out Boolean)
renames Write_Lock;
------------
-- Unlock --
------------
procedure Unlock (L : in out Lock) is
Result : int;
begin
Result := pthread_mutex_unlock (L.mutex'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI FUNC_ERR---pthread_mutex_unlock");
end Unlock;
---------------------
-- Initialize_Cond --
---------------------
procedure Initialize_Cond (Cond : in out Condition_Variable) is
Attributes : aliased Pthreads.pthread_condattr_t;
Result : int;
begin
Result := pthread_condattr_init (Attributes'Access);
if Result = FUNC_ERR then
raise STORAGE_ERROR; -- should be ENOMEM ???
end if;
-- Result := pthread_cond_init (Cond.CV'Access, Attributes'Access);
Result := pthread_cond_init (Cond.CV'Access, Attributes);
if Result = FUNC_ERR then
raise STORAGE_ERROR; -- should be ENOMEM ???
end if;
Result := pthread_condattr_destroy (Attributes'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI FUNC_ERR---pthread_condattr_destroy");
end Initialize_Cond;
-------------------
-- Finalize_Cond --
-------------------
procedure Finalize_Cond (Cond : in out Condition_Variable) is
Result : int;
begin
Result := pthread_cond_destroy (Cond.CV'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_cond_destroy");
end Finalize_Cond;
---------------
-- Cond_Wait --
---------------
procedure Cond_Wait (Cond : in out Condition_Variable; L : in out Lock) is
Result : int;
begin
Result := pthread_cond_wait (Cond.CV'Access, L.mutex'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_cond_wait");
end Cond_Wait;
---------------------
-- Cond_Timed_Wait --
---------------------
procedure Cond_Timed_Wait
(Cond : in out Condition_Variable;
L : in out Lock;
Abs_Time : System.Task_Clock.Stimespec;
Timed_Out : out Boolean) is
Result : int;
TV : aliased timespec;
use POSIX_Error;
begin
Timed_Out := False; -- Assume success until we know otherwise
TV.tv_sec := int (Interfaces.C.POSIX_timers.time_t
(Task_Clock.Stimespec_Seconds (Abs_Time)));
TV.tv_nsec := long (Interfaces.C.POSIX_timers.Nanoseconds
(Task_Clock.Stimespec_NSeconds (Abs_Time)));
Result := pthread_cond_timedwait
(Cond.CV'Access, L.mutex'Access, TV'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_cond_timedwait");
end Cond_Timed_Wait;
-----------------
-- Cond_Signal --
-----------------
procedure Cond_Signal (Cond : in out Condition_Variable) is
Result : int;
begin
Result := pthread_cond_signal (Cond.CV'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_cond_signal");
end Cond_Signal;
------------------
-- Set_Priority --
------------------
procedure Set_Priority
(T : TCB_Ptr;
Prio : System.Any_Priority) is
Result : int;
Thread : Pthreads.pthread_t renames T.Thread;
begin
Result := pthread_setprio (Thread, int (Prio));
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_setprio");
end Set_Priority;
----------------------
-- Set_Own_Priority --
----------------------
procedure Set_Own_Priority (Prio : System.Any_Priority) is
begin
null;
-- ENOSYS Result :=
-- pthread_setprio (pthread_self, int (Prio));
-- pragma Assert
-- (Result /= FUNC_ERR, "GNULLI failure---pthread_setprio");
end Set_Own_Priority;
------------------
-- Get_Priority --
------------------
function Get_Priority (T : TCB_Ptr) return System.Any_Priority is
Priority : aliased int := 0;
begin
-- ENOSYS Result := pthread_getprio (T.Thread, Priority'Access);
-- pragma Assert
-- (Result /= FUNC_ERR, "GNULLI failure---pthread_getprio");
return System.Priority (Priority);
end Get_Priority;
-----------------------
-- Get_Own_Priority --
-----------------------
function Get_Own_Priority return System.Any_Priority is
Result : int;
Priority : aliased int := 0;
begin
Result := pthread_getprio (pthread_self, Priority'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_getprio");
return System.Priority (Priority);
end Get_Own_Priority;
--------------------
-- Create_LL_Task --
--------------------
procedure Create_LL_Task
(Priority : System.Any_Priority;
Stack_Size : Task_Storage_Size;
Task_Info : System.Task_Info.Task_Info_Type;
LL_Entry_Point : LL_Task_Procedure_Access;
Arg : System.Address;
T : TCB_Ptr) is
use Pthreads;
Attributes : aliased pthread_attr_t;
Result : int;
L_Priority : System.Any_Priority := Priority;
function To_Start_Addr is new
Unchecked_Conversion (System.Address, start_addr);
begin
T.LL_Entry_Point := LL_Entry_Point;
T.LL_Arg := Arg;
T.Stack_Size := Stack_Size;
Result := pthread_attr_init (Attributes'Access);
pragma Assert (Result /= FUNC_ERR, "GNULLI failure---pthread_attr_init");
-- Result := pthread_attr_setdetachstate (Attributes'Access, 1);
-- pragma Assert
-- (Result /= FUNC_ERR, "GNULLI failure---pthread_setdetachstate");
Result := pthread_attr_setstacksize
(Attributes'Access, size_t (Stack_Size));
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_setstacksize");
Result := pthread_attr_setinheritsched
(Attributes'Access, PTHREAD_DEFAULT_SCHED);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_setinheritsched");
Result := pthread_attr_setsched
(Attributes'Access, SCHED_FIFO);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_setinheritsched");
-- The following priority adjustment is a kludge to get around needing
-- root privileges to run at higher than 18 for FIFO or 19 for OTHER.
if (L_Priority > 18) then
L_Priority := 18;
elsif (L_Priority < 14) then
L_Priority := 14;
end if;
Result := pthread_attr_setprio
(Attributes'Access, int (L_Priority));
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_attr_setprio");
Result := pthread_create
(T.Thread'Access,
Attributes,
To_Start_Addr (LL_Wrapper'Address),
T.all'Address);
if Result = FUNC_ERR then
GNAT.IO.Put_Line ("pthread create failed");
raise Storage_Error;
end if;
pragma Assert (Result /= FUNC_ERR, "GNULLI failure---pthread_create");
Result := pthread_attr_destroy (Attributes'Access);
pragma Assert
(Result /= FUNC_ERR, "GNULLI failure---pthread_attr_destroy");
end Create_LL_Task;
-----------------
-- Exit_LL_Task --
------------------
procedure Exit_LL_Task is
begin
pthread_exit (System.Null_Address);
end Exit_LL_Task;
----------------
-- Abort_Task --
----------------
procedure Abort_Task (T : TCB_Ptr) is
Result : int;
begin
-- Result := pthread_kill (T.Thread);
-- pragma Assert
-- (Result /= FUNC_ERR, "GNULLI failure---pthread_kill");
null;
end Abort_Task;
----------------
-- Test_Abort --
----------------
-- This procedure does nothing. It is intended for systems without
-- asynchronous abortion, where the runtime system would have to
-- synchronously poll for pending abortions. This should be done
-- at least at every synchronization point.
procedure Test_Abort is
begin
null;
end Test_Abort;
---------------------------
-- Install_Abort_Handler --
---------------------------
procedure Install_Abort_Handler (Handler : Abort_Handler_Pointer) is
act : aliased RTE.struct_sigaction;
old_act : aliased RTE.struct_sigaction;
Result : POSIX_Error.Return_Code;
SA_SIGINFO : constant := 64;
use type POSIX_Error.Return_Code;
begin
Abort_Handler := Handler;
act.sa_flags := SA_SIGINFO;
act.sa_handler := Abort_Wrapper'Address;
RTE.sigemptyset (act.sa_mask'Access, Result);
pragma Assert (Result /= FUNC_ERR, "GNULLI failure---sigemptyset");
RTE.sigaction (Abort_Signal, act'Access, old_act'Access, Result);
pragma Assert (Result /= FUNC_ERR, "GNULLI failure---sigaction");
end Install_Abort_Handler;
-------------------
-- Abort_Wrapper --
-------------------
-- This is the handler called by the OS when an abort signal is
-- received; it in turn calls the handler installed by the client.
-- This procedure serves to isolate the client from the
-- implementation-specific calling conventions of asynchronous
-- handlers.
procedure Abort_Wrapper
(signo : Integer;
info : RTE.siginfo_ptr;
context : System.Address)
is
function Address_To_Call_State is new
Unchecked_Conversion (System.Address, Pre_Call_State);
begin
Abort_Handler (Address_To_Call_State (context));
end Abort_Wrapper;
---------------------------
-- Install_Error_Handler --
---------------------------
procedure Install_Error_Handler (Handler : System.Address) is
Temp : Address;
use Pthreads;
begin
-- Set up the soft links to tasking services used in the absence of
-- tasking. These replace tasking-free defaults.
Temp := TSL.Get_Jmpbuf_Address.all;
-- pthread_set_jumpbuf_address (Temp);
Temp := TSL.Get_Sec_Stack_Addr.all;
-- pthread_set_sec_stack_addr (Temp);
-- TSL.Get_Jmpbuf_Address := pthread_get_jumpbuf_address'Access;
-- TSL.Set_Jmpbuf_Address := pthread_set_jumpbuf_address'Access;
-- TSL.Get_Gnat_Exception := pthread_get_exception'Access;
-- TSL.Set_Gnat_Exception := pthread_set_exception'Access;
end Install_Error_Handler;
---------------
-- LL_Assert --
---------------
procedure LL_Assert (B : Boolean; M : String) is
begin
null;
end LL_Assert;
----------------
-- LL_Wrapper --
----------------
procedure LL_Wrapper (T : TCB_Ptr) is
Result : POSIX_Error.Return_Code;
Result1 : int;
Exc_Stack : String (1 .. 256);
Exc_Base : Address := Exc_Stack (Exc_Stack'Last)'Address + 1;
Old_Set : aliased RTE.Signal_Set;
begin
Result1 := pthread_setspecific (ATCB_Key, T.all'Address);
RTE.sigprocmask (
RTE.SIG_UNBLOCK, Unblocked_Signal_Mask'Access, Old_Set'Access, Result);
pragma Assert (
Result /= Failure, "GNULLI failure---sigprocmask");
-- Note that the following call may not return!
T.LL_Entry_Point (T.LL_Arg);
end LL_Wrapper;
--------------------------
-- Test and Set support --
--------------------------
procedure Initialize_TAS_Cell (Cell : out TAS_Cell) is
begin
Cell.Value := 0;
end Initialize_TAS_Cell;
procedure Finalize_TAS_Cell (Cell : in out TAS_Cell) is
begin
null;
end Finalize_TAS_Cell;
procedure Clear (Cell : in out TAS_Cell) is
begin
Cell.Value := 1;
end Clear;
procedure Test_And_Set (Cell : in out TAS_Cell; Result : out Boolean) is
Error : Boolean;
begin
Write_Lock (Test_And_Set_Mutex, Error);
if Cell.Value = 1 then
Result := False;
else
Result := True;
Cell.Value := 1;
end if;
Unlock (Test_And_Set_Mutex);
end Test_And_Set;
function Is_Set (Cell : in TAS_Cell) return Boolean is
begin
return Cell.Value = 1;
end Is_Set;
begin
Initialize_Lock (System.Any_Priority'Last, Test_And_Set_Mutex);
end System.Task_Primitives;
|