MythTV  master
programinfoupdater.cpp
Go to the documentation of this file.
1 // Posix headers
2 #include <unistd.h> // for usleep()
3 
4 // MythTV headers
8 
9 #include "programinfoupdater.h"
10 #include "remoteutil.h"
11 
13  uint recordedid, PIAction action, uint64_t filesize)
14 {
15  if (recordedid == 0)
16  return;
17  QMutexLocker locker(&m_lock);
19  {
20  QHash<uint,PIKeyData>::iterator it = m_needsUpdate.find(recordedid);
21  // If there is no action in the set we can insert
22  // If it is the same type of action we can overwrite,
23  // If it the new action is a full update we can overwrite
24  if (it == m_needsUpdate.end())
25  m_needsUpdate.insert(recordedid, PIKeyData(action, filesize));
26  else if (((*it).m_action == action) || (kPIUpdate == action))
27  (*it) = PIKeyData(action, filesize);
28  }
29  else
30  {
31  m_needsAddDelete.emplace_back(recordedid, action);
32  }
33 
34  // Start a new run() if one isn't already running..
35  // The lock prevents anything from getting stuck on a queue.
36  if (!m_isRunning)
37  {
38  m_isRunning = true;
39  MThreadPool::globalInstance()->start(this, "ProgramInfoUpdater");
40  }
41  else
42  m_moreWork.wakeAll();
43 }
44 
46 {
47  bool workDone = false;
48 
49  do {
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  usleep(200 * 1000); // 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  } while( workDone );
103 
104  m_isRunning = false;
105 }
106 
107 /*
108  * vim:ts=4:sw=4:ai:et:si:sts=4
109  */
PIKeyData
Definition: programinfoupdater.h:40
MythCoreContext::SendMessage
void SendMessage(const QString &message)
Definition: mythcorecontext.cpp:1517
kPIDelete
@ kPIDelete
Definition: programinfoupdater.h:20
ProgramInfoUpdater::run
void run(void) override
Definition: programinfoupdater.cpp:45
kPIAdd
@ kPIAdd
Definition: programinfoupdater.h:19
remoteutil.h
ProgramInfoUpdater::m_moreWork
QWaitCondition m_moreWork
Definition: programinfoupdater.h:59
ProgramInfoUpdater::m_lock
QMutex m_lock
Definition: programinfoupdater.h:58
ProgramInfoUpdater::m_needsUpdate
QHash< uint, PIKeyData > m_needsUpdate
Definition: programinfoupdater.h:62
mythlogging.h
programinfoupdater.h
PIAction
PIAction
Definition: programinfoupdater.h:18
ProgramInfoUpdater::insert
void insert(uint recordedid, PIAction action, uint64_t filesize=0ULL)
Definition: programinfoupdater.cpp:12
ProgramInfoUpdater::m_needsAddDelete
std::vector< PIKeyAction > m_needsAddDelete
Definition: programinfoupdater.h:61
uint
unsigned int uint
Definition: compat.h:81
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
mthreadpool.h
kPIUpdateFileSize
@ kPIUpdateFileSize
Definition: programinfoupdater.h:22
mythcorecontext.h
ProgramInfoUpdater::m_isRunning
bool m_isRunning
Definition: programinfoupdater.h:60
build_compdb.action
action
Definition: build_compdb.py:9
kPIUpdate
@ kPIUpdate
Definition: programinfoupdater.h:21
MThreadPool::globalInstance
static MThreadPool * globalInstance(void)
Definition: mthreadpool.cpp:307
MThreadPool::start
void start(QRunnable *runnable, const QString &debugName, int priority=0)
Definition: mthreadpool.cpp:342