3 //=============================================================================
5 * @file Base_Thread_Adapter.h
7 * @author Nanbor Wang <nanbor@cs.wustl.edu>
9 //=============================================================================
11 #ifndef ACE_BASE_THREAD_ADAPTER_H
12 #define ACE_BASE_THREAD_ADAPTER_H
13 #include /**/ "ace/pre.h"
15 #include "ace/OS_Log_Msg_Attributes.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include /**/ "ace/ACE_export.h"
22 #include "ace/OS_Log_Msg_Attributes.h"
25 #include "os_include/sys/os_time.h"
26 #endif // ACE_USES_GPROF
28 #if (defined (ACE_HAS_VERSIONED_NAMESPACE) && ACE_HAS_VERSIONED_NAMESPACE == 1)
29 # define ACE_THREAD_ADAPTER_NAME ACE_PREPROC_CONCATENATE(ACE_VERSIONED_NAMESPACE_NAME, _ace_thread_adapter)
31 # define ACE_THREAD_ADAPTER_NAME ace_thread_adapter
32 #endif /* ACE_HAS_VERSIONED_NAMESPACE == 1 */
34 /// Run the thread entry point for the ACE_Thread_Adapter. This must
35 /// be an extern "C" to make certain compilers happy...
36 extern "C" ACE_Export ACE_THR_FUNC_RETURN
ACE_THREAD_ADAPTER_NAME (void *args
);
38 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
41 * @class ACE_OS_Thread_Descriptor
43 * @brief Parent class of all ACE_Thread_Descriptor classes.
45 * Container for ACE_Thread_Descriptor members that are
48 class ACE_Export ACE_OS_Thread_Descriptor
51 /// Get the thread creation flags.
54 virtual ~ACE_OS_Thread_Descriptor () = default;
57 /// For use by ACE_Thread_Descriptor.
58 ACE_OS_Thread_Descriptor (long flags
= 0);
61 * Keeps track of whether this thread was created "detached" or not.
62 * If a thread is *not* created detached then if someone calls
63 * ACE_Thread_Manager::wait(), we need to join with that thread (and
64 * close down the handle).
69 class ACE_Service_Gestalt
;
72 * @class ACE_Base_Thread_Adapter
74 * @brief Base class for all the Thread_Adapters.
76 * Converts a C++ function into a function that can be
77 * called from a thread creation routine
78 * (e.g., pthread_create() or _beginthreadex()) that expects an
79 * extern "C" entry point. This class also makes it possible to
80 * transparently provide hooks to register a thread with an
82 * This class is used in ACE_OS::thr_create(). In general, the
83 * thread that creates an object of this class is different from
84 * the thread that calls @c invoke() on this object. Therefore,
85 * the @c invoke() method is responsible for deleting itself.
87 class ACE_Export ACE_Base_Thread_Adapter
90 virtual ~ACE_Base_Thread_Adapter () = default;
92 /// Virtual method invoked by the thread entry point.
93 virtual ACE_THR_FUNC_RETURN
invoke () = 0;
95 /// Accessor for the C entry point function to the OS thread creation
97 ACE_THR_C_FUNC
entry_point ();
100 /// Accessor to the itimer_
101 /// followed http://sam.zoy.org/writings/programming/gprof.html
102 struct itimerval
* timerval ();
103 #endif // ACE_USES_PROF
105 /// Invoke the close_log_msg_hook, if it is present
106 static void close_log_msg ();
108 /// Invoke the sync_log_msg_hook, if it is present
109 static void sync_log_msg (const ACE_TCHAR
*prog_name
);
111 /// Invoke the thr_desc_log_msg_hook, if it is present
112 static ACE_OS_Thread_Descriptor
*thr_desc_log_msg ();
116 ACE_Base_Thread_Adapter (ACE_THR_FUNC user_func
,
118 ACE_THR_C_FUNC entry_point
= (ACE_THR_C_FUNC
) ACE_THREAD_ADAPTER_NAME
,
119 ACE_OS_Thread_Descriptor
*td
= 0
120 # if defined (ACE_HAS_WIN32_STRUCTURED_EXCEPTIONS)
121 , ACE_SEH_EXCEPT_HANDLER selector
= 0
122 , ACE_SEH_EXCEPT_HANDLER handler
= 0
123 # endif /* ACE_HAS_WIN32_STRUCTURED_EXCEPTIONS */
124 , long cancel_flags
= 0
126 /// Inherit the logging features if the parent thread has an
128 void inherit_log_msg ();
131 /// The hooks to inherit and cleanup the Log_Msg attributes
132 static ACE_INIT_LOG_MSG_HOOK init_log_msg_hook_
;
133 static ACE_INHERIT_LOG_MSG_HOOK inherit_log_msg_hook_
;
134 static ACE_CLOSE_LOG_MSG_HOOK close_log_msg_hook_
;
135 static ACE_SYNC_LOG_MSG_HOOK sync_log_msg_hook_
;
136 static ACE_THR_DESC_LOG_MSG_HOOK thr_desc_log_msg_hook_
;
138 /// Set the Log_Msg hooks
139 static void set_log_msg_hooks (ACE_INIT_LOG_MSG_HOOK init_hook
,
140 ACE_INHERIT_LOG_MSG_HOOK inherit_hook
,
141 ACE_CLOSE_LOG_MSG_HOOK close_hook
,
142 ACE_SYNC_LOG_MSG_HOOK sync_hook
,
143 ACE_THR_DESC_LOG_MSG_HOOK thr_desc
);
145 /// Allow the ACE_Log_Msg class to set its hooks.
146 friend class ACE_Log_Msg
;
149 /// Thread startup function passed in by the user (C++ linkage).
150 ACE_THR_FUNC user_func_
;
152 /// Argument to thread startup function.
155 /// Entry point to the underlying OS thread creation call (C
157 ACE_THR_C_FUNC entry_point_
;
160 * Optional thread descriptor. Passing this pointer in will force
161 * the spawned thread to cache this location in Log_Msg and wait
162 * until Thread_Manager fills in all information in thread
165 ACE_OS_Thread_Descriptor
*thr_desc_
;
167 /// The ACE_Log_Msg attributes.
168 ACE_OS_Log_Msg_Attributes log_msg_attributes_
;
170 /// That is useful for gprof, define itimerval
171 #ifdef ACE_USES_GPROF
172 struct itimerval itimer_
;
173 #endif // ACE_USES_GPROF
175 /// Keep a reference to the configuration context that spawns the
176 /// thread so the child can inherit it.
177 ACE_Service_Gestalt
* const ctx_
;
179 /// Pass through the thread-creation flags that can only be acted on by
180 /// the spawned thread. Currently this is only the cancellation-related
185 ACE_END_VERSIONED_NAMESPACE_DECL
187 # if defined (ACE_HAS_INLINED_OSCALLS)
188 # if defined (ACE_INLINE)
190 # endif /* ACE_INLINE */
191 # define ACE_INLINE inline
192 # include "ace/Base_Thread_Adapter.inl"
193 # endif /* ACE_HAS_INLINED_OSCALLS */
195 #include /**/ "ace/post.h"
196 #endif /* ACE_BASE_THREAD_ADAPTER_H */