summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/service.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/src/Caelestia/service.cpp')
-rw-r--r--plugin/src/Caelestia/service.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/service.cpp b/plugin/src/Caelestia/service.cpp
new file mode 100644
index 0000000..7c0df92
--- /dev/null
+++ b/plugin/src/Caelestia/service.cpp
@@ -0,0 +1,54 @@
+#include "service.hpp"
+
+#include <qdebug.h>
+
+namespace caelestia {
+
+Service::Service(QObject* parent)
+ : QObject(parent)
+ , m_refCount(0) {}
+
+int Service::refCount() {
+ QMutexLocker locker(&m_mutex);
+ return m_refCount;
+}
+
+void Service::ref() {
+ bool needsStart = false;
+
+ {
+ QMutexLocker locker(&m_mutex);
+ if (m_refCount == 0) {
+ needsStart = true;
+ }
+ m_refCount++;
+ }
+ emit refCountChanged();
+
+ if (needsStart) {
+ QMetaObject::invokeMethod(this, &Service::start, Qt::QueuedConnection);
+ }
+}
+
+void Service::unref() {
+ bool needsStop = false;
+
+ {
+ QMutexLocker locker(&m_mutex);
+ if (m_refCount == 0) {
+ qWarning() << "ServiceRef::unref: attempted to unref service with no active refs";
+ return;
+ }
+ m_refCount--;
+ if (m_refCount == 0) {
+ needsStop = true;
+ }
+ }
+ emit refCountChanged();
+
+ if (needsStop) {
+ QMetaObject::invokeMethod(this, &Service::stop, Qt::QueuedConnection);
+ }
+}
+
+} // namespace caelestia