Show
Ignore:
Timestamp:
10/11/08 23:06:14 (3 months ago)
Author:
frsyuki
Message:

lang/c/mpio: added mp::iothreads::close()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/c/mpio/trunk/src/iothreads_reader.cc

    r20335 r21142  
    3636public: 
    3737        void add(handler* h); 
     38        void close(int fd); 
    3839        void send_message(int fd, message_t* msg); 
    3940 
     
    4647private: 
    4748        typedef enum { 
    48                 HANDLER, 
     49                ADD_HANDLER, 
     50                CLOSE_HANDLER, 
    4951                MESSAGE, 
    5052        } notify_type_t; 
     
    5355                notify_type_t type; 
    5456                union { 
    55                         handler* h; 
    56                         struct { 
     57                        handler* h;             // ADD_HANDLER 
     58                        int fd;                 // CLOSE_HANDLER 
     59                        struct {                // MESSAGE 
    5760                                message_t* func; 
    5861                                int fd; 
     
    6568 
    6669private: 
     70        void add_handler(handler* h); 
     71        void close_handler(handler* h); 
     72 
     73private: 
    6774        typedef event<handler*> ev_t; 
    6875        ev_t m_ev; 
     
    105112} 
    106113 
     114void reader::impl::close(int fd) 
     115{ 
     116        worker_of(fd).close(fd); 
     117} 
     118 
    107119void reader::impl::send_message(int fd, message_t* msg) 
    108120{ 
     
    126138inline void reader::impl::worker::add(handler* h) 
    127139{ 
    128         notify_entry e = { HANDLER }; 
     140        notify_entry e = { ADD_HANDLER }; 
    129141        e.as.h = h; 
     142        m_notify.send(e); 
     143} 
     144 
     145inline void reader::impl::worker::close(int fd) 
     146{ 
     147        notify_entry e = { CLOSE_HANDLER }; 
     148        e.as.fd = fd; 
    130149        m_notify.send(e); 
    131150} 
     
    179198                h->read_event(); 
    180199        } catch (...) { 
    181                 int fd = h->fd(); 
    182                 m_ev.remove(fd, EV_READ); 
    183                 // FIXME closeはロジックスレッドで 
    184                 // fdに対してイベント到着 
    185                 // 直後にclose 
    186                 // 直後にaccept 
    187                 // イベントに対して返信 
    188                 // 期待とは異なるfdに返信される 
    189                 //::close(fd); 
    190                 try { 
    191                         iothreads::submit(close_delete, fd, h); 
    192                 } catch (...) { 
    193                         ::close(fd); 
    194                         delete h; 
    195                 } 
     200                close_handler(h); 
    196201        } 
    197202} 
     
    199204inline void reader::impl::worker::notify_impl(notify_entry& e) 
    200205{ 
    201         if(e.type == HANDLER) { 
     206        if(e.type == ADD_HANDLER) { 
    202207                handler* h = e.as.h; 
    203                 try { 
    204                         m_ev.add(h->fd(), EV_READ, h); 
    205                 } catch (...) { 
    206                         ::close(h->fd()); 
    207                         delete h; 
    208                 } 
    209  
    210         } else { 
     208                add_handler(h); 
     209 
     210        } else if(e.type == CLOSE_HANDLER) { 
     211                int fd = e.as.fd; 
     212                if(!m_ev.test(fd)) { return; } 
     213                close_handler(m_ev.data(fd)); 
     214 
     215        } else {  // e.type == MESSAGE 
    211216                int fd = e.as.m.fd; 
    212217                if(!m_ev.test(fd)) { return; } 
    213218                message_t* func = e.as.m.func; 
    214219                try { 
    215                         // FIXME check m_ev.test(fd); 
    216220                        (*func)(*m_ev.data(fd)); 
    217221                } catch (...) { 
     
    223227} 
    224228 
     229inline void reader::impl::worker::add_handler(handler* h) 
     230{ 
     231        try { 
     232                m_ev.add(h->fd(), EV_READ, h); 
     233        } catch (...) { 
     234                ::close(h->fd()); 
     235                delete h; 
     236        } 
     237} 
     238 
     239inline void reader::impl::worker::close_handler(handler* h) 
     240{ 
     241        m_ev.remove(h->fd(), EV_READ); 
     242        //::close(fd);  // FIXME closeはロジックスレッドで 
     243        // 1. fdに対してイベント到着 
     244        // 2. 直後にclose 
     245        // 3. 直後にaccept, closeしたfdと同じ番号になり得る 
     246        // 4. イベントに対して返信するためにsend_data 
     247        // 5. 期待とは異なるfdに返信されてしまう 
     248        try { 
     249                iothreads::submit(close_delete, h->fd(), h); 
     250        } catch (...) { 
     251                ::close(h->fd()); 
     252                delete h; 
     253        } 
     254} 
     255 
    225256 
    226257}  // namespace iothreads