ACE_Thread Class Reference

Provides a wrapper for threads. More...

#include <Thread.h>

List of all members.

Static Public Member Functions

int spawn (ACE_THR_FUNC func, void *arg=0, long flags=THR_NEW_LWP|THR_JOINABLE, ACE_thread_t *t_id=0, ACE_hthread_t *t_handle=0, long priority=ACE_DEFAULT_THREAD_PRIORITY, void *stack=0, size_t stack_size=0, ACE_Thread_Adapter *thread_adapter=0)
size_t spawn_n (size_t n, ACE_THR_FUNC func, void *arg=0, long flags=THR_NEW_LWP|THR_JOINABLE, long priority=ACE_DEFAULT_THREAD_PRIORITY, void *stack[]=0, size_t stack_size[]=0, ACE_Thread_Adapter *thread_adapter=0)
size_t spawn_n (ACE_thread_t thread_ids[], size_t n, ACE_THR_FUNC func, void *arg, long flags, long priority=ACE_DEFAULT_THREAD_PRIORITY, void *stack[]=0, size_t stack_size[]=0, ACE_hthread_t thread_handles[]=0, ACE_Thread_Adapter *thread_adapter=0)
int join (ACE_thread_t thread_id, ACE_thread_t *departed, ACE_THR_FUNC_RETURN *status)
int join (ACE_hthread_t, ACE_THR_FUNC_RETURN *=0)
 Wait for one thread to exit and reap its exit status.

int resume (ACE_hthread_t)
 Continue the execution of a previously suspended thread.

int suspend (ACE_hthread_t)
 Suspend the execution of a particular thread.

int getprio (ACE_hthread_t ht_id, int &priority)
 Get the priority of a particular thread.

int getprio (ACE_hthread_t ht_id, int &priority, int &policy)
 Get the priority and policy of a particular thread.

int setprio (ACE_hthread_t ht_id, int priority, int policy=-1)
 Set the priority of a particular thread.

int kill (ACE_thread_t, int signum)
 Send a signal to the thread.

void yield (void)
 Yield the thread to another.

void self (ACE_hthread_t &t_handle)
ACE_thread_t self (void)
 Return the unique ID of the thread.

void exit (ACE_THR_FUNC_RETURN status=0)
int getconcurrency (void)
 Get the LWP concurrency level of the process.

int setconcurrency (int new_level)
 Set the LWP concurrency level of the process.

int sigsetmask (int how, const sigset_t *sigset, sigset_t *osigset=0)
 Change and/or examine calling thread's signal mask.

int keycreate (ACE_thread_key_t *keyp, ACE_THR_DEST destructor, void *=0)
int keyfree (ACE_thread_key_t key)
 Free up the key so that other threads can reuse it.

int setspecific (ACE_thread_key_t key, void *value)
int getspecific (ACE_thread_key_t key, void **valuep)
int disablecancel (struct cancel_state *old_state)
 Disable thread cancellation.

int enablecancel (struct cancel_state *old_state, int flag)
 Enable thread cancellation.

int setcancelstate (struct cancel_state &new_state, struct cancel_state *old_state)
 Set the cancellation state.

int cancel (ACE_thread_t t_id)
void testcancel (void)
 Test the cancel.


Private Member Functions

 ACE_Thread (void)
 Ensure that we don't get instantiated.


Detailed Description

Provides a wrapper for threads.

This class provides a common interface that is mapped onto POSIX Pthreads, Solaris threads, Win32 threads, VxWorks threads, or pSoS threads. Note, however, that it is generally a better idea to use the programming API rather than the API since the thread manager is more powerful.

Definition at line 51 of file Thread.h.


Constructor & Destructor Documentation

ACE_Thread::ACE_Thread void   )  [private]
 

Ensure that we don't get instantiated.


Member Function Documentation

ACE_INLINE int ACE_Thread::cancel ACE_thread_t  t_id  )  [static]
 

Cancel a thread. Note that this method is only portable on platforms, such as POSIX pthreads, that support thread cancellation.

Definition at line 241 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_cancel().

Referenced by ACE_Thread_Manager::cancel_thr().

00242 {
00243   ACE_TRACE ("ACE_Thread::cancel");
00244 
00245   return ACE_OS::thr_cancel (t_id);
00246 }

ACE_INLINE int ACE_Thread::disablecancel struct cancel_state old_state  )  [static]
 

Disable thread cancellation.

Definition at line 162 of file Thread.inl.

References ACE_TRACE, ACE_OS::memset(), and ACE_OS::thr_setcancelstate().

00163 {
00164   ACE_TRACE ("ACE_Thread::disablecancel");
00165   int old_cstate = 0;
00166   int result = ACE_OS::thr_setcancelstate (THR_CANCEL_DISABLE,
00167                                            &old_cstate);
00168   if (result == 0 && old_state != 0)
00169     {
00170       ACE_OS::memset (old_state,
00171                       0,
00172                       sizeof (old_state));
00173       old_state->cancelstate = old_cstate;
00174     }
00175 
00176   return result;
00177 }

ACE_INLINE int ACE_Thread::enablecancel struct cancel_state old_state,
int  flag
[static]
 

Enable thread cancellation.

Definition at line 180 of file Thread.inl.

References ACE_TRACE, cancel_state::cancelstate, cancel_state::canceltype, ACE_OS::thr_setcancelstate(), and ACE_OS::thr_setcanceltype().

00182 {
00183   ACE_TRACE ("ACE_Thread::enablecancel");
00184   int old_cstate = 0;
00185   int old_ctype = 0;
00186   int result;
00187 
00188   result = ACE_OS::thr_setcancelstate (THR_CANCEL_ENABLE,
00189                                        &old_cstate);
00190   if (result != 0)
00191     return result;
00192 
00193   result = ACE_OS::thr_setcanceltype (flag,
00194                                       &old_ctype);
00195   if (result != 0)
00196     return result;
00197 
00198   if (old_state != 0)
00199     {
00200       old_state->cancelstate = old_cstate;
00201       old_state->canceltype = old_ctype;
00202     }
00203 
00204   return 0;
00205 }

ACE_INLINE void ACE_Thread::exit ACE_THR_FUNC_RETURN  status = 0  )  [static]
 

Exit the current thread and return "status". Should _not_ be called by main thread.

Definition at line 63 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_exit().

Referenced by ACE_Thread_Manager::exit().

00064 {
00065   ACE_TRACE ("ACE_Thread::exit");
00066   ACE_OS::thr_exit (status);
00067 }

ACE_INLINE int ACE_Thread::getconcurrency void   )  [static]
 

Get the LWP concurrency level of the process.

Definition at line 139 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_getconcurrency().

00140 {
00141   ACE_TRACE ("ACE_Thread::getconcurrency");
00142   return ACE_OS::thr_getconcurrency ();
00143 }

ACE_INLINE int ACE_Thread::getprio ACE_hthread_t  ht_id,
int &  priority,
int &  policy
[static]
 

Get the priority and policy of a particular thread.

Definition at line 271 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_getprio().

00272 {
00273   ACE_TRACE ("ACE_Thread::getprio");
00274   return ACE_OS::thr_getprio (ht_id, priority, policy);
00275 }

ACE_INLINE int ACE_Thread::getprio ACE_hthread_t  ht_id,
int &  priority
[static]
 

Get the priority of a particular thread.

Definition at line 264 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_getprio().

00265 {
00266   ACE_TRACE ("ACE_Thread::getprio");
00267   return ACE_OS::thr_getprio (ht_id, priority);
00268 }

ACE_INLINE int ACE_Thread::getspecific ACE_thread_key_t  key,
void **  valuep
[static]
 

Stores the current value bound to for the calling thread into the location pointed to by .

Definition at line 49 of file Thread.inl.

References ACE_thread_key_t, and ACE_OS::thr_getspecific().

Referenced by ACE_Log_Msg::close(), ACE_Log_Msg::exists(), and ACE_Log_Msg::instance().

00050 {
00051   // ACE_TRACE ("ACE_Thread::getspecific");
00052   return ACE_OS::thr_getspecific (key, valuep);
00053 }

ACE_INLINE int ACE_Thread::join ACE_hthread_t  ,
ACE_THR_FUNC_RETURN *  = 0
[static]
 

Wait for one thread to exit and reap its exit status.

Definition at line 131 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_join().

00133 {
00134   ACE_TRACE ("ACE_Thread::join");
00135   return ACE_OS::thr_join (wait_for, status);
00136 }

ACE_INLINE int ACE_Thread::join ACE_thread_t  thread_id,
ACE_thread_t departed,
ACE_THR_FUNC_RETURN *  status
[static]
 

Wait for one or more threads to exit and reap their exit status. thr_join() returns successfully when the target thread terminates.

Parameters:
thread_id is the ACE_thread_t ID of the thread to wait for. If thread_id is 0, join() waits for any undetached thread in the process to terminate on platforms that support this capability (for example, Solaris).
departed points to a location that is set to the ID of the terminated thread if join() returns successfully. If departed is 0, it is ignored.
status Points to the location that receives the joined thread's exit value. If status is 0, it is ignored.
Return values:
0 for success; -1 (with errno set) for failure.

Definition at line 122 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_join().

Referenced by ACE_Thread_Manager::join(), ACE_Thread_Manager::join_thr(), ACE_Thread_Manager::wait(), ACE_Thread_Manager::wait_grp(), and ACE_Thread_Manager::wait_task().

00125 {
00126   ACE_TRACE ("ACE_Thread::join");
00127   return ACE_OS::thr_join (wait_for, departed, status);
00128 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE int ACE_Thread::keycreate ACE_thread_key_t keyp,
ACE_THR_DEST  destructor,
void *  = 0
[static]
 

Allocates a that is used to identify data that is specific to each thread in the process. The key is global to all threads in the process.

Definition at line 14 of file Thread.inl.

References ACE_thread_key_t, and ACE_OS::thr_keycreate().

Referenced by ACE_Log_Msg::instance().

00021 {
00022   // ACE_TRACE ("ACE_Thread::keycreate");
00023   return ACE_OS::thr_keycreate (keyp, destructor, inst);
00024 }

ACE_INLINE int ACE_Thread::keyfree ACE_thread_key_t  key  )  [static]
 

Free up the key so that other threads can reuse it.

Definition at line 29 of file Thread.inl.

References ACE_thread_key_t, ACE_TRACE, and ACE_OS::thr_keyfree().

00030 {
00031   ACE_TRACE ("ACE_Thread::keyfree");
00032   return ACE_OS::thr_keyfree (key);
00033 }

ACE_INLINE int ACE_Thread::kill ACE_thread_t  ,
int  signum
[static]
 

Send a signal to the thread.

Definition at line 115 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_kill().

Referenced by ACE_Thread_Manager::kill_thr().

00116 {
00117   ACE_TRACE ("ACE_Thread::kill");
00118   return ACE_OS::thr_kill (t_id, signum);
00119 }

ACE_INLINE int ACE_Thread::resume ACE_hthread_t   )  [static]
 

Continue the execution of a previously suspended thread.

Definition at line 101 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_continue().

Referenced by ACE_Thread_Manager::resume_thr().

00102 {
00103   ACE_TRACE ("ACE_Thread::resume");
00104   return ACE_OS::thr_continue (t_id);
00105 }

ACE_INLINE ACE_thread_t ACE_Thread::self void   )  [static]
 

Return the unique ID of the thread.

Definition at line 56 of file Thread.inl.

References ACE_OS::thr_self().

Referenced by ACE_TPQ_Entry::ACE_TPQ_Entry(), ACE_WFMO_Reactor::calculate_timeout(), ACE_Task_Base::cleanup(), ACE_WFMO_Reactor_Handler_Repository::current_info(), ACE_WFMO_Reactor::expire_timers(), ACE_WFMO_Reactor_Handler_Repository::handles(), ACE_Log_Msg::log(), ACE_WFMO_Reactor_Handler_Repository::max_handlep1(), ACE_Select_Reactor_T< ACE_SELECT_REACTOR_TOKEN >::open(), ACE_TP_Reactor::owner(), ACE_Token::renew(), ACE_WFMO_Reactor_Handler_Repository::scheduled_for_deletion(), ACE_Token::shared_acquire(), ACE_Thread_Timer_Queue_Adapter< TQ >::svc(), ACE_Thread_Manager::thr_self(), and ACE_WFMO_Reactor::update_state().

00057 {
00058 //  ACE_TRACE ("ACE_Thread::self");
00059   return ACE_OS::thr_self ();
00060 }

ACE_INLINE void ACE_Thread::self ACE_hthread_t t_handle  )  [static]
 

Return the unique kernel handle of the thread. Note that on Win32 this is actually a pseudohandle, which cannot be shared with other processes or waited on by threads. To locate the real handle, please use the ACE_Thread_Manager::thr_self() method.

Definition at line 257 of file Thread.inl.

References ACE_hthread_t, and ACE_OS::thr_self().

00258 {
00259 //  ACE_TRACE ("ACE_Thread::self");
00260   ACE_OS::thr_self (t_id);
00261 }

ACE_INLINE int ACE_Thread::setcancelstate struct cancel_state new_state,
struct cancel_state old_state
[static]
 

Set the cancellation state.

Definition at line 208 of file Thread.inl.

References ACE_TRACE, cancel_state::cancelstate, cancel_state::canceltype, ACE_OS::thr_setcancelstate(), and ACE_OS::thr_setcanceltype().

00210 {
00211   ACE_TRACE ("ACE_Thread::setcancelstate");
00212   int old_cstate = 0;
00213   int old_ctype = 0;
00214 
00215   if (new_state.cancelstate != 0
00216       && ACE_OS::thr_setcancelstate (new_state.cancelstate,
00217                                      &old_cstate) != 0)
00218     return -1;
00219 
00220   if (new_state.canceltype != 0
00221       && ACE_OS::thr_setcanceltype (new_state.canceltype,
00222                                     &old_ctype) != 0)
00223     {
00224       int o_cstate;
00225 
00226       ACE_OS::thr_setcancelstate (old_cstate,
00227                                   &o_cstate);
00228       return -1;
00229     }
00230 
00231   if (old_state != 0)
00232     {
00233       old_state->cancelstate = old_cstate;
00234       old_state->canceltype = old_ctype;
00235     }
00236 
00237   return 0;
00238 }

ACE_INLINE int ACE_Thread::setconcurrency int  new_level  )  [static]
 

Set the LWP concurrency level of the process.

Definition at line 146 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_setconcurrency().

00147 {
00148   ACE_TRACE ("ACE_Thread::setconcurrency");
00149   return ACE_OS::thr_setconcurrency (new_level);
00150 }

ACE_INLINE int ACE_Thread::setprio ACE_hthread_t  ht_id,
int  priority,
int  policy = -1
[static]
 

Set the priority of a particular thread.

Definition at line 278 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_setprio().

00279 {
00280   ACE_TRACE ("ACE_Thread::setprio");
00281   return ACE_OS::thr_setprio (ht_id, priority, policy);
00282 }

ACE_INLINE int ACE_Thread::setspecific ACE_thread_key_t  key,
void *  value
[static]
 

Bind value to the thread-specific data key, , for the calling thread.

Definition at line 39 of file Thread.inl.

References ACE_thread_key_t, and ACE_OS::thr_setspecific().

Referenced by ACE_Log_Msg::close(), and ACE_Log_Msg::instance().

00040 {
00041   // ACE_TRACE ("ACE_Thread::setspecific");
00042   return ACE_OS::thr_setspecific (key, value);
00043 }

ACE_INLINE int ACE_Thread::sigsetmask int  how,
const sigset_t *  sigset,
sigset_t *  osigset = 0
[static]
 

Change and/or examine calling thread's signal mask.

Definition at line 153 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_sigsetmask().

00156 {
00157   ACE_TRACE ("ACE_Thread::sigsetmask");
00158   return ACE_OS::thr_sigsetmask (how, sigset, osigset);
00159 }

ACE_INLINE int ACE_Thread::spawn ACE_THR_FUNC  func,
void *  arg = 0,
long  flags = THR_NEW_LWP|THR_JOINABLE,
ACE_thread_t t_id = 0,
ACE_hthread_t t_handle = 0,
long  priority = ACE_DEFAULT_THREAD_PRIORITY,
void *  stack = 0,
size_t  stack_size = 0,
ACE_Thread_Adapter thread_adapter = 0
[static]
 

Creates a new thread having attributes and running with (if is non-0 then and are ignored and are obtained from ). and are set to the thread's ID and handle (?), respectively. The thread runs at priority (see below).

The are a bitwise-OR of the following: = BEGIN<INDENT> THR_CANCEL_DISABLE, THR_CANCEL_ENABLE, THR_CANCEL_DEFERRED, THR_CANCEL_ASYNCHRONOUS, THR_BOUND, THR_NEW_LWP, THR_DETACHED, THR_SUSPENDED, THR_DAEMON, THR_JOINABLE, THR_SCHED_FIFO, THR_SCHED_RR, THR_SCHED_DEFAULT, THR_EXPLICIT_SCHED, THR_SCOPE_SYSTEM, THR_SCOPE_PROCESS = END<INDENT>

By default, or if is set to ACE_DEFAULT_THREAD_PRIORITY, an "appropriate" priority value for the given scheduling policy (specified in <flags}>, e.g., ) is used. This value is calculated dynamically, and is the median value between the minimum and maximum priority values for the given policy. If an explicit value is given, it is used. Note that actual priority values are EXTREMEMLY implementation-dependent, and are probably best avoided.

Note that is always deleted when is called, so it must be allocated with global operator new.

Definition at line 77 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_create().

Referenced by ACE_Thread_Manager::spawn_i().

00086 {
00087   ACE_TRACE ("ACE_Thread::spawn");
00088 
00089   return ACE_OS::thr_create (func,
00090                              arg,
00091                              flags,
00092                              t_id,
00093                              t_handle,
00094                              priority,
00095                              thr_stack,
00096                              thr_stack_size,
00097                              thread_adapter);
00098 }

size_t ACE_Thread::spawn_n ACE_thread_t  thread_ids[],
size_t  n,
ACE_THR_FUNC  func,
void *  arg,
long  flags,
long  priority = ACE_DEFAULT_THREAD_PRIORITY,
void *  stack[] = 0,
size_t  stack_size[] = 0,
ACE_hthread_t  thread_handles[] = 0,
ACE_Thread_Adapter thread_adapter = 0
[static]
 

Spawn new threads, which execute with argument (if is non-0 then and are ignored and are obtained from ). The thread_ids of successfully spawned threads will be placed into the buffer (which must be the same size as ). If != 0 it is assumed to be an array of pointers to the base of the stacks to use for the threads being spawned. If != 0 it is assumed to be an array of values indicating how big each of the corresponding s are. If != 0 it is assumed to be an array of thread_handles that will be assigned the values of the thread handles being spawned. Returns the number of threads actually spawned (if this doesn't equal the number requested then something has gone wrong and will explain...).

See also:
spawn()

Definition at line 48 of file Thread.cpp.

References ACE_hthread_t, ACE_thread_t, ACE_TRACE, and ACE_OS::thr_create().

00058 {
00059   ACE_TRACE ("ACE_Thread::spawn_n");
00060   size_t i;
00061 
00062   for (i = 0; i < n; i++)
00063     {
00064       ACE_thread_t t_id;
00065       ACE_hthread_t t_handle;
00066 
00067       int result =
00068         ACE_OS::thr_create (func,
00069                             arg,
00070                             flags,
00071                             &t_id,
00072                             &t_handle,
00073                             priority,
00074                             stack == 0 ? 0 : stack[i],
00075                             stack_size == 0 ? 0 : stack_size[i],
00076                             thread_adapter);
00077 
00078       if (result == 0)
00079         {
00080           if (thread_ids != 0)
00081             thread_ids[i] = t_id;
00082           if (thread_handles != 0)
00083             thread_handles[i] = t_handle;
00084         }
00085       else
00086         // Bail out if error occurs.
00087         break;
00088     }
00089 
00090   return i;
00091 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL size_t ACE_Thread::spawn_n size_t  n,
ACE_THR_FUNC  func,
void *  arg = 0,
long  flags = THR_NEW_LWP|THR_JOINABLE,
long  priority = ACE_DEFAULT_THREAD_PRIORITY,
void *  stack[] = 0,
size_t  stack_size[] = 0,
ACE_Thread_Adapter thread_adapter = 0
[static]
 

Spawn N new threads, which execute with argument (if is non-0 then and are ignored and are obtained from ). If != 0 it is assumed to be an array of pointers to the base of the stacks to use for the threads being spawned. Likewise, if != 0 it is assumed to be an array of values indicating how big each of the corresponding s are. Returns the number of threads actually spawned (if this doesn't equal the number requested then something has gone wrong and will explain...).

See also:
spawn()

Definition at line 18 of file Thread.cpp.

References ACE_thread_t, ACE_TRACE, and ACE_OS::thr_create().

00026 {
00027   ACE_TRACE ("ACE_Thread::spawn_n");
00028   ACE_thread_t t_id;
00029   size_t i;
00030 
00031   for (i = 0; i < n; i++)
00032     // Bail out if error occurs.
00033     if (ACE_OS::thr_create (func,
00034                             arg,
00035                             flags,
00036                             &t_id,
00037                             0,
00038                             priority,
00039                             stack == 0 ? 0 : stack[i],
00040                             stack_size == 0 ? 0 : stack_size[i],
00041                             thread_adapter) != 0)
00042       break;
00043 
00044   return i;
00045 }

ACE_INLINE int ACE_Thread::suspend ACE_hthread_t   )  [static]
 

Suspend the execution of a particular thread.

Definition at line 108 of file Thread.inl.

References ACE_hthread_t, ACE_TRACE, and ACE_OS::thr_suspend().

Referenced by ACE_Thread_Manager::suspend_thr().

00109 {
00110   ACE_TRACE ("ACE_Thread::suspend");
00111   return ACE_OS::thr_suspend (t_id);
00112 }

ACE_INLINE void ACE_Thread::testcancel void   )  [static]
 

Test the cancel.

Definition at line 249 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_testcancel().

00250 {
00251   ACE_TRACE ("ACE_Thread::testcancel");
00252 
00253   ACE_OS::thr_testcancel ();
00254 }

ACE_INLINE void ACE_Thread::yield void   )  [static]
 

Yield the thread to another.

Definition at line 70 of file Thread.inl.

References ACE_TRACE, and ACE_OS::thr_yield().

00071 {
00072   ACE_TRACE ("ACE_Thread::yield");
00073   ACE_OS::thr_yield ();
00074 }


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:30:46 2006 for ACE by doxygen 1.3.6