Skip to content

Commit 1bfa458

Browse files
committed
tasking: advanced examples
1 parent e07ca65 commit 1bfa458

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

courses/fundamentals_of_ada/240_tasking/05-task_types_in_depth.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,40 @@ Protected Object Entries
158158
...
159159
end Object;
160160
161+
-------------------------------------
162+
Discriminated Protected or Task types
163+
-------------------------------------
164+
165+
* Discriminant can be an :ada:`access` or discrete type
166+
* Resulting type is indefinite
167+
168+
- Unless mutable
169+
170+
* Example: counter shared between tasks
171+
172+
.. code:: Ada
173+
174+
protected type Counter_T is
175+
procedure Increment;
176+
end Counter_T
177+
178+
task type My_Task (Counter : not null access Counter_T) is [...]
179+
180+
task body My_Task is
181+
begin
182+
Counter.Increment;
183+
[...]
184+
185+
----------------------------------------
186+
Using discriminant for Real-Time aspects
187+
----------------------------------------
188+
189+
.. code:: Ada
190+
191+
protected type Protected_With_Priority (Prio : System.Priority)
192+
with Priority => Prio
193+
is
194+
161195
------------------------------------------
162196
Example: Protected Objects - Declaration
163197
------------------------------------------
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
====================
2+
Task Safe Interfaces
3+
====================
4+
5+
-----------------
6+
Problem Statement
7+
-----------------
8+
9+
* Designing task-safe code requires using dedicated constructs
10+
* How to reuse the components?
11+
* How to refactor task-unsafe code into task-safe version?
12+
13+
----------------
14+
Access Protected
15+
----------------
16+
17+
* Access to :ada:`protected` objects' subprograms
18+
* :ada:`type P is access protected procedure (args...)`
19+
* :ada:`type F is access protected function (args...) return ...`
20+
21+
.. code:: Ada
22+
23+
type Work_Id is tagged limited private;
24+
25+
type Work_Handler is
26+
access protected procedure (T : Work_Id);
27+
28+
----------------------
29+
Synchronized Interface
30+
----------------------
31+
32+
* :ada:`synchronized interface` can be inherited by :ada:`task`/:ada:`protected` types
33+
34+
.. code:: Ada
35+
36+
type Counter_I is synchronized interface;
37+
procedure Increment (Counter : in out Counter_I) is abstract;
38+
39+
task type Counter_Task_T is new Counter_I with
40+
-- Always implemented as an entry for tasks
41+
entry Increment;
42+
end task;
43+
44+
protected type Counter_Prot_T is new Counter_I with
45+
procedure Increment;
46+
end Counter_Prot_T;
47+
48+
* Also present:
49+
50+
- :ada:`task interface` meant for tasks only
51+
- :ada:`protected interface` meant for protected types only
52+
53+
.. warning::
54+
55+
Only available in **full-tasking** runtimes
56+
57+
---------------------------------
58+
Standard Library Queues Interface
59+
---------------------------------
60+
61+
* In :ada:`Ada.Containers`
62+
* :ada:`Synchronized_Queue_Interfaces` interface
63+
64+
.. tip::
65+
66+
Provides a portable interface
67+
68+
.. code:: Ada
69+
70+
generic
71+
type Element_Type is private;
72+
package Ada.Containers.Synchronized_Queue_Interfaces is
73+
type Queue is synchronized interface;
74+
75+
---------------------------------------
76+
Standard Library Queues Implementations
77+
---------------------------------------
78+
79+
* Four implementations
80+
81+
.. tip::
82+
83+
Better than rolling-out one's own queue implementation
84+
85+
* Synchronized implementations
86+
87+
- :ada:`Unbounded_Synchronized_Queues`
88+
- :ada:`Bounded_Synchronized_Queues`
89+
- As :ada:`protected` types
90+
- With priority ceiling
91+
92+
* Priority implementations
93+
94+
- :ada:`Unbounded_Priority_Queues`
95+
- :ada:`Bounded_Priotiry_Queues`
96+
- As :ada:`protected` types
97+
- Elements provide :ada:`Get_Priority`
98+
99+
+ Used for sorting elements
100+
101+
----------------------------
102+
Example: Scheduler Interface
103+
----------------------------
104+
105+
.. code:: Ada
106+
107+
type Scheduler_I;
108+
type Maybe_Work_Item_I is access protected procedure;
109+
type Work_Item_I is not null access protected procedure;
110+
111+
type Scheduler_I is synchronized interface;
112+
procedure Queue (S : in out Scheduler_I; W : Work_Item_I) is abstract;
113+
procedure Execute_Next (S : in out Scheduler_I) is abstract;
114+
115+
type Work_Items_Array is array (Positive range <>)
116+
of Maybe_Work_Item_I;
117+
118+
protected type Scheduler_T (Size : Positive) is new Scheduler_I with
119+
procedure Queue (W : Work_Item_I);
120+
entry Execute_Next;
121+
private
122+
Number_Of_Items : Natural := 0;
123+
Items : Work_Items_Array (1 .. Size);
124+
end Scheduler_T;
125+
126+
-------------------------
127+
Example: Scheduler (Body)
128+
-------------------------
129+
130+
.. code:: Ada
131+
132+
protected body Scheduler_T is
133+
procedure Queue (W : Work_Item_I) is
134+
begin
135+
Number_Of_Items := Number_Of_Items + 1;
136+
Items (Number_Of_Items) := Maybe_Work_Item_I (W);
137+
end Queue;
138+
139+
entry Execute_Next
140+
when Number_Of_Items > 0
141+
is
142+
W : Work_Item_I := Work_Item_I (Items (Number_Of_Items));
143+
begin
144+
Number_Of_Items := Number_Of_Items - 1;
145+
W.all;
146+
end Execute_Next;
147+
end Scheduler_T;

courses/fundamentals_of_ada/700_expert_resource_management.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ Expert Resource Management
4040
.. include:: 140_access_types/11-idiom_constant_pointer.rst
4141
.. include:: 260_controlled_types/10-idiom_refcounting.rst
4242
.. include:: 240_tasking/21-gnat_semaphores.rst
43+
.. include:: 240_tasking/22-task_safe_interfaces.rst

0 commit comments

Comments
 (0)