Thread.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Thread.h
00006  *
00007  *  $Id: Thread.h 78460 2007-05-23 13:33:56Z johnnyw $
00008  *
00009  *  @author Douglas Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //==========================================================================
00012 
00013 #ifndef ACE_THREAD_H
00014 #define ACE_THREAD_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include /**/ "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/OS_NS_Thread.h"
00025 #include "ace/Thread_Adapter.h"
00026 
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028 
00029 struct cancel_state
00030 {
00031   /// e.g., PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE,
00032   /// PTHREAD_CANCELED.
00033   int cancelstate;
00034 
00035   /// e.g., PTHREAD_CANCEL_DEFERRED and PTHREAD_CANCEL_ASYNCHRONOUS.
00036   int canceltype;
00037 };
00038 
00039 /**
00040  * @class ACE_Thread
00041  *
00042  * @brief Provides a wrapper for threads.
00043  *
00044  * This class provides a common interface that is mapped onto
00045  * POSIX Pthreads, Solaris threads, Win32 threads, VxWorks
00046  * threads, or pSoS threads.  Note, however, that it is
00047  * generally a better idea to use the ACE_Thread_Manager
00048  * programming API rather than the <ACE_Thread> API since the
00049  * thread manager is more powerful.
00050  */
00051 class ACE_Export ACE_Thread
00052 {
00053 public:
00054   /**
00055    * Creates a new thread having <flags> attributes and running <func>
00056    * with <args> (if <thread_adapter> is non-0 then <func> and <args>
00057    * are ignored and are obtained from <thread_adapter>).  <thr_id>
00058    * and <t_handle> are set to the thread's ID and handle (?),
00059    * respectively.  The thread runs at <priority> priority (see
00060    * below).
00061    *
00062    * The <flags> are a bitwise-OR of the following:
00063    * = BEGIN<INDENT>
00064    * THR_CANCEL_DISABLE, THR_CANCEL_ENABLE, THR_CANCEL_DEFERRED,
00065    * THR_CANCEL_ASYNCHRONOUS, THR_BOUND, THR_NEW_LWP, THR_DETACHED,
00066    * THR_SUSPENDED, THR_DAEMON, THR_JOINABLE, THR_SCHED_FIFO,
00067    * THR_SCHED_RR, THR_SCHED_DEFAULT, THR_EXPLICIT_SCHED,
00068    * THR_SCOPE_SYSTEM, THR_SCOPE_PROCESS
00069    * = END<INDENT>
00070    *
00071    * By default, or if <priority> is set to
00072    * ACE_DEFAULT_THREAD_PRIORITY, an "appropriate" priority value for
00073    * the given scheduling policy (specified in <flags}>, e.g.,
00074    * <THR_SCHED_DEFAULT>) is used.  This value is calculated
00075    * dynamically, and is the median value between the minimum and
00076    * maximum priority values for the given policy.  If an explicit
00077    * value is given, it is used.  Note that actual priority values are
00078    * EXTREMEMLY implementation-dependent, and are probably best
00079    * avoided.
00080    *
00081    * Note that <thread_adapter> is always deleted when <spawn>
00082    * is called, so it must be allocated with global operator new.
00083    */
00084   static int spawn (ACE_THR_FUNC func,
00085                     void *arg = 0,
00086                     long flags = THR_NEW_LWP | THR_JOINABLE,
00087                     ACE_thread_t *t_id = 0,
00088                     ACE_hthread_t *t_handle = 0,
00089                     long priority = ACE_DEFAULT_THREAD_PRIORITY,
00090                     void *stack = 0,
00091                     size_t stack_size = ACE_DEFAULT_THREAD_STACKSIZE,
00092                     ACE_Thread_Adapter *thread_adapter = 0);
00093 
00094   /**
00095    * Spawn N new threads, which execute @a func with argument @a arg (if
00096    * @a thread_adapter is non-0 then @a func and @a args are ignored and
00097    * are obtained from @a thread_adapter).  If @a stack != 0 it is
00098    * assumed to be an array of @a n pointers to the base of the stacks
00099    * to use for the threads being spawned.  Likewise, if @a stack_size
00100    * != 0 it is assumed to be an array of @a n values indicating how
00101    * big each of the corresponding <stack>s are.  Returns the number
00102    * of threads actually spawned (if this doesn't equal the number
00103    * requested then something has gone wrong and @c errno will
00104    * explain...).
00105    *
00106    * @see spawn()
00107    */
00108   static size_t spawn_n (size_t n,
00109                          ACE_THR_FUNC func,
00110                          void *arg = 0,
00111                          long flags = THR_NEW_LWP | THR_JOINABLE,
00112                          long priority = ACE_DEFAULT_THREAD_PRIORITY,
00113                          void *stack[] = 0,
00114                          size_t stack_size[] = 0,
00115                          ACE_Thread_Adapter *thread_adapter = 0);
00116 
00117   /**
00118    * Spawn @a n new threads, which execute <func> with argument <arg>
00119    * (if <thread_adapter> is non-0 then <func> and <args> are ignored
00120    * and are obtained from <thread_adapter>).  The thread_ids of
00121    * successfully spawned threads will be placed into the <thread_ids>
00122    * buffer (which must be the same size as @a n).  If <stack> != 0 it
00123    * is assumed to be an array of @a n pointers to the base of the
00124    * stacks to use for the threads being spawned.  If <stack_size> !=
00125    * 0 it is assumed to be an array of @a n values indicating how big
00126    * each of the corresponding <stack>s are.  If <thread_handles> != 0
00127    * it is assumed to be an array of @a n thread_handles that will be
00128    * assigned the values of the thread handles being spawned.  Returns
00129    * the number of threads actually spawned (if this doesn't equal the
00130    * number requested then something has gone wrong and @c errno will
00131    * explain...).
00132    *
00133    * @see spawn()
00134    */
00135   static size_t spawn_n (ACE_thread_t thread_ids[],
00136                          size_t n,
00137                          ACE_THR_FUNC func,
00138                          void *arg,
00139                          long flags,
00140                          long priority = ACE_DEFAULT_THREAD_PRIORITY,
00141                          void *stack[] = 0,
00142                          size_t stack_size[] = 0,
00143                          ACE_hthread_t thread_handles[] = 0,
00144                          ACE_Thread_Adapter *thread_adapter = 0);
00145 
00146   /**
00147    * Wait for one or more threads to exit and reap their exit status.
00148    * thr_join() returns successfully when the target thread terminates.
00149    *
00150    * @param thread_id is the ACE_thread_t ID of the thread to wait for.
00151    *                  If @a thread_id is 0, join() waits for any
00152    *                  undetached thread in the process to terminate
00153    *                  on platforms that support this capability
00154    *                  (for example, Solaris).
00155    * @param departed  points to a location that is set to the ID of the
00156    *                  terminated thread if join() returns successfully.
00157    *                  If @a departed is 0, it is ignored.
00158    * @param status    Points to the location that receives the joined
00159    *                  thread's exit value. If @a status is 0, it is ignored.
00160    *
00161    * @retval  0 for success
00162    * @retval  -1 (with errno set) for failure.
00163    */
00164   static int join (ACE_thread_t thread_id,
00165                    ACE_thread_t *departed,
00166                    ACE_THR_FUNC_RETURN *status);
00167 
00168   /// Wait for one thread to exit and reap its exit status.
00169   static int join (ACE_hthread_t,
00170                    ACE_THR_FUNC_RETURN * = 0);
00171 
00172   /// Continue the execution of a previously suspended thread.
00173   static int resume (ACE_hthread_t);
00174 
00175   /// Suspend the execution of a particular thread.
00176   static int suspend (ACE_hthread_t);
00177 
00178   /// Get the priority of a particular thread.
00179   static int getprio (ACE_hthread_t ht_id, int &priority);
00180 
00181   /// Get the priority and policy of a particular thread.
00182   static int getprio (ACE_hthread_t ht_id, int &priority, int &policy);
00183 
00184   /// Set the priority of a particular thread.
00185   static int setprio (ACE_hthread_t ht_id, int priority, int policy = -1);
00186 
00187   /// Send a signal to the thread.
00188   static int kill (ACE_thread_t, int signum);
00189 
00190   /// Yield the thread to another.
00191   static void yield (void);
00192 
00193   /**
00194    * Return the unique kernel handle of the thread.  Note that on
00195    * Win32 this is actually a pseudohandle, which cannot be shared
00196    * with other processes or waited on by threads.  To locate the real
00197    * handle, please use the ACE_Thread_Manager::thr_self() method.
00198    */
00199   static void self (ACE_hthread_t &t_handle);
00200 
00201   /// Return the unique ID of the thread.
00202   static ACE_thread_t self (void);
00203 
00204   /// Exit the current thread and return "status".
00205   /// Should _not_ be called by main thread.
00206   static void exit (ACE_THR_FUNC_RETURN status = 0);
00207 
00208   /// Get the LWP concurrency level of the process.
00209   static int getconcurrency (void);
00210 
00211   /// Set the LWP concurrency level of the process.
00212   static int setconcurrency (int new_level);
00213 
00214   /// Change and/or examine calling thread's signal mask.
00215   static int sigsetmask (int how,
00216                          const sigset_t *sigset,
00217                          sigset_t *osigset = 0);
00218 
00219   /**
00220    * Allocates a @a keyp that is used to identify data that is specific
00221    * to each thread in the process.  The key is global to all threads
00222    * in the process.
00223    */
00224   static int keycreate (ACE_thread_key_t *keyp,
00225 #if defined (ACE_HAS_THR_C_DEST)
00226                         ACE_THR_C_DEST destructor,
00227 #else
00228                         ACE_THR_DEST destructor,
00229 #endif /* ACE_HAS_THR_C_DEST */
00230                         void * = 0);
00231 
00232   /// Free up the key so that other threads can reuse it.
00233   static int keyfree (ACE_thread_key_t key);
00234 
00235   /// Bind value to the thread-specific data key, @a key, for the calling
00236   /// thread.
00237   static int setspecific (ACE_thread_key_t key,
00238                           void *value);
00239 
00240   /// Stores the current value bound to @a key for the calling thread
00241   /// into the location pointed to by @a valuep.
00242   static int getspecific (ACE_thread_key_t key,
00243                           void **valuep);
00244 
00245   /// Disable thread cancellation.
00246   static int disablecancel (struct cancel_state *old_state);
00247 
00248   /// Enable thread cancellation.
00249   static int enablecancel (struct cancel_state *old_state,
00250                            int flag);
00251 
00252   /// Set the cancellation state.
00253   static int setcancelstate (struct cancel_state &new_state,
00254                              struct cancel_state *old_state);
00255 
00256   /**
00257    * Cancel a thread.
00258    * @note This method is only portable on platforms, such as POSIX pthreads,
00259    * that support thread cancellation.
00260    */
00261   static int cancel (ACE_thread_t t_id);
00262 
00263   /// Test the cancel.
00264   static void testcancel (void);
00265 
00266 private:
00267   /// Ensure that we don't get instantiated.
00268   ACE_Thread (void);
00269 };
00270 
00271 ACE_END_VERSIONED_NAMESPACE_DECL
00272 
00273 #if defined (__ACE_INLINE__)
00274 #include "ace/Thread.inl"
00275 #endif /* __ACE_INLINE__ */
00276 
00277 #include /**/ "ace/post.h"
00278 
00279 #endif /* ACE_THREAD_H */

Generated on Sun Jan 27 12:05:40 2008 for ACE by doxygen 1.3.6