Thu, 25 Jan 2018 22:44:10 +0000
README Non-MacOS description fix
|
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
|
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Repository abstraction for queuing |
|
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # |
|
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
| 54 | 5 | import weakref |
| 64 | 6 | import borgend |
| 54 | 7 | from scheduler import QueueThread, QueuedEvent |
| 8 | ||
| 64 | 9 | logger=borgend.logger.getChild(__name__) |
| 10 | ||
| 54 | 11 | class FIFOEvent(QueuedEvent): |
| 12 | def __init__(self, cond, name=None): | |
| 13 | self._goodtogo=False | |
| 14 | super().__init__(cond, name=name) | |
| 15 | ||
| 16 | def __lt__(self, other): | |
| 64 | 17 | return False |
| 54 | 18 | |
| 19 | class FIFO(QueueThread): | |
| 20 | def __init__(self, **kwargs): | |
| 21 | super().__init__(target = self._fifo_thread, **kwargs) | |
| 22 | ||
| 23 | def _fifo_thread(self): | |
| 24 | with self._cond: | |
| 25 | while not self._terminate: | |
| 26 | ev=self._list | |
| 27 | if ev: | |
| 28 | # We can only remove ev from the list when ev.cond allows | |
| 29 | with ev.cond: | |
| 64 | 30 | if not ev._goodtogo: |
| 31 | ev._goodtogo=True | |
| 32 | ev.cond.notifyAll() | |
| 54 | 33 | self._cond.wait() |
| 34 | ||
| 35 | # Termination cleanup | |
| 36 | ev=self._list | |
| 37 | while ev: | |
| 38 | # We can only remove ev from the list when ev.cond allows | |
| 39 | with ev.cond: | |
| 40 | ev.cond.notifyAll() | |
| 41 | ev=ev.next | |
| 42 | ||
| 43 | # cond has to be acquired on entry! | |
| 44 | def queue_action(self, cond, action=lambda: (), name=None): | |
| 45 | ev=FIFOEvent(cond, name=name) | |
|
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | |
| 54 | 47 | with self._cond: |
| 48 | self._insert(ev) | |
| 49 | ||
| 64 | 50 | # This will release the lock on cond, allowing queue manager (scheduler) |
| 51 | # thread to notify us if we are already to be released | |
| 52 | logger.debug("%s:Queuing %s", self.name, ev.name or 'UNKNOWN') | |
| 53 | ev.cond.wait() | |
| 54 | 54 | |
| 55 | try: | |
| 64 | 56 | if ev._goodtogo: |
| 57 | logger.debug("%s:Executing %s", self.name, ev.name or 'UNKNOWN') | |
| 58 | # | |
| 59 | # TODO: action() has to unlink on finish; so should maybe | |
| 60 | # use weak references to event. | |
| 61 | # Or we have to make action take all the time, so make the | |
| 62 | # stdout thread. | |
| 63 | # OR: Easiest to just move finish-waiting into __launch_check | |
| 64 | # instead of at the outer level of the main loop. | |
| 65 | # | |
| 54 | 66 | action() |
| 67 | finally: | |
| 68 | with self._cond: | |
| 69 | self._unlink(ev) | |
| 70 | # Let _fifo_thread proceed to next action | |
| 71 | self._cond.notify() | |
| 72 | ||
| 64 | 73 | return ev._goodtogo |
| 74 | ||
| 54 | 75 | class Repository(FIFO): |
| 76 | def __init__(self, name): | |
| 77 | super().__init__(name = 'RepositoryThread %s' % name) | |
| 78 | self.repository_name=name | |
| 79 | ||
| 80 | ||
| 81 | # TODO: Should use weak references but they give KeyError | |
| 82 | repositories=weakref.WeakValueDictionary() | |
| 83 | ||
| 84 | def get_controller(name): | |
| 85 | if name in repositories: | |
| 86 | repo = repositories[name] | |
| 87 | else: | |
| 88 | repo = Repository(name) | |
| 89 | repo.start() | |
| 90 | repositories[name] = repo | |
| 91 | return repo | |
| 92 |