LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_atomic.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Provides a simple atomic variable:
5
6>>> a = Atomic(0)
7
8Retrieve the value
9>>> b = a()
10Set a new value:
11>>> a <<= b
12Get conversion to boolean:
13>>> if a:
14 do something
15
16Atomic increment:
17>>> A += 1
18
19@author: J.A. de Jong - ASCEE
20"""
21from threading import Lock
22
23
24class Atomic:
25 """
26 Implementation of atomic operations on integers and booleans.
27
28 """
29 def __init__(self, val):
30 self.checkType(val)
31 self._val = val
32 self._lock = Lock()
33
34 def checkType(self, val):
35 if not (type(val) == bool or type(val) == int):
36 raise RuntimeError("Invalid type for Atomic")
37
38 def __iadd__(self, toadd):
39 self.checkType(toadd)
40 with self._lock:
41 self._val += toadd
42 return self
43
44 def __isub__(self, toadd):
45 self.checkType(toadd)
46 with self._lock:
47 self._val -= toadd
48 return self
49
50 def __bool__(self):
51 with self._lock:
52 return self._val
53
54 def __ilshift__(self, other):
55 self.checkType(other)
56 with self._lock:
57 self._val = other
58 return self
59
60 def __call__(self):
61 with self._lock:
62 return self._val
Implementation of atomic operations on integers and booleans.
__ilshift__(self, other)