From: Andreas Monzner Date: Wed, 8 Jun 2005 12:38:15 +0000 (+0000) Subject: merge some code with enigma code X-Git-Tag: 2.6.0~5818 X-Git-Url: https://git.cweiske.de/enigma2.git/commitdiff_plain/62b8a649fcae500c983215fac2e5202916c0195f merge some code with enigma code --- diff --git a/lib/base/buffer.cpp b/lib/base/buffer.cpp index 07e9d7f1..9839b3e8 100644 --- a/lib/base/buffer.cpp +++ b/lib/base/buffer.cpp @@ -120,23 +120,22 @@ int eIOBuffer::fromfile(int fd, int 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; - 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; } diff --git a/lib/base/ebase.h b/lib/base/ebase.h index d9a17b79..3bdd705c 100644 --- a/lib/base/ebase.h +++ b/lib/base/ebase.h @@ -187,7 +187,7 @@ public: 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; } diff --git a/lib/base/eptrlist.h b/lib/base/eptrlist.h index 3c21510d..ea7e285c 100644 --- a/lib/base/eptrlist.h +++ b/lib/base/eptrlist.h @@ -3,6 +3,7 @@ #include #include +#include #include template @@ -64,55 +65,55 @@ public: private: iterator cur; public: - iterator ePtrList::begin() + iterator begin() { // makes implicit type conversion form std::list::iterator to ePtrList::iterator return std::list::begin(); } - iterator ePtrList::end() + iterator end() { // makes implicit type conversion form std::list::iterator to ePtrList::iterator return std::list::end(); } - const_iterator ePtrList::begin() const + const_iterator begin() const { // makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator return std::list::begin(); } - const_iterator ePtrList::end() const + const_iterator end() const { // makes implicit type conversion form std::list::const_iterator to ePtrList::const_iterator return std::list::end(); } - reverse_iterator ePtrList::rbegin() + reverse_iterator rbegin() { // makes implicit type conversion form std::list::reverse:_iterator to ePtrList::reverse_iterator return std::list::rbegin(); } - reverse_iterator ePtrList::rend() + reverse_iterator rend() { // makes implicit type conversion form std::list::reverse_iterator to ePtrList::reverse_iterator return std::list::rend(); } - const_reverse_iterator ePtrList::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::rbegin(); } - const_reverse_iterator ePtrList::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::rend(); } - iterator ePtrList::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 @@ -122,7 +123,7 @@ public: return std::list::erase(it); } - iterator ePtrList::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 @@ -168,12 +169,12 @@ public: 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::begin(), std::list::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::begin(), std::list::end(), e, less()), e ); + } }; @@ -693,55 +694,55 @@ public: private: iterator cur; public: - iterator eSmartPtrList::begin() + iterator begin() { // makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator return std::list >::begin(); } - iterator eSmartPtrList::end() + iterator end() { // makes implicit type conversion form std::list::iterator to eSmartPtrList::iterator return std::list >::end(); } - const_iterator eSmartPtrList::begin() const + const_iterator begin() const { // makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator return std::list >::begin(); } - const_iterator eSmartPtrList::end() const + const_iterator end() const { // makes implicit type conversion form std::list::const_iterator to eSmartPtrList::const_iterator return std::list >::end(); } - reverse_iterator eSmartPtrList::rbegin() + reverse_iterator rbegin() { // makes implicit type conversion form std::list::reverse:_iterator to eSmartPtrList::reverse_iterator return std::list >::rbegin(); } - reverse_iterator eSmartPtrList::rend() + reverse_iterator rend() { // makes implicit type conversion form std::list::reverse_iterator to eSmartPtrList::reverse_iterator return std::list >::rend(); } - const_reverse_iterator eSmartPtrList::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 >::rbegin(); } - const_reverse_iterator eSmartPtrList::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 >::rend(); } - iterator eSmartPtrList::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 @@ -752,7 +753,7 @@ public: return std::list >::erase(it); } - iterator eSmartPtrList::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 @@ -798,12 +799,12 @@ public: 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 >::begin(), std::list >::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 >::begin(), e, std::list >::end()), e ); + } }; @@ -1100,27 +1101,6 @@ inline void eSmartPtrList::push_front(T* x) first(); } -/////////////////// eSmartPtrList take() //////////////////// -//template -//inline T* eSmartPtrList::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::erase(cur); -// return tmp; -//} - -/////////////////// eSmartPtrList take(T*) //////////////////// -//template -//inline void eSmartPtrList::take(T* t) -//{ -//// Takes all item with T* out of the list without deleting it (even if auto-deletion is enabled). -// std::list::remove(t); -//} - /////////////////// eSmartPtrList setCurrent(T*) //////////////////// template inline T* eSmartPtrList::setCurrent(const T* t) diff --git a/lib/base/message.cpp b/lib/base/message.cpp index dafbf3f8..48fcd210 100644 --- a/lib/base/message.cpp +++ b/lib/base/message.cpp @@ -17,25 +17,19 @@ eMessagePump::~eMessagePump() 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; - 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 diff --git a/lib/base/message.h b/lib/base/message.h index 6a9ff43e..de14db0f 100644 --- a/lib/base/message.h +++ b/lib/base/message.h @@ -18,7 +18,7 @@ class eMessagePump 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; @@ -56,9 +56,10 @@ public: ~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 diff --git a/lib/base/thread.cpp b/lib/base/thread.cpp index b75378b8..fa09691a 100644 --- a/lib/base/thread.cpp +++ b/lib/base/thread.cpp @@ -1,40 +1,87 @@ #include + #include +#include #include +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) { - ((eThread*)ptr)->thread(); + eThread *p = (eThread*)ptr; + p->alive=1; + pthread_cleanup_push( thread_completed, (void*)p ); + p->thread(); pthread_exit(0); + pthread_cleanup_pop(0); } 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() { - if (alive) + if ( alive ) 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"); + } } diff --git a/lib/base/thread.h b/lib/base/thread.h index 629a9656..f7130057 100644 --- a/lib/base/thread.h +++ b/lib/base/thread.h @@ -9,17 +9,19 @@ class eThread 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(); - void run(); + void run(int prio=0,int policy=0); virtual void thread()=0; - + virtual void thread_finished() { } + void sendSignal(int sig); - void kill(); + void kill(bool hard=false); }; #endif