merge some code with enigma code
authorAndreas Monzner <andreas.monzner@multimedia-labs.de>
Wed, 8 Jun 2005 12:38:15 +0000 (12:38 +0000)
committerAndreas Monzner <andreas.monzner@multimedia-labs.de>
Wed, 8 Jun 2005 12:38:15 +0000 (12:38 +0000)
lib/base/buffer.cpp
lib/base/ebase.h
lib/base/eptrlist.h
lib/base/message.cpp
lib/base/message.h
lib/base/thread.cpp
lib/base/thread.h

index 07e9d7f1c086f55c68b6b0c6b7ace45cbce47da8..9839b3e8536597a7f3cf9c894ddfe09e3c7d6762 100644 (file)
@@ -120,23 +120,22 @@ int eIOBuffer::fromfile(int fd, int len)
        while (len)
        {
                int tc=len;
        while (len)
        {
                int tc=len;
-               int r;
+               int r=0;
                if (buffer.empty() || (allocationsize == buffer.back().len))
                        addblock();
                if (tc > allocationsize-buffer.back().len)
                        tc=allocationsize-buffer.back().len;
                r=::read(fd, buffer.back().data+buffer.back().len, tc);
                buffer.back().len+=r;
                if (buffer.empty() || (allocationsize == buffer.back().len))
                        addblock();
                if (tc > allocationsize-buffer.back().len)
                        tc=allocationsize-buffer.back().len;
                r=::read(fd, buffer.back().data+buffer.back().len, tc);
                buffer.back().len+=r;
-               len-=r;
-               if (r < 0)
+               if (r < 0 && errno != EWOULDBLOCK )
+                       eDebug("couldn't read: %m");
+               else
                {
                {
-                       if (errno != EWOULDBLOCK)
-                               eDebug("read: %m");
-                       r=0;
+                       len-=r;
+                       re+=r;
+                       if (r != tc)
+                               break;
                }
                }
-               re+=r;
-               if (r != tc)
-                       break;
        }
        return re;
 }
        }
        return re;
 }
index d9a17b79881cf7714775f883cbaafe96f2a6dc6a..3bdd705c4289b57bc53dd931175f944ce6084a2c 100644 (file)
@@ -187,7 +187,7 @@ public:
        eMainloop():app_quit_now(0),loop_level(0),retval(0){    }
        void addSocketNotifier(eSocketNotifier *sn);
        void removeSocketNotifier(eSocketNotifier *sn);
        eMainloop():app_quit_now(0),loop_level(0),retval(0){    }
        void addSocketNotifier(eSocketNotifier *sn);
        void removeSocketNotifier(eSocketNotifier *sn);
-       void addTimer(eTimer* e)        {               TimerList.push_back(e);         TimerList.sort();       }
+       void addTimer(eTimer* e)        {               TimerList.insert_in_order(e);   }
        void removeTimer(eTimer* e)     {               TimerList.remove(e);    }
 
        int looplevel() { return loop_level; }
        void removeTimer(eTimer* e)     {               TimerList.remove(e);    }
 
        int looplevel() { return loop_level; }
index 3c21510d2dd31a06df86e0fdf20602b5b4789cb7..ea7e285c79f918b72925762c38736261bfd16350 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list>
 #include <vector>
 
 #include <list>
 #include <vector>
+#include <algorithm>
 #include <lib/base/smartptr.h>
 
 template <class T>
 #include <lib/base/smartptr.h>
 
 template <class T>
@@ -64,55 +65,55 @@ public:
 private:
        iterator cur;
 public:
 private:
        iterator cur;
 public:
-       iterator ePtrList<T>::begin()
+       iterator begin()
        {                               
        //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
                return std::list<T*>::begin();          
        }
 
        {                               
        //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
                return std::list<T*>::begin();          
        }
 
-       iterator ePtrList<T>::end()
+       iterator end()
        {                               
        //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
                return std::list<T*>::end();            
        }
 
        {                               
        //      makes implicit type conversion form std::list::iterator to ePtrList::iterator
                return std::list<T*>::end();            
        }
 
-       const_iterator ePtrList<T>::begin() const
+       const_iterator begin() const
        {                               
        //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
                return std::list<T*>::begin();          
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
                return std::list<T*>::begin();          
        }
 
-       const_iterator ePtrList<T>::end() const
+       const_iterator end() const
        {                               
        //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
                return std::list<T*>::end();            
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator
                return std::list<T*>::end();            
        }
 
-       reverse_iterator ePtrList<T>::rbegin()
+       reverse_iterator rbegin()
        {                               
        //      makes implicit type conversion form std::list::reverse:_iterator to ePtrList::reverse_iterator
                return std::list<T*>::rbegin();         
        }
 
        {                               
        //      makes implicit type conversion form std::list::reverse:_iterator to ePtrList::reverse_iterator
                return std::list<T*>::rbegin();         
        }
 
-       reverse_iterator ePtrList<T>::rend()
+       reverse_iterator rend()
        {                               
        //      makes implicit type conversion form std::list::reverse_iterator to ePtrList::reverse_iterator
                return std::list<T*>::rend();           
        }
 
        {                               
        //      makes implicit type conversion form std::list::reverse_iterator to ePtrList::reverse_iterator
                return std::list<T*>::rend();           
        }
 
-       const_reverse_iterator ePtrList<T>::rbegin() const
+       const_reverse_iterator rbegin() const
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
                return std::list<T*>::rbegin();         
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
                return std::list<T*>::rbegin();         
        }
 
-       const_reverse_iterator ePtrList<T>::rend() const
+       const_reverse_iterator rend() const
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
                return std::list<T*>::rend();           
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to ePtrList::const_reverse_iterator
                return std::list<T*>::rend();           
        }
 
-       iterator ePtrList<T>::erase(iterator it)
+       iterator erase(iterator it)
        {
        //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
        //  If current is equal to the item that was removed, current is set to the next item in the list
        {
        //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
        //  If current is equal to the item that was removed, current is set to the next item in the list
@@ -122,7 +123,7 @@ public:
                        return std::list<T*>::erase(it);
        }
 
                        return std::list<T*>::erase(it);
        }
 
-       iterator ePtrList<T>::erase(iterator from, iterator to)
+       iterator erase(iterator from, iterator to)
        {
        //      Remove all items between the to iterators from and to
        //      If auto-deletion is enabled, than the list call delete for all removed items
        {
        //      Remove all items between the to iterators from and to
        //      If auto-deletion is enabled, than the list call delete for all removed items
@@ -168,12 +169,12 @@ public:
                return v;
        }
 
                return v;
        }
 
-//     inline iterator insert_in_order( T* e )
-//     {
-//             // added a new item to the list... in order
-//             // returns a iterator to the new item
-//             return insert( std::lower_bound( std::list<T*>::begin(), std::list<T*>::end(), e ), e );
-//     }
+       inline iterator insert_in_order( T* e )
+       {
+               // added a new item to the list... in order
+               // returns a iterator to the new item
+               return insert( std::lower_bound( std::list<T*>::begin(), std::list<T*>::end(), e, less()), e );
+       }
 
 };
 
 
 };
 
@@ -693,55 +694,55 @@ public:
 private:
        iterator cur;
 public:
 private:
        iterator cur;
 public:
-       iterator eSmartPtrList<T>::begin()
+       iterator begin()
        {                               
        //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
                return std::list<ePtr<T> >::begin();            
        }
 
        {                               
        //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
                return std::list<ePtr<T> >::begin();            
        }
 
-       iterator eSmartPtrList<T>::end()
+       iterator end()
        {                               
        //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
                return std::list<ePtr<T> >::end();              
        }
 
        {                               
        //      makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator
                return std::list<ePtr<T> >::end();              
        }
 
-       const_iterator eSmartPtrList<T>::begin() const
+       const_iterator begin() const
        {                               
        //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
                return std::list<ePtr<T> >::begin();            
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
                return std::list<ePtr<T> >::begin();            
        }
 
-       const_iterator eSmartPtrList<T>::end() const
+       const_iterator end() const
        {                               
        //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
                return std::list<ePtr<T> >::end();              
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator
                return std::list<ePtr<T> >::end();              
        }
 
-       reverse_iterator eSmartPtrList<T>::rbegin()
+       reverse_iterator rbegin()
        {                               
        //      makes implicit type conversion form std::list::reverse:_iterator to eSmartPtrList::reverse_iterator
                return std::list<ePtr<T> >::rbegin();           
        }
 
        {                               
        //      makes implicit type conversion form std::list::reverse:_iterator to eSmartPtrList::reverse_iterator
                return std::list<ePtr<T> >::rbegin();           
        }
 
-       reverse_iterator eSmartPtrList<T>::rend()
+       reverse_iterator rend()
        {                               
        //      makes implicit type conversion form std::list::reverse_iterator to eSmartPtrList::reverse_iterator
                return std::list<ePtr<T> >::rend();             
        }
 
        {                               
        //      makes implicit type conversion form std::list::reverse_iterator to eSmartPtrList::reverse_iterator
                return std::list<ePtr<T> >::rend();             
        }
 
-       const_reverse_iterator eSmartPtrList<T>::rbegin() const
+       const_reverse_iterator rbegin() const
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
                return std::list<ePtr<T> >::rbegin();           
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
                return std::list<ePtr<T> >::rbegin();           
        }
 
-       const_reverse_iterator eSmartPtrList<T>::rend() const
+       const_reverse_iterator rend() const
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
                return std::list<ePtr<T> >::rend();             
        }
 
        {                               
        //      makes implicit type conversion form std::list::const_reverse_iterator to eSmartPtrList::const_reverse_iterator
                return std::list<ePtr<T> >::rend();             
        }
 
-       iterator eSmartPtrList<T>::erase(iterator it)
+       iterator erase(iterator it)
        {
        //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
        //  If current is equal to the item that was removed, current is set to the next item in the list
        {
        //      Remove the item it, if auto-deletion is enabled, than the list call delete for this item
        //  If current is equal to the item that was removed, current is set to the next item in the list
@@ -752,7 +753,7 @@ public:
                        return std::list<ePtr<T> >::erase(it);
        }
 
                        return std::list<ePtr<T> >::erase(it);
        }
 
-       iterator eSmartPtrList<T>::erase(iterator from, iterator to)
+       iterator erase(iterator from, iterator to)
        {
        //      Remove all items between the to iterators from and to
        //      If auto-deletion is enabled, than the list call delete for all removed items
        {
        //      Remove all items between the to iterators from and to
        //      If auto-deletion is enabled, than the list call delete for all removed items
@@ -798,12 +799,12 @@ public:
                return v;
        }
 
                return v;
        }
 
-//     inline iterator insert_in_order( T* e )
-//     {
-//             // added a new item to the list... in order
-//             // returns a iterator to the new item
-//             return insert( std::lower_bound( std::list<ePtr<T> >::begin(), std::list<ePtr<T> >::end(), e ), e );
-//     }
+       inline iterator insert_in_order( T* e )
+       {
+               // added a new item to the list... in order
+               // returns a iterator to the new item
+               return insert( std::lower_bound( std::list<ePtr<T> >::begin(), e, std::list<ePtr<T> >::end()), e );
+       }
 
 };
 
 
 };
 
@@ -1100,27 +1101,6 @@ inline void eSmartPtrList<T>::push_front(T* x)
        first();        
 }
 
        first();        
 }
 
-/////////////////// eSmartPtrList take() ////////////////////
-//template <class T>
-//inline T* eSmartPtrList<T>::take()
-//{
-//// Takes the current item out of the list without deleting it (even if auto-deletion is enabled).
-//// Returns a pointer to the item taken out of the list, or null if the index is out of range.
-//// The item after the taken item becomes the new current list item if the taken item is not the last item in the list. If the last item is taken, the new last item becomes the current item.
-//// The current item is set to null if the list becomes empty.
-//     T* tmp = *cur;
-//     cur = std::list<T*>::erase(cur);
-//     return tmp;
-//}
-
-/////////////////// eSmartPtrList take(T*) ////////////////////
-//template <class T>
-//inline void eSmartPtrList<T>::take(T* t)
-//{
-//// Takes all item with T* out of the list without deleting it (even if auto-deletion is enabled).
-//     std::list<T*>::remove(t);
-//}
-
 /////////////////// eSmartPtrList setCurrent(T*) ////////////////////
 template <class T>
 inline T* eSmartPtrList<T>::setCurrent(const T* t)
 /////////////////// eSmartPtrList setCurrent(T*) ////////////////////
 template <class T>
 inline T* eSmartPtrList<T>::setCurrent(const T* t)
index dafbf3f88c0b9a49988daa365574b7bc379bd822..48fcd21035886d5ccce41f868cffc3aad792cd85 100644 (file)
@@ -17,25 +17,19 @@ eMessagePump::~eMessagePump()
 
 int eMessagePump::send(const void *data, int len)
 {
 
 int eMessagePump::send(const void *data, int len)
 {
-       if (ismt)
-               content.lock(len);
-       return ::write(fd[1], data, len)<0;
+       int wr = ::write(fd[1], data, len);
+       if (ismt && wr > 0)
+               content.lock(wr);
+       return wr<0;
 }
 
 int eMessagePump::recv(void *data, int len)
 {
        unsigned char*dst=(unsigned char*)data;
 }
 
 int eMessagePump::recv(void *data, int len)
 {
        unsigned char*dst=(unsigned char*)data;
-       while (len)
-       {
-               if (ismt)
-                       content.unlock(len);
-               int r=::read(fd[0], dst, len);
-               if (r<0)
-                       return r;
-               dst+=r;
-               len-=r;
-       }
-       return 0;
+       int recv=::read(fd[0], dst, len);
+       if (recv > 0 && ismt)
+               content.unlock(recv);
+       return recv;
 }
 
 int eMessagePump::getInputFD() const
 }
 
 int eMessagePump::getInputFD() const
index 6a9ff43ede2bbc462ed82b44deffd7c99c2719e0..de14db0fd61cda4c70587685e5f76e3b6bb88444 100644 (file)
@@ -18,7 +18,7 @@ class eMessagePump
        int ismt;
 public:
        eMessagePump(int mt=0);
        int ismt;
 public:
        eMessagePump(int mt=0);
-       ~eMessagePump();
+       virtual ~eMessagePump();
        int send(const void *data, int len);
        int recv(void *data, int len); // blockierend
        int getInputFD() const;
        int send(const void *data, int len);
        int recv(void *data, int len); // blockierend
        int getInputFD() const;
@@ -56,9 +56,10 @@ public:
        ~eFixedMessagePump()
        {
                delete sn;
        ~eFixedMessagePump()
        {
                delete sn;
+               sn=0;
        }
        }
-       void start() { sn->start(); }
-       void stop() { sn->stop(); }
+       void start() { if (sn) sn->start(); }
+       void stop() { if (sn) sn->stop(); }
 };
 
 #endif
 };
 
 #endif
index b75378b88ba83028a35a47e265d1c87ea3e34300..fa09691a37162be4f7d664edef7142d5186ca99c 100644 (file)
@@ -1,40 +1,87 @@
 #include <lib/base/thread.h>
 #include <lib/base/thread.h>
+
 #include <stdio.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <lib/base/eerror.h>
 
 #include <lib/base/eerror.h>
 
+void eThread::thread_completed(void *ptr)
+{
+       eThread *p = (eThread*) ptr;
+       eDebug("thread has completed..");
+       p->alive=0;
+       p->thread_finished();
+}
+
 void *eThread::wrapper(void *ptr)
 {
 void *eThread::wrapper(void *ptr)
 {
-       ((eThread*)ptr)->thread();
+       eThread *p = (eThread*)ptr;
+       p->alive=1;
+       pthread_cleanup_push( thread_completed, (void*)p );
+       p->thread();
        pthread_exit(0);
        pthread_exit(0);
+       pthread_cleanup_pop(0);
 }
 
 eThread::eThread()
 }
 
 eThread::eThread()
+       :alive(0)
 {
 {
-       alive=0;
 }
 
 }
 
-void eThread::run()
+void eThread::run( int prio, int policy )
 {
 {
-       alive=1;
-       pthread_create(&the_thread, 0, wrapper, this);
-}
+       pthread_attr_t attr;
+       pthread_attr_init(&attr);
+       if (prio||policy)
+       {
+               struct sched_param p;
+               p.__sched_priority=prio;
+               pthread_attr_setschedpolicy(&attr, policy );
+               pthread_attr_setschedparam(&attr, &p);
+       }
+       pthread_create(&the_thread, &attr, wrapper, this);
+       usleep(1000);
+       int timeout=20;
+       while(!alive && timeout--)
+       {
+               eDebug("waiting for thread start...");
+               usleep(1000*10);
+       }
+       if ( !timeout )
+               eDebug("thread couldn't be started !!!");
+}                     
 
 eThread::~eThread()
 {
 
 eThread::~eThread()
 {
-       if (alive)
+       if ( alive )
                kill();
 }
 
                kill();
 }
 
-void eThread::kill()
+void eThread::sendSignal(int sig)
 {
 {
-       alive=0;
-       eDebug("waiting for thread shutdown");
-       pthread_join(the_thread, 0);
-       eDebug("ok");
+       if ( alive )
+               pthread_kill( the_thread, sig );
+       else 
+               eDebug("send signal to non running thread");
 }
 
 }
 
-void eThread::sendSignal(int sig)
+void eThread::kill(bool hard)
 {
 {
-       if (alive)
-               pthread_kill(the_thread, sig);
+       if ( !alive )
+       {
+               eDebug("kill.. but thread don't running");
+               return;
+       }
+
+       if ( hard )
+       {
+               eDebug("killing the thread...");
+               pthread_cancel(the_thread);
+               alive=0;
+       }
+       else
+       {
+               eDebug("waiting for thread shutdown...");
+               pthread_join(the_thread, 0);
+               eDebug("ok");
+       }
 }
 }
index 629a96566c566410dfaa9bdc081ecd592723cf10..f71300579238c22b839f14e69d051e0a1776b30c 100644 (file)
@@ -9,17 +9,19 @@ class eThread
        pthread_t the_thread;
        static void *wrapper(void *ptr);
        int alive;
        pthread_t the_thread;
        static void *wrapper(void *ptr);
        int alive;
+       static void thread_completed(void *p);
 public:
        bool thread_running() { return alive; }
        eThread();
        virtual ~eThread();
        
 public:
        bool thread_running() { return alive; }
        eThread();
        virtual ~eThread();
        
-       void run();
+       void run(int prio=0,int policy=0);
 
        virtual void thread()=0;
 
        virtual void thread()=0;
-       
+       virtual void thread_finished() { }
+
        void sendSignal(int sig);
        void sendSignal(int sig);
-       void kill();
+       void kill(bool hard=false);
 };
 
 #endif
 };
 
 #endif