Base_Thread_Adapter.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Base_Thread_Adapter.h
00006  *
00007  *  $Id: Base_Thread_Adapter.h 81239 2008-04-04 22:28:48Z iliyan $
00008  *
00009  *  @author Nanbor Wang <nanbor@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_BASE_THREAD_ADAPTER_H
00014 #define ACE_BASE_THREAD_ADAPTER_H
00015 #include /**/ "ace/pre.h"
00016 
00017 #include "ace/OS_Log_Msg_Attributes.h"
00018 
00019 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00020 # pragma once
00021 #endif /* ACE_LACKS_PRAGMA_ONCE */
00022 
00023 #include /**/ "ace/ACE_export.h"
00024 #include "ace/OS_Log_Msg_Attributes.h"
00025 
00026 #ifdef ACE_USES_GPROF
00027 #include "os_include/sys/os_time.h"
00028 #endif // ACE_USES_GPROF
00029 
00030 #if (defined (ACE_HAS_VERSIONED_NAMESPACE) && ACE_HAS_VERSIONED_NAMESPACE == 1)
00031 # define ACE_THREAD_ADAPTER_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ace_thread_adapter)
00032 #else
00033 # define ACE_THREAD_ADAPTER_NAME ace_thread_adapter
00034 #endif  /* ACE_HAS_VERSIONED_NAMESPACE == 1 */
00035 
00036 // Run the thread entry point for the ACE_Thread_Adapter.  This must
00037 // be an extern "C" to make certain compilers happy...
00038 
00039 extern "C" ACE_Export ACE_THR_FUNC_RETURN ACE_THREAD_ADAPTER_NAME (void *args);
00040 
00041 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00042 
00043 /**
00044  * @class ACE_OS_Thread_Descriptor
00045  *
00046  * @brief Parent class of all ACE_Thread_Descriptor classes.
00047  * =
00048  * Container for ACE_Thread_Descriptor members that are
00049  * used in ACE_OS.
00050  */
00051 class ACE_Export ACE_OS_Thread_Descriptor
00052 {
00053 public:
00054   /// Get the thread creation flags.
00055   long flags (void) const;
00056 
00057 protected:
00058   /// For use by ACE_Thread_Descriptor.
00059   ACE_OS_Thread_Descriptor (long flags = 0);
00060 
00061   /**
00062    * Keeps track of whether this thread was created "detached" or not.
00063    * If a thread is *not* created detached then if someone calls
00064    * <ACE_Thread_Manager::wait>, we need to join with that thread (and
00065    * close down the handle).
00066    */
00067   long flags_;
00068 };
00069 
00070 
00071 
00072 class ACE_Service_Gestalt;
00073 
00074 
00075 /**
00076  * @class ACE_Base_Thread_Adapter
00077  *
00078  * @brief Base class for all the Thread_Adapters.
00079  *
00080  * Converts a C++ function into a function that can be
00081  * called from a thread creation routine
00082  * (e.g., pthread_create() or _beginthreadex()) that expects an
00083  * extern "C" entry point.  This class also makes it possible to
00084  * transparently provide hooks to register a thread with an
00085  * ACE_Thread_Manager.
00086  * This class is used in ACE_OS::thr_create().  In general, the
00087  * thread that creates an object of this class is different from
00088  * the thread that calls @c invoke() on this object.  Therefore,
00089  * the @c invoke() method is responsible for deleting itself.
00090  */
00091 class ACE_Export ACE_Base_Thread_Adapter
00092 {
00093 public:
00094 
00095   virtual ~ACE_Base_Thread_Adapter (void);
00096 
00097   /// Virtual method invoked by the thread entry point.
00098   virtual ACE_THR_FUNC_RETURN invoke (void) = 0;
00099 
00100   /// Accessor for the C entry point function to the OS thread creation
00101   /// routine.
00102   ACE_THR_C_FUNC entry_point (void);
00103 
00104 #ifdef ACE_USES_GPROF
00105   /// Accessor to the itimer_
00106   /// followed http://sam.zoy.org/writings/programming/gprof.html
00107   struct itimerval* timerval (void);
00108 #endif // ACE_USES_PROF
00109 
00110   /// Invoke the close_log_msg_hook, if it is present
00111   static void close_log_msg (void);
00112 
00113   /// Invoke the sync_log_msg_hook, if it is present
00114   static void sync_log_msg (const ACE_TCHAR *prog_name);
00115 
00116   /// Invoke the thr_desc_log_msg_hook, if it is present
00117   static ACE_OS_Thread_Descriptor *thr_desc_log_msg (void);
00118 
00119 protected:
00120   /// Constructor.
00121   ACE_Base_Thread_Adapter (ACE_THR_FUNC user_func,
00122                            void *arg,
00123                            ACE_THR_C_FUNC entry_point = (ACE_THR_C_FUNC) ACE_THREAD_ADAPTER_NAME,
00124                            ACE_OS_Thread_Descriptor *td = 0
00125 # if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
00126                            , ACE_SEH_EXCEPT_HANDLER selector = 0
00127                            , ACE_SEH_EXCEPT_HANDLER handler = 0
00128 # endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
00129                       );
00130   /// Inherit the logging features if the parent thread has an
00131   /// ACE_Log_Msg.
00132   void inherit_log_msg (void);
00133 
00134 private:
00135   /// The hooks to inherit and cleanup the Log_Msg attributes
00136   static ACE_INIT_LOG_MSG_HOOK init_log_msg_hook_;
00137   static ACE_INHERIT_LOG_MSG_HOOK inherit_log_msg_hook_;
00138   static ACE_CLOSE_LOG_MSG_HOOK close_log_msg_hook_;
00139   static ACE_SYNC_LOG_MSG_HOOK sync_log_msg_hook_;
00140   static ACE_THR_DESC_LOG_MSG_HOOK thr_desc_log_msg_hook_;
00141 
00142   /// Set the Log_Msg hooks
00143   static void set_log_msg_hooks (ACE_INIT_LOG_MSG_HOOK init_hook,
00144                                  ACE_INHERIT_LOG_MSG_HOOK inherit_hook,
00145                                  ACE_CLOSE_LOG_MSG_HOOK close_hook,
00146                                  ACE_SYNC_LOG_MSG_HOOK sync_hook,
00147                                  ACE_THR_DESC_LOG_MSG_HOOK thr_desc);
00148 
00149   /// Allow the ACE_Log_Msg class to set its hooks.
00150   friend class ACE_Log_Msg;
00151 
00152 protected:
00153   /// Thread startup function passed in by the user (C++ linkage).
00154   ACE_THR_FUNC user_func_;
00155 
00156   /// Argument to thread startup function.
00157   void *arg_;
00158 
00159   /// Entry point to the underlying OS thread creation call (C
00160   /// linkage).
00161   ACE_THR_C_FUNC entry_point_;
00162 
00163   /**
00164    * Optional thread descriptor.  Passing this pointer in will force
00165    * the spawned thread to cache this location in <Log_Msg> and wait
00166    * until <Thread_Manager> fills in all information in thread
00167    * descriptor.
00168    */
00169   ACE_OS_Thread_Descriptor *thr_desc_;
00170 
00171   /// The ACE_Log_Msg attributes.
00172   ACE_OS_Log_Msg_Attributes log_msg_attributes_;
00173 
00174   /// That is usefull for gprof, define itimerval
00175 #ifdef ACE_USES_GPROF
00176   struct itimerval itimer_;
00177 #endif // ACE_USES_GPROF
00178 
00179   /// Keep a reference to the configuration context that spawns the
00180   /// thread so the child can inherit it.
00181   ACE_Service_Gestalt * const ctx_;
00182 };
00183 
00184 ACE_END_VERSIONED_NAMESPACE_DECL
00185 
00186 # if defined (ACE_HAS_INLINED_OSCALLS)
00187 #   if defined (ACE_INLINE)
00188 #     undef ACE_INLINE
00189 #   endif /* ACE_INLINE */
00190 #   define ACE_INLINE inline
00191 #   include "ace/Base_Thread_Adapter.inl"
00192 # endif /* ACE_HAS_INLINED_OSCALLS */
00193 
00194 #include /**/ "ace/post.h"
00195 #endif /* ACE_BASE_THREAD_ADAPTER_H */

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7