QtGStreamer  1.2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
objectstore.cpp
1 /*
2  Copyright (C) 2010 Collabora Multimedia.
3  @author Mauricio Piacentini <mauricio.piacentini@collabora.co.uk>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation; either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <QtCore/QMutex>
20 #include <QtCore/QHash>
21 #include <QtCore/QAtomicInt>
22 
23 #include "objectstore_p.h"
24 
25 namespace {
26  class GlobalStore
27  {
28  public:
29  QMutex mutex;
30  QHash<const void *, QAtomicInt> refCount;
31  };
32 }
33 
34 Q_GLOBAL_STATIC(GlobalStore, globalStore)
35 
36 namespace QGst {
37 namespace Private {
38 
39 bool ObjectStore::put(const void * ptr)
40 {
41  bool mustAddStrongRef = false;
42  GlobalStore *const gs = globalStore();
43  if (!gs) return mustAddStrongRef;
44 
45  QMutexLocker lock(&gs->mutex);
46  if (!gs->refCount.contains(ptr)) {
47  gs->refCount.insert(ptr, QAtomicInt(0));
48  mustAddStrongRef = true;
49  }
50  (gs->refCount[ptr]).ref();
51 
52  return mustAddStrongRef;
53 }
54 
55 bool ObjectStore::take(const void * ptr)
56 {
57  bool mustSubtractStrongRef = false;
58  GlobalStore *const gs = globalStore();
59  if (!gs) return mustSubtractStrongRef;
60 
61  QMutexLocker lock(&gs->mutex);
62 
63  //Make sure there are no extra unrefs()
64  Q_ASSERT(gs->refCount.contains(ptr));
65 
66  if (!gs->refCount.contains(ptr)) {
67  return false;
68  }
69 
70  //Decrease our bindings (weak) reference count
71  (gs->refCount[ptr]).deref();
72 
73 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
74  if (!gs->refCount[ptr].load()) {
75 #else
76  if (!gs->refCount[ptr]) {
77 #endif
78  //refCount is 0
79  gs->refCount.remove(ptr);
80  mustSubtractStrongRef = true;
81  }
82  return mustSubtractStrongRef;
83 }
84 
85 bool ObjectStore::isEmpty()
86 {
87  GlobalStore *const gs = globalStore();
88  if (!gs) return true;
89 
90  QMutexLocker lock(&gs->mutex);
91 
92  if (gs->refCount.count()>0) {
93  return false;
94  }
95 
96  return true;
97 }
98 
99 }
100 } //namespace QGst