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
"""
4
Provides a simple atomic variable:
5
6
>>> a = Atomic(0)
7
8
Retrieve the value
9
>>> b = a()
10
Set a new value:
11
>>> a <<= b
12
Get conversion to boolean:
13
>>> if a:
14
do something
15
16
Atomic increment:
17
>>> A += 1
18
19
@author: J.A. de Jong - ASCEE
20
"""
21
from
threading
import
Lock
22
23
24
class
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
lasp.lasp_atomic.Atomic
Implementation of atomic operations on integers and booleans.
Definition
lasp_atomic.py:24
lasp.lasp_atomic.Atomic._val
_val
Definition
lasp_atomic.py:31
lasp.lasp_atomic.Atomic.__iadd__
__iadd__(self, toadd)
Definition
lasp_atomic.py:38
lasp.lasp_atomic.Atomic.__ilshift__
__ilshift__(self, other)
Definition
lasp_atomic.py:54
lasp.lasp_atomic.Atomic.checkType
checkType(self, val)
Definition
lasp_atomic.py:34
lasp.lasp_atomic.Atomic.__bool__
__bool__(self)
Definition
lasp_atomic.py:50
lasp.lasp_atomic.Atomic.__call__
__call__(self)
Definition
lasp_atomic.py:60
lasp.lasp_atomic.Atomic.__isub__
__isub__(self, toadd)
Definition
lasp_atomic.py:44
lasp.lasp_atomic.Atomic.__init__
__init__(self, val)
Definition
lasp_atomic.py:29
lasp.lasp_atomic.Atomic._lock
_lock
Definition
lasp_atomic.py:32
src
lasp
lasp_atomic.py
Generated by
1.9.8