Sun, 04 Feb 2018 01:27:38 +0000
Part 2 of handling macOS sleep/wake signal brokenness.
a) Added Time.horizon(), which indicates how far the Time event is from the
epoch, in monotonic time. For DreamTime, if the system is sleeping, this
returns ∞. b) The DreamTime monitor also signals sleep to callbacks, so
that the Scheduler can re-sort the events. The sorting is now done by
the horizon, so DreamTime events will be moved last and not activated when
the system is sleeping or "sleeping", but other events will be executed
normally if the system is merely "sleeping".
49 | 1 | # |
89
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
2 | # Borgend by Tuomo Valkonen, 2018 |
49 | 3 | # |
89
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
4 | # This file is the scheduler: it provides e a way for other threads to |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
5 | # wait until a given time; which may be "dreamtime" that discounts system |
51cc2e25af38
Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents:
87
diff
changeset
|
6 | # sleep periods |
49 | 7 | # |
8 | ||
9 | import time | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
10 | import logging |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
11 | from threading import Condition, Thread |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
12 | |
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
13 | from . import dreamtime |
49 | 14 | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
15 | logger=logging.getLogger(__name__) |
49 | 16 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
17 | class QueuedEvent: |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
18 | def __init__(self, cond, name=None): |
49 | 19 | self.next=None |
20 | self.prev=None | |
21 | self.name=name | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
22 | self.cond=cond |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
23 | self.linked=False |
49 | 24 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
25 | @staticmethod |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
26 | def snapshot(): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
27 | return None |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
28 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
29 | def is_before(self, other, snapshot=None): |
54 | 30 | raise NotImplementedError |
49 | 31 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
32 | def __lt__(self, other): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
33 | return self.is_before(other, self.snapshot()) |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
34 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
35 | def insert_after(self, ev, snapshot=None): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
36 | if not snapshot: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
37 | snapshot=self.snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
38 | if not self.next or ev.is_before(self.next, snapshot): |
49 | 39 | self.insert_immediately_after(ev) |
40 | else: | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
41 | self.next.insert_after(ev, snapshot) |
49 | 42 | |
43 | def insert_immediately_after(self, ev): | |
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
44 | assert(ev.next is None and ev.prev is None) |
49 | 45 | ev.prev=self |
46 | ev.next=self.next | |
47 | self.next=ev | |
48 | ||
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
49 | def insert_immediately_before(self, ev): |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
50 | assert(ev.next is None and ev.prev is None) |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
51 | ev.next=self |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
52 | ev.prev=self.prev |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
53 | self.prev=ev |
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
54 | |
49 | 55 | def unlink(self): |
56 | n=self.next | |
57 | p=self.prev | |
58 | if n: | |
59 | n.prev=p | |
60 | if p: | |
61 | p.next=n | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
62 | self.next=None |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
63 | self.prev=None |
49 | 64 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
65 | class ScheduledEvent(QueuedEvent): |
78
83b43987e61e
Renamed the "sleep" module "dreamtime"
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
66 | #@accepts(ScheduledEvent, dreamtime.Time, threading.Cond, str) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
67 | def __init__(self, when, cond, name=None): |
54 | 68 | super().__init__(cond, name=name) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
69 | self.when=when |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
70 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
71 | @staticmethod |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
72 | def snapshot(): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
73 | return dreamtime.Snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
74 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
75 | def is_before(self, other, snapshot=None): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
76 | if not snapshot: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
77 | snapshot=self.snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
78 | return self.when.horizon(snapshot) < other.when.horizon(snapshot) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
79 | |
49 | 80 | class TerminableThread(Thread): |
81 | def __init__(self, *args, **kwargs): | |
82 | super().__init__(*args, **kwargs) | |
83 | self._terminate=False | |
84 | self._cond=Condition() | |
85 | ||
86 | def terminate(self): | |
87 | with self._cond: | |
88 | _terminate=True | |
89 | self._cond.notify() | |
90 | ||
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
91 | class QueueThread(TerminableThread): |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
92 | def __init__(self, *args, **kwargs): |
54 | 93 | super().__init__(*args, **kwargs) |
94 | self.daemon = True | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
95 | self._list = None |
49 | 96 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
97 | def _insert(self, ev, snapshot=None): |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
98 | assert(not ev.linked) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
99 | if not self._list: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
100 | #logger.debug("Insert first") |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
101 | self._list=ev |
54 | 102 | elif ev<self._list: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
103 | #logger.debug("Insert beginning") |
69
8705e296c7a0
Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
59
diff
changeset
|
104 | self._list.insert_immediately_before(ev) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
105 | self._list=ev |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
106 | else: |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
107 | #logger.debug("Insert after") |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
108 | if not snapshot: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
109 | snapshot=ev.snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
110 | self._list.insert_after(ev, snapshot) |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
111 | ev.linked=True |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
112 | |
54 | 113 | def _unlink(self, ev): |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
114 | assert(ev.linked) |
54 | 115 | if ev==self._list: |
116 | self._list=ev.next | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
117 | ev.unlink() |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
118 | ev.linked=False |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
119 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
120 | def _resort(self, snapshot=None): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
121 | oldlist=self._list |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
122 | self._list=None |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
123 | if oldlist and not snapshot: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
124 | snapshot=oldlist.snapshot() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
125 | while oldlist: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
126 | ev=oldlist |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
127 | oldlist=oldlist.next |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
128 | ev.unlink() |
91
f53aa2007a84
Oops; _resort needs to mark ev.linked=False to avoid assertion errors in _insert.
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
129 | ev.linked=False |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
130 | self._insert(ev, snapshot) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
131 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
132 | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
133 | |
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
134 | class Scheduler(QueueThread): |
49 | 135 | # Default to precision of 60 seconds: the scheduler thread will never |
136 | # sleep longer than that, to get quickly back on track with the schedule | |
137 | # when the computer wakes up from sleep | |
138 | def __init__(self, precision=60): | |
139 | self.precision = precision | |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
140 | self._next_event_time = None |
54 | 141 | super().__init__(target = self._scheduler_thread, name = 'Scheduler') |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
142 | dreamtime.add_callback(self, self._sleepwake_callback) |
49 | 143 | |
54 | 144 | def _scheduler_thread(self): |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
145 | logger.debug("Scheduler thread started") |
49 | 146 | with self._cond: |
147 | while not self._terminate: | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
148 | snapshot = dreamtime.Snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
149 | now = snapshot.monotonic() |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
150 | if not self._list: |
49 | 151 | timeout = None |
152 | else: | |
153 | # Wait at most precision seconds, or until next event if it | |
154 | # comes earlier | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
155 | delta = self._list.when.monotonic(snapshot)-now |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
156 | timeout = min(self.precision, delta) |
49 | 157 | |
158 | if not timeout or timeout>0: | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
159 | logger.debug("Scheduler waiting %s seconds" % str(timeout)) |
49 | 160 | self._cond.wait(timeout) |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
161 | snapshot = dreamtime.Snapshot() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
162 | now = snapshot.monotonic() |
49 | 163 | |
75
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
164 | logger.debug("Scheduler timed out") |
2a44b9649212
UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
165 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
166 | while self._list and self._list.when.horizon(snapshot) <= now: |
54 | 167 | ev=self._list |
59
8d0a815022cc
Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents:
55
diff
changeset
|
168 | logger.debug("Scheduler activating %s" % (ev.name or "(unknown)")) |
54 | 169 | # We are only allowed to remove ev from list when ev.cond allows |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
170 | self._unlink(ev) |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
171 | # We need to release the lock on self._cond before acquire |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
172 | # one ev.cond to avoid race conditions with self._wait |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
173 | self._cond.release() |
54 | 174 | with ev.cond: |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
175 | ev.cond.notify_all() |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
176 | self._cond.acquire() |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
177 | |
54 | 178 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
179 | def _sleepwake_callback(self, woke): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
91
diff
changeset
|
180 | logger.debug("Rescheduling events after sleep/wakeup") |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
181 | with self._cond: |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
182 | self._resort() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
183 | |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
184 | # It is required to have acquired the lock on ev.cond on entry |
54 | 185 | def _wait(self, ev): |
186 | with self._cond: | |
187 | self._insert(ev) | |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
188 | self._cond.notify() |
54 | 189 | |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
190 | # This will release the lock on cond, allowing the scheduler |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
191 | # thread to notify us if we are ready to be released |
54 | 192 | ev.cond.wait() |
193 | ||
194 | # If we were woken up by some other event, not the scheduler, | |
195 | # ensure the event is removed | |
87
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
196 | if ev.linked: |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
197 | # Deal with race conditions wrt. the two different locks |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
198 | # in the scheduler |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
199 | #ev.cond.release() |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
200 | with self._cond: |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
201 | self._unlink(ev) |
a214d475aa28
Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents:
86
diff
changeset
|
202 | #ev.cond.acquire() |
49 | 203 | |
204 | # cond has to be acquired on entry! | |
205 | def wait_until(self, when, cond, name=None): | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
206 | logger.debug("Scheduling '%s' in %s seconds [%s]" % |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
75
diff
changeset
|
207 | (name, when.seconds_to(), when.__class__.__name__)) |
53
442c558bd632
Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents:
49
diff
changeset
|
208 | self._wait(ScheduledEvent(when, cond, name)) |
49 | 209 |