Mercurial > hg > svcore
comparison base/Scavenger.h @ 178:0e266fa2510f
* pthread_mutex -> QMutex
author | Chris Cannam |
---|---|
date | Thu, 05 Oct 2006 11:02:05 +0000 |
parents | 74abef65711b |
children | 218605f94073 |
comparison
equal
deleted
inserted
replaced
177:aff66ec5aea4 | 178:0e266fa2510f |
---|---|
25 #include "system/System.h" | 25 #include "system/System.h" |
26 | 26 |
27 #include <vector> | 27 #include <vector> |
28 #include <list> | 28 #include <list> |
29 #include <sys/time.h> | 29 #include <sys/time.h> |
30 #include <pthread.h> | 30 #include <QMutex> |
31 #include <iostream> | 31 #include <iostream> |
32 | 32 |
33 /** | 33 /** |
34 * A very simple class that facilitates running things like plugins | 34 * A very simple class that facilitates running things like plugins |
35 * without locking, by collecting unwanted objects and deleting them | 35 * without locking, by collecting unwanted objects and deleting them |
67 int m_sec; | 67 int m_sec; |
68 | 68 |
69 typedef std::list<T *> ObjectList; | 69 typedef std::list<T *> ObjectList; |
70 ObjectList m_excess; | 70 ObjectList m_excess; |
71 int m_lastExcess; | 71 int m_lastExcess; |
72 pthread_mutex_t m_excessMutex; | 72 QMutex m_excessMutex; |
73 void pushExcess(T *); | 73 void pushExcess(T *); |
74 void clearExcess(int); | 74 void clearExcess(int); |
75 | 75 |
76 unsigned int m_claimed; | 76 unsigned int m_claimed; |
77 unsigned int m_scavenged; | 77 unsigned int m_scavenged; |
98 m_objects(ObjectTimeList(defaultObjectListSize)), | 98 m_objects(ObjectTimeList(defaultObjectListSize)), |
99 m_sec(sec), | 99 m_sec(sec), |
100 m_claimed(0), | 100 m_claimed(0), |
101 m_scavenged(0) | 101 m_scavenged(0) |
102 { | 102 { |
103 pthread_mutex_init(&m_excessMutex, NULL); | |
104 } | 103 } |
105 | 104 |
106 template <typename T> | 105 template <typename T> |
107 Scavenger<T>::~Scavenger() | 106 Scavenger<T>::~Scavenger() |
108 { | 107 { |
117 } | 116 } |
118 } | 117 } |
119 } | 118 } |
120 | 119 |
121 clearExcess(0); | 120 clearExcess(0); |
122 | |
123 pthread_mutex_destroy(&m_excessMutex); | |
124 } | 121 } |
125 | 122 |
126 template <typename T> | 123 template <typename T> |
127 void | 124 void |
128 Scavenger<T>::claim(T *t) | 125 Scavenger<T>::claim(T *t) |
178 | 175 |
179 template <typename T> | 176 template <typename T> |
180 void | 177 void |
181 Scavenger<T>::pushExcess(T *t) | 178 Scavenger<T>::pushExcess(T *t) |
182 { | 179 { |
183 pthread_mutex_lock(&m_excessMutex); | 180 m_excessMutex.lock(); |
184 m_excess.push_back(t); | 181 m_excess.push_back(t); |
185 struct timeval tv; | 182 struct timeval tv; |
186 (void)gettimeofday(&tv, 0); | 183 (void)gettimeofday(&tv, 0); |
187 m_lastExcess = tv.tv_sec; | 184 m_lastExcess = tv.tv_sec; |
188 pthread_mutex_unlock(&m_excessMutex); | 185 m_excessMutex.unlock(); |
189 } | 186 } |
190 | 187 |
191 template <typename T> | 188 template <typename T> |
192 void | 189 void |
193 Scavenger<T>::clearExcess(int sec) | 190 Scavenger<T>::clearExcess(int sec) |
194 { | 191 { |
195 pthread_mutex_lock(&m_excessMutex); | 192 m_excessMutex.lock(); |
196 for (typename ObjectList::iterator i = m_excess.begin(); | 193 for (typename ObjectList::iterator i = m_excess.begin(); |
197 i != m_excess.end(); ++i) { | 194 i != m_excess.end(); ++i) { |
198 delete *i; | 195 delete *i; |
199 } | 196 } |
200 m_excess.clear(); | 197 m_excess.clear(); |
201 m_lastExcess = sec; | 198 m_lastExcess = sec; |
202 pthread_mutex_unlock(&m_excessMutex); | 199 m_excessMutex.unlock(); |
203 } | 200 } |
204 | 201 |
205 #endif | 202 #endif |