TuttleOFX  1
ofxMultiThread.h
Go to the documentation of this file.
00001 #ifndef _ofxMultiThread_h_
00002 #define _ofxMultiThread_h_
00003 
00004 /*
00005  * Software License :
00006  *
00007  * Copyright (c) 2003-2009, The Open Effects Association Ltd. All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  * Redistributions of source code must retain the above copyright notice,
00013  *    this list of conditions and the following disclaimer.
00014  * Redistributions in binary form must reproduce the above copyright notice,
00015  *    this list of conditions and the following disclaimer in the documentation
00016  *    and/or other materials provided with the distribution.
00017  * Neither the name The Open Effects Association Ltd, nor the names of its
00018  *    contributors may be used to endorse or promote products derived from this
00019  *    software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00025  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00028  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 
00034 #include "ofxCore.h"
00035 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 /** @file ofxMultiThread.h
00041  *
00042  *  This file contains the Host Suite for threading
00043  */
00044 
00045 #define kOfxMultiThreadSuite "OfxMultiThreadSuite"
00046 
00047 /** @brief Mutex blind data handle
00048  */
00049 typedef struct OfxMutex* OfxMutexHandle;
00050 
00051 /** @brief The function type to passed to the multi threading routines
00052  *
00053  *  \arg \e threadIndex unique index of this thread, will be between 0 and threadMax
00054  *  \arg \e threadMax to total number of threads executing this function
00055  *  \arg \e customArg the argument passed into multiThread
00056  *
00057  * A function of this type is passed to OfxMultiThreadSuiteV1::multiThread to be launched in multiple threads.
00058  */
00059 typedef void ( OfxThreadFunctionV1 )( unsigned int threadIndex,
00060                                       unsigned int threadMax,
00061                                       void*        customArg );
00062 
00063 /** @brief OFX suite that provides simple SMP style multi-processing
00064  */
00065 typedef struct OfxMultiThreadSuiteV1
00066 {
00067         /**@brief Function to spawn SMP threads
00068          *
00069          * \arg func The function to call in each thread.
00070          * \arg nThreads The number of threads to launch
00071          * \arg customArg The paramter to pass to customArg of func in each thread.
00072          *
00073          * This function will spawn nThreads separate threads of computation (typically one per CPU)
00074          * to allow something to perform symmetric multi processing. Each thread will call 'func' passing
00075          * in the index of the thread and the number of threads actually launched.
00076          *
00077          * multiThread will not return until all the spawned threads have returned. It is up to the host
00078          * how it waits for all the threads to return (busy wait, blocking, whatever).
00079          *
00080          * \e nThreads can be more than the value returned by multiThreadNumCPUs, however the threads will
00081          * be limitted to the number of CPUs returned by multiThreadNumCPUs.
00082          *
00083          * This function cannot be called recursively.
00084          *
00085          * @returns
00086          * - ::kOfxStatOK, the function func has executed and returned sucessfully
00087          * - ::kOfxStatFailed, the threading function failed to launch
00088          * - ::kOfxStatErrExists, failed in an attempt to call multiThread recursively,
00089          *
00090          */
00091         OfxStatus ( *multiThread )( OfxThreadFunctionV1 func,
00092                                     const unsigned int  nThreads,
00093                                     void*               customArg );
00094 
00095         /**@brief Function which indicates the number of CPUs available for SMP processing
00096          *
00097          * \arg nCPUs pointer to an integer where the result is returned
00098          *
00099          * This value may be less than the actual number of CPUs on a machine, as the host may reserve other CPUs for itself.
00100          *
00101          * @returns
00102          * - ::kOfxStatOK, all was OK and the maximum number of threads is in nThreads.
00103          * - ::kOfxStatFailed, the function failed to get the number of CPUs
00104          */
00105         OfxStatus ( *multiThreadNumCPUs )( unsigned int* const nCPUs );
00106 
00107         /**@brief Function which indicates the index of the current thread
00108          *
00109          * \arg threadIndex  pointer to an integer where the result is returned
00110          *
00111          * This function returns the thread index, which is the same as the \e threadIndex argument passed to the ::OfxThreadFunctionV1.
00112          *
00113          * If there are no threads currently spawned, then this function will set threadIndex to 0
00114          *
00115          * @returns
00116          * - ::kOfxStatOK, all was OK and the maximum number of threads is in nThreads.
00117          * - ::kOfxStatFailed, the function failed to return an index
00118          */
00119         OfxStatus ( *multiThreadIndex )( unsigned int* const threadIndex );
00120 
00121         /**@brief Function to enquire if the calling thread was spawned by multiThread
00122          *
00123          * @returns
00124          * - 0 if the thread is not one spawned by multiThread
00125          * - 1 if the thread was spawned by multiThread
00126          */
00127         int ( *multiThreadIsSpawnedThread )( void );
00128 
00129         /** @brief Create a mutex
00130          *
00131          * \arg mutex - where the new handle is returned
00132          * \arg count - initial lock count on the mutex. This can be negative.
00133          *
00134          * Creates a new mutex with lockCount locks on the mutex intially set.
00135          *
00136          * @returns
00137          * - kOfxStatOK - mutex is now valid and ready to go
00138          */
00139         OfxStatus ( *mutexCreate )( OfxMutexHandle* mutex, const int lockCount ); ///@todo; ofxtuttle fix: no const on mutex
00140 
00141         /** @brief Destroy a mutex
00142          *
00143          * Destroys a mutex intially created by mutexCreate.
00144          *
00145          * @returns
00146          * - kOfxStatOK - if it destroyed the mutex
00147          * - kOfxStatErrBadHandle - if the handle was bad
00148          */
00149         OfxStatus ( *mutexDestroy )( OfxMutexHandle mutex ); ///@todo; ofxtuttle fix: no const on mutex
00150 
00151         /** @brief Blocking lock on the mutex
00152          *
00153          * This trys to lock a mutex and blocks the thread it is in until the lock suceeds.
00154          *
00155          * A sucessful lock causes the mutex's lock count to be increased by one and to block any other calls to lock the mutex until it is unlocked.
00156          *
00157          * @returns
00158          * - kOfxStatOK - if it got the lock
00159          * - kOfxStatErrBadHandle - if the handle was bad
00160          */
00161         OfxStatus ( *mutexLock )( OfxMutexHandle mutex ); ///@todo; ofxtuttle fix: no const on mutex
00162 
00163         /** @brief Unlock the mutex
00164          *
00165          * This  unlocks a mutex. Unlocking a mutex decreases its lock count by one.
00166          *
00167          * @returns
00168          * - kOfxStatOK if it released the lock
00169          * - kOfxStatErrBadHandle if the handle was bad
00170          */
00171         OfxStatus ( *mutexUnLock )( OfxMutexHandle mutex ); ///@todo; ofxtuttle fix: no const on mutex
00172 
00173         /** @brief Non blocking attempt to lock the mutex
00174          *
00175          * This attempts to lock a mutex, if it cannot, it returns and says so, rather than blocking.
00176          *
00177          * A sucessful lock causes the mutex's lock count to be increased by one, if the lock did not suceed, the call returns immediately and the lock count remains unchanged.
00178          *
00179          * @returns
00180          * - kOfxStatOK - if it got the lock
00181          * - kOfxStatFailed - if it did not get the lock
00182          * - kOfxStatErrBadHandle - if the handle was bad
00183          */
00184         OfxStatus ( *mutexTryLock )( OfxMutexHandle mutex ); ///@todo; ofxtuttle fix: no const on mutex
00185 
00186 } OfxMultiThreadSuiteV1;
00187 
00188 #ifdef __cplusplus
00189 }
00190 #endif
00191 
00192 
00193 #endif