diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2005-06-24 16:43:32 +0000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2005-06-24 16:43:32 +0000 |
commit | ee2d1169d638d91ff551ac0e0ee2aa80bac85a74 (patch) | |
tree | 32f0e3a56dec6296928d3da7f685d90414eeee70 /gpsfake.py | |
parent | 8347a0493cea7787b21a36448d4c47d1b6687b3c (diff) | |
download | gpsd-ee2d1169d638d91ff551ac0e0ee2aa80bac85a74.tar.gz |
Guard thread starts so we don't have to track when a fake GPS is active.
Diffstat (limited to 'gpsfake.py')
-rw-r--r-- | gpsfake.py | 23 |
1 files changed, 13 insertions, 10 deletions
@@ -76,7 +76,7 @@ class FakeGPS: "A fake GPS is a pty with a test log ready to be cycled to it." def __init__(self, logfp, rate=4800): self.go_predicate = lambda i, s: True - self.stopme = False + self.readers = 0 self.thread = None self.index = 0 baudrates = { @@ -128,19 +128,22 @@ class FakeGPS: return True def __feed(self): "Feed the contents of the GPS log to the daemon." - while not self.stopme and self.go_predicate(self.index, self): + while self.readers and self.go_predicate(self.index, self): os.write(self.master_fd, self.testload.sentences[self.index % len(self.testload.sentences)]) self.index += 1 def start(self, thread=False): - self.thread = threading.Thread(self.__feed()) - self.stopme = False - if thread: - self.thread.start() # Run asynchronously - else: - self.thread.run() # Run synchronously + "Increment pseudodevice's reader count, starting it if necessary." + self.readers += 1 + if self.readers == 1: + self.thread = threading.Thread(self.__feed()) + if thread: + self.thread.start() # Run asynchronously + else: + self.thread.run() # Run synchronously def stop(self): - "Stop this fake GPS." - self.stopme = True + "Decrement pseudodevice's reader count; it will stop when count==0." + if self.readers > 0: + self.readers -= 1 class DaemonInstance: "Control a gpsd instance." |