[dataio/buffer] setting mutex lock while appending
This commit is contained in:
parent
25d16cc5fd
commit
bbc8def460
@ -1,10 +1,17 @@
|
|||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from IPython import embed
|
from IPython import embed
|
||||||
|
from pyqtgraph.Qt.QtCore import QMutex
|
||||||
|
|
||||||
|
|
||||||
class CircBuffer:
|
class CircBuffer:
|
||||||
def __init__(self, size: int, channels: int = 1, samplerate: int = 40_000):
|
def __init__(
|
||||||
|
self,
|
||||||
|
size: int,
|
||||||
|
mutex: QMutex,
|
||||||
|
channels: int = 1,
|
||||||
|
samplerate: int = 40_000,
|
||||||
|
):
|
||||||
self.__size = size
|
self.__size = size
|
||||||
self.__channels = channels
|
self.__channels = channels
|
||||||
self.__samplereate = samplerate
|
self.__samplereate = samplerate
|
||||||
@ -16,7 +23,7 @@ class CircBuffer:
|
|||||||
self.__is_full = [False for i in range(channels)]
|
self.__is_full = [False for i in range(channels)]
|
||||||
self.__totalcount = [0 for i in range(channels)]
|
self.__totalcount = [0 for i in range(channels)]
|
||||||
self.__overflows = [0 for i in range(channels)]
|
self.__overflows = [0 for i in range(channels)]
|
||||||
self.__read_increment = samplerate * 0.1
|
self.mutex = mutex
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def size(self):
|
def size(self):
|
||||||
@ -40,6 +47,7 @@ class CircBuffer:
|
|||||||
return self.__index[channel]
|
return self.__index[channel]
|
||||||
|
|
||||||
def append(self, item, channel: int = 0):
|
def append(self, item, channel: int = 0):
|
||||||
|
self.mutex.lock()
|
||||||
self.__buffer[channel, self.write_index(channel)] = item
|
self.__buffer[channel, self.write_index(channel)] = item
|
||||||
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
self.__index[channel] = (self.write_index(channel) + 1) % self.__size
|
||||||
self.__totalcount[channel] += 1
|
self.__totalcount[channel] += 1
|
||||||
@ -49,6 +57,7 @@ class CircBuffer:
|
|||||||
if self.__index[channel] == 0:
|
if self.__index[channel] == 0:
|
||||||
self.__is_full[channel] = True
|
self.__is_full[channel] = True
|
||||||
self.__overflows[channel] += 1
|
self.__overflows[channel] += 1
|
||||||
|
self.mutex.unlock()
|
||||||
|
|
||||||
def get_all(self, channel: int = 0):
|
def get_all(self, channel: int = 0):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user