Use smart pointers to hold references of global Application::_scheduler (#1732)

* use std::weak_ptr

* revert build/libcocos2d.vcxproj

* fix mac & ios & android

* declare variable in if block

* revert build/libcocos2d.vcxproj
This commit is contained in:
Arnold
2019-04-25 18:43:01 +08:00
committed by Jare Guo
parent 57554f4ed9
commit d88d4ffead
12 changed files with 85 additions and 49 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ private:
bool _lazyInitLoop;
int _currentAudioID;
Scheduler* _scheduler;
std::weak_ptr<Scheduler> _scheduler;
};
NS_CC_END
#endif // __AUDIO_ENGINE_INL_H_
+16 -10
View File
@@ -236,16 +236,15 @@ ALvoid AudioEngineImpl::myAlSourceNotificationCallback(ALuint sid, ALuint notifi
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, _currentAudioID(0)
, _scheduler(nullptr)
{
s_instance = this;
}
AudioEngineImpl::~AudioEngineImpl()
{
if (_scheduler != nullptr)
if (auto sche = _scheduler.lock())
{
_scheduler->unschedule("AudioEngine", this);
sche->unschedule("AudioEngine", this);
}
if (s_ALContext) {
@@ -430,7 +429,10 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
if (_lazyInitLoop) {
_lazyInitLoop = false;
_scheduler->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine");
if(auto sche = _scheduler.lock())
{
sche->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine");
}
}
return _currentAudioID++;
@@ -444,12 +446,14 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
_threadMutex.lock();
auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
_scheduler->performFunctionInCocosThread([audioID](){
if(auto sche = _scheduler.lock()){
sche->performFunctionInCocosThread([audioID](){
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
}
}
_threadMutex.unlock();
}
@@ -695,7 +699,9 @@ void AudioEngineImpl::update(float dt)
if(_audioPlayers.empty()){
_lazyInitLoop = true;
_scheduler->unschedule("AudioEngine", this);
if(auto sche = _scheduler.lock()) {
sche->unschedule("AudioEngine", this);
}
}
}
+18 -10
View File
@@ -112,16 +112,15 @@ static ALCcontext *s_ALContext = nullptr;
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, _currentAudioID(0)
, _scheduler(nullptr)
{
}
AudioEngineImpl::~AudioEngineImpl()
{
if (_scheduler != nullptr)
if (auto sche = _scheduler.lock())
{
_scheduler->unschedule("AudioEngine", this);
sche->unschedule("AudioEngine", this);
}
if (s_ALContext) {
@@ -251,7 +250,10 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
if (_lazyInitLoop) {
_lazyInitLoop = false;
_scheduler->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine");
if(auto sche = _scheduler.lock())
{
sche->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine");
}
}
return _currentAudioID++;
@@ -265,12 +267,15 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
_threadMutex.lock();
auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
_scheduler->performFunctionInCocosThread([audioID](){
if (auto sche = _scheduler.lock())
{
sche->performFunctionInCocosThread([audioID]() {
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
}
}
_threadMutex.unlock();
}
@@ -513,7 +518,10 @@ void AudioEngineImpl::update(float dt)
if(_audioPlayers.empty()){
_lazyInitLoop = true;
_scheduler->unschedule("AudioEngine", this);
if(auto sche = _scheduler.lock())
{
sche->unschedule("AudioEngine", this);
}
}
}
+1 -1
View File
@@ -84,7 +84,7 @@ private:
bool _lazyInitLoop;
int _currentAudioID;
Scheduler* _scheduler;
std::weak_ptr<Scheduler> _scheduler;
};
NS_CC_END
#endif // __AUDIO_ENGINE_INL_H_
+17 -4
View File
@@ -729,16 +729,22 @@ namespace cocos2d { namespace network {
sprintf(key, "DownloaderCURL(%p)", this);
_schedulerKey = key;
_scheduler->schedule(bind(&DownloaderCURL::_onSchedule, this, placeholders::_1),
if(auto sche = _scheduler.lock())
{
sche->schedule(bind(&DownloaderCURL::_onSchedule, this, placeholders::_1),
this,
0.1f,
true,
_schedulerKey);
}
}
DownloaderCURL::~DownloaderCURL()
{
_scheduler->unschedule(_schedulerKey, this);
if(auto sche = _scheduler.lock())
{
sche->unschedule(_schedulerKey, this);
}
_impl->stop();
DLLOG("Destruct DownloaderCURL %p", this);
@@ -753,7 +759,11 @@ namespace cocos2d { namespace network {
_impl->addTask(task, coTask);
_impl->run();
_scheduler->resumeTarget(this);
if(auto sche = _scheduler.lock())
{
sche->resumeTarget(this);
}
return coTask;
}
@@ -793,7 +803,10 @@ namespace cocos2d { namespace network {
_impl->getFinishedTasks(tasks);
if (_impl->stoped())
{
_scheduler->pauseTarget(this);
if (auto sche = _scheduler.lock())
{
sche->pauseTarget(this);
}
}
for (auto& wrapper : tasks)
+2 -2
View File
@@ -56,8 +56,8 @@ namespace cocos2d { namespace network
// scheduler for update processing and finished task in main schedule
void _onSchedule(float);
std::string _schedulerKey;
Scheduler* _scheduler;
std::string _schedulerKey;
std::weak_ptr<Scheduler> _scheduler;
};
}} // namespace cocos2d::network
+8 -6
View File
@@ -838,9 +838,9 @@ void HttpClient::networkThread()
_responseQueueMutex.unlock();
_schedulerMutex.lock();
if (nullptr != _scheduler)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
}
_schedulerMutex.unlock();
}
@@ -866,9 +866,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response
processResponse(response, responseMessage);
_schedulerMutex.lock();
if (_scheduler != nullptr)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread([this, response, request]{
sche->performFunctionInCocosThread([this, response, request]{
const ccHttpRequestCallback& callback = request->getResponseCallback();
if (callback != nullptr)
@@ -909,10 +909,12 @@ void HttpClient::destroyInstance()
auto thiz = _httpClient;
_httpClient = nullptr;
thiz->_scheduler->unscheduleAllForTarget(thiz);
if(auto sche = thiz->_scheduler.lock()) {
sche->unscheduleAllForTarget(thiz);
}
thiz->_schedulerMutex.lock();
thiz->_scheduler = nullptr;
thiz->_scheduler.reset();
thiz->_schedulerMutex.unlock();
{
+9 -6
View File
@@ -76,9 +76,9 @@ void HttpClient::networkThread()
_responseQueueMutex.unlock();
_schedulerMutex.lock();
if (nullptr != _scheduler)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
}
_schedulerMutex.unlock();
}
@@ -104,9 +104,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response
processResponse(response, responseMessage);
_schedulerMutex.lock();
if (nullptr != _scheduler)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread([this, response, request]{
sche->performFunctionInCocosThread([this, response, request]{
const ccHttpRequestCallback& callback = request->getResponseCallback();
if (callback != nullptr)
@@ -312,9 +312,12 @@ void HttpClient::destroyInstance()
auto thiz = _httpClient;
_httpClient = nullptr;
thiz->_scheduler->unscheduleAllForTarget(thiz);
if(auto sche = thiz->_scheduler.lock())
{
sche->unscheduleAllForTarget(thiz);
}
thiz->_schedulerMutex.lock();
thiz->_scheduler = nullptr;
thiz->_scheduler.reset();
thiz->_schedulerMutex.unlock();
thiz->_requestQueueMutex.lock();
+10 -6
View File
@@ -115,9 +115,9 @@ void HttpClient::networkThread()
_responseQueueMutex.unlock();
_schedulerMutex.lock();
if (nullptr != _scheduler)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
}
_schedulerMutex.unlock();
}
@@ -143,9 +143,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response
processResponse(response, responseMessage);
_schedulerMutex.lock();
if (nullptr != _scheduler)
if (auto sche = _scheduler.lock())
{
_scheduler->performFunctionInCocosThread([this, response, request]{
sche->performFunctionInCocosThread([this, response, request]{
const ccHttpRequestCallback& callback = request->getResponseCallback();
if (callback != nullptr)
@@ -356,9 +356,13 @@ void HttpClient::destroyInstance()
auto thiz = _httpClient;
_httpClient = nullptr;
thiz->_scheduler->unscheduleAllForTarget(thiz);
if(auto sche = thiz->_scheduler.lock())
{
sche->unscheduleAllForTarget(thiz);
}
thiz->_schedulerMutex.lock();
thiz->_scheduler = nullptr;
thiz->_scheduler.reset();
thiz->_schedulerMutex.unlock();
thiz->_requestQueueMutex.lock();
+1 -1
View File
@@ -185,7 +185,7 @@ private:
int _threadCount;
std::mutex _threadCountMutex;
Scheduler* _scheduler;
std::weak_ptr<Scheduler> _scheduler;
std::mutex _schedulerMutex;
Vector<HttpRequest*> _requestQueue;
+1 -1
View File
@@ -109,7 +109,7 @@ public:
virtual void applicationWillEnterForeground();
inline void* getView() const { return _view; }
inline Scheduler* getScheduler() const { return _scheduler.get(); }
inline std::shared_ptr<Scheduler> getScheduler() const { return _scheduler; }
inline RenderTexture* getRenderTexture() const { return _renderTexture; }
void runOnMainThread();
@@ -72,7 +72,7 @@ namespace
float _systemVersion;
BOOL _isAppActive;
cocos2d::Application* _application;
cocos2d::Scheduler* _scheduler;
std::shared_ptr<cocos2d::Scheduler> _scheduler;
}
-(void) startMainLoop;
-(void) stopMainLoop;