gloox  1.1-svn
thread.cpp
1 /*
2  Copyright (c) 2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "thread.h"
15 
16 #include "config.h"
17 
18 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
19 # include <windows.h>
20 #endif
21 
22 #ifdef _WIN32_WCE
23 # include <winbase.h>
24 #endif
25 
26 #ifdef HAVE_PTHREAD
27 # include <pthread.h>
28 #endif
29 
30 namespace gloox
31 {
32 
33  namespace util
34  {
35 
36  class Thread::ThreadImpl
37  {
38  public:
39  ThreadImpl() {}
40  ~ThreadImpl() {}
41 
42  void start( Thread* thread );
43  void join();
44 
45  private:
46 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
47  HANDLE m_self;
48 #elif defined( HAVE_PTHREAD )
49  pthread_t m_self;
50 #endif
51 
52 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
53  static DWORD WINAPI redirect( void* context );
54 #elif defined( HAVE_PTHREAD )
55  static void* redirect( void* context );
56 #endif
57 
58  };
59 
60 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
61  DWORD WINAPI Thread::ThreadImpl::redirect( void* context )
62  {
63  Thread* self = static_cast<Thread*>( context );
64  self->run();
65  return 0;
66  }
67 #elif defined( HAVE_PTHREAD )
68  void* Thread::ThreadImpl::redirect( void* context )
69  {
70  Thread* self = static_cast<Thread*>( context );
71  self->run();
72  return 0;
73  }
74 #endif
75 
76  void Thread::ThreadImpl::start( Thread* thread )
77  {
78 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
79  m_self = CreateThread( 0, 0, redirect, thread, 0, 0 );
80 #elif defined( HAVE_PTHREAD )
81  pthread_create( &m_self, 0, redirect, thread );
82 #endif
83  }
84 
86  {
87 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
88  WaitForSingleObject( m_self, INFINITE );
89 #elif defined( HAVE_PTHREAD )
90  pthread_join( m_self, 0 );
91 #endif
92  }
93 // ---- ThreadImpl ----
94 
95 // ---- Thread ----
97  : m_impl( new ThreadImpl() )
98  {
99  }
100 
102  {
103  delete m_impl;
104  }
105 
107  {
108  if( m_impl )
109  m_impl->start( this );
110  }
111 
113  {
114  if( m_impl )
115  m_impl->join();
116  }
117 
118  }
119 
120 }