|
| 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; |
0 commit comments