OS_Thread_Adapter.cpp

Go to the documentation of this file.
00001 // OS_Thread_Adapter.cpp,v 4.8 2006/05/16 13:14:25 jwillemsen Exp
00002 
00003 #include "ace/OS_Thread_Adapter.h"
00004 
00005 ACE_RCSID (ace,
00006            OS_Thread_Adapter,
00007            "OS_Thread_Adapter.cpp,v 4.8 2006/05/16 13:14:25 jwillemsen Exp")
00008 
00009 #include "ace/Thread_Hook.h"
00010 #include "ace/Object_Manager_Base.h"
00011 #include "ace/Global_Macros.h"
00012 #include "ace/OS_NS_Thread.h"
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 ACE_OS_Thread_Adapter::ACE_OS_Thread_Adapter (
00017      ACE_THR_FUNC user_func
00018      , void *arg
00019      , ACE_THR_C_FUNC entry_point
00020 # if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
00021      , ACE_SEH_EXCEPT_HANDLER selector
00022      , ACE_SEH_EXCEPT_HANDLER handler
00023 # endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
00024      )
00025   : ACE_Base_Thread_Adapter (user_func, arg, entry_point
00026 # if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
00027                              , 0
00028                              , selector
00029                              , handler
00030 # endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
00031                              )
00032 {
00033 }
00034 
00035 ACE_OS_Thread_Adapter::~ACE_OS_Thread_Adapter (void)
00036 {
00037 }
00038 
00039 ACE_THR_FUNC_RETURN
00040 ACE_OS_Thread_Adapter::invoke (void)
00041 {
00042   // Inherit the logging features if the parent thread has an
00043   // ACE_Log_Msg instance in thread-specific storage.
00044   this->inherit_log_msg ();
00045 
00046   // Extract the arguments.
00047   ACE_THR_FUNC_INTERNAL func =
00048     reinterpret_cast<ACE_THR_FUNC_INTERNAL> (this->user_func_);
00049   void *arg = this->arg_;
00050 
00051   // Delete ourselves since we don't need <this> anymore.  Make sure
00052   // not to access <this> anywhere below this point.
00053   delete this;
00054 
00055 #if defined (ACE_NEEDS_LWP_PRIO_SET)
00056   // On SunOS, the LWP priority needs to be set in order to get
00057   // preemption when running in the RT class.  This is the ACE way to
00058   // do that . . .
00059   ACE_hthread_t thr_handle;
00060   ACE_OS::thr_self (thr_handle);
00061   int prio;
00062 
00063   // thr_getprio () on the current thread should never fail.
00064   ACE_OS::thr_getprio (thr_handle, prio);
00065 
00066   // ACE_OS::thr_setprio () has the special logic to set the LWP priority,
00067   // if running in the RT class.
00068   ACE_OS::thr_setprio (prio);
00069 
00070 #endif /* ACE_NEEDS_LWP_PRIO_SET */
00071 
00072   ACE_THR_FUNC_RETURN status = 0;
00073 
00074   ACE_SEH_TRY
00075     {
00076       ACE_SEH_TRY
00077         {
00078           ACE_Thread_Hook *hook =
00079             ACE_OS_Object_Manager::thread_hook ();
00080 
00081           if (hook)
00082             // Invoke the start hook to give the user a chance to
00083             // perform some initialization processing before the
00084             // <func> is invoked.
00085             status = hook->start (reinterpret_cast<ACE_THR_FUNC> (func),
00086                                   arg);
00087           else
00088             {
00089               // Call thread entry point.
00090               status = (*func) (arg);
00091             }
00092         }
00093 
00094 #if defined (ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS)
00095       ACE_SEH_EXCEPT (ACE_OS_Object_Manager::seh_except_selector ()(
00096                           (void *) GetExceptionInformation ()))
00097         {
00098           ACE_OS_Object_Manager::seh_except_handler ()(0);
00099         }
00100 #endif /* ACE_HAS_WIN32_STRUCTURAL_EXCEPTIONS */
00101     }
00102 
00103   ACE_SEH_FINALLY
00104     {
00105       // If we changed this to 1, change the respective if in
00106       // Task::svc_run to 0.
00107 #if 0
00108       // Call the <Task->close> hook.
00109       if (func == reinterpret_cast<ACE_THR_FUNC_INTERNAL> (ACE_Task_Base::svc_run))
00110         {
00111           ACE_Task_Base *task_ptr = (ACE_Task_Base *) arg;
00112           ACE_Thread_Manager *thr_mgr_ptr = task_ptr->thr_mgr ();
00113 
00114           // This calls the Task->close () hook.
00115           task_ptr->cleanup (task_ptr, 0);
00116 
00117           // This prevents a second invocation of the cleanup code
00118           // (called later by <ACE_Thread_Manager::exit>.
00119           thr_mgr_ptr->at_exit (task_ptr, 0, 0);
00120         }
00121 #endif /* 0 */
00122 
00123 #if defined (ACE_WIN32) || defined (ACE_HAS_TSS_EMULATION)
00124       // Call TSS destructors.
00125       ACE_OS::cleanup_tss (0 /* not main thread */);
00126 
00127 # if defined (ACE_WIN32)
00128       // Exit the thread.  Allow CWinThread-destructor to be invoked
00129       // from AfxEndThread.  _endthreadex will be called from
00130       // AfxEndThread so don't exit the thread now if we are running
00131       // an MFC thread.
00132 #   if defined (ACE_HAS_MFC) && (ACE_HAS_MFC != 0)
00133       // Not spawned by ACE_Thread_Manager, use the old buggy
00134       // version.  You should seriously consider using
00135       // ACE_Thread_Manager to spawn threads.  The following code
00136       // is know to cause some problem.
00137       CWinThread *pThread = ::AfxGetThread ();
00138 
00139       if (!pThread || pThread->m_nThreadID != ACE_OS::thr_self ())
00140         ACE_ENDTHREADEX (status);
00141       else
00142         ::AfxEndThread (status);
00143 #   else
00144       ACE_ENDTHREADEX (status);
00145 #   endif /* ACE_HAS_MFC && ACE_HAS_MFS != 0*/
00146 # endif /* ACE_WIN32 */
00147 #endif /* ACE_WIN32 || ACE_HAS_TSS_EMULATION */
00148     }
00149 
00150   return status;
00151 }
00152 
00153 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:58 2006 for ACE by doxygen 1.3.6