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