TuttleOFX
1
|
00001 #ifndef _ofxsMultiThread_H_ 00002 #define _ofxsMultiThread_H_ 00003 /* 00004 * OFX Support Library, a library that skins the OFX plug-in API with C++ classes. 00005 * Copyright (C) 2004-2007 The Open Effects Association Ltd 00006 * Author Bruno Nicoletti bruno@thefoundry.co.uk 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the following disclaimer. 00013 * Redistributions in binary form must reproduce the above copyright notice, 00014 * this list of conditions and the following disclaimer in the documentation 00015 * and/or other materials provided with the distribution. 00016 * Neither the name The Open Effects Association Ltd, nor the names of its 00017 * contributors may be used to endorse or promote products derived from this 00018 * software without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00023 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00024 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00027 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 * 00031 * The Open Effects Association Ltd 00032 * 1 Wardour St 00033 * London W1D 6PA 00034 * England 00035 * 00036 * 00037 * 00038 */ 00039 00040 /** @file This file contains core code that wraps OFX 'objects' with C++ classes. 00041 * 00042 * This file only holds code that is visible to a plugin implementation, and so hides much 00043 * of the direct OFX objects and any library side only functions. 00044 */ 00045 00046 #include "ofxsCore.h" 00047 00048 typedef struct OfxMutex* OfxMutexHandle; 00049 00050 namespace OFX { 00051 00052 /** @brief Multi thread namespace */ 00053 namespace MultiThread { 00054 00055 /** @brief Class that wraps up SMP multi-processing */ 00056 class Processor 00057 { 00058 private: 00059 /** @brief Function to pass to the multi thread suite */ 00060 static void staticMultiThreadFunction( unsigned int threadIndex, unsigned int threadMax, void* customArg ); 00061 00062 public: 00063 Processor(); 00064 virtual ~Processor(); 00065 00066 /** @brief function that will be called in each thread. ID is from 0..nThreads-1 nThreads are the number of threads it is being run over */ 00067 virtual void multiThreadFunction( const unsigned int threadID, const unsigned int nThreads ) = 0; 00068 00069 /** @brief call this to kick off multi threading 00070 * 00071 * The nCPUs is 0, the maximum allowable number of CPUs will be used. 00072 */ 00073 virtual void multiThread( const unsigned int nCPUs = 0 ); 00074 }; 00075 00076 /** @brief Has the current thread been spawned from an MP */ 00077 bool isSpawnedThread( void ); 00078 00079 /** @brief The number of CPUs that can be used for MP-ing */ 00080 unsigned int getNumCPUs( void ); 00081 00082 /** @brief The index of the current thread. From 0 to numCPUs() - 1 */ 00083 unsigned int getThreadIndex( void ); 00084 00085 /** @brief An OFX mutex */ 00086 class Mutex 00087 { 00088 protected: 00089 OfxMutexHandle _handle; /**< @brief The handle */ 00090 00091 public: 00092 /** @brief ctor */ 00093 Mutex( int lockCount = 0 ); 00094 00095 /** @brief dtor */ 00096 virtual ~Mutex( void ); 00097 00098 /** @brief lock it, blocks until lock is gained */ 00099 void lock(); 00100 00101 /** @brief unlock it */ 00102 void unlock(); 00103 00104 /** @brief attempt to lock, non-blocking 00105 * 00106 * \brief returns 00107 * - true if the lock was achieved 00108 * - false if it could not 00109 */ 00110 bool tryLock(); 00111 }; 00112 00113 /// a class to wrap around a mutex which is exception safe 00114 /// it locks the mutex on construction and unlocks it on destruction 00115 class AutoMutex 00116 { 00117 protected: 00118 Mutex& _mutex; 00119 00120 public: 00121 /// ctor, acquires the lock 00122 explicit AutoMutex( Mutex& m ) 00123 : _mutex( m ) 00124 { 00125 _mutex.lock(); 00126 } 00127 00128 /// dtor, releases the lock 00129 virtual ~AutoMutex() 00130 { 00131 _mutex.unlock(); 00132 } 00133 00134 }; 00135 }; 00136 }; 00137 00138 #endif