Commit 17d4bb12 authored by captainwong's avatar captainwong

add lock for observer list

parent 7ef14a28
...@@ -14,6 +14,7 @@ protected: \ ...@@ -14,6 +14,7 @@ protected: \
typedef callback _callback; \ typedef callback _callback; \
typedef param_type _param_type; \ typedef param_type _param_type; \
std::list<_callbackInfo *> _observerList; \ std::list<_callbackInfo *> _observerList; \
CLock _lock4ObserverList; \
public: \ public: \
void RegisterObserver(void* udata, callback cb); \ void RegisterObserver(void* udata, callback cb); \
void UnRegisterObserver(void* udata); \ void UnRegisterObserver(void* udata); \
...@@ -24,19 +25,23 @@ public: \ ...@@ -24,19 +25,23 @@ public: \
#define IMPLEMENT_OBSERVER(class_name) \ #define IMPLEMENT_OBSERVER(class_name) \
void class_name::RegisterObserver(void* udata, _callback cb) \ void class_name::RegisterObserver(void* udata, _callback cb) \
{ \ { \
_lock4ObserverList.Lock(); \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \ std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \ while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \ _callbackInfo* observer = *iter; \
if (observer->_udata == udata) { \ if (observer->_udata == udata) { \
_lock4ObserverList.UnLock(); \
return; \ return; \
} \ } \
iter++; \ iter++; \
} \ } \
_callbackInfo *observer = new _callbackInfo(udata, cb); \ _callbackInfo *observer = new _callbackInfo(udata, cb); \
_observerList.insert(iter, observer); \ _observerList.insert(iter, observer); \
_lock4ObserverList.UnLock(); \
} \ } \
void class_name::UnRegisterObserver(void* udata) \ void class_name::UnRegisterObserver(void* udata) \
{ \ { \
_lock4ObserverList.Lock(); \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \ std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \ while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \ _callbackInfo* observer = *iter; \
...@@ -47,14 +52,17 @@ void class_name::UnRegisterObserver(void* udata) \ ...@@ -47,14 +52,17 @@ void class_name::UnRegisterObserver(void* udata) \
} \ } \
iter++; \ iter++; \
} \ } \
_lock4ObserverList.UnLock(); \
} \ } \
void class_name::NotifyObservers(_param_type param) \ void class_name::NotifyObservers(_param_type param) \
{ \ { \
_lock4ObserverList.Lock(); \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \ std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \ while (iter != _observerList.end()) { \
_callbackInfo * observer = *iter++; \ _callbackInfo * observer = *iter++; \
observer->_on_result(observer->_udata, param); \ observer->_on_result(observer->_udata, param); \
} \ } \
_lock4ObserverList.UnLock(); \
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment