Ticket #10732: DeviceReadBuffer.cpp-patch

File DeviceReadBuffer.cpp-patch, 4.5 KB (added by ltskinol@…, 12 years ago)
Line 
1--- DeviceReadBuffer.cpp.orig   2012-10-03 19:40:49.000000000 -0400
2+++ DeviceReadBuffer.cpp        2012-10-03 19:37:28.000000000 -0400
3@@ -421,7 +421,7 @@
4         "mingw DeviceReadBuffer::Poll is not implemented");
5     return false;
6 #else
7-    bool retval = true;
8+    bool retval;
9     MythTimer timer;
10     timer.start();
11 
12@@ -441,6 +441,7 @@
13         polls[1].revents = 0;
14     }
15 
16+    retval = true;
17     while (true)
18     {
19         polls[0].revents = 0;
20@@ -455,65 +456,75 @@
21 
22         int ret = poll(polls, poll_cnt, timeout);
23 
24-        if (polls[0].revents & (POLLHUP | POLLNVAL))
25-        {
26-            LOG(VB_GENERAL, LOG_ERR, LOC + "poll error");
27-            error = true;
28-            return true;
29-        }
30-
31         if (!dorun || !IsOpen() || IsPauseRequested())
32         {
33-            retval = false;
34-            break; // are we supposed to pause, stop, etc.
35+            return false; // are we supposed to pause, stop, etc.
36         }
37-
38-        if (polls[0].revents & POLLPRI)
39+   
40+        if (ret < 0) // error
41         {
42-            readerCB->PriorityEvent(polls[0].fd);
43-        }
44+           if ((EOVERFLOW == errno))
45+           // TODO shouldn't this lock and then set error=true?
46+               break; // we have an error to handle
47+
48+            if ((EAGAIN == errno) || (EINTR  == errno))
49+                continue; // errors that tell you to try again
50 
51-        if (polls[0].revents & POLLIN)
52+            usleep(2500 /*2.5 ms*/);
53+        }
54+        else if (ret == 0) // timeout
55         {
56-            if (ret > 0)
57-                break; // we have data to read :)
58-            else if (ret < 0)
59-            {
60-                if ((EOVERFLOW == errno))
61-                    break; // we have an error to handle
62-
63-                if ((EAGAIN == errno) || (EINTR  == errno))
64-                    continue; // errors that tell you to try again
65-
66-                usleep(2500 /*2.5 ms*/);
67-            }
68-            else //  ret == 0
69-            {
70-                if (poll_timeout_is_error &&
71-                    (timer.elapsed() >= (int)max_poll_wait))
72-                {
73-                    LOG(VB_GENERAL, LOG_ERR, LOC + "Poll giving up 1");
74-                    QMutexLocker locker(&lock);
75-                    error = true;
76-                    return true;
77-                }
78+            if (poll_timeout_is_error &&
79+                (timer.elapsed() >= (int)max_poll_wait))
80+            {
81+                LOG(VB_GENERAL, LOG_ERR, LOC + "Poll giving up 1");
82+                QMutexLocker locker(&lock);
83+                error = true;
84+                return true;
85             }
86         }
87-
88-        // Clear out any pending pipe reads
89-        if ((poll_cnt > 1) && (polls[1].revents & POLLIN))
90+        else // normal return - some event available
91         {
92-            char dummy[128];
93-            int cnt = (wake_pipe_flags[0] & O_NONBLOCK) ? 128 : 1;
94-            cnt = ::read(wake_pipe[0], dummy, cnt);
95-        }
96+            if (polls[0].revents & POLLHUP)
97+            {
98+                LOG(VB_GENERAL, LOG_ERR, LOC + "poll eof (POLLHUP)");
99+                break;
100+            }
101+            else if (polls[0].revents & POLLNVAL)
102+            {
103+                LOG(VB_GENERAL, LOG_ERR, LOC + "poll error (POLLINVAL)");
104+                QMutexLocker locker(&lock);
105+                error = true;
106+                return true;
107+            }
108 
109-        if (poll_timeout_is_error && (timer.elapsed() >= (int)max_poll_wait))
110-        {
111-            LOG(VB_GENERAL, LOG_ERR, LOC + "Poll giving up 2");
112-            QMutexLocker locker(&lock);
113-            error = true;
114-            return true;
115+            if (polls[0].revents & POLLPRI)
116+            {
117+                readerCB->PriorityEvent(polls[0].fd);
118+            }
119+       
120+            // Clear out any pending pipe reads
121+            if (polls[1].revents & POLLIN)
122+            {
123+                char dummy[128];
124+                int cnt = (wake_pipe_flags[0] & O_NONBLOCK) ? 128 : 1;
125+                cnt = ::read(wake_pipe[0], dummy, cnt);
126+            }
127+       
128+            // we have data to read :)
129+            if (polls[0].revents & POLLIN)
130+            {
131+                break;
132+            }
133+       
134+            if (poll_timeout_is_error &&
135+            (timer.elapsed() >= (int)max_poll_wait))
136+            {
137+                LOG(VB_GENERAL, LOG_ERR, LOC + "Poll giving up 2");
138+                QMutexLocker locker(&lock);
139+                error = true;
140+                return true;
141+            }
142         }
143     }
144