SAGA Adaptor CPI v.1.0
session.cpp
Go to the documentation of this file.
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 
00016 namespace saga 
00017 {
00018   // Instantiate the default session object
00019   //
00020   // Most saga objects have a session attached.  If no session is explicitly
00021   // specified on object creation, this default session is attached instead.
00022   
00024   namespace detail 
00025   {
00027       //  Helper object allowing to break the cyclic dependency between a 
00028       //  session and the contexts held inside this session
00029       class session_helper
00030       {
00031       public:
00032           session_helper(session& s)
00033             : session_(s)
00034           {}
00035           ~session_helper()
00036           {
00037               impl::runtime::get_impl(session_)->release_contexts();
00038           }
00039 
00040       private:
00041           session& session_;
00042       };
00043 
00045       //  Helper functions to allow thread safe initialization of the static session 
00046       //  object
00047       session& get_instance()
00048       {
00049           static session s(true);
00050           static session_helper helper(s);
00051           return s;
00052       }
00053 
00054       static void call_once_session()
00055       {
00056           get_instance();
00057       }
00058 
00060       //  This is an API function
00061       session& get_the_session (void)
00062       {
00063           static boost::once_flag once_flag = BOOST_ONCE_INIT;
00064           boost::call_once(&call_once_session, once_flag);
00065 
00066           return get_instance();
00067       }
00068   }
00070 
00071   session get_default_session (void)
00072   {
00073       return detail::get_the_session();
00074   }
00075 
00076   namespace adaptors
00077   {
00078 //       struct managed_threads : boost::thread_group
00079 //       {
00080 //           ~managed_threads() 
00081 //           {
00082 //               join_all();
00083 //           }
00084 //       };
00085 //       
00086 //       static managed_threads& get_thread_group_helper()
00087 //       {
00088 //           static managed_threads threads;
00089 //           return threads;
00090 //       }
00091 // 
00092 //       static void call_once_thread_group()
00093 //       {
00094 //           get_thread_group_helper();
00095 //       }
00096 // 
00097 //       static managed_threads& get_thread_group()
00098 //       {
00099 //           static boost::once_flag once_flag = BOOST_ONCE_INIT;
00100 //           boost::call_once(&call_once_thread_group, once_flag);
00101 //           
00102 //           return get_thread_group_helper();
00103 //       }
00104 
00106       void add_managed_thread (saga::session s, boost::thread* thrd)
00107       {
00108 //           get_thread_group().add_thread(thrd);
00109       }
00110       
00111       void remove_managed_thread (saga::session s, boost::thread* thrd)
00112       {
00113 //           get_thread_group().remove_thread(thrd);
00114       }
00115   }
00116 
00118   session::session () 
00119     : saga::object (new saga::impl::session(false))
00120   {
00121       BOOST_ASSERT(get_impl());
00122   }
00123 
00124   session::session (bool default_session) 
00125     : saga::object (new saga::impl::session(default_session))
00126   {
00127       BOOST_ASSERT(get_impl());
00128   }
00129 
00130   session::session (saga::object const& obj) 
00131     : saga::object(obj)
00132   {
00133       if (this->get_type() != saga::object::Session)
00134       {
00135           SAGA_THROW("Bad type conversion.", saga::BadParameter);
00136       }
00137   }
00138 
00139   session& session::operator=(saga::object const& obj)
00140   {
00141       saga::object::operator=(obj);
00142       if (this->get_type() != saga::object::Session)
00143       {
00144           SAGA_THROW("Bad type conversion.", saga::BadParameter);
00145       }
00146       return *this;
00147   }
00148 
00149   session::~session (void)
00150   {
00151   }
00152 
00153   saga::impl::session* session::get_impl (void) const
00154   { 
00155       typedef saga::object base_type;
00156       return static_cast<saga::impl::session*>(this->base_type::get_impl ());
00157   }
00158 
00159   TR1::shared_ptr<saga::impl::session> session::get_impl_sp(void) const
00160   { 
00161       typedef saga::object base_type;
00162       return TR1::static_pointer_cast<saga::impl::session>(
00163           this->base_type::get_impl_sp());
00164   }
00165 
00166   void session::add_context (context const & c)
00167   {
00168       get_impl()->add_context (c);
00169   }
00170 
00171   void session::remove_context (context const & c)
00172   {
00173       get_impl()->remove_context (c);
00174   }
00175 
00176   std::vector <context> 
00177   session::list_contexts() const
00178   {
00179       return get_impl()->list_contexts();
00180   }
00181 
00182   void session::close(double timeout)
00183   {
00184       return get_impl()->close(timeout);
00185   }
00186 
00188 
00189   bool operator== (session const & lhs,
00190                    session const & rhs)
00191   {
00192       if ( ! lhs.is_impl_valid() )
00193       {
00194         // IncorrrectState
00195         SAGA_THROW_VERBATIM(lhs, "The lhs session is not initialized", 
00196             saga::IncorrectState);
00197       }
00198 
00199       if ( ! rhs.is_impl_valid() )
00200       {
00201         // IncorrrectState
00202         SAGA_THROW_VERBATIM(rhs, "The rhs session is not initialized", 
00203             saga::IncorrectState);
00204       }
00205 
00206       return (lhs.get_impl() == rhs.get_impl()); 
00207   }
00209 
00212   namespace detail
00213   {
00214       preference_type const& get_preferences(saga::session const& s)
00215       {
00216           return saga::impl::runtime::get_impl(s)->get_preferences();
00217       }
00218   }
00220 
00221 } // namespace saga
00223 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines