SAGA C++ API 1.6
|
00001 // Copyright (c) 2005-2007 Andre Merzky (andre@merzky.net) 00002 // Copyright (c) 2005-2009 Hartmut Kaiser 00003 // 00004 // Use, modification and distribution is subject to the Boost Software 00005 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 00006 // http://www.boost.org/LICENSE_1_0.txt) 00007 00008 #include <boost/thread.hpp> 00009 00010 // include the session implementation we want to use 00011 #include <saga/saga/session.hpp> 00012 #include <saga/impl/session.hpp> 00013 #include <saga/saga/adaptors/manage_threads.hpp> 00014 00015 #include <boost/plugin/dll.hpp> 00016 00018 namespace saga 00019 { 00020 // Instantiate the default session object 00021 // 00022 // Most saga objects have a session attached. If no session is explicitly 00023 // specified on object creation, this default session is attached instead. 00024 00026 namespace detail 00027 { 00029 // Helper object allowing to break the cyclic dependency between a 00030 // session and the contexts held inside this session 00031 class session_helper 00032 { 00033 public: 00034 session_helper(session& s) 00035 : session_(s) 00036 {} 00037 ~session_helper() 00038 { 00039 impl::runtime::get_impl(session_)->release_contexts(); 00040 } 00041 00042 private: 00043 session& session_; 00044 }; 00045 00047 // Helper functions to allow thread safe initialization of the static session 00048 // object 00049 session& get_instance() 00050 { 00051 static session s(true); 00052 static session_helper helper(s); 00053 return s; 00054 } 00055 00056 static void call_once_session() 00057 { 00058 get_instance(); 00059 } 00060 00062 // This is an API function 00063 session& get_the_session (void) 00064 { 00065 static boost::once_flag once_flag = BOOST_ONCE_INIT; 00066 boost::call_once(&call_once_session, once_flag); 00067 00068 return get_instance(); 00069 } 00070 } 00072 00073 session get_default_session (void) 00074 { 00075 return detail::get_the_session(); 00076 } 00077 00078 namespace adaptors 00079 { 00080 // struct managed_threads : boost::thread_group 00081 // { 00082 // ~managed_threads() 00083 // { 00084 // join_all(); 00085 // } 00086 // }; 00087 // 00088 // static managed_threads& get_thread_group_helper() 00089 // { 00090 // static managed_threads threads; 00091 // return threads; 00092 // } 00093 // 00094 // static void call_once_thread_group() 00095 // { 00096 // get_thread_group_helper(); 00097 // } 00098 // 00099 // static managed_threads& get_thread_group() 00100 // { 00101 // static boost::once_flag once_flag = BOOST_ONCE_INIT; 00102 // boost::call_once(&call_once_thread_group, once_flag); 00103 // 00104 // return get_thread_group_helper(); 00105 // } 00106 00108 void add_managed_thread (saga::session s, boost::thread* thrd) 00109 { 00110 // get_thread_group().add_thread(thrd); 00111 } 00112 00113 void remove_managed_thread (saga::session s, boost::thread* thrd) 00114 { 00115 // get_thread_group().remove_thread(thrd); 00116 } 00117 } 00118 00120 session::session () 00121 : saga::object (new saga::impl::session(false)) 00122 { 00123 BOOST_ASSERT(get_impl()); 00124 } 00125 00126 session::session (bool default_session) 00127 : saga::object (new saga::impl::session(default_session)) 00128 { 00129 BOOST_ASSERT(get_impl()); 00130 } 00131 00132 session::session (saga::object const& obj) 00133 : saga::object(obj) 00134 { 00135 if (this->get_type() != saga::object::Session) 00136 { 00137 SAGA_THROW("Bad type conversion.", saga::BadParameter); 00138 } 00139 } 00140 00141 session& session::operator=(saga::object const& obj) 00142 { 00143 saga::object::operator=(obj); 00144 if (this->get_type() != saga::object::Session) 00145 { 00146 SAGA_THROW("Bad type conversion.", saga::BadParameter); 00147 } 00148 return *this; 00149 } 00150 00151 session::~session (void) 00152 { 00153 } 00154 00155 saga::impl::session* session::get_impl (void) const 00156 { 00157 typedef saga::object base_type; 00158 return static_cast<saga::impl::session*>(this->base_type::get_impl ()); 00159 } 00160 00161 TR1::shared_ptr<saga::impl::session> session::get_impl_sp(void) const 00162 { 00163 typedef saga::object base_type; 00164 return TR1::static_pointer_cast<saga::impl::session>( 00165 this->base_type::get_impl_sp()); 00166 } 00167 00168 void session::add_context (context const & c) 00169 { 00170 get_impl()->add_context (c); 00171 } 00172 00173 void session::remove_context (context const & c) 00174 { 00175 get_impl()->remove_context (c); 00176 } 00177 00178 std::vector <context> 00179 session::list_contexts() const 00180 { 00181 return get_impl()->list_contexts(); 00182 } 00183 00184 void session::close(double timeout) 00185 { 00186 return get_impl()->close(timeout); 00187 } 00188 00190 00191 bool operator== (session const & lhs, 00192 session const & rhs) 00193 { 00194 if ( ! lhs.is_impl_valid() ) 00195 { 00196 // IncorrrectState 00197 SAGA_THROW_VERBATIM(lhs, "The lhs session is not initialized", 00198 saga::IncorrectState); 00199 } 00200 00201 if ( ! rhs.is_impl_valid() ) 00202 { 00203 // IncorrrectState 00204 SAGA_THROW_VERBATIM(rhs, "The rhs session is not initialized", 00205 saga::IncorrectState); 00206 } 00207 00208 return (lhs.get_impl() == rhs.get_impl()); 00209 } 00211 00214 namespace detail 00215 { 00216 preference_type const& get_preferences(saga::session const& s) 00217 { 00218 return saga::impl::runtime::get_impl(s)->get_preferences(); 00219 } 00220 } 00222 00223 } // namespace saga 00225