tav (owner)

Revisions

gist: 228049 Download_button fork
public
Public Clone URL: git://gist.github.com/228049.git
Embed All Files: show embed
stm.py #
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
"""Software Transactional Memory and Observers"""
 
import heapq
import sys
import threading
import weakref
 
from utils import Max
 
__all__ = [
    'STMHistory', 'AbstractSubject', 'Link', 'AbstractListener', 'Controller',
    'CircularityError', 'LocalController',
]
 
 
class CircularityError(Exception):
    """Rules arranged in an infinite loop"""
 
 
class AbstractSubject(object):
    """Abstract base for objects that can be linked via ``Link`` objects"""
 
    __slots__ = ()
 
    manager = None
    layer = 0
 
    def __init__(self):
        self.next_listener = None
 
    def iter_listeners(self):
        """Yield the listeners of this subject"""
        link = self.next_listener
        while link is not None:
            nxt = link.next_listener # avoid unlinks breaking iteration
            ob = link()
            if ob is not None:
                yield ob
            link = nxt
 
class AbstractListener(object):
    """Abstract base for objects that can be linked via ``Link`` objects"""
 
    __slots__ = ()
    layer = 0
 
    def __init__(self):
        self.next_subject = None
 
    def iter_subjects(self):
        """Yield the listeners of this subject"""
        link = self.next_subject
        while link is not None:
            nxt = link.next_subject # avoid unlinks breaking iteration
            if link.subject is not None:
                yield link.subject
            link = nxt
 
    def dirty(self):
        """Mark the listener dirty and query whether it should be scheduled
 
If a true value is returned, the listener should be scheduled. Note
that this method is allowed to have side-effects, but must be
idempotent.
"""
        return True
 
    def run(self):
        """Take whatever action the listener is supposed to take"""
        raise NotImplementedError
 
 
 
link_base = weakref.ref
 
class Link(link_base):
    """Dependency link"""
    __slots__ = [
        'subject','next_subject','prev_subject','next_listener','prev_listener'
    ]
 
    def __new__(cls, subject, listener):
        self = link_base.__new__(Link, listener, _unlink_fn)
        self.subject = self.prev_listener = subject
        self.prev_subject = None # listener link is via weak ref
        nxt = self.next_subject = listener.next_subject
        if nxt is not None:
            nxt.prev_subject = self
        nxt = self.next_listener = subject.next_listener
        if nxt is not None:
            nxt.prev_listener = self
        listener.next_subject = self
        subject.next_listener = self
        return self
 
    def unlink(self):
        """Deactivate the link and remove it from its lists"""
        nxt = self.next_listener
        prev = self.prev_listener
        if nxt is not None:
            nxt.prev_listener = prev
        if prev is not None and prev.next_listener is self:
            prev.next_listener = nxt
        prev = self.prev_subject
        nxt = self.next_subject
        if nxt is not None:
            nxt.prev_subject = prev
        if prev is None:
            prev = self() # get head of list
        if prev is not None and prev.next_subject is self:
            prev.next_subject = nxt
        self.subject = self.next_subject = self.prev_subject = None
        self.next_listener = self.prev_listener = None
 
_unlink_fn = Link.unlink
 
class STMHistory(object):
    """Simple STM implementation using undo logging and context managers"""
 
    active = in_cleanup = undoing = False
 
    def __init__(self):
        self.undo = [] # [(func,args), ...]
        self.at_commit =[] # [(func,args), ...]
        self.managers = {} # [mgr]->seq # (context managers to __exit__ with)
 
    def atomically(self, func=lambda:None, *args, **kw):
        """Invoke ``func(*args,**kw)`` atomically"""
        if self.active:
            return func(*args, **kw)
        self.active = True
        try:
            try:
                retval = func(*args, **kw)
                self.cleanup()
                return retval
            except:
                self.cleanup(*sys.exc_info())
        finally:
            self.active = False
 
    def manage(self, mgr):
        assert self.active, "Can't manage without active history"
        if mgr not in self.managers:
            mgr.__enter__()
            self.managers[mgr] = len(self.managers)
 
    def on_undo(self, func, *args):
        """Call `func(*args)` if atomic operation is undone"""
        assert self.active, "Can't record undo without active history"
        if not self.undoing: self.undo.append((func, args))
 
    def savepoint(self):
        """Get a savepoint suitable for calling ``rollback_to()``"""
        return len(self.undo)
 
 
    def cleanup(self, typ=None, val=None, tb=None):
        # Exit the processing loop, unwinding managers
        assert self.active, "Can't exit when inactive"
        assert not self.in_cleanup, "Can't invoke cleanup while in cleanup"
        self.in_cleanup = True
 
        if typ is None:
            try:
                self.checkpoint()
            except:
                typ, val, tb = sys.exc_info()
 
        if typ is not None:
            try:
                self.rollback_to(0)
            except:
                typ, val, tb = sys.exc_info()
 
        managers = [(posn,mgr) for (mgr, posn) in self.managers.items()]
        managers.sort()
        self.managers.clear()
        try:
            while managers:
                try:
                    managers.pop()[1].__exit__(typ, val, tb)
                except:
                    typ, val, tb = sys.exc_info()
            if typ is not None:
                raise typ, val, tb
        finally:
            del self.at_commit[:], self.undo[:]
            self.in_cleanup = False
            typ = val = tb = None
 
    def change_attr(self, ob, attr, val):
        """Set `ob.attr` to `val`, w/undo log to restore the previous value"""
        self.on_undo(setattr, ob, attr, getattr(ob, attr))
        setattr(ob, attr, val)
 
 
 
    def rollback_to(self, sp=0):
        """Rollback to the specified savepoint"""
        assert self.active, "Can't rollback without active history"
        undo = self.undo
        self.undoing = True
        rb = self.rollback_to
        try:
            while len(undo) > sp:
                f, a = undo.pop()
                if f==rb and a:
                    sp = min(sp, a[0])
                else:
                    f(*a)
        finally:
            self.undoing = False
 
    def on_commit(self, func, *args):
        """Call `func(*args)` if atomic operation is committed"""
        assert self.active, "Not in an atomic operation"
        s = slice(len(self.at_commit), None)
        self.undo.append((self.at_commit.__delitem__, (s,)))
        self.at_commit.append((func, args))
 
    def checkpoint(self):
        """Invoke actions registered w/``on_commit()``, and clear the queue"""
        for (f,a) in self.at_commit:
            f(*a)
        del self.at_commit[:]
 
 
 
 
 
 
 
 
 
 
 
 
 
class Controller(STMHistory):
    """STM History with support for subjects, listeners, and queueing"""
    current_listener = destinations = routes = newcells = None
    readonly = False
 
    def __init__(self):
        super(Controller, self).__init__()
        self.reads = {}
        self.writes = {}
        self.has_run = {} # listeners that have run
        self.layers = [] # heap of layer numbers
        self.queues = {} # [layer] -> dict of listeners to be run
        self.to_retry = {}
 
    def checkpoint(self):
        self.has_run.clear()
        return super(Controller, self).checkpoint()
 
    def _retry(self):
        try:
            # undo back through listeners, watching to detect cycles
            self.destinations = set(self.to_retry)
            self.routes = {} # tree of rules that (re)triggered retry targets
            self.rollback_to(min([self.has_run[r] for r in self.to_retry]))
            for item in self.to_retry:
                if item in self.routes:
                    path = check_circularity(item, self.routes)
                    if path: raise CircularityError(self.routes, path)
            else:
                map(self.schedule, self.to_retry)
        finally:
            self.to_retry.clear()
            self.destinations = self.routes = None
 
    def __getattr__(self, name):
        if name=='pulse': # lazy init due to circular dependency
            from peak.events.trellis import Value
            self.pulse = Value(0)
            return self.pulse
        raise AttributeError(name)
 
    def _unrun(self, listener, notified):
        destinations = self.destinations
        if destinations is not None:
            via = destinations.intersection(notified)
            if via:
                self.routes[listener] = via; destinations.add(listener)
 
    def run_rule(self, listener, initialized=True):
        """Run the specified listener"""
        if listener.layer is Max and not self.readonly:
            return self.with_readonly(self.run_rule, listener, initialized)
 
        old = self.current_listener
        self.current_listener = listener
        try:
            assert listener not in self.has_run,"Re-run of rule without retry"
            assert self.active, "Rules must be run atomically"
            if old is not None:
                assert not initialized,"Only un-initialized rules can be nested"
                old_reads, self.reads = self.reads, {}
                try:
                    listener.run()
                    self._process_reads(listener)
                finally:
                    self.reads = old_reads
            else:
                if initialized:
                    self.has_run[listener] = self.savepoint()
                    self.on_undo(self.has_run.pop, listener, None)
 
                try:
                    listener.run()
                    self._process_writes(listener)
                    self._process_reads(listener)
                except:
                    self.reads.clear()
                    self.writes.clear()
                    raise
        finally:
            self.current_listener = old
            
    def _process_writes(self, listener):
        #
        # Remove changed items from self.writes and notify their listeners,
        # and setting up an undo action to track this listener's participation
        # in any cyclic dependency that might occur later.
        #
        notified = {}
        writes = self.writes
        layer = listener.layer
        while writes:
            subject, writer = writes.popitem()
            for dependent in subject.iter_listeners():
                if dependent is not listener:
                    if dependent.dirty():
                        self.schedule(dependent, layer) #, writer
                        notified[dependent] = 1
        if notified:
            self.on_undo(self._unrun, listener, notified)
 
    def _process_reads(self, listener):
        #
        # Remove subjects from self.reads and link them to `listener`
        # (Old subjects of the listener are deleted, and self.reads is cleared
        #
        subjects = self.reads
 
        link = listener.next_subject
        while link is not None:
            nxt = link.next_subject # avoid unlinks breaking iteration
            if link.subject in subjects:
                del subjects[link.subject]
            else:
                self.undo.append((Link, (link.subject, listener)))
                link.unlink()
            link = nxt
 
        while subjects:
            link = Link(subjects.popitem()[0], listener)
            self.undo.append((link.unlink, ()))
 
 
    def schedule(self, listener, source_layer=None):
        """Schedule `listener` to run during an atomic operation
 
If an operation is already in progress, it's immediately scheduled, and
its scheduling is logged in the undo queue (unless it was already
scheduled).
 
If `source_layer` is specified, ensure that the listener belongs to
a higher layer than the source, moving the listener from an existing
queue layer if necessary. (This layer elevation is intentionally
NOT undo-logged, however.)
"""
        new = old = listener.layer
        get = self.queues.get
 
        assert not self.readonly or old is Max, \
            "Shouldn't be scheduling a non-Observer during commit"
 
        if source_layer is not None and source_layer >= listener.layer:
            new = source_layer + 1
 
        if listener in self.has_run:
            self.to_retry[listener]=1
 
        q = get(old)
        if q and listener in q:
            if new is not old:
                self.cancel(listener)
        elif self.active and not self.undoing:
            self.on_undo(self.cancel, listener)
 
        if new is not old:
            listener.layer = new
            q = get(new)
 
        if q is None:
             q = self.queues[new] = {listener:1}
             heapq.heappush(self.layers, new)
        else:
            q[listener] = 1
 
    def cancel(self, listener):
        """Prevent the listener from being recalculated, if applicable"""
        q = self.queues.get(listener.layer)
        if q and listener in q:
            del q[listener]
            if not q:
                del self.queues[listener.layer]
                self.layers.remove(listener.layer)
                self.layers.sort() # preserve heap order
 
    def atomically(self, func=lambda:None, *args, **kw):
        """Invoke ``func(*args,**kw)`` atomically"""
        if self.active:
            return func(*args, **kw)
        return super(Controller,self).atomically(self._process, func, args, kw)
 
    def _process(self, func, args, kw):
        try:
            retval = func(*args, **kw)
            layers = self.layers
            queues = self.queues
            while layers or self.at_commit:
                self.pulse.value += 1
                while layers:
                    if self.to_retry:
                        self._retry()
                    q = queues[layers[0]]
                    if q:
                        listener = q.popitem()[0]
                        self.on_undo(self.schedule, listener)
                        self.run_rule(listener)
                    else:
                        del queues[layers[0]]
                        heapq.heappop(layers)
                self.checkpoint()
            return retval
        except:
            del self.layers[:]
            self.queues.clear()
            raise
 
    def lock(self, subject):
        assert self.active, "Subjects must be accessed atomically"
        manager = subject.manager
        if manager is not None and manager not in self.managers:
            self.manage(manager)
 
    def used(self, subject):
        self.lock(subject)
        cl = self.current_listener
        if cl is not None and subject not in self.reads:
            self.reads[subject] = 1
            if cl is not subject and subject.layer >= cl.layer:
                cl.layer = subject.layer + 1
 
    def changed(self, subject):
        self.lock(subject)
        cl = self.current_listener
        if self.readonly and subject is not cl and (
            self.newcells is None or subject not in self.newcells
        ):
            raise RuntimeError("Can't change objects during @perform or @compute")
        if cl is not None:
            self.writes[subject] = cl
        else:
            for listener in subject.iter_listeners():
                if listener.dirty():
                    self.schedule(listener)
 
    def initialize(self, listener):
        self.run_rule(listener, False)
 
    def with_readonly(self, func, *args):
        if self.readonly:
            return func(*args)
        else:
            self.readonly = True
            try: return func(*args)
            finally: self.readonly = False
 
 
 
    def new_cell(self, cell):
        if self.newcells is not None:
            self.newcells[cell] = 1
 
    def with_new(self, func, *args, **kw):
        old = self.newcells
        if old is None:
            self.newcells = {}
        try:
            return func(*args, **kw)
        finally:
            self.newcells = old
 
 
def check_circularity(item, routes, start=None, seen=None):
    """Detect CircularityError, if applicable"""
    if seen is None: seen = {}
    if start is None: start = item
    for via in routes.get(item, ()):
        if via is start:
            return (via,)
        elif via not in seen:
            seen[via] = 1
            path = check_circularity(via, routes, start, seen)
            if path:
                return (via,) + path
    return ()
 
class LocalController(Controller, threading.local):
    """Thread-local Controller"""