Thread: [complement-svn] SF.net SVN: complement: [1919] branches/complement-sockios/explore (Page 2)
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-06-26 05:47:14
|
Revision: 1919 http://complement.svn.sourceforge.net/complement/?rev=1919&view=rev Author: complement Date: 2008-06-25 22:47:07 -0700 (Wed, 25 Jun 2008) Log Message: ----------- Use explicit counter (entry/exit) in tests when possible. Comment debug prints; code more-or-less work, but still remains unclean issues: duplicate removes from epoll vector (may be dangerous, due to reuse descriptor possibility); connection may be accepted after sockmgr's loop finished (?) --- may lead to stalling on server dtor; issue really unclean. Modified Paths: -------------- branches/complement-sockios/explore/include/sockios/socksrv.cc branches/complement-sockios/explore/include/sockios/socksrv.h branches/complement-sockios/explore/include/sockios/sockstream branches/complement-sockios/explore/include/sockios/sockstream.cc branches/complement-sockios/explore/include/sockios/sp.cc branches/complement-sockios/explore/include/sockios/sp.h branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc Modified: branches/complement-sockios/explore/include/sockios/socksrv.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/socksrv.cc 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/socksrv.cc 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 21:28:07 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 09:00:54 ptr> /* * Copyright (c) 2008 @@ -72,7 +72,7 @@ if ( !basic_socket_t::is_open_unsafe() ) { return; } - std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; #ifdef WIN32 ::closesocket( basic_socket_t::_fd ); @@ -87,7 +87,7 @@ template<class charT, class traits, class _Alloc> void sock_processor_base<charT,traits,_Alloc>::shutdown( sock_base::shutdownflg dir ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; std::tr2::lock_guard<std::tr2::mutex> lk(_fd_lck); if ( basic_socket_t::is_open_unsafe() ) { @@ -186,27 +186,6 @@ template<class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> char connect_processor<Connect, charT, traits, _Alloc, C>::Init_buf[128]; -template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> -void connect_processor<Connect, charT, traits, _Alloc, C>::_close() -{ - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - base_t::_close(); - -#if 0 - { - std::tr2::lock_guard<std::tr2::mutex> lk(inwlock); - _in_work = false; // <--- set before cnd.notify_one(); (below in this func) - } - - { - std::tr2::lock_guard<std::tr2::mutex> lk2( rdlock ); - ready_pool.push_back( processor() ); // make ready_pool not empty - // std::cerr << "=== " << ready_pool.size() << std::endl; - cnd.notify_one(); - } -#endif -} - template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> typename connect_processor<Connect, charT, traits, _Alloc, C>::base_t::sockbuf_t* connect_processor<Connect, charT, traits, _Alloc, C>::operator ()( sock_base::socket_type fd, const sockaddr& addr ) { @@ -221,12 +200,12 @@ if ( s->rdbuf()->in_avail() ) { std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); ready_pool.push_back( processor( c, s ) ); - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; cnd.notify_one(); } else { std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); worker_pool.insert( std::make_pair( fd, processor( c, s ) ) ); - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; } return s->rdbuf(); @@ -235,15 +214,14 @@ template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> void connect_processor<Connect, charT, traits, _Alloc, C>::operator ()( sock_base::socket_type fd, const typename connect_processor<Connect, charT, traits, _Alloc, C>::base_t::adopt_close_t& ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; { std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); typename worker_pool_t::iterator i = worker_pool.find( fd ); if ( i != worker_pool.end() ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; delete i->second.c; delete i->second.s; - // std::cerr << "oops\n"; worker_pool.erase( i ); return; } @@ -254,15 +232,12 @@ std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); typename ready_pool_t::iterator j = std::find( ready_pool.begin(), ready_pool.end(), /* std::bind2nd( typename processor::equal_to(), &s ) */ fd ); if ( j != ready_pool.end() ) { - // std::cerr << "oops 2\n"; p = *j; ready_pool.erase( j ); } } if ( p.c != 0 ) { - // (*p.c)( *p.s ); - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; - + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; (p.c->*C)( *p.s ); delete p.c; @@ -273,7 +248,7 @@ template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> void connect_processor<Connect, charT, traits, _Alloc, C>::operator ()( sock_base::socket_type fd ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; processor p; @@ -303,16 +278,11 @@ return false; } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; cnd.wait( lk, not_empty ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; p = ready_pool.front(); // it may contain p.c == 0, p.s == 0, if !in_work() ready_pool.pop_front(); -#if 0 - if ( p.c == 0 ) { // wake up, but _in_work may be still true here (in processor pipe?), - return false; // even I know that _in_work <- false before notification... - } // so, check twice -#endif if ( _in_work ) { return true; @@ -328,7 +298,7 @@ while ( pop_ready( p ) ) { if ( p.c != 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; (p.c->*C)( *p.s ); if ( p.s->rdbuf()->in_avail() ) { std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); @@ -337,30 +307,23 @@ std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); worker_pool[p.s->rdbuf()->fd()] = p; } - } else { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - } + } // else { + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // } } - { - std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); - std::cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << std::endl; - } +// { +// std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); +// std::cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << std::endl; +// } - { - std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); - std::cerr << __FILE__ << ":" << __LINE__ << " " << ready_pool.size() << std::endl; - } +// { +// std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); +// std::cerr << __FILE__ << ":" << __LINE__ << " " << ready_pool.size() << std::endl; +// } } - template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> -void connect_processor<Connect, charT, traits, _Alloc, C>::stop() -{ - _stop(); -} - -template <class Connect, class charT, class traits, class _Alloc, void (Connect::*C)( std::basic_sockstream<charT,traits,_Alloc>& )> void connect_processor<Connect, charT, traits, _Alloc, C>::_stop() { std::tr2::lock_guard<std::tr2::mutex> lk2( rdlock ); @@ -368,11 +331,11 @@ _in_work = false; // <--- set before cnd.notify_one(); (below in this func) if ( ready_pool.empty() ) { ready_pool.push_back( processor() ); // make ready_pool not empty - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; cnd.notify_one(); - } else { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - } + } // else { + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // } } } // namespace std Modified: branches/complement-sockios/explore/include/sockios/socksrv.h =================================================================== --- branches/complement-sockios/explore/include/sockios/socksrv.h 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/socksrv.h 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 21:30:31 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 08:59:54 ptr> /* * Copyright (c) 2008 @@ -65,14 +65,8 @@ { sock_processor_base::open( port, t, sock_base::inet ); } virtual ~sock_processor_base() - { - sock_processor_base::_close(); + { sock_processor_base::_close(); } - // Never uncomment next line: - // basic_socket<charT,traits,_Alloc>::mgr->final( *this ); - // this lead to virtual fuction call, that is already pure here. - } - void open( const in_addr& addr, int port, sock_base::stype type, sock_base::protocol prot ); void open( unsigned long addr, int port, sock_base::stype type, sock_base::protocol prot ) @@ -87,14 +81,8 @@ virtual void close() { _close(); } -#if 0 - virtual void stop() = 0; -#else virtual void stop() - { /* abort(); */ } - // void stop() - // { (this->*_real_stop)(); } -#endif + { } #if 0 virtual sockbuf_t* operator ()( sock_base::socket_type fd, const sockaddr& ) = 0; @@ -204,45 +192,23 @@ ploop.join(); } - // basic_socket<charT,traits,_Alloc>::mgr->final( *this ); + // { + // std::tr2::lock_guard<std::tr2::mutex> lk2( rdlock ); + // cerr << __FILE__ << ":" << __LINE__ << " " << ready_pool.size() << endl; + // } -#if 0 - { - std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); - std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); - if ( worker_pool.empty() && ready_pool.empty() ) { - break; - } + // { + // std::tr2::lock_guard<std::tr2::mutex> lk2( wklock ); + // cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << endl; + // } - for ( ; ; ) { - - } - } -#endif - - - { - std::tr2::lock_guard<std::tr2::mutex> lk2( rdlock ); - cerr << __FILE__ << ":" << __LINE__ << " " << ready_pool.size() << endl; - } - - { - std::tr2::lock_guard<std::tr2::mutex> lk2( wklock ); - cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << endl; -#if 0 - for ( typename worker_pool_t::iterator i = worker_pool.begin(); i != worker_pool.end(); ++i ) { - delete i->second.c; - delete i->second.s; - } -#endif - } - ((Init *)Init_buf)->~Init(); } virtual void close() { connect_processor::_close(); } - virtual void stop(); + virtual void stop() + { connect_processor::_stop(); } void wait() { if ( ploop.joinable() ) { ploop.join(); } } @@ -304,7 +270,8 @@ }; bool pop_ready( processor& ); - void _close(); + void _close() + { base_t::_close(); } void _stop(); #ifdef __USE_STLPORT_HASH Modified: branches/complement-sockios/explore/include/sockios/sockstream =================================================================== --- branches/complement-sockios/explore/include/sockios/sockstream 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/sockstream 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/17 17:09:45 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 08:57:00 ptr> /* * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 @@ -448,8 +448,7 @@ virtual ~basic_sockbuf() { - std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; - close(); + basic_sockbuf::close(); _M_deallocate_block(); } Modified: branches/complement-sockios/explore/include/sockios/sockstream.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/sockstream.cc 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/sockstream.cc 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 18:48:36 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 08:40:03 ptr> /* * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 @@ -273,7 +273,7 @@ setg( this->epptr(), this->epptr(), this->epptr() ); // if ( basic_socket_t::_notify_close ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; basic_socket_t::mgr->exit_notify( this, basic_socket_t::_fd ); // basic_socket_t::_notify_close = false; // } @@ -393,22 +393,22 @@ template<class charT, class traits, class _Alloc> int basic_sockbuf<charT, traits, _Alloc>::sync() { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( !basic_socket_t::is_open() ) { return -1; } long count = this->pptr() - this->pbase(); if ( count ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // _STLP_ASSERT( this->pbase() != 0 ); count *= sizeof(charT); long start = 0; while ( count > 0 ) { long offset = (this->*_xwrite)( this->pbase() + start, count ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( offset < 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( errno == EINTR ) { errno = 0; continue; @@ -436,7 +436,7 @@ return -1; } } - std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; count -= offset; start += offset; } Modified: branches/complement-sockios/explore/include/sockios/sp.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/sp.cc 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/sp.cc 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 22:25:48 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 09:10:47 ptr> /* * Copyright (c) 2008 @@ -39,21 +39,16 @@ } // throw system_error } - // std::cerr << "epoll see " << n << std::endl; std::tr2::lock_guard<std::tr2::mutex> lk( dll ); for ( int i = 0; i < n; ++i ) { - // std::cerr << "epoll i = " << i << std::endl; if ( ev[i].data.fd == pipefd[0] ) { - // std::cerr << "on pipe\n"; cmd_from_pipe(); } else { - // std::cerr << "#\n"; - typename fd_container_type::iterator ifd = descr.find( ev[i].data.fd ); if ( ifd == descr.end() ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ev[i].data.fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ev[i].data.fd << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ev[i].data.fd, 0 ) < 0 ) { // throw system_error } @@ -63,10 +58,8 @@ fd_info& info = ifd->second; if ( info.flags & fd_info::listener ) { - // std::cerr << "%\n"; process_listener( ev[i], ifd ); } else { - // std::cerr << "not listener\n"; process_regular( ev[i], ifd ); } } @@ -90,10 +83,8 @@ int r = read( pipefd[0], &_ctl, sizeof(ctl) ); if ( r < 0 ) { // throw system_error - // std::cerr << "Read pipe\n"; throw std::detail::stop_request(); // runtime_error( "Stop request (normal flow)" ); } else if ( r == 0 ) { - // std::cerr << "Read pipe 0\n"; throw runtime_error( "Read pipe return 0" ); } @@ -114,7 +105,7 @@ return; } } else { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ev_add.data.fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ev_add.data.fd << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_ADD, ev_add.data.fd, &ev_add ) < 0 ) { std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // throw system_error @@ -136,7 +127,7 @@ return; } } else { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ev_add.data.fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ev_add.data.fd << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_ADD, ev_add.data.fd, &ev_add ) < 0 ) { std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // throw system_error @@ -148,13 +139,13 @@ break; case listener_on_exit: listeners_final.insert( _ctl.data.ptr ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; { int lfd = check_closed_listener( reinterpret_cast<socks_processor_t*>(_ctl.data.ptr) ); if ( lfd != -1 ) { descr.erase( lfd ); } - dump_descr(); + // dump_descr(); } break; case rqstop: @@ -167,9 +158,9 @@ template<class charT, class traits, class _Alloc> void sockmgr<charT,traits,_Alloc>::process_listener( epoll_event& ev, typename sockmgr<charT,traits,_Alloc>::fd_container_type::iterator ifd ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( ev.events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR) ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { // throw system_error std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << " " << errno << std::endl; @@ -186,7 +177,7 @@ listeners_final.insert(static_cast<void *>(ifd->second.p)); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; socks_processor_t* p = ifd->second.p; @@ -198,13 +189,13 @@ descr.erase( lfd ); } - dump_descr(); + // dump_descr(); return; } if ( (ev.events & EPOLLIN) == 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << std::hex << ev.events << std::dec << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << std::hex << ev.events << std::dec << std::endl; return; // I don't know what to do this case... } @@ -216,16 +207,14 @@ for ( ; ; ) { int fd = accept( ev.data.fd, &addr, &sz ); if ( fd < 0 ) { - // std::cerr << "Accept, listener # " << ev.data.fd << ", errno " << errno << std::endl; - std::cerr << __FILE__ << ":" << __LINE__ /* << " " << std::tr2::getpid() */ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ /* << " " << std::tr2::getpid() */ << std::endl; if ( (errno == EINTR) || (errno == ECONNABORTED) /* || (errno == ERESTARTSYS) */ ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; errno = 0; continue; } if ( !(errno == EAGAIN /* || errno == EWOULDBLOCK */ ) ) { // EWOULDBLOCK == EAGAIN - // std::cerr << "Accept, listener " << ev.data.fd << ", errno " << errno << std::endl; - std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << " " << errno << std::endl; @@ -241,7 +230,7 @@ } } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; socks_processor_t* p = ifd->second.p; listeners_final.insert( static_cast<void *>(p) ); @@ -250,18 +239,19 @@ check_closed_listener( p ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - dump_descr(); + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // dump_descr(); } else { // back to listen errno = 0; epoll_event xev; xev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET | EPOLLONESHOT; xev.data.fd = ev.data.fd; if ( epoll_ctl( efd, EPOLL_CTL_MOD, ev.data.fd, &xev ) < 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << " " + << errno << std::endl; } } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; return; } if ( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) != 0 ) { @@ -275,25 +265,25 @@ ev_add.data.fd = fd; if ( descr.find( fd ) != descr.end() ) { // reuse? - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_MOD, fd, &ev_add ) < 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << " " << errno << std::endl; descr.erase( fd ); // throw system_error return; // throw } } else { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_ADD, fd, &ev_add ) < 0 ) { // throw system_error - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << " " << errno << std::endl; return; // throw } } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; sockbuf_t* b = (*info.p)( fd, addr ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; descr[fd] = fd_info( b, info.p ); } @@ -310,14 +300,13 @@ template<class charT, class traits, class _Alloc> void sockmgr<charT,traits,_Alloc>::process_regular( epoll_event& ev, typename sockmgr<charT,traits,_Alloc>::fd_container_type::iterator ifd ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - fd_info& info = ifd->second; sockbuf_t* b = info.b; if ( b == 0 ) { // marginal case: sockbuf wasn't created by processor... - std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << " " << errno << std::endl; // throw system_error } if ( info.p != 0 ) { // ... but controlled by processor @@ -332,19 +321,17 @@ } else { descr.erase( ifd ); } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - dump_descr(); return; } errno = 0; - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( ev.events & EPOLLIN ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; for ( ; ; ) { if ( b->_ebuf == b->egptr() ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // process extract data from buffer too slow for us! if ( (info.flags & fd_info::level_triggered) == 0 ) { epoll_event xev; @@ -352,20 +339,18 @@ xev.data.fd = ev.data.fd; info.flags |= fd_info::level_triggered; if ( epoll_ctl( efd, EPOLL_CTL_MOD, ev.data.fd, &xev ) < 0 ) { - // std::cerr << "X " << ev.data.fd << ", " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << " " << errno << std::endl; } } - // std::cerr << "Z " << ev.data.fd << ", " << errno << std::endl; - if ( info.p != 0 ) { // or (info.flags & fd_info::owner) != 0 + if ( info.p != 0 ) { (*info.p)( ev.data.fd ); } break; } - // std::cerr << "ptr " << (void *)b->egptr() << ", " << errno << std::endl; + long offset = read( ev.data.fd, b->egptr(), sizeof(charT) * (b->_ebuf - b->egptr()) ); - // std::cerr << "offset " << offset << ", " << errno << std::endl; + if ( offset < 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; switch ( errno ) { case EINTR: // read was interrupted errno = 0; @@ -383,17 +368,17 @@ epoll_event xev; xev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET | EPOLLONESHOT; xev.data.fd = ev.data.fd; - epoll_ctl( efd, EPOLL_CTL_MOD, ev.data.fd, &xev ); + if ( epoll_ctl( efd, EPOLL_CTL_MOD, ev.data.fd, &xev ) < 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << " " << errno << std::endl; + } } break; default: - // std::cerr << "not listener, other " << ev.data.fd << std::hex << ev.events << std::dec << " : " << errno << std::endl; std::cerr << __FILE__ << ":" << __LINE__ << std::endl; break; } break; } else if ( offset > 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; offset /= sizeof(charT); // if offset % sizeof(charT) != 0, rest will be lost! if ( info.flags & fd_info::level_triggered ) { @@ -402,19 +387,18 @@ xev.data.fd = ev.data.fd; info.flags &= ~static_cast<unsigned>(fd_info::level_triggered); if ( epoll_ctl( efd, EPOLL_CTL_MOD, ev.data.fd, &xev ) < 0 ) { - // std::cerr << "Y " << ev.data.fd << ", " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << " " << errno << std::endl; } } std::tr2::lock_guard<std::tr2::mutex> lk( b->ulck ); b->setg( b->eback(), b->gptr(), b->egptr() + offset ); b->ucnd.notify_one(); if ( info.p != 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << std::endl; (*info.p)( ev.data.fd ); } } else { - std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << std::endl; - // std::cerr << "K " << ev.data.fd << ", " << errno << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << std::endl; // EPOLLRDHUP may be missed in kernel, but offset 0 is the same ev.events |= EPOLLRDHUP; // will be processed below break; @@ -423,17 +407,16 @@ } if ( (ev.events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR) ) != 0 ) { - // std::cerr << "Poll EPOLLRDHUP " << ev.data.fd << ", " << errno << std::endl; - std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { // throw system_error - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << " " << errno << std::endl; } if ( info.p != 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; (*info.p)( ifd->first, typename socks_processor_t::adopt_close_t() ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; socks_processor_t* p = info.p; @@ -445,14 +428,15 @@ } } else { b->_notify_close = false; // avoid deadlock - std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { // throw system_error + std::cerr << __FILE__ << ":" << __LINE__ << " " << ifd->first << " " << errno << std::endl; } descr.erase( ifd ); b->close(); } - dump_descr(); + // dump_descr(); } // if ( ev.events & EPOLLHUP ) { // std::cerr << "Poll HUP" << std::endl; @@ -479,30 +463,29 @@ { int myfd = -1; - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( !listeners_final.empty() ) { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( listeners_final.find( static_cast<void*>(p) ) != listeners_final.end() ) { for ( typename fd_container_type::iterator i = descr.begin(); i != descr.end(); ++i ) { if ( i->second.p == p ) { if ( (i->second.flags & fd_info::listener) == 0 ) { // it's not me! - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; return -1; } myfd = i->first; - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; } } - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // no more connection with this listener listeners_final.erase( static_cast<void*>(p) ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // if ( myfd != -1 ) { // std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - p->stop(); + p->stop(); // } } } Modified: branches/complement-sockios/explore/include/sockios/sp.h =================================================================== --- branches/complement-sockios/explore/include/sockios/sp.h 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/include/sockios/sp.h 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 22:10:36 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 08:22:27 ptr> /* * Copyright (c) 2008 @@ -229,31 +229,28 @@ void exit_notify( sockbuf_t* b, sock_base::socket_type fd ) { - // fd_info info = { 0, 0, 0 }; - // std::tr2::lock_guard<std::tr2::mutex> lk( dll ); - try { - std::tr2::unique_lock<std::tr2::mutex> lk( dll, std::tr2::try_to_lock ); + std::tr2::unique_lock<std::tr2::mutex> lk( dll, std::tr2::defer_lock ); - if ( b->_notify_close ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; - typename fd_container_type::iterator i = descr.find( fd ); - if ( i != descr.end() ) { - if ( (i->second.b == b) && (i->second.p == 0) ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; - if ( epoll_ctl( efd, EPOLL_CTL_DEL, fd, 0 ) < 0 ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; - // throw system_error - } - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; - descr.erase( i ); + if ( lk.try_lock() ) { + if ( b->_notify_close ) { + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + typename fd_container_type::iterator i = descr.find( fd ); + if ( (i != descr.end()) && (i->second.b == b) && (i->second.p == 0) ) { + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + if ( epoll_ctl( efd, EPOLL_CTL_DEL, fd, 0 ) < 0 ) { + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // throw system_error + } + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + descr.erase( i ); } + b->_notify_close = false; } - b->_notify_close = false; } } catch ( const std::tr2::lock_error& ) { - std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; + // std::cerr << __FILE__ << ":" << __LINE__ << " " << fd << std::endl; } } Modified: branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc =================================================================== --- branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc 2008-06-26 05:46:44 UTC (rev 1918) +++ branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc 2008-06-26 05:47:07 UTC (rev 1919) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/25 17:59:36 yeti> +// -*- C++ -*- Time-stamp: <08/06/26 09:28:53 ptr> /* * @@ -263,14 +263,14 @@ { lock_guard<mutex> lk(lock); ++cnt; ++visits; cnd.notify_one(); } ~worker() - { lock_guard<mutex> lk(lock); --cnt; } + { lock_guard<mutex> lk(lock); --cnt; cnd.notify_one(); } void connect( sockstream& s ) { lock_guard<mutex> lk(lock); getline( s, line ); - cerr << __FILE__ << ":" << __LINE__ << " " << s.good() << " " - << s.rdbuf()->in_avail() << endl; + // cerr << __FILE__ << ":" << __LINE__ << " " << s.good() << " " + // << s.rdbuf()->in_avail() << endl; ++rd; line_cnd.notify_one(); } @@ -297,6 +297,9 @@ static bool rd_counter1() { return worker::rd == 1; } + + static bool counter0() + { return worker::cnt == 0; } }; mutex worker::lock; @@ -338,6 +341,13 @@ EXAM_CHECK( worker::cnd.timed_wait( lk, milliseconds( 500 ), worker::visits_counter1 ) ); worker::visits = 0; } + + // for ( int i = 0; i < 64; ++i ) { // give chance for system + // std::tr2::this_thread::yield(); + // } + + unique_lock<mutex> lksrv( worker::lock ); + EXAM_CHECK( worker::cnd.timed_wait( lksrv, milliseconds( 500 ), worker::counter0 ) ); } { lock_guard<mutex> lk( worker::lock ); @@ -370,6 +380,12 @@ EXAM_CHECK( worker::cnd.timed_wait( lk, milliseconds( 500 ), worker::visits_counter2 ) ); worker::visits = 0; } + + // for ( int i = 0; i < 64; ++i ) { // give chance for system + // std::tr2::this_thread::yield(); + // } + unique_lock<mutex> lksrv( worker::lock ); + EXAM_CHECK( worker::cnd.timed_wait( lksrv, milliseconds( 500 ), worker::counter0 ) ); } { lock_guard<mutex> lk( worker::lock ); @@ -400,6 +416,13 @@ worker::line = ""; worker::rd = 0; } + + // for ( int i = 0; i < 64; ++i ) { // give chance for system + // std::tr2::this_thread::yield(); + // } + + unique_lock<mutex> lksrv( worker::lock ); + EXAM_CHECK( worker::cnd.timed_wait( lksrv, milliseconds( 500 ), worker::counter0 ) ); } // check after sockstream was closed, i.e. ensure, that all data available @@ -420,12 +443,18 @@ } unique_lock<mutex> lk( worker::lock ); - EXAM_CHECK( worker::line_cnd.timed_wait( lk, milliseconds( 50000 ), worker::rd_counter1 ) ); + EXAM_CHECK( worker::line_cnd.timed_wait( lk, milliseconds( 500 ), worker::rd_counter1 ) ); // cerr << worker::line << endl; EXAM_CHECK( worker::line == "Hello, world!" ); worker::line = ""; worker::rd = 0; + + // for ( int i = 0; i < 64; ++i ) { // give chance for system + // std::tr2::this_thread::yield(); + // } + unique_lock<mutex> lksrv( worker::lock ); + EXAM_CHECK( worker::cnd.timed_wait( lksrv, milliseconds( 500 ), worker::counter0 ) ); } return EXAM_RESULT; @@ -471,6 +500,13 @@ unique_lock<mutex> lk( worker::lock ); EXAM_CHECK_ASYNC( worker::cnd.timed_wait( lk, milliseconds( 500 ), worker::visits_counter1 ) ); + + // for ( int i = 0; i < 64; ++i ) { // give chance for system + // std::tr2::this_thread::yield(); + // } + + unique_lock<mutex> lksrv( worker::lock ); + EXAM_CHECK( worker::cnd.timed_wait( lksrv, milliseconds( 500 ), worker::counter0 ) ); } exit( 0 ); @@ -601,6 +637,9 @@ EXAM_CHECK( r.good() ); EXAM_CHECK( r.is_open() ); + for ( int i = 0; i < 64; ++i ) { // give chance for system + std::tr2::this_thread::yield(); + } } shm.deallocate( &b ); seg.deallocate(); @@ -708,6 +747,10 @@ EXAM_CHECK( r.good() ); EXAM_CHECK( r.is_open() ); + + for ( int i = 0; i < 64; ++i ) { // give chance for system + std::tr2::this_thread::yield(); + } } shm.deallocate( &bnew ); shm.deallocate( &b ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |