utilada_aws_2.5.0_f65f9ba9/src/core/concurrent/asm-x86/util-concurrent-counters.adb

 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
-----------------------------------------------------------------------
--  Util.Concurrent -- Concurrent Counters
--  Copyright (C) 2009, 2010 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

--  This implementation of atomic counters works only for Intel x86 based
--  platforms.  It uses the <b>lock</b> instruction followed by an <b>incl</b>
--  or a <b>decl</b> instruction to implement the atomic operations.
--  (See GNU/Linux atomic.h headers).
with System.Machine_Code;
package body Util.Concurrent.Counters is

   use System.Machine_Code;
   use Interfaces;

   --  ------------------------------
   --  Increment the counter atomically.
   --  ------------------------------
   procedure Increment (C : in out Counter) is
   begin
      Asm (Template => "lock incl %0",
           Volatile => True,
           Outputs  => Unsigned_32'Asm_Output ("+m", C.Value),
           Clobber  => "memory");
   end Increment;

   --  ------------------------------
   --  Increment the counter atomically and return the value before increment.
   --  ------------------------------
   procedure Increment (C     : in out Counter;
                        Value : out Integer) is
      Val : Unsigned_32 := 1;
   begin
      Asm (Template => "lock xaddl %1,%0",
           Volatile => True,
           Outputs  => (Unsigned_32'Asm_Output ("+m", C.Value),
                        Unsigned_32'Asm_Output ("=r", Val)),
           Inputs   => Unsigned_32'Asm_Input ("1", Val),
           Clobber  => "memory");
      Value := Integer (Val);
   end Increment;

   --  ------------------------------
   --  Decrement the counter atomically.
   --  ------------------------------
   procedure Decrement (C : in out Counter) is
   begin
      Asm (Template => "lock decl %0",
           Volatile => True,
           Outputs  => Unsigned_32'Asm_Output ("+m", C.Value),
           Clobber  => "memory");
   end Decrement;

   --  ------------------------------
   --  Decrement the counter atomically and return a status.
   --  ------------------------------
   procedure Decrement (C : in out Counter;
                        Is_Zero : out Boolean) is
      Result : Unsigned_8;
   begin
      Asm ("lock decl %0; sete %1",
           Volatile => True,
           Outputs => (Unsigned_32'Asm_Output ("+m", C.Value),
                       Unsigned_8'Asm_Output ("=qm", Result)));
      Is_Zero := Result /= 0;
   end Decrement;

   --  ------------------------------
   --  Get the counter value
   --  ------------------------------
   function Value (C : in Counter) return Integer is
   begin
      --  On x86, reading the counter is atomic.
      return Integer (C.Value);
   end Value;

end Util.Concurrent.Counters;