OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreThreads.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __Threads_H__
30 #define __Threads_H__
31 
32 #include "OgreSharedPtr.h"
33 
34 #if defined(__i386) || defined(_M_IX86)
35  // Calling conventions are needed for x86 (32-bit ONLY) CPUs
36  #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
37  #define OGRE_THREAD_CALL_CONVENTION _OGRE_SIMD_ALIGN_ATTRIBUTE __stdcall
38  #elif OGRE_COMPILER == OGRE_COMPILER_GNUC || OGRE_COMPILER == OGRE_COMPILER_CLANG
39  #define __cdecl __attribute__((__cdecl__))
40  #define OGRE_THREAD_CALL_CONVENTION __cdecl
41  #endif
42 #else
43  #define OGRE_THREAD_CALL_CONVENTION
44 #endif
45 
46 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
47  #define THREAD_DECLARE( threadFunction ) \
49  unsigned long OGRE_THREAD_CALL_CONVENTION threadFunction##_internal( void *argName )\
50  {\
51  unsigned long retVal = 0;\
52  ThreadHandle *threadHandle( reinterpret_cast<ThreadHandle*>( argName ) );\
53  try {\
54  retVal = threadFunction( threadHandle );\
55  }\
56  catch( ... )\
57  {\
58  }\
59  delete threadHandle;\
60  return retVal;\
61  }
62 #else
63  #define THREAD_DECLARE( threadFunction ) \
65  void* OGRE_THREAD_CALL_CONVENTION threadFunction##_internal( void *argName )\
66  {\
67  unsigned long retVal = 0;\
68  ThreadHandle *threadHandle( reinterpret_cast<ThreadHandle*>( argName ) );\
69  try {\
70  retVal = threadFunction( threadHandle );\
71  }\
72  catch( ... )\
73  {\
74  }\
75  delete threadHandle;\
76  \
77  return 0;\
78  }
79 #endif
80 
81 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
82  //No need to include the heavy windows.h header for something like this!
83  typedef void* HANDLE;
84 #else
85  #include <pthread.h>
86 #endif
87 
88 namespace Ogre
89 {
91  {
92 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
94 #else
95  pthread_t mThread;
96 #endif
97  size_t mThreadIdx;
98  void *mUserParam;
99 
100  public:
101  ThreadHandle( size_t threadIdx, void *userParam );
102  ~ThreadHandle();
103 
104  size_t getThreadIdx() const { return mThreadIdx; }
105  void* getUserParam() const { return mUserParam; }
106 
107 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
108  void _setOsHandle( HANDLE handle ) { mThread = handle; }
111  HANDLE _getOsHandle() const { return mThread; }
112 #else
113  void _setOsHandle( pthread_t &handle ) { mThread = handle; }
116  pthread_t _getOsHandle() const { return mThread; }
117 #endif
118  };
119 
122 
123 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
124  typedef unsigned long (OGRE_THREAD_CALL_CONVENTION *THREAD_ENTRY_POINT)( void *lpThreadParameter );
125 #else
126  typedef void* (OGRE_THREAD_CALL_CONVENTION *THREAD_ENTRY_POINT)( void *lpThreadParameter );
127 #endif
128 
130  {
131  public:
132  #define THREAD_GET( threadFunction ) threadFunction##_internal
133 
167  static ThreadHandlePtr CreateThread( THREAD_ENTRY_POINT entryPoint,
168  size_t threadIdx, void *param );
169 
178  static void WaitForThreads( size_t numThreadHandles, const ThreadHandlePtr *threadHandles );
179  static void WaitForThreads( const ThreadHandleVec &threadHandles );
180  };
181 }
182 
183 #endif
vector< ThreadHandlePtr >::type ThreadHandleVec
Definition: OgreThreads.h:121
void * HANDLE
Definition: OgreBarrier.h:37
#define _OgreExport
Definition: OgrePlatform.h:255
size_t getThreadIdx() const
Definition: OgreThreads.h:104
SharedPtr< ThreadHandle > ThreadHandlePtr
Definition: OgreThreads.h:120
void * HANDLE
Definition: OgreThreads.h:83
void * getUserParam() const
Definition: OgreThreads.h:105
unsigned long(OGRE_THREAD_CALL_CONVENTION * THREAD_ENTRY_POINT)(void *lpThreadParameter)
Definition: OgreThreads.h:124
HANDLE _getOsHandle() const
Internal use.
Definition: OgreThreads.h:111
#define OGRE_THREAD_CALL_CONVENTION
Definition: OgreThreads.h:43
Reference-counted shared pointer, used for objects where implicit destruction is required.