MythTV  master
programinfoupdater.cpp
Go to the documentation of this file.
1 // MythTV headers
2 #include "programinfoupdater.h"
3 #include "mthreadpool.h"
4 #include "mythlogging.h"
5 #include "mythcorecontext.h"
6 #include "remoteutil.h"
7 
8 #include <unistd.h> // for usleep()
9 
10 using std::vector;
11 
13  uint recordedid, PIAction action, uint64_t filesize)
14 {
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  m_moreWork.wakeAll();
41 }
42 
44 {
45  bool workDone;
46 
47  do {
48  workDone = false;
49 
50  // we don't need instant updates allow a few to queue up
51  // if they come in quick succession, this allows multiple
52  // updates to be consolidated into one update...
53  usleep(200 * 1000); // 200ms
54 
55  m_lock.lock();
56 
57  // send adds and deletes in the order they were queued
58  vector<PIKeyAction>::iterator ita = m_needsAddDelete.begin();
59  for (; ita != m_needsAddDelete.end(); ++ita)
60  {
61  if (kPIAdd != (*ita).m_action && kPIDelete != (*ita).m_action)
62  continue;
63 
64  QString type = (kPIAdd == (*ita).m_action) ? "ADD" : "DELETE";
65  QString msg = QString("RECORDING_LIST_CHANGE %1 %2")
66  .arg(type).arg((*ita).m_recordedid);
67 
68  workDone = true;
70  }
71  m_needsAddDelete.clear();
72 
73  // Send updates in any old order, we just need
74  // one per updated ProgramInfo.
75  QHash<uint,PIKeyData>::iterator itu = m_needsUpdate.begin();
76  for (; itu != m_needsUpdate.end(); ++itu)
77  {
78  QString msg;
79 
80  if (kPIUpdateFileSize == (*itu).m_action)
81  {
82  msg = QString("UPDATE_FILE_SIZE %1 %2")
83  .arg(itu.key())
84  .arg((*itu).m_filesize);
85  }
86  else
87  {
88  msg = QString("MASTER_UPDATE_REC_INFO %1")
89  .arg(itu.key());
90  }
91 
92  workDone = true;
94  }
95  m_needsUpdate.clear();
96 
97  if ( workDone )
98  m_moreWork.wait(&m_lock, 1000);
99 
100  m_lock.unlock();
101  } while( workDone );
102 
103  m_isRunning = false;
104 }
105 
106 /*
107  * vim:ts=4:sw=4:ai:et:si:sts=4
108  */
QWaitCondition m_moreWork
void run(void) override
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QHash< uint, PIKeyData > m_needsUpdate
void SendMessage(const QString &message)
void insert(uint recordedid, PIAction action, uint64_t filesize=0ULL)
static MThreadPool * globalInstance(void)
void start(QRunnable *runnable, const QString &debugName, int priority=0)
std::vector< PIKeyAction > m_needsAddDelete