// // mp::iothreads // // Copyright (C) 2008 FURUHASHI Sadayuki // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #ifndef MP_IOTHREADS_H__ #define MP_IOTHREADS_H__ #include "mp/blocking_vector.h" #include "mp/pthread.h" #include "mp/source.h" #include "mp/zone.h" #include "mp/callback_message.h" #include "mp/functional.h" #include #include namespace mp { namespace iothreads { class manager { public: static void initialize(); static void destroy(); ~manager(); public: class message { public: template static message* create(void (*func)(T*), T* obj, Argument arg); void execute(); private: void (*m_executor)(void* target); void* m_target; private: message(void (*m_executor)(void* target), void* target); message(); }; public: template static ThreadIMPL* add_thread(); MP_ARGS_BEGIN template static ThreadIMPL* add_thread(MP_ARGS_PARAMS); MP_ARGS_END static void submit(function func); static void join(); static void run(); static void end(); static bool is_end(); private: template ThreadIMPL* add_thread_impl(); MP_ARGS_BEGIN template ThreadIMPL* add_thread_impl(MP_ARGS_PARAMS); MP_ARGS_END void submit_impl(function func); void run_impl(); void join_impl(); private: blocking_vector > m_messages; zone m_zone; private: volatile sig_atomic_t m_end_flag; typedef std::vector threads_t; threads_t m_threads; private: static std::auto_ptr s_instance; static manager& instance() { return *s_instance; } private: manager(); manager(const manager&); }; inline bool is_end() { return manager::is_end(); } inline void run() { manager::run(); } inline void end() { manager::end(); } inline void join() { manager::join(); } template inline void submit(F f) { manager::submit(f); } MP_ARGS_BEGIN template inline void submit(F f, MP_ARGS_PARAMS) { manager::submit(bind(f, MP_ARGS_FUNC)); } MP_ARGS_END } // namespace iothreads } // namespace mp #include "mp/iothreads_impl.h" #include "mp/iothreads/writer.h" #include "mp/iothreads/reader.h" #include "mp/iothreads/listener.h" #include "mp/iothreads/connector.h" #include "mp/iothreads/timer.h" #endif /* mp/iothreads.h */