github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/sawtooth_validator/concurrent/atomic.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  from threading import Lock
    17  from threading import RLock
    18  
    19  
    20  class Counter:
    21      def __init__(self, initial_value=0):
    22          self._value = initial_value
    23          self._lock = Lock()
    24  
    25      def get(self):
    26          with self._lock:
    27              return self._value
    28  
    29      def get_and_inc(self, step=1):
    30          with self._lock:
    31              ret_value = self._value
    32              self._value += step
    33  
    34          return ret_value
    35  
    36      def get_and_dec(self, step=1):
    37          with self._lock:
    38              ret_value = self._value
    39              self._value -= step
    40  
    41          return ret_value
    42  
    43      def inc(self, step=1):
    44          with self._lock:
    45              self._value += step
    46  
    47      def dec(self, step=1):
    48          with self._lock:
    49              self._value -= step
    50  
    51  
    52  class ConcurrentSet:
    53      def __init__(self):
    54          self._set = set()
    55          self._lock = RLock()
    56  
    57      def add(self, element):
    58          with self._lock:
    59              self._set.add(element)
    60  
    61      def remove(self, element):
    62          with self._lock:
    63              self._set.remove(element)
    64  
    65      def __contains__(self, element):
    66          with self._lock:
    67              return element in self._set
    68  
    69  
    70  class ConcurrentMultiMap:
    71      """A dictionary that maps keys to lists of items. All methods are
    72      modifications to the referenced list."""
    73      def __init__(self):
    74          self._dict = dict()
    75          self._lock = RLock()
    76  
    77      def __contains__(self, key):
    78          with self._lock:
    79              return key in self._dict
    80  
    81      def append(self, key, item):
    82          """Append item to the list at key. Creates the list at key if it
    83          doesn't exist.
    84          """
    85          with self._lock:
    86              if key in self._dict:
    87                  self._dict[key].append(item)
    88              else:
    89                  self._dict[key] = [item]
    90  
    91      def set(self, key, items):
    92          """Set key to a copy of items"""
    93          if not isinstance(items, list):
    94              raise ValueError("items must be a list")
    95          with self._lock:
    96              self._dict[key] = items.copy()
    97  
    98      def swap(self, key, items):
    99          """Set key to a copy of items and return the list that was previously
   100          stored if the key was set. If not key was set, returns an empty list.
   101          """
   102          if not isinstance(items, list):
   103              raise ValueError("items must be a list")
   104          return_value = []
   105          with self._lock:
   106              if key in self._dict:
   107                  return_value = self._dict[key]
   108              # Make a copy since we don't want users keeping a reference that is
   109              # outside the lock
   110              self._dict[key] = items.copy()
   111          return return_value
   112  
   113      def pop(self, key, default):
   114          """If the key is set, remove and return the list stored at key.
   115          Otherwise return default."""
   116          with self._lock:
   117              return self._dict.pop(key, default)
   118  
   119      def get(self, key, default):
   120          """If the key is set, return a copy of the list stored at key.
   121          Otherwise return default."""
   122          with self._lock:
   123              try:
   124                  return self._dict[key].copy()
   125              except KeyError:
   126                  return default