QtGStreamer  0.10.2
 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 (!gs->refCount[ptr]) {
74  //refCount is 0
75  gs->refCount.remove(ptr);
76  mustSubtractStrongRef = true;
77  }
78  return mustSubtractStrongRef;
79 }
80 
81 bool ObjectStore::isEmpty()
82 {
83  GlobalStore *const gs = globalStore();
84  if (!gs) return true;
85 
86  QMutexLocker lock(&gs->mutex);
87 
88  if (gs->refCount.count()>0) {
89  return false;
90  }
91 
92  return true;
93 }
94 
95 }
96 } //namespace QGst