41 #ifndef __CAThreadSafeList_h__
42 #define __CAThreadSafeList_h__
44 #include "CAAtomicStack.h"
49 class TThreadSafeList {
51 enum EEventType { kAdd, kRemove, kClear };
55 EEventType mEventType;
58 void set_next(Node *node) { mNext = node; }
59 Node * get_next() {
return mNext; }
66 iterator(Node *n) : mNode(n) { }
68 bool operator == (
const iterator &other)
const {
return this->mNode == other.mNode; }
69 bool operator != (
const iterator &other)
const {
return this->mNode != other.mNode; }
71 T & operator * ()
const {
return mNode->mObject; }
73 iterator & operator ++ () { mNode = mNode->get_next();
return *
this; }
74 iterator operator ++ (
int) { iterator tmp = *
this; mNode = mNode->get_next();
return tmp; }
83 mActiveList.free_all();
84 mPendingList.free_all();
90 void deferred_add(
const T &obj)
92 Node *node = AllocNode();
93 node->mEventType = kAdd;
95 mPendingList.push_atomic(node);
99 void deferred_remove(
const T &obj)
101 Node *node = AllocNode();
102 node->mEventType = kRemove;
104 mPendingList.push_atomic(node);
108 void deferred_clear()
110 Node *node = AllocNode();
111 node->mEventType = kClear;
112 mPendingList.push_atomic(node);
120 Node *event, *node, *next;
121 bool workDone =
false;
124 event = mPendingList.pop_all();
125 while (event != NULL) {
127 reversed.push_NA(event);
136 while ((event = reversed.pop_NA()) != NULL) {
137 switch (event->mEventType) {
141 bool needToInsert =
true;
142 for (pnode = mActiveList.phead(); *pnode != NULL; pnode = &node->mNext) {
144 if (node->mObject == event->mObject) {
147 needToInsert =
false;
160 for (Node **pnode = mActiveList.phead(); *pnode != NULL; ) {
162 if (node->mObject == event->mObject) {
163 *pnode = node->mNext;
167 pnode = &node->mNext;
173 for (node = mActiveList.head(); node != NULL; ) {
189 iterator begin()
const {
191 return iterator(mActiveList.head());
193 iterator end()
const {
return iterator(NULL); }
199 Node *node = mFreeList.pop_atomic();
201 node = (Node *)CA_malloc(
sizeof(Node));
205 void FreeNode(Node *node)
207 mFreeList.push_atomic(node);
211 class NodeStack :
public TAtomicStack<Node> {
215 while ((node = this->pop_NA()) != NULL)
219 Node ** phead() {
return &this->mHead; }
220 Node * head()
const {
return this->mHead; }
245 NodeStack mActiveList;
246 NodeStack mPendingList;
250 #endif // __CAThreadSafeList_h__
bool TTFOUNDATION_EXPORT operator!=(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for inequality.
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.