Torc  0.1
torcadminthread.cpp
Go to the documentation of this file.
1 /* Class TorcAdminThread
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QMutex>
25 
26 // Torc
27 #include "torclocalcontext.h"
28 #include "torclogging.h"
29 #include "torcadminthread.h"
30 
45 {
46 }
47 
49 {
50  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Admin thread starting"));
51 
52  // create objects that will run in the admin thread
54 }
55 
57 {
59  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Admin thread stopping"));
60 }
61 
100 QList<TorcAdminObject*> TorcAdminObject::gTorcAdminObjects;
101 TorcAdminObject* TorcAdminObject::gTorcAdminObject = nullptr;
102 QMutex* TorcAdminObject::gTorcAdminObjectsLock = new QMutex(QMutex::Recursive);
103 
105  : m_nextTorcAdminObject(nullptr),
106  m_priority(Priority)
107 {
108  QMutexLocker lock(gTorcAdminObjectsLock);
109  m_nextTorcAdminObject = gTorcAdminObject;
110  gTorcAdminObject = this;
111 }
112 
114 {
115  return m_priority;
116 }
117 
119 {
120  return m_nextTorcAdminObject;
121 }
122 
127 {
128  return Object1->Priority() > Object2->Priority();
129 }
130 
135 {
136  QMutexLocker lock(gTorcAdminObjectsLock);
137 
138  // guard against multiple concurrent use
139  if (!gTorcAdminObjects.isEmpty())
140  {
141  LOG(VB_GENERAL, LOG_CRIT, QStringLiteral("Trying to create admin objects but they already exist!"));
142  return;
143  }
144 
145  // create a list of static objects that can be sorted by priority.
146  // Dynamically created objects will be appended to this list.
147  TorcAdminObject* object = gTorcAdminObject;
148  while (object)
149  {
150  gTorcAdminObjects.append(object);
151  object = object->GetNextObject();
152  }
153 
154  if (gTorcAdminObjects.isEmpty())
155  return;
156 
157  qSort(gTorcAdminObjects.begin(), gTorcAdminObjects.end(), TorcAdminObject::HigherPriority);
158 
159  QList<TorcAdminObject*>::const_iterator it = gTorcAdminObjects.constBegin();
160  for ( ; it != gTorcAdminObjects.constEnd(); ++it)
161  (*it)->Create();
162 }
163 
168 {
169  QMutexLocker lock(gTorcAdminObjectsLock);
170 
171  // destroy in the reverse order of creation
172  QList<TorcAdminObject*>::const_iterator it = gTorcAdminObjects.constEnd();
173  while (it != gTorcAdminObjects.constBegin())
174  {
175  --it;
176  (*it)->Destroy();
177  }
178  gTorcAdminObjects.clear();
179 }
180 
int Priority(void) const
A factory class for automatically running objects outside of the main loop.
TorcAdminObject * GetNextObject(void)
#define TORC_ADMIN_THREAD
Definition: torclocaldefs.h:11
static void CreateObjects(void)
Iterates through the list of registered TorcAdminObject&#39;s and creates them.
static bool HigherPriority(const TorcAdminObject *Object1, const TorcAdminObject *Object2)
Sort TorcAdminObject&#39;s based on relative priorities.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
TorcAdminObject(int Priority=TORC_ADMIN_LOW_PRIORITY)
static void DestroyObjects(void)
Destroys each created admin object.
A Torc specific wrapper around QThread.
Definition: torcqthread.h:7