Ticket #3517: mythbackend_mainserver_threadpool.patch.txt

File mythbackend_mainserver_threadpool.patch.txt, 3.1 KB (added by Russell Bryant <russell@…>, 15 years ago)
Line 
1Index: programs/mythbackend/mainserver.h
2===================================================================
3--- programs/mythbackend/mainserver.h   (revision 13509)
4+++ programs/mythbackend/mainserver.h   (working copy)
5@@ -175,6 +175,7 @@
6 
7     QMutex deletelock;
8     QMutex threadPoolLock;
9+    QWaitCondition threadPoolCond;
10     vector<ProcessRequestThread *> threadPool;
11 
12     bool masterBackendOverride;
13Index: programs/mythbackend/mainserver.cpp
14===================================================================
15--- programs/mythbackend/mainserver.cpp (revision 13509)
16+++ programs/mythbackend/mainserver.cpp (working copy)
17@@ -112,16 +112,16 @@
18         lock.lock();
19         socket = sock;
20         socket->UpRef();
21-        lock.unlock();
22         waitCond.wakeOne();
23+        lock.unlock();
24     }
25 
26     void killit(void)
27     {
28         lock.lock();
29         threadlives = false;
30-        lock.unlock();
31         waitCond.wakeOne();
32+        lock.unlock();
33     }
34 
35     virtual void run()
36@@ -130,6 +130,10 @@
37 
38         lock.lock();
39 
40+        // Signal back to the thread that created this one in case it is
41+        // waiting to find out that it is up and running.
42+        waitCond.wakeOne();
43+
44         while (1)
45         {
46             waitCond.wait(&lock);
47@@ -149,13 +153,14 @@
48         lock.unlock();
49     }
50 
51+    QMutex lock;
52+    QWaitCondition waitCond;
53+
54   private:
55     MainServer *parent;
56 
57     MythSocket *socket;
58 
59-    QMutex lock;
60-    QWaitCondition waitCond;
61     bool threadlives;
62 };
63 
64@@ -175,7 +180,10 @@
65     for (int i = 0; i < PRT_STARTUP_THREAD_COUNT; i++)
66     {
67         ProcessRequestThread *prt = new ProcessRequestThread(this);
68+        prt->lock.lock();
69         prt->start();
70+        prt->waitCond.wait(&prt->lock);
71+        prt->lock.unlock();
72         threadPool.push_back(prt);
73     }
74 
75@@ -245,38 +253,28 @@
76 
77     readReadyLock.lock();
78 
79-    MythTimer t;
80-    t.start();
81     ProcessRequestThread *prt = NULL;
82-    while (!prt)
83+    threadPoolLock.lock();
84+    if (threadPool.empty())
85     {
86-        threadPoolLock.lock();
87-        if (!threadPool.empty())
88-        {
89-            prt = threadPool.back();
90-            threadPool.pop_back();
91-        }
92-        threadPoolLock.unlock();
93-
94-        if (t.elapsed() > PRT_TIMEOUT)
95-            break;
96-
97-        if (!prt)
98-        {
99-            VERBOSE(VB_IMPORTANT, "Waiting for a process request thread..");
100-            usleep(1000); /* 1 millisecond */
101-        }
102+        VERBOSE(VB_IMPORTANT, "Waiting for a process request thread..");
103+        threadPoolCond.wait(&threadPoolLock, PRT_TIMEOUT);
104     }
105-
106-    if (!prt)
107+    if (!threadPool.empty())
108     {
109-        threadPoolLock.lock();
110+        prt = threadPool.back();
111+        threadPool.pop_back();
112+    }
113+    else
114+    {
115         VERBOSE(VB_IMPORTANT, "Adding a new process request thread");
116         prt = new ProcessRequestThread(this);
117+        prt->lock.lock();
118         prt->start();
119-        usleep(50000); /* Wait 50 milliseconds for start to actually run. */
120-        threadPoolLock.unlock();
121+        prt->waitCond.wait(&prt->lock);
122+        prt->lock.unlock();
123     }
124+    threadPoolLock.unlock();
125 
126     prt->setup(sock);
127