Skip to content

Commit 708af17

Browse files
committed
Editorial change: rename variables
`ID` => `Task_Count` `Task_ID` => `Task_Number`
1 parent ab2e9dc commit 708af17

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

content/courses/advanced-ada/parts/data_types/shared_variable_control.rst

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ Note that :ada:`System.Atomic_Operations.Exchange` is a generic package, so we
577577
have to instantiate it for a specific atomic type |mdash| in this case, the
578578
atomic Boolean :ada:`Lock` type.
579579

580-
.. _Adv_Ada_Package_System_Atomic_Operations_Spinlocks_Task_ID:
580+
.. _Adv_Ada_Package_System_Atomic_Operations_Spinlocks_Task_Number:
581581

582582
We can use multiple tasks to illustrate a situation where using a lock is
583583
important to ensure that no :wikipedia:`race conditions <Race_condition>`
@@ -605,13 +605,13 @@ occur:
605605
use Spinlocks.Lock_Exchange;
606606

607607
procedure Show_Locks is
608-
L : aliased Lock := False;
609-
ID : Integer := 0;
608+
L : aliased Lock := False;
609+
Task_Count : Integer := 0;
610610

611611
task type A_Task;
612612

613613
task body A_Task is
614-
Task_ID : Integer;
614+
Task_Number : Integer;
615615
begin
616616
-- Get the lock
617617
while Atomic_Exchange (Item => L,
@@ -620,13 +620,14 @@ occur:
620620
end loop;
621621

622622
-- At this point, we got the lock.
623-
ID := ID + 1;
624-
Task_ID := ID;
623+
Task_Count := Task_Count + 1;
624+
Task_Number := Task_Count;
625625

626626
-- Release the lock.
627627
L := False;
628628

629-
Put_Line ("Task_ID: " & Task_ID'Image);
629+
Put_Line ("Task_Number: "
630+
& Task_Number'Image);
630631

631632
end A_Task;
632633

@@ -636,10 +637,10 @@ occur:
636637
end Show_Locks;
637638

638639
In this example, we create multiple tasks (:ada:`A`, :ada:`B`, :ada:`C`,
639-
:ada:`D`, :ada:`E`, :ada:`F`) and initialize the :ada:`Task_ID` of each task
640-
based on the value of the :ada:`ID` variable. To avoid multiple tasks accessing
641-
the :ada:`ID` variable at the same time, we use the :ada:`L` lock, which we get
642-
before updating the :ada:`ID`.
640+
:ada:`D`, :ada:`E`, :ada:`F`) and initialize the :ada:`Task_Number` of each task
641+
based on the value of the :ada:`Task_Count` variable. To avoid multiple tasks accessing
642+
the :ada:`Task_Count` variable at the same time, we use the :ada:`L` lock, which we get
643+
before updating the :ada:`Task_Count`.
643644

644645
:ada:`Atomic_Compare_And_Exchange` function
645646
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -740,21 +741,21 @@ Let's use this package in the :ada:`Show_Lazy_Initialization` procedure:
740741
use Lazy_Initialization.Value_Exchange;
741742

742743
procedure Show_Lazy_Initialization is
743-
subtype A_Task_ID is Natural;
744+
subtype A_Task_Number is Natural;
744745

745746
Value : aliased Lazy_Value;
746-
Value_Modified_By : A_Task_ID := 0;
747+
Value_Modified_By : A_Task_Number := 0;
747748

748749
task type A_Task is
749-
entry Start (ID : A_Task_ID);
750+
entry Start (This : A_Task_Number);
750751
entry Stop;
751752
end A_Task;
752753

753754
task body A_Task is
754-
Task_ID : A_Task_ID;
755+
Task_Number : A_Task_Number;
755756
begin
756-
accept Start (ID : A_Task_ID) do
757-
Task_ID := ID;
757+
accept Start (This : A_Task_Number) do
758+
Task_Number := This;
758759
end Start;
759760

760761
Sleep_Some_Time : declare
@@ -787,17 +788,17 @@ Let's use this package in the :ada:`Show_Lazy_Initialization` procedure:
787788
Prior => Prior,
788789
Desired => Lazy_Value (Initial_Value))
789790
then
790-
Value_Modified_By := Task_ID;
791+
Value_Modified_By := Task_Number;
791792
end if;
792793

793794
end Generate_Value;
794795

795796
accept Stop do
796-
Put_Line ("Current task ID: "
797-
& Task_ID'Image);
797+
Put_Line ("Current task number: "
798+
& Task_Number'Image);
798799
Put_Line ("Value: "
799800
& Value'Image);
800-
Put_Line ("Modified by task ID: "
801+
Put_Line ("Modified by task number: "
801802
& Value_Modified_By'Image);
802803
Put_Line ("---------------------");
803804
end Stop;
@@ -816,7 +817,7 @@ Let's use this package in the :ada:`Show_Lazy_Initialization` procedure:
816817
In the :ada:`Show_Lazy_Initialization` procedure, the most important variable
817818
is :ada:`Value`, which is the variable we have to protect via a lock. In
818819
addition, we have the auxiliary :ada:`Value_Modified_By` variable, which
819-
indicates the ID of the task that initialized the :ada:`Value` variable.
820+
indicates the number of the task that initialized the :ada:`Value` variable.
820821

821822
In this procedure, we also see two main
822823
:ref:`block statements <Adv_Ada_Block_Statements>`:
@@ -890,27 +891,28 @@ we've seen before:
890891
with Ada.Text_IO; use Ada.Text_IO;
891892

892893
procedure Show_Test_And_Set is
893-
Lock : aliased Test_And_Set_Flag;
894-
ID : Integer := 0;
894+
Lock : aliased Test_And_Set_Flag;
895+
Task_Count : Integer := 0;
895896

896897
task type A_Task;
897898

898899
task body A_Task is
899-
Task_ID : Integer;
900+
Task_Number : Integer;
900901
begin
901902
-- Get the lock
902903
while Atomic_Test_And_Set (Lock) loop
903904
null;
904905
end loop;
905906

906907
-- At this point, we got the lock.
907-
ID := ID + 1;
908-
Task_ID := ID;
908+
Task_Count := Task_Count + 1;
909+
Task_Number := Task_Count;
909910

910911
-- Release the lock.
911912
Atomic_Clear (Lock);
912913

913-
Put_Line ("Task_ID: " & Task_ID'Image);
914+
Put_Line ("Task_Number: "
915+
& Task_Number'Image);
914916

915917
end A_Task;
916918

@@ -920,7 +922,7 @@ we've seen before:
920922
end Show_Test_And_Set;
921923

922924
Here, we call :ada:`Atomic_Test_And_Set` in a loop until it returns
923-
:ada:`True`. Then, we update the :ada:`ID` and :ada:`Task_ID`. When we're
925+
:ada:`True`. Then, we update the :ada:`Task_Count` and :ada:`Task_Number`. When we're
924926
finished, we call the :ada:`Atomic_Clear` procedure to release the lock.
925927

926928
.. todo::
@@ -998,11 +1000,11 @@ following operations atomically:
9981000
return Old_Item;
9991001
end Atomic_Fetch_And_Subtract;
10001002
1001-
.. _Adv_Ada_Package_System_Integer_Arithmetic_Task_ID:
1003+
.. _Adv_Ada_Package_System_Integer_Arithmetic_Task_Number:
10021004

10031005
Let's reuse a
1004-
:ref:`previous code example <Adv_Ada_Package_System_Atomic_Operations_Spinlocks_Task_ID>`
1005-
that sets a unique ID for each task. In this case, instead of using locks, we
1006+
:ref:`previous code example <Adv_Ada_Package_System_Atomic_Operations_Spinlocks_Task_Number>`
1007+
that sets a unique number for each task. In this case, instead of using locks, we
10061008
use the atomic operations from the
10071009
:ada:`System.Atomic_Operations.Integer_Arithmetic` package:
10081010

@@ -1030,16 +1032,18 @@ use the atomic operations from the
10301032
use Atomic_Integers.Atomic_Integer_Arithmetic;
10311033

10321034
procedure Show_Atomic_Integers is
1033-
ID : aliased Atomic_Integer := 0;
1035+
Task_Count : aliased Atomic_Integer := 0;
10341036

10351037
task type A_Task;
10361038

10371039
task body A_Task is
1038-
Task_ID : Atomic_Integer;
1040+
Task_Number : Atomic_Integer;
10391041
begin
1040-
Task_ID := Atomic_Fetch_And_Add (ID, 1);
1042+
Task_Number :=
1043+
Atomic_Fetch_And_Add (Task_Count, 1);
10411044

1042-
Put_Line ("Task_ID: " & Task_ID'Image);
1045+
Put_Line ("Task_Number: "
1046+
& Task_Number'Image);
10431047

10441048
end A_Task;
10451049

@@ -1049,7 +1053,7 @@ use the atomic operations from the
10491053
end Show_Atomic_Integers;
10501054

10511055
In this example, we call the :ada:`Atomic_Fetch_And_Add` function to update the
1052-
:ada:`ID` variable and, at the same time, initialize the :ada:`Task_ID`
1056+
:ada:`Task_Count` variable and, at the same time, initialize the :ada:`Task_Number`
10531057
variable of the current task.
10541058

10551059

@@ -1069,7 +1073,7 @@ fact, it provides the same operations: the procedures :ada:`Atomic_Add` and
10691073
modular types instead of integer types.
10701074

10711075
Let's reuse the
1072-
:ref:`previous code example <Adv_Ada_Package_System_Integer_Arithmetic_Task_ID>`,
1076+
:ref:`previous code example <Adv_Ada_Package_System_Integer_Arithmetic_Task_Number>`,
10731077
but replace the atomic integer type by an atomic modular type:
10741078

10751079
.. code:: ada run_button project=Courses.Advanced_Ada.Data_Types.Shared_Variable_Control.Atomic_Operations.Modular_Arithmetic
@@ -1096,16 +1100,18 @@ but replace the atomic integer type by an atomic modular type:
10961100
use Atomic_Modulars.Atomic_Modular_Arithmetic;
10971101

10981102
procedure Show_Atomic_Modulars is
1099-
ID : aliased Atomic_Modular := 0;
1103+
Task_Count : aliased Atomic_Modular := 0;
11001104

11011105
task type A_Task;
11021106

11031107
task body A_Task is
1104-
Task_ID : Atomic_Modular;
1108+
Task_Number : Atomic_Modular;
11051109
begin
1106-
Task_ID := Atomic_Fetch_And_Add (ID, 1);
1110+
Task_Number :=
1111+
Atomic_Fetch_And_Add (Task_Count, 1);
11071112

1108-
Put_Line ("Task_ID: " & Task_ID'Image);
1113+
Put_Line ("Task_Number: "
1114+
& Task_Number'Image);
11091115

11101116
end A_Task;
11111117

@@ -1115,8 +1121,8 @@ but replace the atomic integer type by an atomic modular type:
11151121
end Show_Atomic_Modulars;
11161122

11171123
As we did in the previous example, we again call the
1118-
:ada:`Atomic_Fetch_And_Add` function to update the :ada:`ID` variable and, at
1119-
the same time, initialize the :ada:`Task_ID` variable of the current task. The
1124+
:ada:`Atomic_Fetch_And_Add` function to update the :ada:`Task_Count` variable and, at
1125+
the same time, initialize the :ada:`Task_Number` variable of the current task. The
11201126
only difference is that we use a modular type (:ada:`Atomic_Modular`).
11211127

11221128
.. admonition:: Relevant topics

0 commit comments

Comments
 (0)