Ticket #9927: DRB-leak.patch2

File DRB-leak.patch2, 3.3 KB (added by Tony Lill <ajlill@…>, 8 years ago)

The correct patch

Line 
1diff --git a/mythtv/libs/libmythtv/DeviceReadBuffer.cpp b/mythtv/libs/libmythtv/DeviceReadBuffer.cpp
2index cce5732..f27c015 100644
3--- a/mythtv/libs/libmythtv/DeviceReadBuffer.cpp
4+++ b/mythtv/libs/libmythtv/DeviceReadBuffer.cpp
5@@ -19,7 +19,7 @@ using namespace std;
6 
7 DeviceReadBuffer::DeviceReadBuffer(ReaderPausedCB *cb, bool use_poll)
8     : videodevice(QString::null),   _stream_fd(-1),
9-      readerPausedCB(cb),
10+      readerPausedCB(cb),           thread(NULL),
11 
12       // Data for managing the device ringbuffer
13       run(false),                   running(false),
14@@ -43,6 +43,8 @@ DeviceReadBuffer::~DeviceReadBuffer()
15 {
16     if (buffer)
17         delete[] buffer;
18+    if (thread)
19+      Stop();
20 }
21 
22 bool DeviceReadBuffer::Setup(const QString &streamName, int streamfd)
23@@ -94,15 +96,14 @@ bool DeviceReadBuffer::Setup(const QString &streamName, int streamfd)
24 
25 void DeviceReadBuffer::Start(void)
26 {
27-    bool was_running;
28+    QMutexLocker locker(&thread_lock);
29 
30     {
31         QMutexLocker locker(&lock);
32-        was_running = running;
33         error = false;
34     }
35 
36-    if (was_running)
37+    if (thread)
38     {
39         VERBOSE(VB_IMPORTANT, LOC_ERR + "Start(): Already running.");
40         SetRequestPause(false);
41@@ -135,9 +136,8 @@ void DeviceReadBuffer::Reset(const QString &streamName, int streamfd)
42 
43 void DeviceReadBuffer::Stop(void)
44 {
45-    bool was_running = IsRunning();
46-
47-    if (!was_running)
48+    QMutexLocker locker(&thread_lock);
49+    if (!thread)
50     {
51         VERBOSE(VB_IMPORTANT, LOC + "Stop(): Not running.");
52         return;
53@@ -149,6 +149,7 @@ void DeviceReadBuffer::Stop(void)
54     }
55 
56     pthread_join(thread, NULL);
57+    thread = (pthread_t)NULL;
58 }
59 
60 void DeviceReadBuffer::SetRequestPause(bool req)
61@@ -489,6 +490,8 @@ uint DeviceReadBuffer::Read(unsigned char *buf, const uint count)
62 }
63 
64 /** \fn DeviceReadBuffer::WaitForUnused(uint) const
65+ *  \brief Return the amount of empty space in our internal buffer.
66+ *         If there is less than what is needed, and something is emptying the buffer, wait.
67  *  \param needed Number of bytes we want to write
68  *  \return bytes available for writing
69  */
70@@ -515,6 +518,8 @@ uint DeviceReadBuffer::WaitForUnused(uint needed) const
71 }
72 
73 /** \fn DeviceReadBuffer::WaitForUsed(uint) const
74+ *  \brief Return the number of bytes available in our buffer.
75+ *         Wait if another thread is actively  reading from the device.
76  *  \param needed Number of bytes we want to read
77  *  \return bytes available for reading
78  */
79diff --git a/mythtv/libs/libmythtv/DeviceReadBuffer.h b/mythtv/libs/libmythtv/DeviceReadBuffer.h
80index 70fc262..84c1ba7 100644
81--- a/mythtv/libs/libmythtv/DeviceReadBuffer.h
82+++ b/mythtv/libs/libmythtv/DeviceReadBuffer.h
83@@ -78,11 +78,12 @@ class DeviceReadBuffer
84 
85     ReaderPausedCB  *readerPausedCB;
86     pthread_t        thread;
87+    mutable QMutex   thread_lock;      // Manage access to thread variable
88 
89     // Data for managing the device ringbuffer
90     mutable QMutex   lock;
91-    bool             run;
92-    bool             running;
93+    bool             run;              // Set to false if we want the thread to stop
94+    bool             running;          // If the read thread is running
95     bool             eof;
96     mutable bool     error;
97     bool             request_pause;