-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase.py
More file actions
112 lines (97 loc) · 3.09 KB
/
base.py
File metadata and controls
112 lines (97 loc) · 3.09 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#base.py
"""
The most rudimentary helpers for stackless, that don't depend on
other stacklesslib functionality. Mostly context managers
"""
import sys
import time
from contextlib import contextmanager
import stackless
__all__ = ["time", "atomic", "block_trap", "ignore_nesting", "switch_trap"]
# Get the best wallclock time to use.
if hasattr(time, "monotonic"):
time = time.monotonic
else:
if sys.platform == "win32":
time = time.clock
else:
# Time.clock reports CPU time on unix, not good.
time = time.time
try:
# New versions of stackless have a fast binary implementation of this
from stackless import atomic
except ImportError:
@contextmanager
def atomic():
"""a context manager to make the tasklet atomic for the duration"""
c = stackless.getcurrent()
old = c.set_atomic(True)
try:
yield
finally:
c.set_atomic(old)
@contextmanager
def block_trap(trap=True):
"""
A context manager to temporarily set the block trap state of the
current tasklet. Defaults to setting it to True
"""
c = stackless.getcurrent()
old = c.block_trap
c.block_trap = trap
try:
yield
finally:
c.block_trap = old
@contextmanager
def ignore_nesting(flag=True):
"""
A context manager which allows the current tasklet to engage the
ignoring of nesting levels. By default pre-emptive switching can
only happen at the top nesting level, setting this allows it to
happen at all nesting levels. Defaults to setting it to True.
"""
c = stackless.getcurrent()
old = c.set_ignore_nesting(flag)
try:
yield
finally:
c.set_ignore_nesting(old)
def switch_trap():
"""
A context manager to temporarily increase the switch-trap level of
the current thread.
"""
stackless.switch_trap(1)
try:
yield
finally:
stackless.switch_trap(-1)
class SignalChannel(stackless.channel):
"""
A subclass of channel that is used to implement more complex entities.
It has sender preference, so that a ``signal()`` call won't block.
"""
__slots__ = []
def __init__(self):
self.preference = 1
def signal(self, value=None):
"""send(value) if someone is waiting"""
with atomic():
if self.balance < 0:
self.send(value)
def signal_all(self, value=None):
"""send(value) for every waiting tasklet"""
with atomic():
for i in xrange(-self.balance):
assert self.balance < 0
self.send(value)
def asignal(self):
"""send(None) if someone is waiting. Should be called while in an atomic state."""
if self.balance < 0:
self.send(None)
def asignal_all(self):
"""send(None) for every waiting tasklet. Should be called while in an atomic state."""
for i in xrange(-self.balance):
assert self.balance < 0
self.send(None)