PosixThreadSupport.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #ifndef BT_POSIX_THREAD_SUPPORT_H
00017 #define BT_POSIX_THREAD_SUPPORT_H
00018 
00019 
00020 #include "LinearMath/btScalar.h"
00021 #include "PlatformDefinitions.h"
00022 
00023 #ifdef USE_PTHREADS //platform specifc defines are defined in PlatformDefinitions.h
00024 
00025 #ifndef _XOPEN_SOURCE
00026 #define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
00027 #endif //_XOPEN_SOURCE
00028 #include <pthread.h>
00029 #include <semaphore.h>
00030 
00031 
00032 
00033 #include "LinearMath/btAlignedObjectArray.h"
00034 
00035 #include "btThreadSupportInterface.h"
00036 
00037 
00038 typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
00039 typedef void* (*PosixlsMemorySetupFunc)();
00040 
00041 // PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
00042 class PosixThreadSupport : public btThreadSupportInterface 
00043 {
00044 public:
00045     typedef enum sStatus {
00046         STATUS_BUSY,
00047         STATUS_READY,
00048         STATUS_FINISHED
00049     } Status;
00050 
00051         // placeholder, until libspe2 support is there
00052         struct  btSpuStatus
00053         {
00054                 uint32_t        m_taskId;
00055                 uint32_t        m_commandId;
00056                 uint32_t        m_status;
00057 
00058                 PosixThreadFunc m_userThreadFunc;
00059                 void*   m_userPtr; //for taskDesc etc
00060                 void*   m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
00061 
00062                 pthread_t thread;
00063                 sem_t* startSemaphore;
00064 
00065         unsigned long threadUsed;
00066         };
00067 private:
00068 
00069         btAlignedObjectArray<btSpuStatus>       m_activeSpuStatus;
00070 public:
00072 
00073         
00074 
00075         struct  ThreadConstructionInfo
00076         {
00077                 ThreadConstructionInfo(const char* uniqueName,
00078                                                                         PosixThreadFunc userThreadFunc,
00079                                                                         PosixlsMemorySetupFunc  lsMemoryFunc,
00080                                                                         int numThreads=1,
00081                                                                         int threadStackSize=65535
00082                                                                         )
00083                                                                         :m_uniqueName(uniqueName),
00084                                                                         m_userThreadFunc(userThreadFunc),
00085                                                                         m_lsMemoryFunc(lsMemoryFunc),
00086                                                                         m_numThreads(numThreads),
00087                                                                         m_threadStackSize(threadStackSize)
00088                 {
00089 
00090                 }
00091 
00092                 const char*                                     m_uniqueName;
00093                 PosixThreadFunc                 m_userThreadFunc;
00094                 PosixlsMemorySetupFunc  m_lsMemoryFunc;
00095                 int                                             m_numThreads;
00096                 int                                             m_threadStackSize;
00097 
00098         };
00099 
00100         PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
00101 
00103         virtual ~PosixThreadSupport();
00104 
00105         void    startThreads(ThreadConstructionInfo&    threadInfo);
00106 
00107 
00109         virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
00110 
00112         virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
00113 
00115         virtual void startSPU();
00116 
00118         virtual void stopSPU();
00119 
00120         virtual void setNumTasks(int numTasks) {}
00121 
00122         virtual int getNumTasks() const
00123         {
00124                 return m_activeSpuStatus.size();
00125         }
00126 
00127         virtual btBarrier* createBarrier();
00128 
00129         virtual btCriticalSection* createCriticalSection();
00130 
00131         virtual void deleteBarrier(btBarrier* barrier);
00132 
00133         virtual void deleteCriticalSection(btCriticalSection* criticalSection);
00134         
00135         
00136         virtual void*   getThreadLocalMemory(int taskId)
00137         {
00138                 return m_activeSpuStatus[taskId].m_lsMemory;
00139         }
00140 
00141 };
00142 
00143 #endif // USE_PTHREADS
00144 
00145 #endif // BT_POSIX_THREAD_SUPPORT_H
00146 
00147