improve lock handling

This commit is contained in:
Jakob Ketterl 2020-02-27 19:48:22 +01:00
parent c325368be8
commit d36be799d0

View File

@ -111,7 +111,11 @@ class dsp(object):
self.unvoiced_quality = 1 self.unvoiced_quality = 1
self.modification_lock = threading.Lock() self.modification_lock = threading.Lock()
self.output = output self.output = output
self.temporary_directory = "/tmp"
self.temporary_directory = None
self.pipe_base_path = None
self.set_temporary_directory("/tmp")
self.is_service = False self.is_service = False
self.direwolf_config = None self.direwolf_config = None
self.direwolf_port = None self.direwolf_port = None
@ -122,6 +126,7 @@ class dsp(object):
def set_temporary_directory(self, what): def set_temporary_directory(self, what):
self.temporary_directory = what self.temporary_directory = what
self.pipe_base_path = "{tmp_dir}/openwebrx_pipe_{myid}_".format(tmp_dir=self.temporary_directory, myid=id(self))
def chain(self, which): def chain(self, which):
chain = ["nc -v 127.0.0.1 {nc_port}"] chain = ["nc -v 127.0.0.1 {nc_port}"]
@ -528,21 +533,19 @@ class dsp(object):
def set_offset_freq(self, offset_freq): def set_offset_freq(self, offset_freq):
self.offset_freq = offset_freq self.offset_freq = offset_freq
if self.running: if self.running:
self.modification_lock.acquire() with self.modification_lock:
self.pipe_files["shift_pipe"].write("%g\n" % (-float(self.offset_freq) / self.samp_rate)) self.pipe_files["shift_pipe"].write("%g\n" % (-float(self.offset_freq) / self.samp_rate))
self.pipe_files["shift_pipe"].flush() self.pipe_files["shift_pipe"].flush()
self.modification_lock.release()
def set_bpf(self, low_cut, high_cut): def set_bpf(self, low_cut, high_cut):
self.low_cut = low_cut self.low_cut = low_cut
self.high_cut = high_cut self.high_cut = high_cut
if self.running: if self.running:
self.modification_lock.acquire() with self.modification_lock:
self.pipe_files["bpf_pipe"].write( self.pipe_files["bpf_pipe"].write(
"%g %g\n" % (float(self.low_cut) / self.if_samp_rate(), float(self.high_cut) / self.if_samp_rate()) "%g %g\n" % (float(self.low_cut) / self.if_samp_rate(), float(self.high_cut) / self.if_samp_rate())
) )
self.pipe_files["bpf_pipe"].flush() self.pipe_files["bpf_pipe"].flush()
self.modification_lock.release()
def get_bpf(self): def get_bpf(self):
return [self.low_cut, self.high_cut] return [self.low_cut, self.high_cut]
@ -555,10 +558,9 @@ class dsp(object):
# no squelch required on digital voice modes # no squelch required on digital voice modes
actual_squelch = -150 if self.isDigitalVoice() or self.isPacket() or self.isPocsag() else self.squelch_level actual_squelch = -150 if self.isDigitalVoice() or self.isPacket() or self.isPocsag() else self.squelch_level
if self.running: if self.running:
self.modification_lock.acquire() with self.modification_lock:
self.pipe_files["squelch_pipe"].write("%g\n" % (self.convertToLinear(actual_squelch))) self.pipe_files["squelch_pipe"].write("%g\n" % (self.convertToLinear(actual_squelch)))
self.pipe_files["squelch_pipe"].flush() self.pipe_files["squelch_pipe"].flush()
self.modification_lock.release()
def set_unvoiced_quality(self, q): def set_unvoiced_quality(self, q):
self.unvoiced_quality = q self.unvoiced_quality = q
@ -633,17 +635,14 @@ class dsp(object):
self.direwolf_config = None self.direwolf_config = None
def start(self): def start(self):
self.modification_lock.acquire() with self.modification_lock:
if self.running: if self.running:
self.modification_lock.release()
return return
self.running = True self.running = True
command_base = " | ".join(self.chain(self.demodulator)) command_base = " | ".join(self.chain(self.demodulator))
# create control pipes for csdr # create control pipes for csdr
self.pipe_base_path = "{tmp_dir}/openwebrx_pipe_{myid}_".format(tmp_dir=self.temporary_directory, myid=id(self))
self.try_create_pipes(self.pipe_names, command_base) self.try_create_pipes(self.pipe_names, command_base)
# run the command # run the command
@ -700,13 +699,12 @@ class dsp(object):
), ),
) )
# open control pipes for csdr
for p in ["bpf_pipe", "shift_pipe", "squelch_pipe"]:
if self.has_pipe(p):
self.pipe_files[p] = open(self.pipes[p], "w")
self.start_secondary_demodulator() self.start_secondary_demodulator()
self.modification_lock.release() # open control pipes for csdr
for p in ["bpf_pipe", "shift_pipe", "squelch_pipe", "dmr_control_pipe"]:
if self.has_pipe(p):
self.pipe_files[p] = open(self.pipes[p], "w")
# send initial config through the pipes # send initial config through the pipes
if self.has_pipe("squelch_pipe"): if self.has_pipe("squelch_pipe"):
@ -739,11 +737,8 @@ class dsp(object):
self.output.send_output("meta", read_meta) self.output.send_output("meta", read_meta)
if self.has_pipe("dmr_control_pipe"):
self.pipe_files["dmr_control_pipe"] = open(self.pipes["dmr_control_pipe"], "w")
def stop(self): def stop(self):
self.modification_lock.acquire() with self.modification_lock:
self.running = False self.running = False
if self.process is not None: if self.process is not None:
try: try:
@ -756,8 +751,6 @@ class dsp(object):
self.try_delete_pipes(self.pipe_names) self.try_delete_pipes(self.pipe_names)
self.modification_lock.release()
def restart(self): def restart(self):
if not self.running: if not self.running:
return return