Ticket #9927: DRB-leak.patch3

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

Patch using new boolean instead of thread.

Line 
1diff --git a/mythtv/libs/libmythtv/DeviceReadBuffer.cpp b/mythtv/libs/libmythtv/DeviceReadBuffer.cpp
2index cce5732..5892d20 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_created(false),
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_created)
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_created)
38     {
39         VERBOSE(VB_IMPORTANT, LOC_ERR + "Start(): Already running.");
40         SetRequestPause(false);
41@@ -117,6 +118,7 @@ void DeviceReadBuffer::Start(void)
42         QMutexLocker locker(&lock);
43         error = true;
44     }
45+    thread_created = true;
46 }
47 
48 void DeviceReadBuffer::Reset(const QString &streamName, int streamfd)
49@@ -135,9 +137,8 @@ void DeviceReadBuffer::Reset(const QString &streamName, int streamfd)
50 
51 void DeviceReadBuffer::Stop(void)
52 {
53-    bool was_running = IsRunning();
54-
55-    if (!was_running)
56+    QMutexLocker locker(&thread_lock);
57+    if (!thread_created)
58     {
59         VERBOSE(VB_IMPORTANT, LOC + "Stop(): Not running.");
60         return;
61@@ -149,6 +150,7 @@ void DeviceReadBuffer::Stop(void)
62     }
63 
64     pthread_join(thread, NULL);
65+    thread_created = false;
66 }
67 
68 void DeviceReadBuffer::SetRequestPause(bool req)
69@@ -489,6 +491,8 @@ uint DeviceReadBuffer::Read(unsigned char *buf, const uint count)
70 }
71 
72 /** \fn DeviceReadBuffer::WaitForUnused(uint) const
73+ *  \brief Return the amount of empty space in our internal buffer.
74+ *         If there is less than what is needed, and something is emptying the buffer, wait.
75  *  \param needed Number of bytes we want to write
76  *  \return bytes available for writing
77  */
78@@ -515,6 +519,8 @@ uint DeviceReadBuffer::WaitForUnused(uint needed) const
79 }
80 
81 /** \fn DeviceReadBuffer::WaitForUsed(uint) const
82+ *  \brief Return the number of bytes available in our buffer.
83+ *         Wait if another thread is actively  reading from the device.
84  *  \param needed Number of bytes we want to read
85  *  \return bytes available for reading
86  */
87diff --git a/mythtv/libs/libmythtv/DeviceReadBuffer.h b/mythtv/libs/libmythtv/DeviceReadBuffer.h
88index 70fc262..f25f3a4 100644
89--- a/mythtv/libs/libmythtv/DeviceReadBuffer.h
90+++ b/mythtv/libs/libmythtv/DeviceReadBuffer.h
91@@ -78,11 +78,13 @@ class DeviceReadBuffer
92 
93     ReaderPausedCB  *readerPausedCB;
94     pthread_t        thread;
95+    bool thread_created;               // True if a thread has been created and needs reaping
96+    mutable QMutex   thread_lock;      // Manage access to thread variable
97 
98     // Data for managing the device ringbuffer
99     mutable QMutex   lock;
100-    bool             run;
101-    bool             running;
102+    bool             run;              // Set to false if we want the thread to stop
103+    bool             running;          // If the read thread is running
104     bool             eof;
105     mutable bool     error;
106     bool             request_pause;