Browse Source

improving ThreadLoader and ThreadDispatcher

dmuratshin 9 years ago
parent
commit
8c6d6de8c9

+ 17 - 3
oxygine/src/ThreadLoader.cpp

@@ -12,8 +12,8 @@ namespace oxygine
 
 
     ThreadLoader::~ThreadLoader()
     ThreadLoader::~ThreadLoader()
     {
     {
-        //if (!pthread_equal(_thread, pthread_self()))
-        //  pthread_join(_thread, 0);
+        if (!pthread_equal(_thread, pthread_self()))
+            pthread_join(_thread, 0);
     }
     }
 
 
 
 
@@ -27,6 +27,11 @@ namespace oxygine
         _ress.push_back(res);
         _ress.push_back(res);
     }
     }
 
 
+    void ThreadLoader::add(std::function< void() > v)
+    {
+        _funcs.push_back(v);
+    }
+
     bool ThreadLoader::isCompleted()
     bool ThreadLoader::isCompleted()
     {
     {
         return _threadDone;
         return _threadDone;
@@ -84,6 +89,11 @@ namespace oxygine
             res->load();
             res->load();
         }
         }
 
 
+        for (funcs::iterator i = _funcs.begin(); i != _funcs.end(); ++i)
+        {
+            (*i)();
+        }
+
         core::getMainThreadDispatcher().postCallback(0, 0, 0, threadDone, this);
         core::getMainThreadDispatcher().postCallback(0, 0, 0, threadDone, this);
     }
     }
 
 
@@ -91,6 +101,10 @@ namespace oxygine
     {
     {
         _threadDone = false;
         _threadDone = false;
         addRef();
         addRef();
-        pthread_create(&_thread, 0, _staticThreadFunc, this);
+
+        pthread_attr_t attr;
+        pthread_attr_init(&attr);
+        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+        pthread_create(&_thread, &attr, _staticThreadFunc, this);
     }
     }
 }
 }

+ 6 - 0
oxygine/src/ThreadLoader.h

@@ -3,6 +3,7 @@
 #include "EventDispatcher.h"
 #include "EventDispatcher.h"
 #include "pthread.h"
 #include "pthread.h"
 #include "core/ThreadDispatcher.h"
 #include "core/ThreadDispatcher.h"
+#include <functional>
 
 
 namespace oxygine
 namespace oxygine
 {
 {
@@ -26,6 +27,8 @@ namespace oxygine
         virtual void add(Resources* res);
         virtual void add(Resources* res);
         virtual void add(Resource* res);
         virtual void add(Resource* res);
 
 
+        virtual void add(std::function< void() >);
+
         void start();
         void start();
         //void stop();
         //void stop();
 
 
@@ -45,5 +48,8 @@ namespace oxygine
 
 
         typedef std::list<Resource*> ress;
         typedef std::list<Resource*> ress;
         ress _ress;
         ress _ress;
+
+        typedef std::list<std::function<void()> > funcs;
+        funcs _funcs;
     };
     };
 }
 }

+ 32 - 0
oxygine/src/core/ThreadDispatcher.cpp

@@ -100,6 +100,38 @@ namespace oxygine
         _popMessage(ev);
         _popMessage(ev);
     }
     }
 
 
+
+    void ThreadDispatcher::get2(message& ev)
+    {
+        {
+            MutexPthreadLock lock(_mutex);
+            LOGDN("get2");
+            _waitMessage();
+
+            _last = _events.front();
+            _events.erase(_events.begin());
+        }
+
+
+        if (_last.cb)
+        {
+            LOGDN("running callback for id=%d", _last._id);
+            _last.cb(_last);
+            _last.cb = 0;
+        }
+
+#ifndef __S3E__
+        if (_last.cbFunction)
+        {
+            LOGDN("running callback function for id=%d", _last._id);
+            _last.cbFunction();
+            _last.cbFunction = std::function< void() >();
+        }
+#endif
+
+    }
+
+
     bool ThreadDispatcher::empty()
     bool ThreadDispatcher::empty()
     {
     {
         MutexPthreadLock lock(_mutex);
         MutexPthreadLock lock(_mutex);

+ 4 - 0
oxygine/src/core/ThreadDispatcher.h

@@ -66,6 +66,9 @@ namespace oxygine
         //blocking
         //blocking
         void get(message& ev);
         void get(message& ev);
 
 
+        //test
+        void get2(message& ev);
+
         bool peek(peekMessage& ev, bool del);
         bool peek(peekMessage& ev, bool del);
         void clear();
         void clear();
 
 
@@ -100,6 +103,7 @@ namespace oxygine
         void _pushMessage(message&);
         void _pushMessage(message&);
         void _pushMessageWaitReply(message&);
         void _pushMessageWaitReply(message&);
         void _popMessage(message&);
         void _popMessage(message&);
+        void _popMessageNoCB(message&);
         void _replyLast(void* val);
         void _replyLast(void* val);
         unsigned int _id;
         unsigned int _id;
         void*   _result;
         void*   _result;

+ 1 - 0
oxygine/src/res/CreateResourceContext.cpp

@@ -228,6 +228,7 @@ namespace oxygine
     void MTLoadingResourcesContext::createTexture(const CreateTextureTask& opt)
     void MTLoadingResourcesContext::createTexture(const CreateTextureTask& opt)
     {
     {
         core::getMainThreadDispatcher().sendCallback(0, 0, copyTexture, (void*)&opt);
         core::getMainThreadDispatcher().sendCallback(0, 0, copyTexture, (void*)&opt);
+        //core::getMainThreadDispatcher().postCallback(0, 0, 0, copyTexture, (void*)&opt);
     }
     }
 
 
     bool MTLoadingResourcesContext::isNeedProceed(spNativeTexture t)
     bool MTLoadingResourcesContext::isNeedProceed(spNativeTexture t)