MythTV master
programinfoupdater.cpp
Go to the documentation of this file.
1#include <thread>
2
3// MythTV headers
6
8
10 uint recordedid, PIAction action, uint64_t filesize)
11{
12 if (recordedid == 0)
13 return;
14 QMutexLocker locker(&m_lock);
16 {
17 QHash<uint,PIKeyData>::iterator it = m_needsUpdate.find(recordedid);
18 // If there is no action in the set we can insert
19 // If it is the same type of action we can overwrite,
20 // If it the new action is a full update we can overwrite
21 if (it == m_needsUpdate.end())
22 m_needsUpdate.insert(recordedid, PIKeyData(action, filesize));
23 else if (((*it).m_action == action) || (kPIUpdate == action))
24 (*it) = PIKeyData(action, filesize);
25 }
26 else
27 {
28 m_needsAddDelete.emplace_back(recordedid, action);
29 }
30
31 // Start a new run() if one isn't already running..
32 // The lock prevents anything from getting stuck on a queue.
33 if (!m_isRunning)
34 {
35 m_isRunning = true;
36 MThreadPool::globalInstance()->start(this, "ProgramInfoUpdater");
37 }
38 else
39 {
40 m_moreWork.wakeAll();
41 }
42}
43
45{
46 bool workDone = true;
47
48 while( workDone )
49 {
50 workDone = false;
51
52 // we don't need instant updates allow a few to queue up
53 // if they come in quick succession, this allows multiple
54 // updates to be consolidated into one update...
55 std::this_thread::sleep_for(200ms);
56
57 m_lock.lock();
58
59 // send adds and deletes in the order they were queued
60 for (auto & pi : m_needsAddDelete)
61 {
62 if (kPIAdd != pi.m_action && kPIDelete != pi.m_action)
63 continue;
64
65 QString type = (kPIAdd == pi.m_action) ? "ADD" : "DELETE";
66 QString msg = QString("RECORDING_LIST_CHANGE %1 %2")
67 .arg(type).arg(pi.m_recordedid);
68
69 workDone = true;
71 }
72 m_needsAddDelete.clear();
73
74 // Send updates in any old order, we just need
75 // one per updated ProgramInfo.
76 // NOLINTNEXTLINE(modernize-loop-convert)
77 for (auto itu = m_needsUpdate.begin(); itu != m_needsUpdate.end(); ++itu)
78 {
79 QString msg;
80
81 if (kPIUpdateFileSize == (*itu).m_action)
82 {
83 msg = QString("UPDATE_FILE_SIZE %1 %2")
84 .arg(itu.key())
85 .arg((*itu).m_filesize);
86 }
87 else
88 {
89 msg = QString("MASTER_UPDATE_REC_INFO %1")
90 .arg(itu.key());
91 }
92
93 workDone = true;
95 }
96 m_needsUpdate.clear();
97
98 if ( workDone )
99 m_moreWork.wait(&m_lock, 1000);
100
101 m_lock.unlock();
102 }
103
104 m_isRunning = false;
105}
106
107/*
108 * vim:ts=4:sw=4:ai:et:si:sts=4
109 */
static MThreadPool * globalInstance(void)
void start(QRunnable *runnable, const QString &debugName, int priority=0)
void SendMessage(const QString &message)
QHash< uint, PIKeyData > m_needsUpdate
void run(void) override
void insert(uint recordedid, PIAction action, uint64_t filesize=0ULL)
std::vector< PIKeyAction > m_needsAddDelete
QWaitCondition m_moreWork
unsigned int uint
Definition: compat.h:60
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
@ kPIUpdateFileSize
@ kPIUpdate
@ kPIAdd
@ kPIDelete