libzmq master
The Intelligent Transport Layer

kqueue.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __ZMQ_KQUEUE_HPP_INCLUDED__
00023 #define __ZMQ_KQUEUE_HPP_INCLUDED__
00024 
00025 //  poller.hpp decides which polling mechanism to use.
00026 #include "poller.hpp"
00027 #if defined ZMQ_USE_KQUEUE
00028 
00029 #include <vector>
00030 
00031 #include "fd.hpp"
00032 #include "thread.hpp"
00033 #include "poller_base.hpp"
00034 
00035 namespace zmq
00036 {
00037 
00038     //  Implements socket polling mechanism using the BSD-specific
00039     //  kqueue interface.
00040 
00041     class kqueue_t : public poller_base_t
00042     {
00043     public:
00044 
00045         typedef void* handle_t;
00046 
00047         kqueue_t ();
00048         ~kqueue_t ();
00049 
00050         //  "poller" concept.
00051         handle_t add_fd (fd_t fd_, struct i_poll_events *events_);
00052         void rm_fd (handle_t handle_);
00053         void set_pollin (handle_t handle_);
00054         void reset_pollin (handle_t handle_);
00055         void set_pollout (handle_t handle_);
00056         void reset_pollout (handle_t handle_);
00057         void start ();
00058         void stop ();
00059 
00060     private:
00061 
00062         //  Main worker thread routine.
00063         static void worker_routine (void *arg_);
00064 
00065         //  Main event loop.
00066         void loop ();
00067 
00068         //  File descriptor referring to the kernel event queue.
00069         fd_t kqueue_fd;
00070 
00071         //  Adds the event to the kqueue.
00072         void kevent_add (fd_t fd_, short filter_, void *udata_);
00073 
00074         //  Deletes the event from the kqueue.
00075         void kevent_delete (fd_t fd_, short filter_);
00076 
00077         struct poll_entry_t
00078         {
00079             fd_t fd;
00080             bool flag_pollin;
00081             bool flag_pollout;
00082             i_poll_events *reactor;
00083         };
00084 
00085         //  List of retired event sources.
00086         typedef std::vector <poll_entry_t*> retired_t;
00087         retired_t retired;
00088 
00089         //  If true, thread is in the process of shutting down.
00090         bool stopping;
00091 
00092         //  Handle of the physical thread doing the I/O work.
00093         thread_t worker;
00094 
00095         kqueue_t (const kqueue_t&);
00096         const kqueue_t &operator = (const kqueue_t&);
00097     };
00098 
00099     typedef kqueue_t poller_t;
00100 
00101 }
00102 
00103 #endif
00104 
00105 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines