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 | ------------------------------------------------------------------------------
-- --
-- GLADE COMPONENTS --
-- --
-- S Y S T E M . R P C . S E R V E R --
-- --
-- B o d y --
-- --
-- Copyright (C) 1996-2020 Free Software Foundation, Inc. --
-- --
-- GARLIC is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GARLIC is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI- --
-- LITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with GARLIC; see file COPYING. If --
-- not, write to the Free Software Foundation, 59 Temple Place - Suite 330, --
-- Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
-- --
------------------------------------------------------------------------------
with Ada.Exceptions;
pragma Warnings (Off, Ada.Exceptions);
with Ada.Finalization;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with Interfaces;
with System.Partition_Interface;
with System.Garlic; use System.Garlic;
with System.Garlic.Debug; use System.Garlic.Debug;
with System.Garlic.Exceptions; use System.Garlic.Exceptions;
with System.Garlic.Heart; use System.Garlic.Heart;
with System.Garlic.Options;
with System.Garlic.Platform_Specific;
with System.Garlic.Priorities;
with System.Garlic.Priorities.Mapping;
with System.Garlic.Soft_Links;
with System.Garlic.Streams;
with System.Garlic.Tasking;
with System.Garlic.Types;
with System.Garlic.Startup;
pragma Elaborate_All (System.Garlic.Startup);
pragma Warnings (Off, System.Garlic.Startup);
package body System.RPC.Server is
use type System.Garlic.Types.Partition_ID;
Private_Debug_Key : constant Debug_Key :=
Debug_Initialize ("S_RPCSER", "(s-rpcser): ");
procedure D
(Message : String;
Key : Debug_Key := Private_Debug_Key)
renames Print_Debug_Info;
type Inner_Abort_Handler_Type is
new Ada.Finalization.Controlled with
record
Outer : System.Garlic.Soft_Links.Abort_Handler_Type;
end record;
type Outer_Abort_Handler_Type is
new System.Garlic.Soft_Links.Abort_Handler_Type with
record
Inner : Inner_Abort_Handler_Type;
end record;
procedure Finalize (Handler : in out Inner_Abort_Handler_Type);
procedure Adjust (Self : in out Outer_Abort_Handler_Type);
function Convert is
new Ada.Unchecked_Conversion
(System.Address, System.Partition_Interface.RPC_Receiver);
-- This package handles a pool of anonymous tasks which will be used
-- by System.RPC to handle incoming calls.
procedure Allocate_Task
(Partition : System.Garlic.Types.Partition_ID;
Session : System.RPC.Session_Type;
-- Stamp : System.Garlic.Types.Stamp_Type;
Params : System.Garlic.Streams.Params_Stream_Access;
Async : Boolean);
-- Start a new anonymous task to handle the request
procedure Abort_Task
(Partition : System.Garlic.Types.Partition_ID;
Session : System.RPC.Session_Type);
-- Abort a running task
procedure Execute_Remote_Subprogram
(Params : Streams.Params_Stream_Access;
Result : Streams.Params_Stream_Access);
-- Extract access to subprogram from Params and execute it
-- locally. Parameters for this subprogram are also marshalled in
-- Params. The returned parameters are marshalled in Result.
procedure Initialize;
-- Initialize this package
procedure Shutdown;
-- Called on shutdown
type Task_Identifier;
type Task_Identifier_Access is access Task_Identifier;
function Create_RPC_Handler return Task_Identifier_Access;
procedure Destroy_RPC_Handler
(Identifier : in out Task_Identifier_Access);
task type RPC_Handler is
entry Initialize (Identifier : Task_Identifier_Access);
entry Execute;
entry Shutdown;
pragma Priority (System.Priority'Last);
pragma Storage_Size (2_000_000);
end RPC_Handler;
type RPC_Handler_Access is access RPC_Handler;
-- An anonymous task will serve a request. Is the pragma Storage_Size
-- still needed there ???
type Task_Identifier is record
Handler : RPC_Handler_Access;
Session : Session_Type;
Partition : Types.Partition_ID;
Stop : System.Garlic.Tasking.Mutex_PO_Access;
Params : Streams.Params_Stream_Access;
-- Stamp : System.Garlic.Types.Stamp_Type;
Async : Boolean;
Next : Task_Identifier_Access;
Prev : Task_Identifier_Access;
end record;
-- Since it is impossible for a task to get a pointer on itself, it
-- is transmitted through this structure. Moreover, this allows to
-- handle a list of free tasks very easily.
procedure Free is
new Ada.Unchecked_Deallocation (Task_Identifier, Task_Identifier_Access);
Low_Mark : Positive renames System.Garlic.Options.Task_Pool_Low_Bound;
High_Mark : Positive renames System.Garlic.Options.Task_Pool_High_Bound;
Max_Mark : Positive renames System.Garlic.Options.Task_Pool_Max_Bound;
Allocated_Tasks : Natural := 0;
Deallocated_Tasks : Natural := 0;
Tasks_Pool_Count : Natural := 0;
Idle_Tasks_Count : Natural := 0;
Tasks_Pool_Mutex : System.Garlic.Soft_Links.Mutex_Access;
Tasks_Pool_Watcher : System.Garlic.Soft_Links.Watcher_Access;
Idle_Tasks_Queue : Task_Identifier_Access;
Used_Tasks_Queue : Task_Identifier_Access;
Terminated : Boolean := False;
procedure Show_Tasks_Pool;
-- This procedure will print a tasks pool status in debug mode
----------------
-- Abort_Task --
----------------
procedure Abort_Task
(Partition : Types.Partition_ID;
Session : Session_Type)
is
Identifier : Task_Identifier_Access;
begin
System.Garlic.Soft_Links.Enter (Tasks_Pool_Mutex);
Identifier := Used_Tasks_Queue;
while Identifier /= null
and then (Identifier.Partition /= Partition
or else Identifier.Session /= Session)
loop
Identifier := Identifier.Next;
end loop;
if Identifier /= null then
Identifier.Stop.Leave;
end if;
System.Garlic.Soft_Links.Leave (Tasks_Pool_Mutex);
end Abort_Task;
------------
-- Adjust --
------------
procedure Adjust (Self : in out Outer_Abort_Handler_Type) is
begin
Self.Inner.Outer.Partition := Self.Partition;
Self.Inner.Outer.Waiting := Self.Waiting;
Self.Inner.Outer.Session := Self.Session;
end Adjust;
-------------------
-- Allocate_Task --
-------------------
procedure Allocate_Task
(Partition : Types.Partition_ID;
Session : Session_Type;
-- Stamp : System.Garlic.Types.Stamp_Type;
Params : Streams.Params_Stream_Access;
Async : Boolean)
is
Identifier : Task_Identifier_Access;
Version : System.Garlic.Types.Version_Id;
begin
-- Recycle an anonymous task from the task pool or allocate a
-- new one depending on the anonymous task pool.
while not Terminated loop
System.Garlic.Soft_Links.Enter (Tasks_Pool_Mutex);
if Idle_Tasks_Count > 0 then
Identifier := Idle_Tasks_Queue;
Idle_Tasks_Queue := Identifier.Next;
Idle_Tasks_Count := Idle_Tasks_Count - 1;
elsif Tasks_Pool_Count < Max_Mark then
Identifier := Create_RPC_Handler;
else
System.Garlic.Soft_Links.Lookup (Tasks_Pool_Watcher, Version);
end if;
if Identifier /= null then
Identifier.Next := Used_Tasks_Queue;
if Identifier.Next /= null then
Identifier.Next.Prev := Identifier;
end if;
Identifier.Prev := null;
Used_Tasks_Queue := Identifier;
Identifier.Session := Session;
Identifier.Partition := Partition;
Identifier.Params := Params;
-- Identifier.Stamp := Stamp;
Identifier.Async := Async;
Identifier.Handler.Execute;
end if;
System.Garlic.Soft_Links.Leave (Tasks_Pool_Mutex);
exit when Identifier /= null;
System.Garlic.Soft_Links.Differ (Tasks_Pool_Watcher, Version);
end loop;
if Terminated then
raise System.RPC.Communication_Error;
end if;
Show_Tasks_Pool;
end Allocate_Task;
-----------------
-- RPC_Handler --
-----------------
task body RPC_Handler is
Callee : Types.Partition_ID;
Result : Streams.Params_Stream_Access;
Cancelled : Boolean;
Priority : Priorities.Global_Priority;
Self : Task_Identifier_Access;
Aborted : Boolean := False;
use Ada.Exceptions;
use System.Garlic.Priorities;
use System.Garlic.Priorities.Mapping;
begin
pragma Debug (D ("Anonymous task starting"));
select
accept Initialize (Identifier : Task_Identifier_Access) do
Self := Identifier;
end Initialize;
or
terminate;
end select;
while Self /= null loop
pragma Debug (D ("Waiting for a job"));
select
accept Execute;
or
accept Shutdown do
Aborted := True;
end Shutdown;
or
terminate;
end select;
-- Soft_Links.Set_Stamp (Self.Stamp);
exit when Aborted;
-- Before executing anything, make sure that our elaboration is
-- finished.
Wait_For_Elaboration_Completion;
Result := new Streams.Params_Stream_Type (0);
Cancelled := False;
Types.Partition_ID'Read (Self.Params, Callee);
if not Callee'Valid then
pragma Debug (D ("Invalid Partition received"));
raise Constraint_Error;
end if;
Global_Priority'Read (Self.Params, Priority);
if RPC_Handler_Priority_Policy = Client_Propagated then
System.Garlic.Soft_Links.Set_Priority
(To_Native_Priority (Priority));
else
System.Garlic.Soft_Links.Set_Priority
(To_Native_Priority (RPC_Handler_Priority));
end if;
When_Established;
if Self.Async
or else not System.Garlic.Platform_Specific.Support_RPC_Abortion
then
Execute_Remote_Subprogram (Self.Params, Result);
else
select
Self.Stop.Enter;
-- This RPC is aborted. Send an abortion reply to recycle
-- properly Session_Type.
declare
Empty : aliased Streams.Params_Stream_Type (0);
Header : constant RPC_Header
:= (Abortion_Reply, Self.Session);
Error : aliased Error_Type;
begin
pragma Debug (D ("Abortion queried by caller"));
Insert_RPC_Header (Empty'Access, Header);
Send (Self.Partition, Remote_Call, Empty'Access, Error);
if Found (Error) then
Raise_Exception (Communication_Error'Identity,
Content (Error'Access));
end if;
Cancelled := True;
end;
then abort
Execute_Remote_Subprogram (Self.Params, Result);
end select;
end if;
declare
Copy : Streams.Params_Stream_Access := Self.Params;
begin
-- Yes, we deallocate a copy, because Params is readonly (it's
-- a discriminant). We must *not* use Params later in this task.
Streams.Deallocate (Copy);
end;
if Self.Async or else Cancelled then
pragma Debug (D ("Result not sent"));
Streams.Deallocate (Result);
else
declare
Header : constant RPC_Header := (RPC_Reply, Self.Session);
Error : aliased Error_Type;
begin
pragma Debug (D ("Result will be sent"));
Insert_RPC_Header (Result, Header);
Send (Self.Partition, Remote_Call, Result, Error);
if Found (Error) then
Raise_Exception (Communication_Error'Identity,
Content (Error'Access));
end if;
Streams.Free (Result);
end;
end if;
pragma Debug (D ("Job finished, queuing"));
-- pragma Debug (Soft_Links.Set_Stamp (Types.No_Stamp));
-- Set RPC handler back to its initial priority.
System.Garlic.Soft_Links.Set_Priority (System.Priority'Last);
-- Recycle anonymous task or destroy depending on the
-- configuration of the anonymous task pool.
System.Garlic.Soft_Links.Enter (Tasks_Pool_Mutex);
if Self.Prev = null then
Used_Tasks_Queue := Self.Next;
else
Self.Prev.Next := Self.Next;
end if;
if Self.Next /= null then
Self.Next.Prev := Self.Prev;
end if;
if Idle_Tasks_Count < High_Mark then
Self.Prev := null;
Self.Next := Idle_Tasks_Queue;
Idle_Tasks_Queue := Self;
Idle_Tasks_Count := Idle_Tasks_Count + 1;
else
Destroy_RPC_Handler (Self);
end if;
System.Garlic.Soft_Links.Update (Tasks_Pool_Watcher);
System.Garlic.Soft_Links.Leave (Tasks_Pool_Mutex);
end loop;
exception
when E : others =>
pragma Warnings (Off, E);
pragma Debug (D ("Error in anonymous task " &
"(exception " & Exception_Name (E) & ")"));
Destroy_RPC_Handler (Self);
end RPC_Handler;
------------------------
-- Create_RPC_Handler --
------------------------
function Create_RPC_Handler return Task_Identifier_Access is
Identifier : constant Task_Identifier_Access := new Task_Identifier;
begin
Allocated_Tasks := Allocated_Tasks + 1;
Tasks_Pool_Count := Tasks_Pool_Count + 1;
Identifier.Handler := new RPC_Handler;
Identifier.Stop := new System.Garlic.Tasking.Mutex_PO;
Identifier.Stop.Enter;
Identifier.Handler.Initialize (Identifier);
return Identifier;
end Create_RPC_Handler;
-------------------------
-- Destroy_RPC_Handler --
-------------------------
procedure Destroy_RPC_Handler
(Identifier : in out Task_Identifier_Access) is
begin
Deallocated_Tasks := Deallocated_Tasks + 1;
Tasks_Pool_Count := Tasks_Pool_Count - 1;
System.Garlic.Tasking.Free (Identifier.Stop);
Free (Identifier);
end Destroy_RPC_Handler;
-------------------------------
-- Execute_Remote_Subprogram --
-------------------------------
procedure Execute_Remote_Subprogram
(Params : Streams.Params_Stream_Access;
Result : Streams.Params_Stream_Access)
is
Receiver : System.Partition_Interface.RPC_Receiver;
begin
pragma Debug (D ("Job to achieve"));
-- Execute locally remote procedure call. Extract RPC_Receiver
-- of the package and then dereference it.
Receiver := Convert
(System.Address (Interfaces.Unsigned_64'Input (Params)));
Receiver (System.Partition_Interface.Request_Access'(
Params => Params.all'Access, Result => Result.all'Access));
pragma Debug (D ("Job achieved without abortion"));
end Execute_Remote_Subprogram;
--------------
-- Finalize --
--------------
procedure Finalize (Handler : in out Inner_Abort_Handler_Type) is
begin
Finalize
(Handler.Outer.Partition,
Handler.Outer.Waiting,
Session_Type (Handler.Outer.Session));
end Finalize;
----------------
-- Initialize --
----------------
procedure Initialize is
Identifier : Task_Identifier_Access;
Handler : constant System.Garlic.Soft_Links.Abort_Handler_Access
:= new Outer_Abort_Handler_Type;
begin
-- Preallocate a pool of anonymous tasks
System.Garlic.Soft_Links.Enter (Tasks_Pool_Mutex);
while Tasks_Pool_Count < Low_Mark loop
Identifier := Create_RPC_Handler;
Identifier.Next := Idle_Tasks_Queue;
Idle_Tasks_Queue := Identifier;
Idle_Tasks_Count := Idle_Tasks_Count + 1;
end loop;
System.Garlic.Soft_Links.Leave (Tasks_Pool_Mutex);
-- This handler will be finalized. We must initialize its
-- internal values correctly.
Handler.Partition := System.Garlic.Types.Null_Partition_ID;
Handler.Waiting := False;
Handler.Session := Natural (No_Session);
System.Garlic.Soft_Links.Adjust (Handler.all);
System.Garlic.Soft_Links.Register_Abort_Handler (Handler);
end Initialize;
---------------------
-- Show_Tasks_Pool --
---------------------
procedure Show_Tasks_Pool is
begin
pragma Debug (D ("Idle Tasks Count :" & Idle_Tasks_Count'Img));
pragma Debug (D ("Tasks Pool Count :" & Tasks_Pool_Count'Img));
pragma Debug (D ("Allocated Tasks:" & Allocated_Tasks'Img));
pragma Debug (D ("Deallocated Tasks:" & Deallocated_Tasks'Img));
null;
end Show_Tasks_Pool;
--------------
-- Shutdown --
--------------
procedure Shutdown is
begin
pragma Debug (D ("Shutdown called"));
Show_Tasks_Pool;
Terminated := True;
System.Garlic.Soft_Links.Enter (Tasks_Pool_Mutex);
while Idle_Tasks_Queue /= null loop
Idle_Tasks_Queue.Handler.Shutdown;
Idle_Tasks_Queue := Idle_Tasks_Queue.Next;
end loop;
System.Garlic.Soft_Links.Update (Tasks_Pool_Watcher);
System.Garlic.Soft_Links.Leave (Tasks_Pool_Mutex);
end Shutdown;
begin
System.Garlic.Soft_Links.Create (Tasks_Pool_Mutex);
System.Garlic.Soft_Links.Create (Tasks_Pool_Watcher);
Register_Task_Pool
(Allocate_Task => Allocate_Task'Access,
Abort_Task => Abort_Task'Access,
Initialize => Initialize'Access,
Shutdown => Shutdown'Access);
end System.RPC.Server;
|